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 1388338050 Lines: 243 341 71.3 %
Date: 2013-12-29 Functions: 8 8 100.0 %
Branches: 111 186 59.7 %

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

Generated by: LCOV version 1.9