LCOV - code coverage report
Current view: top level - src/p2p - p2p.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1393793999 Lines: 1768 2445 72.3 %
Date: 2014-03-02 Functions: 132 162 81.5 %
Branches: 762 1313 58.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * Wi-Fi Direct - P2P module
       3                 :            :  * Copyright (c) 2009-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 "eloop.h"
      13                 :            : #include "common/ieee802_11_defs.h"
      14                 :            : #include "common/ieee802_11_common.h"
      15                 :            : #include "wps/wps_i.h"
      16                 :            : #include "p2p_i.h"
      17                 :            : #include "p2p.h"
      18                 :            : 
      19                 :            : 
      20                 :            : static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
      21                 :            : static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
      22                 :            : static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
      23                 :            :                                      const u8 *sa, const u8 *data, size_t len,
      24                 :            :                                      int rx_freq);
      25                 :            : static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
      26                 :            :                                       const u8 *sa, const u8 *data,
      27                 :            :                                       size_t len);
      28                 :            : static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
      29                 :            : static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
      30                 :            : 
      31                 :            : 
      32                 :            : /*
      33                 :            :  * p2p_scan recovery timeout
      34                 :            :  *
      35                 :            :  * Many drivers are using 30 second timeout on scan results. Allow a bit larger
      36                 :            :  * timeout for this to avoid hitting P2P timeout unnecessarily.
      37                 :            :  */
      38                 :            : #define P2P_SCAN_TIMEOUT 35
      39                 :            : 
      40                 :            : /**
      41                 :            :  * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
      42                 :            :  * entries will be removed
      43                 :            :  */
      44                 :            : #ifndef P2P_PEER_EXPIRATION_AGE
      45                 :            : #define P2P_PEER_EXPIRATION_AGE 60
      46                 :            : #endif /* P2P_PEER_EXPIRATION_AGE */
      47                 :            : 
      48                 :            : #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
      49                 :            : 
      50                 :        144 : static void p2p_expire_peers(struct p2p_data *p2p)
      51                 :            : {
      52                 :            :         struct p2p_device *dev, *n;
      53                 :            :         struct os_reltime now;
      54                 :            :         size_t i;
      55                 :            : 
      56                 :        144 :         os_get_reltime(&now);
      57         [ +  + ]:        177 :         dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
      58         [ +  - ]:         33 :                 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
      59                 :         33 :                         continue;
      60                 :            : 
      61         [ #  # ]:          0 :                 if (dev == p2p->go_neg_peer) {
      62                 :            :                         /*
      63                 :            :                          * GO Negotiation is in progress with the peer, so
      64                 :            :                          * don't expire the peer entry until GO Negotiation
      65                 :            :                          * fails or times out.
      66                 :            :                          */
      67                 :          0 :                         continue;
      68                 :            :                 }
      69                 :            : 
      70   [ #  #  #  # ]:          0 :                 if (p2p->cfg->go_connected &&
      71                 :          0 :                     p2p->cfg->go_connected(p2p->cfg->cb_ctx,
      72                 :          0 :                                            dev->info.p2p_device_addr)) {
      73                 :            :                         /*
      74                 :            :                          * We are connected as a client to a group in which the
      75                 :            :                          * peer is the GO, so do not expire the peer entry.
      76                 :            :                          */
      77                 :          0 :                         os_get_reltime(&dev->last_seen);
      78                 :          0 :                         continue;
      79                 :            :                 }
      80                 :            : 
      81         [ #  # ]:          0 :                 for (i = 0; i < p2p->num_groups; i++) {
      82         [ #  # ]:          0 :                         if (p2p_group_is_client_connected(
      83                 :          0 :                                     p2p->groups[i], dev->info.p2p_device_addr))
      84                 :          0 :                                 break;
      85                 :            :                 }
      86         [ #  # ]:          0 :                 if (i < p2p->num_groups) {
      87                 :            :                         /*
      88                 :            :                          * The peer is connected as a client in a group where
      89                 :            :                          * we are the GO, so do not expire the peer entry.
      90                 :            :                          */
      91                 :          0 :                         os_get_reltime(&dev->last_seen);
      92                 :          0 :                         continue;
      93                 :            :                 }
      94                 :            : 
      95                 :          0 :                 p2p_dbg(p2p, "Expiring old peer entry " MACSTR,
      96                 :          0 :                         MAC2STR(dev->info.p2p_device_addr));
      97                 :          0 :                 dl_list_del(&dev->list);
      98                 :          0 :                 p2p_device_free(p2p, dev);
      99                 :            :         }
     100                 :        144 : }
     101                 :            : 
     102                 :            : 
     103                 :        144 : static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
     104                 :            : {
     105                 :        144 :         struct p2p_data *p2p = eloop_ctx;
     106                 :        144 :         p2p_expire_peers(p2p);
     107                 :        144 :         eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
     108                 :            :                                p2p_expiration_timeout, p2p, NULL);
     109                 :        144 : }
     110                 :            : 
     111                 :            : 
     112                 :      14434 : static const char * p2p_state_txt(int state)
     113                 :            : {
     114   [ +  +  +  +  :      14434 :         switch (state) {
          +  +  +  +  +  
             +  +  +  +  
                      - ]
     115                 :            :         case P2P_IDLE:
     116                 :       8672 :                 return "IDLE";
     117                 :            :         case P2P_SEARCH:
     118                 :       3284 :                 return "SEARCH";
     119                 :            :         case P2P_CONNECT:
     120                 :        809 :                 return "CONNECT";
     121                 :            :         case P2P_CONNECT_LISTEN:
     122                 :        196 :                 return "CONNECT_LISTEN";
     123                 :            :         case P2P_GO_NEG:
     124                 :        323 :                 return "GO_NEG";
     125                 :            :         case P2P_LISTEN_ONLY:
     126                 :        584 :                 return "LISTEN_ONLY";
     127                 :            :         case P2P_WAIT_PEER_CONNECT:
     128                 :         49 :                 return "WAIT_PEER_CONNECT";
     129                 :            :         case P2P_WAIT_PEER_IDLE:
     130                 :         44 :                 return "WAIT_PEER_IDLE";
     131                 :            :         case P2P_SD_DURING_FIND:
     132                 :        101 :                 return "SD_DURING_FIND";
     133                 :            :         case P2P_PROVISIONING:
     134                 :        270 :                 return "PROVISIONING";
     135                 :            :         case P2P_PD_DURING_FIND:
     136                 :         12 :                 return "PD_DURING_FIND";
     137                 :            :         case P2P_INVITE:
     138                 :         86 :                 return "INVITE";
     139                 :            :         case P2P_INVITE_LISTEN:
     140                 :          4 :                 return "INVITE_LISTEN";
     141                 :            :         default:
     142                 :      14434 :                 return "?";
     143                 :            :         }
     144                 :            : }
     145                 :            : 
     146                 :            : 
     147                 :          0 : const char * p2p_get_state_txt(struct p2p_data *p2p)
     148                 :            : {
     149                 :          0 :         return p2p_state_txt(p2p->state);
     150                 :            : }
     151                 :            : 
     152                 :            : 
     153                 :         28 : u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
     154                 :            : {
     155                 :         28 :         struct p2p_device *dev = NULL;
     156                 :            : 
     157 [ +  - ][ -  + ]:         28 :         if (!addr || !p2p)
     158                 :          0 :                 return 0;
     159                 :            : 
     160                 :         28 :         dev = p2p_get_device(p2p, addr);
     161         [ +  - ]:         28 :         if (dev)
     162                 :         28 :                 return dev->wps_prov_info;
     163                 :            :         else
     164                 :         28 :                 return 0;
     165                 :            : }
     166                 :            : 
     167                 :            : 
     168                 :         91 : void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
     169                 :            : {
     170                 :         91 :         struct p2p_device *dev = NULL;
     171                 :            : 
     172 [ +  - ][ -  + ]:         91 :         if (!addr || !p2p)
     173                 :         91 :                 return;
     174                 :            : 
     175                 :         91 :         dev = p2p_get_device(p2p, addr);
     176         [ +  + ]:         91 :         if (dev)
     177                 :         88 :                 dev->wps_prov_info = 0;
     178                 :            : }
     179                 :            : 
     180                 :            : 
     181                 :       4307 : void p2p_set_state(struct p2p_data *p2p, int new_state)
     182                 :            : {
     183                 :       4307 :         p2p_dbg(p2p, "State %s -> %s",
     184                 :       4307 :                 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
     185                 :       4307 :         p2p->state = new_state;
     186                 :       4307 : }
     187                 :            : 
     188                 :            : 
     189                 :       1131 : void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
     190                 :            : {
     191                 :       1131 :         p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec",
     192                 :       1131 :                 p2p_state_txt(p2p->state), sec, usec);
     193                 :       1131 :         eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
     194                 :       1131 :         eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
     195                 :       1131 : }
     196                 :            : 
     197                 :            : 
     198                 :       3334 : void p2p_clear_timeout(struct p2p_data *p2p)
     199                 :            : {
     200                 :       3334 :         p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state));
     201                 :       3334 :         eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
     202                 :       3334 : }
     203                 :            : 
     204                 :            : 
     205                 :          6 : void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
     206                 :            :                        int status)
     207                 :            : {
     208                 :            :         struct p2p_go_neg_results res;
     209                 :          6 :         p2p_clear_timeout(p2p);
     210                 :          6 :         p2p_set_state(p2p, P2P_IDLE);
     211         [ +  - ]:          6 :         if (p2p->go_neg_peer) {
     212                 :          6 :                 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
     213                 :          6 :                 p2p->go_neg_peer->wps_method = WPS_NOT_READY;
     214                 :          6 :                 p2p->go_neg_peer->oob_pw_id = 0;
     215                 :            :         }
     216                 :          6 :         p2p->go_neg_peer = NULL;
     217                 :            : 
     218                 :          6 :         os_memset(&res, 0, sizeof(res));
     219                 :          6 :         res.status = status;
     220         [ +  - ]:          6 :         if (peer) {
     221                 :          6 :                 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr,
     222                 :            :                           ETH_ALEN);
     223                 :          6 :                 os_memcpy(res.peer_interface_addr, peer->intended_addr,
     224                 :            :                           ETH_ALEN);
     225                 :            :         }
     226                 :          6 :         p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
     227                 :          6 : }
     228                 :            : 
     229                 :            : 
     230                 :        622 : static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc)
     231                 :            : {
     232                 :            :         unsigned int r, tu;
     233                 :            :         int freq;
     234                 :            :         struct wpabuf *ies;
     235                 :            : 
     236                 :        622 :         p2p_dbg(p2p, "Starting short listen state (state=%s)",
     237                 :        622 :                 p2p_state_txt(p2p->state));
     238                 :            : 
     239                 :        622 :         freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
     240         [ -  + ]:        622 :         if (freq < 0) {
     241                 :          0 :                 p2p_dbg(p2p, "Unknown regulatory class/channel");
     242                 :          0 :                 return;
     243                 :            :         }
     244                 :            : 
     245                 :        622 :         os_get_random((u8 *) &r, sizeof(r));
     246                 :       1244 :         tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
     247                 :        622 :               p2p->min_disc_int) * 100;
     248 [ -  + ][ #  # ]:        622 :         if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu)
     249                 :          0 :                 tu = p2p->max_disc_tu;
     250 [ +  + ][ -  + ]:        622 :         if (!dev_disc && tu < 100)
     251                 :          0 :                 tu = 100; /* Need to wait in non-device discovery use cases */
     252 [ +  - ][ -  + ]:        622 :         if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen)
     253                 :          0 :                 tu = p2p->cfg->max_listen * 1000 / 1024;
     254                 :            : 
     255         [ -  + ]:        622 :         if (tu == 0) {
     256                 :          0 :                 p2p_dbg(p2p, "Skip listen state since duration was 0 TU");
     257                 :          0 :                 p2p_set_timeout(p2p, 0, 0);
     258                 :          0 :                 return;
     259                 :            :         }
     260                 :            : 
     261                 :        622 :         p2p->pending_listen_freq = freq;
     262                 :        622 :         p2p->pending_listen_sec = 0;
     263                 :        622 :         p2p->pending_listen_usec = 1024 * tu;
     264                 :            : 
     265                 :        622 :         ies = p2p_build_probe_resp_ies(p2p);
     266         [ -  + ]:        622 :         if (ies == NULL)
     267                 :          0 :                 return;
     268                 :            : 
     269         [ -  + ]:        622 :         if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
     270                 :            :                     ies) < 0) {
     271                 :          0 :                 p2p_dbg(p2p, "Failed to start listen mode");
     272                 :          0 :                 p2p->pending_listen_freq = 0;
     273                 :            :         }
     274                 :        622 :         wpabuf_free(ies);
     275                 :            : }
     276                 :            : 
     277                 :            : 
     278                 :        153 : int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
     279                 :            : {
     280                 :            :         int freq;
     281                 :            :         struct wpabuf *ies;
     282                 :            : 
     283                 :        153 :         p2p_dbg(p2p, "Going to listen(only) state");
     284                 :            : 
     285                 :        153 :         freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
     286         [ -  + ]:        153 :         if (freq < 0) {
     287                 :          0 :                 p2p_dbg(p2p, "Unknown regulatory class/channel");
     288                 :          0 :                 return -1;
     289                 :            :         }
     290                 :            : 
     291                 :        153 :         p2p->pending_listen_freq = freq;
     292                 :        153 :         p2p->pending_listen_sec = timeout / 1000;
     293                 :        153 :         p2p->pending_listen_usec = (timeout % 1000) * 1000;
     294                 :            : 
     295         [ +  + ]:        153 :         if (p2p->p2p_scan_running) {
     296         [ -  + ]:          2 :                 if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
     297                 :          0 :                         p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen");
     298                 :          0 :                         return 0;
     299                 :            :                 }
     300                 :          2 :                 p2p_dbg(p2p, "p2p_scan running - delay start of listen state");
     301                 :          2 :                 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
     302                 :          2 :                 return 0;
     303                 :            :         }
     304                 :            : 
     305                 :        151 :         ies = p2p_build_probe_resp_ies(p2p);
     306         [ -  + ]:        151 :         if (ies == NULL)
     307                 :          0 :                 return -1;
     308                 :            : 
     309         [ -  + ]:        151 :         if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
     310                 :          0 :                 p2p_dbg(p2p, "Failed to start listen mode");
     311                 :          0 :                 p2p->pending_listen_freq = 0;
     312                 :          0 :                 wpabuf_free(ies);
     313                 :          0 :                 return -1;
     314                 :            :         }
     315                 :        151 :         wpabuf_free(ies);
     316                 :            : 
     317                 :        151 :         p2p_set_state(p2p, P2P_LISTEN_ONLY);
     318                 :            : 
     319                 :        153 :         return 0;
     320                 :            : }
     321                 :            : 
     322                 :            : 
     323                 :        151 : static void p2p_device_clear_reported(struct p2p_data *p2p)
     324                 :            : {
     325                 :            :         struct p2p_device *dev;
     326         [ +  + ]:        193 :         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
     327                 :         42 :                 dev->flags &= ~P2P_DEV_REPORTED;
     328                 :        151 : }
     329                 :            : 
     330                 :            : 
     331                 :            : /**
     332                 :            :  * p2p_get_device - Fetch a peer entry
     333                 :            :  * @p2p: P2P module context from p2p_init()
     334                 :            :  * @addr: P2P Device Address of the peer
     335                 :            :  * Returns: Pointer to the device entry or %NULL if not found
     336                 :            :  */
     337                 :       2525 : struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
     338                 :            : {
     339                 :            :         struct p2p_device *dev;
     340         [ +  + ]:       2761 :         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
     341         [ +  + ]:       2222 :                 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
     342                 :       1986 :                         return dev;
     343                 :            :         }
     344                 :       2525 :         return NULL;
     345                 :            : }
     346                 :            : 
     347                 :            : 
     348                 :            : /**
     349                 :            :  * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
     350                 :            :  * @p2p: P2P module context from p2p_init()
     351                 :            :  * @addr: P2P Interface Address of the peer
     352                 :            :  * Returns: Pointer to the device entry or %NULL if not found
     353                 :            :  */
     354                 :         80 : struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
     355                 :            :                                              const u8 *addr)
     356                 :            : {
     357                 :            :         struct p2p_device *dev;
     358         [ +  + ]:        175 :         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
     359         [ +  + ]:        106 :                 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
     360                 :         11 :                         return dev;
     361                 :            :         }
     362                 :         80 :         return NULL;
     363                 :            : }
     364                 :            : 
     365                 :            : 
     366                 :            : /**
     367                 :            :  * p2p_create_device - Create a peer entry
     368                 :            :  * @p2p: P2P module context from p2p_init()
     369                 :            :  * @addr: P2P Device Address of the peer
     370                 :            :  * Returns: Pointer to the device entry or %NULL on failure
     371                 :            :  *
     372                 :            :  * If there is already an entry for the peer, it will be returned instead of
     373                 :            :  * creating a new one.
     374                 :            :  */
     375                 :        910 : static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
     376                 :            :                                              const u8 *addr)
     377                 :            : {
     378                 :        910 :         struct p2p_device *dev, *oldest = NULL;
     379                 :        910 :         size_t count = 0;
     380                 :            : 
     381                 :        910 :         dev = p2p_get_device(p2p, addr);
     382         [ +  + ]:        910 :         if (dev)
     383                 :        645 :                 return dev;
     384                 :            : 
     385         [ +  + ]:        309 :         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
     386                 :         44 :                 count++;
     387   [ -  +  #  # ]:         44 :                 if (oldest == NULL ||
     388                 :          0 :                     os_reltime_before(&dev->last_seen, &oldest->last_seen))
     389                 :         44 :                         oldest = dev;
     390                 :            :         }
     391 [ -  + ][ #  # ]:        265 :         if (count + 1 > p2p->cfg->max_peers && oldest) {
     392                 :          0 :                 p2p_dbg(p2p, "Remove oldest peer entry to make room for a new peer");
     393                 :          0 :                 dl_list_del(&oldest->list);
     394                 :          0 :                 p2p_device_free(p2p, oldest);
     395                 :            :         }
     396                 :            : 
     397                 :        265 :         dev = os_zalloc(sizeof(*dev));
     398         [ -  + ]:        265 :         if (dev == NULL)
     399                 :          0 :                 return NULL;
     400                 :        265 :         dl_list_add(&p2p->devices, &dev->list);
     401                 :        265 :         os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
     402                 :            : 
     403                 :        910 :         return dev;
     404                 :            : }
     405                 :            : 
     406                 :            : 
     407                 :         21 : static void p2p_copy_client_info(struct p2p_device *dev,
     408                 :            :                                  struct p2p_client_info *cli)
     409                 :            : {
     410                 :         21 :         os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
     411                 :         21 :         dev->info.device_name[cli->dev_name_len] = '\0';
     412                 :         21 :         dev->info.dev_capab = cli->dev_capab;
     413                 :         21 :         dev->info.config_methods = cli->config_methods;
     414                 :         21 :         os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
     415                 :         21 :         dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
     416                 :         21 :         os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
     417                 :            :                   dev->info.wps_sec_dev_type_list_len);
     418                 :         21 : }
     419                 :            : 
     420                 :            : 
     421                 :        433 : static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
     422                 :            :                                  const u8 *go_interface_addr, int freq,
     423                 :            :                                  const u8 *gi, size_t gi_len)
     424                 :            : {
     425                 :            :         struct p2p_group_info info;
     426                 :            :         size_t c;
     427                 :            :         struct p2p_device *dev;
     428                 :            : 
     429         [ +  + ]:        433 :         if (gi == NULL)
     430                 :        410 :                 return 0;
     431                 :            : 
     432         [ -  + ]:         23 :         if (p2p_group_info_parse(gi, gi_len, &info) < 0)
     433                 :          0 :                 return -1;
     434                 :            : 
     435                 :            :         /*
     436                 :            :          * Clear old data for this group; if the devices are still in the
     437                 :            :          * group, the information will be restored in the loop following this.
     438                 :            :          */
     439         [ +  + ]:         56 :         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
     440         [ +  + ]:         33 :                 if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
     441                 :            :                               ETH_ALEN) == 0) {
     442                 :          8 :                         os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
     443                 :          8 :                         os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
     444                 :            :                 }
     445                 :            :         }
     446                 :            : 
     447         [ +  + ]:         46 :         for (c = 0; c < info.num_clients; c++) {
     448                 :         23 :                 struct p2p_client_info *cli = &info.client[c];
     449         [ +  + ]:         23 :                 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
     450                 :            :                               ETH_ALEN) == 0)
     451                 :          1 :                         continue; /* ignore our own entry */
     452                 :         22 :                 dev = p2p_get_device(p2p, cli->p2p_device_addr);
     453         [ +  + ]:         22 :                 if (dev) {
     454         [ +  + ]:         10 :                         if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
     455                 :            :                                           P2P_DEV_PROBE_REQ_ONLY)) {
     456                 :            :                                 /*
     457                 :            :                                  * Update information since we have not
     458                 :            :                                  * received this directly from the client.
     459                 :            :                                  */
     460                 :          9 :                                 p2p_copy_client_info(dev, cli);
     461                 :            :                         } else {
     462                 :            :                                 /*
     463                 :            :                                  * Need to update P2P Client Discoverability
     464                 :            :                                  * flag since it is valid only in P2P Group
     465                 :            :                                  * Info attribute.
     466                 :            :                                  */
     467                 :          1 :                                 dev->info.dev_capab &=
     468                 :            :                                         ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
     469                 :          1 :                                 dev->info.dev_capab |=
     470                 :          1 :                                         cli->dev_capab &
     471                 :            :                                         P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
     472                 :            :                         }
     473         [ -  + ]:         10 :                         if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
     474                 :          0 :                                 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
     475                 :            :                         }
     476                 :            :                 } else {
     477                 :         12 :                         dev = p2p_create_device(p2p, cli->p2p_device_addr);
     478         [ -  + ]:         12 :                         if (dev == NULL)
     479                 :          0 :                                 continue;
     480                 :         12 :                         dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
     481                 :         12 :                         p2p_copy_client_info(dev, cli);
     482                 :         12 :                         dev->oper_freq = freq;
     483                 :         12 :                         p2p->cfg->dev_found(p2p->cfg->cb_ctx,
     484                 :         12 :                                             dev->info.p2p_device_addr,
     485                 :         12 :                                             &dev->info, 1);
     486                 :         12 :                         dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
     487                 :            :                 }
     488                 :            : 
     489                 :         22 :                 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
     490                 :            :                           ETH_ALEN);
     491                 :         22 :                 os_get_reltime(&dev->last_seen);
     492                 :         22 :                 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
     493                 :         22 :                 os_memcpy(dev->member_in_go_iface, go_interface_addr,
     494                 :            :                           ETH_ALEN);
     495                 :            :         }
     496                 :            : 
     497                 :        433 :         return 0;
     498                 :            : }
     499                 :            : 
     500                 :            : 
     501                 :        767 : static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev,
     502                 :            :                               int probe_req, const struct p2p_message *msg)
     503                 :            : {
     504                 :        767 :         os_memcpy(dev->info.device_name, msg->device_name,
     505                 :            :                   sizeof(dev->info.device_name));
     506                 :            : 
     507 [ +  + ][ +  - ]:        767 :         if (msg->manufacturer &&
     508                 :        548 :             msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
     509                 :        548 :                 os_memset(dev->info.manufacturer, 0,
     510                 :            :                           sizeof(dev->info.manufacturer));
     511                 :        548 :                 os_memcpy(dev->info.manufacturer, msg->manufacturer,
     512                 :            :                           msg->manufacturer_len);
     513                 :            :         }
     514                 :            : 
     515 [ +  + ][ +  - ]:        767 :         if (msg->model_name &&
     516                 :        548 :             msg->model_name_len < sizeof(dev->info.model_name)) {
     517                 :        548 :                 os_memset(dev->info.model_name, 0,
     518                 :            :                           sizeof(dev->info.model_name));
     519                 :        548 :                 os_memcpy(dev->info.model_name, msg->model_name,
     520                 :            :                           msg->model_name_len);
     521                 :            :         }
     522                 :            : 
     523 [ +  + ][ +  - ]:        767 :         if (msg->model_number &&
     524                 :        548 :             msg->model_number_len < sizeof(dev->info.model_number)) {
     525                 :        548 :                 os_memset(dev->info.model_number, 0,
     526                 :            :                           sizeof(dev->info.model_number));
     527                 :        548 :                 os_memcpy(dev->info.model_number, msg->model_number,
     528                 :            :                           msg->model_number_len);
     529                 :            :         }
     530                 :            : 
     531 [ +  + ][ +  - ]:        767 :         if (msg->serial_number &&
     532                 :        449 :             msg->serial_number_len < sizeof(dev->info.serial_number)) {
     533                 :        449 :                 os_memset(dev->info.serial_number, 0,
     534                 :            :                           sizeof(dev->info.serial_number));
     535                 :        449 :                 os_memcpy(dev->info.serial_number, msg->serial_number,
     536                 :            :                           msg->serial_number_len);
     537                 :            :         }
     538                 :            : 
     539         [ +  + ]:        767 :         if (msg->pri_dev_type)
     540                 :        662 :                 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
     541                 :            :                           sizeof(dev->info.pri_dev_type));
     542         [ +  + ]:        105 :         else if (msg->wps_pri_dev_type)
     543                 :        104 :                 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
     544                 :            :                           sizeof(dev->info.pri_dev_type));
     545                 :            : 
     546         [ +  + ]:        767 :         if (msg->wps_sec_dev_type_list) {
     547                 :         12 :                 os_memcpy(dev->info.wps_sec_dev_type_list,
     548                 :            :                           msg->wps_sec_dev_type_list,
     549                 :            :                           msg->wps_sec_dev_type_list_len);
     550                 :         12 :                 dev->info.wps_sec_dev_type_list_len =
     551                 :         12 :                         msg->wps_sec_dev_type_list_len;
     552                 :            :         }
     553                 :            : 
     554         [ +  + ]:        767 :         if (msg->capability) {
     555                 :            :                 /*
     556                 :            :                  * P2P Client Discoverability bit is reserved in all frames
     557                 :            :                  * that use this function, so do not change its value here.
     558                 :            :                  */
     559                 :        760 :                 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
     560                 :        760 :                 dev->info.dev_capab |= msg->capability[0] &
     561                 :            :                         ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
     562                 :        760 :                 dev->info.group_capab = msg->capability[1];
     563                 :            :         }
     564                 :            : 
     565         [ +  + ]:        767 :         if (msg->ext_listen_timing) {
     566                 :          1 :                 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
     567                 :          1 :                 dev->ext_listen_interval =
     568                 :          1 :                         WPA_GET_LE16(msg->ext_listen_timing + 2);
     569                 :            :         }
     570                 :            : 
     571         [ +  + ]:        767 :         if (!probe_req) {
     572                 :            :                 u16 new_config_methods;
     573         [ +  + ]:        667 :                 new_config_methods = msg->config_methods ?
     574                 :            :                         msg->config_methods : msg->wps_config_methods;
     575 [ +  + ][ +  + ]:        667 :                 if (new_config_methods &&
     576                 :        656 :                     dev->info.config_methods != new_config_methods) {
     577                 :        224 :                         p2p_dbg(p2p, "Update peer " MACSTR
     578                 :            :                                 " config_methods 0x%x -> 0x%x",
     579                 :       1344 :                                 MAC2STR(dev->info.p2p_device_addr),
     580                 :        224 :                                 dev->info.config_methods,
     581                 :            :                                 new_config_methods);
     582                 :        224 :                         dev->info.config_methods = new_config_methods;
     583                 :            :                 }
     584                 :            :         }
     585                 :        767 : }
     586                 :            : 
     587                 :            : 
     588                 :            : /**
     589                 :            :  * p2p_add_device - Add peer entries based on scan results or P2P frames
     590                 :            :  * @p2p: P2P module context from p2p_init()
     591                 :            :  * @addr: Source address of Beacon or Probe Response frame (may be either
     592                 :            :  *      P2P Device Address or P2P Interface Address)
     593                 :            :  * @level: Signal level (signal strength of the received frame from the peer)
     594                 :            :  * @freq: Frequency on which the Beacon or Probe Response frame was received
     595                 :            :  * @rx_time: Time when the result was received
     596                 :            :  * @ies: IEs from the Beacon or Probe Response frame
     597                 :            :  * @ies_len: Length of ies buffer in octets
     598                 :            :  * @scan_res: Whether this was based on scan results
     599                 :            :  * Returns: 0 on success, -1 on failure
     600                 :            :  *
     601                 :            :  * If the scan result is for a GO, the clients in the group will also be added
     602                 :            :  * to the peer table. This function can also be used with some other frames
     603                 :            :  * like Provision Discovery Request that contains P2P Capability and P2P Device
     604                 :            :  * Info attributes.
     605                 :            :  */
     606                 :        855 : int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
     607                 :            :                    struct os_reltime *rx_time, int level, const u8 *ies,
     608                 :            :                    size_t ies_len, int scan_res)
     609                 :            : {
     610                 :            :         struct p2p_device *dev;
     611                 :            :         struct p2p_message msg;
     612                 :            :         const u8 *p2p_dev_addr;
     613                 :            :         int i;
     614                 :            :         struct os_reltime time_now;
     615                 :            : 
     616                 :        855 :         os_memset(&msg, 0, sizeof(msg));
     617         [ -  + ]:        855 :         if (p2p_parse_ies(ies, ies_len, &msg)) {
     618                 :          0 :                 p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
     619                 :          0 :                 p2p_parse_free(&msg);
     620                 :          0 :                 return -1;
     621                 :            :         }
     622                 :            : 
     623         [ +  + ]:        855 :         if (msg.p2p_device_addr)
     624                 :        767 :                 p2p_dev_addr = msg.p2p_device_addr;
     625         [ +  + ]:         88 :         else if (msg.device_id)
     626                 :          5 :                 p2p_dev_addr = msg.device_id;
     627                 :            :         else {
     628                 :         83 :                 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
     629                 :         83 :                 p2p_parse_free(&msg);
     630                 :         83 :                 return -1;
     631                 :            :         }
     632                 :            : 
     633 [ -  + ][ #  # ]:        772 :         if (!is_zero_ether_addr(p2p->peer_filter) &&
     634                 :          0 :             os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
     635                 :          0 :                 p2p_dbg(p2p, "Do not add peer filter for " MACSTR
     636                 :          0 :                         " due to peer filter", MAC2STR(p2p_dev_addr));
     637                 :          0 :                 p2p_parse_free(&msg);
     638                 :          0 :                 return 0;
     639                 :            :         }
     640                 :            : 
     641                 :        772 :         dev = p2p_create_device(p2p, p2p_dev_addr);
     642         [ -  + ]:        772 :         if (dev == NULL) {
     643                 :          0 :                 p2p_parse_free(&msg);
     644                 :          0 :                 return -1;
     645                 :            :         }
     646                 :            : 
     647         [ +  + ]:        772 :         if (rx_time == NULL) {
     648                 :        198 :                 os_get_reltime(&time_now);
     649                 :        198 :                 rx_time = &time_now;
     650                 :            :         }
     651                 :            : 
     652                 :            :         /*
     653                 :            :          * Update the device entry only if the new peer
     654                 :            :          * entry is newer than the one previously stored.
     655                 :            :          */
     656   [ +  +  +  + ]:       1417 :         if (dev->last_seen.sec > 0 &&
     657                 :        645 :             os_reltime_before(rx_time, &dev->last_seen)) {
     658                 :        141 :                 p2p_dbg(p2p, "Do not update peer entry based on old frame (rx_time=%u.%06u last_seen=%u.%06u)",
     659                 :        141 :                         (unsigned int) rx_time->sec,
     660                 :        141 :                         (unsigned int) rx_time->usec,
     661                 :        141 :                         (unsigned int) dev->last_seen.sec,
     662                 :        141 :                         (unsigned int) dev->last_seen.usec);
     663                 :        141 :                 p2p_parse_free(&msg);
     664                 :        141 :                 return -1;
     665                 :            :         }
     666                 :            : 
     667                 :        631 :         os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
     668                 :            : 
     669                 :        631 :         dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
     670                 :            : 
     671         [ +  + ]:        631 :         if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
     672                 :         30 :                 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
     673 [ +  + ][ +  + ]:        631 :         if (msg.ssid &&
     674         [ -  + ]:        370 :             (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
     675                 :        370 :              os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
     676                 :            :              != 0)) {
     677                 :        230 :                 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
     678                 :        230 :                 dev->oper_ssid_len = msg.ssid[1];
     679                 :            :         }
     680                 :            : 
     681 [ +  + ][ +  - ]:        631 :         if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
         [ +  + ][ +  - ]
     682         [ +  - ]:        433 :             *msg.ds_params >= 1 && *msg.ds_params <= 14) {
     683                 :            :                 int ds_freq;
     684         [ -  + ]:        433 :                 if (*msg.ds_params == 14)
     685                 :          0 :                         ds_freq = 2484;
     686                 :            :                 else
     687                 :        433 :                         ds_freq = 2407 + *msg.ds_params * 5;
     688         [ -  + ]:        433 :                 if (freq != ds_freq) {
     689                 :          0 :                         p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
     690                 :            :                                 freq, ds_freq);
     691                 :          0 :                         freq = ds_freq;
     692                 :            :                 }
     693                 :            :         }
     694                 :            : 
     695 [ +  + ][ +  + ]:        631 :         if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
                 [ +  + ]
     696         [ +  - ]:          2 :                 p2p_dbg(p2p, "Update Listen frequency based on scan results ("
     697                 :            :                         MACSTR " %d -> %d MHz (DS param %d)",
     698                 :         12 :                         MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
     699                 :          4 :                         freq, msg.ds_params ? *msg.ds_params : -1);
     700                 :            :         }
     701         [ +  + ]:        631 :         if (scan_res) {
     702                 :        433 :                 dev->listen_freq = freq;
     703         [ +  + ]:        433 :                 if (msg.group_info)
     704                 :         23 :                         dev->oper_freq = freq;
     705                 :            :         }
     706                 :        631 :         dev->info.level = level;
     707                 :            : 
     708                 :        631 :         p2p_copy_wps_info(p2p, dev, 0, &msg);
     709                 :            : 
     710         [ +  + ]:       6941 :         for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
     711                 :       6310 :                 wpabuf_free(dev->info.wps_vendor_ext[i]);
     712                 :       6310 :                 dev->info.wps_vendor_ext[i] = NULL;
     713                 :            :         }
     714                 :            : 
     715         [ +  - ]:        631 :         for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
     716         [ +  - ]:        631 :                 if (msg.wps_vendor_ext[i] == NULL)
     717                 :        631 :                         break;
     718                 :          0 :                 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
     719                 :          0 :                         msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
     720         [ #  # ]:          0 :                 if (dev->info.wps_vendor_ext[i] == NULL)
     721                 :          0 :                         break;
     722                 :            :         }
     723                 :            : 
     724         [ +  + ]:        631 :         if (msg.wfd_subelems) {
     725                 :        563 :                 wpabuf_free(dev->info.wfd_subelems);
     726                 :        563 :                 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
     727                 :            :         }
     728                 :            : 
     729         [ +  + ]:        631 :         if (scan_res) {
     730                 :        433 :                 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
     731                 :            :                                       msg.group_info, msg.group_info_len);
     732                 :            :         }
     733                 :            : 
     734                 :        631 :         p2p_parse_free(&msg);
     735                 :            : 
     736         [ +  + ]:        631 :         if (dev->flags & P2P_DEV_REPORTED)
     737                 :        433 :                 return 0;
     738                 :            : 
     739                 :        198 :         p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
     740                 :        198 :                 freq, (unsigned int) rx_time->sec,
     741                 :        198 :                 (unsigned int) rx_time->usec);
     742         [ -  + ]:        198 :         if (dev->flags & P2P_DEV_USER_REJECTED) {
     743                 :          0 :                 p2p_dbg(p2p, "Do not report rejected device");
     744                 :          0 :                 return 0;
     745                 :            :         }
     746                 :            : 
     747 [ +  + ][ -  + ]:        198 :         if (dev->info.config_methods == 0 &&
     748 [ #  # ][ #  # ]:          0 :             (freq == 2412 || freq == 2437 || freq == 2462)) {
     749                 :            :                 /*
     750                 :            :                  * If we have only seen a Beacon frame from a GO, we do not yet
     751                 :            :                  * know what WPS config methods it supports. Since some
     752                 :            :                  * applications use config_methods value from P2P-DEVICE-FOUND
     753                 :            :                  * events, postpone reporting this peer until we've fully
     754                 :            :                  * discovered its capabilities.
     755                 :            :                  *
     756                 :            :                  * At least for now, do this only if the peer was detected on
     757                 :            :                  * one of the social channels since that peer can be easily be
     758                 :            :                  * found again and there are no limitations of having to use
     759                 :            :                  * passive scan on this channels, so this can be done through
     760                 :            :                  * Probe Response frame that includes the config_methods
     761                 :            :                  * information.
     762                 :            :                  */
     763                 :          5 :                 p2p_dbg(p2p, "Do not report peer " MACSTR
     764                 :         30 :                         " with unknown config methods", MAC2STR(addr));
     765                 :          5 :                 return 0;
     766                 :            :         }
     767                 :            : 
     768                 :        193 :         p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
     769                 :        193 :                             !(dev->flags & P2P_DEV_REPORTED_ONCE));
     770                 :        193 :         dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
     771                 :            : 
     772                 :        855 :         return 0;
     773                 :            : }
     774                 :            : 
     775                 :            : 
     776                 :        265 : static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
     777                 :            : {
     778                 :            :         int i;
     779                 :            : 
     780         [ -  + ]:        265 :         if (p2p->go_neg_peer == dev) {
     781                 :            :                 /*
     782                 :            :                  * If GO Negotiation is in progress, report that it has failed.
     783                 :            :                  */
     784                 :          0 :                 p2p_go_neg_failed(p2p, dev, -1);
     785                 :          0 :                 p2p->go_neg_peer = NULL;
     786                 :            :         }
     787         [ -  + ]:        265 :         if (p2p->invite_peer == dev)
     788                 :          0 :                 p2p->invite_peer = NULL;
     789         [ -  + ]:        265 :         if (p2p->sd_peer == dev)
     790                 :          0 :                 p2p->sd_peer = NULL;
     791         [ +  + ]:        265 :         if (p2p->pending_client_disc_go == dev)
     792                 :          1 :                 p2p->pending_client_disc_go = NULL;
     793                 :            : 
     794                 :            :         /* dev_lost() device, but only if it was previously dev_found() */
     795         [ +  + ]:        265 :         if (dev->flags & P2P_DEV_REPORTED_ONCE)
     796                 :        236 :                 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
     797                 :        236 :                                    dev->info.p2p_device_addr);
     798                 :            : 
     799         [ +  + ]:       2915 :         for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
     800                 :       2650 :                 wpabuf_free(dev->info.wps_vendor_ext[i]);
     801                 :       2650 :                 dev->info.wps_vendor_ext[i] = NULL;
     802                 :            :         }
     803                 :            : 
     804                 :        265 :         wpabuf_free(dev->info.wfd_subelems);
     805                 :            : 
     806                 :        265 :         os_free(dev);
     807                 :        265 : }
     808                 :            : 
     809                 :            : 
     810                 :          0 : static int p2p_get_next_prog_freq(struct p2p_data *p2p)
     811                 :            : {
     812                 :            :         struct p2p_channels *c;
     813                 :            :         struct p2p_reg_class *cla;
     814                 :            :         size_t cl, ch;
     815                 :          0 :         int found = 0;
     816                 :            :         u8 reg_class;
     817                 :            :         u8 channel;
     818                 :            :         int freq;
     819                 :            : 
     820                 :          0 :         c = &p2p->cfg->channels;
     821         [ #  # ]:          0 :         for (cl = 0; cl < c->reg_classes; cl++) {
     822                 :          0 :                 cla = &c->reg_class[cl];
     823         [ #  # ]:          0 :                 if (cla->reg_class != p2p->last_prog_scan_class)
     824                 :          0 :                         continue;
     825         [ #  # ]:          0 :                 for (ch = 0; ch < cla->channels; ch++) {
     826         [ #  # ]:          0 :                         if (cla->channel[ch] == p2p->last_prog_scan_chan) {
     827                 :          0 :                                 found = 1;
     828                 :          0 :                                 break;
     829                 :            :                         }
     830                 :            :                 }
     831         [ #  # ]:          0 :                 if (found)
     832                 :          0 :                         break;
     833                 :            :         }
     834                 :            : 
     835         [ #  # ]:          0 :         if (!found) {
     836                 :            :                 /* Start from beginning */
     837                 :          0 :                 reg_class = c->reg_class[0].reg_class;
     838                 :          0 :                 channel = c->reg_class[0].channel[0];
     839                 :            :         } else {
     840                 :            :                 /* Pick the next channel */
     841                 :          0 :                 ch++;
     842         [ #  # ]:          0 :                 if (ch == cla->channels) {
     843                 :          0 :                         cl++;
     844         [ #  # ]:          0 :                         if (cl == c->reg_classes)
     845                 :          0 :                                 cl = 0;
     846                 :          0 :                         ch = 0;
     847                 :            :                 }
     848                 :          0 :                 reg_class = c->reg_class[cl].reg_class;
     849                 :          0 :                 channel = c->reg_class[cl].channel[ch];
     850                 :            :         }
     851                 :            : 
     852                 :          0 :         freq = p2p_channel_to_freq(reg_class, channel);
     853                 :          0 :         p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
     854                 :            :                 reg_class, channel, freq);
     855                 :          0 :         p2p->last_prog_scan_class = reg_class;
     856                 :          0 :         p2p->last_prog_scan_chan = channel;
     857                 :            : 
     858 [ #  # ][ #  # ]:          0 :         if (freq == 2412 || freq == 2437 || freq == 2462)
                 [ #  # ]
     859                 :          0 :                 return 0; /* No need to add social channels */
     860                 :          0 :         return freq;
     861                 :            : }
     862                 :            : 
     863                 :            : 
     864                 :        492 : static void p2p_search(struct p2p_data *p2p)
     865                 :            : {
     866                 :        492 :         int freq = 0;
     867                 :            :         enum p2p_scan_type type;
     868                 :        492 :         u16 pw_id = DEV_PW_DEFAULT;
     869                 :            :         int res;
     870                 :            : 
     871         [ +  + ]:        492 :         if (p2p->drv_in_listen) {
     872                 :          5 :                 p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
     873                 :        492 :                 return;
     874                 :            :         }
     875                 :        487 :         p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
     876                 :            : 
     877 [ -  + ][ #  # ]:        487 :         if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
     878                 :            :             (freq = p2p_get_next_prog_freq(p2p)) > 0) {
     879                 :          0 :                 type = P2P_SCAN_SOCIAL_PLUS_ONE;
     880                 :          0 :                 p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
     881                 :            :         } else {
     882                 :        487 :                 type = P2P_SCAN_SOCIAL;
     883                 :        487 :                 p2p_dbg(p2p, "Starting search");
     884                 :            :         }
     885                 :            : 
     886                 :        487 :         res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
     887                 :        487 :                                  p2p->num_req_dev_types, p2p->req_dev_types,
     888                 :        487 :                                  p2p->find_dev_id, pw_id);
     889         [ +  + ]:        487 :         if (res < 0) {
     890                 :          1 :                 p2p_dbg(p2p, "Scan request failed");
     891                 :          1 :                 p2p_continue_find(p2p);
     892                 :            :         } else {
     893                 :        486 :                 p2p_dbg(p2p, "Running p2p_scan");
     894                 :        486 :                 p2p->p2p_scan_running = 1;
     895                 :        486 :                 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
     896                 :        486 :                 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
     897                 :            :                                        p2p, NULL);
     898                 :            :         }
     899                 :            : }
     900                 :            : 
     901                 :            : 
     902                 :          0 : static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
     903                 :            : {
     904                 :          0 :         struct p2p_data *p2p = eloop_ctx;
     905                 :          0 :         p2p_dbg(p2p, "Find timeout -> stop");
     906                 :          0 :         p2p_stop_find(p2p);
     907                 :          0 : }
     908                 :            : 
     909                 :            : 
     910                 :        658 : static int p2p_run_after_scan(struct p2p_data *p2p)
     911                 :            : {
     912                 :            :         struct p2p_device *dev;
     913                 :            :         enum p2p_after_scan op;
     914                 :            : 
     915         [ +  + ]:        658 :         if (p2p->after_scan_tx) {
     916                 :         17 :                 p2p->after_scan_tx_in_progress = 1;
     917                 :         17 :                 p2p_dbg(p2p, "Send pending Action frame at p2p_scan completion");
     918                 :         17 :                 p2p->cfg->send_action(p2p->cfg->cb_ctx,
     919                 :         17 :                                       p2p->after_scan_tx->freq,
     920                 :         17 :                                       p2p->after_scan_tx->dst,
     921                 :         17 :                                       p2p->after_scan_tx->src,
     922                 :         17 :                                       p2p->after_scan_tx->bssid,
     923                 :         17 :                                       (u8 *) (p2p->after_scan_tx + 1),
     924                 :         17 :                                       p2p->after_scan_tx->len,
     925                 :         17 :                                       p2p->after_scan_tx->wait_time);
     926                 :         17 :                 os_free(p2p->after_scan_tx);
     927                 :         17 :                 p2p->after_scan_tx = NULL;
     928                 :         17 :                 return 1;
     929                 :            :         }
     930                 :            : 
     931                 :        641 :         op = p2p->start_after_scan;
     932                 :        641 :         p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
     933   [ +  +  +  - ]:        641 :         switch (op) {
     934                 :            :         case P2P_AFTER_SCAN_NOTHING:
     935                 :        610 :                 break;
     936                 :            :         case P2P_AFTER_SCAN_LISTEN:
     937                 :          2 :                 p2p_dbg(p2p, "Start previously requested Listen state");
     938                 :          2 :                 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
     939                 :          2 :                            p2p->pending_listen_usec / 1000);
     940                 :          2 :                 return 1;
     941                 :            :         case P2P_AFTER_SCAN_CONNECT:
     942                 :         29 :                 p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
     943                 :        174 :                         MAC2STR(p2p->after_scan_peer));
     944                 :         29 :                 dev = p2p_get_device(p2p, p2p->after_scan_peer);
     945         [ -  + ]:         29 :                 if (dev == NULL) {
     946                 :          0 :                         p2p_dbg(p2p, "Peer not known anymore");
     947                 :          0 :                         break;
     948                 :            :                 }
     949                 :         29 :                 p2p_connect_send(p2p, dev);
     950                 :         29 :                 return 1;
     951                 :            :         }
     952                 :            : 
     953                 :        658 :         return 0;
     954                 :            : }
     955                 :            : 
     956                 :            : 
     957                 :          0 : static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
     958                 :            : {
     959                 :          0 :         struct p2p_data *p2p = eloop_ctx;
     960                 :            :         int running;
     961                 :          0 :         p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
     962                 :          0 :         running = p2p->p2p_scan_running;
     963                 :            :         /* Make sure we recover from missed scan results callback */
     964                 :          0 :         p2p->p2p_scan_running = 0;
     965                 :            : 
     966         [ #  # ]:          0 :         if (running)
     967                 :          0 :                 p2p_run_after_scan(p2p);
     968                 :          0 : }
     969                 :            : 
     970                 :            : 
     971                 :       2883 : static void p2p_free_req_dev_types(struct p2p_data *p2p)
     972                 :            : {
     973                 :       2883 :         p2p->num_req_dev_types = 0;
     974                 :       2883 :         os_free(p2p->req_dev_types);
     975                 :       2883 :         p2p->req_dev_types = NULL;
     976                 :       2883 : }
     977                 :            : 
     978                 :            : 
     979                 :        151 : int p2p_find(struct p2p_data *p2p, unsigned int timeout,
     980                 :            :              enum p2p_discovery_type type,
     981                 :            :              unsigned int num_req_dev_types, const u8 *req_dev_types,
     982                 :            :              const u8 *dev_id, unsigned int search_delay)
     983                 :            : {
     984                 :            :         int res;
     985                 :            : 
     986                 :        151 :         p2p_dbg(p2p, "Starting find (type=%d)", type);
     987                 :        151 :         os_get_reltime(&p2p->find_start);
     988         [ +  + ]:        151 :         if (p2p->p2p_scan_running) {
     989                 :          6 :                 p2p_dbg(p2p, "p2p_scan is already running");
     990                 :            :         }
     991                 :            : 
     992                 :        151 :         p2p_free_req_dev_types(p2p);
     993 [ +  + ][ +  - ]:        151 :         if (req_dev_types && num_req_dev_types) {
     994                 :          4 :                 p2p->req_dev_types = os_malloc(num_req_dev_types *
     995                 :            :                                                WPS_DEV_TYPE_LEN);
     996         [ -  + ]:          4 :                 if (p2p->req_dev_types == NULL)
     997                 :          0 :                         return -1;
     998                 :          4 :                 os_memcpy(p2p->req_dev_types, req_dev_types,
     999                 :            :                           num_req_dev_types * WPS_DEV_TYPE_LEN);
    1000                 :          4 :                 p2p->num_req_dev_types = num_req_dev_types;
    1001                 :            :         }
    1002                 :            : 
    1003         [ +  + ]:        151 :         if (dev_id) {
    1004                 :          4 :                 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
    1005                 :          4 :                 p2p->find_dev_id = p2p->find_dev_id_buf;
    1006                 :            :         } else
    1007                 :        147 :                 p2p->find_dev_id = NULL;
    1008                 :            : 
    1009                 :        151 :         p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
    1010                 :        151 :         p2p_clear_timeout(p2p);
    1011                 :        151 :         p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
    1012                 :        151 :         p2p->find_type = type;
    1013                 :        151 :         p2p_device_clear_reported(p2p);
    1014                 :        151 :         p2p_set_state(p2p, P2P_SEARCH);
    1015                 :        151 :         p2p->search_delay = search_delay;
    1016                 :        151 :         p2p->in_search_delay = 0;
    1017                 :        151 :         eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
    1018                 :        151 :         p2p->last_p2p_find_timeout = timeout;
    1019         [ -  + ]:        151 :         if (timeout)
    1020                 :          0 :                 eloop_register_timeout(timeout, 0, p2p_find_timeout,
    1021                 :            :                                        p2p, NULL);
    1022      [ +  +  - ]:        151 :         switch (type) {
    1023                 :            :         case P2P_FIND_START_WITH_FULL:
    1024                 :            :         case P2P_FIND_PROGRESSIVE:
    1025                 :         22 :                 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
    1026                 :            :                                          p2p->num_req_dev_types,
    1027                 :         22 :                                          p2p->req_dev_types, dev_id,
    1028                 :            :                                          DEV_PW_DEFAULT);
    1029                 :         22 :                 break;
    1030                 :            :         case P2P_FIND_ONLY_SOCIAL:
    1031                 :        129 :                 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
    1032                 :            :                                          p2p->num_req_dev_types,
    1033                 :        129 :                                          p2p->req_dev_types, dev_id,
    1034                 :            :                                          DEV_PW_DEFAULT);
    1035                 :        129 :                 break;
    1036                 :            :         default:
    1037                 :          0 :                 return -1;
    1038                 :            :         }
    1039                 :            : 
    1040         [ +  + ]:        151 :         if (res == 0) {
    1041                 :        146 :                 p2p_dbg(p2p, "Running p2p_scan");
    1042                 :        146 :                 p2p->p2p_scan_running = 1;
    1043                 :        146 :                 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
    1044                 :        146 :                 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
    1045                 :            :                                        p2p, NULL);
    1046         [ +  - ]:          5 :         } else if (p2p->p2p_scan_running) {
    1047                 :          5 :                 p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running");
    1048                 :            :                 /* wait for the previous p2p_scan to complete */
    1049                 :          5 :                 res = 0; /* do not report failure */
    1050                 :            :         } else {
    1051                 :          0 :                 p2p_dbg(p2p, "Failed to start p2p_scan");
    1052                 :          0 :                 p2p_set_state(p2p, P2P_IDLE);
    1053                 :          0 :                 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
    1054                 :            :         }
    1055                 :            : 
    1056                 :        151 :         return res;
    1057                 :            : }
    1058                 :            : 
    1059                 :            : 
    1060                 :       2717 : void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
    1061                 :            : {
    1062                 :       2717 :         p2p_dbg(p2p, "Stopping find");
    1063                 :       2717 :         eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
    1064                 :       2717 :         p2p_clear_timeout(p2p);
    1065         [ +  + ]:       2717 :         if (p2p->state == P2P_SEARCH)
    1066                 :        144 :                 p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
    1067                 :       2717 :         p2p_set_state(p2p, P2P_IDLE);
    1068                 :       2717 :         p2p_free_req_dev_types(p2p);
    1069                 :       2717 :         p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
    1070         [ +  + ]:       2717 :         if (p2p->go_neg_peer)
    1071                 :         10 :                 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
    1072                 :       2717 :         p2p->go_neg_peer = NULL;
    1073                 :       2717 :         p2p->sd_peer = NULL;
    1074                 :       2717 :         p2p->invite_peer = NULL;
    1075                 :       2717 :         p2p_stop_listen_for_freq(p2p, freq);
    1076                 :       2717 : }
    1077                 :            : 
    1078                 :            : 
    1079                 :       2726 : void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
    1080                 :            : {
    1081 [ +  + ][ +  + ]:       2726 :         if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
                 [ +  - ]
    1082                 :         32 :                 p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
    1083                 :       2726 :                 return;
    1084                 :            :         }
    1085         [ +  + ]:       2694 :         if (p2p->in_listen) {
    1086                 :        137 :                 p2p->in_listen = 0;
    1087                 :        137 :                 p2p_clear_timeout(p2p);
    1088                 :            :         }
    1089         [ +  + ]:       2694 :         if (p2p->drv_in_listen) {
    1090                 :            :                 /*
    1091                 :            :                  * The driver may not deliver callback to p2p_listen_end()
    1092                 :            :                  * when the operation gets canceled, so clear the internal
    1093                 :            :                  * variable that is tracking driver state.
    1094                 :            :                  */
    1095                 :         96 :                 p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
    1096                 :         96 :                 p2p->drv_in_listen = 0;
    1097                 :            :         }
    1098                 :       2694 :         p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
    1099                 :            : }
    1100                 :            : 
    1101                 :            : 
    1102                 :        716 : void p2p_stop_listen(struct p2p_data *p2p)
    1103                 :            : {
    1104         [ +  - ]:        716 :         if (p2p->state != P2P_LISTEN_ONLY) {
    1105                 :        716 :                 p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
    1106                 :        716 :                 return;
    1107                 :            :         }
    1108                 :            : 
    1109                 :          0 :         p2p_stop_listen_for_freq(p2p, 0);
    1110                 :          0 :         p2p_set_state(p2p, P2P_IDLE);
    1111                 :            : }
    1112                 :            : 
    1113                 :            : 
    1114                 :       2670 : void p2p_stop_find(struct p2p_data *p2p)
    1115                 :            : {
    1116                 :       2670 :         p2p_stop_find_for_freq(p2p, 0);
    1117                 :       2670 : }
    1118                 :            : 
    1119                 :            : 
    1120                 :         29 : static int p2p_prepare_channel_pref(struct p2p_data *p2p,
    1121                 :            :                                     unsigned int force_freq,
    1122                 :            :                                     unsigned int pref_freq, int go)
    1123                 :            : {
    1124                 :            :         u8 op_class, op_channel;
    1125         [ +  + ]:         29 :         unsigned int freq = force_freq ? force_freq : pref_freq;
    1126                 :            : 
    1127                 :         29 :         p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
    1128                 :            :                 force_freq, pref_freq, go);
    1129         [ -  + ]:         29 :         if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
    1130                 :          0 :                 p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
    1131                 :          0 :                 return -1;
    1132                 :            :         }
    1133                 :            : 
    1134 [ -  + ][ #  # ]:         29 :         if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
    1135         [ #  # ]:          0 :             (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
    1136                 :            :                                           op_channel))) {
    1137                 :          0 :                 p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
    1138                 :            :                         freq, op_class, op_channel);
    1139                 :          0 :                 return -1;
    1140                 :            :         }
    1141                 :            : 
    1142                 :         29 :         p2p->op_reg_class = op_class;
    1143                 :         29 :         p2p->op_channel = op_channel;
    1144                 :            : 
    1145         [ +  + ]:         29 :         if (force_freq) {
    1146                 :         28 :                 p2p->channels.reg_classes = 1;
    1147                 :         28 :                 p2p->channels.reg_class[0].channels = 1;
    1148                 :         28 :                 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
    1149                 :         28 :                 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
    1150                 :            :         } else {
    1151                 :          1 :                 os_memcpy(&p2p->channels, &p2p->cfg->channels,
    1152                 :            :                           sizeof(struct p2p_channels));
    1153                 :            :         }
    1154                 :            : 
    1155                 :         29 :         return 0;
    1156                 :            : }
    1157                 :            : 
    1158                 :            : 
    1159                 :         86 : static void p2p_prepare_channel_best(struct p2p_data *p2p)
    1160                 :            : {
    1161                 :            :         u8 op_class, op_channel;
    1162                 :         86 :         const int op_classes_5ghz[] = { 124, 115, 0 };
    1163                 :         86 :         const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
    1164                 :         86 :         const int op_classes_vht[] = { 128, 0 };
    1165                 :            : 
    1166                 :         86 :         p2p_dbg(p2p, "Prepare channel best");
    1167                 :            : 
    1168         [ +  - ]:         86 :         if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
           [ -  +  #  # ]
    1169         [ #  # ]:          0 :             p2p_supported_freq(p2p, p2p->best_freq_overall) &&
    1170                 :          0 :             p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
    1171                 :            :             == 0) {
    1172                 :          0 :                 p2p_dbg(p2p, "Select best overall channel as operating channel preference");
    1173                 :          0 :                 p2p->op_reg_class = op_class;
    1174                 :          0 :                 p2p->op_channel = op_channel;
    1175         [ +  - ]:         86 :         } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
           [ -  +  #  # ]
    1176         [ #  # ]:          0 :                    p2p_supported_freq(p2p, p2p->best_freq_5) &&
    1177                 :          0 :                    p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
    1178                 :            :                    == 0) {
    1179                 :          0 :                 p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
    1180                 :          0 :                 p2p->op_reg_class = op_class;
    1181                 :          0 :                 p2p->op_channel = op_channel;
    1182         [ +  - ]:         86 :         } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
           [ -  +  #  # ]
    1183         [ #  # ]:          0 :                    p2p_supported_freq(p2p, p2p->best_freq_24) &&
    1184                 :          0 :                    p2p_freq_to_channel(p2p->best_freq_24, &op_class,
    1185                 :            :                                        &op_channel) == 0) {
    1186                 :          0 :                 p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
    1187                 :          0 :                 p2p->op_reg_class = op_class;
    1188                 :          0 :                 p2p->op_channel = op_channel;
    1189   [ +  +  +  - ]:         88 :         } else if (p2p->cfg->num_pref_chan > 0 &&
    1190                 :          2 :                    p2p_channels_includes(&p2p->cfg->channels,
    1191                 :          2 :                                          p2p->cfg->pref_chan[0].op_class,
    1192                 :          2 :                                          p2p->cfg->pref_chan[0].chan)) {
    1193                 :          2 :                 p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
    1194                 :          2 :                 p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
    1195                 :          2 :                 p2p->op_channel = p2p->cfg->pref_chan[0].chan;
    1196         [ -  + ]:         84 :         } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
    1197                 :            :                                       &p2p->op_reg_class, &p2p->op_channel) ==
    1198                 :            :                    0) {
    1199                 :          0 :                 p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference",
    1200                 :          0 :                         p2p->op_reg_class, p2p->op_channel);
    1201         [ -  + ]:         84 :         } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40,
    1202                 :            :                                       &p2p->op_reg_class, &p2p->op_channel) ==
    1203                 :            :                    0) {
    1204                 :          0 :                 p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
    1205                 :          0 :                         p2p->op_reg_class, p2p->op_channel);
    1206         [ -  + ]:         84 :         } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
    1207                 :            :                                       &p2p->op_reg_class, &p2p->op_channel) ==
    1208                 :            :                    0) {
    1209                 :          0 :                 p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
    1210                 :          0 :                         p2p->op_reg_class, p2p->op_channel);
    1211                 :            :         } else {
    1212                 :         84 :                 p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
    1213                 :         84 :                 p2p->op_reg_class = p2p->cfg->op_reg_class;
    1214                 :         84 :                 p2p->op_channel = p2p->cfg->op_channel;
    1215                 :            :         }
    1216                 :            : 
    1217                 :         86 :         os_memcpy(&p2p->channels, &p2p->cfg->channels,
    1218                 :            :                   sizeof(struct p2p_channels));
    1219                 :         86 : }
    1220                 :            : 
    1221                 :            : 
    1222                 :            : /**
    1223                 :            :  * p2p_prepare_channel - Select operating channel for GO Negotiation
    1224                 :            :  * @p2p: P2P module context from p2p_init()
    1225                 :            :  * @dev: Selected peer device
    1226                 :            :  * @force_freq: Forced frequency in MHz or 0 if not forced
    1227                 :            :  * @pref_freq: Preferred frequency in MHz or 0 if no preference
    1228                 :            :  * @go: Whether the local end will be forced to be GO
    1229                 :            :  * Returns: 0 on success, -1 on failure (channel not supported for P2P)
    1230                 :            :  *
    1231                 :            :  * This function is used to do initial operating channel selection for GO
    1232                 :            :  * Negotiation prior to having received peer information. The selected channel
    1233                 :            :  * may be further optimized in p2p_reselect_channel() once the peer information
    1234                 :            :  * is available.
    1235                 :            :  */
    1236                 :        115 : int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
    1237                 :            :                         unsigned int force_freq, unsigned int pref_freq, int go)
    1238                 :            : {
    1239                 :        115 :         p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
    1240                 :            :                 force_freq, pref_freq, go);
    1241 [ +  + ][ +  + ]:        115 :         if (force_freq || pref_freq) {
    1242         [ -  + ]:         29 :                 if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
    1243                 :            :                     0)
    1244                 :          0 :                         return -1;
    1245                 :            :         } else {
    1246                 :         86 :                 p2p_prepare_channel_best(p2p);
    1247                 :            :         }
    1248                 :        115 :         p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
    1249         [ +  + ]:        115 :         if (go)
    1250                 :         45 :                 p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
    1251         [ +  + ]:         70 :         else if (!force_freq)
    1252                 :         54 :                 p2p_channels_union(&p2p->channels, &p2p->cfg->cli_channels,
    1253                 :            :                                    &p2p->channels);
    1254                 :        115 :         p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
    1255                 :            : 
    1256         [ +  + ]:        115 :         p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
    1257                 :        230 :                 p2p->op_reg_class, p2p->op_channel,
    1258                 :            :                 force_freq ? " (forced)" : "");
    1259                 :            : 
    1260         [ +  + ]:        115 :         if (force_freq)
    1261                 :         28 :                 dev->flags |= P2P_DEV_FORCE_FREQ;
    1262                 :            :         else
    1263                 :         87 :                 dev->flags &= ~P2P_DEV_FORCE_FREQ;
    1264                 :            : 
    1265                 :        115 :         return 0;
    1266                 :            : }
    1267                 :            : 
    1268                 :            : 
    1269                 :        102 : static void p2p_set_dev_persistent(struct p2p_device *dev,
    1270                 :            :                                    int persistent_group)
    1271                 :            : {
    1272   [ +  +  +  - ]:        102 :         switch (persistent_group) {
    1273                 :            :         case 0:
    1274                 :         92 :                 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
    1275                 :            :                                 P2P_DEV_PREFER_PERSISTENT_RECONN);
    1276                 :         92 :                 break;
    1277                 :            :         case 1:
    1278                 :          2 :                 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
    1279                 :          2 :                 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
    1280                 :          2 :                 break;
    1281                 :            :         case 2:
    1282                 :          8 :                 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
    1283                 :            :                         P2P_DEV_PREFER_PERSISTENT_RECONN;
    1284                 :          8 :                 break;
    1285                 :            :         }
    1286                 :        102 : }
    1287                 :            : 
    1288                 :            : 
    1289                 :         59 : int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
    1290                 :            :                 enum p2p_wps_method wps_method,
    1291                 :            :                 int go_intent, const u8 *own_interface_addr,
    1292                 :            :                 unsigned int force_freq, int persistent_group,
    1293                 :            :                 const u8 *force_ssid, size_t force_ssid_len,
    1294                 :            :                 int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id)
    1295                 :            : {
    1296                 :            :         struct p2p_device *dev;
    1297                 :            : 
    1298                 :         59 :         p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
    1299                 :            :                 "  GO Intent=%d  Intended Interface Address=" MACSTR
    1300                 :            :                 " wps_method=%d persistent_group=%d pd_before_go_neg=%d "
    1301                 :            :                 "oob_pw_id=%u",
    1302                 :        708 :                 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
    1303                 :            :                 wps_method, persistent_group, pd_before_go_neg, oob_pw_id);
    1304                 :            : 
    1305                 :         59 :         dev = p2p_get_device(p2p, peer_addr);
    1306 [ +  - ][ -  + ]:         59 :         if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
    1307                 :          0 :                 p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
    1308                 :          0 :                         MAC2STR(peer_addr));
    1309                 :          0 :                 return -1;
    1310                 :            :         }
    1311                 :            : 
    1312         [ -  + ]:         59 :         if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
    1313                 :            :                                 go_intent == 15) < 0)
    1314                 :          0 :                 return -1;
    1315                 :            : 
    1316         [ +  + ]:         59 :         if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
    1317         [ -  + ]:          1 :                 if (!(dev->info.dev_capab &
    1318                 :            :                       P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
    1319                 :          0 :                         p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
    1320                 :            :                                 " that is in a group and is not discoverable",
    1321                 :          0 :                                 MAC2STR(peer_addr));
    1322                 :          0 :                         return -1;
    1323                 :            :                 }
    1324         [ -  + ]:          1 :                 if (dev->oper_freq <= 0) {
    1325                 :          0 :                         p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
    1326                 :            :                                 " with incomplete information",
    1327                 :          0 :                                 MAC2STR(peer_addr));
    1328                 :          0 :                         return -1;
    1329                 :            :                 }
    1330                 :            : 
    1331                 :            :                 /*
    1332                 :            :                  * First, try to connect directly. If the peer does not
    1333                 :            :                  * acknowledge frames, assume it is sleeping and use device
    1334                 :            :                  * discoverability via the GO at that point.
    1335                 :            :                  */
    1336                 :            :         }
    1337                 :            : 
    1338                 :         59 :         p2p->ssid_set = 0;
    1339         [ -  + ]:         59 :         if (force_ssid) {
    1340                 :          0 :                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
    1341                 :            :                                   force_ssid, force_ssid_len);
    1342                 :          0 :                 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
    1343                 :          0 :                 p2p->ssid_len = force_ssid_len;
    1344                 :          0 :                 p2p->ssid_set = 1;
    1345                 :            :         }
    1346                 :            : 
    1347                 :         59 :         dev->flags &= ~P2P_DEV_NOT_YET_READY;
    1348                 :         59 :         dev->flags &= ~P2P_DEV_USER_REJECTED;
    1349                 :         59 :         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
    1350                 :         59 :         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
    1351         [ -  + ]:         59 :         if (pd_before_go_neg)
    1352                 :          0 :                 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
    1353                 :            :         else {
    1354                 :         59 :                 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
    1355                 :            :                 /*
    1356                 :            :                  * Assign dialog token and tie breaker here to use the same
    1357                 :            :                  * values in each retry within the same GO Negotiation exchange.
    1358                 :            :                  */
    1359                 :         59 :                 dev->dialog_token++;
    1360         [ -  + ]:         59 :                 if (dev->dialog_token == 0)
    1361                 :          0 :                         dev->dialog_token = 1;
    1362                 :         59 :                 dev->tie_breaker = p2p->next_tie_breaker;
    1363                 :         59 :                 p2p->next_tie_breaker = !p2p->next_tie_breaker;
    1364                 :            :         }
    1365                 :         59 :         dev->connect_reqs = 0;
    1366                 :         59 :         dev->go_neg_req_sent = 0;
    1367                 :         59 :         dev->go_state = UNKNOWN_GO;
    1368                 :         59 :         p2p_set_dev_persistent(dev, persistent_group);
    1369                 :         59 :         p2p->go_intent = go_intent;
    1370                 :         59 :         os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
    1371                 :            : 
    1372         [ +  + ]:         59 :         if (p2p->state != P2P_IDLE)
    1373                 :         51 :                 p2p_stop_find(p2p);
    1374                 :            : 
    1375         [ +  + ]:         59 :         if (p2p->after_scan_tx) {
    1376                 :            :                 /*
    1377                 :            :                  * We need to drop the pending frame to avoid issues with the
    1378                 :            :                  * new GO Negotiation, e.g., when the pending frame was from a
    1379                 :            :                  * previous attempt at starting a GO Negotiation.
    1380                 :            :                  */
    1381                 :          2 :                 p2p_dbg(p2p, "Dropped previous pending Action frame TX that was waiting for p2p_scan completion");
    1382                 :          2 :                 os_free(p2p->after_scan_tx);
    1383                 :          2 :                 p2p->after_scan_tx = NULL;
    1384                 :            :         }
    1385                 :            : 
    1386                 :         59 :         dev->wps_method = wps_method;
    1387                 :         59 :         dev->oob_pw_id = oob_pw_id;
    1388                 :         59 :         dev->status = P2P_SC_SUCCESS;
    1389                 :            : 
    1390         [ +  + ]:         59 :         if (p2p->p2p_scan_running) {
    1391                 :         29 :                 p2p_dbg(p2p, "p2p_scan running - delay connect send");
    1392                 :         29 :                 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
    1393                 :         29 :                 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
    1394                 :         29 :                 return 0;
    1395                 :            :         }
    1396                 :         30 :         p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
    1397                 :            : 
    1398                 :         59 :         return p2p_connect_send(p2p, dev);
    1399                 :            : }
    1400                 :            : 
    1401                 :            : 
    1402                 :         43 : int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
    1403                 :            :                   enum p2p_wps_method wps_method,
    1404                 :            :                   int go_intent, const u8 *own_interface_addr,
    1405                 :            :                   unsigned int force_freq, int persistent_group,
    1406                 :            :                   const u8 *force_ssid, size_t force_ssid_len,
    1407                 :            :                   unsigned int pref_freq, u16 oob_pw_id)
    1408                 :            : {
    1409                 :            :         struct p2p_device *dev;
    1410                 :            : 
    1411                 :         43 :         p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
    1412                 :            :                 "  GO Intent=%d  Intended Interface Address=" MACSTR
    1413                 :            :                 " wps_method=%d  persistent_group=%d oob_pw_id=%u",
    1414                 :        516 :                 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
    1415                 :            :                 wps_method, persistent_group, oob_pw_id);
    1416                 :            : 
    1417                 :         43 :         dev = p2p_get_device(p2p, peer_addr);
    1418         [ -  + ]:         43 :         if (dev == NULL) {
    1419                 :          0 :                 p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
    1420                 :          0 :                         MAC2STR(peer_addr));
    1421                 :          0 :                 return -1;
    1422                 :            :         }
    1423                 :            : 
    1424         [ -  + ]:         43 :         if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
    1425                 :            :                                 15) < 0)
    1426                 :          0 :                 return -1;
    1427                 :            : 
    1428                 :         43 :         p2p->ssid_set = 0;
    1429         [ -  + ]:         43 :         if (force_ssid) {
    1430                 :          0 :                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
    1431                 :            :                                   force_ssid, force_ssid_len);
    1432                 :          0 :                 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
    1433                 :          0 :                 p2p->ssid_len = force_ssid_len;
    1434                 :          0 :                 p2p->ssid_set = 1;
    1435                 :            :         }
    1436                 :            : 
    1437                 :         43 :         dev->flags &= ~P2P_DEV_NOT_YET_READY;
    1438                 :         43 :         dev->flags &= ~P2P_DEV_USER_REJECTED;
    1439                 :         43 :         dev->go_neg_req_sent = 0;
    1440                 :         43 :         dev->go_state = UNKNOWN_GO;
    1441                 :         43 :         p2p_set_dev_persistent(dev, persistent_group);
    1442                 :         43 :         p2p->go_intent = go_intent;
    1443                 :         43 :         os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
    1444                 :            : 
    1445                 :         43 :         dev->wps_method = wps_method;
    1446                 :         43 :         dev->oob_pw_id = oob_pw_id;
    1447                 :         43 :         dev->status = P2P_SC_SUCCESS;
    1448                 :            : 
    1449                 :         43 :         return 0;
    1450                 :            : }
    1451                 :            : 
    1452                 :            : 
    1453                 :         14 : void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
    1454                 :            :                       struct p2p_device *dev, struct p2p_message *msg)
    1455                 :            : {
    1456                 :         14 :         os_get_reltime(&dev->last_seen);
    1457                 :            : 
    1458                 :         14 :         p2p_copy_wps_info(p2p, dev, 0, msg);
    1459                 :            : 
    1460         [ +  - ]:         14 :         if (msg->listen_channel) {
    1461                 :            :                 int freq;
    1462                 :         14 :                 freq = p2p_channel_to_freq(msg->listen_channel[3],
    1463                 :         14 :                                            msg->listen_channel[4]);
    1464         [ -  + ]:         14 :                 if (freq < 0) {
    1465                 :          0 :                         p2p_dbg(p2p, "Unknown peer Listen channel: "
    1466                 :            :                                 "country=%c%c(0x%02x) reg_class=%u channel=%u",
    1467                 :          0 :                                 msg->listen_channel[0],
    1468                 :          0 :                                 msg->listen_channel[1],
    1469                 :          0 :                                 msg->listen_channel[2],
    1470                 :          0 :                                 msg->listen_channel[3],
    1471                 :          0 :                                 msg->listen_channel[4]);
    1472                 :            :                 } else {
    1473                 :         14 :                         p2p_dbg(p2p, "Update peer " MACSTR
    1474                 :            :                                 " Listen channel: %u -> %u MHz",
    1475                 :         84 :                                 MAC2STR(dev->info.p2p_device_addr),
    1476                 :            :                                 dev->listen_freq, freq);
    1477                 :         14 :                         dev->listen_freq = freq;
    1478                 :            :                 }
    1479                 :            :         }
    1480                 :            : 
    1481         [ +  + ]:         14 :         if (msg->wfd_subelems) {
    1482                 :         11 :                 wpabuf_free(dev->info.wfd_subelems);
    1483                 :         11 :                 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
    1484                 :            :         }
    1485                 :            : 
    1486         [ +  + ]:         14 :         if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
    1487                 :          5 :                 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
    1488                 :          5 :                 p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
    1489                 :            :         } else {
    1490                 :          9 :                 p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
    1491                 :            :                         MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
    1492                 :            :                         "listen_freq=%d",
    1493                 :         54 :                         MAC2STR(dev->info.p2p_device_addr),
    1494                 :         18 :                         dev->info.dev_capab, dev->info.group_capab,
    1495                 :          9 :                         dev->info.device_name, dev->listen_freq);
    1496                 :            :         }
    1497                 :            : 
    1498                 :         14 :         dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
    1499                 :            : 
    1500         [ -  + ]:         14 :         if (dev->flags & P2P_DEV_USER_REJECTED) {
    1501                 :          0 :                 p2p_dbg(p2p, "Do not report rejected device");
    1502                 :         14 :                 return;
    1503                 :            :         }
    1504                 :            : 
    1505                 :         14 :         p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
    1506                 :         14 :                             !(dev->flags & P2P_DEV_REPORTED_ONCE));
    1507                 :         14 :         dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
    1508                 :            : }
    1509                 :            : 
    1510                 :            : 
    1511                 :         72 : void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
    1512                 :            : {
    1513                 :         72 :         os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
    1514                 :         72 :         p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
    1515                 :         72 :         os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
    1516                 :            :                   p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
    1517                 :         72 :         *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
    1518                 :         72 : }
    1519                 :            : 
    1520                 :            : 
    1521                 :         27 : int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
    1522                 :            : {
    1523                 :         27 :         p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
    1524                 :         27 :         p2p_random(params->passphrase, 8);
    1525                 :         27 :         return 0;
    1526                 :            : }
    1527                 :            : 
    1528                 :            : 
    1529                 :         90 : void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
    1530                 :            : {
    1531                 :            :         struct p2p_go_neg_results res;
    1532                 :         90 :         int go = peer->go_state == LOCAL_GO;
    1533                 :            :         struct p2p_channels intersection;
    1534                 :            :         int freqs;
    1535                 :            :         size_t i, j;
    1536                 :            : 
    1537         [ +  + ]:         90 :         p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
    1538                 :        540 :                 MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
    1539                 :            : 
    1540                 :         90 :         os_memset(&res, 0, sizeof(res));
    1541                 :         90 :         res.role_go = go;
    1542                 :         90 :         os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
    1543                 :         90 :         os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
    1544                 :         90 :         res.wps_method = peer->wps_method;
    1545         [ +  + ]:         90 :         if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
    1546         [ +  + ]:         10 :                 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
    1547                 :          8 :                         res.persistent_group = 2;
    1548                 :            :                 else
    1549                 :          2 :                         res.persistent_group = 1;
    1550                 :            :         }
    1551                 :            : 
    1552         [ +  + ]:         90 :         if (go) {
    1553                 :            :                 /* Setup AP mode for WPS provisioning */
    1554                 :         45 :                 res.freq = p2p_channel_to_freq(p2p->op_reg_class,
    1555                 :         45 :                                                p2p->op_channel);
    1556                 :         45 :                 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
    1557                 :         45 :                 res.ssid_len = p2p->ssid_len;
    1558                 :         45 :                 p2p_random(res.passphrase, 8);
    1559                 :            :         } else {
    1560                 :         45 :                 res.freq = peer->oper_freq;
    1561         [ +  - ]:         45 :                 if (p2p->ssid_len) {
    1562                 :         45 :                         os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
    1563                 :         45 :                         res.ssid_len = p2p->ssid_len;
    1564                 :            :                 }
    1565                 :            :         }
    1566                 :            : 
    1567                 :         90 :         p2p_channels_dump(p2p, "own channels", &p2p->channels);
    1568                 :         90 :         p2p_channels_dump(p2p, "peer channels", &peer->channels);
    1569                 :         90 :         p2p_channels_intersect(&p2p->channels, &peer->channels,
    1570                 :            :                                &intersection);
    1571         [ +  + ]:         90 :         if (go) {
    1572                 :         45 :                 p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
    1573                 :         45 :                 p2p_channels_dump(p2p, "intersection after no-GO removal",
    1574                 :            :                                   &intersection);
    1575                 :            :         }
    1576                 :         90 :         freqs = 0;
    1577         [ +  + ]:        180 :         for (i = 0; i < intersection.reg_classes; i++) {
    1578                 :         90 :                 struct p2p_reg_class *c = &intersection.reg_class[i];
    1579         [ -  + ]:         90 :                 if (freqs + 1 == P2P_MAX_CHANNELS)
    1580                 :          0 :                         break;
    1581         [ +  + ]:        860 :                 for (j = 0; j < c->channels; j++) {
    1582                 :            :                         int freq;
    1583         [ -  + ]:        770 :                         if (freqs + 1 == P2P_MAX_CHANNELS)
    1584                 :          0 :                                 break;
    1585                 :        770 :                         freq = p2p_channel_to_freq(c->reg_class, c->channel[j]);
    1586         [ -  + ]:        770 :                         if (freq < 0)
    1587                 :          0 :                                 continue;
    1588                 :        770 :                         res.freq_list[freqs++] = freq;
    1589                 :            :                 }
    1590                 :            :         }
    1591                 :            : 
    1592         [ +  + ]:         90 :         res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
    1593                 :            : 
    1594                 :         90 :         p2p_clear_timeout(p2p);
    1595                 :         90 :         p2p->ssid_set = 0;
    1596                 :         90 :         peer->go_neg_req_sent = 0;
    1597                 :         90 :         peer->wps_method = WPS_NOT_READY;
    1598                 :         90 :         peer->oob_pw_id = 0;
    1599                 :            : 
    1600                 :         90 :         p2p_set_state(p2p, P2P_PROVISIONING);
    1601                 :         90 :         p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
    1602                 :         90 : }
    1603                 :            : 
    1604                 :            : 
    1605                 :        317 : static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
    1606                 :            :                               const u8 *data, size_t len, int rx_freq)
    1607                 :            : {
    1608                 :        317 :         p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
    1609                 :        317 :         wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
    1610                 :            : 
    1611         [ +  + ]:        317 :         if (len < 1)
    1612                 :        317 :                 return;
    1613                 :            : 
    1614   [ +  +  +  +  :        316 :         switch (data[0]) {
          +  +  +  +  +  
                      - ]
    1615                 :            :         case P2P_GO_NEG_REQ:
    1616                 :         74 :                 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
    1617                 :         74 :                 break;
    1618                 :            :         case P2P_GO_NEG_RESP:
    1619                 :         57 :                 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
    1620                 :         57 :                 break;
    1621                 :            :         case P2P_GO_NEG_CONF:
    1622                 :         45 :                 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
    1623                 :         45 :                 break;
    1624                 :            :         case P2P_INVITATION_REQ:
    1625                 :         54 :                 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
    1626                 :            :                                            rx_freq);
    1627                 :         54 :                 break;
    1628                 :            :         case P2P_INVITATION_RESP:
    1629                 :         13 :                 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    1630                 :         13 :                 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
    1631                 :         13 :                 break;
    1632                 :            :         case P2P_PROV_DISC_REQ:
    1633                 :         35 :                 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
    1634                 :         35 :                 break;
    1635                 :            :         case P2P_PROV_DISC_RESP:
    1636                 :         32 :                 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
    1637                 :         32 :                 break;
    1638                 :            :         case P2P_DEV_DISC_REQ:
    1639                 :          3 :                 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
    1640                 :          3 :                 break;
    1641                 :            :         case P2P_DEV_DISC_RESP:
    1642                 :          3 :                 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
    1643                 :          3 :                 break;
    1644                 :            :         default:
    1645                 :          0 :                 p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
    1646                 :          0 :                         data[0]);
    1647                 :          0 :                 break;
    1648                 :            :         }
    1649                 :            : }
    1650                 :            : 
    1651                 :            : 
    1652                 :        387 : static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
    1653                 :            :                                  const u8 *sa, const u8 *bssid, const u8 *data,
    1654                 :            :                                  size_t len, int freq)
    1655                 :            : {
    1656         [ -  + ]:        387 :         if (len < 1)
    1657                 :          0 :                 return;
    1658                 :            : 
    1659   [ +  +  +  +  :        387 :         switch (data[0]) {
                   +  - ]
    1660                 :            :         case WLAN_PA_VENDOR_SPECIFIC:
    1661                 :        317 :                 data++;
    1662                 :        317 :                 len--;
    1663         [ -  + ]:        317 :                 if (len < 3)
    1664                 :          0 :                         return;
    1665         [ -  + ]:        317 :                 if (WPA_GET_BE24(data) != OUI_WFA)
    1666                 :          0 :                         return;
    1667                 :            : 
    1668                 :        317 :                 data += 3;
    1669                 :        317 :                 len -= 3;
    1670         [ -  + ]:        317 :                 if (len < 1)
    1671                 :          0 :                         return;
    1672                 :            : 
    1673         [ -  + ]:        317 :                 if (*data != P2P_OUI_TYPE)
    1674                 :          0 :                         return;
    1675                 :            : 
    1676                 :        317 :                 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
    1677                 :        317 :                 break;
    1678                 :            :         case WLAN_PA_GAS_INITIAL_REQ:
    1679                 :         22 :                 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
    1680                 :         22 :                 break;
    1681                 :            :         case WLAN_PA_GAS_INITIAL_RESP:
    1682                 :         22 :                 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
    1683                 :         22 :                 break;
    1684                 :            :         case WLAN_PA_GAS_COMEBACK_REQ:
    1685                 :         13 :                 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
    1686                 :         13 :                 break;
    1687                 :            :         case WLAN_PA_GAS_COMEBACK_RESP:
    1688                 :         13 :                 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
    1689                 :        387 :                 break;
    1690                 :            :         }
    1691                 :            : }
    1692                 :            : 
    1693                 :            : 
    1694                 :        389 : void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
    1695                 :            :                    const u8 *bssid, u8 category,
    1696                 :            :                    const u8 *data, size_t len, int freq)
    1697                 :            : {
    1698         [ +  + ]:        389 :         if (category == WLAN_ACTION_PUBLIC) {
    1699                 :        387 :                 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
    1700                 :        387 :                 return;
    1701                 :            :         }
    1702                 :            : 
    1703         [ -  + ]:          2 :         if (category != WLAN_ACTION_VENDOR_SPECIFIC)
    1704                 :          0 :                 return;
    1705                 :            : 
    1706         [ -  + ]:          2 :         if (len < 4)
    1707                 :          0 :                 return;
    1708                 :            : 
    1709         [ -  + ]:          2 :         if (WPA_GET_BE24(data) != OUI_WFA)
    1710                 :          0 :                 return;
    1711                 :          2 :         data += 3;
    1712                 :          2 :         len -= 3;
    1713                 :            : 
    1714         [ -  + ]:          2 :         if (*data != P2P_OUI_TYPE)
    1715                 :          0 :                 return;
    1716                 :          2 :         data++;
    1717                 :          2 :         len--;
    1718                 :            : 
    1719                 :            :         /* P2P action frame */
    1720                 :          2 :         p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
    1721                 :          2 :         wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
    1722                 :            : 
    1723         [ -  + ]:          2 :         if (len < 1)
    1724                 :          0 :                 return;
    1725   [ -  +  +  -  :          2 :         switch (data[0]) {
                      - ]
    1726                 :            :         case P2P_NOA:
    1727                 :          0 :                 p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
    1728                 :            :                 /* TODO */
    1729                 :          0 :                 break;
    1730                 :            :         case P2P_PRESENCE_REQ:
    1731                 :          1 :                 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
    1732                 :          1 :                 break;
    1733                 :            :         case P2P_PRESENCE_RESP:
    1734                 :          1 :                 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
    1735                 :          1 :                 break;
    1736                 :            :         case P2P_GO_DISC_REQ:
    1737                 :          0 :                 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
    1738                 :          0 :                 break;
    1739                 :            :         default:
    1740                 :          0 :                 p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
    1741                 :        389 :                 break;
    1742                 :            :         }
    1743                 :            : }
    1744                 :            : 
    1745                 :            : 
    1746                 :         16 : static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
    1747                 :            : {
    1748                 :         16 :         struct p2p_data *p2p = eloop_ctx;
    1749         [ -  + ]:         16 :         if (p2p->go_neg_peer == NULL)
    1750                 :         16 :                 return;
    1751                 :         16 :         p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
    1752                 :         16 :         p2p->go_neg_peer->status = P2P_SC_SUCCESS;
    1753                 :         16 :         p2p_connect_send(p2p, p2p->go_neg_peer);
    1754                 :            : }
    1755                 :            : 
    1756                 :            : 
    1757                 :          0 : static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
    1758                 :            : {
    1759                 :          0 :         struct p2p_data *p2p = eloop_ctx;
    1760         [ #  # ]:          0 :         if (p2p->invite_peer == NULL)
    1761                 :          0 :                 return;
    1762                 :          0 :         p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
    1763                 :          0 :         p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
    1764                 :            :                         p2p->invite_dev_pw_id);
    1765                 :            : }
    1766                 :            : 
    1767                 :            : 
    1768                 :        542 : static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
    1769                 :            :                                        const u8 *ie, size_t ie_len)
    1770                 :            : {
    1771                 :            :         struct p2p_message msg;
    1772                 :            :         struct p2p_device *dev;
    1773                 :            : 
    1774                 :        542 :         os_memset(&msg, 0, sizeof(msg));
    1775 [ +  - ][ +  + ]:        542 :         if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
    1776                 :            :         {
    1777                 :          2 :                 p2p_parse_free(&msg);
    1778                 :          2 :                 return; /* not a P2P probe */
    1779                 :            :         }
    1780                 :            : 
    1781 [ +  - ][ +  + ]:        540 :         if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
                 [ -  + ]
    1782                 :        418 :             os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
    1783                 :            :             != 0) {
    1784                 :            :                 /* The Probe Request is not part of P2P Device Discovery. It is
    1785                 :            :                  * not known whether the source address of the frame is the P2P
    1786                 :            :                  * Device Address or P2P Interface Address. Do not add a new
    1787                 :            :                  * peer entry based on this frames.
    1788                 :            :                  */
    1789                 :        122 :                 p2p_parse_free(&msg);
    1790                 :        122 :                 return;
    1791                 :            :         }
    1792                 :            : 
    1793                 :        418 :         dev = p2p_get_device(p2p, addr);
    1794         [ +  + ]:        418 :         if (dev) {
    1795 [ +  + ][ +  - ]:        318 :                 if (dev->country[0] == 0 && msg.listen_channel)
    1796                 :         45 :                         os_memcpy(dev->country, msg.listen_channel, 3);
    1797                 :        318 :                 os_get_reltime(&dev->last_seen);
    1798                 :        318 :                 p2p_parse_free(&msg);
    1799                 :        318 :                 return; /* already known */
    1800                 :            :         }
    1801                 :            : 
    1802                 :        100 :         dev = p2p_create_device(p2p, addr);
    1803         [ -  + ]:        100 :         if (dev == NULL) {
    1804                 :          0 :                 p2p_parse_free(&msg);
    1805                 :          0 :                 return;
    1806                 :            :         }
    1807                 :            : 
    1808                 :        100 :         os_get_reltime(&dev->last_seen);
    1809                 :        100 :         dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
    1810                 :            : 
    1811         [ +  - ]:        100 :         if (msg.listen_channel) {
    1812                 :        100 :                 os_memcpy(dev->country, msg.listen_channel, 3);
    1813                 :        100 :                 dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
    1814                 :        100 :                                                        msg.listen_channel[4]);
    1815                 :            :         }
    1816                 :            : 
    1817                 :        100 :         p2p_copy_wps_info(p2p, dev, 1, &msg);
    1818                 :            : 
    1819         [ +  + ]:        100 :         if (msg.wfd_subelems) {
    1820                 :         89 :                 wpabuf_free(dev->info.wfd_subelems);
    1821                 :         89 :                 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
    1822                 :            :         }
    1823                 :            : 
    1824                 :        100 :         p2p_parse_free(&msg);
    1825                 :            : 
    1826                 :        542 :         p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
    1827                 :            :                 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
    1828                 :        700 :                 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
    1829                 :        100 :                 dev->info.group_capab, dev->info.device_name,
    1830                 :            :                 dev->listen_freq);
    1831                 :            : }
    1832                 :            : 
    1833                 :            : 
    1834                 :          4 : struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
    1835                 :            :                                                 const u8 *addr,
    1836                 :            :                                                 struct p2p_message *msg)
    1837                 :            : {
    1838                 :            :         struct p2p_device *dev;
    1839                 :            : 
    1840                 :          4 :         dev = p2p_get_device(p2p, addr);
    1841         [ -  + ]:          4 :         if (dev) {
    1842                 :          0 :                 os_get_reltime(&dev->last_seen);
    1843                 :          0 :                 return dev; /* already known */
    1844                 :            :         }
    1845                 :            : 
    1846                 :          4 :         dev = p2p_create_device(p2p, addr);
    1847         [ -  + ]:          4 :         if (dev == NULL)
    1848                 :          0 :                 return NULL;
    1849                 :            : 
    1850                 :          4 :         p2p_add_dev_info(p2p, addr, dev, msg);
    1851                 :            : 
    1852                 :          4 :         return dev;
    1853                 :            : }
    1854                 :            : 
    1855                 :            : 
    1856                 :         20 : static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
    1857                 :            : {
    1858         [ +  + ]:         20 :         if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
    1859                 :          2 :                 return 1;
    1860   [ -  +  #  # ]:         18 :         if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
    1861         [ #  # ]:          0 :             WPA_GET_BE32(&req_dev_type[2]) == 0 &&
    1862                 :          0 :             WPA_GET_BE16(&req_dev_type[6]) == 0)
    1863                 :          0 :                 return 1; /* Category match with wildcard OUI/sub-category */
    1864                 :         20 :         return 0;
    1865                 :            : }
    1866                 :            : 
    1867                 :            : 
    1868                 :         20 : int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
    1869                 :            :                         size_t num_req_dev_type)
    1870                 :            : {
    1871                 :            :         size_t i;
    1872         [ +  + ]:         38 :         for (i = 0; i < num_req_dev_type; i++) {
    1873         [ +  + ]:         20 :                 if (dev_type_match(dev_type, req_dev_type[i]))
    1874                 :          2 :                         return 1;
    1875                 :            :         }
    1876                 :         20 :         return 0;
    1877                 :            : }
    1878                 :            : 
    1879                 :            : 
    1880                 :            : /**
    1881                 :            :  * p2p_match_dev_type - Match local device type with requested type
    1882                 :            :  * @p2p: P2P module context from p2p_init()
    1883                 :            :  * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
    1884                 :            :  * Returns: 1 on match, 0 on mismatch
    1885                 :            :  *
    1886                 :            :  * This function can be used to match the Requested Device Type attribute in
    1887                 :            :  * WPS IE with the local device types for deciding whether to reply to a Probe
    1888                 :            :  * Request frame.
    1889                 :            :  */
    1890                 :        512 : int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
    1891                 :            : {
    1892                 :            :         struct wps_parse_attr attr;
    1893                 :            :         size_t i;
    1894                 :            : 
    1895         [ -  + ]:        512 :         if (wps_parse_msg(wps, &attr))
    1896                 :          0 :                 return 1; /* assume no Requested Device Type attributes */
    1897                 :            : 
    1898         [ +  + ]:        512 :         if (attr.num_req_dev_type == 0)
    1899                 :        504 :                 return 1; /* no Requested Device Type attributes -> match */
    1900                 :            : 
    1901         [ -  + ]:          8 :         if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
    1902                 :            :                                 attr.num_req_dev_type))
    1903                 :          0 :                 return 1; /* Own Primary Device Type matches */
    1904                 :            : 
    1905         [ +  + ]:         11 :         for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
    1906         [ +  + ]:          4 :                 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
    1907                 :            :                                         attr.req_dev_type,
    1908                 :            :                                         attr.num_req_dev_type))
    1909                 :          1 :                 return 1; /* Own Secondary Device Type matches */
    1910                 :            : 
    1911                 :            :         /* No matching device type found */
    1912                 :        512 :         return 0;
    1913                 :            : }
    1914                 :            : 
    1915                 :            : 
    1916                 :       1108 : struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
    1917                 :            : {
    1918                 :            :         struct wpabuf *buf;
    1919                 :            :         u8 *len;
    1920                 :       1108 :         int pw_id = -1;
    1921                 :       1108 :         size_t extra = 0;
    1922                 :            : 
    1923                 :            : #ifdef CONFIG_WIFI_DISPLAY
    1924         [ +  + ]:       1108 :         if (p2p->wfd_ie_probe_resp)
    1925                 :        963 :                 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
    1926                 :            : #endif /* CONFIG_WIFI_DISPLAY */
    1927                 :            : 
    1928                 :       1108 :         buf = wpabuf_alloc(1000 + extra);
    1929         [ -  + ]:       1108 :         if (buf == NULL)
    1930                 :          0 :                 return NULL;
    1931                 :            : 
    1932         [ +  + ]:       1108 :         if (p2p->go_neg_peer) {
    1933                 :            :                 /* Advertise immediate availability of WPS credential */
    1934                 :         60 :                 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
    1935                 :            :         }
    1936                 :            : 
    1937         [ -  + ]:       1108 :         if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
    1938                 :          0 :                 p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
    1939                 :          0 :                 wpabuf_free(buf);
    1940                 :          0 :                 return NULL;
    1941                 :            :         }
    1942                 :            : 
    1943                 :            : #ifdef CONFIG_WIFI_DISPLAY
    1944         [ +  + ]:       1108 :         if (p2p->wfd_ie_probe_resp)
    1945                 :        963 :                 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
    1946                 :            : #endif /* CONFIG_WIFI_DISPLAY */
    1947                 :            : 
    1948                 :            :         /* P2P IE */
    1949                 :       1108 :         len = p2p_buf_add_ie_hdr(buf);
    1950                 :       1108 :         p2p_buf_add_capability(buf, p2p->dev_capab &
    1951                 :            :                                ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
    1952         [ -  + ]:       1108 :         if (p2p->ext_listen_interval)
    1953                 :          0 :                 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
    1954                 :          0 :                                               p2p->ext_listen_interval);
    1955                 :       1108 :         p2p_buf_add_device_info(buf, p2p, NULL);
    1956                 :       1108 :         p2p_buf_update_ie_hdr(buf, len);
    1957                 :            : 
    1958                 :       1108 :         return buf;
    1959                 :            : }
    1960                 :            : 
    1961                 :            : 
    1962                 :            : static enum p2p_probe_req_status
    1963                 :        542 : p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
    1964                 :            :                 const u8 *bssid, const u8 *ie, size_t ie_len)
    1965                 :            : {
    1966                 :            :         struct ieee802_11_elems elems;
    1967                 :            :         struct wpabuf *buf;
    1968                 :            :         struct ieee80211_mgmt *resp;
    1969                 :            :         struct p2p_message msg;
    1970                 :            :         struct wpabuf *ies;
    1971                 :            : 
    1972 [ +  + ][ +  + ]:        542 :         if (!p2p->in_listen || !p2p->drv_in_listen) {
    1973                 :            :                 /* not in Listen state - ignore Probe Request */
    1974                 :        200 :                 p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request",
    1975                 :            :                         p2p->in_listen, p2p->drv_in_listen);
    1976                 :        200 :                 return P2P_PREQ_NOT_LISTEN;
    1977                 :            :         }
    1978                 :            : 
    1979         [ -  + ]:        342 :         if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
    1980                 :            :             ParseFailed) {
    1981                 :            :                 /* Ignore invalid Probe Request frames */
    1982                 :          0 :                 p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it");
    1983                 :          0 :                 return P2P_PREQ_MALFORMED;
    1984                 :            :         }
    1985                 :            : 
    1986         [ -  + ]:        342 :         if (elems.p2p == NULL) {
    1987                 :            :                 /* not a P2P probe - ignore it */
    1988                 :          0 :                 p2p_dbg(p2p, "Not a P2P probe - ignore it");
    1989                 :          0 :                 return P2P_PREQ_NOT_P2P;
    1990                 :            :         }
    1991                 :            : 
    1992 [ +  - ][ -  + ]:        342 :         if (dst && !is_broadcast_ether_addr(dst) &&
                 [ #  # ]
    1993                 :          0 :             os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
    1994                 :            :                 /* Not sent to the broadcast address or our P2P Device Address
    1995                 :            :                  */
    1996                 :          0 :                 p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it",
    1997                 :          0 :                         MAC2STR(dst));
    1998                 :          0 :                 return P2P_PREQ_NOT_PROCESSED;
    1999                 :            :         }
    2000                 :            : 
    2001 [ +  - ][ -  + ]:        342 :         if (bssid && !is_broadcast_ether_addr(bssid)) {
    2002                 :            :                 /* Not sent to the Wildcard BSSID */
    2003                 :          0 :                 p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it",
    2004                 :          0 :                         MAC2STR(bssid));
    2005                 :          0 :                 return P2P_PREQ_NOT_PROCESSED;
    2006                 :            :         }
    2007                 :            : 
    2008 [ +  - ][ +  + ]:        342 :         if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
                 [ -  + ]
    2009                 :        341 :             os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
    2010                 :            :             0) {
    2011                 :            :                 /* not using P2P Wildcard SSID - ignore */
    2012                 :          1 :                 p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it");
    2013                 :          1 :                 return P2P_PREQ_NOT_PROCESSED;
    2014                 :            :         }
    2015                 :            : 
    2016         [ -  + ]:        341 :         if (supp_rates_11b_only(&elems)) {
    2017                 :            :                 /* Indicates support for 11b rates only */
    2018                 :          0 :                 p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it");
    2019                 :          0 :                 return P2P_PREQ_NOT_P2P;
    2020                 :            :         }
    2021                 :            : 
    2022                 :        341 :         os_memset(&msg, 0, sizeof(msg));
    2023         [ -  + ]:        341 :         if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
    2024                 :            :                 /* Could not parse P2P attributes */
    2025                 :          0 :                 p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it");
    2026                 :          0 :                 return P2P_PREQ_NOT_P2P;
    2027                 :            :         }
    2028                 :            : 
    2029 [ +  + ][ +  + ]:        341 :         if (msg.device_id &&
    2030                 :          4 :             os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
    2031                 :            :                 /* Device ID did not match */
    2032                 :          3 :                 p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it",
    2033                 :         18 :                         MAC2STR(msg.device_id));
    2034                 :          3 :                 p2p_parse_free(&msg);
    2035                 :          3 :                 return P2P_PREQ_NOT_PROCESSED;
    2036                 :            :         }
    2037                 :            : 
    2038                 :            :         /* Check Requested Device Type match */
    2039   [ +  +  +  + ]:        675 :         if (msg.wps_attributes &&
    2040                 :        337 :             !p2p_match_dev_type(p2p, msg.wps_attributes)) {
    2041                 :            :                 /* No match with Requested Device Type */
    2042                 :          3 :                 p2p_dbg(p2p, "Probe Req requestred Device Type did not match - ignore it");
    2043                 :          3 :                 p2p_parse_free(&msg);
    2044                 :          3 :                 return P2P_PREQ_NOT_PROCESSED;
    2045                 :            :         }
    2046                 :        335 :         p2p_parse_free(&msg);
    2047                 :            : 
    2048         [ -  + ]:        335 :         if (!p2p->cfg->send_probe_resp) {
    2049                 :            :                 /* Response generated elsewhere */
    2050                 :          0 :                 p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response");
    2051                 :          0 :                 return P2P_PREQ_NOT_PROCESSED;
    2052                 :            :         }
    2053                 :            : 
    2054                 :        335 :         p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
    2055                 :            : 
    2056                 :            :         /*
    2057                 :            :          * We do not really have a specific BSS that this frame is advertising,
    2058                 :            :          * so build a frame that has some information in valid format. This is
    2059                 :            :          * really only used for discovery purposes, not to learn exact BSS
    2060                 :            :          * parameters.
    2061                 :            :          */
    2062                 :        335 :         ies = p2p_build_probe_resp_ies(p2p);
    2063         [ -  + ]:        335 :         if (ies == NULL)
    2064                 :          0 :                 return P2P_PREQ_NOT_PROCESSED;
    2065                 :            : 
    2066                 :        335 :         buf = wpabuf_alloc(200 + wpabuf_len(ies));
    2067         [ -  + ]:        335 :         if (buf == NULL) {
    2068                 :          0 :                 wpabuf_free(ies);
    2069                 :          0 :                 return P2P_PREQ_NOT_PROCESSED;
    2070                 :            :         }
    2071                 :            : 
    2072                 :        335 :         resp = NULL;
    2073                 :        335 :         resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
    2074                 :            : 
    2075                 :        335 :         resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
    2076                 :            :                                            (WLAN_FC_STYPE_PROBE_RESP << 4));
    2077                 :        335 :         os_memcpy(resp->da, addr, ETH_ALEN);
    2078                 :        335 :         os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
    2079                 :        335 :         os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
    2080                 :        335 :         resp->u.probe_resp.beacon_int = host_to_le16(100);
    2081                 :            :         /* hardware or low-level driver will setup seq_ctrl and timestamp */
    2082                 :        335 :         resp->u.probe_resp.capab_info =
    2083                 :            :                 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
    2084                 :            :                              WLAN_CAPABILITY_PRIVACY |
    2085                 :            :                              WLAN_CAPABILITY_SHORT_SLOT_TIME);
    2086                 :            : 
    2087                 :        335 :         wpabuf_put_u8(buf, WLAN_EID_SSID);
    2088                 :        335 :         wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
    2089                 :        335 :         wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
    2090                 :            : 
    2091                 :        335 :         wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
    2092                 :        335 :         wpabuf_put_u8(buf, 8);
    2093                 :        335 :         wpabuf_put_u8(buf, (60 / 5) | 0x80);
    2094                 :        335 :         wpabuf_put_u8(buf, 90 / 5);
    2095                 :        335 :         wpabuf_put_u8(buf, (120 / 5) | 0x80);
    2096                 :        335 :         wpabuf_put_u8(buf, 180 / 5);
    2097                 :        335 :         wpabuf_put_u8(buf, (240 / 5) | 0x80);
    2098                 :        335 :         wpabuf_put_u8(buf, 360 / 5);
    2099                 :        335 :         wpabuf_put_u8(buf, 480 / 5);
    2100                 :        335 :         wpabuf_put_u8(buf, 540 / 5);
    2101                 :            : 
    2102                 :        335 :         wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
    2103                 :        335 :         wpabuf_put_u8(buf, 1);
    2104                 :        335 :         wpabuf_put_u8(buf, p2p->cfg->channel);
    2105                 :            : 
    2106                 :        335 :         wpabuf_put_buf(buf, ies);
    2107                 :        335 :         wpabuf_free(ies);
    2108                 :            : 
    2109                 :        335 :         p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
    2110                 :            : 
    2111                 :        335 :         wpabuf_free(buf);
    2112                 :            : 
    2113                 :        542 :         return P2P_PREQ_NOT_PROCESSED;
    2114                 :            : }
    2115                 :            : 
    2116                 :            : 
    2117                 :            : enum p2p_probe_req_status
    2118                 :        542 : p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
    2119                 :            :                  const u8 *bssid, const u8 *ie, size_t ie_len)
    2120                 :            : {
    2121                 :            :         enum p2p_probe_req_status res;
    2122                 :            : 
    2123                 :        542 :         p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
    2124                 :            : 
    2125                 :        542 :         res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
    2126                 :            : 
    2127 [ +  + ][ +  + ]:        542 :         if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
                 [ +  - ]
    2128         [ +  - ]:         16 :             p2p->go_neg_peer &&
    2129                 :         16 :             os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
    2130         [ +  - ]:         16 :             == 0 &&
    2131                 :         16 :             !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
    2132                 :            :                 /* Received a Probe Request from GO Negotiation peer */
    2133                 :         16 :                 p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
    2134                 :         16 :                 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
    2135                 :         16 :                 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
    2136                 :         16 :                 return P2P_PREQ_PROCESSED;
    2137                 :            :         }
    2138                 :            : 
    2139 [ +  - ][ -  + ]:        526 :         if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
                 [ #  # ]
    2140         [ #  # ]:          0 :             p2p->invite_peer &&
    2141         [ #  # ]:          0 :             (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) &&
    2142                 :          0 :             os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
    2143                 :            :             == 0) {
    2144                 :            :                 /* Received a Probe Request from Invite peer */
    2145                 :          0 :                 p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
    2146                 :          0 :                 eloop_cancel_timeout(p2p_invite_start, p2p, NULL);
    2147                 :          0 :                 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
    2148                 :          0 :                 return P2P_PREQ_PROCESSED;
    2149                 :            :         }
    2150                 :            : 
    2151                 :        542 :         return res;
    2152                 :            : }
    2153                 :            : 
    2154                 :            : 
    2155                 :        523 : static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
    2156                 :            :                                     u8 *buf, size_t len, struct wpabuf *p2p_ie)
    2157                 :            : {
    2158                 :            :         struct wpabuf *tmp;
    2159                 :            :         u8 *lpos;
    2160                 :            :         size_t tmplen;
    2161                 :            :         int res;
    2162                 :            :         u8 group_capab;
    2163                 :            : 
    2164         [ +  + ]:        523 :         if (p2p_ie == NULL)
    2165                 :        516 :                 return 0; /* WLAN AP is not a P2P manager */
    2166                 :            : 
    2167                 :            :         /*
    2168                 :            :          * (Re)Association Request - P2P IE
    2169                 :            :          * P2P Capability attribute (shall be present)
    2170                 :            :          * P2P Interface attribute (present if concurrent device and
    2171                 :            :          *      P2P Management is enabled)
    2172                 :            :          */
    2173                 :          7 :         tmp = wpabuf_alloc(200);
    2174         [ -  + ]:          7 :         if (tmp == NULL)
    2175                 :          0 :                 return -1;
    2176                 :            : 
    2177                 :          7 :         lpos = p2p_buf_add_ie_hdr(tmp);
    2178                 :          7 :         group_capab = 0;
    2179         [ -  + ]:          7 :         if (p2p->num_groups > 0) {
    2180                 :          0 :                 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
    2181 [ #  # ][ #  # ]:          0 :                 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
    2182         [ #  # ]:          0 :                     (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
    2183                 :          0 :                     p2p->cross_connect)
    2184                 :          0 :                         group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
    2185                 :            :         }
    2186                 :          7 :         p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
    2187 [ +  - ][ -  + ]:          7 :         if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
    2188                 :          7 :             (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
    2189                 :          0 :                 p2p_buf_add_p2p_interface(tmp, p2p);
    2190                 :          7 :         p2p_buf_update_ie_hdr(tmp, lpos);
    2191                 :            : 
    2192                 :          7 :         tmplen = wpabuf_len(tmp);
    2193         [ -  + ]:          7 :         if (tmplen > len)
    2194                 :          0 :                 res = -1;
    2195                 :            :         else {
    2196                 :          7 :                 os_memcpy(buf, wpabuf_head(tmp), tmplen);
    2197                 :          7 :                 res = tmplen;
    2198                 :            :         }
    2199                 :          7 :         wpabuf_free(tmp);
    2200                 :            : 
    2201                 :        523 :         return res;
    2202                 :            : }
    2203                 :            : 
    2204                 :            : 
    2205                 :        693 : int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
    2206                 :            :                      size_t len, int p2p_group, struct wpabuf *p2p_ie)
    2207                 :            : {
    2208                 :            :         struct wpabuf *tmp;
    2209                 :            :         u8 *lpos;
    2210                 :            :         struct p2p_device *peer;
    2211                 :            :         size_t tmplen;
    2212                 :            :         int res;
    2213                 :        693 :         size_t extra = 0;
    2214                 :            : 
    2215         [ +  + ]:        693 :         if (!p2p_group)
    2216                 :        523 :                 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
    2217                 :            : 
    2218                 :            : #ifdef CONFIG_WIFI_DISPLAY
    2219         [ +  + ]:        170 :         if (p2p->wfd_ie_assoc_req)
    2220                 :        150 :                 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
    2221                 :            : #endif /* CONFIG_WIFI_DISPLAY */
    2222                 :            : 
    2223                 :            :         /*
    2224                 :            :          * (Re)Association Request - P2P IE
    2225                 :            :          * P2P Capability attribute (shall be present)
    2226                 :            :          * Extended Listen Timing (may be present)
    2227                 :            :          * P2P Device Info attribute (shall be present)
    2228                 :            :          */
    2229                 :        170 :         tmp = wpabuf_alloc(200 + extra);
    2230         [ -  + ]:        170 :         if (tmp == NULL)
    2231                 :          0 :                 return -1;
    2232                 :            : 
    2233                 :            : #ifdef CONFIG_WIFI_DISPLAY
    2234         [ +  + ]:        170 :         if (p2p->wfd_ie_assoc_req)
    2235                 :        150 :                 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
    2236                 :            : #endif /* CONFIG_WIFI_DISPLAY */
    2237                 :            : 
    2238         [ +  - ]:        170 :         peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
    2239                 :            : 
    2240                 :        170 :         lpos = p2p_buf_add_ie_hdr(tmp);
    2241                 :        170 :         p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
    2242         [ -  + ]:        170 :         if (p2p->ext_listen_interval)
    2243                 :          0 :                 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
    2244                 :          0 :                                               p2p->ext_listen_interval);
    2245                 :        170 :         p2p_buf_add_device_info(tmp, p2p, peer);
    2246                 :        170 :         p2p_buf_update_ie_hdr(tmp, lpos);
    2247                 :            : 
    2248                 :        170 :         tmplen = wpabuf_len(tmp);
    2249         [ -  + ]:        170 :         if (tmplen > len)
    2250                 :          0 :                 res = -1;
    2251                 :            :         else {
    2252                 :        170 :                 os_memcpy(buf, wpabuf_head(tmp), tmplen);
    2253                 :        170 :                 res = tmplen;
    2254                 :            :         }
    2255                 :        170 :         wpabuf_free(tmp);
    2256                 :            : 
    2257                 :        693 :         return res;
    2258                 :            : }
    2259                 :            : 
    2260                 :            : 
    2261                 :         34 : int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
    2262                 :            : {
    2263                 :            :         struct wpabuf *p2p_ie;
    2264                 :            :         int ret;
    2265                 :            : 
    2266                 :         34 :         p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
    2267         [ +  + ]:         34 :         if (p2p_ie == NULL)
    2268                 :         30 :                 return 0;
    2269                 :            : 
    2270                 :          4 :         ret = p2p_attr_text(p2p_ie, buf, end);
    2271                 :          4 :         wpabuf_free(p2p_ie);
    2272                 :         34 :         return ret;
    2273                 :            : }
    2274                 :            : 
    2275                 :            : 
    2276                 :        105 : int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
    2277                 :            : {
    2278                 :            :         struct p2p_message msg;
    2279                 :            : 
    2280                 :        105 :         os_memset(&msg, 0, sizeof(msg));
    2281         [ -  + ]:        105 :         if (p2p_parse_p2p_ie(p2p_ie, &msg))
    2282                 :          0 :                 return -1;
    2283                 :            : 
    2284         [ +  + ]:        105 :         if (msg.p2p_device_addr) {
    2285                 :        104 :                 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
    2286                 :        104 :                 return 0;
    2287         [ -  + ]:          1 :         } else if (msg.device_id) {
    2288                 :          0 :                 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
    2289                 :          0 :                 return 0;
    2290                 :            :         }
    2291                 :        105 :         return -1;
    2292                 :            : }
    2293                 :            : 
    2294                 :            : 
    2295                 :         17 : int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
    2296                 :            : {
    2297                 :            :         struct wpabuf *p2p_ie;
    2298                 :            :         int ret;
    2299                 :            : 
    2300                 :         17 :         p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
    2301                 :            :                                              P2P_IE_VENDOR_TYPE);
    2302         [ +  + ]:         17 :         if (p2p_ie == NULL)
    2303                 :          2 :                 return -1;
    2304                 :         15 :         ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
    2305                 :         15 :         wpabuf_free(p2p_ie);
    2306                 :         17 :         return ret;
    2307                 :            : }
    2308                 :            : 
    2309                 :            : 
    2310                 :         90 : static void p2p_clear_go_neg(struct p2p_data *p2p)
    2311                 :            : {
    2312                 :         90 :         p2p->go_neg_peer = NULL;
    2313                 :         90 :         p2p_clear_timeout(p2p);
    2314                 :         90 :         p2p_set_state(p2p, P2P_IDLE);
    2315                 :         90 : }
    2316                 :            : 
    2317                 :            : 
    2318                 :        120 : void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
    2319                 :            : {
    2320         [ +  + ]:        120 :         if (p2p->go_neg_peer == NULL) {
    2321                 :         32 :                 p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
    2322                 :         32 :                 return; /* No pending Group Formation */
    2323                 :            :         }
    2324                 :            : 
    2325         [ -  + ]:         88 :         if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
    2326                 :            :             0) {
    2327                 :          0 :                 p2p_dbg(p2p, "Ignore WPS registration success notification for "
    2328                 :            :                         MACSTR " (GO Negotiation peer " MACSTR ")",
    2329                 :          0 :                         MAC2STR(mac_addr),
    2330                 :          0 :                         MAC2STR(p2p->go_neg_peer->intended_addr));
    2331                 :          0 :                 return; /* Ignore unexpected peer address */
    2332                 :            :         }
    2333                 :            : 
    2334                 :         88 :         p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
    2335                 :        528 :                 MAC2STR(mac_addr));
    2336                 :            : 
    2337                 :        120 :         p2p_clear_go_neg(p2p);
    2338                 :            : }
    2339                 :            : 
    2340                 :            : 
    2341                 :          2 : void p2p_group_formation_failed(struct p2p_data *p2p)
    2342                 :            : {
    2343         [ -  + ]:          2 :         if (p2p->go_neg_peer == NULL) {
    2344                 :          0 :                 p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
    2345                 :          2 :                 return; /* No pending Group Formation */
    2346                 :            :         }
    2347                 :            : 
    2348                 :          2 :         p2p_dbg(p2p, "Group Formation failed with " MACSTR,
    2349                 :         12 :                 MAC2STR(p2p->go_neg_peer->intended_addr));
    2350                 :            : 
    2351                 :          2 :         p2p_clear_go_neg(p2p);
    2352                 :            : }
    2353                 :            : 
    2354                 :            : 
    2355                 :         15 : struct p2p_data * p2p_init(const struct p2p_config *cfg)
    2356                 :            : {
    2357                 :            :         struct p2p_data *p2p;
    2358                 :            : 
    2359         [ -  + ]:         15 :         if (cfg->max_peers < 1)
    2360                 :          0 :                 return NULL;
    2361                 :            : 
    2362                 :         15 :         p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
    2363         [ -  + ]:         15 :         if (p2p == NULL)
    2364                 :          0 :                 return NULL;
    2365                 :         15 :         p2p->cfg = (struct p2p_config *) (p2p + 1);
    2366                 :         15 :         os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
    2367         [ +  + ]:         15 :         if (cfg->dev_name)
    2368                 :          3 :                 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
    2369         [ -  + ]:         15 :         if (cfg->manufacturer)
    2370                 :          0 :                 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
    2371         [ -  + ]:         15 :         if (cfg->model_name)
    2372                 :          0 :                 p2p->cfg->model_name = os_strdup(cfg->model_name);
    2373         [ -  + ]:         15 :         if (cfg->model_number)
    2374                 :          0 :                 p2p->cfg->model_number = os_strdup(cfg->model_number);
    2375         [ -  + ]:         15 :         if (cfg->serial_number)
    2376                 :          0 :                 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
    2377         [ -  + ]:         15 :         if (cfg->pref_chan) {
    2378                 :          0 :                 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
    2379                 :            :                                                 sizeof(struct p2p_channel));
    2380         [ #  # ]:          0 :                 if (p2p->cfg->pref_chan) {
    2381                 :          0 :                         os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
    2382                 :            :                                   cfg->num_pref_chan *
    2383                 :            :                                   sizeof(struct p2p_channel));
    2384                 :            :                 } else
    2385                 :          0 :                         p2p->cfg->num_pref_chan = 0;
    2386                 :            :         }
    2387                 :            : 
    2388                 :         15 :         p2p->min_disc_int = 1;
    2389                 :         15 :         p2p->max_disc_int = 3;
    2390                 :         15 :         p2p->max_disc_tu = -1;
    2391                 :            : 
    2392                 :         15 :         os_get_random(&p2p->next_tie_breaker, 1);
    2393                 :         15 :         p2p->next_tie_breaker &= 0x01;
    2394         [ +  - ]:         15 :         if (cfg->sd_request)
    2395                 :         15 :                 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
    2396                 :         15 :         p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
    2397         [ +  - ]:         15 :         if (cfg->concurrent_operations)
    2398                 :         15 :                 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
    2399                 :         15 :         p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
    2400                 :            : 
    2401                 :         15 :         dl_list_init(&p2p->devices);
    2402                 :            : 
    2403                 :         15 :         eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
    2404                 :            :                                p2p_expiration_timeout, p2p, NULL);
    2405                 :            : 
    2406                 :         15 :         p2p->go_timeout = 100;
    2407                 :         15 :         p2p->client_timeout = 20;
    2408                 :         15 :         p2p->num_p2p_sd_queries = 0;
    2409                 :            : 
    2410                 :         15 :         p2p_dbg(p2p, "initialized");
    2411                 :         15 :         p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
    2412                 :         15 :         p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
    2413                 :            : 
    2414                 :         15 :         return p2p;
    2415                 :            : }
    2416                 :            : 
    2417                 :            : 
    2418                 :         15 : void p2p_deinit(struct p2p_data *p2p)
    2419                 :            : {
    2420                 :            : #ifdef CONFIG_WIFI_DISPLAY
    2421                 :         15 :         wpabuf_free(p2p->wfd_ie_beacon);
    2422                 :         15 :         wpabuf_free(p2p->wfd_ie_probe_req);
    2423                 :         15 :         wpabuf_free(p2p->wfd_ie_probe_resp);
    2424                 :         15 :         wpabuf_free(p2p->wfd_ie_assoc_req);
    2425                 :         15 :         wpabuf_free(p2p->wfd_ie_invitation);
    2426                 :         15 :         wpabuf_free(p2p->wfd_ie_prov_disc_req);
    2427                 :         15 :         wpabuf_free(p2p->wfd_ie_prov_disc_resp);
    2428                 :         15 :         wpabuf_free(p2p->wfd_ie_go_neg);
    2429                 :         15 :         wpabuf_free(p2p->wfd_dev_info);
    2430                 :         15 :         wpabuf_free(p2p->wfd_assoc_bssid);
    2431                 :         15 :         wpabuf_free(p2p->wfd_coupled_sink_info);
    2432                 :            : #endif /* CONFIG_WIFI_DISPLAY */
    2433                 :            : 
    2434                 :         15 :         eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
    2435                 :         15 :         eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
    2436                 :         15 :         eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
    2437                 :         15 :         eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
    2438                 :         15 :         p2p_flush(p2p);
    2439                 :         15 :         p2p_free_req_dev_types(p2p);
    2440                 :         15 :         os_free(p2p->cfg->dev_name);
    2441                 :         15 :         os_free(p2p->cfg->manufacturer);
    2442                 :         15 :         os_free(p2p->cfg->model_name);
    2443                 :         15 :         os_free(p2p->cfg->model_number);
    2444                 :         15 :         os_free(p2p->cfg->serial_number);
    2445                 :         15 :         os_free(p2p->cfg->pref_chan);
    2446                 :         15 :         os_free(p2p->groups);
    2447                 :         15 :         wpabuf_free(p2p->sd_resp);
    2448                 :         15 :         os_free(p2p->after_scan_tx);
    2449                 :         15 :         p2p_remove_wps_vendor_extensions(p2p);
    2450                 :         15 :         os_free(p2p->no_go_freq.range);
    2451                 :         15 :         os_free(p2p);
    2452                 :         15 : }
    2453                 :            : 
    2454                 :            : 
    2455                 :       1217 : void p2p_flush(struct p2p_data *p2p)
    2456                 :            : {
    2457                 :            :         struct p2p_device *dev, *prev;
    2458                 :       1217 :         p2p_stop_find(p2p);
    2459         [ +  + ]:       1482 :         dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
    2460                 :            :                               list) {
    2461                 :        265 :                 dl_list_del(&dev->list);
    2462                 :        265 :                 p2p_device_free(p2p, dev);
    2463                 :            :         }
    2464                 :       1217 :         p2p_free_sd_queries(p2p);
    2465                 :       1217 :         os_free(p2p->after_scan_tx);
    2466                 :       1217 :         p2p->after_scan_tx = NULL;
    2467                 :       1217 : }
    2468                 :            : 
    2469                 :            : 
    2470                 :          0 : int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
    2471                 :            : {
    2472                 :            :         struct p2p_device *dev;
    2473                 :            : 
    2474                 :          0 :         dev = p2p_get_device(p2p, addr);
    2475         [ #  # ]:          0 :         if (dev == NULL)
    2476                 :          0 :                 return -1;
    2477                 :            : 
    2478                 :          0 :         p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
    2479                 :            : 
    2480         [ #  # ]:          0 :         if (p2p->go_neg_peer == dev)
    2481                 :          0 :                 p2p->go_neg_peer = NULL;
    2482                 :            : 
    2483                 :          0 :         dev->wps_method = WPS_NOT_READY;
    2484                 :          0 :         dev->oob_pw_id = 0;
    2485                 :          0 :         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
    2486                 :          0 :         dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
    2487                 :            : 
    2488                 :            :         /* Check if after_scan_tx is for this peer. If so free it */
    2489 [ #  # ][ #  # ]:          0 :         if (p2p->after_scan_tx &&
    2490                 :          0 :             os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
    2491                 :          0 :                 os_free(p2p->after_scan_tx);
    2492                 :          0 :                 p2p->after_scan_tx = NULL;
    2493                 :            :         }
    2494                 :            : 
    2495                 :          0 :         return 0;
    2496                 :            : }
    2497                 :            : 
    2498                 :            : 
    2499                 :          3 : int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
    2500                 :            : {
    2501                 :          3 :         os_free(p2p->cfg->dev_name);
    2502         [ +  - ]:          3 :         if (dev_name) {
    2503                 :          3 :                 p2p->cfg->dev_name = os_strdup(dev_name);
    2504         [ -  + ]:          3 :                 if (p2p->cfg->dev_name == NULL)
    2505                 :          0 :                         return -1;
    2506                 :            :         } else
    2507                 :          0 :                 p2p->cfg->dev_name = NULL;
    2508                 :          3 :         return 0;
    2509                 :            : }
    2510                 :            : 
    2511                 :            : 
    2512                 :          1 : int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
    2513                 :            : {
    2514                 :          1 :         os_free(p2p->cfg->manufacturer);
    2515                 :          1 :         p2p->cfg->manufacturer = NULL;
    2516         [ -  + ]:          1 :         if (manufacturer) {
    2517                 :          0 :                 p2p->cfg->manufacturer = os_strdup(manufacturer);
    2518         [ #  # ]:          0 :                 if (p2p->cfg->manufacturer == NULL)
    2519                 :          0 :                         return -1;
    2520                 :            :         }
    2521                 :            : 
    2522                 :          1 :         return 0;
    2523                 :            : }
    2524                 :            : 
    2525                 :            : 
    2526                 :          1 : int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
    2527                 :            : {
    2528                 :          1 :         os_free(p2p->cfg->model_name);
    2529                 :          1 :         p2p->cfg->model_name = NULL;
    2530         [ -  + ]:          1 :         if (model_name) {
    2531                 :          0 :                 p2p->cfg->model_name = os_strdup(model_name);
    2532         [ #  # ]:          0 :                 if (p2p->cfg->model_name == NULL)
    2533                 :          0 :                         return -1;
    2534                 :            :         }
    2535                 :            : 
    2536                 :          1 :         return 0;
    2537                 :            : }
    2538                 :            : 
    2539                 :            : 
    2540                 :          1 : int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
    2541                 :            : {
    2542                 :          1 :         os_free(p2p->cfg->model_number);
    2543                 :          1 :         p2p->cfg->model_number = NULL;
    2544         [ -  + ]:          1 :         if (model_number) {
    2545                 :          0 :                 p2p->cfg->model_number = os_strdup(model_number);
    2546         [ #  # ]:          0 :                 if (p2p->cfg->model_number == NULL)
    2547                 :          0 :                         return -1;
    2548                 :            :         }
    2549                 :            : 
    2550                 :          1 :         return 0;
    2551                 :            : }
    2552                 :            : 
    2553                 :            : 
    2554                 :          1 : int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
    2555                 :            : {
    2556                 :          1 :         os_free(p2p->cfg->serial_number);
    2557                 :          1 :         p2p->cfg->serial_number = NULL;
    2558         [ -  + ]:          1 :         if (serial_number) {
    2559                 :          0 :                 p2p->cfg->serial_number = os_strdup(serial_number);
    2560         [ #  # ]:          0 :                 if (p2p->cfg->serial_number == NULL)
    2561                 :          0 :                         return -1;
    2562                 :            :         }
    2563                 :            : 
    2564                 :          1 :         return 0;
    2565                 :            : }
    2566                 :            : 
    2567                 :            : 
    2568                 :          0 : void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
    2569                 :            : {
    2570                 :          0 :         p2p->cfg->config_methods = config_methods;
    2571                 :          0 : }
    2572                 :            : 
    2573                 :            : 
    2574                 :          0 : void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
    2575                 :            : {
    2576                 :          0 :         os_memcpy(p2p->cfg->uuid, uuid, 16);
    2577                 :          0 : }
    2578                 :            : 
    2579                 :            : 
    2580                 :          0 : int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
    2581                 :            : {
    2582                 :          0 :         os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
    2583                 :          0 :         return 0;
    2584                 :            : }
    2585                 :            : 
    2586                 :            : 
    2587                 :          2 : int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
    2588                 :            :                           size_t num_dev_types)
    2589                 :            : {
    2590         [ -  + ]:          2 :         if (num_dev_types > P2P_SEC_DEVICE_TYPES)
    2591                 :          0 :                 num_dev_types = P2P_SEC_DEVICE_TYPES;
    2592                 :          2 :         p2p->cfg->num_sec_dev_types = num_dev_types;
    2593                 :          2 :         os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
    2594                 :          2 :         return 0;
    2595                 :            : }
    2596                 :            : 
    2597                 :            : 
    2598                 :         16 : void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
    2599                 :            : {
    2600                 :            :         int i;
    2601                 :            : 
    2602         [ +  + ]:        176 :         for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
    2603                 :        160 :                 wpabuf_free(p2p->wps_vendor_ext[i]);
    2604                 :        160 :                 p2p->wps_vendor_ext[i] = NULL;
    2605                 :            :         }
    2606                 :         16 : }
    2607                 :            : 
    2608                 :            : 
    2609                 :          0 : int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
    2610                 :            :                                  const struct wpabuf *vendor_ext)
    2611                 :            : {
    2612                 :            :         int i;
    2613                 :            : 
    2614         [ #  # ]:          0 :         if (vendor_ext == NULL)
    2615                 :          0 :                 return -1;
    2616                 :            : 
    2617         [ #  # ]:          0 :         for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
    2618         [ #  # ]:          0 :                 if (p2p->wps_vendor_ext[i] == NULL)
    2619                 :          0 :                         break;
    2620                 :            :         }
    2621         [ #  # ]:          0 :         if (i >= P2P_MAX_WPS_VENDOR_EXT)
    2622                 :          0 :                 return -1;
    2623                 :            : 
    2624                 :          0 :         p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
    2625         [ #  # ]:          0 :         if (p2p->wps_vendor_ext[i] == NULL)
    2626                 :          0 :                 return -1;
    2627                 :            : 
    2628                 :          0 :         return 0;
    2629                 :            : }
    2630                 :            : 
    2631                 :            : 
    2632                 :          0 : int p2p_set_country(struct p2p_data *p2p, const char *country)
    2633                 :            : {
    2634                 :          0 :         os_memcpy(p2p->cfg->country, country, 3);
    2635                 :          0 :         return 0;
    2636                 :            : }
    2637                 :            : 
    2638                 :            : 
    2639                 :        597 : void p2p_continue_find(struct p2p_data *p2p)
    2640                 :            : {
    2641                 :            :         struct p2p_device *dev;
    2642                 :        597 :         p2p_set_state(p2p, P2P_SEARCH);
    2643         [ +  + ]:       1168 :         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
    2644         [ +  + ]:        597 :                 if (dev->sd_pending_bcast_queries == 0) {
    2645                 :            :                         /* Initialize with total number of registered broadcast
    2646                 :            :                          * SD queries. */
    2647                 :        548 :                         dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries;
    2648                 :            :                 }
    2649                 :            : 
    2650         [ +  + ]:        597 :                 if (p2p_start_sd(p2p, dev) == 0)
    2651                 :         22 :                         return;
    2652 [ +  + ][ +  - ]:        575 :                 if (dev->req_config_methods &&
    2653                 :          4 :                     !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
    2654                 :          4 :                         p2p_dbg(p2p, "Send pending Provision Discovery Request to "
    2655                 :            :                                 MACSTR " (config methods 0x%x)",
    2656                 :         24 :                                 MAC2STR(dev->info.p2p_device_addr),
    2657                 :          4 :                                 dev->req_config_methods);
    2658         [ +  - ]:          4 :                         if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
    2659                 :          4 :                                 return;
    2660                 :            :                 }
    2661                 :            :         }
    2662                 :            : 
    2663                 :        597 :         p2p_listen_in_find(p2p, 1);
    2664                 :            : }
    2665                 :            : 
    2666                 :            : 
    2667                 :         22 : static void p2p_sd_cb(struct p2p_data *p2p, int success)
    2668                 :            : {
    2669                 :         22 :         p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
    2670                 :            :                 success);
    2671                 :         22 :         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
    2672                 :            : 
    2673         [ -  + ]:         22 :         if (!success) {
    2674                 :          0 :                 p2p->sd_peer = NULL;
    2675                 :          0 :                 p2p_continue_find(p2p);
    2676                 :          0 :                 return;
    2677                 :            :         }
    2678                 :            : 
    2679         [ -  + ]:         22 :         if (p2p->sd_peer == NULL) {
    2680                 :          0 :                 p2p_dbg(p2p, "No SD peer entry known");
    2681                 :          0 :                 p2p_continue_find(p2p);
    2682                 :          0 :                 return;
    2683                 :            :         }
    2684                 :            : 
    2685                 :            :         /* Wait for response from the peer */
    2686                 :         22 :         p2p_set_state(p2p, P2P_SD_DURING_FIND);
    2687                 :         22 :         p2p_set_timeout(p2p, 0, 200000);
    2688                 :            : }
    2689                 :            : 
    2690                 :            : 
    2691                 :            : /**
    2692                 :            :  * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
    2693                 :            :  * @p2p: P2P module context from p2p_init()
    2694                 :            :  */
    2695                 :        124 : static void p2p_retry_pd(struct p2p_data *p2p)
    2696                 :            : {
    2697                 :            :         struct p2p_device *dev;
    2698                 :            : 
    2699         [ +  + ]:        124 :         if (p2p->state != P2P_IDLE)
    2700                 :          4 :                 return;
    2701                 :            : 
    2702                 :            :         /*
    2703                 :            :          * Retry the prov disc req attempt only for the peer that the user had
    2704                 :            :          * requested.
    2705                 :            :          */
    2706                 :            : 
    2707         [ +  - ]:        244 :         dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
    2708         [ -  + ]:        120 :                 if (os_memcmp(p2p->pending_pd_devaddr,
    2709                 :            :                               dev->info.p2p_device_addr, ETH_ALEN) != 0)
    2710                 :          0 :                         continue;
    2711         [ -  + ]:        120 :                 if (!dev->req_config_methods)
    2712                 :          0 :                         continue;
    2713                 :            : 
    2714                 :        120 :                 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
    2715                 :            :                         MACSTR " (config methods 0x%x)",
    2716                 :        720 :                         MAC2STR(dev->info.p2p_device_addr),
    2717                 :        120 :                         dev->req_config_methods);
    2718                 :        120 :                 p2p_send_prov_disc_req(p2p, dev,
    2719                 :        120 :                                        dev->flags & P2P_DEV_PD_FOR_JOIN,
    2720                 :            :                                        p2p->pd_force_freq);
    2721                 :        120 :                 return;
    2722                 :            :         }
    2723                 :            : }
    2724                 :            : 
    2725                 :            : 
    2726                 :        156 : static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
    2727                 :            : {
    2728                 :        156 :         p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
    2729                 :            :                 success);
    2730                 :            : 
    2731                 :            :         /*
    2732                 :            :          * Postpone resetting the pending action state till after we actually
    2733                 :            :          * time out. This allows us to take some action like notifying any
    2734                 :            :          * interested parties about no response to the request.
    2735                 :            :          *
    2736                 :            :          * When the timer (below) goes off we check in IDLE, SEARCH, or
    2737                 :            :          * LISTEN_ONLY state, which are the only allowed states to issue a PD
    2738                 :            :          * requests in, if this was still pending and then raise notification.
    2739                 :            :          */
    2740                 :            : 
    2741         [ +  + ]:        156 :         if (!success) {
    2742                 :        125 :                 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
    2743                 :            : 
    2744 [ +  - ][ +  + ]:        125 :                 if (p2p->user_initiated_pd &&
    2745         [ -  + ]:        121 :                     (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
    2746                 :            :                 {
    2747                 :            :                         /* Retry request from timeout to avoid busy loops */
    2748                 :          4 :                         p2p->pending_action_state = P2P_PENDING_PD;
    2749                 :          4 :                         p2p_set_timeout(p2p, 0, 50000);
    2750         [ -  + ]:        121 :                 } else if (p2p->state != P2P_IDLE)
    2751                 :          0 :                         p2p_continue_find(p2p);
    2752         [ +  - ]:        121 :                 else if (p2p->user_initiated_pd) {
    2753                 :        121 :                         p2p->pending_action_state = P2P_PENDING_PD;
    2754                 :        121 :                         p2p_set_timeout(p2p, 0, 300000);
    2755                 :            :                 }
    2756                 :        156 :                 return;
    2757                 :            :         }
    2758                 :            : 
    2759                 :            :         /*
    2760                 :            :          * This postponing, of resetting pending_action_state, needs to be
    2761                 :            :          * done only for user initiated PD requests and not internal ones.
    2762                 :            :          */
    2763         [ +  + ]:         31 :         if (p2p->user_initiated_pd)
    2764                 :         30 :                 p2p->pending_action_state = P2P_PENDING_PD;
    2765                 :            :         else
    2766                 :          1 :                 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
    2767                 :            : 
    2768                 :            :         /* Wait for response from the peer */
    2769         [ +  + ]:         31 :         if (p2p->state == P2P_SEARCH)
    2770                 :          3 :                 p2p_set_state(p2p, P2P_PD_DURING_FIND);
    2771                 :         31 :         p2p_set_timeout(p2p, 0, 200000);
    2772                 :            : }
    2773                 :            : 
    2774                 :            : 
    2775                 :       1428 : int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
    2776                 :            :                          struct os_reltime *rx_time, int level, const u8 *ies,
    2777                 :            :                          size_t ies_len)
    2778                 :            : {
    2779         [ +  + ]:       1428 :         if (os_reltime_before(rx_time, &p2p->find_start)) {
    2780                 :            :                 /*
    2781                 :            :                  * The driver may have cached (e.g., in cfg80211 BSS table) the
    2782                 :            :                  * scan results for relatively long time. To avoid reporting
    2783                 :            :                  * stale information, update P2P peers only based on results
    2784                 :            :                  * that have based on frames received after the last p2p_find
    2785                 :            :                  * operation was started.
    2786                 :            :                  */
    2787                 :        799 :                 p2p_dbg(p2p, "Ignore old scan result for " MACSTR
    2788                 :            :                         " (rx_time=%u.%06u)",
    2789                 :       5593 :                         MAC2STR(bssid), (unsigned int) rx_time->sec,
    2790                 :        799 :                         (unsigned int) rx_time->usec);
    2791                 :        799 :                 return 0;
    2792                 :            :         }
    2793                 :            : 
    2794                 :        629 :         p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
    2795                 :            : 
    2796                 :       1428 :         return 0;
    2797                 :            : }
    2798                 :            : 
    2799                 :            : 
    2800                 :        658 : void p2p_scan_res_handled(struct p2p_data *p2p)
    2801                 :            : {
    2802         [ +  + ]:        658 :         if (!p2p->p2p_scan_running) {
    2803                 :         28 :                 p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
    2804                 :            :         }
    2805                 :        658 :         p2p->p2p_scan_running = 0;
    2806                 :        658 :         eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
    2807                 :            : 
    2808         [ +  + ]:        658 :         if (p2p_run_after_scan(p2p))
    2809                 :        658 :                 return;
    2810         [ +  + ]:        610 :         if (p2p->state == P2P_SEARCH)
    2811                 :        570 :                 p2p_continue_find(p2p);
    2812                 :            : }
    2813                 :            : 
    2814                 :            : 
    2815                 :       1354 : void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
    2816                 :            : {
    2817                 :            :         u8 *len;
    2818                 :            : 
    2819                 :            : #ifdef CONFIG_WIFI_DISPLAY
    2820         [ +  + ]:       1354 :         if (p2p->wfd_ie_probe_req)
    2821                 :       1165 :                 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
    2822                 :            : #endif /* CONFIG_WIFI_DISPLAY */
    2823                 :            : 
    2824                 :       1354 :         len = p2p_buf_add_ie_hdr(ies);
    2825                 :       1354 :         p2p_buf_add_capability(ies, p2p->dev_capab &
    2826                 :            :                                ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
    2827         [ +  + ]:       1354 :         if (dev_id)
    2828                 :          8 :                 p2p_buf_add_device_id(ies, dev_id);
    2829 [ +  - ][ +  - ]:       1354 :         if (p2p->cfg->reg_class && p2p->cfg->channel)
    2830                 :       1354 :                 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
    2831                 :       1354 :                                            p2p->cfg->reg_class,
    2832                 :       1354 :                                            p2p->cfg->channel);
    2833         [ -  + ]:       1354 :         if (p2p->ext_listen_interval)
    2834                 :          0 :                 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
    2835                 :          0 :                                               p2p->ext_listen_interval);
    2836                 :            :         /* TODO: p2p_buf_add_operating_channel() if GO */
    2837                 :       1354 :         p2p_buf_update_ie_hdr(ies, len);
    2838                 :       1354 : }
    2839                 :            : 
    2840                 :            : 
    2841                 :       1355 : size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
    2842                 :            : {
    2843                 :       1355 :         size_t len = 100;
    2844                 :            : 
    2845                 :            : #ifdef CONFIG_WIFI_DISPLAY
    2846 [ +  - ][ +  + ]:       1355 :         if (p2p && p2p->wfd_ie_probe_req)
    2847                 :       1166 :                 len += wpabuf_len(p2p->wfd_ie_probe_req);
    2848                 :            : #endif /* CONFIG_WIFI_DISPLAY */
    2849                 :            : 
    2850                 :       1355 :         return len;
    2851                 :            : }
    2852                 :            : 
    2853                 :            : 
    2854                 :          0 : int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
    2855                 :            : {
    2856                 :          0 :         return p2p_attr_text(p2p_ie, buf, end);
    2857                 :            : }
    2858                 :            : 
    2859                 :            : 
    2860                 :        105 : static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
    2861                 :            : {
    2862                 :        105 :         struct p2p_device *dev = p2p->go_neg_peer;
    2863                 :            :         int timeout;
    2864                 :            : 
    2865                 :        105 :         p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
    2866                 :            : 
    2867         [ -  + ]:        105 :         if (dev == NULL) {
    2868                 :          0 :                 p2p_dbg(p2p, "No pending GO Negotiation");
    2869                 :          0 :                 return;
    2870                 :            :         }
    2871                 :            : 
    2872         [ +  + ]:        105 :         if (success) {
    2873         [ -  + ]:         62 :                 if (dev->flags & P2P_DEV_USER_REJECTED) {
    2874                 :          0 :                         p2p_set_state(p2p, P2P_IDLE);
    2875                 :          0 :                         return;
    2876                 :            :                 }
    2877         [ +  - ]:         43 :         } else if (dev->go_neg_req_sent) {
    2878                 :            :                 /* Cancel the increment from p2p_connect_send() on failure */
    2879                 :         43 :                 dev->go_neg_req_sent--;
    2880                 :            :         }
    2881                 :            : 
    2882 [ +  + ][ +  + ]:        105 :         if (!success &&
    2883         [ +  - ]:          3 :             (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
    2884                 :          3 :             !is_zero_ether_addr(dev->member_in_go_dev)) {
    2885                 :          3 :                 p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
    2886                 :         18 :                         MAC2STR(dev->info.p2p_device_addr));
    2887                 :          3 :                 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    2888                 :          3 :                 p2p_send_dev_disc_req(p2p, dev);
    2889                 :          3 :                 return;
    2890                 :            :         }
    2891                 :            : 
    2892                 :            :         /*
    2893                 :            :          * Use P2P find, if needed, to find the other device from its listen
    2894                 :            :          * channel.
    2895                 :            :          */
    2896                 :        102 :         p2p_set_state(p2p, P2P_CONNECT);
    2897         [ +  + ]:        102 :         timeout = success ? 500000 : 100000;
    2898 [ +  + ][ +  - ]:        102 :         if (!success && p2p->go_neg_peer &&
                 [ +  + ]
    2899                 :         40 :             (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
    2900                 :            :                 unsigned int r;
    2901                 :            :                 /*
    2902                 :            :                  * Peer is expected to wait our response and we will skip the
    2903                 :            :                  * listen phase. Add some randomness to the wait time here to
    2904                 :            :                  * make it less likely to hit cases where we could end up in
    2905                 :            :                  * sync with peer not listening.
    2906                 :            :                  */
    2907                 :          1 :                 os_get_random((u8 *) &r, sizeof(r));
    2908                 :          1 :                 timeout += r % 100000;
    2909                 :            :         }
    2910                 :        105 :         p2p_set_timeout(p2p, 0, timeout);
    2911                 :            : }
    2912                 :            : 
    2913                 :            : 
    2914                 :         45 : static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
    2915                 :            : {
    2916                 :         45 :         p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
    2917                 :            :                 success);
    2918 [ -  + ][ #  # ]:         45 :         if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
    2919                 :          0 :                 p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
    2920                 :         45 :                 return;
    2921                 :            :         }
    2922                 :         45 :         p2p_set_state(p2p, P2P_CONNECT);
    2923                 :         45 :         p2p_set_timeout(p2p, 0, 500000);
    2924                 :            : }
    2925                 :            : 
    2926                 :            : 
    2927                 :         23 : static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
    2928                 :            :                                        const u8 *addr)
    2929                 :            : {
    2930                 :         23 :         p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
    2931 [ -  + ][ #  # ]:         23 :         if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
    2932                 :          0 :                 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
    2933                 :          0 :                                   p2p->go_neg_peer->status);
    2934         [ +  - ]:         23 :         } else if (success) {
    2935                 :            :                 struct p2p_device *dev;
    2936                 :         23 :                 dev = p2p_get_device(p2p, addr);
    2937 [ +  + ][ +  + ]:         23 :                 if (dev &&
    2938                 :         16 :                     dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
    2939                 :          6 :                         dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
    2940                 :            :         }
    2941                 :         23 : }
    2942                 :            : 
    2943                 :            : 
    2944                 :         45 : static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
    2945                 :            :                                enum p2p_send_action_result result)
    2946                 :            : {
    2947                 :            :         struct p2p_device *dev;
    2948                 :            : 
    2949                 :         45 :         p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
    2950                 :         45 :         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    2951         [ -  + ]:         45 :         if (result == P2P_SEND_ACTION_FAILED) {
    2952                 :          0 :                 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
    2953                 :          0 :                 return;
    2954                 :            :         }
    2955         [ -  + ]:         45 :         if (result == P2P_SEND_ACTION_NO_ACK) {
    2956                 :            :                 /*
    2957                 :            :                  * It looks like the TX status for GO Negotiation Confirm is
    2958                 :            :                  * often showing failure even when the peer has actually
    2959                 :            :                  * received the frame. Since the peer may change channels
    2960                 :            :                  * immediately after having received the frame, we may not see
    2961                 :            :                  * an Ack for retries, so just dropping a single frame may
    2962                 :            :                  * trigger this. To allow the group formation to succeed if the
    2963                 :            :                  * peer did indeed receive the frame, continue regardless of
    2964                 :            :                  * the TX status.
    2965                 :            :                  */
    2966                 :          0 :                 p2p_dbg(p2p, "Assume GO Negotiation Confirm TX was actually received by the peer even though Ack was not reported");
    2967                 :            :         }
    2968                 :            : 
    2969                 :         45 :         dev = p2p->go_neg_peer;
    2970         [ -  + ]:         45 :         if (dev == NULL)
    2971                 :          0 :                 return;
    2972                 :            : 
    2973                 :         45 :         p2p_go_complete(p2p, dev);
    2974                 :            : }
    2975                 :            : 
    2976                 :            : 
    2977                 :        525 : void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
    2978                 :            :                         const u8 *src, const u8 *bssid,
    2979                 :            :                         enum p2p_send_action_result result)
    2980                 :            : {
    2981                 :            :         enum p2p_pending_action_state state;
    2982                 :            :         int success;
    2983                 :            : 
    2984                 :        525 :         p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
    2985                 :            :                 " src=" MACSTR " bssid=" MACSTR " result=%d",
    2986                 :       6825 :                 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
    2987                 :       3150 :                 MAC2STR(bssid), result);
    2988                 :        525 :         success = result == P2P_SEND_ACTION_SUCCESS;
    2989                 :        525 :         state = p2p->pending_action_state;
    2990                 :        525 :         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
    2991   [ +  +  +  +  :        525 :         switch (state) {
          +  +  +  +  +  
             +  +  -  - ]
    2992                 :            :         case P2P_NO_PENDING_ACTION:
    2993         [ +  + ]:         84 :                 if (p2p->send_action_in_progress) {
    2994                 :         34 :                         p2p->send_action_in_progress = 0;
    2995                 :         34 :                         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    2996                 :            :                 }
    2997         [ +  + ]:         84 :                 if (p2p->after_scan_tx_in_progress) {
    2998                 :          1 :                         p2p->after_scan_tx_in_progress = 0;
    2999   [ -  +  #  # ]:          1 :                         if (p2p->start_after_scan != P2P_AFTER_SCAN_NOTHING &&
    3000                 :          0 :                             p2p_run_after_scan(p2p))
    3001                 :          0 :                                 break;
    3002         [ +  - ]:          1 :                         if (p2p->state == P2P_SEARCH) {
    3003                 :          1 :                                 p2p_dbg(p2p, "Continue find after after_scan_tx completion");
    3004                 :          1 :                                 p2p_continue_find(p2p);
    3005                 :            :                         }
    3006                 :            :                 }
    3007                 :         84 :                 break;
    3008                 :            :         case P2P_PENDING_GO_NEG_REQUEST:
    3009                 :        105 :                 p2p_go_neg_req_cb(p2p, success);
    3010                 :        105 :                 break;
    3011                 :            :         case P2P_PENDING_GO_NEG_RESPONSE:
    3012                 :         45 :                 p2p_go_neg_resp_cb(p2p, success);
    3013                 :         45 :                 break;
    3014                 :            :         case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
    3015                 :         23 :                 p2p_go_neg_resp_failure_cb(p2p, success, dst);
    3016                 :         23 :                 break;
    3017                 :            :         case P2P_PENDING_GO_NEG_CONFIRM:
    3018                 :         45 :                 p2p_go_neg_conf_cb(p2p, result);
    3019                 :         45 :                 break;
    3020                 :            :         case P2P_PENDING_SD:
    3021                 :         22 :                 p2p_sd_cb(p2p, success);
    3022                 :         22 :                 break;
    3023                 :            :         case P2P_PENDING_PD:
    3024                 :        156 :                 p2p_prov_disc_cb(p2p, success);
    3025                 :        156 :                 break;
    3026                 :            :         case P2P_PENDING_INVITATION_REQUEST:
    3027                 :         14 :                 p2p_invitation_req_cb(p2p, success);
    3028                 :         14 :                 break;
    3029                 :            :         case P2P_PENDING_INVITATION_RESPONSE:
    3030                 :         25 :                 p2p_invitation_resp_cb(p2p, success);
    3031                 :         25 :                 break;
    3032                 :            :         case P2P_PENDING_DEV_DISC_REQUEST:
    3033                 :          3 :                 p2p_dev_disc_req_cb(p2p, success);
    3034                 :          3 :                 break;
    3035                 :            :         case P2P_PENDING_DEV_DISC_RESPONSE:
    3036                 :          3 :                 p2p_dev_disc_resp_cb(p2p, success);
    3037                 :          3 :                 break;
    3038                 :            :         case P2P_PENDING_GO_DISC_REQ:
    3039                 :          0 :                 p2p_go_disc_req_cb(p2p, success);
    3040                 :          0 :                 break;
    3041                 :            :         }
    3042                 :            : 
    3043                 :        525 :         p2p->after_scan_tx_in_progress = 0;
    3044                 :        525 : }
    3045                 :            : 
    3046                 :            : 
    3047                 :        773 : void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
    3048                 :            :                    unsigned int duration)
    3049                 :            : {
    3050         [ -  + ]:        773 :         if (freq == p2p->pending_client_disc_freq) {
    3051                 :          0 :                 p2p_dbg(p2p, "Client discoverability remain-awake completed");
    3052                 :          0 :                 p2p->pending_client_disc_freq = 0;
    3053                 :          0 :                 return;
    3054                 :            :         }
    3055                 :            : 
    3056         [ +  + ]:        773 :         if (freq != p2p->pending_listen_freq) {
    3057                 :          1 :                 p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
    3058                 :            :                         freq, duration, p2p->pending_listen_freq);
    3059                 :          1 :                 return;
    3060                 :            :         }
    3061                 :            : 
    3062                 :        772 :         p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
    3063                 :            :                 p2p->pending_listen_sec, p2p->pending_listen_usec,
    3064                 :            :                 p2p->pending_listen_freq);
    3065                 :        772 :         p2p->in_listen = 1;
    3066                 :        772 :         p2p->drv_in_listen = freq;
    3067 [ +  + ][ +  - ]:        772 :         if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
    3068                 :            :                 /*
    3069                 :            :                  * Add 20 msec extra wait to avoid race condition with driver
    3070                 :            :                  * remain-on-channel end event, i.e., give driver more time to
    3071                 :            :                  * complete the operation before our timeout expires.
    3072                 :            :                  */
    3073                 :        772 :                 p2p_set_timeout(p2p, p2p->pending_listen_sec,
    3074                 :        772 :                                 p2p->pending_listen_usec + 20000);
    3075                 :            :         }
    3076                 :            : 
    3077                 :        773 :         p2p->pending_listen_freq = 0;
    3078                 :            : }
    3079                 :            : 
    3080                 :            : 
    3081                 :        769 : int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
    3082                 :            : {
    3083                 :        769 :         p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
    3084                 :        769 :         p2p->drv_in_listen = 0;
    3085         [ +  + ]:        769 :         if (p2p->in_listen)
    3086                 :        671 :                 return 0; /* Internal timeout will trigger the next step */
    3087                 :            : 
    3088 [ -  + ][ #  # ]:         98 :         if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
    3089         [ #  # ]:          0 :                 if (p2p->go_neg_peer->connect_reqs >= 120) {
    3090                 :          0 :                         p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
    3091                 :          0 :                         p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
    3092                 :          0 :                         return 0;
    3093                 :            :                 }
    3094                 :            : 
    3095                 :          0 :                 p2p_set_state(p2p, P2P_CONNECT);
    3096                 :          0 :                 p2p_connect_send(p2p, p2p->go_neg_peer);
    3097                 :          0 :                 return 1;
    3098         [ +  + ]:         98 :         } else if (p2p->state == P2P_SEARCH) {
    3099         [ +  + ]:          7 :                 if (p2p->p2p_scan_running) {
    3100                 :            :                          /*
    3101                 :            :                           * Search is already in progress. This can happen if
    3102                 :            :                           * an Action frame RX is reported immediately after
    3103                 :            :                           * the end of a remain-on-channel operation and the
    3104                 :            :                           * response frame to that is sent using an offchannel
    3105                 :            :                           * operation while in p2p_find. Avoid an attempt to
    3106                 :            :                           * restart a scan here.
    3107                 :            :                           */
    3108                 :          1 :                         p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
    3109                 :          1 :                         return 1;
    3110                 :            :                 }
    3111         [ -  + ]:          6 :                 if (p2p->pending_listen_freq) {
    3112                 :            :                         /*
    3113                 :            :                          * Better wait a bit if the driver is unable to start
    3114                 :            :                          * offchannel operation for some reason. p2p_search()
    3115                 :            :                          * will be started from internal timeout.
    3116                 :            :                          */
    3117                 :          0 :                         p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
    3118                 :          0 :                         p2p_set_timeout(p2p, 0, 100000);
    3119                 :          0 :                         return 1;
    3120                 :            :                 }
    3121         [ -  + ]:          6 :                 if (p2p->search_delay) {
    3122                 :          0 :                         p2p_dbg(p2p, "Delay search operation by %u ms",
    3123                 :            :                                 p2p->search_delay);
    3124                 :          0 :                         p2p_set_timeout(p2p, p2p->search_delay / 1000,
    3125                 :          0 :                                         (p2p->search_delay % 1000) * 1000);
    3126                 :          0 :                         return 1;
    3127                 :            :                 }
    3128                 :          6 :                 p2p_search(p2p);
    3129                 :          6 :                 return 1;
    3130                 :            :         }
    3131                 :            : 
    3132                 :        769 :         return 0;
    3133                 :            : }
    3134                 :            : 
    3135                 :            : 
    3136                 :         42 : static void p2p_timeout_connect(struct p2p_data *p2p)
    3137                 :            : {
    3138                 :         42 :         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    3139 [ +  - ][ -  + ]:         42 :         if (p2p->go_neg_peer &&
    3140                 :         42 :             (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
    3141                 :          0 :                 p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
    3142                 :          0 :                 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
    3143                 :          0 :                 return;
    3144                 :            :         }
    3145 [ +  - ][ +  + ]:         42 :         if (p2p->go_neg_peer &&
    3146         [ +  - ]:          1 :             (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
    3147                 :          1 :             p2p->go_neg_peer->connect_reqs < 120) {
    3148                 :          1 :                 p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
    3149                 :          1 :                 p2p_connect_send(p2p, p2p->go_neg_peer);
    3150                 :          1 :                 return;
    3151                 :            :         }
    3152 [ +  - ][ -  + ]:         41 :         if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
    3153                 :          0 :                 p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
    3154                 :          0 :                 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
    3155                 :          0 :                 p2p_set_timeout(p2p, 0, 30000);
    3156                 :          0 :                 return;
    3157                 :            :         }
    3158                 :         41 :         p2p_set_state(p2p, P2P_CONNECT_LISTEN);
    3159                 :         42 :         p2p_listen_in_find(p2p, 0);
    3160                 :            : }
    3161                 :            : 
    3162                 :            : 
    3163                 :         30 : static void p2p_timeout_connect_listen(struct p2p_data *p2p)
    3164                 :            : {
    3165         [ +  - ]:         30 :         if (p2p->go_neg_peer) {
    3166         [ -  + ]:         30 :                 if (p2p->drv_in_listen) {
    3167                 :          0 :                         p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
    3168                 :          0 :                         return;
    3169                 :            :                 }
    3170                 :            : 
    3171         [ -  + ]:         30 :                 if (p2p->go_neg_peer->connect_reqs >= 120) {
    3172                 :          0 :                         p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
    3173                 :          0 :                         p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
    3174                 :          0 :                         return;
    3175                 :            :                 }
    3176                 :            : 
    3177                 :         30 :                 p2p_set_state(p2p, P2P_CONNECT);
    3178                 :         30 :                 p2p_connect_send(p2p, p2p->go_neg_peer);
    3179                 :            :         } else
    3180                 :         30 :                 p2p_set_state(p2p, P2P_IDLE);
    3181                 :            : }
    3182                 :            : 
    3183                 :            : 
    3184                 :          5 : static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
    3185                 :            : {
    3186                 :          5 :         p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
    3187                 :            : 
    3188   [ +  -  -  + ]:         10 :         if (p2p->cfg->is_concurrent_session_active &&
    3189                 :          5 :             p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
    3190                 :          0 :                 p2p_set_timeout(p2p, 0, 500000);
    3191                 :            :         else
    3192                 :          5 :                 p2p_set_timeout(p2p, 0, 200000);
    3193                 :          5 : }
    3194                 :            : 
    3195                 :            : 
    3196                 :         10 : static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
    3197                 :            : {
    3198                 :         10 :         struct p2p_device *dev = p2p->go_neg_peer;
    3199                 :            : 
    3200         [ -  + ]:         10 :         if (dev == NULL) {
    3201                 :          0 :                 p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
    3202                 :          0 :                 return;
    3203                 :            :         }
    3204                 :            : 
    3205                 :         10 :         dev->wait_count++;
    3206         [ -  + ]:         10 :         if (dev->wait_count >= 120) {
    3207                 :          0 :                 p2p_dbg(p2p, "Timeout on waiting peer to become ready for GO Negotiation");
    3208                 :          0 :                 p2p_go_neg_failed(p2p, dev, -1);
    3209                 :          0 :                 return;
    3210                 :            :         }
    3211                 :            : 
    3212                 :         10 :         p2p_dbg(p2p, "Go to Listen state while waiting for the peer to become ready for GO Negotiation");
    3213                 :         10 :         p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
    3214                 :         10 :         p2p_listen_in_find(p2p, 0);
    3215                 :            : }
    3216                 :            : 
    3217                 :            : 
    3218                 :          0 : static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
    3219                 :            : {
    3220                 :          0 :         p2p_dbg(p2p, "Service Discovery Query timeout");
    3221         [ #  # ]:          0 :         if (p2p->sd_peer) {
    3222                 :          0 :                 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    3223                 :          0 :                 p2p->sd_peer = NULL;
    3224                 :            :         }
    3225                 :          0 :         p2p_continue_find(p2p);
    3226                 :          0 : }
    3227                 :            : 
    3228                 :            : 
    3229                 :          0 : static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
    3230                 :            : {
    3231                 :          0 :         p2p_dbg(p2p, "Provision Discovery Request timeout");
    3232                 :          0 :         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    3233                 :          0 :         p2p_continue_find(p2p);
    3234                 :          0 : }
    3235                 :            : 
    3236                 :            : 
    3237                 :        125 : static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
    3238                 :            : {
    3239                 :        125 :         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
    3240                 :            : 
    3241                 :            :         /*
    3242                 :            :          * For user initiated PD requests that we have not gotten any responses
    3243                 :            :          * for while in IDLE state, we retry them a couple of times before
    3244                 :            :          * giving up.
    3245                 :            :          */
    3246         [ -  + ]:        125 :         if (!p2p->user_initiated_pd)
    3247                 :        125 :                 return;
    3248                 :            : 
    3249                 :        125 :         p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
    3250                 :            : 
    3251         [ +  + ]:        125 :         if (p2p->pd_retries) {
    3252                 :        124 :                 p2p->pd_retries--;
    3253                 :        124 :                 p2p_retry_pd(p2p);
    3254                 :            :         } else {
    3255                 :            :                 struct p2p_device *dev;
    3256                 :          1 :                 int for_join = 0;
    3257                 :            : 
    3258         [ +  + ]:          2 :                 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
    3259         [ -  + ]:          1 :                         if (os_memcmp(p2p->pending_pd_devaddr,
    3260                 :            :                                       dev->info.p2p_device_addr, ETH_ALEN) != 0)
    3261                 :          0 :                                 continue;
    3262 [ +  - ][ -  + ]:          1 :                         if (dev->req_config_methods &&
    3263                 :          1 :                             (dev->flags & P2P_DEV_PD_FOR_JOIN))
    3264                 :          0 :                                 for_join = 1;
    3265                 :            :                 }
    3266                 :            : 
    3267         [ +  - ]:          1 :                 if (p2p->cfg->prov_disc_fail)
    3268         [ -  + ]:          1 :                         p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
    3269                 :          1 :                                                  p2p->pending_pd_devaddr,
    3270                 :            :                                                  for_join ?
    3271                 :            :                                                  P2P_PROV_DISC_TIMEOUT_JOIN :
    3272                 :            :                                                  P2P_PROV_DISC_TIMEOUT);
    3273                 :          1 :                 p2p_reset_pending_pd(p2p);
    3274                 :            :         }
    3275                 :            : }
    3276                 :            : 
    3277                 :            : 
    3278                 :          1 : static void p2p_timeout_invite(struct p2p_data *p2p)
    3279                 :            : {
    3280                 :          1 :         p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
    3281                 :          1 :         p2p_set_state(p2p, P2P_INVITE_LISTEN);
    3282         [ +  - ]:          1 :         if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
    3283                 :            :                 /*
    3284                 :            :                  * Better remain on operating channel instead of listen channel
    3285                 :            :                  * when running a group.
    3286                 :            :                  */
    3287                 :          1 :                 p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
    3288                 :          1 :                 p2p_set_timeout(p2p, 0, 100000);
    3289                 :          1 :                 return;
    3290                 :            :         }
    3291                 :          0 :         p2p_listen_in_find(p2p, 0);
    3292                 :            : }
    3293                 :            : 
    3294                 :            : 
    3295                 :          1 : static void p2p_timeout_invite_listen(struct p2p_data *p2p)
    3296                 :            : {
    3297 [ +  - ][ +  - ]:          1 :         if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
    3298                 :          1 :                 p2p_set_state(p2p, P2P_INVITE);
    3299                 :          1 :                 p2p_invite_send(p2p, p2p->invite_peer,
    3300                 :            :                                 p2p->invite_go_dev_addr, p2p->invite_dev_pw_id);
    3301                 :            :         } else {
    3302         [ #  # ]:          0 :                 if (p2p->invite_peer) {
    3303                 :          0 :                         p2p_dbg(p2p, "Invitation Request retry limit reached");
    3304         [ #  # ]:          0 :                         if (p2p->cfg->invitation_result)
    3305                 :          0 :                                 p2p->cfg->invitation_result(
    3306                 :          0 :                                         p2p->cfg->cb_ctx, -1, NULL, NULL,
    3307                 :          0 :                                         p2p->invite_peer->info.p2p_device_addr,
    3308                 :            :                                         0);
    3309                 :            :                 }
    3310                 :          0 :                 p2p_set_state(p2p, P2P_IDLE);
    3311                 :            :         }
    3312                 :          1 : }
    3313                 :            : 
    3314                 :            : 
    3315                 :        733 : static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
    3316                 :            : {
    3317                 :        733 :         struct p2p_data *p2p = eloop_ctx;
    3318                 :            : 
    3319                 :        733 :         p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
    3320                 :            : 
    3321                 :        733 :         p2p->in_listen = 0;
    3322                 :            : 
    3323   [ +  +  +  +  :        733 :         switch (p2p->state) {
          -  +  +  +  -  
             -  -  +  +  
                      - ]
    3324                 :            :         case P2P_IDLE:
    3325                 :            :                 /* Check if we timed out waiting for PD req */
    3326         [ +  + ]:        152 :                 if (p2p->pending_action_state == P2P_PENDING_PD)
    3327                 :        121 :                         p2p_timeout_prov_disc_req(p2p);
    3328                 :        152 :                 break;
    3329                 :            :         case P2P_SEARCH:
    3330                 :            :                 /* Check if we timed out waiting for PD req */
    3331         [ +  + ]:        491 :                 if (p2p->pending_action_state == P2P_PENDING_PD)
    3332                 :          4 :                         p2p_timeout_prov_disc_req(p2p);
    3333 [ +  + ][ +  + ]:        491 :                 if (p2p->search_delay && !p2p->in_search_delay) {
    3334                 :          5 :                         p2p_dbg(p2p, "Delay search operation by %u ms",
    3335                 :            :                                 p2p->search_delay);
    3336                 :          5 :                         p2p->in_search_delay = 1;
    3337                 :          5 :                         p2p_set_timeout(p2p, p2p->search_delay / 1000,
    3338                 :          5 :                                         (p2p->search_delay % 1000) * 1000);
    3339                 :          5 :                         break;
    3340                 :            :                 }
    3341                 :        486 :                 p2p->in_search_delay = 0;
    3342                 :        486 :                 p2p_search(p2p);
    3343                 :        486 :                 break;
    3344                 :            :         case P2P_CONNECT:
    3345                 :         42 :                 p2p_timeout_connect(p2p);
    3346                 :         42 :                 break;
    3347                 :            :         case P2P_CONNECT_LISTEN:
    3348                 :         30 :                 p2p_timeout_connect_listen(p2p);
    3349                 :         30 :                 break;
    3350                 :            :         case P2P_GO_NEG:
    3351                 :          0 :                 break;
    3352                 :            :         case P2P_LISTEN_ONLY:
    3353                 :            :                 /* Check if we timed out waiting for PD req */
    3354         [ -  + ]:          1 :                 if (p2p->pending_action_state == P2P_PENDING_PD)
    3355                 :          0 :                         p2p_timeout_prov_disc_req(p2p);
    3356                 :            : 
    3357         [ -  + ]:          1 :                 if (p2p->ext_listen_only) {
    3358                 :          0 :                         p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
    3359                 :          0 :                         p2p->ext_listen_only = 0;
    3360                 :          0 :                         p2p_set_state(p2p, P2P_IDLE);
    3361                 :            :                 }
    3362                 :          1 :                 break;
    3363                 :            :         case P2P_WAIT_PEER_CONNECT:
    3364                 :          5 :                 p2p_timeout_wait_peer_connect(p2p);
    3365                 :          5 :                 break;
    3366                 :            :         case P2P_WAIT_PEER_IDLE:
    3367                 :         10 :                 p2p_timeout_wait_peer_idle(p2p);
    3368                 :         10 :                 break;
    3369                 :            :         case P2P_SD_DURING_FIND:
    3370                 :          0 :                 p2p_timeout_sd_during_find(p2p);
    3371                 :          0 :                 break;
    3372                 :            :         case P2P_PROVISIONING:
    3373                 :          0 :                 break;
    3374                 :            :         case P2P_PD_DURING_FIND:
    3375                 :          0 :                 p2p_timeout_prov_disc_during_find(p2p);
    3376                 :          0 :                 break;
    3377                 :            :         case P2P_INVITE:
    3378                 :          1 :                 p2p_timeout_invite(p2p);
    3379                 :          1 :                 break;
    3380                 :            :         case P2P_INVITE_LISTEN:
    3381                 :          1 :                 p2p_timeout_invite_listen(p2p);
    3382                 :          1 :                 break;
    3383                 :            :         }
    3384                 :        733 : }
    3385                 :            : 
    3386                 :            : 
    3387                 :          1 : int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
    3388                 :            : {
    3389                 :            :         struct p2p_device *dev;
    3390                 :            : 
    3391                 :          1 :         dev = p2p_get_device(p2p, peer_addr);
    3392                 :          1 :         p2p_dbg(p2p, "Local request to reject connection attempts by peer "
    3393                 :          6 :                 MACSTR, MAC2STR(peer_addr));
    3394         [ -  + ]:          1 :         if (dev == NULL) {
    3395                 :          0 :                 p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
    3396                 :          0 :                 return -1;
    3397                 :            :         }
    3398                 :          1 :         dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
    3399                 :          1 :         dev->flags |= P2P_DEV_USER_REJECTED;
    3400                 :          1 :         return 0;
    3401                 :            : }
    3402                 :            : 
    3403                 :            : 
    3404                 :        328 : const char * p2p_wps_method_text(enum p2p_wps_method method)
    3405                 :            : {
    3406   [ +  +  +  +  :        328 :         switch (method) {
                   +  - ]
    3407                 :            :         case WPS_NOT_READY:
    3408                 :        237 :                 return "not-ready";
    3409                 :            :         case WPS_PIN_DISPLAY:
    3410                 :         36 :                 return "Display";
    3411                 :            :         case WPS_PIN_KEYPAD:
    3412                 :         36 :                 return "Keypad";
    3413                 :            :         case WPS_PBC:
    3414                 :          7 :                 return "PBC";
    3415                 :            :         case WPS_NFC:
    3416                 :         12 :                 return "NFC";
    3417                 :            :         }
    3418                 :            : 
    3419                 :        328 :         return "??";
    3420                 :            : }
    3421                 :            : 
    3422                 :            : 
    3423                 :        238 : static const char * p2p_go_state_text(enum p2p_go_state go_state)
    3424                 :            : {
    3425   [ +  +  +  - ]:        238 :         switch (go_state) {
    3426                 :            :         case UNKNOWN_GO:
    3427                 :        219 :                 return "unknown";
    3428                 :            :         case LOCAL_GO:
    3429                 :          9 :                 return "local";
    3430                 :            :         case  REMOTE_GO:
    3431                 :         10 :                 return "remote";
    3432                 :            :         }
    3433                 :            : 
    3434                 :        238 :         return "??";
    3435                 :            : }
    3436                 :            : 
    3437                 :            : 
    3438                 :        346 : const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
    3439                 :            :                                                const u8 *addr, int next)
    3440                 :            : {
    3441                 :            :         struct p2p_device *dev;
    3442                 :            : 
    3443         [ +  - ]:        346 :         if (addr)
    3444                 :        346 :                 dev = p2p_get_device(p2p, addr);
    3445                 :            :         else
    3446         [ #  # ]:          0 :                 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
    3447                 :            : 
    3448 [ +  + ][ -  + ]:        346 :         if (dev && next) {
    3449         [ #  # ]:          0 :                 dev = dl_list_first(&dev->list, struct p2p_device, list);
    3450         [ #  # ]:          0 :                 if (&dev->list == &p2p->devices)
    3451                 :          0 :                         dev = NULL;
    3452                 :            :         }
    3453                 :            : 
    3454         [ +  + ]:        346 :         if (dev == NULL)
    3455                 :        108 :                 return NULL;
    3456                 :            : 
    3457                 :        346 :         return &dev->info;
    3458                 :            : }
    3459                 :            : 
    3460                 :            : 
    3461                 :        238 : int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
    3462                 :            :                           char *buf, size_t buflen)
    3463                 :            : {
    3464                 :            :         struct p2p_device *dev;
    3465                 :            :         int res;
    3466                 :            :         char *pos, *end;
    3467                 :            :         struct os_reltime now;
    3468                 :            : 
    3469         [ -  + ]:        238 :         if (info == NULL)
    3470                 :          0 :                 return -1;
    3471                 :            : 
    3472                 :        238 :         dev = (struct p2p_device *) (((u8 *) info) -
    3473                 :            :                                      offsetof(struct p2p_device, info));
    3474                 :            : 
    3475                 :        238 :         pos = buf;
    3476                 :        238 :         end = buf + buflen;
    3477                 :            : 
    3478                 :        238 :         os_get_reltime(&now);
    3479 [ +  + ][ +  + ]:        238 :         res = os_snprintf(pos, end - pos,
         [ +  + ][ -  + ]
         [ -  + ][ +  + ]
         [ +  + ][ -  + ]
         [ -  + ][ +  + ]
         [ +  + ][ +  + ]
         [ +  + ][ +  + ]
                 [ +  + ]
    3480                 :            :                           "age=%d\n"
    3481                 :            :                           "listen_freq=%d\n"
    3482                 :            :                           "wps_method=%s\n"
    3483                 :            :                           "interface_addr=" MACSTR "\n"
    3484                 :            :                           "member_in_go_dev=" MACSTR "\n"
    3485                 :            :                           "member_in_go_iface=" MACSTR "\n"
    3486                 :            :                           "go_neg_req_sent=%d\n"
    3487                 :            :                           "go_state=%s\n"
    3488                 :            :                           "dialog_token=%u\n"
    3489                 :            :                           "intended_addr=" MACSTR "\n"
    3490                 :            :                           "country=%c%c\n"
    3491                 :            :                           "oper_freq=%d\n"
    3492                 :            :                           "req_config_methods=0x%x\n"
    3493                 :            :                           "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
    3494                 :            :                           "status=%d\n"
    3495                 :            :                           "wait_count=%u\n"
    3496                 :            :                           "invitation_reqs=%u\n",
    3497                 :        476 :                           (int) (now.sec - dev->last_seen.sec),
    3498                 :            :                           dev->listen_freq,
    3499                 :            :                           p2p_wps_method_text(dev->wps_method),
    3500                 :       1428 :                           MAC2STR(dev->interface_addr),
    3501                 :       1428 :                           MAC2STR(dev->member_in_go_dev),
    3502                 :       1428 :                           MAC2STR(dev->member_in_go_iface),
    3503                 :            :                           dev->go_neg_req_sent,
    3504                 :            :                           p2p_go_state_text(dev->go_state),
    3505                 :        238 :                           dev->dialog_token,
    3506                 :       1428 :                           MAC2STR(dev->intended_addr),
    3507                 :        371 :                           dev->country[0] ? dev->country[0] : '_',
    3508                 :        371 :                           dev->country[1] ? dev->country[1] : '_',
    3509                 :            :                           dev->oper_freq,
    3510                 :        238 :                           dev->req_config_methods,
    3511                 :        238 :                           dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
    3512                 :            :                           "[PROBE_REQ_ONLY]" : "",
    3513                 :        238 :                           dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
    3514                 :        238 :                           dev->flags & P2P_DEV_NOT_YET_READY ?
    3515                 :            :                           "[NOT_YET_READY]" : "",
    3516                 :        238 :                           dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
    3517                 :            :                           "[PD_PEER_DISPLAY]" : "",
    3518                 :        238 :                           dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
    3519                 :            :                           "[PD_PEER_KEYPAD]" : "",
    3520                 :        238 :                           dev->flags & P2P_DEV_USER_REJECTED ?
    3521                 :            :                           "[USER_REJECTED]" : "",
    3522                 :        238 :                           dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
    3523                 :            :                           "[PEER_WAITING_RESPONSE]" : "",
    3524                 :        238 :                           dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
    3525                 :            :                           "[PREFER_PERSISTENT_GROUP]" : "",
    3526                 :        238 :                           dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
    3527                 :            :                           "[WAIT_GO_NEG_RESPONSE]" : "",
    3528                 :        238 :                           dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
    3529                 :            :                           "[WAIT_GO_NEG_CONFIRM]" : "",
    3530                 :        238 :                           dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
    3531                 :            :                           "[GROUP_CLIENT_ONLY]" : "",
    3532                 :        238 :                           dev->flags & P2P_DEV_FORCE_FREQ ?
    3533                 :            :                           "[FORCE_FREQ]" : "",
    3534                 :        238 :                           dev->flags & P2P_DEV_PD_FOR_JOIN ?
    3535                 :            :                           "[PD_FOR_JOIN]" : "",
    3536                 :            :                           dev->status,
    3537                 :            :                           dev->wait_count,
    3538                 :            :                           dev->invitation_reqs);
    3539 [ +  - ][ -  + ]:        238 :         if (res < 0 || res >= end - pos)
    3540                 :          0 :                 return pos - buf;
    3541                 :        238 :         pos += res;
    3542                 :            : 
    3543         [ -  + ]:        238 :         if (dev->ext_listen_period) {
    3544                 :          0 :                 res = os_snprintf(pos, end - pos,
    3545                 :            :                                   "ext_listen_period=%u\n"
    3546                 :            :                                   "ext_listen_interval=%u\n",
    3547                 :          0 :                                   dev->ext_listen_period,
    3548                 :          0 :                                   dev->ext_listen_interval);
    3549 [ #  # ][ #  # ]:          0 :                 if (res < 0 || res >= end - pos)
    3550                 :          0 :                         return pos - buf;
    3551                 :          0 :                 pos += res;
    3552                 :            :         }
    3553                 :            : 
    3554         [ +  + ]:        238 :         if (dev->oper_ssid_len) {
    3555                 :         38 :                 res = os_snprintf(pos, end - pos,
    3556                 :            :                                   "oper_ssid=%s\n",
    3557                 :         38 :                                   wpa_ssid_txt(dev->oper_ssid,
    3558                 :            :                                                dev->oper_ssid_len));
    3559 [ +  - ][ -  + ]:         38 :                 if (res < 0 || res >= end - pos)
    3560                 :          0 :                         return pos - buf;
    3561                 :         38 :                 pos += res;
    3562                 :            :         }
    3563                 :            : 
    3564                 :            : #ifdef CONFIG_WIFI_DISPLAY
    3565         [ +  + ]:        238 :         if (dev->info.wfd_subelems) {
    3566                 :        211 :                 res = os_snprintf(pos, end - pos, "wfd_subelems=");
    3567 [ +  - ][ -  + ]:        211 :                 if (res < 0 || res >= end - pos)
    3568                 :          0 :                         return pos - buf;
    3569                 :        211 :                 pos += res;
    3570                 :            : 
    3571                 :        422 :                 pos += wpa_snprintf_hex(pos, end - pos,
    3572                 :        211 :                                         wpabuf_head(dev->info.wfd_subelems),
    3573                 :        211 :                                         wpabuf_len(dev->info.wfd_subelems));
    3574                 :            : 
    3575                 :        211 :                 res = os_snprintf(pos, end - pos, "\n");
    3576 [ +  - ][ -  + ]:        211 :                 if (res < 0 || res >= end - pos)
    3577                 :          0 :                         return pos - buf;
    3578                 :        211 :                 pos += res;
    3579                 :            :         }
    3580                 :            : #endif /* CONFIG_WIFI_DISPLAY */
    3581                 :            : 
    3582                 :        238 :         return pos - buf;
    3583                 :            : }
    3584                 :            : 
    3585                 :            : 
    3586                 :          0 : int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
    3587                 :            : {
    3588                 :          0 :         return p2p_get_device(p2p, addr) != NULL;
    3589                 :            : }
    3590                 :            : 
    3591                 :            : 
    3592                 :          0 : void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
    3593                 :            : {
    3594         [ #  # ]:          0 :         if (enabled) {
    3595                 :          0 :                 p2p_dbg(p2p, "Client discoverability enabled");
    3596                 :          0 :                 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
    3597                 :            :         } else {
    3598                 :          0 :                 p2p_dbg(p2p, "Client discoverability disabled");
    3599                 :          0 :                 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
    3600                 :            :         }
    3601                 :          0 : }
    3602                 :            : 
    3603                 :            : 
    3604                 :          1 : static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
    3605                 :            :                                               u32 duration2, u32 interval2)
    3606                 :            : {
    3607                 :            :         struct wpabuf *req;
    3608                 :          1 :         struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
    3609                 :            :         u8 *len;
    3610                 :            : 
    3611                 :          1 :         req = wpabuf_alloc(100);
    3612         [ -  + ]:          1 :         if (req == NULL)
    3613                 :          0 :                 return NULL;
    3614                 :            : 
    3615 [ -  + ][ #  # ]:          1 :         if (duration1 || interval1) {
    3616                 :          1 :                 os_memset(&desc1, 0, sizeof(desc1));
    3617                 :          1 :                 desc1.count_type = 1;
    3618                 :          1 :                 desc1.duration = duration1;
    3619                 :          1 :                 desc1.interval = interval1;
    3620                 :          1 :                 ptr1 = &desc1;
    3621                 :            : 
    3622 [ +  - ][ -  + ]:          1 :                 if (duration2 || interval2) {
    3623                 :          0 :                         os_memset(&desc2, 0, sizeof(desc2));
    3624                 :          0 :                         desc2.count_type = 2;
    3625                 :          0 :                         desc2.duration = duration2;
    3626                 :          0 :                         desc2.interval = interval2;
    3627                 :          0 :                         ptr2 = &desc2;
    3628                 :            :                 }
    3629                 :            :         }
    3630                 :            : 
    3631                 :          1 :         p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
    3632                 :          1 :         len = p2p_buf_add_ie_hdr(req);
    3633                 :          1 :         p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
    3634                 :          1 :         p2p_buf_update_ie_hdr(req, len);
    3635                 :            : 
    3636                 :          1 :         return req;
    3637                 :            : }
    3638                 :            : 
    3639                 :            : 
    3640                 :          1 : int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
    3641                 :            :                      const u8 *own_interface_addr, unsigned int freq,
    3642                 :            :                      u32 duration1, u32 interval1, u32 duration2,
    3643                 :            :                      u32 interval2)
    3644                 :            : {
    3645                 :            :         struct wpabuf *req;
    3646                 :            : 
    3647                 :          1 :         p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
    3648                 :            :                 " (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
    3649                 :            :                 "dur2=%u int2=%u",
    3650                 :         12 :                 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
    3651                 :            :                 freq, duration1, interval1, duration2, interval2);
    3652                 :            : 
    3653                 :          1 :         req = p2p_build_presence_req(duration1, interval1, duration2,
    3654                 :            :                                      interval2);
    3655         [ -  + ]:          1 :         if (req == NULL)
    3656                 :          0 :                 return -1;
    3657                 :            : 
    3658                 :          1 :         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
    3659         [ -  + ]:          2 :         if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
    3660                 :            :                             go_interface_addr,
    3661                 :          1 :                             wpabuf_head(req), wpabuf_len(req), 200) < 0) {
    3662                 :          0 :                 p2p_dbg(p2p, "Failed to send Action frame");
    3663                 :            :         }
    3664                 :          1 :         wpabuf_free(req);
    3665                 :            : 
    3666                 :          1 :         return 0;
    3667                 :            : }
    3668                 :            : 
    3669                 :            : 
    3670                 :          1 : static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
    3671                 :            :                                                size_t noa_len, u8 dialog_token)
    3672                 :            : {
    3673                 :            :         struct wpabuf *resp;
    3674                 :            :         u8 *len;
    3675                 :            : 
    3676                 :          1 :         resp = wpabuf_alloc(100 + noa_len);
    3677         [ -  + ]:          1 :         if (resp == NULL)
    3678                 :          0 :                 return NULL;
    3679                 :            : 
    3680                 :          1 :         p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
    3681                 :          1 :         len = p2p_buf_add_ie_hdr(resp);
    3682                 :          1 :         p2p_buf_add_status(resp, status);
    3683         [ -  + ]:          1 :         if (noa) {
    3684                 :          0 :                 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
    3685                 :          0 :                 wpabuf_put_le16(resp, noa_len);
    3686                 :          0 :                 wpabuf_put_data(resp, noa, noa_len);
    3687                 :            :         } else
    3688                 :          1 :                 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
    3689                 :          1 :         p2p_buf_update_ie_hdr(resp, len);
    3690                 :            : 
    3691                 :          1 :         return resp;
    3692                 :            : }
    3693                 :            : 
    3694                 :            : 
    3695                 :          1 : static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
    3696                 :            :                                      const u8 *sa, const u8 *data, size_t len,
    3697                 :            :                                      int rx_freq)
    3698                 :            : {
    3699                 :            :         struct p2p_message msg;
    3700                 :            :         u8 status;
    3701                 :            :         struct wpabuf *resp;
    3702                 :            :         size_t g;
    3703                 :          1 :         struct p2p_group *group = NULL;
    3704                 :          1 :         int parsed = 0;
    3705                 :            :         u8 noa[50];
    3706                 :            :         int noa_len;
    3707                 :            : 
    3708                 :          1 :         p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
    3709                 :            : 
    3710         [ +  - ]:          1 :         for (g = 0; g < p2p->num_groups; g++) {
    3711         [ +  - ]:          1 :                 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
    3712                 :            :                               ETH_ALEN) == 0) {
    3713                 :          1 :                         group = p2p->groups[g];
    3714                 :          1 :                         break;
    3715                 :            :                 }
    3716                 :            :         }
    3717         [ -  + ]:          1 :         if (group == NULL) {
    3718                 :          0 :                 p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
    3719                 :          0 :                         MACSTR, MAC2STR(da));
    3720                 :          0 :                 return;
    3721                 :            :         }
    3722                 :            : 
    3723         [ -  + ]:          1 :         if (p2p_parse(data, len, &msg) < 0) {
    3724                 :          0 :                 p2p_dbg(p2p, "Failed to parse P2P Presence Request");
    3725                 :          0 :                 status = P2P_SC_FAIL_INVALID_PARAMS;
    3726                 :          0 :                 goto fail;
    3727                 :            :         }
    3728                 :          1 :         parsed = 1;
    3729                 :            : 
    3730         [ -  + ]:          1 :         if (msg.noa == NULL) {
    3731                 :          0 :                 p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
    3732                 :          0 :                 status = P2P_SC_FAIL_INVALID_PARAMS;
    3733                 :          0 :                 goto fail;
    3734                 :            :         }
    3735                 :            : 
    3736                 :          1 :         status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
    3737                 :            : 
    3738                 :            : fail:
    3739         [ +  - ]:          1 :         if (p2p->cfg->get_noa)
    3740                 :          1 :                 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
    3741                 :            :                                             sizeof(noa));
    3742                 :            :         else
    3743                 :          0 :                 noa_len = -1;
    3744         [ -  + ]:          1 :         resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
    3745                 :          1 :                                        noa_len > 0 ? noa_len : 0,
    3746                 :          1 :                                        msg.dialog_token);
    3747         [ +  - ]:          1 :         if (parsed)
    3748                 :          1 :                 p2p_parse_free(&msg);
    3749         [ -  + ]:          1 :         if (resp == NULL)
    3750                 :          0 :                 return;
    3751                 :            : 
    3752                 :          1 :         p2p->pending_action_state = P2P_NO_PENDING_ACTION;
    3753         [ -  + ]:          2 :         if (p2p_send_action(p2p, rx_freq, sa, da, da,
    3754                 :          1 :                             wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
    3755                 :          0 :                 p2p_dbg(p2p, "Failed to send Action frame");
    3756                 :            :         }
    3757                 :          1 :         wpabuf_free(resp);
    3758                 :            : }
    3759                 :            : 
    3760                 :            : 
    3761                 :          1 : static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
    3762                 :            :                                       const u8 *sa, const u8 *data, size_t len)
    3763                 :            : {
    3764                 :            :         struct p2p_message msg;
    3765                 :            : 
    3766                 :          1 :         p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
    3767                 :            : 
    3768         [ -  + ]:          1 :         if (p2p_parse(data, len, &msg) < 0) {
    3769                 :          0 :                 p2p_dbg(p2p, "Failed to parse P2P Presence Response");
    3770                 :          0 :                 return;
    3771                 :            :         }
    3772                 :            : 
    3773 [ +  - ][ -  + ]:          1 :         if (msg.status == NULL || msg.noa == NULL) {
    3774                 :          0 :                 p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
    3775                 :          0 :                 p2p_parse_free(&msg);
    3776                 :          0 :                 return;
    3777                 :            :         }
    3778                 :            : 
    3779         [ +  - ]:          1 :         if (p2p->cfg->presence_resp) {
    3780                 :          1 :                 p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
    3781                 :            :                                         msg.noa, msg.noa_len);
    3782                 :            :         }
    3783                 :            : 
    3784         [ +  - ]:          1 :         if (*msg.status) {
    3785                 :          1 :                 p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
    3786                 :          1 :                         *msg.status);
    3787                 :          1 :                 p2p_parse_free(&msg);
    3788                 :          1 :                 return;
    3789                 :            :         }
    3790                 :            : 
    3791                 :          0 :         p2p_dbg(p2p, "P2P Presence Request was accepted");
    3792                 :          0 :         wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
    3793                 :          0 :                     msg.noa, msg.noa_len);
    3794                 :            :         /* TODO: process NoA */
    3795                 :          1 :         p2p_parse_free(&msg);
    3796                 :            : }
    3797                 :            : 
    3798                 :            : 
    3799                 :          0 : static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
    3800                 :            : {
    3801                 :          0 :         struct p2p_data *p2p = eloop_ctx;
    3802                 :            : 
    3803         [ #  # ]:          0 :         if (p2p->ext_listen_interval) {
    3804                 :            :                 /* Schedule next extended listen timeout */
    3805                 :          0 :                 eloop_register_timeout(p2p->ext_listen_interval_sec,
    3806                 :            :                                        p2p->ext_listen_interval_usec,
    3807                 :            :                                        p2p_ext_listen_timeout, p2p, NULL);
    3808                 :            :         }
    3809                 :            : 
    3810 [ #  # ][ #  # ]:          0 :         if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
    3811                 :            :                 /*
    3812                 :            :                  * This should not really happen, but it looks like the Listen
    3813                 :            :                  * command may fail is something else (e.g., a scan) was
    3814                 :            :                  * running at an inconvenient time. As a workaround, allow new
    3815                 :            :                  * Extended Listen operation to be started.
    3816                 :            :                  */
    3817                 :          0 :                 p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
    3818                 :          0 :                 p2p->ext_listen_only = 0;
    3819                 :          0 :                 p2p_set_state(p2p, P2P_IDLE);
    3820                 :            :         }
    3821                 :            : 
    3822         [ #  # ]:          0 :         if (p2p->state != P2P_IDLE) {
    3823                 :          0 :                 p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
    3824                 :          0 :                 return;
    3825                 :            :         }
    3826                 :            : 
    3827                 :          0 :         p2p_dbg(p2p, "Extended Listen timeout");
    3828                 :          0 :         p2p->ext_listen_only = 1;
    3829         [ #  # ]:          0 :         if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
    3830                 :          0 :                 p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
    3831                 :          0 :                 p2p->ext_listen_only = 0;
    3832                 :            :         }
    3833                 :            : }
    3834                 :            : 
    3835                 :            : 
    3836                 :          0 : int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
    3837                 :            :                    unsigned int interval)
    3838                 :            : {
    3839 [ #  # ][ #  # ]:          0 :         if (period > 65535 || interval > 65535 || period > interval ||
         [ #  # ][ #  # ]
    3840 [ #  # ][ #  # ]:          0 :             (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
                 [ #  # ]
    3841                 :          0 :                 p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
    3842                 :            :                         period, interval);
    3843                 :          0 :                 return -1;
    3844                 :            :         }
    3845                 :            : 
    3846                 :          0 :         eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
    3847                 :            : 
    3848         [ #  # ]:          0 :         if (interval == 0) {
    3849                 :          0 :                 p2p_dbg(p2p, "Disabling Extended Listen Timing");
    3850                 :          0 :                 p2p->ext_listen_period = 0;
    3851                 :          0 :                 p2p->ext_listen_interval = 0;
    3852                 :          0 :                 return 0;
    3853                 :            :         }
    3854                 :            : 
    3855                 :          0 :         p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
    3856                 :            :                 period, interval);
    3857                 :          0 :         p2p->ext_listen_period = period;
    3858                 :          0 :         p2p->ext_listen_interval = interval;
    3859                 :          0 :         p2p->ext_listen_interval_sec = interval / 1000;
    3860                 :          0 :         p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
    3861                 :            : 
    3862                 :          0 :         eloop_register_timeout(p2p->ext_listen_interval_sec,
    3863                 :            :                                p2p->ext_listen_interval_usec,
    3864                 :            :                                p2p_ext_listen_timeout, p2p, NULL);
    3865                 :            : 
    3866                 :          0 :         return 0;
    3867                 :            : }
    3868                 :            : 
    3869                 :            : 
    3870                 :         99 : void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
    3871                 :            :                       const u8 *ie, size_t ie_len)
    3872                 :            : {
    3873                 :            :         struct p2p_message msg;
    3874                 :            : 
    3875 [ +  - ][ +  - ]:         99 :         if (bssid == NULL || ie == NULL)
    3876                 :         99 :                 return;
    3877                 :            : 
    3878                 :          0 :         os_memset(&msg, 0, sizeof(msg));
    3879         [ #  # ]:          0 :         if (p2p_parse_ies(ie, ie_len, &msg))
    3880                 :          0 :                 return;
    3881         [ #  # ]:          0 :         if (msg.minor_reason_code == NULL) {
    3882                 :          0 :                 p2p_parse_free(&msg);
    3883                 :          0 :                 return;
    3884                 :            :         }
    3885                 :            : 
    3886                 :          0 :         p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
    3887                 :            :                 " reason_code=%u minor_reason_code=%u",
    3888                 :          0 :                 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
    3889                 :            : 
    3890                 :         99 :         p2p_parse_free(&msg);
    3891                 :            : }
    3892                 :            : 
    3893                 :            : 
    3894                 :          1 : void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
    3895                 :            :                         const u8 *ie, size_t ie_len)
    3896                 :            : {
    3897                 :            :         struct p2p_message msg;
    3898                 :            : 
    3899 [ +  - ][ +  - ]:          1 :         if (bssid == NULL || ie == NULL)
    3900                 :          1 :                 return;
    3901                 :            : 
    3902                 :          0 :         os_memset(&msg, 0, sizeof(msg));
    3903         [ #  # ]:          0 :         if (p2p_parse_ies(ie, ie_len, &msg))
    3904                 :          0 :                 return;
    3905         [ #  # ]:          0 :         if (msg.minor_reason_code == NULL) {
    3906                 :          0 :                 p2p_parse_free(&msg);
    3907                 :          0 :                 return;
    3908                 :            :         }
    3909                 :            : 
    3910                 :          0 :         p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
    3911                 :            :                 " reason_code=%u minor_reason_code=%u",
    3912                 :          0 :                 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
    3913                 :            : 
    3914                 :          1 :         p2p_parse_free(&msg);
    3915                 :            : }
    3916                 :            : 
    3917                 :            : 
    3918                 :          0 : void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
    3919                 :            : {
    3920         [ #  # ]:          0 :         if (enabled) {
    3921                 :          0 :                 p2p_dbg(p2p, "Managed P2P Device operations enabled");
    3922                 :          0 :                 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
    3923                 :            :         } else {
    3924                 :          0 :                 p2p_dbg(p2p, "Managed P2P Device operations disabled");
    3925                 :          0 :                 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
    3926                 :            :         }
    3927                 :          0 : }
    3928                 :            : 
    3929                 :            : 
    3930                 :          0 : int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
    3931                 :            : {
    3932         [ #  # ]:          0 :         if (p2p_channel_to_freq(reg_class, channel) < 0)
    3933                 :          0 :                 return -1;
    3934                 :            : 
    3935                 :          0 :         p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
    3936                 :            :                 reg_class, channel);
    3937                 :          0 :         p2p->cfg->reg_class = reg_class;
    3938                 :          0 :         p2p->cfg->channel = channel;
    3939                 :            : 
    3940                 :          0 :         return 0;
    3941                 :            : }
    3942                 :            : 
    3943                 :            : 
    3944                 :          0 : int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
    3945                 :            : {
    3946                 :          0 :         p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
    3947         [ #  # ]:          0 :         if (postfix == NULL) {
    3948                 :          0 :                 p2p->cfg->ssid_postfix_len = 0;
    3949                 :          0 :                 return 0;
    3950                 :            :         }
    3951         [ #  # ]:          0 :         if (len > sizeof(p2p->cfg->ssid_postfix))
    3952                 :          0 :                 return -1;
    3953                 :          0 :         os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
    3954                 :          0 :         p2p->cfg->ssid_postfix_len = len;
    3955                 :          0 :         return 0;
    3956                 :            : }
    3957                 :            : 
    3958                 :            : 
    3959                 :          0 : int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
    3960                 :            :                          int cfg_op_channel)
    3961                 :            : {
    3962         [ #  # ]:          0 :         if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
    3963                 :          0 :                 return -1;
    3964                 :            : 
    3965                 :          0 :         p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
    3966                 :            :                 op_reg_class, op_channel);
    3967                 :          0 :         p2p->cfg->op_reg_class = op_reg_class;
    3968                 :          0 :         p2p->cfg->op_channel = op_channel;
    3969                 :          0 :         p2p->cfg->cfg_op_channel = cfg_op_channel;
    3970                 :          0 :         return 0;
    3971                 :            : }
    3972                 :            : 
    3973                 :            : 
    3974                 :       2344 : int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
    3975                 :            :                       const struct p2p_channel *pref_chan)
    3976                 :            : {
    3977                 :            :         struct p2p_channel *n;
    3978                 :            : 
    3979         [ +  + ]:       2344 :         if (pref_chan) {
    3980                 :          4 :                 n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
    3981         [ -  + ]:          4 :                 if (n == NULL)
    3982                 :          0 :                         return -1;
    3983                 :          4 :                 os_memcpy(n, pref_chan,
    3984                 :            :                           num_pref_chan * sizeof(struct p2p_channel));
    3985                 :            :         } else
    3986                 :       2340 :                 n = NULL;
    3987                 :            : 
    3988                 :       2344 :         os_free(p2p->cfg->pref_chan);
    3989                 :       2344 :         p2p->cfg->pref_chan = n;
    3990                 :       2344 :         p2p->cfg->num_pref_chan = num_pref_chan;
    3991                 :            : 
    3992                 :       2344 :         return 0;
    3993                 :            : }
    3994                 :            : 
    3995                 :            : 
    3996                 :       2359 : int p2p_set_no_go_freq(struct p2p_data *p2p,
    3997                 :            :                        const struct wpa_freq_range_list *list)
    3998                 :            : {
    3999                 :            :         struct wpa_freq_range *tmp;
    4000                 :            : 
    4001 [ +  - ][ +  + ]:       2359 :         if (list == NULL || list->num == 0) {
    4002                 :       2357 :                 os_free(p2p->no_go_freq.range);
    4003                 :       2357 :                 p2p->no_go_freq.range = NULL;
    4004                 :       2357 :                 p2p->no_go_freq.num = 0;
    4005                 :       2357 :                 return 0;
    4006                 :            :         }
    4007                 :            : 
    4008                 :          2 :         tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
    4009         [ -  + ]:          2 :         if (tmp == NULL)
    4010                 :          0 :                 return -1;
    4011                 :          2 :         os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
    4012                 :          2 :         os_free(p2p->no_go_freq.range);
    4013                 :          2 :         p2p->no_go_freq.range = tmp;
    4014                 :          2 :         p2p->no_go_freq.num = list->num;
    4015                 :          2 :         p2p_dbg(p2p, "Updated no GO chan list");
    4016                 :            : 
    4017                 :       2359 :         return 0;
    4018                 :            : }
    4019                 :            : 
    4020                 :            : 
    4021                 :         53 : int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
    4022                 :            :                            u8 *iface_addr)
    4023                 :            : {
    4024                 :         53 :         struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
    4025 [ +  - ][ +  + ]:         53 :         if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
    4026                 :         46 :                 return -1;
    4027                 :          7 :         os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
    4028                 :         53 :         return 0;
    4029                 :            : }
    4030                 :            : 
    4031                 :            : 
    4032                 :         22 : int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
    4033                 :            :                            u8 *dev_addr)
    4034                 :            : {
    4035                 :         22 :         struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
    4036         [ +  - ]:         22 :         if (dev == NULL)
    4037                 :         22 :                 return -1;
    4038                 :          0 :         os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
    4039                 :         22 :         return 0;
    4040                 :            : }
    4041                 :            : 
    4042                 :            : 
    4043                 :          0 : void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
    4044                 :            : {
    4045                 :          0 :         os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
    4046         [ #  # ]:          0 :         if (is_zero_ether_addr(p2p->peer_filter))
    4047                 :          0 :                 p2p_dbg(p2p, "Disable peer filter");
    4048                 :            :         else
    4049                 :          0 :                 p2p_dbg(p2p, "Enable peer filter for " MACSTR,
    4050                 :          0 :                         MAC2STR(p2p->peer_filter));
    4051                 :          0 : }
    4052                 :            : 
    4053                 :            : 
    4054                 :          0 : void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
    4055                 :            : {
    4056         [ #  # ]:          0 :         p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
    4057         [ #  # ]:          0 :         if (p2p->cross_connect == enabled)
    4058                 :          0 :                 return;
    4059                 :          0 :         p2p->cross_connect = enabled;
    4060                 :            :         /* TODO: may need to tear down any action group where we are GO(?) */
    4061                 :            : }
    4062                 :            : 
    4063                 :            : 
    4064                 :         57 : int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
    4065                 :            : {
    4066                 :         57 :         struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
    4067         [ +  + ]:         57 :         if (dev == NULL)
    4068                 :         46 :                 return -1;
    4069         [ +  - ]:         11 :         if (dev->oper_freq <= 0)
    4070                 :         11 :                 return -1;
    4071                 :         57 :         return dev->oper_freq;
    4072                 :            : }
    4073                 :            : 
    4074                 :            : 
    4075                 :          0 : void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
    4076                 :            : {
    4077         [ #  # ]:          0 :         p2p_dbg(p2p, "Intra BSS distribution %s",
    4078                 :            :                 enabled ? "enabled" : "disabled");
    4079                 :          0 :         p2p->cfg->p2p_intra_bss = enabled;
    4080                 :          0 : }
    4081                 :            : 
    4082                 :            : 
    4083                 :       1663 : void p2p_update_channel_list(struct p2p_data *p2p,
    4084                 :            :                              const struct p2p_channels *chan,
    4085                 :            :                              const struct p2p_channels *cli_chan)
    4086                 :            : {
    4087                 :       1663 :         p2p_dbg(p2p, "Update channel list");
    4088                 :       1663 :         os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
    4089                 :       1663 :         p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
    4090                 :       1663 :         os_memcpy(&p2p->cfg->cli_channels, cli_chan,
    4091                 :            :                   sizeof(struct p2p_channels));
    4092                 :       1663 :         p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
    4093                 :       1663 : }
    4094                 :            : 
    4095                 :            : 
    4096                 :        530 : int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
    4097                 :            :                     const u8 *src, const u8 *bssid, const u8 *buf,
    4098                 :            :                     size_t len, unsigned int wait_time)
    4099                 :            : {
    4100         [ +  + ]:        530 :         if (p2p->p2p_scan_running) {
    4101                 :         21 :                 p2p_dbg(p2p, "Delay Action frame TX until p2p_scan completes");
    4102         [ +  + ]:         21 :                 if (p2p->after_scan_tx) {
    4103                 :          2 :                         p2p_dbg(p2p, "Dropped previous pending Action frame TX");
    4104                 :          2 :                         os_free(p2p->after_scan_tx);
    4105                 :            :                 }
    4106                 :         21 :                 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
    4107                 :            :                                                len);
    4108         [ -  + ]:         21 :                 if (p2p->after_scan_tx == NULL)
    4109                 :          0 :                         return -1;
    4110                 :         21 :                 p2p->after_scan_tx->freq = freq;
    4111                 :         21 :                 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
    4112                 :         21 :                 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
    4113                 :         21 :                 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
    4114                 :         21 :                 p2p->after_scan_tx->len = len;
    4115                 :         21 :                 p2p->after_scan_tx->wait_time = wait_time;
    4116                 :         21 :                 os_memcpy(p2p->after_scan_tx + 1, buf, len);
    4117                 :         21 :                 return 0;
    4118                 :            :         }
    4119                 :            : 
    4120                 :        530 :         return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
    4121                 :            :                                      buf, len, wait_time);
    4122                 :            : }
    4123                 :            : 
    4124                 :            : 
    4125                 :          0 : void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
    4126                 :            :                            int freq_overall)
    4127                 :            : {
    4128                 :          0 :         p2p_dbg(p2p, "Best channel: 2.4 GHz: %d,  5 GHz: %d,  overall: %d",
    4129                 :            :                 freq_24, freq_5, freq_overall);
    4130                 :          0 :         p2p->best_freq_24 = freq_24;
    4131                 :          0 :         p2p->best_freq_5 = freq_5;
    4132                 :          0 :         p2p->best_freq_overall = freq_overall;
    4133                 :          0 : }
    4134                 :            : 
    4135                 :            : 
    4136                 :        116 : void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
    4137                 :            : {
    4138                 :        116 :         p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
    4139                 :        116 :         p2p->own_freq_preference = freq;
    4140                 :        116 : }
    4141                 :            : 
    4142                 :            : 
    4143                 :          0 : const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
    4144                 :            : {
    4145 [ #  # ][ #  # ]:          0 :         if (p2p == NULL || p2p->go_neg_peer == NULL)
    4146                 :          0 :                 return NULL;
    4147                 :          0 :         return p2p->go_neg_peer->info.p2p_device_addr;
    4148                 :            : }
    4149                 :            : 
    4150                 :            : 
    4151                 :            : const struct p2p_peer_info *
    4152                 :          0 : p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
    4153                 :            : {
    4154                 :            :         struct p2p_device *dev;
    4155                 :            : 
    4156         [ #  # ]:          0 :         if (addr) {
    4157                 :          0 :                 dev = p2p_get_device(p2p, addr);
    4158         [ #  # ]:          0 :                 if (!dev)
    4159                 :          0 :                         return NULL;
    4160                 :            : 
    4161         [ #  # ]:          0 :                 if (!next) {
    4162         [ #  # ]:          0 :                         if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
    4163                 :          0 :                                 return NULL;
    4164                 :            : 
    4165                 :          0 :                         return &dev->info;
    4166                 :            :                 } else {
    4167                 :            :                         do {
    4168         [ #  # ]:          0 :                                 dev = dl_list_first(&dev->list,
    4169                 :            :                                                     struct p2p_device,
    4170                 :            :                                                     list);
    4171         [ #  # ]:          0 :                                 if (&dev->list == &p2p->devices)
    4172                 :          0 :                                         return NULL;
    4173         [ #  # ]:          0 :                         } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
    4174                 :            :                 }
    4175                 :            :         } else {
    4176         [ #  # ]:          0 :                 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
    4177         [ #  # ]:          0 :                 if (!dev)
    4178                 :          0 :                         return NULL;
    4179         [ #  # ]:          0 :                 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
    4180         [ #  # ]:          0 :                         dev = dl_list_first(&dev->list,
    4181                 :            :                                             struct p2p_device,
    4182                 :            :                                             list);
    4183         [ #  # ]:          0 :                         if (&dev->list == &p2p->devices)
    4184                 :          0 :                                 return NULL;
    4185                 :            :                 }
    4186                 :            :         }
    4187                 :            : 
    4188                 :          0 :         return &dev->info;
    4189                 :            : }
    4190                 :            : 
    4191                 :            : 
    4192                 :        773 : int p2p_in_progress(struct p2p_data *p2p)
    4193                 :            : {
    4194         [ -  + ]:        773 :         if (p2p == NULL)
    4195                 :          0 :                 return 0;
    4196         [ -  + ]:        773 :         if (p2p->state == P2P_SEARCH)
    4197                 :          0 :                 return 2;
    4198 [ +  + ][ -  + ]:        773 :         return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
    4199                 :            : }
    4200                 :            : 
    4201                 :            : 
    4202                 :         59 : void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
    4203                 :            :                             u8 client_timeout)
    4204                 :            : {
    4205         [ +  - ]:         59 :         if (p2p) {
    4206                 :         59 :                 p2p->go_timeout = go_timeout;
    4207                 :         59 :                 p2p->client_timeout = client_timeout;
    4208                 :            :         }
    4209                 :         59 : }
    4210                 :            : 
    4211                 :            : 
    4212                 :            : #ifdef CONFIG_WIFI_DISPLAY
    4213                 :            : 
    4214                 :         12 : static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
    4215                 :            : {
    4216                 :            :         size_t g;
    4217                 :            :         struct p2p_group *group;
    4218                 :            : 
    4219         [ -  + ]:         12 :         for (g = 0; g < p2p->num_groups; g++) {
    4220                 :          0 :                 group = p2p->groups[g];
    4221                 :          0 :                 p2p_group_force_beacon_update_ies(group);
    4222                 :            :         }
    4223                 :         12 : }
    4224                 :            : 
    4225                 :            : 
    4226                 :          6 : int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
    4227                 :            : {
    4228                 :          6 :         wpabuf_free(p2p->wfd_ie_beacon);
    4229                 :          6 :         p2p->wfd_ie_beacon = ie;
    4230                 :          6 :         p2p_update_wfd_ie_groups(p2p);
    4231                 :          6 :         return 0;
    4232                 :            : }
    4233                 :            : 
    4234                 :            : 
    4235                 :          6 : int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
    4236                 :            : {
    4237                 :          6 :         wpabuf_free(p2p->wfd_ie_probe_req);
    4238                 :          6 :         p2p->wfd_ie_probe_req = ie;
    4239                 :          6 :         return 0;
    4240                 :            : }
    4241                 :            : 
    4242                 :            : 
    4243                 :          6 : int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
    4244                 :            : {
    4245                 :          6 :         wpabuf_free(p2p->wfd_ie_probe_resp);
    4246                 :          6 :         p2p->wfd_ie_probe_resp = ie;
    4247                 :          6 :         p2p_update_wfd_ie_groups(p2p);
    4248                 :          6 :         return 0;
    4249                 :            : }
    4250                 :            : 
    4251                 :            : 
    4252                 :          6 : int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
    4253                 :            : {
    4254                 :          6 :         wpabuf_free(p2p->wfd_ie_assoc_req);
    4255                 :          6 :         p2p->wfd_ie_assoc_req = ie;
    4256                 :          6 :         return 0;
    4257                 :            : }
    4258                 :            : 
    4259                 :            : 
    4260                 :          6 : int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
    4261                 :            : {
    4262                 :          6 :         wpabuf_free(p2p->wfd_ie_invitation);
    4263                 :          6 :         p2p->wfd_ie_invitation = ie;
    4264                 :          6 :         return 0;
    4265                 :            : }
    4266                 :            : 
    4267                 :            : 
    4268                 :          6 : int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
    4269                 :            : {
    4270                 :          6 :         wpabuf_free(p2p->wfd_ie_prov_disc_req);
    4271                 :          6 :         p2p->wfd_ie_prov_disc_req = ie;
    4272                 :          6 :         return 0;
    4273                 :            : }
    4274                 :            : 
    4275                 :            : 
    4276                 :          6 : int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
    4277                 :            : {
    4278                 :          6 :         wpabuf_free(p2p->wfd_ie_prov_disc_resp);
    4279                 :          6 :         p2p->wfd_ie_prov_disc_resp = ie;
    4280                 :          6 :         return 0;
    4281                 :            : }
    4282                 :            : 
    4283                 :            : 
    4284                 :          6 : int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
    4285                 :            : {
    4286                 :          6 :         wpabuf_free(p2p->wfd_ie_go_neg);
    4287                 :          6 :         p2p->wfd_ie_go_neg = ie;
    4288                 :          6 :         return 0;
    4289                 :            : }
    4290                 :            : 
    4291                 :            : 
    4292                 :          6 : int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
    4293                 :            : {
    4294                 :          6 :         wpabuf_free(p2p->wfd_dev_info);
    4295         [ +  + ]:          6 :         if (elem) {
    4296                 :          3 :                 p2p->wfd_dev_info = wpabuf_dup(elem);
    4297         [ -  + ]:          3 :                 if (p2p->wfd_dev_info == NULL)
    4298                 :          0 :                         return -1;
    4299                 :            :         } else
    4300                 :          3 :                 p2p->wfd_dev_info = NULL;
    4301                 :            : 
    4302                 :          6 :         return 0;
    4303                 :            : }
    4304                 :            : 
    4305                 :            : 
    4306                 :          6 : int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
    4307                 :            : {
    4308                 :          6 :         wpabuf_free(p2p->wfd_assoc_bssid);
    4309         [ -  + ]:          6 :         if (elem) {
    4310                 :          0 :                 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
    4311         [ #  # ]:          0 :                 if (p2p->wfd_assoc_bssid == NULL)
    4312                 :          0 :                         return -1;
    4313                 :            :         } else
    4314                 :          6 :                 p2p->wfd_assoc_bssid = NULL;
    4315                 :            : 
    4316                 :          6 :         return 0;
    4317                 :            : }
    4318                 :            : 
    4319                 :            : 
    4320                 :          6 : int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
    4321                 :            :                                   const struct wpabuf *elem)
    4322                 :            : {
    4323                 :          6 :         wpabuf_free(p2p->wfd_coupled_sink_info);
    4324         [ -  + ]:          6 :         if (elem) {
    4325                 :          0 :                 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
    4326         [ #  # ]:          0 :                 if (p2p->wfd_coupled_sink_info == NULL)
    4327                 :          0 :                         return -1;
    4328                 :            :         } else
    4329                 :          6 :                 p2p->wfd_coupled_sink_info = NULL;
    4330                 :            : 
    4331                 :          6 :         return 0;
    4332                 :            : }
    4333                 :            : 
    4334                 :            : #endif /* CONFIG_WIFI_DISPLAY */
    4335                 :            : 
    4336                 :            : 
    4337                 :          0 : int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
    4338                 :            :                      int max_disc_tu)
    4339                 :            : {
    4340 [ #  # ][ #  # ]:          0 :         if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
                 [ #  # ]
    4341                 :          0 :                 return -1;
    4342                 :            : 
    4343                 :          0 :         p2p->min_disc_int = min_disc_int;
    4344                 :          0 :         p2p->max_disc_int = max_disc_int;
    4345                 :          0 :         p2p->max_disc_tu = max_disc_tu;
    4346                 :          0 :         p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
    4347                 :            :                 min_disc_int, max_disc_int, max_disc_tu);
    4348                 :            : 
    4349                 :          0 :         return 0;
    4350                 :            : }
    4351                 :            : 
    4352                 :            : 
    4353                 :      29927 : void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
    4354                 :            : {
    4355                 :            :         va_list ap;
    4356                 :            :         char buf[500];
    4357                 :            : 
    4358         [ -  + ]:      29927 :         if (!p2p->cfg->debug_print)
    4359                 :      29927 :                 return;
    4360                 :            : 
    4361                 :      29927 :         va_start(ap, fmt);
    4362                 :      29927 :         vsnprintf(buf, sizeof(buf), fmt, ap);
    4363                 :      29927 :         buf[sizeof(buf) - 1] = '\0';
    4364                 :      29927 :         va_end(ap);
    4365                 :      29927 :         p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
    4366                 :            : }
    4367                 :            : 
    4368                 :            : 
    4369                 :          2 : void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
    4370                 :            : {
    4371                 :            :         va_list ap;
    4372                 :            :         char buf[500];
    4373                 :            : 
    4374         [ -  + ]:          2 :         if (!p2p->cfg->debug_print)
    4375                 :          2 :                 return;
    4376                 :            : 
    4377                 :          2 :         va_start(ap, fmt);
    4378                 :          2 :         vsnprintf(buf, sizeof(buf), fmt, ap);
    4379                 :          2 :         buf[sizeof(buf) - 1] = '\0';
    4380                 :          2 :         va_end(ap);
    4381                 :          2 :         p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
    4382                 :            : }
    4383                 :            : 
    4384                 :            : 
    4385                 :          0 : void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
    4386                 :            : {
    4387                 :            :         va_list ap;
    4388                 :            :         char buf[500];
    4389                 :            : 
    4390         [ #  # ]:          0 :         if (!p2p->cfg->debug_print)
    4391                 :          0 :                 return;
    4392                 :            : 
    4393                 :          0 :         va_start(ap, fmt);
    4394                 :          0 :         vsnprintf(buf, sizeof(buf), fmt, ap);
    4395                 :          0 :         buf[sizeof(buf) - 1] = '\0';
    4396                 :          0 :         va_end(ap);
    4397                 :          0 :         p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
    4398                 :            : }
    4399                 :            : 
    4400                 :            : 
    4401                 :            : #ifdef CONFIG_WPS_NFC
    4402                 :            : 
    4403                 :         21 : static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
    4404                 :            :                                               int client_freq,
    4405                 :            :                                               const u8 *go_dev_addr,
    4406                 :            :                                               const u8 *ssid, size_t ssid_len)
    4407                 :            : {
    4408                 :            :         struct wpabuf *buf;
    4409                 :            :         u8 op_class, channel;
    4410                 :         21 :         enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
    4411                 :            : 
    4412                 :         21 :         buf = wpabuf_alloc(1000);
    4413         [ -  + ]:         21 :         if (buf == NULL)
    4414                 :          0 :                 return NULL;
    4415                 :            : 
    4416                 :         21 :         op_class = p2p->cfg->reg_class;
    4417                 :         21 :         channel = p2p->cfg->channel;
    4418                 :            : 
    4419                 :         21 :         p2p_buf_add_capability(buf, p2p->dev_capab &
    4420                 :            :                                ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
    4421                 :         21 :         p2p_buf_add_device_info(buf, p2p, NULL);
    4422                 :            : 
    4423         [ +  + ]:         21 :         if (p2p->num_groups > 0) {
    4424                 :          5 :                 role = P2P_GO_IN_A_GROUP;
    4425                 :          5 :                 p2p_freq_to_channel(p2p_group_get_freq(p2p->groups[0]),
    4426                 :            :                                     &op_class, &channel);
    4427         [ +  + ]:         16 :         } else if (client_freq > 0) {
    4428                 :          1 :                 role = P2P_CLIENT_IN_A_GROUP;
    4429                 :          1 :                 p2p_freq_to_channel(client_freq, &op_class, &channel);
    4430                 :            :         }
    4431                 :            : 
    4432                 :         21 :         p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
    4433                 :            :                                        channel, role);
    4434                 :            : 
    4435         [ +  + ]:         21 :         if (p2p->num_groups > 0) {
    4436                 :            :                 /* Limit number of clients to avoid very long message */
    4437                 :          5 :                 p2p_buf_add_group_info(p2p->groups[0], buf, 5);
    4438                 :          5 :                 p2p_group_buf_add_id(p2p->groups[0], buf);
    4439 [ +  + ][ +  - ]:         16 :         } else if (client_freq > 0 &&
    4440 [ +  - ][ +  - ]:          1 :                    go_dev_addr && !is_zero_ether_addr(go_dev_addr) &&
    4441         [ +  - ]:          1 :                    ssid && ssid_len > 0) {
    4442                 :            :                 /*
    4443                 :            :                  * Add the optional P2P Group ID to indicate in which group this
    4444                 :            :                  * device is a P2P Client.
    4445                 :            :                  */
    4446                 :          1 :                 p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len);
    4447                 :            :         }
    4448                 :            : 
    4449                 :         21 :         return buf;
    4450                 :            : }
    4451                 :            : 
    4452                 :            : 
    4453                 :          8 : struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
    4454                 :            :                                            int client_freq,
    4455                 :            :                                            const u8 *go_dev_addr,
    4456                 :            :                                            const u8 *ssid, size_t ssid_len)
    4457                 :            : {
    4458                 :          8 :         return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
    4459                 :            :                                       ssid_len);
    4460                 :            : }
    4461                 :            : 
    4462                 :            : 
    4463                 :         13 : struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
    4464                 :            :                                            int client_freq,
    4465                 :            :                                            const u8 *go_dev_addr,
    4466                 :            :                                            const u8 *ssid, size_t ssid_len)
    4467                 :            : {
    4468                 :         13 :         return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
    4469                 :            :                                       ssid_len);
    4470                 :            : }
    4471                 :            : 
    4472                 :            : 
    4473                 :         22 : int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
    4474                 :            :                                         struct p2p_nfc_params *params)
    4475                 :            : {
    4476                 :            :         struct p2p_message msg;
    4477                 :            :         struct p2p_device *dev;
    4478                 :            :         const u8 *p2p_dev_addr;
    4479                 :            :         int freq;
    4480                 :            :         enum p2p_role_indication role;
    4481                 :            : 
    4482                 :         22 :         params->next_step = NO_ACTION;
    4483                 :            : 
    4484         [ -  + ]:         22 :         if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
    4485                 :            :                                    params->p2p_attr, params->p2p_len, &msg)) {
    4486                 :          0 :                 p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
    4487                 :          0 :                 p2p_parse_free(&msg);
    4488                 :          0 :                 return -1;
    4489                 :            :         }
    4490                 :            : 
    4491         [ +  - ]:         22 :         if (msg.p2p_device_addr)
    4492                 :         22 :                 p2p_dev_addr = msg.p2p_device_addr;
    4493         [ #  # ]:          0 :         else if (msg.device_id)
    4494                 :          0 :                 p2p_dev_addr = msg.device_id;
    4495                 :            :         else {
    4496                 :          0 :                 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
    4497                 :          0 :                 p2p_parse_free(&msg);
    4498                 :          0 :                 return -1;
    4499                 :            :         }
    4500                 :            : 
    4501         [ +  + ]:         22 :         if (msg.oob_dev_password) {
    4502                 :         21 :                 os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
    4503                 :            :                           msg.oob_dev_password_len);
    4504                 :         21 :                 params->oob_dev_pw_len = msg.oob_dev_password_len;
    4505                 :            :         }
    4506                 :            : 
    4507                 :         22 :         dev = p2p_create_device(p2p, p2p_dev_addr);
    4508         [ -  + ]:         22 :         if (dev == NULL) {
    4509                 :          0 :                 p2p_parse_free(&msg);
    4510                 :          0 :                 return -1;
    4511                 :            :         }
    4512                 :            : 
    4513                 :         22 :         params->peer = &dev->info;
    4514                 :            : 
    4515                 :         22 :         os_get_reltime(&dev->last_seen);
    4516                 :         22 :         dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
    4517                 :         22 :         p2p_copy_wps_info(p2p, dev, 0, &msg);
    4518                 :            : 
    4519         [ -  + ]:         22 :         if (!msg.oob_go_neg_channel) {
    4520                 :          0 :                 p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
    4521                 :          0 :                 return -1;
    4522                 :            :         }
    4523                 :            : 
    4524 [ -  + ][ #  # ]:         22 :         if (msg.oob_go_neg_channel[3] == 0 &&
    4525                 :          0 :             msg.oob_go_neg_channel[4] == 0)
    4526                 :          0 :                 freq = 0;
    4527                 :            :         else
    4528                 :         22 :                 freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
    4529                 :         22 :                                            msg.oob_go_neg_channel[4]);
    4530         [ -  + ]:         22 :         if (freq < 0) {
    4531                 :          0 :                 p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
    4532                 :          0 :                 return -1;
    4533                 :            :         }
    4534                 :         22 :         role = msg.oob_go_neg_channel[5];
    4535                 :            : 
    4536         [ +  + ]:         22 :         if (role == P2P_GO_IN_A_GROUP) {
    4537                 :          6 :                 p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
    4538                 :          6 :                 params->go_freq = freq;
    4539         [ +  + ]:         16 :         } else if (role == P2P_CLIENT_IN_A_GROUP) {
    4540                 :          1 :                 p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz",
    4541                 :            :                         freq);
    4542                 :          1 :                 params->go_freq = freq;
    4543                 :            :         } else
    4544                 :         15 :                 p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
    4545                 :         22 :         dev->oob_go_neg_freq = freq;
    4546                 :            : 
    4547 [ +  + ][ +  + ]:         22 :         if (!params->sel && role != P2P_GO_IN_A_GROUP) {
    4548                 :          6 :                 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
    4549                 :          6 :                                            p2p->cfg->channel);
    4550         [ -  + ]:          6 :                 if (freq < 0) {
    4551                 :          0 :                         p2p_dbg(p2p, "Own listen channel not known");
    4552                 :          0 :                         return -1;
    4553                 :            :                 }
    4554                 :          6 :                 p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
    4555                 :          6 :                 dev->oob_go_neg_freq = freq;
    4556                 :            :         }
    4557                 :            : 
    4558         [ +  + ]:         22 :         if (msg.group_id) {
    4559                 :          7 :                 os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
    4560                 :          7 :                 params->go_ssid_len = msg.group_id_len - ETH_ALEN;
    4561                 :          7 :                 os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
    4562                 :            :                           params->go_ssid_len);
    4563                 :            :         }
    4564                 :            : 
    4565                 :         22 :         p2p_parse_free(&msg);
    4566                 :            : 
    4567         [ -  + ]:         22 :         if (dev->flags & P2P_DEV_USER_REJECTED) {
    4568                 :          0 :                 p2p_dbg(p2p, "Do not report rejected device");
    4569                 :          0 :                 return 0;
    4570                 :            :         }
    4571                 :            : 
    4572         [ +  - ]:         22 :         if (!(dev->flags & P2P_DEV_REPORTED)) {
    4573                 :         22 :                 p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
    4574                 :         22 :                                     !(dev->flags & P2P_DEV_REPORTED_ONCE));
    4575                 :         22 :                 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
    4576                 :            :         }
    4577                 :            : 
    4578 [ +  + ][ +  + ]:         22 :         if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
    4579                 :          2 :                 params->next_step = BOTH_GO;
    4580         [ +  + ]:         20 :         else if (role == P2P_GO_IN_A_GROUP)
    4581                 :          4 :                 params->next_step = JOIN_GROUP;
    4582         [ +  + ]:         16 :         else if (role == P2P_CLIENT_IN_A_GROUP) {
    4583                 :          1 :                 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
    4584                 :          1 :                 params->next_step = PEER_CLIENT;
    4585         [ +  + ]:         15 :         } else if (p2p->num_groups > 0)
    4586                 :          4 :                 params->next_step = AUTH_JOIN;
    4587         [ +  + ]:         11 :         else if (params->sel)
    4588                 :          7 :                 params->next_step = INIT_GO_NEG;
    4589                 :            :         else
    4590                 :          4 :                 params->next_step = RESP_GO_NEG;
    4591                 :            : 
    4592                 :         22 :         return 0;
    4593                 :            : }
    4594                 :            : 
    4595                 :            : 
    4596                 :          5 : void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
    4597                 :            :                                       int go_intent,
    4598                 :            :                                       const u8 *own_interface_addr)
    4599                 :            : {
    4600                 :            : 
    4601                 :          5 :         p2p->authorized_oob_dev_pw_id = dev_pw_id;
    4602         [ -  + ]:          5 :         if (dev_pw_id == 0) {
    4603                 :          0 :                 p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
    4604                 :          5 :                 return;
    4605                 :            :         }
    4606                 :            : 
    4607                 :          5 :         p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
    4608                 :            :                 dev_pw_id);
    4609                 :            : 
    4610                 :          5 :         p2p->go_intent = go_intent;
    4611                 :          5 :         os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
    4612                 :            : }
    4613                 :            : 
    4614                 :            : #endif /* CONFIG_WPS_NFC */

Generated by: LCOV version 1.9