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 1401264779 Lines: 1894 2487 76.2 %
Date: 2014-05-28 Functions: 143 162 88.3 %

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

Generated by: LCOV version 1.10