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 1443382998 Lines: 2359 2851 82.7 %
Date: 2015-09-27 Functions: 176 182 96.7 %

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

Generated by: LCOV version 1.10