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 1422976643 Lines: 2283 2803 81.4 %
Date: 2015-02-03 Functions: 173 181 95.6 %

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

Generated by: LCOV version 1.10