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 1393793999 Lines: 272 356 76.4 %
Date: 2014-03-02 Functions: 8 8 100.0 %
Branches: 136 200 68.0 %

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

Generated by: LCOV version 1.9