LCOV - code coverage report
Current view: top level - src/p2p - p2p_invitation.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1401264779 Lines: 273 364 75.0 %
Date: 2014-05-28 Functions: 8 8 100.0 %

          Line data    Source code
       1             : /*
       2             :  * Wi-Fi Direct - P2P Invitation procedure
       3             :  * Copyright (c) 2010, Atheros Communications
       4             :  *
       5             :  * This software may be distributed under the terms of the BSD license.
       6             :  * See README for more details.
       7             :  */
       8             : 
       9             : #include "includes.h"
      10             : 
      11             : #include "common.h"
      12             : #include "common/ieee802_11_defs.h"
      13             : #include "p2p_i.h"
      14             : #include "p2p.h"
      15             : 
      16             : 
      17          26 : static struct wpabuf * p2p_build_invitation_req(struct p2p_data *p2p,
      18             :                                                 struct p2p_device *peer,
      19             :                                                 const u8 *go_dev_addr,
      20             :                                                 int dev_pw_id)
      21             : {
      22             :         struct wpabuf *buf;
      23             :         u8 *len;
      24             :         const u8 *dev_addr;
      25          26 :         size_t extra = 0;
      26             : 
      27             : #ifdef CONFIG_WIFI_DISPLAY
      28          26 :         struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
      29          26 :         if (wfd_ie && p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
      30             :                 size_t i;
      31           0 :                 for (i = 0; i < p2p->num_groups; i++) {
      32           0 :                         struct p2p_group *g = p2p->groups[i];
      33             :                         struct wpabuf *ie;
      34           0 :                         if (os_memcmp(p2p_group_get_interface_addr(g),
      35             :                                       p2p->inv_bssid, ETH_ALEN) != 0)
      36           0 :                                 continue;
      37           0 :                         ie = p2p_group_get_wfd_ie(g);
      38           0 :                         if (ie) {
      39           0 :                                 wfd_ie = ie;
      40           0 :                                 break;
      41             :                         }
      42             :                 }
      43             :         }
      44          26 :         if (wfd_ie)
      45           0 :                 extra = wpabuf_len(wfd_ie);
      46             : #endif /* CONFIG_WIFI_DISPLAY */
      47             : 
      48          26 :         buf = wpabuf_alloc(1000 + extra);
      49          26 :         if (buf == NULL)
      50           0 :                 return NULL;
      51             : 
      52          26 :         peer->dialog_token++;
      53          26 :         if (peer->dialog_token == 0)
      54           0 :                 peer->dialog_token = 1;
      55          26 :         p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_REQ,
      56          26 :                                       peer->dialog_token);
      57             : 
      58          26 :         len = p2p_buf_add_ie_hdr(buf);
      59          26 :         if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO || !p2p->inv_persistent)
      60           7 :                 p2p_buf_add_config_timeout(buf, 0, 0);
      61             :         else
      62          19 :                 p2p_buf_add_config_timeout(buf, p2p->go_timeout,
      63          19 :                                            p2p->client_timeout);
      64          26 :         p2p_buf_add_invitation_flags(buf, p2p->inv_persistent ?
      65             :                                      P2P_INVITATION_FLAGS_TYPE : 0);
      66          36 :         if (p2p->inv_role != P2P_INVITE_ROLE_CLIENT ||
      67          10 :             !(peer->flags & P2P_DEV_NO_PREF_CHAN))
      68          38 :                 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
      69          19 :                                               p2p->op_reg_class,
      70          19 :                                               p2p->op_channel);
      71          26 :         if (p2p->inv_bssid_set)
      72          16 :                 p2p_buf_add_group_bssid(buf, p2p->inv_bssid);
      73          26 :         p2p_buf_add_channel_list(buf, p2p->cfg->country, &p2p->channels);
      74          26 :         if (go_dev_addr)
      75           7 :                 dev_addr = go_dev_addr;
      76          19 :         else if (p2p->inv_role == P2P_INVITE_ROLE_CLIENT)
      77          10 :                 dev_addr = peer->info.p2p_device_addr;
      78             :         else
      79           9 :                 dev_addr = p2p->cfg->dev_addr;
      80          26 :         p2p_buf_add_group_id(buf, dev_addr, p2p->inv_ssid, p2p->inv_ssid_len);
      81          26 :         p2p_buf_add_device_info(buf, p2p, peer);
      82          26 :         p2p_buf_update_ie_hdr(buf, len);
      83             : 
      84             : #ifdef CONFIG_WIFI_DISPLAY
      85          26 :         if (wfd_ie)
      86           0 :                 wpabuf_put_buf(buf, wfd_ie);
      87             : #endif /* CONFIG_WIFI_DISPLAY */
      88             : 
      89          26 :         if (dev_pw_id >= 0) {
      90             :                 /* WSC IE in Invitation Request for NFC static handover */
      91           2 :                 p2p_build_wps_ie(p2p, buf, dev_pw_id, 0);
      92             :         }
      93             : 
      94          26 :         return buf;
      95             : }
      96             : 
      97             : 
      98          38 : static struct wpabuf * p2p_build_invitation_resp(struct p2p_data *p2p,
      99             :                                                  struct p2p_device *peer,
     100             :                                                  u8 dialog_token, u8 status,
     101             :                                                  const u8 *group_bssid,
     102             :                                                  u8 reg_class, u8 channel,
     103             :                                                  struct p2p_channels *channels)
     104             : {
     105             :         struct wpabuf *buf;
     106             :         u8 *len;
     107          38 :         size_t extra = 0;
     108             : 
     109             : #ifdef CONFIG_WIFI_DISPLAY
     110          38 :         struct wpabuf *wfd_ie = p2p->wfd_ie_invitation;
     111          38 :         if (wfd_ie && group_bssid) {
     112             :                 size_t i;
     113           0 :                 for (i = 0; i < p2p->num_groups; i++) {
     114           0 :                         struct p2p_group *g = p2p->groups[i];
     115             :                         struct wpabuf *ie;
     116           0 :                         if (os_memcmp(p2p_group_get_interface_addr(g),
     117             :                                       group_bssid, ETH_ALEN) != 0)
     118           0 :                                 continue;
     119           0 :                         ie = p2p_group_get_wfd_ie(g);
     120           0 :                         if (ie) {
     121           0 :                                 wfd_ie = ie;
     122           0 :                                 break;
     123             :                         }
     124             :                 }
     125             :         }
     126          38 :         if (wfd_ie)
     127           0 :                 extra = wpabuf_len(wfd_ie);
     128             : #endif /* CONFIG_WIFI_DISPLAY */
     129             : 
     130          38 :         buf = wpabuf_alloc(1000 + extra);
     131          38 :         if (buf == NULL)
     132           0 :                 return NULL;
     133             : 
     134          38 :         p2p_buf_add_public_action_hdr(buf, P2P_INVITATION_RESP,
     135             :                                       dialog_token);
     136             : 
     137          38 :         len = p2p_buf_add_ie_hdr(buf);
     138          38 :         p2p_buf_add_status(buf, status);
     139          38 :         p2p_buf_add_config_timeout(buf, 0, 0); /* FIX */
     140          38 :         if (reg_class && channel)
     141          20 :                 p2p_buf_add_operating_channel(buf, p2p->cfg->country,
     142             :                                               reg_class, channel);
     143          38 :         if (group_bssid)
     144           9 :                 p2p_buf_add_group_bssid(buf, group_bssid);
     145          38 :         if (channels)
     146          20 :                 p2p_buf_add_channel_list(buf, p2p->cfg->country, channels);
     147          38 :         p2p_buf_update_ie_hdr(buf, len);
     148             : 
     149             : #ifdef CONFIG_WIFI_DISPLAY
     150          38 :         if (wfd_ie)
     151           0 :                 wpabuf_put_buf(buf, wfd_ie);
     152             : #endif /* CONFIG_WIFI_DISPLAY */
     153             : 
     154          38 :         return buf;
     155             : }
     156             : 
     157             : 
     158          67 : void p2p_process_invitation_req(struct p2p_data *p2p, const u8 *sa,
     159             :                                 const u8 *data, size_t len, int rx_freq)
     160             : {
     161             :         struct p2p_device *dev;
     162             :         struct p2p_message msg;
     163          67 :         struct wpabuf *resp = NULL;
     164          67 :         u8 status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
     165             :         int freq;
     166          67 :         int go = 0;
     167             :         u8 group_bssid[ETH_ALEN], *bssid;
     168          67 :         int op_freq = 0;
     169          67 :         u8 reg_class = 0, channel = 0;
     170          67 :         struct p2p_channels intersection, *channels = NULL;
     171             :         int persistent;
     172             : 
     173          67 :         os_memset(group_bssid, 0, sizeof(group_bssid));
     174             : 
     175         402 :         p2p_dbg(p2p, "Received Invitation Request from " MACSTR " (freq=%d)",
     176         402 :                 MAC2STR(sa), rx_freq);
     177             : 
     178          67 :         if (p2p_parse(data, len, &msg))
     179          96 :                 return;
     180             : 
     181          38 :         dev = p2p_get_device(p2p, sa);
     182          38 :         if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
     183          36 :                 p2p_dbg(p2p, "Invitation Request from unknown peer " MACSTR,
     184          36 :                         MAC2STR(sa));
     185             : 
     186           6 :                 if (p2p_add_device(p2p, sa, rx_freq, NULL, 0, data + 1, len - 1,
     187             :                                    0)) {
     188           6 :                         p2p_dbg(p2p, "Invitation Request add device failed "
     189           6 :                                 MACSTR, MAC2STR(sa));
     190           1 :                         status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
     191           1 :                         goto fail;
     192             :                 }
     193             : 
     194           5 :                 dev = p2p_get_device(p2p, sa);
     195           5 :                 if (dev == NULL) {
     196           0 :                         p2p_dbg(p2p, "Reject Invitation Request from unknown peer "
     197           0 :                                 MACSTR, MAC2STR(sa));
     198           0 :                         status = P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
     199           0 :                         goto fail;
     200             :                 }
     201             :         }
     202             : 
     203          37 :         if (!msg.group_id || !msg.channel_list) {
     204          18 :                 p2p_dbg(p2p, "Mandatory attribute missing in Invitation Request from "
     205          18 :                         MACSTR, MAC2STR(sa));
     206           3 :                 status = P2P_SC_FAIL_INVALID_PARAMS;
     207           3 :                 goto fail;
     208             :         }
     209             : 
     210          34 :         if (msg.invitation_flags)
     211          33 :                 persistent = *msg.invitation_flags & P2P_INVITATION_FLAGS_TYPE;
     212             :         else {
     213             :                 /* Invitation Flags is a mandatory attribute starting from P2P
     214             :                  * spec 1.06. As a backwards compatibility mechanism, assume
     215             :                  * the request was for a persistent group if the attribute is
     216             :                  * missing.
     217             :                  */
     218           1 :                 p2p_dbg(p2p, "Mandatory Invitation Flags attribute missing from Invitation Request");
     219           1 :                 persistent = 1;
     220             :         }
     221             : 
     222          34 :         if (p2p_peer_channels_check(p2p, &p2p->cfg->channels, dev,
     223          34 :                                     msg.channel_list, msg.channel_list_len) <
     224             :             0) {
     225           1 :                 p2p_dbg(p2p, "No common channels found");
     226           1 :                 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
     227           1 :                 goto fail;
     228             :         }
     229             : 
     230          33 :         p2p_channels_intersect(&p2p->cfg->channels, &dev->channels,
     231             :                                &intersection);
     232             : 
     233          33 :         if (p2p->cfg->invitation_process) {
     234         167 :                 status = p2p->cfg->invitation_process(
     235          33 :                         p2p->cfg->cb_ctx, sa, msg.group_bssid, msg.group_id,
     236          66 :                         msg.group_id + ETH_ALEN, msg.group_id_len - ETH_ALEN,
     237             :                         &go, group_bssid, &op_freq, persistent, &intersection,
     238          35 :                         msg.dev_password_id_present ? msg.dev_password_id : -1);
     239             :         }
     240             : 
     241          33 :         if (op_freq) {
     242           2 :                 p2p_dbg(p2p, "Invitation processing forced frequency %d MHz",
     243             :                         op_freq);
     244           2 :                 if (p2p_freq_to_channel(op_freq, &reg_class, &channel) < 0) {
     245           0 :                         p2p_dbg(p2p, "Unknown forced freq %d MHz from invitation_process()",
     246             :                                 op_freq);
     247           0 :                         status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
     248           0 :                         goto fail;
     249             :                 }
     250             : 
     251           2 :                 if (!p2p_channels_includes(&intersection, reg_class, channel))
     252             :                 {
     253           0 :                         p2p_dbg(p2p, "forced freq %d MHz not in the supported channels interaction",
     254             :                                 op_freq);
     255           0 :                         status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
     256           0 :                         goto fail;
     257             :                 }
     258             : 
     259           2 :                 if (status == P2P_SC_SUCCESS)
     260           2 :                         channels = &intersection;
     261             :         } else {
     262          31 :                 p2p_dbg(p2p, "No forced channel from invitation processing - figure out best one to use");
     263             : 
     264             :                 /* Default to own configuration as a starting point */
     265          31 :                 p2p->op_reg_class = p2p->cfg->op_reg_class;
     266          31 :                 p2p->op_channel = p2p->cfg->op_channel;
     267          62 :                 p2p_dbg(p2p, "Own default op_class %d channel %d",
     268          62 :                         p2p->op_reg_class, p2p->op_channel);
     269             : 
     270             :                 /* Use peer preference if specified and compatible */
     271          31 :                 if (msg.operating_channel) {
     272             :                         int req_freq;
     273          50 :                         req_freq = p2p_channel_to_freq(
     274          25 :                                 msg.operating_channel[3],
     275          25 :                                 msg.operating_channel[4]);
     276          25 :                         p2p_dbg(p2p, "Peer operating channel preference: %d MHz",
     277             :                                 req_freq);
     278          50 :                         if (req_freq > 0 &&
     279          50 :                             p2p_channels_includes(&intersection,
     280          25 :                                                   msg.operating_channel[3],
     281          25 :                                                   msg.operating_channel[4])) {
     282          25 :                                 p2p->op_reg_class = msg.operating_channel[3];
     283          25 :                                 p2p->op_channel = msg.operating_channel[4];
     284          50 :                                 p2p_dbg(p2p, "Use peer preference op_class %d channel %d",
     285          50 :                                         p2p->op_reg_class, p2p->op_channel);
     286             :                         } else {
     287           0 :                                 p2p_dbg(p2p, "Cannot use peer channel preference");
     288             :                         }
     289             :                 }
     290             : 
     291             :                 /* Reselect the channel only for the case of the GO */
     292          38 :                 if (go &&
     293           7 :                     !p2p_channels_includes(&intersection, p2p->op_reg_class,
     294           7 :                                            p2p->op_channel)) {
     295           0 :                         p2p_dbg(p2p, "Initially selected channel (op_class %d channel %d) not in channel intersection - try to reselect",
     296           0 :                                 p2p->op_reg_class, p2p->op_channel);
     297           0 :                         p2p_reselect_channel(p2p, &intersection);
     298           0 :                         p2p_dbg(p2p, "Re-selection result: op_class %d channel %d",
     299           0 :                                 p2p->op_reg_class, p2p->op_channel);
     300           0 :                         if (!p2p_channels_includes(&intersection,
     301           0 :                                                    p2p->op_reg_class,
     302           0 :                                                    p2p->op_channel)) {
     303           0 :                                 p2p_dbg(p2p, "Peer does not support selected operating channel (reg_class=%u channel=%u)",
     304           0 :                                         p2p->op_reg_class, p2p->op_channel);
     305           0 :                                 status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
     306           0 :                                 goto fail;
     307             :                         }
     308          37 :                 } else if (go && !(dev->flags & P2P_DEV_FORCE_FREQ) &&
     309           6 :                            !p2p->cfg->cfg_op_channel) {
     310          12 :                         p2p_dbg(p2p, "Try to reselect channel selection with peer information received; previously selected op_class %u channel %u",
     311          12 :                                 p2p->op_reg_class, p2p->op_channel);
     312           6 :                         p2p_reselect_channel(p2p, &intersection);
     313             :                 }
     314             : 
     315          31 :                 op_freq = p2p_channel_to_freq(p2p->op_reg_class,
     316          31 :                                               p2p->op_channel);
     317          31 :                 if (op_freq < 0) {
     318           0 :                         p2p_dbg(p2p, "Unknown operational channel (country=%c%c reg_class=%u channel=%u)",
     319           0 :                                 p2p->cfg->country[0], p2p->cfg->country[1],
     320           0 :                                 p2p->op_reg_class, p2p->op_channel);
     321           0 :                         status = P2P_SC_FAIL_NO_COMMON_CHANNELS;
     322           0 :                         goto fail;
     323             :                 }
     324          31 :                 p2p_dbg(p2p, "Selected operating channel - %d MHz", op_freq);
     325             : 
     326          31 :                 if (status == P2P_SC_SUCCESS) {
     327          18 :                         reg_class = p2p->op_reg_class;
     328          18 :                         channel = p2p->op_channel;
     329          18 :                         channels = &intersection;
     330             :                 }
     331             :         }
     332             : 
     333             : fail:
     334          38 :         if (go && status == P2P_SC_SUCCESS && !is_zero_ether_addr(group_bssid))
     335           9 :                 bssid = group_bssid;
     336             :         else
     337          29 :                 bssid = NULL;
     338          38 :         resp = p2p_build_invitation_resp(p2p, dev, msg.dialog_token, status,
     339             :                                          bssid, reg_class, channel, channels);
     340             : 
     341          38 :         if (resp == NULL)
     342           0 :                 goto out;
     343             : 
     344          38 :         if (rx_freq > 0)
     345          38 :                 freq = rx_freq;
     346             :         else
     347           0 :                 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
     348           0 :                                            p2p->cfg->channel);
     349          38 :         if (freq < 0) {
     350           0 :                 p2p_dbg(p2p, "Unknown regulatory class/channel");
     351           0 :                 goto out;
     352             :         }
     353             : 
     354             :         /*
     355             :          * Store copy of invitation data to be used when processing TX status
     356             :          * callback for the Acton frame.
     357             :          */
     358          38 :         os_memcpy(p2p->inv_sa, sa, ETH_ALEN);
     359          38 :         if (msg.group_bssid) {
     360          26 :                 os_memcpy(p2p->inv_group_bssid, msg.group_bssid, ETH_ALEN);
     361          26 :                 p2p->inv_group_bssid_ptr = p2p->inv_group_bssid;
     362             :         } else
     363          12 :                 p2p->inv_group_bssid_ptr = NULL;
     364          38 :         if (msg.group_id) {
     365          35 :                 if (msg.group_id_len - ETH_ALEN <= 32) {
     366          35 :                         os_memcpy(p2p->inv_ssid, msg.group_id + ETH_ALEN,
     367             :                                   msg.group_id_len - ETH_ALEN);
     368          35 :                         p2p->inv_ssid_len = msg.group_id_len - ETH_ALEN;
     369             :                 }
     370          35 :                 os_memcpy(p2p->inv_go_dev_addr, msg.group_id, ETH_ALEN);
     371             :         } else {
     372           3 :                 p2p->inv_ssid_len = 0;
     373           3 :                 os_memset(p2p->inv_go_dev_addr, 0, ETH_ALEN);
     374             :         }
     375          38 :         p2p->inv_status = status;
     376          38 :         p2p->inv_op_freq = op_freq;
     377             : 
     378          38 :         p2p->pending_action_state = P2P_PENDING_INVITATION_RESPONSE;
     379          76 :         if (p2p_send_action(p2p, freq, sa, p2p->cfg->dev_addr,
     380          38 :                             p2p->cfg->dev_addr,
     381          38 :                             wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
     382           0 :                 p2p_dbg(p2p, "Failed to send Action frame");
     383             :         }
     384             : 
     385             : out:
     386          38 :         wpabuf_free(resp);
     387          38 :         p2p_parse_free(&msg);
     388             : }
     389             : 
     390             : 
     391          25 : void p2p_process_invitation_resp(struct p2p_data *p2p, const u8 *sa,
     392             :                                  const u8 *data, size_t len)
     393             : {
     394             :         struct p2p_device *dev;
     395             :         struct p2p_message msg;
     396          25 :         struct p2p_channels intersection, *channels = NULL;
     397             : 
     398         150 :         p2p_dbg(p2p, "Received Invitation Response from " MACSTR,
     399         150 :                 MAC2STR(sa));
     400             : 
     401          25 :         dev = p2p_get_device(p2p, sa);
     402          25 :         if (dev == NULL) {
     403           0 :                 p2p_dbg(p2p, "Ignore Invitation Response from unknown peer "
     404           0 :                         MACSTR, MAC2STR(sa));
     405           0 :                 return;
     406             :         }
     407             : 
     408          25 :         if (dev != p2p->invite_peer) {
     409           0 :                 p2p_dbg(p2p, "Ignore unexpected Invitation Response from peer "
     410           0 :                         MACSTR, MAC2STR(sa));
     411           0 :                 return;
     412             :         }
     413             : 
     414          25 :         if (p2p_parse(data, len, &msg))
     415           0 :                 return;
     416             : 
     417          25 :         if (!msg.status) {
     418           0 :                 p2p_dbg(p2p, "Mandatory Status attribute missing in Invitation Response from "
     419           0 :                         MACSTR, MAC2STR(sa));
     420           0 :                 p2p_parse_free(&msg);
     421           0 :                 return;
     422             :         }
     423             : 
     424          25 :         if (!msg.channel_list && *msg.status == P2P_SC_SUCCESS) {
     425           0 :                 p2p_dbg(p2p, "Mandatory Channel List attribute missing in Invitation Response from "
     426           0 :                         MACSTR, MAC2STR(sa));
     427             : #ifdef CONFIG_P2P_STRICT
     428             :                 p2p_parse_free(&msg);
     429             :                 return;
     430             : #endif /* CONFIG_P2P_STRICT */
     431             :                 /* Try to survive without peer channel list */
     432           0 :                 channels = &p2p->channels;
     433          25 :         } else if (!msg.channel_list) {
     434             :                 /* Non-success cases are not required to include Channel List */
     435           6 :                 channels = &p2p->channels;
     436          19 :         } else if (p2p_peer_channels_check(p2p, &p2p->channels, dev,
     437             :                                            msg.channel_list,
     438          19 :                                            msg.channel_list_len) < 0) {
     439           0 :                 p2p_dbg(p2p, "No common channels found");
     440           0 :                 p2p_parse_free(&msg);
     441           0 :                 return;
     442             :         } else {
     443          19 :                 p2p_channels_intersect(&p2p->channels, &dev->channels,
     444             :                                        &intersection);
     445          19 :                 channels = &intersection;
     446             :         }
     447             : 
     448          25 :         if (p2p->cfg->invitation_result) {
     449          25 :                 int peer_oper_freq = 0;
     450          25 :                 int freq = p2p_channel_to_freq(p2p->op_reg_class,
     451          25 :                                                p2p->op_channel);
     452          25 :                 if (freq < 0)
     453           0 :                         freq = 0;
     454             : 
     455          25 :                 if (msg.operating_channel) {
     456          38 :                         peer_oper_freq = p2p_channel_to_freq(
     457          19 :                                 msg.operating_channel[3],
     458          19 :                                 msg.operating_channel[4]);
     459          19 :                         if (peer_oper_freq < 0)
     460           0 :                                 peer_oper_freq = 0;
     461             :                 }
     462             : 
     463          25 :                 p2p->cfg->invitation_result(p2p->cfg->cb_ctx, *msg.status,
     464             :                                             msg.group_bssid, channels, sa,
     465             :                                             freq, peer_oper_freq);
     466             :         }
     467             : 
     468          25 :         p2p_parse_free(&msg);
     469             : 
     470          25 :         p2p_clear_timeout(p2p);
     471          25 :         p2p_set_state(p2p, P2P_IDLE);
     472          25 :         p2p->invite_peer = NULL;
     473             : }
     474             : 
     475             : 
     476          26 : int p2p_invite_send(struct p2p_data *p2p, struct p2p_device *dev,
     477             :                     const u8 *go_dev_addr, int dev_pw_id)
     478             : {
     479             :         struct wpabuf *req;
     480             :         int freq;
     481             : 
     482          26 :         freq = dev->listen_freq > 0 ? dev->listen_freq : dev->oper_freq;
     483          26 :         if (freq <= 0)
     484           2 :                 freq = dev->oob_go_neg_freq;
     485          26 :         if (freq <= 0) {
     486           0 :                 p2p_dbg(p2p, "No Listen/Operating frequency known for the peer "
     487             :                         MACSTR " to send Invitation Request",
     488           0 :                         MAC2STR(dev->info.p2p_device_addr));
     489           0 :                 return -1;
     490             :         }
     491             : 
     492          26 :         req = p2p_build_invitation_req(p2p, dev, go_dev_addr, dev_pw_id);
     493          26 :         if (req == NULL)
     494           0 :                 return -1;
     495          26 :         if (p2p->state != P2P_IDLE)
     496           1 :                 p2p_stop_listen_for_freq(p2p, freq);
     497          26 :         p2p_dbg(p2p, "Sending Invitation Request");
     498          26 :         p2p_set_state(p2p, P2P_INVITE);
     499          26 :         p2p->pending_action_state = P2P_PENDING_INVITATION_REQUEST;
     500          26 :         p2p->invite_peer = dev;
     501          26 :         dev->invitation_reqs++;
     502          52 :         if (p2p_send_action(p2p, freq, dev->info.p2p_device_addr,
     503          26 :                             p2p->cfg->dev_addr, dev->info.p2p_device_addr,
     504          26 :                             wpabuf_head(req), wpabuf_len(req), 500) < 0) {
     505           0 :                 p2p_dbg(p2p, "Failed to send Action frame");
     506             :                 /* Use P2P find to recover and retry */
     507           0 :                 p2p_set_timeout(p2p, 0, 0);
     508             :         } else {
     509          26 :                 dev->flags |= P2P_DEV_WAIT_INV_REQ_ACK;
     510             :         }
     511             : 
     512          26 :         wpabuf_free(req);
     513             : 
     514          26 :         return 0;
     515             : }
     516             : 
     517             : 
     518          26 : void p2p_invitation_req_cb(struct p2p_data *p2p, int success)
     519             : {
     520          26 :         p2p_dbg(p2p, "Invitation Request TX callback: success=%d", success);
     521             : 
     522          26 :         if (p2p->invite_peer == NULL) {
     523           0 :                 p2p_dbg(p2p, "No pending Invite");
     524          26 :                 return;
     525             :         }
     526             : 
     527          26 :         if (success)
     528          25 :                 p2p->invite_peer->flags &= ~P2P_DEV_WAIT_INV_REQ_ACK;
     529             : 
     530             :         /*
     531             :          * Use P2P find, if needed, to find the other device from its listen
     532             :          * channel.
     533             :          */
     534          26 :         p2p_set_state(p2p, P2P_INVITE);
     535          26 :         p2p_set_timeout(p2p, 0, success ? 500000 : 100000);
     536             : }
     537             : 
     538             : 
     539          37 : void p2p_invitation_resp_cb(struct p2p_data *p2p, int success)
     540             : {
     541          37 :         p2p_dbg(p2p, "Invitation Response TX callback: success=%d", success);
     542          37 :         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
     543             : 
     544          37 :         if (!success)
     545           0 :                 p2p_dbg(p2p, "Assume Invitation Response was actually received by the peer even though Ack was not reported");
     546             : 
     547          37 :         if (p2p->cfg->invitation_received) {
     548         148 :                 p2p->cfg->invitation_received(p2p->cfg->cb_ctx,
     549          37 :                                               p2p->inv_sa,
     550          37 :                                               p2p->inv_group_bssid_ptr,
     551          37 :                                               p2p->inv_ssid, p2p->inv_ssid_len,
     552          37 :                                               p2p->inv_go_dev_addr,
     553          37 :                                               p2p->inv_status,
     554             :                                               p2p->inv_op_freq);
     555             :         }
     556          37 : }
     557             : 
     558             : 
     559          25 : int p2p_invite(struct p2p_data *p2p, const u8 *peer, enum p2p_invite_role role,
     560             :                const u8 *bssid, const u8 *ssid, size_t ssid_len,
     561             :                unsigned int force_freq, const u8 *go_dev_addr,
     562             :                int persistent_group, unsigned int pref_freq, int dev_pw_id)
     563             : {
     564             :         struct p2p_device *dev;
     565             : 
     566         150 :         p2p_dbg(p2p, "Request to invite peer " MACSTR " role=%d persistent=%d "
     567             :                 "force_freq=%u",
     568         150 :                 MAC2STR(peer), role, persistent_group, force_freq);
     569          25 :         if (bssid)
     570          15 :                 p2p_dbg(p2p, "Invitation for BSSID " MACSTR, MAC2STR(bssid));
     571          25 :         if (go_dev_addr) {
     572          36 :                 p2p_dbg(p2p, "Invitation for GO Device Address " MACSTR,
     573          36 :                         MAC2STR(go_dev_addr));
     574           6 :                 os_memcpy(p2p->invite_go_dev_addr_buf, go_dev_addr, ETH_ALEN);
     575           6 :                 p2p->invite_go_dev_addr = p2p->invite_go_dev_addr_buf;
     576             :         } else
     577          19 :                 p2p->invite_go_dev_addr = NULL;
     578          25 :         wpa_hexdump_ascii(MSG_DEBUG, "Invitation for SSID",
     579             :                           ssid, ssid_len);
     580          25 :         if (dev_pw_id >= 0) {
     581           2 :                 p2p_dbg(p2p, "Invitation to use Device Password ID %d",
     582             :                         dev_pw_id);
     583             :         }
     584          25 :         p2p->invite_dev_pw_id = dev_pw_id;
     585             : 
     586          25 :         dev = p2p_get_device(p2p, peer);
     587          27 :         if (dev == NULL || (dev->listen_freq <= 0 && dev->oper_freq <= 0 &&
     588           2 :                             dev->oob_go_neg_freq <= 0)) {
     589           0 :                 p2p_dbg(p2p, "Cannot invite unknown P2P Device " MACSTR,
     590           0 :                         MAC2STR(peer));
     591           0 :                 return -1;
     592             :         }
     593             : 
     594          25 :         if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
     595             :                                 role != P2P_INVITE_ROLE_CLIENT) < 0)
     596           0 :                 return -1;
     597             : 
     598          25 :         if (persistent_group && role == P2P_INVITE_ROLE_CLIENT && !force_freq &&
     599             :             !pref_freq)
     600           7 :                 dev->flags |= P2P_DEV_NO_PREF_CHAN;
     601             :         else
     602          18 :                 dev->flags &= ~P2P_DEV_NO_PREF_CHAN;
     603             : 
     604          25 :         if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
     605           0 :                 if (!(dev->info.dev_capab &
     606             :                       P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
     607           0 :                         p2p_dbg(p2p, "Cannot invite a P2P Device " MACSTR
     608             :                                 " that is in a group and is not discoverable",
     609           0 :                                 MAC2STR(peer));
     610             :                 }
     611             :                 /* TODO: use device discoverability request through GO */
     612             :         }
     613             : 
     614          25 :         dev->invitation_reqs = 0;
     615             : 
     616          25 :         if (p2p->state != P2P_IDLE)
     617           2 :                 p2p_stop_find(p2p);
     618             : 
     619          25 :         p2p->inv_role = role;
     620          25 :         p2p->inv_bssid_set = bssid != NULL;
     621          25 :         if (bssid)
     622          15 :                 os_memcpy(p2p->inv_bssid, bssid, ETH_ALEN);
     623          25 :         os_memcpy(p2p->inv_ssid, ssid, ssid_len);
     624          25 :         p2p->inv_ssid_len = ssid_len;
     625          25 :         p2p->inv_persistent = persistent_group;
     626          25 :         return p2p_invite_send(p2p, dev, go_dev_addr, dev_pw_id);
     627             : }

Generated by: LCOV version 1.10