LCOV - code coverage report
Current view: top level - wpa_supplicant - p2p_supplicant.c (source / functions) Hit Total Coverage
Test: wpa_supplicant/hostapd combined for hwsim test run 1401264779 Lines: 3035 4031 75.3 %
Date: 2014-05-28 Functions: 183 202 90.6 %

          Line data    Source code
       1             : /*
       2             :  * wpa_supplicant - P2P
       3             :  * Copyright (c) 2009-2010, Atheros Communications
       4             :  * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
       5             :  *
       6             :  * This software may be distributed under the terms of the BSD license.
       7             :  * See README for more details.
       8             :  */
       9             : 
      10             : #include "includes.h"
      11             : 
      12             : #include "common.h"
      13             : #include "eloop.h"
      14             : #include "common/ieee802_11_common.h"
      15             : #include "common/ieee802_11_defs.h"
      16             : #include "common/wpa_ctrl.h"
      17             : #include "wps/wps_i.h"
      18             : #include "p2p/p2p.h"
      19             : #include "ap/hostapd.h"
      20             : #include "ap/ap_config.h"
      21             : #include "ap/sta_info.h"
      22             : #include "ap/ap_drv_ops.h"
      23             : #include "ap/wps_hostapd.h"
      24             : #include "ap/p2p_hostapd.h"
      25             : #include "eapol_supp/eapol_supp_sm.h"
      26             : #include "rsn_supp/wpa.h"
      27             : #include "wpa_supplicant_i.h"
      28             : #include "driver_i.h"
      29             : #include "ap.h"
      30             : #include "config_ssid.h"
      31             : #include "config.h"
      32             : #include "notify.h"
      33             : #include "scan.h"
      34             : #include "bss.h"
      35             : #include "offchannel.h"
      36             : #include "wps_supplicant.h"
      37             : #include "p2p_supplicant.h"
      38             : #include "wifi_display.h"
      39             : 
      40             : 
      41             : /*
      42             :  * How many times to try to scan to find the GO before giving up on join
      43             :  * request.
      44             :  */
      45             : #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
      46             : 
      47             : #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
      48             : 
      49             : #ifndef P2P_MAX_CLIENT_IDLE
      50             : /*
      51             :  * How many seconds to try to reconnect to the GO when connection in P2P client
      52             :  * role has been lost.
      53             :  */
      54             : #define P2P_MAX_CLIENT_IDLE 10
      55             : #endif /* P2P_MAX_CLIENT_IDLE */
      56             : 
      57             : #ifndef P2P_MAX_INITIAL_CONN_WAIT
      58             : /*
      59             :  * How many seconds to wait for initial 4-way handshake to get completed after
      60             :  * WPS provisioning step or after the re-invocation of a persistent group on a
      61             :  * P2P Client.
      62             :  */
      63             : #define P2P_MAX_INITIAL_CONN_WAIT 10
      64             : #endif /* P2P_MAX_INITIAL_CONN_WAIT */
      65             : 
      66             : #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
      67             : /*
      68             :  * How many seconds to wait for initial 4-way handshake to get completed after
      69             :  * WPS provisioning step on the GO. This controls the extra time the P2P
      70             :  * operation is considered to be in progress (e.g., to delay other scans) after
      71             :  * WPS provisioning has been completed on the GO during group formation.
      72             :  */
      73             : #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
      74             : #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
      75             : 
      76             : #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
      77             : /*
      78             :  * How many seconds to wait for initial 4-way handshake to get completed after
      79             :  * re-invocation of a persistent group on the GO when the client is expected
      80             :  * to connect automatically (no user interaction).
      81             :  */
      82             : #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
      83             : #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
      84             : 
      85             : #ifndef P2P_CONCURRENT_SEARCH_DELAY
      86             : #define P2P_CONCURRENT_SEARCH_DELAY 500
      87             : #endif /* P2P_CONCURRENT_SEARCH_DELAY */
      88             : 
      89             : #define P2P_MGMT_DEVICE_PREFIX          "p2p-dev-"
      90             : 
      91             : enum p2p_group_removal_reason {
      92             :         P2P_GROUP_REMOVAL_UNKNOWN,
      93             :         P2P_GROUP_REMOVAL_SILENT,
      94             :         P2P_GROUP_REMOVAL_FORMATION_FAILED,
      95             :         P2P_GROUP_REMOVAL_REQUESTED,
      96             :         P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
      97             :         P2P_GROUP_REMOVAL_UNAVAILABLE,
      98             :         P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
      99             :         P2P_GROUP_REMOVAL_PSK_FAILURE,
     100             :         P2P_GROUP_REMOVAL_FREQ_CONFLICT
     101             : };
     102             : 
     103             : 
     104             : static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
     105             : static struct wpa_supplicant *
     106             : wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
     107             :                          int go);
     108             : static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
     109             :                                const u8 *ssid, size_t ssid_len);
     110             : static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
     111             :                                    const u8 *ssid, size_t ssid_len);
     112             : static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
     113             : static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
     114             :                          const u8 *dev_addr, enum p2p_wps_method wps_method,
     115             :                          int auto_join, int freq,
     116             :                          const u8 *ssid, size_t ssid_len);
     117             : static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
     118             : static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
     119             : static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
     120             : static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
     121             : static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
     122             :                                              void *timeout_ctx);
     123             : static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
     124             : static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
     125             :                                         int group_added);
     126             : static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
     127             : static void wpas_stop_listen(void *ctx);
     128             : static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
     129             : 
     130             : 
     131             : /*
     132             :  * Get the number of concurrent channels that the HW can operate, but that are
     133             :  * currently not in use by any of the wpa_supplicant interfaces.
     134             :  */
     135          84 : static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
     136             : {
     137             :         int *freqs;
     138             :         int num, unused;
     139             : 
     140          84 :         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
     141          84 :         if (!freqs)
     142           0 :                 return -1;
     143             : 
     144          84 :         num = get_shared_radio_freqs(wpa_s, freqs,
     145             :                                      wpa_s->num_multichan_concurrent);
     146          84 :         os_free(freqs);
     147             : 
     148          84 :         unused = wpa_s->num_multichan_concurrent - num;
     149          84 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
     150          84 :         return unused;
     151             : }
     152             : 
     153             : 
     154             : /*
     155             :  * Get the frequencies that are currently in use by one or more of the virtual
     156             :  * interfaces, and that are also valid for P2P operation.
     157             :  */
     158          73 : static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
     159             :                                      int *p2p_freqs, unsigned int len)
     160             : {
     161             :         int *freqs;
     162             :         unsigned int num, i, j;
     163             : 
     164          73 :         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
     165          73 :         if (!freqs)
     166           0 :                 return -1;
     167             : 
     168          73 :         num = get_shared_radio_freqs(wpa_s, freqs,
     169             :                                      wpa_s->num_multichan_concurrent);
     170             : 
     171          73 :         os_memset(p2p_freqs, 0, sizeof(int) * len);
     172             : 
     173          79 :         for (i = 0, j = 0; i < num && j < len; i++) {
     174           6 :                 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
     175           6 :                         p2p_freqs[j++] = freqs[i];
     176             :         }
     177             : 
     178          73 :         os_free(freqs);
     179             : 
     180          73 :         dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
     181             : 
     182          73 :         return j;
     183             : }
     184             : 
     185             : 
     186         189 : static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
     187             :                                              int freq)
     188             : {
     189         189 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
     190         189 :                 return;
     191         189 :         if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
     192           0 :             freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
     193           0 :             wpas_p2p_num_unused_channels(wpa_s) > 0) {
     194           0 :                 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
     195             :                            freq);
     196           0 :                 freq = 0;
     197             :         }
     198         189 :         p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
     199             : }
     200             : 
     201             : 
     202         648 : static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
     203             :                                       struct wpa_scan_results *scan_res)
     204             : {
     205             :         size_t i;
     206             : 
     207         648 :         if (wpa_s->p2p_scan_work) {
     208         604 :                 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
     209         604 :                 wpa_s->p2p_scan_work = NULL;
     210         604 :                 radio_work_done(work);
     211             :         }
     212             : 
     213         648 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
     214         648 :                 return;
     215             : 
     216         648 :         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
     217         648 :                    (int) scan_res->num);
     218             : 
     219        2001 :         for (i = 0; i < scan_res->num; i++) {
     220        1353 :                 struct wpa_scan_res *bss = scan_res->res[i];
     221             :                 struct os_reltime time_tmp_age, entry_ts;
     222             :                 const u8 *ies;
     223             :                 size_t ies_len;
     224             : 
     225        1353 :                 time_tmp_age.sec = bss->age / 1000;
     226        1353 :                 time_tmp_age.usec = (bss->age % 1000) * 1000;
     227        1353 :                 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
     228             : 
     229        1353 :                 ies = (const u8 *) (bss + 1);
     230        1353 :                 ies_len = bss->ie_len;
     231        2062 :                 if (bss->beacon_ie_len > 0 &&
     232         964 :                     !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
     233         255 :                     wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
     234          96 :                         wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
     235          96 :                                    MACSTR, MAC2STR(bss->bssid));
     236          16 :                         ies = ies + ies_len;
     237          16 :                         ies_len = bss->beacon_ie_len;
     238             :                 }
     239             : 
     240             : 
     241        1353 :                 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
     242             :                                          bss->freq, &entry_ts, bss->level,
     243             :                                          ies, ies_len) > 0)
     244           0 :                         break;
     245             :         }
     246             : 
     247         648 :         p2p_scan_res_handled(wpa_s->global->p2p);
     248             : }
     249             : 
     250             : 
     251         623 : static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
     252             : {
     253         623 :         struct wpa_supplicant *wpa_s = work->wpa_s;
     254         623 :         struct wpa_driver_scan_params *params = work->ctx;
     255             :         int ret;
     256             : 
     257         623 :         if (deinit) {
     258          10 :                 if (!work->started) {
     259           1 :                         wpa_scan_free_params(params);
     260           1 :                         return;
     261             :                 }
     262             : 
     263           9 :                 wpa_s->p2p_scan_work = NULL;
     264           9 :                 return;
     265             :         }
     266             : 
     267         613 :         ret = wpa_drv_scan(wpa_s, params);
     268         613 :         wpa_scan_free_params(params);
     269         613 :         work->ctx = NULL;
     270         613 :         if (ret) {
     271           0 :                 radio_work_done(work);
     272           0 :                 return;
     273             :         }
     274             : 
     275         613 :         os_get_reltime(&wpa_s->scan_trigger_time);
     276         613 :         wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
     277         613 :         wpa_s->own_scan_requested = 1;
     278         613 :         wpa_s->p2p_scan_work = work;
     279             : }
     280             : 
     281             : 
     282         623 : static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
     283             :                          unsigned int num_req_dev_types,
     284             :                          const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
     285             : {
     286         623 :         struct wpa_supplicant *wpa_s = ctx;
     287         623 :         struct wpa_driver_scan_params *params = NULL;
     288             :         struct wpabuf *wps_ie, *ies;
     289             :         size_t ielen;
     290             :         u8 *n;
     291             : 
     292         623 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
     293           0 :                 return -1;
     294             : 
     295         623 :         if (wpa_s->p2p_scan_work) {
     296           9 :                 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
     297           9 :                 return -1;
     298             :         }
     299             : 
     300         614 :         params = os_zalloc(sizeof(*params));
     301         614 :         if (params == NULL)
     302           0 :                 return -1;
     303             : 
     304             :         /* P2P Wildcard SSID */
     305         614 :         params->num_ssids = 1;
     306         614 :         n = os_malloc(P2P_WILDCARD_SSID_LEN);
     307         614 :         if (n == NULL)
     308           0 :                 goto fail;
     309         614 :         os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
     310         614 :         params->ssids[0].ssid = n;
     311         614 :         params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
     312             : 
     313         614 :         wpa_s->wps->dev.p2p = 1;
     314         614 :         wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
     315         614 :                                         wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
     316             :                                         num_req_dev_types, req_dev_types);
     317         614 :         if (wps_ie == NULL)
     318           0 :                 goto fail;
     319             : 
     320         614 :         ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
     321         614 :         ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
     322         614 :         if (ies == NULL) {
     323           0 :                 wpabuf_free(wps_ie);
     324           0 :                 goto fail;
     325             :         }
     326         614 :         wpabuf_put_buf(ies, wps_ie);
     327         614 :         wpabuf_free(wps_ie);
     328             : 
     329         614 :         p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
     330             : 
     331         614 :         params->p2p_probe = 1;
     332         614 :         n = os_malloc(wpabuf_len(ies));
     333         614 :         if (n == NULL) {
     334           0 :                 wpabuf_free(ies);
     335           0 :                 goto fail;
     336             :         }
     337         614 :         os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
     338         614 :         params->extra_ies = n;
     339         614 :         params->extra_ies_len = wpabuf_len(ies);
     340         614 :         wpabuf_free(ies);
     341             : 
     342         614 :         switch (type) {
     343             :         case P2P_SCAN_SOCIAL:
     344         585 :                 params->freqs = os_malloc(4 * sizeof(int));
     345         585 :                 if (params->freqs == NULL)
     346           0 :                         goto fail;
     347         585 :                 params->freqs[0] = 2412;
     348         585 :                 params->freqs[1] = 2437;
     349         585 :                 params->freqs[2] = 2462;
     350         585 :                 params->freqs[3] = 0;
     351         585 :                 break;
     352             :         case P2P_SCAN_FULL:
     353          27 :                 break;
     354             :         case P2P_SCAN_SOCIAL_PLUS_ONE:
     355           2 :                 params->freqs = os_malloc(5 * sizeof(int));
     356           2 :                 if (params->freqs == NULL)
     357           0 :                         goto fail;
     358           2 :                 params->freqs[0] = 2412;
     359           2 :                 params->freqs[1] = 2437;
     360           2 :                 params->freqs[2] = 2462;
     361           2 :                 params->freqs[3] = freq;
     362           2 :                 params->freqs[4] = 0;
     363           2 :                 break;
     364             :         }
     365             : 
     366         614 :         radio_remove_works(wpa_s, "p2p-scan", 0);
     367         614 :         if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
     368             :                            params) < 0)
     369           0 :                 goto fail;
     370         614 :         return 0;
     371             : 
     372             : fail:
     373           0 :         wpa_scan_free_params(params);
     374           0 :         return -1;
     375             : }
     376             : 
     377             : 
     378          35 : static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
     379             : {
     380          35 :         switch (p2p_group_interface) {
     381             :         case P2P_GROUP_INTERFACE_PENDING:
     382           0 :                 return WPA_IF_P2P_GROUP;
     383             :         case P2P_GROUP_INTERFACE_GO:
     384          16 :                 return WPA_IF_P2P_GO;
     385             :         case P2P_GROUP_INTERFACE_CLIENT:
     386          19 :                 return WPA_IF_P2P_CLIENT;
     387             :         }
     388             : 
     389           0 :         return WPA_IF_P2P_GROUP;
     390             : }
     391             : 
     392             : 
     393          59 : static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
     394             :                                                   const u8 *ssid,
     395             :                                                   size_t ssid_len, int *go)
     396             : {
     397             :         struct wpa_ssid *s;
     398             : 
     399         116 :         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
     400         119 :                 for (s = wpa_s->conf->ssid; s; s = s->next) {
     401          64 :                         if (s->disabled != 0 || !s->p2p_group ||
     402           4 :                             s->ssid_len != ssid_len ||
     403           2 :                             os_memcmp(ssid, s->ssid, ssid_len) != 0)
     404          60 :                                 continue;
     405           4 :                         if (s->mode == WPAS_MODE_P2P_GO &&
     406           2 :                             s != wpa_s->current_ssid)
     407           0 :                                 continue;
     408           2 :                         if (go)
     409           2 :                                 *go = s->mode == WPAS_MODE_P2P_GO;
     410           2 :                         return wpa_s;
     411             :                 }
     412             :         }
     413             : 
     414          57 :         return NULL;
     415             : }
     416             : 
     417             : 
     418         256 : static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
     419             :                                  enum p2p_group_removal_reason removal_reason)
     420             : {
     421             :         struct wpa_ssid *ssid;
     422             :         char *gtype;
     423             :         const char *reason;
     424             : 
     425         256 :         ssid = wpa_s->current_ssid;
     426         256 :         if (ssid == NULL) {
     427             :                 /*
     428             :                  * The current SSID was not known, but there may still be a
     429             :                  * pending P2P group interface waiting for provisioning or a
     430             :                  * P2P group that is trying to reconnect.
     431             :                  */
     432          18 :                 ssid = wpa_s->conf->ssid;
     433          38 :                 while (ssid) {
     434           7 :                         if (ssid->p2p_group && ssid->disabled != 2)
     435           5 :                                 break;
     436           2 :                         ssid = ssid->next;
     437             :                 }
     438          31 :                 if (ssid == NULL &&
     439          13 :                         wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
     440             :                 {
     441          13 :                         wpa_printf(MSG_ERROR, "P2P: P2P group interface "
     442             :                                    "not found");
     443          13 :                         return -1;
     444             :                 }
     445             :         }
     446         243 :         if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
     447          16 :                 gtype = "GO";
     448         227 :         else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
     449         208 :                  (ssid && ssid->mode == WPAS_MODE_INFRA)) {
     450         125 :                 wpa_s->reassociate = 0;
     451         125 :                 wpa_s->disconnected = 1;
     452         125 :                 wpa_supplicant_deauthenticate(wpa_s,
     453             :                                               WLAN_REASON_DEAUTH_LEAVING);
     454         125 :                 gtype = "client";
     455             :         } else
     456         102 :                 gtype = "GO";
     457         243 :         if (wpa_s->cross_connect_in_use) {
     458           0 :                 wpa_s->cross_connect_in_use = 0;
     459           0 :                 wpa_msg_global(wpa_s->parent, MSG_INFO,
     460             :                                P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
     461           0 :                                wpa_s->ifname, wpa_s->cross_connect_uplink);
     462             :         }
     463         243 :         switch (removal_reason) {
     464             :         case P2P_GROUP_REMOVAL_REQUESTED:
     465         160 :                 reason = " reason=REQUESTED";
     466         160 :                 break;
     467             :         case P2P_GROUP_REMOVAL_FORMATION_FAILED:
     468          11 :                 reason = " reason=FORMATION_FAILED";
     469          11 :                 break;
     470             :         case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
     471           1 :                 reason = " reason=IDLE";
     472           1 :                 break;
     473             :         case P2P_GROUP_REMOVAL_UNAVAILABLE:
     474           1 :                 reason = " reason=UNAVAILABLE";
     475           1 :                 break;
     476             :         case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
     477          69 :                 reason = " reason=GO_ENDING_SESSION";
     478          69 :                 break;
     479             :         case P2P_GROUP_REMOVAL_PSK_FAILURE:
     480           1 :                 reason = " reason=PSK_FAILURE";
     481           1 :                 break;
     482             :         case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
     483           0 :                 reason = " reason=FREQ_CONFLICT";
     484           0 :                 break;
     485             :         default:
     486           0 :                 reason = "";
     487           0 :                 break;
     488             :         }
     489         243 :         if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
     490         243 :                 wpa_msg_global(wpa_s->parent, MSG_INFO,
     491             :                                P2P_EVENT_GROUP_REMOVED "%s %s%s",
     492         243 :                                wpa_s->ifname, gtype, reason);
     493             :         }
     494             : 
     495         243 :         if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
     496           0 :                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
     497         243 :         if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
     498         117 :                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
     499         243 :         if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
     500         243 :                                  wpa_s->parent, NULL) > 0) {
     501           3 :                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
     502             :                            "timeout");
     503           3 :                 wpa_s->p2p_in_provisioning = 0;
     504             :         }
     505             : 
     506         243 :         wpa_s->p2p_in_invitation = 0;
     507             : 
     508             :         /*
     509             :          * Make sure wait for the first client does not remain active after the
     510             :          * group has been removed.
     511             :          */
     512         243 :         wpa_s->global->p2p_go_wait_client.sec = 0;
     513             : 
     514         243 :         if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
     515         243 :                 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
     516             : 
     517         243 :         if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
     518             :                 struct wpa_global *global;
     519             :                 char *ifname;
     520             :                 enum wpa_driver_if_type type;
     521          35 :                 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
     522          35 :                         wpa_s->ifname);
     523          35 :                 global = wpa_s->global;
     524          35 :                 ifname = os_strdup(wpa_s->ifname);
     525          35 :                 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
     526          35 :                 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
     527          35 :                 wpa_s = global->ifaces;
     528          35 :                 if (wpa_s && ifname)
     529          34 :                         wpa_drv_if_remove(wpa_s, type, ifname);
     530          35 :                 os_free(ifname);
     531          35 :                 return 1;
     532             :         }
     533             : 
     534         208 :         if (!wpa_s->p2p_go_group_formation_completed) {
     535           1 :                 wpa_s->global->p2p_group_formation = NULL;
     536           1 :                 wpa_s->p2p_in_provisioning = 0;
     537             :         }
     538             : 
     539         208 :         wpa_s->show_group_started = 0;
     540         208 :         os_free(wpa_s->go_params);
     541         208 :         wpa_s->go_params = NULL;
     542             : 
     543         208 :         wpa_s->waiting_presence_resp = 0;
     544             : 
     545         208 :         wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
     546         208 :         if (ssid && (ssid->p2p_group ||
     547           0 :                      ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
     548         208 :                      (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
     549         208 :                 int id = ssid->id;
     550         208 :                 if (ssid == wpa_s->current_ssid) {
     551         102 :                         wpa_sm_set_config(wpa_s->wpa, NULL);
     552         102 :                         eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
     553         102 :                         wpa_s->current_ssid = NULL;
     554             :                 }
     555             :                 /*
     556             :                  * Networks objects created during any P2P activities are not
     557             :                  * exposed out as they might/will confuse certain non-P2P aware
     558             :                  * applications since these network objects won't behave like
     559             :                  * regular ones.
     560             :                  *
     561             :                  * Likewise, we don't send out network removed signals for such
     562             :                  * network objects.
     563             :                  */
     564         208 :                 wpa_config_remove_network(wpa_s->conf, id);
     565         208 :                 wpa_supplicant_clear_status(wpa_s);
     566         208 :                 wpa_supplicant_cancel_sched_scan(wpa_s);
     567             :         } else {
     568           0 :                 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
     569             :                            "found");
     570             :         }
     571         208 :         if (wpa_s->ap_iface)
     572         102 :                 wpa_supplicant_ap_deinit(wpa_s);
     573             :         else
     574         106 :                 wpa_drv_deinit_p2p_cli(wpa_s);
     575             : 
     576         208 :         return 0;
     577             : }
     578             : 
     579             : 
     580         313 : static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
     581             :                                      u8 *go_dev_addr,
     582             :                                      const u8 *ssid, size_t ssid_len)
     583             : {
     584             :         struct wpa_bss *bss;
     585             :         const u8 *bssid;
     586             :         struct wpabuf *p2p;
     587             :         u8 group_capab;
     588             :         const u8 *addr;
     589             : 
     590         313 :         if (wpa_s->go_params)
     591         294 :                 bssid = wpa_s->go_params->peer_interface_addr;
     592             :         else
     593          19 :                 bssid = wpa_s->bssid;
     594             : 
     595         313 :         bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
     596         327 :         if (bss == NULL && wpa_s->go_params &&
     597          14 :             !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
     598          14 :                 bss = wpa_bss_get_p2p_dev_addr(
     599          14 :                         wpa_s, wpa_s->go_params->peer_device_addr);
     600         313 :         if (bss == NULL) {
     601             :                 u8 iface_addr[ETH_ALEN];
     602           0 :                 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
     603             :                                            iface_addr) == 0)
     604           0 :                         bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
     605             :         }
     606         313 :         if (bss == NULL) {
     607           0 :                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
     608             :                            "group is persistent - BSS " MACSTR " not found",
     609           0 :                            MAC2STR(bssid));
     610           0 :                 return 0;
     611             :         }
     612             : 
     613         313 :         p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
     614         313 :         if (p2p == NULL)
     615           0 :                 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
     616             :                                                          P2P_IE_VENDOR_TYPE);
     617         313 :         if (p2p == NULL) {
     618           0 :                 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
     619             :                            "group is persistent - BSS " MACSTR
     620           0 :                            " did not include P2P IE", MAC2STR(bssid));
     621           0 :                 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
     622             :                             (u8 *) (bss + 1), bss->ie_len);
     623           0 :                 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
     624           0 :                             ((u8 *) bss + 1) + bss->ie_len,
     625             :                             bss->beacon_ie_len);
     626           0 :                 return 0;
     627             :         }
     628             : 
     629         313 :         group_capab = p2p_get_group_capab(p2p);
     630         313 :         addr = p2p_get_go_dev_addr(p2p);
     631         313 :         wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
     632             :                    "group_capab=0x%x", group_capab);
     633         313 :         if (addr) {
     634         313 :                 os_memcpy(go_dev_addr, addr, ETH_ALEN);
     635        1878 :                 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
     636        1878 :                            MAC2STR(addr));
     637             :         } else
     638           0 :                 os_memset(go_dev_addr, 0, ETH_ALEN);
     639         313 :         wpabuf_free(p2p);
     640             : 
     641        3756 :         wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
     642             :                    "go_dev_addr=" MACSTR,
     643        3756 :                    MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
     644             : 
     645         313 :         return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
     646             : }
     647             : 
     648             : 
     649          83 : static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
     650             :                                            struct wpa_ssid *ssid,
     651             :                                            const u8 *go_dev_addr)
     652             : {
     653             :         struct wpa_ssid *s;
     654          83 :         int changed = 0;
     655             : 
     656         498 :         wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
     657         498 :                    "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
     658         135 :         for (s = wpa_s->conf->ssid; s; s = s->next) {
     659         157 :                 if (s->disabled == 2 &&
     660         105 :                     os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
     661         104 :                     s->ssid_len == ssid->ssid_len &&
     662          52 :                     os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
     663          52 :                         break;
     664             :         }
     665             : 
     666          83 :         if (s) {
     667          52 :                 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
     668             :                            "entry");
     669          52 :                 if (ssid->passphrase && !s->passphrase)
     670           0 :                         changed = 1;
     671          69 :                 else if (ssid->passphrase && s->passphrase &&
     672          17 :                          os_strcmp(ssid->passphrase, s->passphrase) != 0)
     673           0 :                         changed = 1;
     674             :         } else {
     675          31 :                 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
     676             :                            "entry");
     677          31 :                 changed = 1;
     678          31 :                 s = wpa_config_add_network(wpa_s->conf);
     679          31 :                 if (s == NULL)
     680           0 :                         return -1;
     681             : 
     682             :                 /*
     683             :                  * Instead of network_added we emit persistent_group_added
     684             :                  * notification. Also to keep the defense checks in
     685             :                  * persistent_group obj registration method, we set the
     686             :                  * relevant flags in s to designate it as a persistent group.
     687             :                  */
     688          31 :                 s->p2p_group = 1;
     689          31 :                 s->p2p_persistent_group = 1;
     690          31 :                 wpas_notify_persistent_group_added(wpa_s, s);
     691          31 :                 wpa_config_set_network_defaults(s);
     692             :         }
     693             : 
     694          83 :         s->p2p_group = 1;
     695          83 :         s->p2p_persistent_group = 1;
     696          83 :         s->disabled = 2;
     697          83 :         s->bssid_set = 1;
     698          83 :         os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
     699          83 :         s->mode = ssid->mode;
     700          83 :         s->auth_alg = WPA_AUTH_ALG_OPEN;
     701          83 :         s->key_mgmt = WPA_KEY_MGMT_PSK;
     702          83 :         s->proto = WPA_PROTO_RSN;
     703          83 :         s->pairwise_cipher = WPA_CIPHER_CCMP;
     704          83 :         s->export_keys = 1;
     705          83 :         if (ssid->passphrase) {
     706          30 :                 os_free(s->passphrase);
     707          30 :                 s->passphrase = os_strdup(ssid->passphrase);
     708             :         }
     709          83 :         if (ssid->psk_set) {
     710          83 :                 s->psk_set = 1;
     711          83 :                 os_memcpy(s->psk, ssid->psk, 32);
     712             :         }
     713          83 :         if (s->passphrase && !s->psk_set)
     714           0 :                 wpa_config_update_psk(s);
     715          83 :         if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
     716          31 :                 os_free(s->ssid);
     717          31 :                 s->ssid = os_malloc(ssid->ssid_len);
     718             :         }
     719          83 :         if (s->ssid) {
     720          83 :                 s->ssid_len = ssid->ssid_len;
     721          83 :                 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
     722             :         }
     723          83 :         if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
     724           2 :                 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
     725           2 :                 wpa_s->global->add_psk = NULL;
     726           2 :                 changed = 1;
     727             :         }
     728             : 
     729          83 :         if (changed && wpa_s->conf->update_config &&
     730           0 :             wpa_config_write(wpa_s->confname, wpa_s->conf)) {
     731           0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
     732             :         }
     733             : 
     734          83 :         return s->id;
     735             : }
     736             : 
     737             : 
     738         116 : static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
     739             :                                                  const u8 *addr)
     740             : {
     741             :         struct wpa_ssid *ssid, *s;
     742             :         u8 *n;
     743             :         size_t i;
     744         116 :         int found = 0;
     745             : 
     746         116 :         ssid = wpa_s->current_ssid;
     747         232 :         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
     748         116 :             !ssid->p2p_persistent_group)
     749          81 :                 return;
     750             : 
     751          54 :         for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
     752          54 :                 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
     753          19 :                         continue;
     754             : 
     755          70 :                 if (s->ssid_len == ssid->ssid_len &&
     756          35 :                     os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
     757          35 :                         break;
     758             :         }
     759             : 
     760          35 :         if (s == NULL)
     761           0 :                 return;
     762             : 
     763          78 :         for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
     764          22 :                 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
     765             :                               ETH_ALEN) != 0)
     766           4 :                         continue;
     767             : 
     768          18 :                 if (i == s->num_p2p_clients - 1)
     769          17 :                         return; /* already the most recent entry */
     770             : 
     771             :                 /* move the entry to mark it most recent */
     772           1 :                 os_memmove(s->p2p_client_list + i * ETH_ALEN,
     773             :                            s->p2p_client_list + (i + 1) * ETH_ALEN,
     774             :                            (s->num_p2p_clients - i - 1) * ETH_ALEN);
     775           1 :                 os_memcpy(s->p2p_client_list +
     776             :                           (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
     777           1 :                 found = 1;
     778           1 :                 break;
     779             :         }
     780             : 
     781          18 :         if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
     782          17 :                 n = os_realloc_array(s->p2p_client_list,
     783          17 :                                      s->num_p2p_clients + 1, ETH_ALEN);
     784          17 :                 if (n == NULL)
     785           0 :                         return;
     786          17 :                 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
     787          17 :                 s->p2p_client_list = n;
     788          17 :                 s->num_p2p_clients++;
     789           1 :         } else if (!found) {
     790             :                 /* Not enough room for an additional entry - drop the oldest
     791             :                  * entry */
     792           0 :                 os_memmove(s->p2p_client_list,
     793             :                            s->p2p_client_list + ETH_ALEN,
     794             :                            (s->num_p2p_clients - 1) * ETH_ALEN);
     795           0 :                 os_memcpy(s->p2p_client_list +
     796             :                           (s->num_p2p_clients - 1) * ETH_ALEN,
     797             :                           addr, ETH_ALEN);
     798             :         }
     799             : 
     800          18 :         if (wpa_s->parent->conf->update_config &&
     801           0 :             wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
     802           0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
     803             : }
     804             : 
     805             : 
     806         172 : static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
     807             :                                            int success)
     808             : {
     809             :         struct wpa_ssid *ssid;
     810             :         const char *ssid_txt;
     811             :         int client;
     812             :         int persistent;
     813             :         u8 go_dev_addr[ETH_ALEN];
     814         172 :         int network_id = -1;
     815             : 
     816             :         /*
     817             :          * This callback is likely called for the main interface. Update wpa_s
     818             :          * to use the group interface if a new interface was created for the
     819             :          * group.
     820             :          */
     821         172 :         if (wpa_s->global->p2p_group_formation)
     822         171 :                 wpa_s = wpa_s->global->p2p_group_formation;
     823         172 :         if (wpa_s->p2p_go_group_formation_completed) {
     824         105 :                 wpa_s->global->p2p_group_formation = NULL;
     825         105 :                 wpa_s->p2p_in_provisioning = 0;
     826             :         }
     827         172 :         wpa_s->p2p_in_invitation = 0;
     828             : 
     829         172 :         if (!success) {
     830          11 :                 wpa_msg_global(wpa_s->parent, MSG_INFO,
     831             :                                P2P_EVENT_GROUP_FORMATION_FAILURE);
     832          11 :                 wpas_p2p_group_delete(wpa_s,
     833             :                                       P2P_GROUP_REMOVAL_FORMATION_FAILED);
     834         183 :                 return;
     835             :         }
     836             : 
     837         161 :         wpa_msg_global(wpa_s->parent, MSG_INFO,
     838             :                        P2P_EVENT_GROUP_FORMATION_SUCCESS);
     839             : 
     840         161 :         ssid = wpa_s->current_ssid;
     841         161 :         if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
     842          63 :                 ssid->mode = WPAS_MODE_P2P_GO;
     843          63 :                 p2p_group_notif_formation_done(wpa_s->p2p_group);
     844          63 :                 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
     845             :         }
     846             : 
     847         161 :         persistent = 0;
     848         161 :         if (ssid) {
     849         161 :                 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
     850         161 :                 client = ssid->mode == WPAS_MODE_INFRA;
     851         161 :                 if (ssid->mode == WPAS_MODE_P2P_GO) {
     852          63 :                         persistent = ssid->p2p_persistent_group;
     853          63 :                         os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
     854             :                                   ETH_ALEN);
     855             :                 } else
     856         196 :                         persistent = wpas_p2p_persistent_group(wpa_s,
     857             :                                                                go_dev_addr,
     858          98 :                                                                ssid->ssid,
     859             :                                                                ssid->ssid_len);
     860             :         } else {
     861           0 :                 ssid_txt = "";
     862           0 :                 client = wpa_s->p2p_group_interface ==
     863             :                         P2P_GROUP_INTERFACE_CLIENT;
     864           0 :                 os_memset(go_dev_addr, 0, ETH_ALEN);
     865             :         }
     866             : 
     867         161 :         wpa_s->show_group_started = 0;
     868         161 :         if (client) {
     869             :                 /*
     870             :                  * Indicate event only after successfully completed 4-way
     871             :                  * handshake, i.e., when the interface is ready for data
     872             :                  * packets.
     873             :                  */
     874          98 :                 wpa_s->show_group_started = 1;
     875          63 :         } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
     876             :                 char psk[65];
     877           0 :                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
     878           0 :                 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
     879             :                                "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
     880             :                                MACSTR "%s",
     881           0 :                                wpa_s->ifname, ssid_txt, ssid->frequency, psk,
     882           0 :                                MAC2STR(go_dev_addr),
     883             :                                persistent ? " [PERSISTENT]" : "");
     884           0 :                 wpas_p2p_cross_connect_setup(wpa_s);
     885           0 :                 wpas_p2p_set_group_idle_timeout(wpa_s);
     886             :         } else {
     887         567 :                 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
     888             :                                "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
     889             :                                "go_dev_addr=" MACSTR "%s",
     890          63 :                                wpa_s->ifname, ssid_txt,
     891             :                                ssid ? ssid->frequency : 0,
     892          63 :                                ssid && ssid->passphrase ? ssid->passphrase : "",
     893         378 :                                MAC2STR(go_dev_addr),
     894             :                                persistent ? " [PERSISTENT]" : "");
     895          63 :                 wpas_p2p_cross_connect_setup(wpa_s);
     896          63 :                 wpas_p2p_set_group_idle_timeout(wpa_s);
     897             :         }
     898             : 
     899         161 :         if (persistent)
     900          30 :                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
     901             :                                                              ssid, go_dev_addr);
     902             :         else {
     903         131 :                 os_free(wpa_s->global->add_psk);
     904         131 :                 wpa_s->global->add_psk = NULL;
     905             :         }
     906         161 :         if (network_id < 0 && ssid)
     907         131 :                 network_id = ssid->id;
     908         161 :         if (!client) {
     909          63 :                 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
     910          63 :                 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
     911             :         }
     912             : }
     913             : 
     914             : 
     915             : struct send_action_work {
     916             :         unsigned int freq;
     917             :         u8 dst[ETH_ALEN];
     918             :         u8 src[ETH_ALEN];
     919             :         u8 bssid[ETH_ALEN];
     920             :         size_t len;
     921             :         unsigned int wait_time;
     922             :         u8 buf[0];
     923             : };
     924             : 
     925             : 
     926         130 : static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
     927             :                                               void *timeout_ctx)
     928             : {
     929         130 :         struct wpa_supplicant *wpa_s = eloop_ctx;
     930             : 
     931         130 :         if (!wpa_s->p2p_send_action_work)
     932         130 :                 return;
     933             : 
     934         130 :         wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
     935         130 :         os_free(wpa_s->p2p_send_action_work->ctx);
     936         130 :         radio_work_done(wpa_s->p2p_send_action_work);
     937         130 :         wpa_s->p2p_send_action_work = NULL;
     938             : }
     939             : 
     940             : 
     941         647 : static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
     942             :                                            unsigned int freq,
     943             :                                            const u8 *dst, const u8 *src,
     944             :                                            const u8 *bssid,
     945             :                                            const u8 *data, size_t data_len,
     946             :                                            enum offchannel_send_action_result
     947             :                                            result)
     948             : {
     949         647 :         enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
     950             : 
     951         647 :         if (wpa_s->p2p_send_action_work) {
     952             :                 struct send_action_work *awork;
     953         471 :                 awork = wpa_s->p2p_send_action_work->ctx;
     954         471 :                 if (awork->wait_time == 0) {
     955           0 :                         os_free(awork);
     956           0 :                         radio_work_done(wpa_s->p2p_send_action_work);
     957           0 :                         wpa_s->p2p_send_action_work = NULL;
     958             :                 } else {
     959             :                         /*
     960             :                          * In theory, this should not be needed, but number of
     961             :                          * places in the P2P code is still using non-zero wait
     962             :                          * time for the last Action frame in the sequence and
     963             :                          * some of these do not call send_action_done().
     964             :                          */
     965         471 :                         eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
     966             :                                              wpa_s, NULL);
     967         471 :                         eloop_register_timeout(
     968         471 :                                 0, awork->wait_time * 1000,
     969             :                                 wpas_p2p_send_action_work_timeout,
     970             :                                 wpa_s, NULL);
     971             :                 }
     972             :         }
     973             : 
     974         647 :         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
     975           0 :                 return;
     976             : 
     977         647 :         switch (result) {
     978             :         case OFFCHANNEL_SEND_ACTION_SUCCESS:
     979         508 :                 res = P2P_SEND_ACTION_SUCCESS;
     980         508 :                 break;
     981             :         case OFFCHANNEL_SEND_ACTION_NO_ACK:
     982         139 :                 res = P2P_SEND_ACTION_NO_ACK;
     983         139 :                 break;
     984             :         case OFFCHANNEL_SEND_ACTION_FAILED:
     985           0 :                 res = P2P_SEND_ACTION_FAILED;
     986           0 :                 break;
     987             :         }
     988             : 
     989         647 :         p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
     990             : 
     991         786 :         if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
     992         139 :             wpa_s->pending_pd_before_join &&
     993           0 :             (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
     994           0 :              os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
     995             :             wpa_s->p2p_fallback_to_go_neg) {
     996           0 :                 wpa_s->pending_pd_before_join = 0;
     997           0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
     998             :                         "during p2p_connect-auto");
     999           0 :                 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
    1000           0 :                 return;
    1001             :         }
    1002             : }
    1003             : 
    1004             : 
    1005         390 : static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
    1006             : {
    1007         390 :         struct wpa_supplicant *wpa_s = work->wpa_s;
    1008         390 :         struct send_action_work *awork = work->ctx;
    1009             : 
    1010         390 :         if (deinit) {
    1011           5 :                 if (work->started) {
    1012           5 :                         eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
    1013             :                                              wpa_s, NULL);
    1014           5 :                         wpa_s->p2p_send_action_work = NULL;
    1015           5 :                         offchannel_send_action_done(wpa_s);
    1016             :                 }
    1017           5 :                 os_free(awork);
    1018           5 :                 return;
    1019             :         }
    1020             : 
    1021         770 :         if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
    1022         385 :                                    awork->bssid, awork->buf, awork->len,
    1023             :                                    awork->wait_time,
    1024             :                                    wpas_p2p_send_action_tx_status, 1) < 0) {
    1025           0 :                 os_free(awork);
    1026           0 :                 radio_work_done(work);
    1027           0 :                 return;
    1028             :         }
    1029         385 :         wpa_s->p2p_send_action_work = work;
    1030             : }
    1031             : 
    1032             : 
    1033         385 : static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
    1034             :                                  unsigned int freq, const u8 *dst,
    1035             :                                  const u8 *src, const u8 *bssid, const u8 *buf,
    1036             :                                  size_t len, unsigned int wait_time)
    1037             : {
    1038             :         struct send_action_work *awork;
    1039             : 
    1040         385 :         if (wpa_s->p2p_send_action_work) {
    1041           0 :                 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
    1042           0 :                 return -1;
    1043             :         }
    1044             : 
    1045         385 :         awork = os_zalloc(sizeof(*awork) + len);
    1046         385 :         if (awork == NULL)
    1047           0 :                 return -1;
    1048             : 
    1049         385 :         awork->freq = freq;
    1050         385 :         os_memcpy(awork->dst, dst, ETH_ALEN);
    1051         385 :         os_memcpy(awork->src, src, ETH_ALEN);
    1052         385 :         os_memcpy(awork->bssid, bssid, ETH_ALEN);
    1053         385 :         awork->len = len;
    1054         385 :         awork->wait_time = wait_time;
    1055         385 :         os_memcpy(awork->buf, buf, len);
    1056             : 
    1057         385 :         if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
    1058             :                            wpas_send_action_cb, awork) < 0) {
    1059           0 :                 os_free(awork);
    1060           0 :                 return -1;
    1061             :         }
    1062             : 
    1063         385 :         return 0;
    1064             : }
    1065             : 
    1066             : 
    1067         649 : static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
    1068             :                             const u8 *src, const u8 *bssid, const u8 *buf,
    1069             :                             size_t len, unsigned int wait_time)
    1070             : {
    1071         649 :         struct wpa_supplicant *wpa_s = ctx;
    1072         649 :         int listen_freq = -1, send_freq = -1;
    1073             : 
    1074         649 :         if (wpa_s->p2p_listen_work)
    1075         178 :                 listen_freq = wpa_s->p2p_listen_work->freq;
    1076         649 :         if (wpa_s->p2p_send_action_work)
    1077          86 :                 send_freq = wpa_s->p2p_send_action_work->freq;
    1078         649 :         if (listen_freq != (int) freq && send_freq != (int) freq) {
    1079         385 :                 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
    1080             :                            listen_freq, send_freq);
    1081         385 :                 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
    1082             :                                              len, wait_time);
    1083             :         }
    1084             : 
    1085         264 :         wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
    1086         264 :         return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
    1087             :                                       wait_time,
    1088             :                                       wpas_p2p_send_action_tx_status, 1);
    1089             : }
    1090             : 
    1091             : 
    1092         364 : static void wpas_send_action_done(void *ctx)
    1093             : {
    1094         364 :         struct wpa_supplicant *wpa_s = ctx;
    1095             : 
    1096         364 :         if (wpa_s->p2p_send_action_work) {
    1097         250 :                 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
    1098             :                                      wpa_s, NULL);
    1099         250 :                 os_free(wpa_s->p2p_send_action_work->ctx);
    1100         250 :                 radio_work_done(wpa_s->p2p_send_action_work);
    1101         250 :                 wpa_s->p2p_send_action_work = NULL;
    1102             :         }
    1103             : 
    1104         364 :         offchannel_send_action_done(wpa_s);
    1105         364 : }
    1106             : 
    1107             : 
    1108         223 : static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
    1109             :                                     struct p2p_go_neg_results *params)
    1110             : {
    1111         223 :         if (wpa_s->go_params == NULL) {
    1112         223 :                 wpa_s->go_params = os_malloc(sizeof(*params));
    1113         223 :                 if (wpa_s->go_params == NULL)
    1114           0 :                         return -1;
    1115             :         }
    1116         223 :         os_memcpy(wpa_s->go_params, params, sizeof(*params));
    1117         223 :         return 0;
    1118             : }
    1119             : 
    1120             : 
    1121         105 : static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
    1122             :                                     struct p2p_go_neg_results *res)
    1123             : {
    1124        1260 :         wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
    1125             :                    " dev_addr " MACSTR " wps_method %d",
    1126         630 :                    MAC2STR(res->peer_interface_addr),
    1127         630 :                    MAC2STR(res->peer_device_addr), res->wps_method);
    1128         210 :         wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
    1129         105 :                           res->ssid, res->ssid_len);
    1130         105 :         wpa_supplicant_ap_deinit(wpa_s);
    1131         105 :         wpas_copy_go_neg_results(wpa_s, res);
    1132         105 :         if (res->wps_method == WPS_PBC) {
    1133          10 :                 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
    1134             : #ifdef CONFIG_WPS_NFC
    1135          95 :         } else if (res->wps_method == WPS_NFC) {
    1136          48 :                 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
    1137          14 :                                    res->peer_interface_addr,
    1138          14 :                                    wpa_s->parent->p2p_oob_dev_pw,
    1139          14 :                                    wpa_s->parent->p2p_oob_dev_pw_id, 1,
    1140          14 :                                    wpa_s->parent->p2p_oob_dev_pw_id ==
    1141             :                                    DEV_PW_NFC_CONNECTION_HANDOVER ?
    1142           6 :                                    wpa_s->parent->p2p_peer_oob_pubkey_hash :
    1143             :                                    NULL,
    1144             :                                    NULL, 0, 0);
    1145             : #endif /* CONFIG_WPS_NFC */
    1146             :         } else {
    1147          81 :                 u16 dev_pw_id = DEV_PW_DEFAULT;
    1148          81 :                 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
    1149          43 :                         dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
    1150         162 :                 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
    1151          81 :                                    wpa_s->p2p_pin, 1, dev_pw_id);
    1152             :         }
    1153         105 : }
    1154             : 
    1155             : 
    1156          18 : static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
    1157             :                                   struct wpa_ssid *ssid)
    1158             : {
    1159             :         struct wpa_ssid *persistent;
    1160             :         struct psk_list_entry *psk;
    1161             :         struct hostapd_data *hapd;
    1162             : 
    1163          18 :         if (!wpa_s->ap_iface)
    1164           0 :                 return;
    1165             : 
    1166          18 :         persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
    1167             :                                              ssid->ssid_len);
    1168          18 :         if (persistent == NULL)
    1169           0 :                 return;
    1170             : 
    1171          18 :         hapd = wpa_s->ap_iface->bss[0];
    1172             : 
    1173          18 :         dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
    1174             :                          list) {
    1175             :                 struct hostapd_wpa_psk *hpsk;
    1176             : 
    1177           0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
    1178             :                         MACSTR " psk=%d",
    1179             :                         MAC2STR(psk->addr), psk->p2p);
    1180           0 :                 hpsk = os_zalloc(sizeof(*hpsk));
    1181           0 :                 if (hpsk == NULL)
    1182           0 :                         break;
    1183           0 :                 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
    1184           0 :                 if (psk->p2p)
    1185           0 :                         os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
    1186             :                 else
    1187           0 :                         os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
    1188           0 :                 hpsk->next = hapd->conf->ssid.wpa_psk;
    1189           0 :                 hapd->conf->ssid.wpa_psk = hpsk;
    1190             :         }
    1191             : }
    1192             : 
    1193             : 
    1194         118 : static void p2p_go_configured(void *ctx, void *data)
    1195             : {
    1196         118 :         struct wpa_supplicant *wpa_s = ctx;
    1197         118 :         struct p2p_go_neg_results *params = data;
    1198             :         struct wpa_ssid *ssid;
    1199         118 :         int network_id = -1;
    1200             : 
    1201         118 :         ssid = wpa_s->current_ssid;
    1202         118 :         if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
    1203          52 :                 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
    1204          52 :                 if (wpa_s->global->p2p_group_formation == wpa_s)
    1205           7 :                         wpa_s->global->p2p_group_formation = NULL;
    1206          52 :                 if (os_strlen(params->passphrase) > 0) {
    1207         520 :                         wpa_msg_global(wpa_s->parent, MSG_INFO,
    1208             :                                        P2P_EVENT_GROUP_STARTED
    1209             :                                        "%s GO ssid=\"%s\" freq=%d "
    1210             :                                        "passphrase=\"%s\" go_dev_addr=" MACSTR
    1211          52 :                                        "%s", wpa_s->ifname,
    1212          52 :                                        wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
    1213          52 :                                        ssid->frequency, params->passphrase,
    1214         312 :                                        MAC2STR(wpa_s->global->p2p_dev_addr),
    1215          52 :                                        params->persistent_group ?
    1216             :                                        " [PERSISTENT]" : "");
    1217             :                 } else {
    1218             :                         char psk[65];
    1219           0 :                         wpa_snprintf_hex(psk, sizeof(psk), params->psk,
    1220             :                                          sizeof(params->psk));
    1221           0 :                         wpa_msg_global(wpa_s->parent, MSG_INFO,
    1222             :                                        P2P_EVENT_GROUP_STARTED
    1223             :                                        "%s GO ssid=\"%s\" freq=%d psk=%s "
    1224             :                                        "go_dev_addr=" MACSTR "%s",
    1225           0 :                                        wpa_s->ifname,
    1226           0 :                                        wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
    1227             :                                        ssid->frequency, psk,
    1228           0 :                                        MAC2STR(wpa_s->global->p2p_dev_addr),
    1229           0 :                                        params->persistent_group ?
    1230             :                                        " [PERSISTENT]" : "");
    1231             :                 }
    1232             : 
    1233          52 :                 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
    1234          52 :                 if (params->persistent_group) {
    1235          18 :                         network_id = wpas_p2p_store_persistent_group(
    1236             :                                 wpa_s->parent, ssid,
    1237          18 :                                 wpa_s->global->p2p_dev_addr);
    1238          18 :                         wpas_p2p_add_psk_list(wpa_s, ssid);
    1239             :                 }
    1240          52 :                 if (network_id < 0)
    1241          34 :                         network_id = ssid->id;
    1242          52 :                 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
    1243          52 :                 wpas_p2p_cross_connect_setup(wpa_s);
    1244          52 :                 wpas_p2p_set_group_idle_timeout(wpa_s);
    1245             : 
    1246          52 :                 if (wpa_s->p2p_first_connection_timeout) {
    1247          15 :                         wpa_dbg(wpa_s, MSG_DEBUG,
    1248             :                                 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
    1249             :                                 wpa_s->p2p_first_connection_timeout);
    1250          15 :                         wpa_s->p2p_go_group_formation_completed = 0;
    1251          15 :                         wpa_s->global->p2p_group_formation = wpa_s;
    1252          15 :                         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    1253          15 :                                              wpa_s->parent, NULL);
    1254          15 :                         eloop_register_timeout(
    1255          15 :                                 wpa_s->p2p_first_connection_timeout, 0,
    1256             :                                 wpas_p2p_group_formation_timeout,
    1257          15 :                                 wpa_s->parent, NULL);
    1258             :                 }
    1259             : 
    1260          52 :                 return;
    1261             :         }
    1262             : 
    1263          66 :         wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
    1264          66 :         if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
    1265          66 :                                               params->peer_interface_addr)) {
    1266           0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
    1267             :                            "filtering");
    1268           0 :                 return;
    1269             :         }
    1270          66 :         if (params->wps_method == WPS_PBC) {
    1271           8 :                 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
    1272           8 :                                           params->peer_device_addr);
    1273             : #ifdef CONFIG_WPS_NFC
    1274          58 :         } else if (params->wps_method == WPS_NFC) {
    1275           8 :                 if (wpa_s->parent->p2p_oob_dev_pw_id !=
    1276           4 :                     DEV_PW_NFC_CONNECTION_HANDOVER &&
    1277           4 :                     !wpa_s->parent->p2p_oob_dev_pw) {
    1278           0 :                         wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
    1279           0 :                         return;
    1280             :                 }
    1281          22 :                 wpas_ap_wps_add_nfc_pw(
    1282           8 :                         wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
    1283           8 :                         wpa_s->parent->p2p_oob_dev_pw,
    1284           8 :                         wpa_s->parent->p2p_peer_oob_pk_hash_known ?
    1285           6 :                         wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
    1286             : #endif /* CONFIG_WPS_NFC */
    1287          50 :         } else if (wpa_s->p2p_pin[0])
    1288          50 :                 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
    1289          50 :                                           wpa_s->p2p_pin, NULL, 0, 0);
    1290          66 :         os_free(wpa_s->go_params);
    1291          66 :         wpa_s->go_params = NULL;
    1292             : }
    1293             : 
    1294             : 
    1295         118 : static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
    1296             :                               struct p2p_go_neg_results *params,
    1297             :                               int group_formation)
    1298             : {
    1299             :         struct wpa_ssid *ssid;
    1300             : 
    1301         118 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
    1302         118 :         if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
    1303           0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
    1304             :                         "results");
    1305           0 :                 return;
    1306             :         }
    1307             : 
    1308         118 :         ssid = wpa_config_add_network(wpa_s->conf);
    1309         118 :         if (ssid == NULL) {
    1310           0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
    1311           0 :                 return;
    1312             :         }
    1313             : 
    1314         118 :         wpa_s->show_group_started = 0;
    1315             : 
    1316         118 :         wpa_config_set_network_defaults(ssid);
    1317         118 :         ssid->temporary = 1;
    1318         118 :         ssid->p2p_group = 1;
    1319         118 :         ssid->p2p_persistent_group = params->persistent_group;
    1320         118 :         ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
    1321             :                 WPAS_MODE_P2P_GO;
    1322         118 :         ssid->frequency = params->freq;
    1323         118 :         ssid->ht40 = params->ht40;
    1324         118 :         ssid->vht = params->vht;
    1325         118 :         ssid->ssid = os_zalloc(params->ssid_len + 1);
    1326         118 :         if (ssid->ssid) {
    1327         118 :                 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
    1328         118 :                 ssid->ssid_len = params->ssid_len;
    1329             :         }
    1330         118 :         ssid->auth_alg = WPA_AUTH_ALG_OPEN;
    1331         118 :         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
    1332         118 :         ssid->proto = WPA_PROTO_RSN;
    1333         118 :         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
    1334         118 :         if (os_strlen(params->passphrase) > 0) {
    1335         118 :                 ssid->passphrase = os_strdup(params->passphrase);
    1336         118 :                 if (ssid->passphrase == NULL) {
    1337           0 :                         wpa_msg_global(wpa_s, MSG_ERROR,
    1338             :                                        "P2P: Failed to copy passphrase for GO");
    1339           0 :                         wpa_config_remove_network(wpa_s->conf, ssid->id);
    1340           0 :                         return;
    1341             :                 }
    1342             :         } else
    1343           0 :                 ssid->passphrase = NULL;
    1344         118 :         ssid->psk_set = params->psk_set;
    1345         118 :         if (ssid->psk_set)
    1346          17 :                 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
    1347         101 :         else if (ssid->passphrase)
    1348         101 :                 wpa_config_update_psk(ssid);
    1349         118 :         ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
    1350             : 
    1351         118 :         wpa_s->ap_configured_cb = p2p_go_configured;
    1352         118 :         wpa_s->ap_configured_cb_ctx = wpa_s;
    1353         118 :         wpa_s->ap_configured_cb_data = wpa_s->go_params;
    1354         118 :         wpa_s->scan_req = NORMAL_SCAN_REQ;
    1355         118 :         wpa_s->connect_without_scan = ssid;
    1356         118 :         wpa_s->reassociate = 1;
    1357         118 :         wpa_s->disconnected = 0;
    1358         118 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
    1359             :                 "start GO)");
    1360         118 :         wpa_supplicant_req_scan(wpa_s, 0, 0);
    1361             : }
    1362             : 
    1363             : 
    1364          35 : static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
    1365             :                                   const struct wpa_supplicant *src)
    1366             : {
    1367             :         struct wpa_config *d;
    1368             :         const struct wpa_config *s;
    1369             : 
    1370          35 :         d = dst->conf;
    1371          35 :         s = src->conf;
    1372             : 
    1373             : #define C(n) if (s->n) d->n = os_strdup(s->n)
    1374          35 :         C(device_name);
    1375          35 :         C(manufacturer);
    1376          35 :         C(model_name);
    1377          35 :         C(model_number);
    1378          35 :         C(serial_number);
    1379          35 :         C(config_methods);
    1380             : #undef C
    1381             : 
    1382          35 :         os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
    1383          35 :         os_memcpy(d->sec_device_type, s->sec_device_type,
    1384             :                   sizeof(d->sec_device_type));
    1385          35 :         d->num_sec_device_types = s->num_sec_device_types;
    1386             : 
    1387          35 :         d->p2p_group_idle = s->p2p_group_idle;
    1388          35 :         d->p2p_intra_bss = s->p2p_intra_bss;
    1389          35 :         d->persistent_reconnect = s->persistent_reconnect;
    1390          35 :         d->max_num_sta = s->max_num_sta;
    1391          35 :         d->pbc_in_m1 = s->pbc_in_m1;
    1392          35 :         d->ignore_old_scan_res = s->ignore_old_scan_res;
    1393          35 :         d->beacon_int = s->beacon_int;
    1394          35 :         d->dtim_period = s->dtim_period;
    1395          35 :         d->disassoc_low_ack = s->disassoc_low_ack;
    1396          35 :         d->disable_scan_offload = s->disable_scan_offload;
    1397             : 
    1398          35 :         if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
    1399          28 :                 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
    1400          28 :                 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
    1401             :         }
    1402          35 : }
    1403             : 
    1404             : 
    1405          38 : static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
    1406             :                                       char *ifname, size_t len)
    1407             : {
    1408          38 :         char *ifname_ptr = wpa_s->ifname;
    1409             : 
    1410          38 :         if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
    1411             :                        os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
    1412           0 :                 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
    1413             :         }
    1414             : 
    1415          38 :         os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
    1416          38 :         if (os_strlen(ifname) >= IFNAMSIZ &&
    1417           0 :             os_strlen(wpa_s->ifname) < IFNAMSIZ) {
    1418             :                 /* Try to avoid going over the IFNAMSIZ length limit */
    1419           0 :                 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
    1420             :         }
    1421          38 : }
    1422             : 
    1423             : 
    1424          40 : static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
    1425             :                                         enum wpa_driver_if_type type)
    1426             : {
    1427             :         char ifname[120], force_ifname[120];
    1428             : 
    1429          40 :         if (wpa_s->pending_interface_name[0]) {
    1430           2 :                 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
    1431             :                            "- skip creation of a new one");
    1432           2 :                 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
    1433           0 :                         wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
    1434             :                                    "unknown?! ifname='%s'",
    1435           0 :                                    wpa_s->pending_interface_name);
    1436           0 :                         return -1;
    1437             :                 }
    1438           2 :                 return 0;
    1439             :         }
    1440             : 
    1441          38 :         wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
    1442          38 :         force_ifname[0] = '\0';
    1443             : 
    1444          38 :         wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
    1445             :                    ifname);
    1446          38 :         wpa_s->p2p_group_idx++;
    1447             : 
    1448          38 :         wpa_s->pending_interface_type = type;
    1449          38 :         if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
    1450          38 :                            wpa_s->pending_interface_addr, NULL) < 0) {
    1451           0 :                 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
    1452             :                            "interface");
    1453           0 :                 return -1;
    1454             :         }
    1455             : 
    1456          38 :         if (force_ifname[0]) {
    1457           0 :                 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
    1458             :                            force_ifname);
    1459           0 :                 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
    1460             :                            sizeof(wpa_s->pending_interface_name));
    1461             :         } else
    1462          38 :                 os_strlcpy(wpa_s->pending_interface_name, ifname,
    1463             :                            sizeof(wpa_s->pending_interface_name));
    1464         266 :         wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
    1465          38 :                    MACSTR, wpa_s->pending_interface_name,
    1466         228 :                    MAC2STR(wpa_s->pending_interface_addr));
    1467             : 
    1468          38 :         return 0;
    1469             : }
    1470             : 
    1471             : 
    1472        4014 : static void wpas_p2p_remove_pending_group_interface(
    1473             :         struct wpa_supplicant *wpa_s)
    1474             : {
    1475        4017 :         if (!wpa_s->pending_interface_name[0] ||
    1476           3 :             is_zero_ether_addr(wpa_s->pending_interface_addr))
    1477        8025 :                 return; /* No pending virtual interface */
    1478             : 
    1479           3 :         wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
    1480           3 :                    wpa_s->pending_interface_name);
    1481           3 :         wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
    1482           3 :                           wpa_s->pending_interface_name);
    1483           3 :         os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
    1484           3 :         wpa_s->pending_interface_name[0] = '\0';
    1485             : }
    1486             : 
    1487             : 
    1488             : static struct wpa_supplicant *
    1489          35 : wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
    1490             : {
    1491             :         struct wpa_interface iface;
    1492             :         struct wpa_supplicant *group_wpa_s;
    1493             : 
    1494          35 :         if (!wpa_s->pending_interface_name[0]) {
    1495           0 :                 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
    1496           0 :                 if (!wpas_p2p_create_iface(wpa_s))
    1497           0 :                         return NULL;
    1498             :                 /*
    1499             :                  * Something has forced us to remove the pending interface; try
    1500             :                  * to create a new one and hope for the best that we will get
    1501             :                  * the same local address.
    1502             :                  */
    1503           0 :                 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
    1504             :                                                  WPA_IF_P2P_CLIENT) < 0)
    1505           0 :                         return NULL;
    1506             :         }
    1507             : 
    1508          35 :         os_memset(&iface, 0, sizeof(iface));
    1509          35 :         iface.ifname = wpa_s->pending_interface_name;
    1510          35 :         iface.driver = wpa_s->driver->name;
    1511          35 :         if (wpa_s->conf->ctrl_interface == NULL &&
    1512           0 :             wpa_s->parent != wpa_s &&
    1513           0 :             wpa_s->p2p_mgmt &&
    1514           0 :             (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
    1515           0 :                 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
    1516             :         else
    1517          35 :                 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
    1518          35 :         iface.driver_param = wpa_s->conf->driver_param;
    1519          35 :         group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
    1520          35 :         if (group_wpa_s == NULL) {
    1521           0 :                 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
    1522             :                            "wpa_supplicant interface");
    1523           0 :                 return NULL;
    1524             :         }
    1525          35 :         wpa_s->pending_interface_name[0] = '\0';
    1526          35 :         group_wpa_s->parent = wpa_s;
    1527          35 :         group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
    1528             :                 P2P_GROUP_INTERFACE_CLIENT;
    1529          35 :         wpa_s->global->p2p_group_formation = group_wpa_s;
    1530             : 
    1531          35 :         wpas_p2p_clone_config(group_wpa_s, wpa_s);
    1532             : 
    1533          35 :         return group_wpa_s;
    1534             : }
    1535             : 
    1536             : 
    1537           3 : static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
    1538             :                                              void *timeout_ctx)
    1539             : {
    1540           3 :         struct wpa_supplicant *wpa_s = eloop_ctx;
    1541           3 :         wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
    1542           3 :         wpas_p2p_group_formation_failed(wpa_s);
    1543           3 : }
    1544             : 
    1545             : 
    1546           7 : void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
    1547             : {
    1548           7 :         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    1549           7 :                              wpa_s->parent, NULL);
    1550           7 :         if (wpa_s->global->p2p)
    1551           7 :                 p2p_group_formation_failed(wpa_s->global->p2p);
    1552           7 :         wpas_group_formation_completed(wpa_s, 0);
    1553           7 : }
    1554             : 
    1555             : 
    1556           3 : static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
    1557             : {
    1558           3 :         wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
    1559           3 :         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    1560           3 :                              wpa_s->parent, NULL);
    1561           3 :         eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
    1562           3 :                                wpa_s->parent, NULL);
    1563           3 :         wpa_s->global->p2p_fail_on_wps_complete = 0;
    1564           3 : }
    1565             : 
    1566             : 
    1567           0 : void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
    1568             : {
    1569           0 :         if (wpa_s->global->p2p_group_formation != wpa_s)
    1570           0 :                 return;
    1571             :         /* Speed up group formation timeout since this cannot succeed */
    1572           0 :         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    1573           0 :                              wpa_s->parent, NULL);
    1574           0 :         eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
    1575           0 :                                wpa_s->parent, NULL);
    1576             : }
    1577             : 
    1578             : 
    1579         153 : static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
    1580             : {
    1581         153 :         struct wpa_supplicant *wpa_s = ctx;
    1582             : 
    1583         153 :         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
    1584           1 :                 wpa_drv_cancel_remain_on_channel(wpa_s);
    1585           1 :                 wpa_s->off_channel_freq = 0;
    1586           1 :                 wpa_s->roc_waiting_drv_freq = 0;
    1587             :         }
    1588             : 
    1589         153 :         if (res->status) {
    1590          21 :                 wpa_msg_global(wpa_s, MSG_INFO,
    1591             :                                P2P_EVENT_GO_NEG_FAILURE "status=%d",
    1592             :                                res->status);
    1593          21 :                 wpas_notify_p2p_go_neg_completed(wpa_s, res);
    1594          21 :                 wpas_p2p_remove_pending_group_interface(wpa_s);
    1595          21 :                 return;
    1596             :         }
    1597             : 
    1598         132 :         if (wpa_s->p2p_go_ht40)
    1599          16 :                 res->ht40 = 1;
    1600         132 :         if (wpa_s->p2p_go_vht)
    1601          16 :                 res->vht = 1;
    1602             : 
    1603        1848 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
    1604             :                        "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
    1605             :                        " wps_method=%s",
    1606         132 :                        res->role_go ? "GO" : "client", res->freq, res->ht40,
    1607         792 :                        MAC2STR(res->peer_device_addr),
    1608         792 :                        MAC2STR(res->peer_interface_addr),
    1609             :                        p2p_wps_method_text(res->wps_method));
    1610         132 :         wpas_notify_p2p_go_neg_completed(wpa_s, res);
    1611             : 
    1612         132 :         if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
    1613             :                 struct wpa_ssid *ssid;
    1614           1 :                 ssid = wpa_config_get_network(wpa_s->conf,
    1615             :                                               wpa_s->p2p_persistent_id);
    1616           2 :                 if (ssid && ssid->disabled == 2 &&
    1617           2 :                     ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
    1618           1 :                         size_t len = os_strlen(ssid->passphrase);
    1619           1 :                         wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
    1620             :                                    "on requested persistent group");
    1621           1 :                         os_memcpy(res->passphrase, ssid->passphrase, len);
    1622           1 :                         res->passphrase[len] = '\0';
    1623             :                 }
    1624             :         }
    1625             : 
    1626         132 :         if (wpa_s->create_p2p_iface) {
    1627          22 :                 struct wpa_supplicant *group_wpa_s =
    1628          22 :                         wpas_p2p_init_group_interface(wpa_s, res->role_go);
    1629          22 :                 if (group_wpa_s == NULL) {
    1630           0 :                         wpas_p2p_remove_pending_group_interface(wpa_s);
    1631           0 :                         eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
    1632             :                                              wpa_s, NULL);
    1633           0 :                         wpas_p2p_group_formation_failed(wpa_s);
    1634           0 :                         return;
    1635             :                 }
    1636          22 :                 if (group_wpa_s != wpa_s) {
    1637          22 :                         os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
    1638             :                                   sizeof(group_wpa_s->p2p_pin));
    1639          22 :                         group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
    1640             :                 }
    1641          22 :                 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
    1642          22 :                 wpa_s->pending_interface_name[0] = '\0';
    1643          22 :                 group_wpa_s->p2p_in_provisioning = 1;
    1644             : 
    1645          22 :                 if (res->role_go)
    1646           9 :                         wpas_start_wps_go(group_wpa_s, res, 1);
    1647             :                 else
    1648          13 :                         wpas_start_wps_enrollee(group_wpa_s, res);
    1649             :         } else {
    1650         110 :                 wpa_s->p2p_in_provisioning = 1;
    1651         110 :                 wpa_s->global->p2p_group_formation = wpa_s;
    1652             : 
    1653         110 :                 if (res->role_go)
    1654          57 :                         wpas_start_wps_go(wpa_s, res, 1);
    1655             :                 else
    1656          53 :                         wpas_start_wps_enrollee(ctx, res);
    1657             :         }
    1658             : 
    1659         132 :         wpa_s->p2p_long_listen = 0;
    1660         132 :         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
    1661             : 
    1662         132 :         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
    1663         132 :         eloop_register_timeout(15 + res->peer_config_timeout / 100,
    1664         132 :                                (res->peer_config_timeout % 100) * 10000,
    1665             :                                wpas_p2p_group_formation_timeout, wpa_s, NULL);
    1666             : }
    1667             : 
    1668             : 
    1669          15 : static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
    1670             : {
    1671          15 :         struct wpa_supplicant *wpa_s = ctx;
    1672         105 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
    1673          90 :                        " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
    1674             : 
    1675          15 :         wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
    1676          15 : }
    1677             : 
    1678             : 
    1679         313 : static void wpas_dev_found(void *ctx, const u8 *addr,
    1680             :                            const struct p2p_peer_info *info,
    1681             :                            int new_device)
    1682             : {
    1683             : #ifndef CONFIG_NO_STDOUT_DEBUG
    1684         313 :         struct wpa_supplicant *wpa_s = ctx;
    1685             :         char devtype[WPS_DEV_TYPE_BUFSIZE];
    1686         313 :         char *wfd_dev_info_hex = NULL;
    1687             : 
    1688             : #ifdef CONFIG_WIFI_DISPLAY
    1689         313 :         wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
    1690             :                                                     WFD_SUBELEM_DEVICE_INFO);
    1691             : #endif /* CONFIG_WIFI_DISPLAY */
    1692             : 
    1693        5321 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
    1694             :                        " p2p_dev_addr=" MACSTR
    1695             :                        " pri_dev_type=%s name='%s' config_methods=0x%x "
    1696             :                        "dev_capab=0x%x group_capab=0x%x%s%s",
    1697        3756 :                        MAC2STR(addr), MAC2STR(info->p2p_device_addr),
    1698         313 :                        wps_dev_type_bin2str(info->pri_dev_type, devtype,
    1699             :                                             sizeof(devtype)),
    1700         626 :                        info->device_name, info->config_methods,
    1701         626 :                        info->dev_capab, info->group_capab,
    1702             :                        wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
    1703             :                        wfd_dev_info_hex ? wfd_dev_info_hex : "");
    1704             : 
    1705         313 :         os_free(wfd_dev_info_hex);
    1706             : #endif /* CONFIG_NO_STDOUT_DEBUG */
    1707             : 
    1708         313 :         wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
    1709         313 : }
    1710             : 
    1711             : 
    1712         308 : static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
    1713             : {
    1714         308 :         struct wpa_supplicant *wpa_s = ctx;
    1715             : 
    1716        1848 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
    1717        1848 :                        "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
    1718             : 
    1719         308 :         wpas_notify_p2p_device_lost(wpa_s, dev_addr);
    1720         308 : }
    1721             : 
    1722             : 
    1723         198 : static void wpas_find_stopped(void *ctx)
    1724             : {
    1725         198 :         struct wpa_supplicant *wpa_s = ctx;
    1726         198 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
    1727         198 : }
    1728             : 
    1729             : 
    1730             : struct wpas_p2p_listen_work {
    1731             :         unsigned int freq;
    1732             :         unsigned int duration;
    1733             :         struct wpabuf *probe_resp_ie;
    1734             : };
    1735             : 
    1736             : 
    1737        1094 : static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
    1738             : {
    1739        1094 :         if (lwork == NULL)
    1740        1094 :                 return;
    1741        1094 :         wpabuf_free(lwork->probe_resp_ie);
    1742        1094 :         os_free(lwork);
    1743             : }
    1744             : 
    1745             : 
    1746        8013 : static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
    1747             : {
    1748             :         struct wpas_p2p_listen_work *lwork;
    1749             : 
    1750        8013 :         if (!wpa_s->p2p_listen_work)
    1751       14932 :                 return;
    1752             : 
    1753        1094 :         lwork = wpa_s->p2p_listen_work->ctx;
    1754        1094 :         wpas_p2p_listen_work_free(lwork);
    1755        1094 :         radio_work_done(wpa_s->p2p_listen_work);
    1756        1094 :         wpa_s->p2p_listen_work = NULL;
    1757             : }
    1758             : 
    1759             : 
    1760        1094 : static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
    1761             : {
    1762        1094 :         struct wpa_supplicant *wpa_s = work->wpa_s;
    1763        1094 :         struct wpas_p2p_listen_work *lwork = work->ctx;
    1764             : 
    1765        1094 :         if (deinit) {
    1766           0 :                 if (work->started) {
    1767           0 :                         wpa_s->p2p_listen_work = NULL;
    1768           0 :                         wpas_stop_listen(wpa_s);
    1769             :                 }
    1770           0 :                 wpas_p2p_listen_work_free(lwork);
    1771           0 :                 return;
    1772             :         }
    1773             : 
    1774        1094 :         wpa_s->p2p_listen_work = work;
    1775             : 
    1776        1094 :         wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
    1777             : 
    1778        1094 :         if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
    1779           1 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
    1780             :                            "report received Probe Request frames");
    1781           1 :                 wpas_p2p_listen_work_done(wpa_s);
    1782           1 :                 return;
    1783             :         }
    1784             : 
    1785        1093 :         wpa_s->pending_listen_freq = lwork->freq;
    1786        1093 :         wpa_s->pending_listen_duration = lwork->duration;
    1787             : 
    1788        1093 :         if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
    1789             :         {
    1790           0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
    1791             :                            "to remain on channel (%u MHz) for Listen "
    1792             :                            "state", lwork->freq);
    1793           0 :                 wpas_p2p_listen_work_done(wpa_s);
    1794           0 :                 wpa_s->pending_listen_freq = 0;
    1795           0 :                 return;
    1796             :         }
    1797        1093 :         wpa_s->off_channel_freq = 0;
    1798        1093 :         wpa_s->roc_waiting_drv_freq = lwork->freq;
    1799             : }
    1800             : 
    1801             : 
    1802        1094 : static int wpas_start_listen(void *ctx, unsigned int freq,
    1803             :                              unsigned int duration,
    1804             :                              const struct wpabuf *probe_resp_ie)
    1805             : {
    1806        1094 :         struct wpa_supplicant *wpa_s = ctx;
    1807             :         struct wpas_p2p_listen_work *lwork;
    1808             : 
    1809        1094 :         if (wpa_s->p2p_listen_work) {
    1810           0 :                 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
    1811           0 :                 return -1;
    1812             :         }
    1813             : 
    1814        1094 :         lwork = os_zalloc(sizeof(*lwork));
    1815        1094 :         if (lwork == NULL)
    1816           0 :                 return -1;
    1817        1094 :         lwork->freq = freq;
    1818        1094 :         lwork->duration = duration;
    1819        1094 :         if (probe_resp_ie) {
    1820        1094 :                 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
    1821        1094 :                 if (lwork->probe_resp_ie == NULL) {
    1822           0 :                         wpas_p2p_listen_work_free(lwork);
    1823           0 :                         return -1;
    1824             :                 }
    1825             :         }
    1826             : 
    1827        1094 :         if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
    1828             :                            lwork) < 0) {
    1829           0 :                 wpas_p2p_listen_work_free(lwork);
    1830           0 :                 return -1;
    1831             :         }
    1832             : 
    1833        1094 :         return 0;
    1834             : }
    1835             : 
    1836             : 
    1837        6870 : static void wpas_stop_listen(void *ctx)
    1838             : {
    1839        6870 :         struct wpa_supplicant *wpa_s = ctx;
    1840        6870 :         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
    1841         254 :                 wpa_drv_cancel_remain_on_channel(wpa_s);
    1842         254 :                 wpa_s->off_channel_freq = 0;
    1843         254 :                 wpa_s->roc_waiting_drv_freq = 0;
    1844             :         }
    1845        6870 :         wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
    1846        6870 :         wpa_drv_probe_req_report(wpa_s, 0);
    1847        6870 :         wpas_p2p_listen_work_done(wpa_s);
    1848        6870 : }
    1849             : 
    1850             : 
    1851         445 : static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
    1852             : {
    1853         445 :         struct wpa_supplicant *wpa_s = ctx;
    1854         445 :         return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
    1855             : }
    1856             : 
    1857             : 
    1858             : /*
    1859             :  * DNS Header section is used only to calculate compression pointers, so the
    1860             :  * contents of this data does not matter, but the length needs to be reserved
    1861             :  * in the virtual packet.
    1862             :  */
    1863             : #define DNS_HEADER_LEN 12
    1864             : 
    1865             : /*
    1866             :  * 27-octet in-memory packet from P2P specification containing two implied
    1867             :  * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
    1868             :  */
    1869             : #define P2P_SD_IN_MEMORY_LEN 27
    1870             : 
    1871           4 : static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
    1872             :                                        u8 **spos, const u8 *end)
    1873             : {
    1874          14 :         while (*spos < end) {
    1875          10 :                 u8 val = ((*spos)[0] & 0xc0) >> 6;
    1876             :                 int len;
    1877             : 
    1878          10 :                 if (val == 1 || val == 2) {
    1879             :                         /* These are reserved values in RFC 1035 */
    1880           0 :                         wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
    1881             :                                    "sequence starting with 0x%x", val);
    1882           0 :                         return -1;
    1883             :                 }
    1884             : 
    1885          10 :                 if (val == 3) {
    1886             :                         u16 offset;
    1887             :                         u8 *spos_tmp;
    1888             : 
    1889             :                         /* Offset */
    1890           2 :                         if (*spos + 2 > end) {
    1891           0 :                                 wpa_printf(MSG_DEBUG, "P2P: No room for full "
    1892             :                                            "DNS offset field");
    1893           0 :                                 return -1;
    1894             :                         }
    1895             : 
    1896           2 :                         offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
    1897           2 :                         if (offset >= *spos - start) {
    1898           0 :                                 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
    1899             :                                            "pointer offset %u", offset);
    1900           0 :                                 return -1;
    1901             :                         }
    1902             : 
    1903           2 :                         (*spos) += 2;
    1904           2 :                         spos_tmp = start + offset;
    1905           2 :                         return p2p_sd_dns_uncompress_label(upos, uend, start,
    1906             :                                                            &spos_tmp,
    1907           2 :                                                            *spos - 2);
    1908             :                 }
    1909             : 
    1910             :                 /* Label */
    1911           8 :                 len = (*spos)[0] & 0x3f;
    1912           8 :                 if (len == 0)
    1913           2 :                         return 0;
    1914             : 
    1915           6 :                 (*spos)++;
    1916           6 :                 if (*spos + len > end) {
    1917           0 :                         wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
    1918             :                                    "sequence - no room for label with length "
    1919             :                                    "%u", len);
    1920           0 :                         return -1;
    1921             :                 }
    1922             : 
    1923           6 :                 if (*upos + len + 2 > uend)
    1924           0 :                         return -2;
    1925             : 
    1926           6 :                 os_memcpy(*upos, *spos, len);
    1927           6 :                 *spos += len;
    1928           6 :                 *upos += len;
    1929           6 :                 (*upos)[0] = '.';
    1930           6 :                 (*upos)++;
    1931           6 :                 (*upos)[0] = '\0';
    1932             :         }
    1933             : 
    1934           0 :         return 0;
    1935             : }
    1936             : 
    1937             : 
    1938             : /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
    1939             :  * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
    1940             :  * not large enough */
    1941           2 : static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
    1942             :                                  size_t msg_len, size_t offset)
    1943             : {
    1944             :         /* 27-octet in-memory packet from P2P specification */
    1945           2 :         const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
    1946             :                 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
    1947             :         u8 *tmp, *end, *spos;
    1948             :         char *upos, *uend;
    1949           2 :         int ret = 0;
    1950             : 
    1951           2 :         if (buf_len < 2)
    1952           0 :                 return -1;
    1953           2 :         if (offset > msg_len)
    1954           0 :                 return -1;
    1955             : 
    1956           2 :         tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
    1957           2 :         if (tmp == NULL)
    1958           0 :                 return -1;
    1959           2 :         spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
    1960           2 :         end = spos + msg_len;
    1961           2 :         spos += offset;
    1962             : 
    1963           2 :         os_memset(tmp, 0, DNS_HEADER_LEN);
    1964           2 :         os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
    1965           2 :         os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
    1966             : 
    1967           2 :         upos = buf;
    1968           2 :         uend = buf + buf_len;
    1969             : 
    1970           2 :         ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
    1971           2 :         if (ret) {
    1972           0 :                 os_free(tmp);
    1973           0 :                 return ret;
    1974             :         }
    1975             : 
    1976           2 :         if (upos == buf) {
    1977           0 :                 upos[0] = '.';
    1978           0 :                 upos[1] = '\0';
    1979           2 :         } else if (upos[-1] == '.')
    1980           2 :                 upos[-1] = '\0';
    1981             : 
    1982           2 :         os_free(tmp);
    1983           2 :         return 0;
    1984             : }
    1985             : 
    1986             : 
    1987             : static struct p2p_srv_bonjour *
    1988          22 : wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
    1989             :                              const struct wpabuf *query)
    1990             : {
    1991             :         struct p2p_srv_bonjour *bsrv;
    1992             :         size_t len;
    1993             : 
    1994          22 :         len = wpabuf_len(query);
    1995          97 :         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
    1996             :                          struct p2p_srv_bonjour, list) {
    1997          97 :                 if (len == wpabuf_len(bsrv->query) &&
    1998          11 :                     os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
    1999             :                               len) == 0)
    2000          11 :                         return bsrv;
    2001             :         }
    2002          11 :         return NULL;
    2003             : }
    2004             : 
    2005             : 
    2006             : static struct p2p_srv_upnp *
    2007         297 : wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
    2008             :                           const char *service)
    2009             : {
    2010             :         struct p2p_srv_upnp *usrv;
    2011             : 
    2012       22435 :         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
    2013             :                          struct p2p_srv_upnp, list) {
    2014       44342 :                 if (version == usrv->version &&
    2015       22171 :                     os_strcmp(service, usrv->service) == 0)
    2016          33 :                         return usrv;
    2017             :         }
    2018         264 :         return NULL;
    2019             : }
    2020             : 
    2021             : 
    2022           4 : static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
    2023             :                                         u8 srv_trans_id)
    2024             : {
    2025             :         u8 *len_pos;
    2026             : 
    2027           4 :         if (wpabuf_tailroom(resp) < 5)
    2028           4 :                 return;
    2029             : 
    2030             :         /* Length (to be filled) */
    2031           4 :         len_pos = wpabuf_put(resp, 2);
    2032           4 :         wpabuf_put_u8(resp, srv_proto);
    2033           4 :         wpabuf_put_u8(resp, srv_trans_id);
    2034             :         /* Status Code */
    2035           4 :         wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
    2036             :         /* Response Data: empty */
    2037           4 :         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
    2038             : }
    2039             : 
    2040             : 
    2041          11 : static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
    2042             :                                 struct wpabuf *resp, u8 srv_trans_id)
    2043             : {
    2044             :         struct p2p_srv_bonjour *bsrv;
    2045             :         u8 *len_pos;
    2046             : 
    2047          11 :         wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
    2048             : 
    2049          11 :         if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
    2050           0 :                 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
    2051           0 :                 return;
    2052             :         }
    2053             : 
    2054          72 :         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
    2055             :                          struct p2p_srv_bonjour, list) {
    2056         122 :                 if (wpabuf_tailroom(resp) <
    2057          61 :                     5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
    2058           0 :                         return;
    2059             :                 /* Length (to be filled) */
    2060          61 :                 len_pos = wpabuf_put(resp, 2);
    2061          61 :                 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
    2062          61 :                 wpabuf_put_u8(resp, srv_trans_id);
    2063             :                 /* Status Code */
    2064          61 :                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
    2065         122 :                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
    2066          61 :                                   wpabuf_head(bsrv->resp),
    2067          61 :                                   wpabuf_len(bsrv->resp));
    2068             :                 /* Response Data */
    2069          61 :                 wpabuf_put_buf(resp, bsrv->query); /* Key */
    2070          61 :                 wpabuf_put_buf(resp, bsrv->resp); /* Value */
    2071          61 :                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
    2072             :                              2);
    2073             :         }
    2074             : }
    2075             : 
    2076             : 
    2077           4 : static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
    2078             :                                size_t query_len)
    2079             : {
    2080             :         char str_rx[256], str_srv[256];
    2081             : 
    2082           4 :         if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
    2083           0 :                 return 0; /* Too short to include DNS Type and Version */
    2084           4 :         if (os_memcmp(query + query_len - 3,
    2085             :                       wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
    2086             :                       3) != 0)
    2087           2 :                 return 0; /* Mismatch in DNS Type or Version */
    2088           3 :         if (query_len == wpabuf_len(bsrv->query) &&
    2089           1 :             os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
    2090           1 :                 return 1; /* Binary match */
    2091             : 
    2092           1 :         if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
    2093             :                                   0))
    2094           0 :                 return 0; /* Failed to uncompress query */
    2095           2 :         if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
    2096           1 :                                   wpabuf_head(bsrv->query),
    2097           1 :                                   wpabuf_len(bsrv->query) - 3, 0))
    2098           0 :                 return 0; /* Failed to uncompress service */
    2099             : 
    2100           1 :         return os_strcmp(str_rx, str_srv) == 0;
    2101             : }
    2102             : 
    2103             : 
    2104           7 : static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
    2105             :                                 struct wpabuf *resp, u8 srv_trans_id,
    2106             :                                 const u8 *query, size_t query_len)
    2107             : {
    2108             :         struct p2p_srv_bonjour *bsrv;
    2109             :         u8 *len_pos;
    2110           7 :         int matches = 0;
    2111             : 
    2112           7 :         wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
    2113             :                           query, query_len);
    2114           7 :         if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
    2115           1 :                 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
    2116           1 :                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
    2117             :                                             srv_trans_id);
    2118           1 :                 return;
    2119             :         }
    2120             : 
    2121           6 :         if (query_len == 0) {
    2122           5 :                 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
    2123           5 :                 return;
    2124             :         }
    2125             : 
    2126           5 :         dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
    2127             :                          struct p2p_srv_bonjour, list) {
    2128           4 :                 if (!match_bonjour_query(bsrv, query, query_len))
    2129           3 :                         continue;
    2130             : 
    2131           2 :                 if (wpabuf_tailroom(resp) <
    2132           1 :                     5 + query_len + wpabuf_len(bsrv->resp))
    2133           0 :                         return;
    2134             : 
    2135           1 :                 matches++;
    2136             : 
    2137             :                 /* Length (to be filled) */
    2138           1 :                 len_pos = wpabuf_put(resp, 2);
    2139           1 :                 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
    2140           1 :                 wpabuf_put_u8(resp, srv_trans_id);
    2141             : 
    2142             :                 /* Status Code */
    2143           1 :                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
    2144           2 :                 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
    2145           1 :                                   wpabuf_head(bsrv->resp),
    2146           1 :                                   wpabuf_len(bsrv->resp));
    2147             : 
    2148             :                 /* Response Data */
    2149           1 :                 wpabuf_put_data(resp, query, query_len); /* Key */
    2150           1 :                 wpabuf_put_buf(resp, bsrv->resp); /* Value */
    2151             : 
    2152           1 :                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
    2153             :         }
    2154             : 
    2155           1 :         if (matches == 0) {
    2156           0 :                 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
    2157             :                            "available");
    2158           0 :                 if (wpabuf_tailroom(resp) < 5)
    2159           0 :                         return;
    2160             : 
    2161             :                 /* Length (to be filled) */
    2162           0 :                 len_pos = wpabuf_put(resp, 2);
    2163           0 :                 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
    2164           0 :                 wpabuf_put_u8(resp, srv_trans_id);
    2165             : 
    2166             :                 /* Status Code */
    2167           0 :                 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
    2168             :                 /* Response Data: empty */
    2169           0 :                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
    2170             :                              2);
    2171             :         }
    2172             : }
    2173             : 
    2174             : 
    2175          11 : static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
    2176             :                              struct wpabuf *resp, u8 srv_trans_id)
    2177             : {
    2178             :         struct p2p_srv_upnp *usrv;
    2179             :         u8 *len_pos;
    2180             : 
    2181          11 :         wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
    2182             : 
    2183          11 :         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
    2184           0 :                 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
    2185           0 :                 return;
    2186             :         }
    2187             : 
    2188         313 :         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
    2189             :                          struct p2p_srv_upnp, list) {
    2190         303 :                 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
    2191           1 :                         return;
    2192             : 
    2193             :                 /* Length (to be filled) */
    2194         302 :                 len_pos = wpabuf_put(resp, 2);
    2195         302 :                 wpabuf_put_u8(resp, P2P_SERV_UPNP);
    2196         302 :                 wpabuf_put_u8(resp, srv_trans_id);
    2197             : 
    2198             :                 /* Status Code */
    2199         302 :                 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
    2200             :                 /* Response Data */
    2201         302 :                 wpabuf_put_u8(resp, usrv->version);
    2202         302 :                 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
    2203             :                            usrv->service);
    2204         302 :                 wpabuf_put_str(resp, usrv->service);
    2205         302 :                 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
    2206             :                              2);
    2207             :         }
    2208             : }
    2209             : 
    2210             : 
    2211           7 : static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
    2212             :                              struct wpabuf *resp, u8 srv_trans_id,
    2213             :                              const u8 *query, size_t query_len)
    2214             : {
    2215             :         struct p2p_srv_upnp *usrv;
    2216             :         u8 *len_pos;
    2217             :         u8 version;
    2218             :         char *str;
    2219           7 :         int count = 0;
    2220             : 
    2221           7 :         wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
    2222             :                           query, query_len);
    2223             : 
    2224           7 :         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
    2225           1 :                 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
    2226           1 :                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
    2227             :                                             srv_trans_id);
    2228           1 :                 return;
    2229             :         }
    2230             : 
    2231           6 :         if (query_len == 0) {
    2232           5 :                 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
    2233           5 :                 return;
    2234             :         }
    2235             : 
    2236           1 :         if (wpabuf_tailroom(resp) < 5)
    2237           0 :                 return;
    2238             : 
    2239             :         /* Length (to be filled) */
    2240           1 :         len_pos = wpabuf_put(resp, 2);
    2241           1 :         wpabuf_put_u8(resp, P2P_SERV_UPNP);
    2242           1 :         wpabuf_put_u8(resp, srv_trans_id);
    2243             : 
    2244           1 :         version = query[0];
    2245           1 :         str = os_malloc(query_len);
    2246           1 :         if (str == NULL)
    2247           0 :                 return;
    2248           1 :         os_memcpy(str, query + 1, query_len - 1);
    2249           1 :         str[query_len - 1] = '\0';
    2250             : 
    2251           6 :         dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
    2252             :                          struct p2p_srv_upnp, list) {
    2253           5 :                 if (version != usrv->version)
    2254           0 :                         continue;
    2255             : 
    2256           5 :                 if (os_strcmp(str, "ssdp:all") != 0 &&
    2257           0 :                     os_strstr(usrv->service, str) == NULL)
    2258           0 :                         continue;
    2259             : 
    2260           5 :                 if (wpabuf_tailroom(resp) < 2)
    2261           0 :                         break;
    2262           5 :                 if (count == 0) {
    2263             :                         /* Status Code */
    2264           1 :                         wpabuf_put_u8(resp, P2P_SD_SUCCESS);
    2265             :                         /* Response Data */
    2266           1 :                         wpabuf_put_u8(resp, version);
    2267             :                 } else
    2268           4 :                         wpabuf_put_u8(resp, ',');
    2269             : 
    2270           5 :                 count++;
    2271             : 
    2272           5 :                 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
    2273             :                            usrv->service);
    2274           5 :                 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
    2275           0 :                         break;
    2276           5 :                 wpabuf_put_str(resp, usrv->service);
    2277             :         }
    2278           1 :         os_free(str);
    2279             : 
    2280           1 :         if (count == 0) {
    2281           0 :                 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
    2282             :                            "available");
    2283             :                 /* Status Code */
    2284           0 :                 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
    2285             :                 /* Response Data: empty */
    2286             :         }
    2287             : 
    2288           1 :         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
    2289             : }
    2290             : 
    2291             : 
    2292             : #ifdef CONFIG_WIFI_DISPLAY
    2293           2 : static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
    2294             :                             struct wpabuf *resp, u8 srv_trans_id,
    2295             :                             const u8 *query, size_t query_len)
    2296             : {
    2297             :         const u8 *pos;
    2298             :         u8 role;
    2299             :         u8 *len_pos;
    2300             : 
    2301           2 :         wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
    2302             : 
    2303           2 :         if (!wpa_s->global->wifi_display) {
    2304           0 :                 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
    2305           0 :                 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
    2306             :                                             srv_trans_id);
    2307           0 :                 return;
    2308             :         }
    2309             : 
    2310           2 :         if (query_len < 1) {
    2311           0 :                 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
    2312             :                            "Role");
    2313           0 :                 return;
    2314             :         }
    2315             : 
    2316           2 :         if (wpabuf_tailroom(resp) < 5)
    2317           0 :                 return;
    2318             : 
    2319           2 :         pos = query;
    2320           2 :         role = *pos++;
    2321           2 :         wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
    2322             : 
    2323             :         /* TODO: role specific handling */
    2324             : 
    2325             :         /* Length (to be filled) */
    2326           2 :         len_pos = wpabuf_put(resp, 2);
    2327           2 :         wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
    2328           2 :         wpabuf_put_u8(resp, srv_trans_id);
    2329           2 :         wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
    2330             : 
    2331          12 :         while (pos < query + query_len) {
    2332          16 :                 if (*pos < MAX_WFD_SUBELEMS &&
    2333          16 :                     wpa_s->global->wfd_subelem[*pos] &&
    2334           8 :                     wpabuf_tailroom(resp) >=
    2335           8 :                     wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
    2336           8 :                         wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
    2337           8 :                                    "subelement %u", *pos);
    2338           8 :                         wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
    2339             :                 }
    2340           8 :                 pos++;
    2341             :         }
    2342             : 
    2343           2 :         WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
    2344             : }
    2345             : #endif /* CONFIG_WIFI_DISPLAY */
    2346             : 
    2347             : 
    2348          23 : static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
    2349             :                             u16 update_indic, const u8 *tlvs, size_t tlvs_len)
    2350             : {
    2351          23 :         struct wpa_supplicant *wpa_s = ctx;
    2352          23 :         const u8 *pos = tlvs;
    2353          23 :         const u8 *end = tlvs + tlvs_len;
    2354             :         const u8 *tlv_end;
    2355             :         u16 slen;
    2356             :         struct wpabuf *resp;
    2357             :         u8 srv_proto, srv_trans_id;
    2358             :         size_t buf_len;
    2359             :         char *buf;
    2360             : 
    2361          23 :         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
    2362             :                     tlvs, tlvs_len);
    2363          23 :         buf_len = 2 * tlvs_len + 1;
    2364          23 :         buf = os_malloc(buf_len);
    2365          23 :         if (buf) {
    2366          23 :                 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
    2367         161 :                 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
    2368             :                              MACSTR " %u %u %s",
    2369         138 :                              freq, MAC2STR(sa), dialog_token, update_indic,
    2370             :                              buf);
    2371          23 :                 os_free(buf);
    2372             :         }
    2373             : 
    2374          23 :         if (wpa_s->p2p_sd_over_ctrl_iface) {
    2375           0 :                 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
    2376             :                                            update_indic, tlvs, tlvs_len);
    2377           0 :                 return; /* to be processed by an external program */
    2378             :         }
    2379             : 
    2380          23 :         resp = wpabuf_alloc(10000);
    2381          23 :         if (resp == NULL)
    2382           0 :                 return;
    2383             : 
    2384          70 :         while (pos + 1 < end) {
    2385          24 :                 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
    2386          24 :                 slen = WPA_GET_LE16(pos);
    2387          24 :                 pos += 2;
    2388          24 :                 if (pos + slen > end || slen < 2) {
    2389           0 :                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
    2390             :                                    "length");
    2391           0 :                         wpabuf_free(resp);
    2392           0 :                         return;
    2393             :                 }
    2394          24 :                 tlv_end = pos + slen;
    2395             : 
    2396          24 :                 srv_proto = *pos++;
    2397          24 :                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
    2398             :                            srv_proto);
    2399          24 :                 srv_trans_id = *pos++;
    2400          24 :                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
    2401             :                            srv_trans_id);
    2402             : 
    2403          24 :                 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
    2404          24 :                             pos, tlv_end - pos);
    2405             : 
    2406             : 
    2407          24 :                 if (wpa_s->force_long_sd) {
    2408           0 :                         wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
    2409             :                                    "response");
    2410           0 :                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
    2411           0 :                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
    2412           0 :                         goto done;
    2413             :                 }
    2414             : 
    2415          24 :                 switch (srv_proto) {
    2416             :                 case P2P_SERV_ALL_SERVICES:
    2417           7 :                         wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
    2418             :                                    "for all services");
    2419           8 :                         if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
    2420           1 :                             dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
    2421           1 :                                 wpa_printf(MSG_DEBUG, "P2P: No service "
    2422             :                                            "discovery protocols available");
    2423           1 :                                 wpas_sd_add_proto_not_avail(
    2424             :                                         resp, P2P_SERV_ALL_SERVICES,
    2425             :                                         srv_trans_id);
    2426           1 :                                 break;
    2427             :                         }
    2428           6 :                         wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
    2429           6 :                         wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
    2430           6 :                         break;
    2431             :                 case P2P_SERV_BONJOUR:
    2432           7 :                         wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
    2433           7 :                                             pos, tlv_end - pos);
    2434           7 :                         break;
    2435             :                 case P2P_SERV_UPNP:
    2436           7 :                         wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
    2437           7 :                                          pos, tlv_end - pos);
    2438           7 :                         break;
    2439             : #ifdef CONFIG_WIFI_DISPLAY
    2440             :                 case P2P_SERV_WIFI_DISPLAY:
    2441           2 :                         wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
    2442           2 :                                         pos, tlv_end - pos);
    2443           2 :                         break;
    2444             : #endif /* CONFIG_WIFI_DISPLAY */
    2445             :                 default:
    2446           1 :                         wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
    2447             :                                    "protocol %u", srv_proto);
    2448           1 :                         wpas_sd_add_proto_not_avail(resp, srv_proto,
    2449             :                                                     srv_trans_id);
    2450           1 :                         break;
    2451             :                 }
    2452             : 
    2453          24 :                 pos = tlv_end;
    2454             :         }
    2455             : 
    2456             : done:
    2457          23 :         wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
    2458             :                                    update_indic, tlvs, tlvs_len);
    2459             : 
    2460          23 :         wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
    2461             : 
    2462          23 :         wpabuf_free(resp);
    2463             : }
    2464             : 
    2465             : 
    2466          23 : static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
    2467             :                              const u8 *tlvs, size_t tlvs_len)
    2468             : {
    2469          23 :         struct wpa_supplicant *wpa_s = ctx;
    2470          23 :         const u8 *pos = tlvs;
    2471          23 :         const u8 *end = tlvs + tlvs_len;
    2472             :         const u8 *tlv_end;
    2473             :         u16 slen;
    2474             :         size_t buf_len;
    2475             :         char *buf;
    2476             : 
    2477          23 :         wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
    2478             :                     tlvs, tlvs_len);
    2479          23 :         if (tlvs_len > 1500) {
    2480             :                 /* TODO: better way for handling this */
    2481          14 :                 wpa_msg_ctrl(wpa_s, MSG_INFO,
    2482             :                              P2P_EVENT_SERV_DISC_RESP MACSTR
    2483             :                              " %u <long response: %u bytes>",
    2484          12 :                              MAC2STR(sa), update_indic,
    2485             :                              (unsigned int) tlvs_len);
    2486             :         } else {
    2487          21 :                 buf_len = 2 * tlvs_len + 1;
    2488          21 :                 buf = os_malloc(buf_len);
    2489          21 :                 if (buf) {
    2490          21 :                         wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
    2491         147 :                         wpa_msg_ctrl(wpa_s, MSG_INFO,
    2492             :                                      P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
    2493         126 :                                      MAC2STR(sa), update_indic, buf);
    2494          21 :                         os_free(buf);
    2495             :                 }
    2496             :         }
    2497             : 
    2498         417 :         while (pos < end) {
    2499             :                 u8 srv_proto, srv_trans_id, status;
    2500             : 
    2501         371 :                 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
    2502         371 :                 slen = WPA_GET_LE16(pos);
    2503         371 :                 pos += 2;
    2504         371 :                 if (pos + slen > end || slen < 3) {
    2505           0 :                         wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
    2506             :                                    "length");
    2507          23 :                         return;
    2508             :                 }
    2509         371 :                 tlv_end = pos + slen;
    2510             : 
    2511         371 :                 srv_proto = *pos++;
    2512         371 :                 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
    2513             :                            srv_proto);
    2514         371 :                 srv_trans_id = *pos++;
    2515         371 :                 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
    2516             :                            srv_trans_id);
    2517         371 :                 status = *pos++;
    2518         371 :                 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
    2519             :                            status);
    2520             : 
    2521         371 :                 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
    2522         371 :                             pos, tlv_end - pos);
    2523             : 
    2524         371 :                 pos = tlv_end;
    2525             :         }
    2526             : 
    2527          23 :         wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
    2528             : }
    2529             : 
    2530             : 
    2531          24 : u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
    2532             :                         const struct wpabuf *tlvs)
    2533             : {
    2534          24 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    2535           0 :                 return 0;
    2536          24 :         return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
    2537             : }
    2538             : 
    2539             : 
    2540           1 : u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
    2541             :                              u8 version, const char *query)
    2542             : {
    2543             :         struct wpabuf *tlvs;
    2544             :         u64 ret;
    2545             : 
    2546           1 :         tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
    2547           1 :         if (tlvs == NULL)
    2548           0 :                 return 0;
    2549           1 :         wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
    2550           1 :         wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
    2551           1 :         wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
    2552           1 :         wpabuf_put_u8(tlvs, version);
    2553           1 :         wpabuf_put_str(tlvs, query);
    2554           1 :         ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
    2555           1 :         wpabuf_free(tlvs);
    2556           1 :         return ret;
    2557             : }
    2558             : 
    2559             : 
    2560             : #ifdef CONFIG_WIFI_DISPLAY
    2561             : 
    2562           1 : static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
    2563             :                                    const struct wpabuf *tlvs)
    2564             : {
    2565           1 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    2566           0 :                 return 0;
    2567           1 :         return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
    2568             : }
    2569             : 
    2570             : 
    2571             : #define MAX_WFD_SD_SUBELEMS 20
    2572             : 
    2573           2 : static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
    2574             :                                 const char *subelems)
    2575             : {
    2576             :         u8 *len;
    2577             :         const char *pos;
    2578             :         int val;
    2579           2 :         int count = 0;
    2580             : 
    2581           2 :         len = wpabuf_put(tlvs, 2);
    2582           2 :         wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
    2583           2 :         wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
    2584             : 
    2585           2 :         wpabuf_put_u8(tlvs, role);
    2586             : 
    2587           2 :         pos = subelems;
    2588          10 :         while (*pos) {
    2589           8 :                 val = atoi(pos);
    2590           8 :                 if (val >= 0 && val < 256) {
    2591           8 :                         wpabuf_put_u8(tlvs, val);
    2592           8 :                         count++;
    2593           8 :                         if (count == MAX_WFD_SD_SUBELEMS)
    2594           0 :                                 break;
    2595             :                 }
    2596           8 :                 pos = os_strchr(pos + 1, ',');
    2597           8 :                 if (pos == NULL)
    2598           2 :                         break;
    2599           6 :                 pos++;
    2600             :         }
    2601             : 
    2602           2 :         WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
    2603           2 : }
    2604             : 
    2605             : 
    2606           1 : u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
    2607             :                                      const u8 *dst, const char *role)
    2608             : {
    2609             :         struct wpabuf *tlvs;
    2610             :         u64 ret;
    2611             :         const char *subelems;
    2612           1 :         u8 id = 1;
    2613             : 
    2614           1 :         subelems = os_strchr(role, ' ');
    2615           1 :         if (subelems == NULL)
    2616           0 :                 return 0;
    2617           1 :         subelems++;
    2618             : 
    2619           1 :         tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
    2620           1 :         if (tlvs == NULL)
    2621           0 :                 return 0;
    2622             : 
    2623           1 :         if (os_strstr(role, "[source]"))
    2624           1 :                 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
    2625           1 :         if (os_strstr(role, "[pri-sink]"))
    2626           1 :                 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
    2627           1 :         if (os_strstr(role, "[sec-sink]"))
    2628           0 :                 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
    2629           1 :         if (os_strstr(role, "[source+sink]"))
    2630           0 :                 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
    2631             : 
    2632           1 :         ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
    2633           1 :         wpabuf_free(tlvs);
    2634           1 :         return ret;
    2635             : }
    2636             : 
    2637             : #endif /* CONFIG_WIFI_DISPLAY */
    2638             : 
    2639             : 
    2640           6 : int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
    2641             : {
    2642           6 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    2643           0 :                 return -1;
    2644           6 :         return p2p_sd_cancel_request(wpa_s->global->p2p,
    2645             :                                      (void *) (uintptr_t) req);
    2646             : }
    2647             : 
    2648             : 
    2649          23 : void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
    2650             :                           const u8 *dst, u8 dialog_token,
    2651             :                           const struct wpabuf *resp_tlvs)
    2652             : {
    2653          23 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    2654          23 :                 return;
    2655          23 :         p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
    2656             :                         resp_tlvs);
    2657             : }
    2658             : 
    2659             : 
    2660        2238 : void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
    2661             : {
    2662        2238 :         if (wpa_s->global->p2p)
    2663        2238 :                 p2p_sd_service_update(wpa_s->global->p2p);
    2664        2238 : }
    2665             : 
    2666             : 
    2667          60 : static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
    2668             : {
    2669          60 :         dl_list_del(&bsrv->list);
    2670          60 :         wpabuf_free(bsrv->query);
    2671          60 :         wpabuf_free(bsrv->resp);
    2672          60 :         os_free(bsrv);
    2673          60 : }
    2674             : 
    2675             : 
    2676         253 : static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
    2677             : {
    2678         253 :         dl_list_del(&usrv->list);
    2679         253 :         os_free(usrv->service);
    2680         253 :         os_free(usrv);
    2681         253 : }
    2682             : 
    2683             : 
    2684        1903 : void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
    2685             : {
    2686             :         struct p2p_srv_bonjour *bsrv, *bn;
    2687             :         struct p2p_srv_upnp *usrv, *un;
    2688             : 
    2689        1952 :         dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
    2690             :                               struct p2p_srv_bonjour, list)
    2691          49 :                 wpas_p2p_srv_bonjour_free(bsrv);
    2692             : 
    2693        2145 :         dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
    2694             :                               struct p2p_srv_upnp, list)
    2695         242 :                 wpas_p2p_srv_upnp_free(usrv);
    2696             : 
    2697        1903 :         wpas_p2p_sd_service_update(wpa_s);
    2698        1903 : }
    2699             : 
    2700             : 
    2701          60 : int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
    2702             :                                  struct wpabuf *query, struct wpabuf *resp)
    2703             : {
    2704             :         struct p2p_srv_bonjour *bsrv;
    2705             : 
    2706          60 :         bsrv = os_zalloc(sizeof(*bsrv));
    2707          60 :         if (bsrv == NULL)
    2708           0 :                 return -1;
    2709          60 :         bsrv->query = query;
    2710          60 :         bsrv->resp = resp;
    2711          60 :         dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
    2712             : 
    2713          60 :         wpas_p2p_sd_service_update(wpa_s);
    2714          60 :         return 0;
    2715             : }
    2716             : 
    2717             : 
    2718          22 : int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
    2719             :                                  const struct wpabuf *query)
    2720             : {
    2721             :         struct p2p_srv_bonjour *bsrv;
    2722             : 
    2723          22 :         bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
    2724          22 :         if (bsrv == NULL)
    2725          11 :                 return -1;
    2726          11 :         wpas_p2p_srv_bonjour_free(bsrv);
    2727          11 :         wpas_p2p_sd_service_update(wpa_s);
    2728          11 :         return 0;
    2729             : }
    2730             : 
    2731             : 
    2732         275 : int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
    2733             :                               const char *service)
    2734             : {
    2735             :         struct p2p_srv_upnp *usrv;
    2736             : 
    2737         275 :         if (wpas_p2p_service_get_upnp(wpa_s, version, service))
    2738          22 :                 return 0; /* Already listed */
    2739         253 :         usrv = os_zalloc(sizeof(*usrv));
    2740         253 :         if (usrv == NULL)
    2741           0 :                 return -1;
    2742         253 :         usrv->version = version;
    2743         253 :         usrv->service = os_strdup(service);
    2744         253 :         if (usrv->service == NULL) {
    2745           0 :                 os_free(usrv);
    2746           0 :                 return -1;
    2747             :         }
    2748         253 :         dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
    2749             : 
    2750         253 :         wpas_p2p_sd_service_update(wpa_s);
    2751         253 :         return 0;
    2752             : }
    2753             : 
    2754             : 
    2755          22 : int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
    2756             :                               const char *service)
    2757             : {
    2758             :         struct p2p_srv_upnp *usrv;
    2759             : 
    2760          22 :         usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
    2761          22 :         if (usrv == NULL)
    2762          11 :                 return -1;
    2763          11 :         wpas_p2p_srv_upnp_free(usrv);
    2764          11 :         wpas_p2p_sd_service_update(wpa_s);
    2765          11 :         return 0;
    2766             : }
    2767             : 
    2768             : 
    2769          36 : static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
    2770             :                                          const u8 *peer, const char *params,
    2771             :                                          unsigned int generated_pin)
    2772             : {
    2773         216 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
    2774         216 :                        " %08d%s", MAC2STR(peer), generated_pin, params);
    2775          36 : }
    2776             : 
    2777             : 
    2778           3 : static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
    2779             :                                         const u8 *peer, const char *params)
    2780             : {
    2781          18 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
    2782          18 :                        "%s", MAC2STR(peer), params);
    2783           3 : }
    2784             : 
    2785             : 
    2786          41 : static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
    2787             :                                const u8 *dev_addr, const u8 *pri_dev_type,
    2788             :                                const char *dev_name, u16 supp_config_methods,
    2789             :                                u8 dev_capab, u8 group_capab, const u8 *group_id,
    2790             :                                size_t group_id_len)
    2791             : {
    2792          41 :         struct wpa_supplicant *wpa_s = ctx;
    2793             :         char devtype[WPS_DEV_TYPE_BUFSIZE];
    2794             :         char params[300];
    2795             :         u8 empty_dev_type[8];
    2796          41 :         unsigned int generated_pin = 0;
    2797          41 :         struct wpa_supplicant *group = NULL;
    2798             : 
    2799          41 :         if (group_id) {
    2800          33 :                 for (group = wpa_s->global->ifaces; group; group = group->next)
    2801             :                 {
    2802          33 :                         struct wpa_ssid *s = group->current_ssid;
    2803          66 :                         if (s != NULL &&
    2804          66 :                             s->mode == WPAS_MODE_P2P_GO &&
    2805          66 :                             group_id_len - ETH_ALEN == s->ssid_len &&
    2806          33 :                             os_memcmp(group_id + ETH_ALEN, s->ssid,
    2807             :                                       s->ssid_len) == 0)
    2808          33 :                                 break;
    2809             :                 }
    2810             :         }
    2811             : 
    2812          41 :         if (pri_dev_type == NULL) {
    2813           0 :                 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
    2814           0 :                 pri_dev_type = empty_dev_type;
    2815             :         }
    2816         287 :         os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
    2817             :                     " pri_dev_type=%s name='%s' config_methods=0x%x "
    2818             :                     "dev_capab=0x%x group_capab=0x%x%s%s",
    2819         246 :                     MAC2STR(dev_addr),
    2820             :                     wps_dev_type_bin2str(pri_dev_type, devtype,
    2821             :                                          sizeof(devtype)),
    2822             :                     dev_name, supp_config_methods, dev_capab, group_capab,
    2823             :                     group ? " group=" : "",
    2824             :                     group ? group->ifname : "");
    2825          41 :         params[sizeof(params) - 1] = '\0';
    2826             : 
    2827          41 :         if (config_methods & WPS_CONFIG_DISPLAY) {
    2828          35 :                 generated_pin = wps_generate_pin();
    2829          35 :                 wpas_prov_disc_local_display(wpa_s, peer, params,
    2830             :                                              generated_pin);
    2831           6 :         } else if (config_methods & WPS_CONFIG_KEYPAD)
    2832           1 :                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
    2833           5 :         else if (config_methods & WPS_CONFIG_PUSHBUTTON)
    2834          30 :                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
    2835          30 :                                MACSTR "%s", MAC2STR(peer), params);
    2836             : 
    2837          41 :         wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
    2838             :                                             P2P_PROV_DISC_SUCCESS,
    2839             :                                             config_methods, generated_pin);
    2840          41 : }
    2841             : 
    2842             : 
    2843          37 : static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
    2844             : {
    2845          37 :         struct wpa_supplicant *wpa_s = ctx;
    2846          37 :         unsigned int generated_pin = 0;
    2847             :         char params[20];
    2848             : 
    2849          70 :         if (wpa_s->pending_pd_before_join &&
    2850          33 :             (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
    2851           0 :              os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
    2852          33 :                 wpa_s->pending_pd_before_join = 0;
    2853          33 :                 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
    2854             :                            "join-existing-group operation");
    2855          33 :                 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
    2856          70 :                 return;
    2857             :         }
    2858             : 
    2859           8 :         if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
    2860           4 :             wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
    2861           0 :                 os_snprintf(params, sizeof(params), " peer_go=%d",
    2862           0 :                             wpa_s->pending_pd_use == AUTO_PD_JOIN);
    2863             :         else
    2864           4 :                 params[0] = '\0';
    2865             : 
    2866           4 :         if (config_methods & WPS_CONFIG_DISPLAY)
    2867           2 :                 wpas_prov_disc_local_keypad(wpa_s, peer, params);
    2868           2 :         else if (config_methods & WPS_CONFIG_KEYPAD) {
    2869           1 :                 generated_pin = wps_generate_pin();
    2870           1 :                 wpas_prov_disc_local_display(wpa_s, peer, params,
    2871             :                                              generated_pin);
    2872           1 :         } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
    2873           6 :                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
    2874           6 :                                MACSTR "%s", MAC2STR(peer), params);
    2875             : 
    2876           4 :         wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
    2877             :                                             P2P_PROV_DISC_SUCCESS,
    2878             :                                             config_methods, generated_pin);
    2879             : }
    2880             : 
    2881             : 
    2882           2 : static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
    2883             :                                 enum p2p_prov_disc_status status)
    2884             : {
    2885           2 :         struct wpa_supplicant *wpa_s = ctx;
    2886             : 
    2887           2 :         if (wpa_s->p2p_fallback_to_go_neg) {
    2888           0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
    2889             :                         "failed - fall back to GO Negotiation");
    2890           0 :                 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
    2891           0 :                 return;
    2892             :         }
    2893             : 
    2894           2 :         if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
    2895           0 :                 wpa_s->pending_pd_before_join = 0;
    2896           0 :                 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
    2897             :                            "join-existing-group operation (no ACK for PD "
    2898             :                            "Req attempts)");
    2899           0 :                 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
    2900           0 :                 return;
    2901             :         }
    2902             : 
    2903          12 :         wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
    2904             :                        " p2p_dev_addr=" MACSTR " status=%d",
    2905          12 :                        MAC2STR(peer), status);
    2906             : 
    2907           2 :         wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
    2908             :                                             status, 0, 0);
    2909             : }
    2910             : 
    2911             : 
    2912         205 : static int freq_included(const struct p2p_channels *channels, unsigned int freq)
    2913             : {
    2914         205 :         if (channels == NULL)
    2915          61 :                 return 1; /* Assume no restrictions */
    2916         144 :         return p2p_channels_includes_freq(channels, freq);
    2917             : 
    2918             : }
    2919             : 
    2920             : 
    2921          33 : static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
    2922             :                                   const u8 *go_dev_addr, const u8 *ssid,
    2923             :                                   size_t ssid_len, int *go, u8 *group_bssid,
    2924             :                                   int *force_freq, int persistent_group,
    2925             :                                   const struct p2p_channels *channels,
    2926             :                                   int dev_pw_id)
    2927             : {
    2928          33 :         struct wpa_supplicant *wpa_s = ctx;
    2929             :         struct wpa_ssid *s;
    2930             :         int res;
    2931             :         struct wpa_supplicant *grp;
    2932             : 
    2933          33 :         if (!persistent_group) {
    2934          84 :                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
    2935             :                            " to join an active group (SSID: %s)",
    2936          72 :                            MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
    2937          13 :                 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
    2938           1 :                     (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
    2939           0 :                      == 0 ||
    2940           0 :                      os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
    2941           1 :                         wpa_printf(MSG_DEBUG, "P2P: Accept previously "
    2942             :                                    "authorized invitation");
    2943           1 :                         goto accept_inv;
    2944             :                 }
    2945             : 
    2946             : #ifdef CONFIG_WPS_NFC
    2947          13 :                 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
    2948           2 :                     dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
    2949           2 :                         wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
    2950           2 :                         wpa_s->parent->p2p_wps_method = WPS_NFC;
    2951           2 :                         wpa_s->parent->pending_join_wps_method = WPS_NFC;
    2952           2 :                         os_memcpy(wpa_s->parent->pending_join_dev_addr,
    2953             :                                   go_dev_addr, ETH_ALEN);
    2954           2 :                         os_memcpy(wpa_s->parent->pending_join_iface_addr,
    2955             :                                   bssid, ETH_ALEN);
    2956           2 :                         goto accept_inv;
    2957             :                 }
    2958             : #endif /* CONFIG_WPS_NFC */
    2959             : 
    2960             :                 /*
    2961             :                  * Do not accept the invitation automatically; notify user and
    2962             :                  * request approval.
    2963             :                  */
    2964           9 :                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
    2965             :         }
    2966             : 
    2967          21 :         grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
    2968          21 :         if (grp) {
    2969           1 :                 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
    2970             :                            "running persistent group");
    2971           1 :                 if (*go)
    2972           1 :                         os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
    2973           1 :                 goto accept_inv;
    2974             :         }
    2975             : 
    2976          28 :         if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
    2977           8 :             os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
    2978           8 :                 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
    2979             :                            "invitation to re-invoke a persistent group");
    2980           8 :                 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
    2981          12 :         } else if (!wpa_s->conf->persistent_reconnect)
    2982           3 :                 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
    2983             : 
    2984          18 :         for (s = wpa_s->conf->ssid; s; s = s->next) {
    2985          33 :                 if (s->disabled == 2 &&
    2986          32 :                     os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
    2987          32 :                     s->ssid_len == ssid_len &&
    2988          16 :                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
    2989          16 :                         break;
    2990             :         }
    2991             : 
    2992          17 :         if (!s) {
    2993           6 :                 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
    2994             :                            " requested reinvocation of an unknown group",
    2995           6 :                            MAC2STR(sa));
    2996           1 :                 return P2P_SC_FAIL_UNKNOWN_GROUP;
    2997             :         }
    2998             : 
    2999          16 :         if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
    3000           7 :                 *go = 1;
    3001           7 :                 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
    3002           0 :                         wpa_printf(MSG_DEBUG, "P2P: The only available "
    3003             :                                    "interface is already in use - reject "
    3004             :                                    "invitation");
    3005           0 :                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
    3006             :                 }
    3007           7 :                 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
    3008           9 :         } else if (s->mode == WPAS_MODE_P2P_GO) {
    3009           1 :                 *go = 1;
    3010           1 :                 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
    3011             :                 {
    3012           0 :                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
    3013             :                                    "interface address for the group");
    3014           0 :                         return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
    3015             :                 }
    3016           1 :                 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
    3017             :                           ETH_ALEN);
    3018             :         }
    3019             : 
    3020             : accept_inv:
    3021          20 :         wpas_p2p_set_own_freq_preference(wpa_s, 0);
    3022             : 
    3023             :         /* Get one of the frequencies currently in use */
    3024          20 :         if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
    3025           2 :                 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
    3026           2 :                 wpas_p2p_set_own_freq_preference(wpa_s, res);
    3027             : 
    3028           2 :                 if (wpa_s->num_multichan_concurrent < 2 ||
    3029           0 :                     wpas_p2p_num_unused_channels(wpa_s) < 1) {
    3030           2 :                         wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
    3031           2 :                         *force_freq = res;
    3032             :                 }
    3033             :         }
    3034             : 
    3035          20 :         if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
    3036           0 :             wpas_p2p_num_unused_channels(wpa_s) > 0) {
    3037           0 :                 if (*go == 0) {
    3038             :                         /* We are the client */
    3039           0 :                         wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
    3040             :                                    "running a GO but we are capable of MCC, "
    3041             :                                    "figure out the best channel to use");
    3042           0 :                         *force_freq = 0;
    3043           0 :                 } else if (!freq_included(channels, *force_freq)) {
    3044             :                         /* We are the GO, and *force_freq is not in the
    3045             :                          * intersection */
    3046           0 :                         wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
    3047             :                                    "in intersection but we are capable of MCC, "
    3048             :                                    "figure out the best channel to use",
    3049             :                                    *force_freq);
    3050           0 :                         *force_freq = 0;
    3051             :                 }
    3052             :         }
    3053             : 
    3054          20 :         return P2P_SC_SUCCESS;
    3055             : }
    3056             : 
    3057             : 
    3058          37 : static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
    3059             :                                      const u8 *ssid, size_t ssid_len,
    3060             :                                      const u8 *go_dev_addr, u8 status,
    3061             :                                      int op_freq)
    3062             : {
    3063          37 :         struct wpa_supplicant *wpa_s = ctx;
    3064             :         struct wpa_ssid *s;
    3065             : 
    3066          38 :         for (s = wpa_s->conf->ssid; s; s = s->next) {
    3067          39 :                 if (s->disabled == 2 &&
    3068          38 :                     s->ssid_len == ssid_len &&
    3069          19 :                     os_memcmp(ssid, s->ssid, ssid_len) == 0)
    3070          19 :                         break;
    3071             :         }
    3072             : 
    3073          37 :         if (status == P2P_SC_SUCCESS) {
    3074         133 :                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
    3075             :                            " was accepted; op_freq=%d MHz, SSID=%s",
    3076         114 :                            MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
    3077          19 :                 if (s) {
    3078          16 :                         int go = s->mode == WPAS_MODE_P2P_GO;
    3079          16 :                         wpas_p2p_group_add_persistent(
    3080             :                                 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
    3081             :                                 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
    3082           3 :                 } else if (bssid) {
    3083           3 :                         wpa_s->user_initiated_pd = 0;
    3084           3 :                         wpas_p2p_join(wpa_s, bssid, go_dev_addr,
    3085           3 :                                       wpa_s->p2p_wps_method, 0, op_freq,
    3086             :                                       ssid, ssid_len);
    3087             :                 }
    3088          19 :                 return;
    3089             :         }
    3090             : 
    3091          18 :         if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
    3092          35 :                 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
    3093          30 :                            " was rejected (status %u)", MAC2STR(sa), status);
    3094           5 :                 return;
    3095             :         }
    3096             : 
    3097          13 :         if (!s) {
    3098          10 :                 if (bssid) {
    3099         162 :                         wpa_msg_global(wpa_s, MSG_INFO,
    3100             :                                        P2P_EVENT_INVITATION_RECEIVED
    3101             :                                        "sa=" MACSTR " go_dev_addr=" MACSTR
    3102             :                                        " bssid=" MACSTR " unknown-network",
    3103         108 :                                        MAC2STR(sa), MAC2STR(go_dev_addr),
    3104          54 :                                        MAC2STR(bssid));
    3105             :                 } else {
    3106          12 :                         wpa_msg_global(wpa_s, MSG_INFO,
    3107             :                                        P2P_EVENT_INVITATION_RECEIVED
    3108             :                                        "sa=" MACSTR " go_dev_addr=" MACSTR
    3109             :                                        " unknown-network",
    3110          12 :                                        MAC2STR(sa), MAC2STR(go_dev_addr));
    3111             :                 }
    3112          10 :                 return;
    3113             :         }
    3114             : 
    3115           3 :         if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
    3116           7 :                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
    3117             :                                "sa=" MACSTR " persistent=%d freq=%d",
    3118           6 :                                MAC2STR(sa), s->id, op_freq);
    3119             :         } else {
    3120          14 :                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
    3121             :                                "sa=" MACSTR " persistent=%d",
    3122          12 :                                MAC2STR(sa), s->id);
    3123             :         }
    3124             : }
    3125             : 
    3126             : 
    3127           4 : static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
    3128             :                                         struct wpa_ssid *ssid,
    3129             :                                         const u8 *peer, int inv)
    3130             : {
    3131             :         size_t i;
    3132             : 
    3133           4 :         if (ssid == NULL)
    3134           0 :                 return;
    3135             : 
    3136           5 :         for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
    3137           5 :                 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
    3138             :                               ETH_ALEN) == 0)
    3139           4 :                         break;
    3140             :         }
    3141           4 :         if (i >= ssid->num_p2p_clients) {
    3142           0 :                 if (ssid->mode != WPAS_MODE_P2P_GO &&
    3143           0 :                     os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
    3144           0 :                         wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
    3145             :                                    "due to invitation result", ssid->id);
    3146           0 :                         wpas_notify_network_removed(wpa_s, ssid);
    3147           0 :                         wpa_config_remove_network(wpa_s->conf, ssid->id);
    3148           0 :                         return;
    3149             :                 }
    3150           0 :                 return; /* Peer not found in client list */
    3151             :         }
    3152             : 
    3153          28 :         wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
    3154             :                    "group %d client list%s",
    3155          24 :                    MAC2STR(peer), ssid->id,
    3156             :                    inv ? " due to invitation result" : "");
    3157           4 :         os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
    3158             :                    ssid->p2p_client_list + (i + 1) * ETH_ALEN,
    3159             :                    (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
    3160           4 :         ssid->num_p2p_clients--;
    3161           4 :         if (wpa_s->parent->conf->update_config &&
    3162           0 :             wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
    3163           0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
    3164             : }
    3165             : 
    3166             : 
    3167           0 : static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
    3168             :                                           const u8 *peer)
    3169             : {
    3170             :         struct wpa_ssid *ssid;
    3171             : 
    3172           0 :         wpa_s = wpa_s->global->p2p_invite_group;
    3173           0 :         if (wpa_s == NULL)
    3174           0 :                 return; /* No known invitation group */
    3175           0 :         ssid = wpa_s->current_ssid;
    3176           0 :         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
    3177           0 :             !ssid->p2p_persistent_group)
    3178           0 :                 return; /* Not operating as a GO in persistent group */
    3179           0 :         ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
    3180           0 :                                        ssid->ssid, ssid->ssid_len);
    3181           0 :         wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
    3182             : }
    3183             : 
    3184             : 
    3185          25 : static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
    3186             :                                    const struct p2p_channels *channels,
    3187             :                                    const u8 *peer, int neg_freq,
    3188             :                                    int peer_oper_freq)
    3189             : {
    3190          25 :         struct wpa_supplicant *wpa_s = ctx;
    3191             :         struct wpa_ssid *ssid;
    3192             :         int freq;
    3193             : 
    3194          25 :         if (bssid) {
    3195          54 :                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
    3196             :                                "status=%d " MACSTR,
    3197          54 :                                status, MAC2STR(bssid));
    3198             :         } else {
    3199          16 :                 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
    3200             :                                "status=%d ", status);
    3201             :         }
    3202          25 :         wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
    3203             : 
    3204         150 :         wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
    3205         150 :                    status, MAC2STR(peer));
    3206          25 :         if (wpa_s->pending_invite_ssid_id == -1) {
    3207           6 :                 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
    3208           0 :                         wpas_remove_persistent_client(wpa_s, peer);
    3209           6 :                 return; /* Invitation to active group */
    3210             :         }
    3211             : 
    3212          19 :         if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
    3213           2 :                 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
    3214             :                            "invitation exchange to indicate readiness for "
    3215             :                            "re-invocation");
    3216             :         }
    3217             : 
    3218          19 :         if (status != P2P_SC_SUCCESS) {
    3219           3 :                 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
    3220           1 :                         ssid = wpa_config_get_network(
    3221             :                                 wpa_s->conf, wpa_s->pending_invite_ssid_id);
    3222           1 :                         wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
    3223             :                 }
    3224           3 :                 wpas_p2p_remove_pending_group_interface(wpa_s);
    3225           3 :                 return;
    3226             :         }
    3227             : 
    3228          16 :         ssid = wpa_config_get_network(wpa_s->conf,
    3229             :                                       wpa_s->pending_invite_ssid_id);
    3230          16 :         if (ssid == NULL) {
    3231           0 :                 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
    3232             :                            "data matching with invitation");
    3233           0 :                 return;
    3234             :         }
    3235             : 
    3236             :         /*
    3237             :          * The peer could have missed our ctrl::ack frame for Invitation
    3238             :          * Response and continue retransmitting the frame. To reduce the
    3239             :          * likelihood of the peer not getting successful TX status for the
    3240             :          * Invitation Response frame, wait a short time here before starting
    3241             :          * the persistent group so that we will remain on the current channel to
    3242             :          * acknowledge any possible retransmission from the peer.
    3243             :          */
    3244          16 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
    3245             :                 "starting persistent group");
    3246          16 :         os_sleep(0, 50000);
    3247             : 
    3248          23 :         if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
    3249           7 :             freq_included(channels, neg_freq))
    3250           7 :                 freq = neg_freq;
    3251          18 :         else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
    3252           9 :                  freq_included(channels, peer_oper_freq))
    3253           9 :                 freq = peer_oper_freq;
    3254             :         else
    3255           0 :                 freq = 0;
    3256             : 
    3257          32 :         wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
    3258          16 :                    freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
    3259          64 :         wpas_p2p_group_add_persistent(wpa_s, ssid,
    3260          16 :                                       ssid->mode == WPAS_MODE_P2P_GO,
    3261             :                                       wpa_s->p2p_persistent_go_freq,
    3262             :                                       freq,
    3263          32 :                                       wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
    3264             :                                       channels,
    3265          16 :                                       ssid->mode == WPAS_MODE_P2P_GO ?
    3266             :                                       P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
    3267             :                                       0);
    3268             : }
    3269             : 
    3270             : 
    3271      262012 : static int wpas_p2p_disallowed_freq(struct wpa_global *global,
    3272             :                                     unsigned int freq)
    3273             : {
    3274      262012 :         if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
    3275         176 :                 return 1;
    3276      261836 :         return freq_range_list_includes(&global->p2p_disallow_freq, freq);
    3277             : }
    3278             : 
    3279             : 
    3280           0 : static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
    3281             : {
    3282           0 :         reg->channel[reg->channels] = chan;
    3283           0 :         reg->channels++;
    3284           0 : }
    3285             : 
    3286             : 
    3287           0 : static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
    3288             :                                      struct p2p_channels *chan,
    3289             :                                      struct p2p_channels *cli_chan)
    3290             : {
    3291           0 :         int i, cla = 0;
    3292             : 
    3293           0 :         os_memset(cli_chan, 0, sizeof(*cli_chan));
    3294             : 
    3295           0 :         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
    3296             :                    "band");
    3297             : 
    3298             :         /* Operating class 81 - 2.4 GHz band channels 1..13 */
    3299           0 :         chan->reg_class[cla].reg_class = 81;
    3300           0 :         chan->reg_class[cla].channels = 0;
    3301           0 :         for (i = 0; i < 11; i++) {
    3302           0 :                 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
    3303           0 :                         wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
    3304             :         }
    3305           0 :         if (chan->reg_class[cla].channels)
    3306           0 :                 cla++;
    3307             : 
    3308           0 :         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
    3309             :                    "band");
    3310             : 
    3311             :         /* Operating class 115 - 5 GHz, channels 36-48 */
    3312           0 :         chan->reg_class[cla].reg_class = 115;
    3313           0 :         chan->reg_class[cla].channels = 0;
    3314           0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
    3315           0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
    3316           0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
    3317           0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
    3318           0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
    3319           0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
    3320           0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
    3321           0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
    3322           0 :         if (chan->reg_class[cla].channels)
    3323           0 :                 cla++;
    3324             : 
    3325           0 :         wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
    3326             :                    "band");
    3327             : 
    3328             :         /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
    3329           0 :         chan->reg_class[cla].reg_class = 124;
    3330           0 :         chan->reg_class[cla].channels = 0;
    3331           0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
    3332           0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
    3333           0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
    3334           0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
    3335           0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
    3336           0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
    3337           0 :         if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
    3338           0 :                 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
    3339           0 :         if (chan->reg_class[cla].channels)
    3340           0 :                 cla++;
    3341             : 
    3342           0 :         chan->reg_classes = cla;
    3343           0 :         return 0;
    3344             : }
    3345             : 
    3346             : 
    3347       21736 : static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
    3348             :                                           u16 num_modes,
    3349             :                                           enum hostapd_hw_mode mode)
    3350             : {
    3351             :         u16 i;
    3352             : 
    3353       40755 :         for (i = 0; i < num_modes; i++) {
    3354       40755 :                 if (modes[i].mode == mode)
    3355       21736 :                         return &modes[i];
    3356             :         }
    3357             : 
    3358           0 :         return NULL;
    3359             : }
    3360             : 
    3361             : 
    3362             : enum chan_allowed {
    3363             :         NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
    3364             : };
    3365             : 
    3366      262006 : static int has_channel(struct wpa_global *global,
    3367             :                        struct hostapd_hw_modes *mode, u8 chan, int *flags)
    3368             : {
    3369             :         int i;
    3370             :         unsigned int freq;
    3371             : 
    3372      524012 :         freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
    3373      262006 :                 chan * 5;
    3374      262006 :         if (wpas_p2p_disallowed_freq(global, freq))
    3375        1396 :                 return NOT_ALLOWED;
    3376             : 
    3377     3195573 :         for (i = 0; i < mode->num_channels; i++) {
    3378     3160551 :                 if (mode->channels[i].chan == chan) {
    3379      225588 :                         if (flags)
    3380      204300 :                                 *flags = mode->channels[i].flag;
    3381      225588 :                         if (mode->channels[i].flag &
    3382             :                             (HOSTAPD_CHAN_DISABLED |
    3383             :                              HOSTAPD_CHAN_RADAR))
    3384       41107 :                                 return NOT_ALLOWED;
    3385      184481 :                         if (mode->channels[i].flag &
    3386             :                             (HOSTAPD_CHAN_PASSIVE_SCAN |
    3387             :                              HOSTAPD_CHAN_NO_IBSS))
    3388      146195 :                                 return PASSIVE_ONLY;
    3389       38286 :                         return ALLOWED;
    3390             :                 }
    3391             :         }
    3392             : 
    3393       35022 :         return NOT_ALLOWED;
    3394             : }
    3395             : 
    3396             : 
    3397             : struct p2p_oper_class_map {
    3398             :         enum hostapd_hw_mode mode;
    3399             :         u8 op_class;
    3400             :         u8 min_chan;
    3401             :         u8 max_chan;
    3402             :         u8 inc;
    3403             :         enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
    3404             : };
    3405             : 
    3406             : static struct p2p_oper_class_map op_class[] = {
    3407             :         { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
    3408             : #if 0 /* Do not enable HT40 on 2 GHz for now */
    3409             :         { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
    3410             :         { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
    3411             : #endif
    3412             :         { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
    3413             :         { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
    3414             :         { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
    3415             :         { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
    3416             :         { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
    3417             :         { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
    3418             : 
    3419             :         /*
    3420             :          * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
    3421             :          * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
    3422             :          * 80 MHz, but currently use the following definition for simplicity
    3423             :          * (these center frequencies are not actual channels, which makes
    3424             :          * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
    3425             :          * removing invalid channels.
    3426             :          */
    3427             :         { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
    3428             :         { -1, 0, 0, 0, 0, BW20 }
    3429             : };
    3430             : 
    3431             : 
    3432       86944 : static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
    3433             :                                      struct hostapd_hw_modes *mode,
    3434             :                                      u8 channel)
    3435             : {
    3436       86944 :         u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
    3437             :         unsigned int i;
    3438             : 
    3439       86944 :         if (mode->mode != HOSTAPD_MODE_IEEE80211A)
    3440           0 :                 return 0;
    3441             : 
    3442      383097 :         for (i = 0; i < ARRAY_SIZE(center_channels); i++)
    3443             :                 /*
    3444             :                  * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
    3445             :                  * so the center channel is 6 channels away from the start/end.
    3446             :                  */
    3447      627627 :                 if (channel >= center_channels[i] - 6 &&
    3448      268983 :                     channel <= center_channels[i] + 6)
    3449       62491 :                         return center_channels[i];
    3450             : 
    3451       24453 :         return 0;
    3452             : }
    3453             : 
    3454             : 
    3455       86944 : static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
    3456             :                                                struct hostapd_hw_modes *mode,
    3457             :                                                u8 channel, u8 bw)
    3458             : {
    3459             :         u8 center_chan;
    3460             :         int i, flags;
    3461       86944 :         enum chan_allowed res, ret = ALLOWED;
    3462             : 
    3463       86944 :         center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
    3464       86944 :         if (!center_chan)
    3465       24453 :                 return NOT_ALLOWED;
    3466       62491 :         if (center_chan >= 58 && center_chan <= 138)
    3467       43472 :                 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
    3468             : 
    3469             :         /* check all the channels are available */
    3470       93635 :         for (i = 0; i < 4; i++) {
    3471       74981 :                 int adj_chan = center_chan - 6 + i * 4;
    3472             : 
    3473       74981 :                 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
    3474       74981 :                 if (res == NOT_ALLOWED)
    3475         356 :                         return NOT_ALLOWED;
    3476       74625 :                 if (res == PASSIVE_ONLY)
    3477       71801 :                         ret = PASSIVE_ONLY;
    3478             : 
    3479       74625 :                 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
    3480           9 :                         return NOT_ALLOWED;
    3481       74616 :                 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
    3482           0 :                         return NOT_ALLOWED;
    3483       74616 :                 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
    3484           0 :                         return NOT_ALLOWED;
    3485       74616 :                 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
    3486           0 :                         return NOT_ALLOWED;
    3487             :         }
    3488             : 
    3489       18654 :         return ret;
    3490             : }
    3491             : 
    3492             : 
    3493      165737 : static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
    3494             :                                                  struct hostapd_hw_modes *mode,
    3495             :                                                  u8 channel, u8 bw)
    3496             : {
    3497      165737 :         int flag = 0;
    3498             :         enum chan_allowed res, res2;
    3499             : 
    3500      165737 :         res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
    3501      165737 :         if (bw == BW40MINUS) {
    3502       10868 :                 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
    3503         224 :                         return NOT_ALLOWED;
    3504       10644 :                 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
    3505      154869 :         } else if (bw == BW40PLUS) {
    3506       10868 :                 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
    3507         224 :                         return NOT_ALLOWED;
    3508       10644 :                 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
    3509      144001 :         } else if (bw == BW80) {
    3510       86944 :                 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
    3511             :         }
    3512             : 
    3513      165289 :         if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
    3514       76745 :                 return NOT_ALLOWED;
    3515       88544 :         if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
    3516       53870 :                 return PASSIVE_ONLY;
    3517       34674 :         return res;
    3518             : }
    3519             : 
    3520             : 
    3521        2717 : static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
    3522             :                                    struct p2p_channels *chan,
    3523             :                                    struct p2p_channels *cli_chan)
    3524             : {
    3525             :         struct hostapd_hw_modes *mode;
    3526             :         int cla, op, cli_cla;
    3527             : 
    3528        2717 :         if (wpa_s->hw.modes == NULL) {
    3529           0 :                 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
    3530             :                            "of all supported channels; assume dualband "
    3531             :                            "support");
    3532           0 :                 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
    3533             :         }
    3534             : 
    3535        2717 :         cla = cli_cla = 0;
    3536             : 
    3537       24453 :         for (op = 0; op_class[op].op_class; op++) {
    3538       21736 :                 struct p2p_oper_class_map *o = &op_class[op];
    3539             :                 u8 ch;
    3540       21736 :                 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
    3541             : 
    3542       21736 :                 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
    3543       21736 :                 if (mode == NULL)
    3544           0 :                         continue;
    3545      187473 :                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
    3546             :                         enum chan_allowed res;
    3547      165737 :                         res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
    3548      165737 :                         if (res == ALLOWED) {
    3549       34674 :                                 if (reg == NULL) {
    3550        3423 :                                         wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
    3551        3423 :                                                    o->op_class);
    3552        3423 :                                         reg = &chan->reg_class[cla];
    3553        3423 :                                         cla++;
    3554        3423 :                                         reg->reg_class = o->op_class;
    3555             :                                 }
    3556       34674 :                                 reg->channel[reg->channels] = ch;
    3557       34674 :                                 reg->channels++;
    3558      184933 :                         } else if (res == PASSIVE_ONLY &&
    3559       53870 :                                    wpa_s->conf->p2p_add_cli_chan) {
    3560         294 :                                 if (cli_reg == NULL) {
    3561         112 :                                         wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
    3562         112 :                                                    o->op_class);
    3563         112 :                                         cli_reg = &cli_chan->reg_class[cli_cla];
    3564         112 :                                         cli_cla++;
    3565         112 :                                         cli_reg->reg_class = o->op_class;
    3566             :                                 }
    3567         294 :                                 cli_reg->channel[cli_reg->channels] = ch;
    3568         294 :                                 cli_reg->channels++;
    3569             :                         }
    3570             :                 }
    3571       21736 :                 if (reg) {
    3572        6846 :                         wpa_hexdump(MSG_DEBUG, "P2P: Channels",
    3573        3423 :                                     reg->channel, reg->channels);
    3574             :                 }
    3575       21736 :                 if (cli_reg) {
    3576         224 :                         wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
    3577         112 :                                     cli_reg->channel, cli_reg->channels);
    3578             :                 }
    3579             :         }
    3580             : 
    3581        2717 :         chan->reg_classes = cla;
    3582        2717 :         cli_chan->reg_classes = cli_cla;
    3583             : 
    3584        2717 :         return 0;
    3585             : }
    3586             : 
    3587             : 
    3588           0 : int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
    3589             :                            struct hostapd_hw_modes *mode, u8 channel)
    3590             : {
    3591             :         int op;
    3592             :         enum chan_allowed ret;
    3593             : 
    3594           0 :         for (op = 0; op_class[op].op_class; op++) {
    3595           0 :                 struct p2p_oper_class_map *o = &op_class[op];
    3596             :                 u8 ch;
    3597             : 
    3598           0 :                 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
    3599           0 :                         if (o->mode != HOSTAPD_MODE_IEEE80211A ||
    3600           0 :                             o->bw == BW20 || ch != channel)
    3601           0 :                                 continue;
    3602           0 :                         ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
    3603           0 :                         if (ret == ALLOWED)
    3604           0 :                                 return (o->bw == BW40MINUS) ? -1 : 1;
    3605             :                 }
    3606             :         }
    3607           0 :         return 0;
    3608             : }
    3609             : 
    3610             : 
    3611           0 : int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
    3612             :                               struct hostapd_hw_modes *mode, u8 channel)
    3613             : {
    3614           0 :         if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
    3615           0 :                 return 0;
    3616             : 
    3617           0 :         return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
    3618             : }
    3619             : 
    3620             : 
    3621           2 : static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
    3622             :                         size_t buf_len)
    3623             : {
    3624           2 :         struct wpa_supplicant *wpa_s = ctx;
    3625             : 
    3626           2 :         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    3627           2 :                 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
    3628           2 :                         break;
    3629             :         }
    3630           2 :         if (wpa_s == NULL)
    3631           0 :                 return -1;
    3632             : 
    3633           2 :         return wpa_drv_get_noa(wpa_s, buf, buf_len);
    3634             : }
    3635             : 
    3636             : 
    3637           1 : static int wpas_go_connected(void *ctx, const u8 *dev_addr)
    3638             : {
    3639           1 :         struct wpa_supplicant *wpa_s = ctx;
    3640             : 
    3641           2 :         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    3642           1 :                 struct wpa_ssid *ssid = wpa_s->current_ssid;
    3643           1 :                 if (ssid == NULL)
    3644           1 :                         continue;
    3645           0 :                 if (ssid->mode != WPAS_MODE_INFRA)
    3646           0 :                         continue;
    3647           0 :                 if (wpa_s->wpa_state != WPA_COMPLETED &&
    3648           0 :                     wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
    3649           0 :                         continue;
    3650           0 :                 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
    3651           0 :                         return 1;
    3652             :         }
    3653             : 
    3654           1 :         return 0;
    3655             : }
    3656             : 
    3657             : 
    3658         272 : static int wpas_is_concurrent_session_active(void *ctx)
    3659             : {
    3660         272 :         struct wpa_supplicant *wpa_s = ctx;
    3661             :         struct wpa_supplicant *ifs;
    3662             : 
    3663         544 :         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
    3664         272 :                 if (ifs == wpa_s)
    3665         272 :                         continue;
    3666           0 :                 if (ifs->wpa_state > WPA_ASSOCIATED)
    3667           0 :                         return 1;
    3668             :         }
    3669         272 :         return 0;
    3670             : }
    3671             : 
    3672             : 
    3673       49723 : static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
    3674             : {
    3675       49723 :         struct wpa_supplicant *wpa_s = ctx;
    3676       49723 :         wpa_msg_global(wpa_s, level, "P2P: %s", msg);
    3677       49723 : }
    3678             : 
    3679             : 
    3680           0 : int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
    3681             : {
    3682             :         struct wpa_interface iface;
    3683             :         struct wpa_supplicant *p2pdev_wpa_s;
    3684             :         char ifname[100];
    3685             :         char force_name[100];
    3686             :         int ret;
    3687             : 
    3688           0 :         os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
    3689           0 :                     wpa_s->ifname);
    3690           0 :         force_name[0] = '\0';
    3691           0 :         wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
    3692           0 :         ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
    3693           0 :                              force_name, wpa_s->pending_interface_addr, NULL);
    3694           0 :         if (ret < 0) {
    3695           0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
    3696           0 :                 return ret;
    3697             :         }
    3698           0 :         os_strlcpy(wpa_s->pending_interface_name, ifname,
    3699             :                    sizeof(wpa_s->pending_interface_name));
    3700             : 
    3701           0 :         os_memset(&iface, 0, sizeof(iface));
    3702           0 :         iface.p2p_mgmt = 1;
    3703           0 :         iface.ifname = wpa_s->pending_interface_name;
    3704           0 :         iface.driver = wpa_s->driver->name;
    3705           0 :         iface.driver_param = wpa_s->conf->driver_param;
    3706             : 
    3707             :         /*
    3708             :          * If a P2P Device configuration file was given, use it as the interface
    3709             :          * configuration file (instead of using parent's configuration file.
    3710             :          */
    3711           0 :         if (wpa_s->conf_p2p_dev) {
    3712           0 :                 iface.confname = wpa_s->conf_p2p_dev;
    3713           0 :                 iface.ctrl_interface = NULL;
    3714             :         } else {
    3715           0 :                 iface.confname = wpa_s->confname;
    3716           0 :                 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
    3717             :         }
    3718           0 :         iface.conf_p2p_dev = NULL;
    3719             : 
    3720           0 :         p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
    3721           0 :         if (!p2pdev_wpa_s) {
    3722           0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
    3723           0 :                 return -1;
    3724             :         }
    3725           0 :         p2pdev_wpa_s->parent = wpa_s;
    3726             : 
    3727           0 :         wpa_s->pending_interface_name[0] = '\0';
    3728           0 :         return 0;
    3729             : }
    3730             : 
    3731             : 
    3732           1 : static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
    3733             :                                const u8 *noa, size_t noa_len)
    3734             : {
    3735           1 :         struct wpa_supplicant *wpa_s, *intf = ctx;
    3736             :         char hex[100];
    3737             : 
    3738           1 :         for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    3739           1 :                 if (wpa_s->waiting_presence_resp)
    3740           1 :                         break;
    3741             :         }
    3742           1 :         if (!wpa_s) {
    3743           0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
    3744           1 :                 return;
    3745             :         }
    3746           1 :         wpa_s->waiting_presence_resp = 0;
    3747             : 
    3748           1 :         wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
    3749           7 :         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
    3750           6 :                 " status=%u noa=%s", MAC2STR(src), status, hex);
    3751             : }
    3752             : 
    3753             : 
    3754           0 : static int _wpas_p2p_in_progress(void *ctx)
    3755             : {
    3756           0 :         struct wpa_supplicant *wpa_s = ctx;
    3757           0 :         return wpas_p2p_in_progress(wpa_s);
    3758             : }
    3759             : 
    3760             : 
    3761             : /**
    3762             :  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
    3763             :  * @global: Pointer to global data from wpa_supplicant_init()
    3764             :  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
    3765             :  * Returns: 0 on success, -1 on failure
    3766             :  */
    3767          69 : int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
    3768             : {
    3769             :         struct p2p_config p2p;
    3770             :         unsigned int r;
    3771             :         int i;
    3772             : 
    3773          69 :         if (wpa_s->conf->p2p_disabled)
    3774           0 :                 return 0;
    3775             : 
    3776          69 :         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
    3777           0 :                 return 0;
    3778             : 
    3779          69 :         if (global->p2p)
    3780          35 :                 return 0;
    3781             : 
    3782          34 :         os_memset(&p2p, 0, sizeof(p2p));
    3783          34 :         p2p.cb_ctx = wpa_s;
    3784          34 :         p2p.debug_print = wpas_p2p_debug_print;
    3785          34 :         p2p.p2p_scan = wpas_p2p_scan;
    3786          34 :         p2p.send_action = wpas_send_action;
    3787          34 :         p2p.send_action_done = wpas_send_action_done;
    3788          34 :         p2p.go_neg_completed = wpas_go_neg_completed;
    3789          34 :         p2p.go_neg_req_rx = wpas_go_neg_req_rx;
    3790          34 :         p2p.dev_found = wpas_dev_found;
    3791          34 :         p2p.dev_lost = wpas_dev_lost;
    3792          34 :         p2p.find_stopped = wpas_find_stopped;
    3793          34 :         p2p.start_listen = wpas_start_listen;
    3794          34 :         p2p.stop_listen = wpas_stop_listen;
    3795          34 :         p2p.send_probe_resp = wpas_send_probe_resp;
    3796          34 :         p2p.sd_request = wpas_sd_request;
    3797          34 :         p2p.sd_response = wpas_sd_response;
    3798          34 :         p2p.prov_disc_req = wpas_prov_disc_req;
    3799          34 :         p2p.prov_disc_resp = wpas_prov_disc_resp;
    3800          34 :         p2p.prov_disc_fail = wpas_prov_disc_fail;
    3801          34 :         p2p.invitation_process = wpas_invitation_process;
    3802          34 :         p2p.invitation_received = wpas_invitation_received;
    3803          34 :         p2p.invitation_result = wpas_invitation_result;
    3804          34 :         p2p.get_noa = wpas_get_noa;
    3805          34 :         p2p.go_connected = wpas_go_connected;
    3806          34 :         p2p.presence_resp = wpas_presence_resp;
    3807          34 :         p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
    3808          34 :         p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
    3809             : 
    3810          34 :         os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
    3811          34 :         os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
    3812          34 :         p2p.dev_name = wpa_s->conf->device_name;
    3813          34 :         p2p.manufacturer = wpa_s->conf->manufacturer;
    3814          34 :         p2p.model_name = wpa_s->conf->model_name;
    3815          34 :         p2p.model_number = wpa_s->conf->model_number;
    3816          34 :         p2p.serial_number = wpa_s->conf->serial_number;
    3817          34 :         if (wpa_s->wps) {
    3818          34 :                 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
    3819          34 :                 p2p.config_methods = wpa_s->wps->config_methods;
    3820             :         }
    3821             : 
    3822          34 :         if (wpa_s->conf->p2p_listen_reg_class &&
    3823           0 :             wpa_s->conf->p2p_listen_channel) {
    3824           0 :                 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
    3825           0 :                 p2p.channel = wpa_s->conf->p2p_listen_channel;
    3826             :         } else {
    3827          34 :                 p2p.reg_class = 81;
    3828             :                 /*
    3829             :                  * Pick one of the social channels randomly as the listen
    3830             :                  * channel.
    3831             :                  */
    3832          34 :                 os_get_random((u8 *) &r, sizeof(r));
    3833          34 :                 p2p.channel = 1 + (r % 3) * 5;
    3834             :         }
    3835          34 :         wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
    3836             : 
    3837          34 :         if (wpa_s->conf->p2p_oper_reg_class &&
    3838           0 :             wpa_s->conf->p2p_oper_channel) {
    3839           0 :                 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
    3840           0 :                 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
    3841           0 :                 p2p.cfg_op_channel = 1;
    3842           0 :                 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
    3843           0 :                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
    3844             : 
    3845             :         } else {
    3846          34 :                 p2p.op_reg_class = 81;
    3847             :                 /*
    3848             :                  * Use random operation channel from (1, 6, 11) if no other
    3849             :                  * preference is indicated.
    3850             :                  */
    3851          34 :                 os_get_random((u8 *) &r, sizeof(r));
    3852          34 :                 p2p.op_channel = 1 + (r % 3) * 5;
    3853          34 :                 p2p.cfg_op_channel = 0;
    3854          68 :                 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
    3855          68 :                            "%d:%d", p2p.op_reg_class, p2p.op_channel);
    3856             :         }
    3857             : 
    3858          34 :         if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
    3859           0 :                 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
    3860           0 :                 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
    3861             :         }
    3862             : 
    3863          34 :         if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
    3864           0 :                 os_memcpy(p2p.country, wpa_s->conf->country, 2);
    3865           0 :                 p2p.country[2] = 0x04;
    3866             :         } else
    3867          34 :                 os_memcpy(p2p.country, "XX\x04", 3);
    3868             : 
    3869          34 :         if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
    3870           0 :                 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
    3871             :                            "channel list");
    3872           0 :                 return -1;
    3873             :         }
    3874             : 
    3875          34 :         os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
    3876             :                   WPS_DEV_TYPE_LEN);
    3877             : 
    3878          34 :         p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
    3879          34 :         os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
    3880             :                   p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
    3881             : 
    3882          34 :         p2p.concurrent_operations = !!(wpa_s->drv_flags &
    3883             :                                        WPA_DRIVER_FLAGS_P2P_CONCURRENT);
    3884             : 
    3885          34 :         p2p.max_peers = 100;
    3886             : 
    3887          34 :         if (wpa_s->conf->p2p_ssid_postfix) {
    3888           0 :                 p2p.ssid_postfix_len =
    3889           0 :                         os_strlen(wpa_s->conf->p2p_ssid_postfix);
    3890           0 :                 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
    3891           0 :                         p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
    3892           0 :                 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
    3893             :                           p2p.ssid_postfix_len);
    3894             :         }
    3895             : 
    3896          34 :         p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
    3897             : 
    3898          34 :         p2p.max_listen = wpa_s->max_remain_on_chan;
    3899             : 
    3900          34 :         global->p2p = p2p_init(&p2p);
    3901          34 :         if (global->p2p == NULL)
    3902           0 :                 return -1;
    3903          34 :         global->p2p_init_wpa_s = wpa_s;
    3904             : 
    3905         374 :         for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
    3906         340 :                 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
    3907         340 :                         continue;
    3908           0 :                 p2p_add_wps_vendor_extension(
    3909           0 :                         global->p2p, wpa_s->conf->wps_vendor_ext[i]);
    3910             :         }
    3911             : 
    3912          34 :         p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
    3913             : 
    3914          34 :         return 0;
    3915             : }
    3916             : 
    3917             : 
    3918             : /**
    3919             :  * wpas_p2p_deinit - Deinitialize per-interface P2P data
    3920             :  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
    3921             :  *
    3922             :  * This function deinitialize per-interface P2P data.
    3923             :  */
    3924          78 : void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
    3925             : {
    3926          78 :         if (wpa_s->driver && wpa_s->drv_priv)
    3927          69 :                 wpa_drv_probe_req_report(wpa_s, 0);
    3928             : 
    3929          78 :         if (wpa_s->go_params) {
    3930             :                 /* Clear any stored provisioning info */
    3931          26 :                 p2p_clear_provisioning_info(
    3932          26 :                         wpa_s->global->p2p,
    3933          26 :                         wpa_s->go_params->peer_device_addr);
    3934             :         }
    3935             : 
    3936          78 :         os_free(wpa_s->go_params);
    3937          78 :         wpa_s->go_params = NULL;
    3938          78 :         eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
    3939          78 :         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
    3940          78 :         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
    3941          78 :         wpa_s->p2p_long_listen = 0;
    3942          78 :         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
    3943          78 :         eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
    3944          78 :         wpas_p2p_remove_pending_group_interface(wpa_s);
    3945          78 :         eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
    3946          78 :         wpas_p2p_listen_work_done(wpa_s);
    3947          78 :         if (wpa_s->p2p_send_action_work) {
    3948           0 :                 os_free(wpa_s->p2p_send_action_work->ctx);
    3949           0 :                 radio_work_done(wpa_s->p2p_send_action_work);
    3950           0 :                 wpa_s->p2p_send_action_work = NULL;
    3951             :         }
    3952          78 :         eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
    3953             : 
    3954          78 :         wpabuf_free(wpa_s->p2p_oob_dev_pw);
    3955          78 :         wpa_s->p2p_oob_dev_pw = NULL;
    3956             : 
    3957             :         /* TODO: remove group interface from the driver if this wpa_s instance
    3958             :          * is on top of a P2P group interface */
    3959          78 : }
    3960             : 
    3961             : 
    3962             : /**
    3963             :  * wpas_p2p_deinit_global - Deinitialize global P2P module
    3964             :  * @global: Pointer to global data from wpa_supplicant_init()
    3965             :  *
    3966             :  * This function deinitializes the global (per device) P2P module.
    3967             :  */
    3968          34 : void wpas_p2p_deinit_global(struct wpa_global *global)
    3969             : {
    3970             :         struct wpa_supplicant *wpa_s, *tmp;
    3971             : 
    3972          34 :         wpa_s = global->ifaces;
    3973          34 :         if (wpa_s)
    3974           0 :                 wpas_p2p_service_flush(wpa_s);
    3975             : 
    3976          34 :         if (global->p2p == NULL)
    3977          34 :                 return;
    3978             : 
    3979             :         /* Remove remaining P2P group interfaces */
    3980          68 :         while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
    3981           0 :                 wpa_s = wpa_s->next;
    3982          68 :         while (wpa_s) {
    3983           0 :                 tmp = global->ifaces;
    3984           0 :                 while (tmp &&
    3985           0 :                        (tmp == wpa_s ||
    3986           0 :                         tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
    3987           0 :                         tmp = tmp->next;
    3988             :                 }
    3989           0 :                 if (tmp == NULL)
    3990           0 :                         break;
    3991             :                 /* Disconnect from the P2P group and deinit the interface */
    3992           0 :                 wpas_p2p_disconnect(tmp);
    3993             :         }
    3994             : 
    3995             :         /*
    3996             :          * Deinit GO data on any possibly remaining interface (if main
    3997             :          * interface is used as GO).
    3998             :          */
    3999          34 :         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    4000           0 :                 if (wpa_s->ap_iface)
    4001           0 :                         wpas_p2p_group_deinit(wpa_s);
    4002             :         }
    4003             : 
    4004          34 :         p2p_deinit(global->p2p);
    4005          34 :         global->p2p = NULL;
    4006          34 :         global->p2p_init_wpa_s = NULL;
    4007             : }
    4008             : 
    4009             : 
    4010         301 : static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
    4011             : {
    4012         602 :         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
    4013         301 :             wpa_s->conf->p2p_no_group_iface)
    4014         261 :                 return 0; /* separate interface disabled per configuration */
    4015          40 :         if (wpa_s->drv_flags &
    4016             :             (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
    4017             :              WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
    4018          40 :                 return 1; /* P2P group requires a new interface in every case
    4019             :                            */
    4020           0 :         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
    4021           0 :                 return 0; /* driver does not support concurrent operations */
    4022           0 :         if (wpa_s->global->ifaces->next)
    4023           0 :                 return 1; /* more that one interface already in use */
    4024           0 :         if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
    4025           0 :                 return 1; /* this interface is already in use */
    4026           0 :         return 0;
    4027             : }
    4028             : 
    4029             : 
    4030         101 : static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
    4031             :                                  const u8 *peer_addr,
    4032             :                                  enum p2p_wps_method wps_method,
    4033             :                                  int go_intent, const u8 *own_interface_addr,
    4034             :                                  unsigned int force_freq, int persistent_group,
    4035             :                                  struct wpa_ssid *ssid, unsigned int pref_freq)
    4036             : {
    4037         101 :         if (persistent_group && wpa_s->conf->persistent_reconnect)
    4038          10 :                 persistent_group = 2;
    4039             : 
    4040             :         /*
    4041             :          * Increase GO config timeout if HT40 is used since it takes some time
    4042             :          * to scan channels for coex purposes before the BSS can be started.
    4043             :          */
    4044         101 :         p2p_set_config_timeout(wpa_s->global->p2p,
    4045         101 :                                wpa_s->p2p_go_ht40 ? 255 : 100, 20);
    4046             : 
    4047         210 :         return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
    4048             :                            go_intent, own_interface_addr, force_freq,
    4049             :                            persistent_group, ssid ? ssid->ssid : NULL,
    4050             :                            ssid ? ssid->ssid_len : 0,
    4051         101 :                            wpa_s->p2p_pd_before_go_neg, pref_freq,
    4052           8 :                            wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
    4053             :                            0);
    4054             : }
    4055             : 
    4056             : 
    4057          62 : static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
    4058             :                                 const u8 *peer_addr,
    4059             :                                 enum p2p_wps_method wps_method,
    4060             :                                 int go_intent, const u8 *own_interface_addr,
    4061             :                                 unsigned int force_freq, int persistent_group,
    4062             :                                 struct wpa_ssid *ssid, unsigned int pref_freq)
    4063             : {
    4064          62 :         if (persistent_group && wpa_s->conf->persistent_reconnect)
    4065           9 :                 persistent_group = 2;
    4066             : 
    4067          66 :         return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
    4068             :                              go_intent, own_interface_addr, force_freq,
    4069             :                              persistent_group, ssid ? ssid->ssid : NULL,
    4070             :                              ssid ? ssid->ssid_len : 0, pref_freq,
    4071           4 :                              wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
    4072             :                              0);
    4073             : }
    4074             : 
    4075             : 
    4076           4 : static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
    4077             : {
    4078           4 :         wpa_s->p2p_join_scan_count++;
    4079           4 :         wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
    4080             :                    wpa_s->p2p_join_scan_count);
    4081           4 :         if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
    4082           0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
    4083             :                            " for join operationg - stop join attempt",
    4084           0 :                            MAC2STR(wpa_s->pending_join_iface_addr));
    4085           0 :                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
    4086           0 :                 if (wpa_s->p2p_auto_pd) {
    4087           0 :                         wpa_s->p2p_auto_pd = 0;
    4088           0 :                         wpa_msg_global(wpa_s, MSG_INFO,
    4089             :                                        P2P_EVENT_PROV_DISC_FAILURE
    4090             :                                        " p2p_dev_addr=" MACSTR " status=N/A",
    4091           0 :                                        MAC2STR(wpa_s->pending_join_dev_addr));
    4092           4 :                         return;
    4093             :                 }
    4094           0 :                 wpa_msg_global(wpa_s->parent, MSG_INFO,
    4095             :                                P2P_EVENT_GROUP_FORMATION_FAILURE);
    4096             :         }
    4097             : }
    4098             : 
    4099             : 
    4100          35 : static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
    4101             : {
    4102             :         int *freqs, res, num, i;
    4103             : 
    4104          35 :         if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
    4105             :                 /* Multiple channels are supported and not all are in use */
    4106          34 :                 return 0;
    4107             :         }
    4108             : 
    4109           1 :         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
    4110           1 :         if (!freqs)
    4111           0 :                 return 1;
    4112             : 
    4113           1 :         num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
    4114             :                                         wpa_s->num_multichan_concurrent);
    4115           1 :         if (num < 0) {
    4116           0 :                 res = 1;
    4117           0 :                 goto exit_free;
    4118             :         }
    4119             : 
    4120           1 :         for (i = 0; i < num; i++) {
    4121           1 :                 if (freqs[i] == freq) {
    4122           1 :                         wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
    4123             :                                    freq);
    4124           1 :                         res = 0;
    4125           1 :                         goto exit_free;
    4126             :                 }
    4127             :         }
    4128             : 
    4129           0 :         res = 1;
    4130             : 
    4131             : exit_free:
    4132           1 :         os_free(freqs);
    4133           1 :         return res;
    4134             : }
    4135             : 
    4136             : 
    4137           0 : static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
    4138             :                             const u8 *peer_dev_addr)
    4139             : {
    4140             :         struct wpa_bss *bss;
    4141             :         int updated;
    4142             : 
    4143           0 :         bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
    4144           0 :         if (bss == NULL)
    4145           0 :                 return -1;
    4146           0 :         if (bss->last_update_idx < wpa_s->bss_update_idx) {
    4147           0 :                 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
    4148             :                            "last scan");
    4149           0 :                 return 0;
    4150             :         }
    4151             : 
    4152           0 :         updated = os_reltime_before(&wpa_s->p2p_auto_started,
    4153             :                                     &bss->last_update);
    4154           0 :         wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
    4155             :                    "%ld.%06ld (%supdated in last scan)",
    4156             :                    bss->last_update.sec, bss->last_update.usec,
    4157             :                    updated ? "": "not ");
    4158             : 
    4159           0 :         return updated;
    4160             : }
    4161             : 
    4162             : 
    4163          35 : static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
    4164             :                                    struct wpa_scan_results *scan_res)
    4165             : {
    4166          35 :         struct wpa_bss *bss = NULL;
    4167             :         int freq;
    4168             :         u8 iface_addr[ETH_ALEN];
    4169             : 
    4170          35 :         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
    4171             : 
    4172          35 :         if (wpa_s->global->p2p_disabled)
    4173          33 :                 return;
    4174             : 
    4175          70 :         wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
    4176          35 :                    scan_res ? (int) scan_res->num : -1,
    4177          35 :                    wpa_s->p2p_auto_join ? "auto_" : "");
    4178             : 
    4179          35 :         if (scan_res)
    4180          35 :                 wpas_p2p_scan_res_handler(wpa_s, scan_res);
    4181             : 
    4182          35 :         if (wpa_s->p2p_auto_pd) {
    4183           0 :                 int join = wpas_p2p_peer_go(wpa_s,
    4184           0 :                                             wpa_s->pending_join_dev_addr);
    4185           0 :                 if (join == 0 &&
    4186           0 :                     wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
    4187           0 :                         wpa_s->auto_pd_scan_retry++;
    4188           0 :                         bss = wpa_bss_get_bssid_latest(
    4189           0 :                                 wpa_s, wpa_s->pending_join_dev_addr);
    4190           0 :                         if (bss) {
    4191           0 :                                 freq = bss->freq;
    4192           0 :                                 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
    4193             :                                            "the peer " MACSTR " at %d MHz",
    4194             :                                            wpa_s->auto_pd_scan_retry,
    4195           0 :                                            MAC2STR(wpa_s->
    4196             :                                                    pending_join_dev_addr),
    4197             :                                            freq);
    4198           0 :                                 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
    4199           0 :                                 return;
    4200             :                         }
    4201             :                 }
    4202             : 
    4203           0 :                 if (join < 0)
    4204           0 :                         join = 0;
    4205             : 
    4206           0 :                 wpa_s->p2p_auto_pd = 0;
    4207           0 :                 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
    4208           0 :                 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
    4209           0 :                            MAC2STR(wpa_s->pending_join_dev_addr), join);
    4210           0 :                 if (p2p_prov_disc_req(wpa_s->global->p2p,
    4211           0 :                                       wpa_s->pending_join_dev_addr,
    4212           0 :                                       wpa_s->pending_pd_config_methods, join,
    4213           0 :                                       0, wpa_s->user_initiated_pd) < 0) {
    4214           0 :                         wpa_s->p2p_auto_pd = 0;
    4215           0 :                         wpa_msg_global(wpa_s, MSG_INFO,
    4216             :                                        P2P_EVENT_PROV_DISC_FAILURE
    4217             :                                        " p2p_dev_addr=" MACSTR " status=N/A",
    4218           0 :                                        MAC2STR(wpa_s->pending_join_dev_addr));
    4219             :                 }
    4220           0 :                 return;
    4221             :         }
    4222             : 
    4223          35 :         if (wpa_s->p2p_auto_join) {
    4224           0 :                 int join = wpas_p2p_peer_go(wpa_s,
    4225           0 :                                             wpa_s->pending_join_dev_addr);
    4226           0 :                 if (join < 0) {
    4227           0 :                         wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
    4228             :                                    "running a GO -> use GO Negotiation");
    4229           0 :                         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
    4230           0 :                                          wpa_s->p2p_pin, wpa_s->p2p_wps_method,
    4231           0 :                                          wpa_s->p2p_persistent_group, 0, 0, 0,
    4232             :                                          wpa_s->p2p_go_intent,
    4233             :                                          wpa_s->p2p_connect_freq,
    4234             :                                          wpa_s->p2p_persistent_id,
    4235           0 :                                          wpa_s->p2p_pd_before_go_neg,
    4236           0 :                                          wpa_s->p2p_go_ht40,
    4237           0 :                                          wpa_s->p2p_go_vht);
    4238           0 :                         return;
    4239             :                 }
    4240             : 
    4241           0 :                 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
    4242             :                            "try to join the group", join ? "" :
    4243             :                            " in older scan");
    4244           0 :                 if (!join)
    4245           0 :                         wpa_s->p2p_fallback_to_go_neg = 1;
    4246             :         }
    4247             : 
    4248          35 :         freq = p2p_get_oper_freq(wpa_s->global->p2p,
    4249          35 :                                  wpa_s->pending_join_iface_addr);
    4250          70 :         if (freq < 0 &&
    4251          35 :             p2p_get_interface_addr(wpa_s->global->p2p,
    4252          35 :                                    wpa_s->pending_join_dev_addr,
    4253           4 :                                    iface_addr) == 0 &&
    4254           4 :             os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
    4255             :         {
    4256          36 :                 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
    4257             :                            "address for join from " MACSTR " to " MACSTR
    4258             :                            " based on newly discovered P2P peer entry",
    4259          18 :                            MAC2STR(wpa_s->pending_join_iface_addr),
    4260          18 :                            MAC2STR(iface_addr));
    4261           3 :                 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
    4262             :                           ETH_ALEN);
    4263             : 
    4264           3 :                 freq = p2p_get_oper_freq(wpa_s->global->p2p,
    4265           3 :                                          wpa_s->pending_join_iface_addr);
    4266             :         }
    4267          35 :         if (freq >= 0) {
    4268           0 :                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
    4269             :                            "from P2P peer table: %d MHz", freq);
    4270             :         }
    4271          35 :         if (wpa_s->p2p_join_ssid_len) {
    4272          24 :                 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
    4273             :                            MACSTR " and SSID %s",
    4274          18 :                            MAC2STR(wpa_s->pending_join_iface_addr),
    4275           3 :                            wpa_ssid_txt(wpa_s->p2p_join_ssid,
    4276             :                                         wpa_s->p2p_join_ssid_len));
    4277           6 :                 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
    4278           3 :                                   wpa_s->p2p_join_ssid,
    4279             :                                   wpa_s->p2p_join_ssid_len);
    4280             :         }
    4281          35 :         if (!bss) {
    4282         192 :                 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
    4283         192 :                            MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
    4284          32 :                 bss = wpa_bss_get_bssid_latest(wpa_s,
    4285          32 :                                                wpa_s->pending_join_iface_addr);
    4286             :         }
    4287          35 :         if (bss) {
    4288          35 :                 freq = bss->freq;
    4289          70 :                 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
    4290             :                            "from BSS table: %d MHz (SSID %s)", freq,
    4291          35 :                            wpa_ssid_txt(bss->ssid, bss->ssid_len));
    4292             :         }
    4293          35 :         if (freq > 0) {
    4294             :                 u16 method;
    4295             : 
    4296          35 :                 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
    4297           0 :                         wpa_msg_global(wpa_s->parent, MSG_INFO,
    4298             :                                        P2P_EVENT_GROUP_FORMATION_FAILURE
    4299             :                                        "reason=FREQ_CONFLICT");
    4300           0 :                         return;
    4301             :                 }
    4302             : 
    4303         210 :                 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
    4304             :                            "prior to joining an existing group (GO " MACSTR
    4305             :                            " freq=%u MHz)",
    4306         210 :                            MAC2STR(wpa_s->pending_join_dev_addr), freq);
    4307          35 :                 wpa_s->pending_pd_before_join = 1;
    4308             : 
    4309          35 :                 switch (wpa_s->pending_join_wps_method) {
    4310             :                 case WPS_PIN_DISPLAY:
    4311           0 :                         method = WPS_CONFIG_KEYPAD;
    4312           0 :                         break;
    4313             :                 case WPS_PIN_KEYPAD:
    4314          31 :                         method = WPS_CONFIG_DISPLAY;
    4315          31 :                         break;
    4316             :                 case WPS_PBC:
    4317           2 :                         method = WPS_CONFIG_PUSHBUTTON;
    4318           2 :                         break;
    4319             :                 default:
    4320           2 :                         method = 0;
    4321           2 :                         break;
    4322             :                 }
    4323             : 
    4324          35 :                 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
    4325          35 :                                                wpa_s->pending_join_dev_addr) ==
    4326             :                      method)) {
    4327             :                         /*
    4328             :                          * We have already performed provision discovery for
    4329             :                          * joining the group. Proceed directly to join
    4330             :                          * operation without duplicated provision discovery. */
    4331          12 :                         wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
    4332             :                                    "with " MACSTR " already done - proceed to "
    4333             :                                    "join",
    4334          12 :                                    MAC2STR(wpa_s->pending_join_dev_addr));
    4335           2 :                         wpa_s->pending_pd_before_join = 0;
    4336           2 :                         goto start;
    4337             :                 }
    4338             : 
    4339          66 :                 if (p2p_prov_disc_req(wpa_s->global->p2p,
    4340          33 :                                       wpa_s->pending_join_dev_addr, method, 1,
    4341          33 :                                       freq, wpa_s->user_initiated_pd) < 0) {
    4342           0 :                         wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
    4343             :                                    "Discovery Request before joining an "
    4344             :                                    "existing group");
    4345           0 :                         wpa_s->pending_pd_before_join = 0;
    4346           0 :                         goto start;
    4347             :                 }
    4348          33 :                 return;
    4349             :         }
    4350             : 
    4351           0 :         wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
    4352           0 :         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
    4353           0 :         eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
    4354           0 :         wpas_p2p_check_join_scan_limit(wpa_s);
    4355           0 :         return;
    4356             : 
    4357             : start:
    4358             :         /* Start join operation immediately */
    4359           2 :         wpas_p2p_join_start(wpa_s, 0, NULL, 0);
    4360             : }
    4361             : 
    4362             : 
    4363          39 : static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
    4364             :                                    const u8 *ssid, size_t ssid_len)
    4365             : {
    4366             :         int ret;
    4367             :         struct wpa_driver_scan_params params;
    4368             :         struct wpabuf *wps_ie, *ies;
    4369             :         size_t ielen;
    4370          39 :         int freqs[2] = { 0, 0 };
    4371             : 
    4372          39 :         os_memset(&params, 0, sizeof(params));
    4373             : 
    4374             :         /* P2P Wildcard SSID */
    4375          39 :         params.num_ssids = 1;
    4376          39 :         if (ssid && ssid_len) {
    4377           3 :                 params.ssids[0].ssid = ssid;
    4378           3 :                 params.ssids[0].ssid_len = ssid_len;
    4379           3 :                 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
    4380           3 :                 wpa_s->p2p_join_ssid_len = ssid_len;
    4381             :         } else {
    4382          36 :                 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
    4383          36 :                 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
    4384          36 :                 wpa_s->p2p_join_ssid_len = 0;
    4385             :         }
    4386             : 
    4387          39 :         wpa_s->wps->dev.p2p = 1;
    4388          39 :         wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
    4389          39 :                                         wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
    4390             :                                         NULL);
    4391          39 :         if (wps_ie == NULL) {
    4392           0 :                 wpas_p2p_scan_res_join(wpa_s, NULL);
    4393           0 :                 return;
    4394             :         }
    4395             : 
    4396          39 :         ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
    4397          39 :         ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
    4398          39 :         if (ies == NULL) {
    4399           0 :                 wpabuf_free(wps_ie);
    4400           0 :                 wpas_p2p_scan_res_join(wpa_s, NULL);
    4401           0 :                 return;
    4402             :         }
    4403          39 :         wpabuf_put_buf(ies, wps_ie);
    4404          39 :         wpabuf_free(wps_ie);
    4405             : 
    4406          39 :         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
    4407             : 
    4408          39 :         params.p2p_probe = 1;
    4409          39 :         params.extra_ies = wpabuf_head(ies);
    4410          39 :         params.extra_ies_len = wpabuf_len(ies);
    4411             : 
    4412          39 :         if (!freq) {
    4413             :                 int oper_freq;
    4414             :                 /*
    4415             :                  * If freq is not provided, check the operating freq of the GO
    4416             :                  * and use a single channel scan on if possible.
    4417             :                  */
    4418          36 :                 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
    4419          36 :                                               wpa_s->pending_join_iface_addr);
    4420          36 :                 if (oper_freq > 0)
    4421           0 :                         freq = oper_freq;
    4422             :         }
    4423          39 :         if (freq > 0) {
    4424           3 :                 freqs[0] = freq;
    4425           3 :                 params.freqs = freqs;
    4426             :         }
    4427             : 
    4428             :         /*
    4429             :          * Run a scan to update BSS table and start Provision Discovery once
    4430             :          * the new scan results become available.
    4431             :          */
    4432          39 :         ret = wpa_drv_scan(wpa_s, &params);
    4433          39 :         if (!ret) {
    4434          35 :                 os_get_reltime(&wpa_s->scan_trigger_time);
    4435          35 :                 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
    4436          35 :                 wpa_s->own_scan_requested = 1;
    4437             :         }
    4438             : 
    4439          39 :         wpabuf_free(ies);
    4440             : 
    4441          39 :         if (ret) {
    4442           4 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
    4443             :                            "try again later");
    4444           4 :                 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
    4445           4 :                 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
    4446           4 :                 wpas_p2p_check_join_scan_limit(wpa_s);
    4447             :         }
    4448             : }
    4449             : 
    4450             : 
    4451           4 : static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
    4452             : {
    4453           4 :         struct wpa_supplicant *wpa_s = eloop_ctx;
    4454           4 :         wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
    4455           4 : }
    4456             : 
    4457             : 
    4458          35 : static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
    4459             :                          const u8 *dev_addr, enum p2p_wps_method wps_method,
    4460             :                          int auto_join, int op_freq,
    4461             :                          const u8 *ssid, size_t ssid_len)
    4462             : {
    4463         455 :         wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
    4464             :                    MACSTR " dev " MACSTR " op_freq=%d)%s",
    4465         420 :                    MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
    4466             :                    auto_join ? " (auto_join)" : "");
    4467          35 :         if (ssid && ssid_len) {
    4468           3 :                 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
    4469             :                            wpa_ssid_txt(ssid, ssid_len));
    4470             :         }
    4471             : 
    4472          35 :         wpa_s->p2p_auto_pd = 0;
    4473          35 :         wpa_s->p2p_auto_join = !!auto_join;
    4474          35 :         os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
    4475          35 :         os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
    4476          35 :         wpa_s->pending_join_wps_method = wps_method;
    4477             : 
    4478             :         /* Make sure we are not running find during connection establishment */
    4479          35 :         wpas_p2p_stop_find(wpa_s);
    4480             : 
    4481          35 :         wpa_s->p2p_join_scan_count = 0;
    4482          35 :         wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
    4483          35 :         return 0;
    4484             : }
    4485             : 
    4486             : 
    4487          39 : static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
    4488             :                                const u8 *ssid, size_t ssid_len)
    4489             : {
    4490             :         struct wpa_supplicant *group;
    4491             :         struct p2p_go_neg_results res;
    4492             :         struct wpa_bss *bss;
    4493             : 
    4494          39 :         group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
    4495          39 :         if (group == NULL)
    4496           0 :                 return -1;
    4497          39 :         if (group != wpa_s) {
    4498           6 :                 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
    4499             :                           sizeof(group->p2p_pin));
    4500           6 :                 group->p2p_wps_method = wpa_s->p2p_wps_method;
    4501             :         } else {
    4502             :                 /*
    4503             :                  * Need to mark the current interface for p2p_group_formation
    4504             :                  * when a separate group interface is not used. This is needed
    4505             :                  * to allow p2p_cancel stop a pending p2p_connect-join.
    4506             :                  * wpas_p2p_init_group_interface() addresses this for the case
    4507             :                  * where a separate group interface is used.
    4508             :                  */
    4509          33 :                 wpa_s->global->p2p_group_formation = wpa_s;
    4510             :         }
    4511             : 
    4512          39 :         group->p2p_in_provisioning = 1;
    4513          39 :         group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
    4514             : 
    4515          39 :         os_memset(&res, 0, sizeof(res));
    4516          39 :         os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
    4517          39 :         os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
    4518             :                   ETH_ALEN);
    4519          39 :         res.wps_method = wpa_s->pending_join_wps_method;
    4520          39 :         if (freq && ssid && ssid_len) {
    4521           4 :                 res.freq = freq;
    4522           4 :                 res.ssid_len = ssid_len;
    4523           4 :                 os_memcpy(res.ssid, ssid, ssid_len);
    4524             :         } else {
    4525          35 :                 bss = wpa_bss_get_bssid_latest(wpa_s,
    4526          35 :                                                wpa_s->pending_join_iface_addr);
    4527          35 :                 if (bss) {
    4528          35 :                         res.freq = bss->freq;
    4529          35 :                         res.ssid_len = bss->ssid_len;
    4530          35 :                         os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
    4531          70 :                         wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
    4532             :                                    bss->freq,
    4533          35 :                                    wpa_ssid_txt(bss->ssid, bss->ssid_len));
    4534             :                 }
    4535             :         }
    4536             : 
    4537          39 :         if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
    4538           0 :                 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
    4539             :                            "starting client");
    4540           0 :                 wpa_drv_cancel_remain_on_channel(wpa_s);
    4541           0 :                 wpa_s->off_channel_freq = 0;
    4542           0 :                 wpa_s->roc_waiting_drv_freq = 0;
    4543             :         }
    4544          39 :         wpas_start_wps_enrollee(group, &res);
    4545             : 
    4546             :         /*
    4547             :          * Allow a longer timeout for join-a-running-group than normal 15
    4548             :          * second group formation timeout since the GO may not have authorized
    4549             :          * our connection yet.
    4550             :          */
    4551          39 :         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
    4552          39 :         eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
    4553             :                                wpa_s, NULL);
    4554             : 
    4555          39 :         return 0;
    4556             : }
    4557             : 
    4558             : 
    4559         186 : static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
    4560             :                                 int *force_freq, int *pref_freq, int go)
    4561             : {
    4562             :         int *freqs, res;
    4563         186 :         unsigned int freq_in_use = 0, num, i;
    4564             : 
    4565         186 :         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
    4566         186 :         if (!freqs)
    4567           0 :                 return -1;
    4568             : 
    4569         186 :         num = get_shared_radio_freqs(wpa_s, freqs,
    4570             :                                      wpa_s->num_multichan_concurrent);
    4571         186 :         wpa_printf(MSG_DEBUG,
    4572             :                    "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
    4573             :                    freq, wpa_s->num_multichan_concurrent, num);
    4574             : 
    4575         186 :         if (freq > 0) {
    4576             :                 int ret;
    4577          29 :                 if (go)
    4578          12 :                         ret = p2p_supported_freq(wpa_s->global->p2p, freq);
    4579             :                 else
    4580          17 :                         ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
    4581          29 :                 if (!ret) {
    4582           0 :                         wpa_printf(MSG_DEBUG, "P2P: The forced channel "
    4583             :                                    "(%u MHz) is not supported for P2P uses",
    4584             :                                    freq);
    4585           0 :                         res = -3;
    4586           0 :                         goto exit_free;
    4587             :                 }
    4588             : 
    4589          37 :                 for (i = 0; i < num; i++) {
    4590           8 :                         if (freqs[i] == freq)
    4591           8 :                                 freq_in_use = 1;
    4592             :                 }
    4593             : 
    4594          29 :                 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
    4595           0 :                         wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
    4596             :                                    freq);
    4597           0 :                         res = -2;
    4598           0 :                         goto exit_free;
    4599             :                 }
    4600          29 :                 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
    4601             :                            "requested channel (%u MHz)", freq);
    4602          29 :                 *force_freq = freq;
    4603          29 :                 goto exit_ok;
    4604             :         }
    4605             : 
    4606         314 :         for (i = 0; i < num; i++) {
    4607           4 :                 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
    4608           0 :                         continue;
    4609             : 
    4610           4 :                 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
    4611           0 :                         wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
    4612           0 :                                    freqs[i]);
    4613           0 :                         *pref_freq = freqs[i];
    4614             :                 } else {
    4615           4 :                         wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
    4616           4 :                                    freqs[i]);
    4617           4 :                         *force_freq = freqs[i];
    4618             :                 }
    4619           4 :                 break;
    4620             :         }
    4621             : 
    4622         157 :         if (i == num) {
    4623         153 :                 if (num < wpa_s->num_multichan_concurrent && num > 0) {
    4624           0 :                         wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
    4625           0 :                         *force_freq = 0;
    4626         153 :                 } else if (num < wpa_s->num_multichan_concurrent) {
    4627         153 :                         wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
    4628         153 :                         *force_freq = 0;
    4629             :                 } else {
    4630           0 :                         wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
    4631           0 :                         res = -2;
    4632           0 :                         goto exit_free;
    4633             :                 }
    4634             :         }
    4635             : 
    4636             : exit_ok:
    4637         186 :         res = 0;
    4638             : exit_free:
    4639         186 :         os_free(freqs);
    4640         186 :         return res;
    4641             : }
    4642             : 
    4643             : 
    4644             : /**
    4645             :  * wpas_p2p_connect - Request P2P Group Formation to be started
    4646             :  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
    4647             :  * @peer_addr: Address of the peer P2P Device
    4648             :  * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
    4649             :  * @persistent_group: Whether to create a persistent group
    4650             :  * @auto_join: Whether to select join vs. GO Negotiation automatically
    4651             :  * @join: Whether to join an existing group (as a client) instead of starting
    4652             :  *      Group Owner negotiation; @peer_addr is BSSID in that case
    4653             :  * @auth: Whether to only authorize the connection instead of doing that and
    4654             :  *      initiating Group Owner negotiation
    4655             :  * @go_intent: GO Intent or -1 to use default
    4656             :  * @freq: Frequency for the group or 0 for auto-selection
    4657             :  * @persistent_id: Persistent group credentials to use for forcing GO
    4658             :  *      parameters or -1 to generate new values (SSID/passphrase)
    4659             :  * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
    4660             :  *      interoperability workaround when initiating group formation
    4661             :  * @ht40: Start GO with 40 MHz channel width
    4662             :  * @vht:  Start GO with VHT support
    4663             :  * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
    4664             :  *      failure, -2 on failure due to channel not currently available,
    4665             :  *      -3 if forced channel is not supported
    4666             :  */
    4667         196 : int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
    4668             :                      const char *pin, enum p2p_wps_method wps_method,
    4669             :                      int persistent_group, int auto_join, int join, int auth,
    4670             :                      int go_intent, int freq, int persistent_id, int pd,
    4671             :                      int ht40, int vht)
    4672             : {
    4673         196 :         int force_freq = 0, pref_freq = 0;
    4674         196 :         int ret = 0, res;
    4675             :         enum wpa_driver_if_type iftype;
    4676             :         const u8 *if_addr;
    4677         196 :         struct wpa_ssid *ssid = NULL;
    4678             : 
    4679         196 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    4680           0 :                 return -1;
    4681             : 
    4682         196 :         if (persistent_id >= 0) {
    4683           1 :                 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
    4684           2 :                 if (ssid == NULL || ssid->disabled != 2 ||
    4685           1 :                     ssid->mode != WPAS_MODE_P2P_GO)
    4686           0 :                         return -1;
    4687             :         }
    4688             : 
    4689         196 :         os_free(wpa_s->global->add_psk);
    4690         196 :         wpa_s->global->add_psk = NULL;
    4691             : 
    4692         196 :         wpa_s->global->p2p_fail_on_wps_complete = 0;
    4693             : 
    4694         196 :         if (go_intent < 0)
    4695         123 :                 go_intent = wpa_s->conf->p2p_go_intent;
    4696             : 
    4697         196 :         if (!auth)
    4698         133 :                 wpa_s->p2p_long_listen = 0;
    4699             : 
    4700         196 :         wpa_s->p2p_wps_method = wps_method;
    4701         196 :         wpa_s->p2p_persistent_group = !!persistent_group;
    4702         196 :         wpa_s->p2p_persistent_id = persistent_id;
    4703         196 :         wpa_s->p2p_go_intent = go_intent;
    4704         196 :         wpa_s->p2p_connect_freq = freq;
    4705         196 :         wpa_s->p2p_fallback_to_go_neg = 0;
    4706         196 :         wpa_s->p2p_pd_before_go_neg = !!pd;
    4707         196 :         wpa_s->p2p_go_ht40 = !!ht40;
    4708         196 :         wpa_s->p2p_go_vht = !!vht;
    4709             : 
    4710         196 :         if (pin)
    4711         146 :                 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
    4712          50 :         else if (wps_method == WPS_PIN_DISPLAY) {
    4713           0 :                 ret = wps_generate_pin();
    4714           0 :                 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
    4715             :                             ret);
    4716           0 :                 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
    4717           0 :                            wpa_s->p2p_pin);
    4718             :         } else
    4719          50 :                 wpa_s->p2p_pin[0] = '\0';
    4720             : 
    4721         196 :         if (join || auto_join) {
    4722             :                 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
    4723          33 :                 if (auth) {
    4724           6 :                         wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
    4725             :                                    "connect a running group from " MACSTR,
    4726           6 :                                    MAC2STR(peer_addr));
    4727           1 :                         os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
    4728           1 :                         return ret;
    4729             :                 }
    4730          32 :                 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
    4731          32 :                 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
    4732             :                                            iface_addr) < 0) {
    4733          29 :                         os_memcpy(iface_addr, peer_addr, ETH_ALEN);
    4734          29 :                         p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
    4735             :                                          dev_addr);
    4736             :                 }
    4737          32 :                 if (auto_join) {
    4738           0 :                         os_get_reltime(&wpa_s->p2p_auto_started);
    4739           0 :                         wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
    4740             :                                    "%ld.%06ld",
    4741             :                                    wpa_s->p2p_auto_started.sec,
    4742             :                                    wpa_s->p2p_auto_started.usec);
    4743             :                 }
    4744          32 :                 wpa_s->user_initiated_pd = 1;
    4745          32 :                 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
    4746             :                                   auto_join, freq, NULL, 0) < 0)
    4747           0 :                         return -1;
    4748          32 :                 return ret;
    4749             :         }
    4750             : 
    4751         163 :         res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
    4752             :                                    go_intent == 15);
    4753         163 :         if (res)
    4754           0 :                 return res;
    4755         163 :         wpas_p2p_set_own_freq_preference(wpa_s,
    4756         163 :                                          force_freq ? force_freq : pref_freq);
    4757             : 
    4758         163 :         wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
    4759             : 
    4760         163 :         if (wpa_s->create_p2p_iface) {
    4761             :                 /* Prepare to add a new interface for the group */
    4762          22 :                 iftype = WPA_IF_P2P_GROUP;
    4763          22 :                 if (go_intent == 15)
    4764           9 :                         iftype = WPA_IF_P2P_GO;
    4765          22 :                 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
    4766           0 :                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
    4767             :                                    "interface for the group");
    4768           0 :                         return -1;
    4769             :                 }
    4770             : 
    4771          22 :                 if_addr = wpa_s->pending_interface_addr;
    4772             :         } else
    4773         141 :                 if_addr = wpa_s->own_addr;
    4774             : 
    4775         163 :         if (auth) {
    4776          62 :                 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
    4777             :                                          go_intent, if_addr,
    4778             :                                          force_freq, persistent_group, ssid,
    4779             :                                          pref_freq) < 0)
    4780           0 :                         return -1;
    4781          62 :                 return ret;
    4782             :         }
    4783             : 
    4784         101 :         if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
    4785             :                                   go_intent, if_addr, force_freq,
    4786             :                                   persistent_group, ssid, pref_freq) < 0) {
    4787           0 :                 if (wpa_s->create_p2p_iface)
    4788           0 :                         wpas_p2p_remove_pending_group_interface(wpa_s);
    4789           0 :                 return -1;
    4790             :         }
    4791         101 :         return ret;
    4792             : }
    4793             : 
    4794             : 
    4795             : /**
    4796             :  * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
    4797             :  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
    4798             :  * @freq: Frequency of the channel in MHz
    4799             :  * @duration: Duration of the stay on the channel in milliseconds
    4800             :  *
    4801             :  * This callback is called when the driver indicates that it has started the
    4802             :  * requested remain-on-channel duration.
    4803             :  */
    4804        1093 : void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
    4805             :                                    unsigned int freq, unsigned int duration)
    4806             : {
    4807        1093 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    4808        1093 :                 return;
    4809        1093 :         if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
    4810        1091 :                 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
    4811             :                               wpa_s->pending_listen_duration);
    4812        1091 :                 wpa_s->pending_listen_freq = 0;
    4813             :         } else {
    4814           2 :                 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
    4815             :                            wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
    4816             :                            freq, duration);
    4817             :         }
    4818             : }
    4819             : 
    4820             : 
    4821         257 : static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
    4822             :                                  unsigned int timeout)
    4823             : {
    4824             :         /* Limit maximum Listen state time based on driver limitation. */
    4825         257 :         if (timeout > wpa_s->max_remain_on_chan)
    4826         256 :                 timeout = wpa_s->max_remain_on_chan;
    4827             : 
    4828         257 :         return p2p_listen(wpa_s->global->p2p, timeout);
    4829             : }
    4830             : 
    4831             : 
    4832             : /**
    4833             :  * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
    4834             :  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
    4835             :  * @freq: Frequency of the channel in MHz
    4836             :  *
    4837             :  * This callback is called when the driver indicates that a remain-on-channel
    4838             :  * operation has been completed, i.e., the duration on the requested channel
    4839             :  * has timed out.
    4840             :  */
    4841        1064 : void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
    4842             :                                           unsigned int freq)
    4843             : {
    4844        1064 :         wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
    4845             :                    "(p2p_long_listen=%d ms pending_action_tx=%p)",
    4846             :                    wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
    4847        1064 :         wpas_p2p_listen_work_done(wpa_s);
    4848        1064 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    4849           0 :                 return;
    4850        1064 :         if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
    4851           0 :                 return; /* P2P module started a new operation */
    4852        1064 :         if (offchannel_pending_action_tx(wpa_s))
    4853          47 :                 return;
    4854        1017 :         if (wpa_s->p2p_long_listen > 0)
    4855          48 :                 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
    4856        1017 :         if (wpa_s->p2p_long_listen > 0) {
    4857          48 :                 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
    4858          48 :                 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
    4859             :         } else {
    4860             :                 /*
    4861             :                  * When listen duration is over, stop listen & update p2p_state
    4862             :                  * to IDLE.
    4863             :                  */
    4864         969 :                 p2p_stop_listen(wpa_s->global->p2p);
    4865             :         }
    4866             : }
    4867             : 
    4868             : 
    4869             : /**
    4870             :  * wpas_p2p_group_remove - Remove a P2P group
    4871             :  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
    4872             :  * @ifname: Network interface name of the group interface or "*" to remove all
    4873             :  *      groups
    4874             :  * Returns: 0 on success, -1 on failure
    4875             :  *
    4876             :  * This function is used to remove a P2P group. This can be used to disconnect
    4877             :  * from a group in which the local end is a P2P Client or to end a P2P Group in
    4878             :  * case the local end is the Group Owner. If a virtual network interface was
    4879             :  * created for this group, that interface will be removed. Otherwise, only the
    4880             :  * configured P2P group network will be removed from the interface.
    4881             :  */
    4882        2078 : int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
    4883             : {
    4884        2078 :         struct wpa_global *global = wpa_s->global;
    4885             : 
    4886        2078 :         if (os_strcmp(ifname, "*") == 0) {
    4887             :                 struct wpa_supplicant *prev;
    4888        1946 :                 wpa_s = global->ifaces;
    4889        5805 :                 while (wpa_s) {
    4890        1913 :                         prev = wpa_s;
    4891        1913 :                         wpa_s = wpa_s->next;
    4892        1913 :                         if (prev->p2p_group_interface !=
    4893        1911 :                             NOT_P2P_GROUP_INTERFACE ||
    4894        2392 :                             (prev->current_ssid &&
    4895         481 :                              prev->current_ssid->p2p_group))
    4896          42 :                                 wpas_p2p_disconnect(prev);
    4897             :                 }
    4898        1946 :                 return 0;
    4899             :         }
    4900             : 
    4901         134 :         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    4902         132 :                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
    4903         130 :                         break;
    4904             :         }
    4905             : 
    4906         132 :         return wpas_p2p_disconnect(wpa_s);
    4907             : }
    4908             : 
    4909             : 
    4910          72 : static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
    4911             : {
    4912             :         unsigned int r;
    4913             : 
    4914          72 :         if (freq == 2) {
    4915           0 :                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
    4916             :                            "band");
    4917           0 :                 if (wpa_s->best_24_freq > 0 &&
    4918           0 :                     p2p_supported_freq_go(wpa_s->global->p2p,
    4919           0 :                                           wpa_s->best_24_freq)) {
    4920           0 :                         freq = wpa_s->best_24_freq;
    4921           0 :                         wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
    4922             :                                    "channel: %d MHz", freq);
    4923             :                 } else {
    4924           0 :                         os_get_random((u8 *) &r, sizeof(r));
    4925           0 :                         freq = 2412 + (r % 3) * 25;
    4926           0 :                         wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
    4927             :                                    "channel: %d MHz", freq);
    4928             :                 }
    4929             :         }
    4930             : 
    4931          72 :         if (freq == 5) {
    4932           0 :                 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
    4933             :                            "band");
    4934           0 :                 if (wpa_s->best_5_freq > 0 &&
    4935           0 :                     p2p_supported_freq_go(wpa_s->global->p2p,
    4936           0 :                                        wpa_s->best_5_freq)) {
    4937           0 :                         freq = wpa_s->best_5_freq;
    4938           0 :                         wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
    4939             :                                    "channel: %d MHz", freq);
    4940             :                 } else {
    4941           0 :                         os_get_random((u8 *) &r, sizeof(r));
    4942           0 :                         freq = 5180 + (r % 4) * 20;
    4943           0 :                         if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
    4944           0 :                                 wpa_printf(MSG_DEBUG, "P2P: Could not select "
    4945             :                                            "5 GHz channel for P2P group");
    4946           0 :                                 return -1;
    4947             :                         }
    4948           0 :                         wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
    4949             :                                    "channel: %d MHz", freq);
    4950             :                 }
    4951             :         }
    4952             : 
    4953          72 :         if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
    4954           0 :                 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
    4955             :                            "(%u MHz) is not supported for P2P uses",
    4956             :                            freq);
    4957           0 :                 return -1;
    4958             :         }
    4959             : 
    4960          72 :         return freq;
    4961             : }
    4962             : 
    4963             : 
    4964          52 : static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
    4965             :                                    struct p2p_go_neg_results *params,
    4966             :                                    int freq, int ht40, int vht,
    4967             :                                    const struct p2p_channels *channels)
    4968             : {
    4969             :         int res, *freqs;
    4970             :         unsigned int pref_freq;
    4971             :         unsigned int num, i;
    4972             : 
    4973          52 :         os_memset(params, 0, sizeof(*params));
    4974          52 :         params->role_go = 1;
    4975          52 :         params->ht40 = ht40;
    4976          52 :         params->vht = vht;
    4977          52 :         if (freq) {
    4978          29 :                 if (!freq_included(channels, freq)) {
    4979           0 :                         wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
    4980             :                                    "accepted", freq);
    4981           0 :                         return -1;
    4982             :                 }
    4983          29 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
    4984             :                            "frequency %d MHz", freq);
    4985          29 :                 params->freq = freq;
    4986          40 :         } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
    4987          34 :                    wpa_s->conf->p2p_oper_channel >= 1 &&
    4988          34 :                    wpa_s->conf->p2p_oper_channel <= 11 &&
    4989          17 :                    freq_included(channels,
    4990          17 :                                  2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
    4991          17 :                 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
    4992          17 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
    4993             :                            "frequency %d MHz", params->freq);
    4994          12 :         } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
    4995          12 :                     wpa_s->conf->p2p_oper_reg_class == 116 ||
    4996          12 :                     wpa_s->conf->p2p_oper_reg_class == 117 ||
    4997          12 :                     wpa_s->conf->p2p_oper_reg_class == 124 ||
    4998          12 :                     wpa_s->conf->p2p_oper_reg_class == 126 ||
    4999           6 :                     wpa_s->conf->p2p_oper_reg_class == 127) &&
    5000           0 :                    freq_included(channels,
    5001           0 :                                  5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
    5002           0 :                 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
    5003           0 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
    5004             :                            "frequency %d MHz", params->freq);
    5005          12 :         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
    5006           6 :                    wpa_s->best_overall_freq > 0 &&
    5007           0 :                    p2p_supported_freq_go(wpa_s->global->p2p,
    5008           0 :                                          wpa_s->best_overall_freq) &&
    5009           0 :                    freq_included(channels, wpa_s->best_overall_freq)) {
    5010           0 :                 params->freq = wpa_s->best_overall_freq;
    5011           0 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
    5012             :                            "channel %d MHz", params->freq);
    5013          12 :         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
    5014           6 :                    wpa_s->best_24_freq > 0 &&
    5015           0 :                    p2p_supported_freq_go(wpa_s->global->p2p,
    5016           0 :                                          wpa_s->best_24_freq) &&
    5017           0 :                    freq_included(channels, wpa_s->best_24_freq)) {
    5018           0 :                 params->freq = wpa_s->best_24_freq;
    5019           0 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
    5020             :                            "channel %d MHz", params->freq);
    5021          12 :         } else if (wpa_s->conf->p2p_oper_channel == 0 &&
    5022           6 :                    wpa_s->best_5_freq > 0 &&
    5023           0 :                    p2p_supported_freq_go(wpa_s->global->p2p,
    5024           0 :                                          wpa_s->best_5_freq) &&
    5025           0 :                    freq_included(channels, wpa_s->best_5_freq)) {
    5026           0 :                 params->freq = wpa_s->best_5_freq;
    5027           0 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
    5028             :                            "channel %d MHz", params->freq);
    5029           6 :         } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
    5030             :                                                   channels))) {
    5031           0 :                 params->freq = pref_freq;
    5032           0 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
    5033             :                            "channels", params->freq);
    5034             :         } else {
    5035             :                 int chan;
    5036           6 :                 for (chan = 0; chan < 11; chan++) {
    5037           6 :                         params->freq = 2412 + chan * 5;
    5038           6 :                         if (!wpas_p2p_disallowed_freq(wpa_s->global,
    5039          12 :                                                       params->freq) &&
    5040           6 :                             freq_included(channels, params->freq))
    5041           6 :                                 break;
    5042             :                 }
    5043           6 :                 if (chan == 11) {
    5044           0 :                         wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
    5045             :                                    "allowed");
    5046           0 :                         return -1;
    5047             :                 }
    5048           6 :                 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
    5049             :                            "known)", params->freq);
    5050             :         }
    5051             : 
    5052          52 :         freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
    5053          52 :         if (!freqs)
    5054           0 :                 return -1;
    5055             : 
    5056          52 :         res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
    5057             :                                         wpa_s->num_multichan_concurrent);
    5058          52 :         if (res < 0) {
    5059           0 :                 os_free(freqs);
    5060           0 :                 return -1;
    5061             :         }
    5062          52 :         num = res;
    5063             : 
    5064          52 :         for (i = 0; i < num; i++) {
    5065           3 :                 if (freq && freqs[i] == freq)
    5066           2 :                         break;
    5067           1 :                 if (!freq && freq_included(channels, freqs[i])) {
    5068           1 :                         wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
    5069           1 :                                    freqs[i]);
    5070           1 :                         params->freq = freqs[i];
    5071           1 :                         break;
    5072             :                 }
    5073             :         }
    5074             : 
    5075          52 :         if (i == num) {
    5076          49 :                 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
    5077           0 :                         if (freq)
    5078           0 :                                 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
    5079             :                         else
    5080           0 :                                 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
    5081           0 :                         os_free(freqs);
    5082           0 :                         return -1;
    5083          49 :                 } else if (num == 0) {
    5084          49 :                         wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
    5085             :                 } else {
    5086           0 :                         wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
    5087             :                 }
    5088             :         }
    5089             : 
    5090          52 :         os_free(freqs);
    5091          52 :         return 0;
    5092             : }
    5093             : 
    5094             : 
    5095             : static struct wpa_supplicant *
    5096         111 : wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
    5097             :                          int go)
    5098             : {
    5099             :         struct wpa_supplicant *group_wpa_s;
    5100             : 
    5101         111 :         if (!wpas_p2p_create_iface(wpa_s)) {
    5102          98 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
    5103             :                         "operations");
    5104          98 :                 wpa_s->p2p_first_connection_timeout = 0;
    5105          98 :                 return wpa_s;
    5106             :         }
    5107             : 
    5108          13 :         if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
    5109             :                                          WPA_IF_P2P_CLIENT) < 0) {
    5110           0 :                 wpa_msg_global(wpa_s, MSG_ERROR,
    5111             :                                "P2P: Failed to add group interface");
    5112           0 :                 return NULL;
    5113             :         }
    5114          13 :         group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
    5115          13 :         if (group_wpa_s == NULL) {
    5116           0 :                 wpa_msg_global(wpa_s, MSG_ERROR,
    5117             :                                "P2P: Failed to initialize group interface");
    5118           0 :                 wpas_p2p_remove_pending_group_interface(wpa_s);
    5119           0 :                 return NULL;
    5120             :         }
    5121             : 
    5122          13 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
    5123             :                 group_wpa_s->ifname);
    5124          13 :         group_wpa_s->p2p_first_connection_timeout = 0;
    5125          13 :         return group_wpa_s;
    5126             : }
    5127             : 
    5128             : 
    5129             : /**
    5130             :  * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
    5131             :  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
    5132             :  * @persistent_group: Whether to create a persistent group
    5133             :  * @freq: Frequency for the group or 0 to indicate no hardcoding
    5134             :  * @ht40: Start GO with 40 MHz channel width
    5135             :  * @vht:  Start GO with VHT support
    5136             :  * Returns: 0 on success, -1 on failure
    5137             :  *
    5138             :  * This function creates a new P2P group with the local end as the Group Owner,
    5139             :  * i.e., without using Group Owner Negotiation.
    5140             :  */
    5141          35 : int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
    5142             :                        int freq, int ht40, int vht)
    5143             : {
    5144             :         struct p2p_go_neg_results params;
    5145             : 
    5146          35 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5147           0 :                 return -1;
    5148             : 
    5149          35 :         os_free(wpa_s->global->add_psk);
    5150          35 :         wpa_s->global->add_psk = NULL;
    5151             : 
    5152             :         /* Make sure we are not running find during connection establishment */
    5153          35 :         wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
    5154          35 :         wpas_p2p_stop_find_oper(wpa_s);
    5155             : 
    5156          35 :         freq = wpas_p2p_select_go_freq(wpa_s, freq);
    5157          35 :         if (freq < 0)
    5158           0 :                 return -1;
    5159             : 
    5160          35 :         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
    5161           0 :                 return -1;
    5162          70 :         if (params.freq &&
    5163          35 :             !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
    5164           0 :                 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
    5165             :                            "(%u MHz) is not supported for P2P uses",
    5166             :                            params.freq);
    5167           0 :                 return -1;
    5168             :         }
    5169          35 :         p2p_go_params(wpa_s->global->p2p, &params);
    5170          35 :         params.persistent_group = persistent_group;
    5171             : 
    5172          35 :         wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
    5173          35 :         if (wpa_s == NULL)
    5174           0 :                 return -1;
    5175          35 :         wpas_start_wps_go(wpa_s, &params, 0);
    5176             : 
    5177          35 :         return 0;
    5178             : }
    5179             : 
    5180             : 
    5181          20 : static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
    5182             :                                  struct wpa_ssid *params, int addr_allocated,
    5183             :                                  int freq)
    5184             : {
    5185             :         struct wpa_ssid *ssid;
    5186             : 
    5187          20 :         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
    5188          20 :         if (wpa_s == NULL)
    5189           0 :                 return -1;
    5190          20 :         wpa_s->p2p_last_4way_hs_fail = NULL;
    5191             : 
    5192          20 :         wpa_supplicant_ap_deinit(wpa_s);
    5193             : 
    5194          20 :         ssid = wpa_config_add_network(wpa_s->conf);
    5195          20 :         if (ssid == NULL)
    5196           0 :                 return -1;
    5197          20 :         wpa_config_set_network_defaults(ssid);
    5198          20 :         ssid->temporary = 1;
    5199          20 :         ssid->proto = WPA_PROTO_RSN;
    5200          20 :         ssid->pairwise_cipher = WPA_CIPHER_CCMP;
    5201          20 :         ssid->group_cipher = WPA_CIPHER_CCMP;
    5202          20 :         ssid->key_mgmt = WPA_KEY_MGMT_PSK;
    5203          20 :         ssid->ssid = os_malloc(params->ssid_len);
    5204          20 :         if (ssid->ssid == NULL) {
    5205           0 :                 wpa_config_remove_network(wpa_s->conf, ssid->id);
    5206           0 :                 return -1;
    5207             :         }
    5208          20 :         os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
    5209          20 :         ssid->ssid_len = params->ssid_len;
    5210          20 :         ssid->p2p_group = 1;
    5211          20 :         ssid->export_keys = 1;
    5212          20 :         if (params->psk_set) {
    5213          20 :                 os_memcpy(ssid->psk, params->psk, 32);
    5214          20 :                 ssid->psk_set = 1;
    5215             :         }
    5216          20 :         if (params->passphrase)
    5217           0 :                 ssid->passphrase = os_strdup(params->passphrase);
    5218             : 
    5219          20 :         wpa_s->show_group_started = 1;
    5220          20 :         wpa_s->p2p_in_invitation = 1;
    5221          20 :         wpa_s->p2p_invite_go_freq = freq;
    5222             : 
    5223          20 :         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
    5224             :                              NULL);
    5225          20 :         eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
    5226             :                                wpas_p2p_group_formation_timeout,
    5227          20 :                                wpa_s->parent, NULL);
    5228          20 :         wpa_supplicant_select_network(wpa_s, ssid);
    5229             : 
    5230          20 :         return 0;
    5231             : }
    5232             : 
    5233             : 
    5234          38 : int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
    5235             :                                   struct wpa_ssid *ssid, int addr_allocated,
    5236             :                                   int force_freq, int neg_freq, int ht40,
    5237             :                                   int vht, const struct p2p_channels *channels,
    5238             :                                   int connection_timeout)
    5239             : {
    5240             :         struct p2p_go_neg_results params;
    5241          38 :         int go = 0, freq;
    5242             : 
    5243          38 :         if (ssid->disabled != 2 || ssid->ssid == NULL)
    5244           0 :                 return -1;
    5245             : 
    5246          39 :         if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
    5247           1 :             go == (ssid->mode == WPAS_MODE_P2P_GO)) {
    5248           1 :                 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
    5249             :                            "already running");
    5250           1 :                 return 0;
    5251             :         }
    5252             : 
    5253          37 :         os_free(wpa_s->global->add_psk);
    5254          37 :         wpa_s->global->add_psk = NULL;
    5255             : 
    5256             :         /* Make sure we are not running find during connection establishment */
    5257          37 :         wpas_p2p_stop_find_oper(wpa_s);
    5258             : 
    5259          37 :         wpa_s->p2p_fallback_to_go_neg = 0;
    5260             : 
    5261          37 :         if (force_freq > 0) {
    5262           5 :                 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
    5263           5 :                 if (freq < 0)
    5264           0 :                         return -1;
    5265             :         } else {
    5266          32 :                 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
    5267          32 :                 if (freq < 0 || (freq > 0 && !freq_included(channels, freq)))
    5268           0 :                         freq = 0;
    5269             :         }
    5270             : 
    5271          37 :         if (ssid->mode == WPAS_MODE_INFRA)
    5272          20 :                 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
    5273             : 
    5274          17 :         if (ssid->mode != WPAS_MODE_P2P_GO)
    5275           0 :                 return -1;
    5276             : 
    5277          17 :         if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
    5278           0 :                 return -1;
    5279             : 
    5280          17 :         params.role_go = 1;
    5281          17 :         params.psk_set = ssid->psk_set;
    5282          17 :         if (params.psk_set)
    5283          17 :                 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
    5284          17 :         if (ssid->passphrase) {
    5285          17 :                 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
    5286           0 :                         wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
    5287             :                                    "persistent group");
    5288           0 :                         return -1;
    5289             :                 }
    5290          17 :                 os_strlcpy(params.passphrase, ssid->passphrase,
    5291             :                            sizeof(params.passphrase));
    5292             :         }
    5293          17 :         os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
    5294          17 :         params.ssid_len = ssid->ssid_len;
    5295          17 :         params.persistent_group = 1;
    5296             : 
    5297          17 :         wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
    5298          17 :         if (wpa_s == NULL)
    5299           0 :                 return -1;
    5300             : 
    5301          17 :         wpa_s->p2p_first_connection_timeout = connection_timeout;
    5302          17 :         wpas_start_wps_go(wpa_s, &params, 0);
    5303             : 
    5304          17 :         return 0;
    5305             : }
    5306             : 
    5307             : 
    5308         671 : static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
    5309             :                                struct wpabuf *proberesp_ies)
    5310             : {
    5311         671 :         struct wpa_supplicant *wpa_s = ctx;
    5312         671 :         if (wpa_s->ap_iface) {
    5313         671 :                 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
    5314         671 :                 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
    5315          56 :                         wpabuf_free(beacon_ies);
    5316          56 :                         wpabuf_free(proberesp_ies);
    5317         727 :                         return;
    5318             :                 }
    5319         615 :                 if (beacon_ies) {
    5320         235 :                         wpabuf_free(hapd->p2p_beacon_ie);
    5321         235 :                         hapd->p2p_beacon_ie = beacon_ies;
    5322             :                 }
    5323         615 :                 wpabuf_free(hapd->p2p_probe_resp_ie);
    5324         615 :                 hapd->p2p_probe_resp_ie = proberesp_ies;
    5325             :         } else {
    5326           0 :                 wpabuf_free(beacon_ies);
    5327           0 :                 wpabuf_free(proberesp_ies);
    5328             :         }
    5329         615 :         wpa_supplicant_ap_update_beacon(wpa_s);
    5330             : }
    5331             : 
    5332             : 
    5333         488 : static void wpas_p2p_idle_update(void *ctx, int idle)
    5334             : {
    5335         488 :         struct wpa_supplicant *wpa_s = ctx;
    5336         488 :         if (!wpa_s->ap_iface)
    5337           0 :                 return;
    5338         488 :         wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
    5339         488 :         if (idle) {
    5340         267 :                 if (wpa_s->global->p2p_fail_on_wps_complete &&
    5341           1 :                     wpa_s->p2p_in_provisioning) {
    5342           1 :                         wpas_p2p_grpform_fail_after_wps(wpa_s);
    5343           1 :                         return;
    5344             :                 }
    5345         265 :                 wpas_p2p_set_group_idle_timeout(wpa_s);
    5346             :         } else
    5347         222 :                 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
    5348             : }
    5349             : 
    5350             : 
    5351         126 : struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
    5352             :                                        struct wpa_ssid *ssid)
    5353             : {
    5354             :         struct p2p_group *group;
    5355             :         struct p2p_group_config *cfg;
    5356             : 
    5357         126 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5358           0 :                 return NULL;
    5359             : 
    5360         126 :         cfg = os_zalloc(sizeof(*cfg));
    5361         126 :         if (cfg == NULL)
    5362           0 :                 return NULL;
    5363             : 
    5364         126 :         if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
    5365          24 :                 cfg->persistent_group = 2;
    5366         102 :         else if (ssid->p2p_persistent_group)
    5367           6 :                 cfg->persistent_group = 1;
    5368         126 :         os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
    5369         126 :         if (wpa_s->max_stations &&
    5370           0 :             wpa_s->max_stations < wpa_s->conf->max_num_sta)
    5371           0 :                 cfg->max_clients = wpa_s->max_stations;
    5372             :         else
    5373         126 :                 cfg->max_clients = wpa_s->conf->max_num_sta;
    5374         126 :         os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
    5375         126 :         cfg->ssid_len = ssid->ssid_len;
    5376         126 :         cfg->freq = ssid->frequency;
    5377         126 :         cfg->cb_ctx = wpa_s;
    5378         126 :         cfg->ie_update = wpas_p2p_ie_update;
    5379         126 :         cfg->idle_update = wpas_p2p_idle_update;
    5380             : 
    5381         126 :         group = p2p_group_init(wpa_s->global->p2p, cfg);
    5382         126 :         if (group == NULL)
    5383           0 :                 os_free(cfg);
    5384         126 :         if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
    5385          60 :                 p2p_group_notif_formation_done(group);
    5386         126 :         wpa_s->p2p_group = group;
    5387         126 :         return group;
    5388             : }
    5389             : 
    5390             : 
    5391         259 : void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
    5392             :                           int registrar)
    5393             : {
    5394         259 :         struct wpa_ssid *ssid = wpa_s->current_ssid;
    5395             : 
    5396         259 :         if (!wpa_s->p2p_in_provisioning) {
    5397          98 :                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
    5398             :                            "provisioning not in progress");
    5399         357 :                 return;
    5400             :         }
    5401             : 
    5402         161 :         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
    5403             :                 u8 go_dev_addr[ETH_ALEN];
    5404          98 :                 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
    5405          98 :                 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
    5406             :                                           ssid->ssid_len);
    5407             :                 /* Clear any stored provisioning info */
    5408          98 :                 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
    5409             :         }
    5410             : 
    5411         161 :         eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
    5412             :                              NULL);
    5413         161 :         wpa_s->p2p_go_group_formation_completed = 1;
    5414         161 :         if (ssid && ssid->mode == WPAS_MODE_INFRA) {
    5415             :                 /*
    5416             :                  * Use a separate timeout for initial data connection to
    5417             :                  * complete to allow the group to be removed automatically if
    5418             :                  * something goes wrong in this step before the P2P group idle
    5419             :                  * timeout mechanism is taken into use.
    5420             :                  */
    5421          98 :                 wpa_dbg(wpa_s, MSG_DEBUG,
    5422             :                         "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
    5423             :                         P2P_MAX_INITIAL_CONN_WAIT);
    5424          98 :                 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
    5425             :                                        wpas_p2p_group_formation_timeout,
    5426          98 :                                        wpa_s->parent, NULL);
    5427          63 :         } else if (ssid) {
    5428             :                 /*
    5429             :                  * Use a separate timeout for initial data connection to
    5430             :                  * complete to allow the group to be removed automatically if
    5431             :                  * the client does not complete data connection successfully.
    5432             :                  */
    5433          63 :                 wpa_dbg(wpa_s, MSG_DEBUG,
    5434             :                         "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
    5435             :                         P2P_MAX_INITIAL_CONN_WAIT_GO);
    5436          63 :                 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
    5437             :                                        wpas_p2p_group_formation_timeout,
    5438          63 :                                        wpa_s->parent, NULL);
    5439             :                 /*
    5440             :                  * Complete group formation on first successful data connection
    5441             :                  */
    5442          63 :                 wpa_s->p2p_go_group_formation_completed = 0;
    5443             :         }
    5444         161 :         if (wpa_s->global->p2p)
    5445         161 :                 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
    5446         161 :         wpas_group_formation_completed(wpa_s, 1);
    5447             : }
    5448             : 
    5449             : 
    5450          46 : void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
    5451             :                          struct wps_event_fail *fail)
    5452             : {
    5453          46 :         if (!wpa_s->p2p_in_provisioning) {
    5454          43 :                 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
    5455             :                            "provisioning not in progress");
    5456          89 :                 return;
    5457             :         }
    5458             : 
    5459           3 :         if (wpa_s->go_params) {
    5460           2 :                 p2p_clear_provisioning_info(
    5461           2 :                         wpa_s->global->p2p,
    5462           2 :                         wpa_s->go_params->peer_device_addr);
    5463             :         }
    5464             : 
    5465           3 :         wpas_notify_p2p_wps_failed(wpa_s, fail);
    5466             : 
    5467           3 :         if (wpa_s == wpa_s->global->p2p_group_formation) {
    5468             :                 /*
    5469             :                  * Allow some time for the failed WPS negotiation exchange to
    5470             :                  * complete, but remove the group since group formation cannot
    5471             :                  * succeed after provisioning failure.
    5472             :                  */
    5473           3 :                 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
    5474           3 :                 wpa_s->global->p2p_fail_on_wps_complete = 1;
    5475           3 :                 eloop_deplete_timeout(0, 50000,
    5476             :                                       wpas_p2p_group_formation_timeout,
    5477           3 :                                       wpa_s->parent, NULL);
    5478             :         }
    5479             : }
    5480             : 
    5481             : 
    5482         886 : int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
    5483             : {
    5484         888 :         if (!wpa_s->global->p2p_fail_on_wps_complete ||
    5485           2 :             !wpa_s->p2p_in_provisioning)
    5486         884 :                 return 0;
    5487             : 
    5488           2 :         wpas_p2p_grpform_fail_after_wps(wpa_s);
    5489             : 
    5490           2 :         return 1;
    5491             : }
    5492             : 
    5493             : 
    5494           8 : int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
    5495             :                        const char *config_method,
    5496             :                        enum wpas_p2p_prov_disc_use use)
    5497             : {
    5498             :         u16 config_methods;
    5499             : 
    5500           8 :         wpa_s->p2p_fallback_to_go_neg = 0;
    5501           8 :         wpa_s->pending_pd_use = NORMAL_PD;
    5502           8 :         if (os_strncmp(config_method, "display", 7) == 0)
    5503           6 :                 config_methods = WPS_CONFIG_DISPLAY;
    5504           2 :         else if (os_strncmp(config_method, "keypad", 6) == 0)
    5505           1 :                 config_methods = WPS_CONFIG_KEYPAD;
    5506           1 :         else if (os_strncmp(config_method, "pbc", 3) == 0 ||
    5507           0 :                  os_strncmp(config_method, "pushbutton", 10) == 0)
    5508           1 :                 config_methods = WPS_CONFIG_PUSHBUTTON;
    5509             :         else {
    5510           0 :                 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
    5511           0 :                 return -1;
    5512             :         }
    5513             : 
    5514           8 :         if (use == WPAS_P2P_PD_AUTO) {
    5515           0 :                 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
    5516           0 :                 wpa_s->pending_pd_config_methods = config_methods;
    5517           0 :                 wpa_s->p2p_auto_pd = 1;
    5518           0 :                 wpa_s->p2p_auto_join = 0;
    5519           0 :                 wpa_s->pending_pd_before_join = 0;
    5520           0 :                 wpa_s->auto_pd_scan_retry = 0;
    5521           0 :                 wpas_p2p_stop_find(wpa_s);
    5522           0 :                 wpa_s->p2p_join_scan_count = 0;
    5523           0 :                 os_get_reltime(&wpa_s->p2p_auto_started);
    5524           0 :                 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
    5525             :                            wpa_s->p2p_auto_started.sec,
    5526             :                            wpa_s->p2p_auto_started.usec);
    5527           0 :                 wpas_p2p_join_scan(wpa_s, NULL);
    5528           0 :                 return 0;
    5529             :         }
    5530             : 
    5531           8 :         if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
    5532           0 :                 return -1;
    5533             : 
    5534           8 :         return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
    5535             :                                  config_methods, use == WPAS_P2P_PD_FOR_JOIN,
    5536             :                                  0, 1);
    5537             : }
    5538             : 
    5539             : 
    5540         158 : int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
    5541             :                               char *end)
    5542             : {
    5543         158 :         return p2p_scan_result_text(ies, ies_len, buf, end);
    5544             : }
    5545             : 
    5546             : 
    5547        4397 : static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
    5548             : {
    5549        4397 :         if (!offchannel_pending_action_tx(wpa_s))
    5550        8793 :                 return;
    5551             : 
    5552           1 :         wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
    5553             :                    "operation request");
    5554           1 :         offchannel_clear_pending_action_tx(wpa_s);
    5555             : }
    5556             : 
    5557             : 
    5558         207 : int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
    5559             :                   enum p2p_discovery_type type,
    5560             :                   unsigned int num_req_dev_types, const u8 *req_dev_types,
    5561             :                   const u8 *dev_id, unsigned int search_delay)
    5562             : {
    5563         207 :         wpas_p2p_clear_pending_action_tx(wpa_s);
    5564         207 :         wpa_s->p2p_long_listen = 0;
    5565             : 
    5566         414 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
    5567         207 :             wpa_s->p2p_in_provisioning)
    5568           0 :                 return -1;
    5569             : 
    5570         207 :         wpa_supplicant_cancel_sched_scan(wpa_s);
    5571             : 
    5572         207 :         return p2p_find(wpa_s->global->p2p, timeout, type,
    5573             :                         num_req_dev_types, req_dev_types, dev_id,
    5574             :                         search_delay);
    5575             : }
    5576             : 
    5577             : 
    5578        3981 : static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
    5579             : {
    5580        3981 :         wpas_p2p_clear_pending_action_tx(wpa_s);
    5581        3981 :         wpa_s->p2p_long_listen = 0;
    5582        3981 :         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
    5583        3981 :         eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
    5584             : 
    5585        3981 :         if (wpa_s->global->p2p)
    5586        3981 :                 p2p_stop_find(wpa_s->global->p2p);
    5587             : 
    5588        3981 :         return 0;
    5589             : }
    5590             : 
    5591             : 
    5592        3909 : void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
    5593             : {
    5594        3909 :         if (wpas_p2p_stop_find_oper(wpa_s) > 0)
    5595        3909 :                 return;
    5596        3909 :         wpas_p2p_remove_pending_group_interface(wpa_s);
    5597             : }
    5598             : 
    5599             : 
    5600           0 : static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
    5601             : {
    5602           0 :         struct wpa_supplicant *wpa_s = eloop_ctx;
    5603           0 :         wpa_s->p2p_long_listen = 0;
    5604           0 : }
    5605             : 
    5606             : 
    5607         209 : int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
    5608             : {
    5609             :         int res;
    5610             : 
    5611         209 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5612           0 :                 return -1;
    5613             : 
    5614         209 :         wpa_supplicant_cancel_sched_scan(wpa_s);
    5615         209 :         wpas_p2p_clear_pending_action_tx(wpa_s);
    5616             : 
    5617         209 :         if (timeout == 0) {
    5618             :                 /*
    5619             :                  * This is a request for unlimited Listen state. However, at
    5620             :                  * least for now, this is mapped to a Listen state for one
    5621             :                  * hour.
    5622             :                  */
    5623         204 :                 timeout = 3600;
    5624             :         }
    5625         209 :         eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
    5626         209 :         wpa_s->p2p_long_listen = 0;
    5627             : 
    5628             :         /*
    5629             :          * Stop previous find/listen operation to avoid trying to request a new
    5630             :          * remain-on-channel operation while the driver is still running the
    5631             :          * previous one.
    5632             :          */
    5633         209 :         if (wpa_s->global->p2p)
    5634         209 :                 p2p_stop_find(wpa_s->global->p2p);
    5635             : 
    5636         209 :         res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
    5637         209 :         if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
    5638         208 :                 wpa_s->p2p_long_listen = timeout * 1000;
    5639         208 :                 eloop_register_timeout(timeout, 0,
    5640             :                                        wpas_p2p_long_listen_timeout,
    5641             :                                        wpa_s, NULL);
    5642             :         }
    5643             : 
    5644         209 :         return res;
    5645             : }
    5646             : 
    5647             : 
    5648        1087 : int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
    5649             :                           u8 *buf, size_t len, int p2p_group)
    5650             : {
    5651             :         struct wpabuf *p2p_ie;
    5652             :         int ret;
    5653             : 
    5654        1087 :         if (wpa_s->global->p2p_disabled)
    5655           3 :                 return -1;
    5656        1084 :         if (wpa_s->global->p2p == NULL)
    5657           0 :                 return -1;
    5658        1084 :         if (bss == NULL)
    5659          17 :                 return -1;
    5660             : 
    5661        1067 :         p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
    5662        1067 :         ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
    5663             :                                p2p_group, p2p_ie);
    5664        1067 :         wpabuf_free(p2p_ie);
    5665             : 
    5666        1067 :         return ret;
    5667             : }
    5668             : 
    5669             : 
    5670         713 : int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
    5671             :                           const u8 *dst, const u8 *bssid,
    5672             :                           const u8 *ie, size_t ie_len, int ssi_signal)
    5673             : {
    5674         713 :         if (wpa_s->global->p2p_disabled)
    5675           0 :                 return 0;
    5676         713 :         if (wpa_s->global->p2p == NULL)
    5677           0 :                 return 0;
    5678             : 
    5679         713 :         switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
    5680             :                                  ie, ie_len)) {
    5681             :         case P2P_PREQ_NOT_P2P:
    5682           0 :                 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
    5683             :                                  ssi_signal);
    5684             :                 /* fall through */
    5685             :         case P2P_PREQ_MALFORMED:
    5686             :         case P2P_PREQ_NOT_LISTEN:
    5687             :         case P2P_PREQ_NOT_PROCESSED:
    5688             :         default: /* make gcc happy */
    5689         710 :                 return 0;
    5690             :         case P2P_PREQ_PROCESSED:
    5691           3 :                 return 1;
    5692             :         }
    5693             : }
    5694             : 
    5695             : 
    5696         534 : void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
    5697             :                         const u8 *sa, const u8 *bssid,
    5698             :                         u8 category, const u8 *data, size_t len, int freq)
    5699             : {
    5700         534 :         if (wpa_s->global->p2p_disabled)
    5701           0 :                 return;
    5702         534 :         if (wpa_s->global->p2p == NULL)
    5703           0 :                 return;
    5704             : 
    5705         534 :         p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
    5706             :                       freq);
    5707             : }
    5708             : 
    5709             : 
    5710        1184 : void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
    5711             : {
    5712        1184 :         if (wpa_s->global->p2p_disabled)
    5713           1 :                 return;
    5714        1183 :         if (wpa_s->global->p2p == NULL)
    5715           0 :                 return;
    5716             : 
    5717        1183 :         p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
    5718             : }
    5719             : 
    5720             : 
    5721         126 : void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
    5722             : {
    5723         126 :         p2p_group_deinit(wpa_s->p2p_group);
    5724         126 :         wpa_s->p2p_group = NULL;
    5725             : 
    5726         126 :         wpa_s->ap_configured_cb = NULL;
    5727         126 :         wpa_s->ap_configured_cb_ctx = NULL;
    5728         126 :         wpa_s->ap_configured_cb_data = NULL;
    5729         126 :         wpa_s->connect_without_scan = NULL;
    5730         126 : }
    5731             : 
    5732             : 
    5733           2 : int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
    5734             : {
    5735           2 :         wpa_s->p2p_long_listen = 0;
    5736             : 
    5737           2 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5738           0 :                 return -1;
    5739             : 
    5740           2 :         return p2p_reject(wpa_s->global->p2p, addr);
    5741             : }
    5742             : 
    5743             : 
    5744             : /* Invite to reinvoke a persistent group */
    5745          19 : int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
    5746             :                     struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
    5747             :                     int ht40, int vht, int pref_freq)
    5748             : {
    5749             :         enum p2p_invite_role role;
    5750          19 :         u8 *bssid = NULL;
    5751          19 :         int force_freq = 0;
    5752             :         int res;
    5753          19 :         int no_pref_freq_given = pref_freq == 0;
    5754             : 
    5755          19 :         wpa_s->global->p2p_invite_group = NULL;
    5756          19 :         if (peer_addr)
    5757          19 :                 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
    5758             :         else
    5759           0 :                 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
    5760             : 
    5761          19 :         wpa_s->p2p_persistent_go_freq = freq;
    5762          19 :         wpa_s->p2p_go_ht40 = !!ht40;
    5763          19 :         if (ssid->mode == WPAS_MODE_P2P_GO) {
    5764           9 :                 role = P2P_INVITE_ROLE_GO;
    5765           9 :                 if (peer_addr == NULL) {
    5766           0 :                         wpa_printf(MSG_DEBUG, "P2P: Missing peer "
    5767             :                                    "address in invitation command");
    5768           0 :                         return -1;
    5769             :                 }
    5770           9 :                 if (wpas_p2p_create_iface(wpa_s)) {
    5771           1 :                         if (wpas_p2p_add_group_interface(wpa_s,
    5772             :                                                          WPA_IF_P2P_GO) < 0) {
    5773           0 :                                 wpa_printf(MSG_ERROR, "P2P: Failed to "
    5774             :                                            "allocate a new interface for the "
    5775             :                                            "group");
    5776           0 :                                 return -1;
    5777             :                         }
    5778           1 :                         bssid = wpa_s->pending_interface_addr;
    5779             :                 } else
    5780           8 :                         bssid = wpa_s->own_addr;
    5781             :         } else {
    5782          10 :                 role = P2P_INVITE_ROLE_CLIENT;
    5783          10 :                 peer_addr = ssid->bssid;
    5784             :         }
    5785          19 :         wpa_s->pending_invite_ssid_id = ssid->id;
    5786             : 
    5787          19 :         res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
    5788             :                                    role == P2P_INVITE_ROLE_GO);
    5789          19 :         if (res)
    5790           0 :                 return res;
    5791             : 
    5792          19 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5793           0 :                 return -1;
    5794             : 
    5795          19 :         if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
    5796           0 :             no_pref_freq_given && pref_freq > 0 &&
    5797           0 :             wpa_s->num_multichan_concurrent > 1 &&
    5798           0 :             wpas_p2p_num_unused_channels(wpa_s) > 0) {
    5799           0 :                 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
    5800             :                            pref_freq);
    5801           0 :                 pref_freq = 0;
    5802             :         }
    5803             : 
    5804          38 :         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
    5805          19 :                           ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
    5806             :                           1, pref_freq, -1);
    5807             : }
    5808             : 
    5809             : 
    5810             : /* Invite to join an active group */
    5811           4 : int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
    5812             :                           const u8 *peer_addr, const u8 *go_dev_addr)
    5813             : {
    5814           4 :         struct wpa_global *global = wpa_s->global;
    5815             :         enum p2p_invite_role role;
    5816           4 :         u8 *bssid = NULL;
    5817             :         struct wpa_ssid *ssid;
    5818             :         int persistent;
    5819           4 :         int freq = 0, force_freq = 0, pref_freq = 0;
    5820             :         int res;
    5821             : 
    5822           4 :         wpa_s->p2p_persistent_go_freq = 0;
    5823           4 :         wpa_s->p2p_go_ht40 = 0;
    5824           4 :         wpa_s->p2p_go_vht = 0;
    5825             : 
    5826           4 :         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    5827           4 :                 if (os_strcmp(wpa_s->ifname, ifname) == 0)
    5828           4 :                         break;
    5829             :         }
    5830           4 :         if (wpa_s == NULL) {
    5831           0 :                 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
    5832           0 :                 return -1;
    5833             :         }
    5834             : 
    5835           4 :         ssid = wpa_s->current_ssid;
    5836           4 :         if (ssid == NULL) {
    5837           0 :                 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
    5838             :                            "invitation");
    5839           0 :                 return -1;
    5840             :         }
    5841             : 
    5842           4 :         wpa_s->global->p2p_invite_group = wpa_s;
    5843           6 :         persistent = ssid->p2p_persistent_group &&
    5844           4 :                 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
    5845           2 :                                         ssid->ssid, ssid->ssid_len);
    5846             : 
    5847           4 :         if (ssid->mode == WPAS_MODE_P2P_GO) {
    5848           4 :                 role = P2P_INVITE_ROLE_ACTIVE_GO;
    5849           4 :                 bssid = wpa_s->own_addr;
    5850           4 :                 if (go_dev_addr == NULL)
    5851           4 :                         go_dev_addr = wpa_s->global->p2p_dev_addr;
    5852           4 :                 freq = ssid->frequency;
    5853             :         } else {
    5854           0 :                 role = P2P_INVITE_ROLE_CLIENT;
    5855           0 :                 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
    5856           0 :                         wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
    5857             :                                    "invite to current group");
    5858           0 :                         return -1;
    5859             :                 }
    5860           0 :                 bssid = wpa_s->bssid;
    5861           0 :                 if (go_dev_addr == NULL &&
    5862           0 :                     !is_zero_ether_addr(wpa_s->go_dev_addr))
    5863           0 :                         go_dev_addr = wpa_s->go_dev_addr;
    5864           0 :                 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
    5865           0 :                         (int) wpa_s->assoc_freq;
    5866             :         }
    5867           4 :         wpa_s->parent->pending_invite_ssid_id = -1;
    5868             : 
    5869           4 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5870           0 :                 return -1;
    5871             : 
    5872           4 :         res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
    5873             :                                    role == P2P_INVITE_ROLE_ACTIVE_GO);
    5874           4 :         if (res)
    5875           0 :                 return res;
    5876           4 :         wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
    5877             : 
    5878           8 :         return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
    5879           4 :                           ssid->ssid, ssid->ssid_len, force_freq,
    5880             :                           go_dev_addr, persistent, pref_freq, -1);
    5881             : }
    5882             : 
    5883             : 
    5884         906 : void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
    5885             : {
    5886         906 :         struct wpa_ssid *ssid = wpa_s->current_ssid;
    5887             :         const char *ssid_txt;
    5888             :         u8 go_dev_addr[ETH_ALEN];
    5889         906 :         int network_id = -1;
    5890             :         int persistent;
    5891             :         int freq;
    5892             :         u8 ip[3 * 4];
    5893             :         char ip_addr[100];
    5894             : 
    5895         906 :         if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
    5896         840 :                 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    5897         840 :                                      wpa_s->parent, NULL);
    5898             :         }
    5899             : 
    5900         906 :         if (!wpa_s->show_group_started || !ssid)
    5901        1697 :                 return;
    5902             : 
    5903         115 :         wpa_s->show_group_started = 0;
    5904             : 
    5905         115 :         ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
    5906         115 :         os_memset(go_dev_addr, 0, ETH_ALEN);
    5907         115 :         if (ssid->bssid_set)
    5908          93 :                 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
    5909         115 :         persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
    5910             :                                                ssid->ssid_len);
    5911         115 :         os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
    5912             : 
    5913         115 :         if (wpa_s->global->p2p_group_formation == wpa_s)
    5914           0 :                 wpa_s->global->p2p_group_formation = NULL;
    5915             : 
    5916         115 :         freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
    5917           0 :                 (int) wpa_s->assoc_freq;
    5918             : 
    5919         115 :         ip_addr[0] = '\0';
    5920         115 :         if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
    5921         888 :                 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
    5922             :                             "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
    5923         296 :                             ip[0], ip[1], ip[2], ip[3],
    5924         296 :                             ip[4], ip[5], ip[6], ip[7],
    5925         296 :                             ip[8], ip[9], ip[10], ip[11]);
    5926             :         }
    5927             : 
    5928         230 :         if (ssid->passphrase == NULL && ssid->psk_set) {
    5929             :                 char psk[65];
    5930         115 :                 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
    5931         920 :                 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
    5932             :                                "%s client ssid=\"%s\" freq=%d psk=%s "
    5933             :                                "go_dev_addr=" MACSTR "%s%s",
    5934         115 :                                wpa_s->ifname, ssid_txt, freq, psk,
    5935         690 :                                MAC2STR(go_dev_addr),
    5936             :                                persistent ? " [PERSISTENT]" : "", ip_addr);
    5937             :         } else {
    5938           0 :                 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
    5939             :                                "%s client ssid=\"%s\" freq=%d "
    5940             :                                "passphrase=\"%s\" go_dev_addr=" MACSTR "%s%s",
    5941           0 :                                wpa_s->ifname, ssid_txt, freq,
    5942           0 :                                ssid->passphrase ? ssid->passphrase : "",
    5943           0 :                                MAC2STR(go_dev_addr),
    5944             :                                persistent ? " [PERSISTENT]" : "", ip_addr);
    5945             :         }
    5946             : 
    5947         115 :         if (persistent)
    5948          35 :                 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
    5949             :                                                              ssid, go_dev_addr);
    5950         115 :         if (network_id < 0)
    5951          80 :                 network_id = ssid->id;
    5952         115 :         wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
    5953             : }
    5954             : 
    5955             : 
    5956           1 : int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
    5957             :                           u32 interval1, u32 duration2, u32 interval2)
    5958             : {
    5959             :         int ret;
    5960             : 
    5961           1 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5962           0 :                 return -1;
    5963             : 
    5964           2 :         if (wpa_s->wpa_state < WPA_ASSOCIATED ||
    5965           2 :             wpa_s->current_ssid == NULL ||
    5966           1 :             wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
    5967           0 :                 return -1;
    5968             : 
    5969           2 :         ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
    5970           1 :                                wpa_s->own_addr, wpa_s->assoc_freq,
    5971             :                                duration1, interval1, duration2, interval2);
    5972           1 :         if (ret == 0)
    5973           1 :                 wpa_s->waiting_presence_resp = 1;
    5974             : 
    5975           1 :         return ret;
    5976             : }
    5977             : 
    5978             : 
    5979           4 : int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
    5980             :                         unsigned int interval)
    5981             : {
    5982           4 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    5983           0 :                 return -1;
    5984             : 
    5985           4 :         return p2p_ext_listen(wpa_s->global->p2p, period, interval);
    5986             : }
    5987             : 
    5988             : 
    5989           0 : static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
    5990             : {
    5991           0 :         if (wpa_s->current_ssid == NULL) {
    5992             :                 /*
    5993             :                  * current_ssid can be cleared when P2P client interface gets
    5994             :                  * disconnected, so assume this interface was used as P2P
    5995             :                  * client.
    5996             :                  */
    5997           0 :                 return 1;
    5998             :         }
    5999           0 :         return wpa_s->current_ssid->p2p_group &&
    6000           0 :                 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
    6001             : }
    6002             : 
    6003             : 
    6004           1 : static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
    6005             : {
    6006           1 :         struct wpa_supplicant *wpa_s = eloop_ctx;
    6007             : 
    6008           1 :         if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
    6009           0 :                 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
    6010             :                            "disabled");
    6011           1 :                 return;
    6012             :         }
    6013             : 
    6014           1 :         wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
    6015             :                    "group");
    6016           1 :         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
    6017             : }
    6018             : 
    6019             : 
    6020        1439 : static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
    6021             : {
    6022             :         int timeout;
    6023             : 
    6024        1439 :         if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
    6025           0 :                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
    6026             : 
    6027        1439 :         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
    6028         974 :                 return;
    6029             : 
    6030         465 :         timeout = wpa_s->conf->p2p_group_idle;
    6031         465 :         if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
    6032           0 :             (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
    6033         227 :             timeout = P2P_MAX_CLIENT_IDLE;
    6034             : 
    6035         465 :         if (timeout == 0)
    6036         235 :                 return;
    6037             : 
    6038         230 :         if (timeout < 0) {
    6039           0 :                 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
    6040           0 :                         timeout = 0; /* special client mode no-timeout */
    6041             :                 else
    6042           0 :                         return;
    6043             :         }
    6044             : 
    6045         230 :         if (wpa_s->p2p_in_provisioning) {
    6046             :                 /*
    6047             :                  * Use the normal group formation timeout during the
    6048             :                  * provisioning phase to avoid terminating this process too
    6049             :                  * early due to group idle timeout.
    6050             :                  */
    6051           8 :                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
    6052             :                            "during provisioning");
    6053           8 :                 return;
    6054             :         }
    6055             : 
    6056         222 :         if (wpa_s->show_group_started) {
    6057             :                 /*
    6058             :                  * Use the normal group formation timeout between the end of
    6059             :                  * the provisioning phase and completion of 4-way handshake to
    6060             :                  * avoid terminating this process too early due to group idle
    6061             :                  * timeout.
    6062             :                  */
    6063         103 :                 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
    6064             :                            "while waiting for initial 4-way handshake to "
    6065             :                            "complete");
    6066         103 :                 return;
    6067             :         }
    6068             : 
    6069         119 :         wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
    6070             :                    timeout);
    6071         119 :         eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
    6072             :                                wpa_s, NULL);
    6073             : }
    6074             : 
    6075             : 
    6076             : /* Returns 1 if the interface was removed */
    6077        1090 : int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
    6078             :                           u16 reason_code, const u8 *ie, size_t ie_len,
    6079             :                           int locally_generated)
    6080             : {
    6081        1090 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    6082           3 :                 return 0;
    6083             : 
    6084        1087 :         if (!locally_generated)
    6085         154 :                 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
    6086             :                                  ie_len);
    6087             : 
    6088        1172 :         if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
    6089         159 :             wpa_s->current_ssid &&
    6090         143 :             wpa_s->current_ssid->p2p_group &&
    6091          69 :             wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
    6092          69 :                 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
    6093             :                            "session is ending");
    6094          69 :                 if (wpas_p2p_group_delete(wpa_s,
    6095             :                                           P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
    6096             :                     > 0)
    6097           6 :                         return 1;
    6098             :         }
    6099             : 
    6100        1081 :         return 0;
    6101             : }
    6102             : 
    6103             : 
    6104           4 : void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
    6105             :                              u16 reason_code, const u8 *ie, size_t ie_len,
    6106             :                              int locally_generated)
    6107             : {
    6108           4 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    6109           4 :                 return;
    6110             : 
    6111           4 :         if (!locally_generated)
    6112           3 :                 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
    6113             :                                    ie_len);
    6114             : }
    6115             : 
    6116             : 
    6117       17475 : void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
    6118             : {
    6119       17475 :         struct p2p_data *p2p = wpa_s->global->p2p;
    6120             : 
    6121       17475 :         if (p2p == NULL)
    6122           0 :                 return;
    6123             : 
    6124       17475 :         if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
    6125           0 :                 return;
    6126             : 
    6127       17475 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
    6128           3 :                 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
    6129             : 
    6130       17475 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
    6131           1 :                 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
    6132             : 
    6133       34950 :         if (wpa_s->wps &&
    6134       17475 :             (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
    6135           1 :                 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
    6136             : 
    6137       17475 :         if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
    6138           1 :                 p2p_set_uuid(p2p, wpa_s->wps->uuid);
    6139             : 
    6140       17475 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
    6141           1 :                 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
    6142           1 :                 p2p_set_model_name(p2p, wpa_s->conf->model_name);
    6143           1 :                 p2p_set_model_number(p2p, wpa_s->conf->model_number);
    6144           1 :                 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
    6145             :         }
    6146             : 
    6147       17475 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
    6148           4 :                 p2p_set_sec_dev_types(p2p,
    6149           2 :                                       (void *) wpa_s->conf->sec_device_type,
    6150           2 :                                       wpa_s->conf->num_sec_device_types);
    6151             : 
    6152       17475 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
    6153             :                 int i;
    6154           1 :                 p2p_remove_wps_vendor_extensions(p2p);
    6155          11 :                 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
    6156          10 :                         if (wpa_s->conf->wps_vendor_ext[i] == NULL)
    6157          10 :                                 continue;
    6158           0 :                         p2p_add_wps_vendor_extension(
    6159           0 :                                 p2p, wpa_s->conf->wps_vendor_ext[i]);
    6160             :                 }
    6161             :         }
    6162             : 
    6163       17477 :         if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
    6164           4 :             wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
    6165             :                 char country[3];
    6166           2 :                 country[0] = wpa_s->conf->country[0];
    6167           2 :                 country[1] = wpa_s->conf->country[1];
    6168           2 :                 country[2] = 0x04;
    6169           2 :                 p2p_set_country(p2p, country);
    6170             :         }
    6171             : 
    6172       17475 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
    6173           0 :                 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
    6174           0 :                                      wpa_s->conf->p2p_ssid_postfix ?
    6175           0 :                                      os_strlen(wpa_s->conf->p2p_ssid_postfix) :
    6176             :                                      0);
    6177             :         }
    6178             : 
    6179       17475 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
    6180           0 :                 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
    6181             : 
    6182       17475 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
    6183             :                 u8 reg_class, channel;
    6184             :                 int ret;
    6185             :                 unsigned int r;
    6186           0 :                 if (wpa_s->conf->p2p_listen_reg_class &&
    6187           0 :                     wpa_s->conf->p2p_listen_channel) {
    6188           0 :                         reg_class = wpa_s->conf->p2p_listen_reg_class;
    6189           0 :                         channel = wpa_s->conf->p2p_listen_channel;
    6190             :                 } else {
    6191           0 :                         reg_class = 81;
    6192             :                         /*
    6193             :                          * Pick one of the social channels randomly as the
    6194             :                          * listen channel.
    6195             :                          */
    6196           0 :                         os_get_random((u8 *) &r, sizeof(r));
    6197           0 :                         channel = 1 + (r % 3) * 5;
    6198             :                 }
    6199           0 :                 ret = p2p_set_listen_channel(p2p, reg_class, channel);
    6200           0 :                 if (ret)
    6201           0 :                         wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
    6202             :                                    "failed: %d", ret);
    6203             :         }
    6204       17475 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
    6205             :                 u8 op_reg_class, op_channel, cfg_op_channel;
    6206           6 :                 int ret = 0;
    6207             :                 unsigned int r;
    6208          10 :                 if (wpa_s->conf->p2p_oper_reg_class &&
    6209           4 :                     wpa_s->conf->p2p_oper_channel) {
    6210           4 :                         op_reg_class = wpa_s->conf->p2p_oper_reg_class;
    6211           4 :                         op_channel = wpa_s->conf->p2p_oper_channel;
    6212           4 :                         cfg_op_channel = 1;
    6213             :                 } else {
    6214           2 :                         op_reg_class = 81;
    6215             :                         /*
    6216             :                          * Use random operation channel from (1, 6, 11)
    6217             :                          *if no other preference is indicated.
    6218             :                          */
    6219           2 :                         os_get_random((u8 *) &r, sizeof(r));
    6220           2 :                         op_channel = 1 + (r % 3) * 5;
    6221           2 :                         cfg_op_channel = 0;
    6222             :                 }
    6223           6 :                 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
    6224             :                                            cfg_op_channel);
    6225           6 :                 if (ret)
    6226           2 :                         wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
    6227             :                                    "failed: %d", ret);
    6228             :         }
    6229             : 
    6230       17475 :         if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
    6231        3790 :                 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
    6232        3790 :                                       wpa_s->conf->p2p_pref_chan) < 0) {
    6233           0 :                         wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
    6234             :                                    "update failed");
    6235             :                 }
    6236             : 
    6237        3790 :                 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
    6238           0 :                         wpa_printf(MSG_ERROR, "P2P: No GO channel list "
    6239             :                                    "update failed");
    6240             :                 }
    6241             :         }
    6242             : }
    6243             : 
    6244             : 
    6245           0 : int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
    6246             :                      int duration)
    6247             : {
    6248           0 :         if (!wpa_s->ap_iface)
    6249           0 :                 return -1;
    6250           0 :         return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
    6251             :                                    duration);
    6252             : }
    6253             : 
    6254             : 
    6255           0 : int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
    6256             : {
    6257           0 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    6258           0 :                 return -1;
    6259             : 
    6260           0 :         wpa_s->global->cross_connection = enabled;
    6261           0 :         p2p_set_cross_connect(wpa_s->global->p2p, enabled);
    6262             : 
    6263           0 :         if (!enabled) {
    6264             :                 struct wpa_supplicant *iface;
    6265             : 
    6266           0 :                 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
    6267             :                 {
    6268           0 :                         if (iface->cross_connect_enabled == 0)
    6269           0 :                                 continue;
    6270             : 
    6271           0 :                         iface->cross_connect_enabled = 0;
    6272           0 :                         iface->cross_connect_in_use = 0;
    6273           0 :                         wpa_msg_global(iface->parent, MSG_INFO,
    6274             :                                        P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
    6275           0 :                                        iface->ifname,
    6276           0 :                                        iface->cross_connect_uplink);
    6277             :                 }
    6278             :         }
    6279             : 
    6280           0 :         return 0;
    6281             : }
    6282             : 
    6283             : 
    6284         815 : static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
    6285             : {
    6286             :         struct wpa_supplicant *iface;
    6287             : 
    6288         815 :         if (!uplink->global->cross_connection)
    6289        1630 :                 return;
    6290             : 
    6291           0 :         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
    6292           0 :                 if (!iface->cross_connect_enabled)
    6293           0 :                         continue;
    6294           0 :                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
    6295             :                     0)
    6296           0 :                         continue;
    6297           0 :                 if (iface->ap_iface == NULL)
    6298           0 :                         continue;
    6299           0 :                 if (iface->cross_connect_in_use)
    6300           0 :                         continue;
    6301             : 
    6302           0 :                 iface->cross_connect_in_use = 1;
    6303           0 :                 wpa_msg_global(iface->parent, MSG_INFO,
    6304             :                                P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
    6305           0 :                                iface->ifname, iface->cross_connect_uplink);
    6306             :         }
    6307             : }
    6308             : 
    6309             : 
    6310        1411 : static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
    6311             : {
    6312             :         struct wpa_supplicant *iface;
    6313             : 
    6314        2863 :         for (iface = uplink->global->ifaces; iface; iface = iface->next) {
    6315        1452 :                 if (!iface->cross_connect_enabled)
    6316        1452 :                         continue;
    6317           0 :                 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
    6318             :                     0)
    6319           0 :                         continue;
    6320           0 :                 if (!iface->cross_connect_in_use)
    6321           0 :                         continue;
    6322             : 
    6323           0 :                 wpa_msg_global(iface->parent, MSG_INFO,
    6324             :                                P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
    6325           0 :                                iface->ifname, iface->cross_connect_uplink);
    6326           0 :                 iface->cross_connect_in_use = 0;
    6327             :         }
    6328        1411 : }
    6329             : 
    6330             : 
    6331         963 : void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
    6332             : {
    6333        1800 :         if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
    6334        1654 :             wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
    6335         817 :             wpa_s->cross_connect_disallowed)
    6336         148 :                 wpas_p2p_disable_cross_connect(wpa_s);
    6337             :         else
    6338         815 :                 wpas_p2p_enable_cross_connect(wpa_s);
    6339        1800 :         if (!wpa_s->ap_iface &&
    6340         837 :             eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
    6341           1 :                 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
    6342         963 : }
    6343             : 
    6344             : 
    6345        1263 : void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
    6346             : {
    6347        1263 :         wpas_p2p_disable_cross_connect(wpa_s);
    6348        2322 :         if (!wpa_s->ap_iface &&
    6349        1059 :             !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
    6350             :                                          wpa_s, NULL))
    6351        1059 :                 wpas_p2p_set_group_idle_timeout(wpa_s);
    6352        1263 : }
    6353             : 
    6354             : 
    6355         115 : static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
    6356             : {
    6357             :         struct wpa_supplicant *iface;
    6358             : 
    6359         115 :         if (!wpa_s->global->cross_connection)
    6360         230 :                 return;
    6361             : 
    6362           0 :         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
    6363           0 :                 if (iface == wpa_s)
    6364           0 :                         continue;
    6365           0 :                 if (iface->drv_flags &
    6366             :                     WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
    6367           0 :                         continue;
    6368           0 :                 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
    6369           0 :                         continue;
    6370             : 
    6371           0 :                 wpa_s->cross_connect_enabled = 1;
    6372           0 :                 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
    6373             :                            sizeof(wpa_s->cross_connect_uplink));
    6374           0 :                 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
    6375             :                            "%s to %s whenever uplink is available",
    6376           0 :                            wpa_s->ifname, wpa_s->cross_connect_uplink);
    6377             : 
    6378           0 :                 if (iface->ap_iface || iface->current_ssid == NULL ||
    6379           0 :                     iface->current_ssid->mode != WPAS_MODE_INFRA ||
    6380           0 :                     iface->cross_connect_disallowed ||
    6381           0 :                     iface->wpa_state != WPA_COMPLETED)
    6382             :                         break;
    6383             : 
    6384           0 :                 wpa_s->cross_connect_in_use = 1;
    6385           0 :                 wpa_msg_global(wpa_s->parent, MSG_INFO,
    6386             :                                P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
    6387           0 :                                wpa_s->ifname, wpa_s->cross_connect_uplink);
    6388           0 :                 break;
    6389             :         }
    6390             : }
    6391             : 
    6392             : 
    6393           5 : int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
    6394             : {
    6395           9 :         if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
    6396           4 :             !wpa_s->p2p_in_provisioning)
    6397           2 :                 return 0; /* not P2P client operation */
    6398             : 
    6399           3 :         wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
    6400             :                    "session overlap");
    6401           3 :         if (wpa_s != wpa_s->parent)
    6402           1 :                 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
    6403           3 :         wpas_p2p_group_formation_failed(wpa_s);
    6404           3 :         return 1;
    6405             : }
    6406             : 
    6407             : 
    6408           5 : void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
    6409             : {
    6410           5 :         struct wpa_supplicant *wpa_s = eloop_ctx;
    6411           5 :         wpas_p2p_notif_pbc_overlap(wpa_s);
    6412           5 : }
    6413             : 
    6414             : 
    6415        2683 : void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
    6416             : {
    6417             :         struct p2p_channels chan, cli_chan;
    6418             :         struct wpa_supplicant *ifs;
    6419             : 
    6420        2683 :         if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
    6421           0 :                 return;
    6422             : 
    6423        2683 :         os_memset(&chan, 0, sizeof(chan));
    6424        2683 :         os_memset(&cli_chan, 0, sizeof(cli_chan));
    6425        2683 :         if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
    6426           0 :                 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
    6427             :                            "channel list");
    6428           0 :                 return;
    6429             :         }
    6430             : 
    6431        2683 :         p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
    6432             : 
    6433        5389 :         for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
    6434             :                 int freq;
    6435        3065 :                 if (!ifs->current_ssid ||
    6436         559 :                     !ifs->current_ssid->p2p_group ||
    6437         291 :                     (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
    6438          91 :                      ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
    6439        2597 :                                 continue;
    6440         109 :                 freq = ifs->current_ssid->frequency;
    6441         109 :                 if (freq_included(&chan, freq)) {
    6442         104 :                         wpa_dbg(ifs, MSG_DEBUG,
    6443             :                                 "P2P GO operating frequency %d MHz in valid range",
    6444             :                                 freq);
    6445         104 :                         continue;
    6446             :                 }
    6447             : 
    6448           5 :                 wpa_dbg(ifs, MSG_DEBUG,
    6449             :                         "P2P GO operating in invalid frequency %d MHz",       freq);
    6450             :                 /* TODO: Consider using CSA or removing the group within
    6451             :                  * wpa_supplicant */
    6452           5 :                 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
    6453             :         }
    6454             : }
    6455             : 
    6456             : 
    6457           0 : static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
    6458             :                                      struct wpa_scan_results *scan_res)
    6459             : {
    6460           0 :         wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
    6461           0 : }
    6462             : 
    6463             : 
    6464        1903 : int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
    6465             : {
    6466        1903 :         struct wpa_global *global = wpa_s->global;
    6467        1903 :         int found = 0;
    6468             :         const u8 *peer;
    6469             : 
    6470        1903 :         if (global->p2p == NULL)
    6471           0 :                 return -1;
    6472             : 
    6473        1903 :         wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
    6474             : 
    6475        1903 :         if (wpa_s->pending_interface_name[0] &&
    6476           0 :             !is_zero_ether_addr(wpa_s->pending_interface_addr))
    6477           0 :                 found = 1;
    6478             : 
    6479        1903 :         peer = p2p_get_go_neg_peer(global->p2p);
    6480        1903 :         if (peer) {
    6481          12 :                 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
    6482          12 :                            MACSTR, MAC2STR(peer));
    6483           2 :                 p2p_unauthorize(global->p2p, peer);
    6484           2 :                 found = 1;
    6485             :         }
    6486             : 
    6487        1903 :         if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
    6488           0 :                 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
    6489           0 :                 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
    6490           0 :                 found = 1;
    6491             :         }
    6492             : 
    6493        1903 :         if (wpa_s->pending_pd_before_join) {
    6494           0 :                 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
    6495           0 :                 wpa_s->pending_pd_before_join = 0;
    6496           0 :                 found = 1;
    6497             :         }
    6498             : 
    6499        1903 :         wpas_p2p_stop_find(wpa_s);
    6500             : 
    6501        3803 :         for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    6502        1908 :                 if (wpa_s == global->p2p_group_formation &&
    6503           4 :                     (wpa_s->p2p_in_provisioning ||
    6504           0 :                      wpa_s->parent->pending_interface_type ==
    6505             :                      WPA_IF_P2P_CLIENT)) {
    6506           4 :                         wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
    6507             :                                    "formation found - cancelling",
    6508           4 :                                    wpa_s->ifname);
    6509           4 :                         found = 1;
    6510           4 :                         eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    6511           4 :                                              wpa_s->parent, NULL);
    6512           4 :                         if (wpa_s->p2p_in_provisioning) {
    6513           4 :                                 wpas_group_formation_completed(wpa_s, 0);
    6514           4 :                                 break;
    6515             :                         }
    6516           0 :                         wpas_p2p_group_delete(wpa_s,
    6517             :                                               P2P_GROUP_REMOVAL_REQUESTED);
    6518           0 :                         break;
    6519        1900 :                 } else if (wpa_s->p2p_in_invitation) {
    6520           1 :                         wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
    6521           1 :                                    wpa_s->ifname);
    6522           1 :                         found = 1;
    6523           1 :                         wpas_p2p_group_formation_failed(wpa_s);
    6524             :                 }
    6525             :         }
    6526             : 
    6527        1903 :         if (!found) {
    6528        1896 :                 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
    6529        1896 :                 return -1;
    6530             :         }
    6531             : 
    6532           7 :         return 0;
    6533             : }
    6534             : 
    6535             : 
    6536           1 : void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
    6537             : {
    6538           1 :         if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
    6539           1 :                 return;
    6540             : 
    6541           1 :         wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
    6542             :                    "being available anymore");
    6543           1 :         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
    6544             : }
    6545             : 
    6546             : 
    6547           0 : void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
    6548             :                                    int freq_24, int freq_5, int freq_overall)
    6549             : {
    6550           0 :         struct p2p_data *p2p = wpa_s->global->p2p;
    6551           0 :         if (p2p == NULL)
    6552           0 :                 return;
    6553           0 :         p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
    6554             : }
    6555             : 
    6556             : 
    6557           0 : int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
    6558             : {
    6559             :         u8 peer[ETH_ALEN];
    6560           0 :         struct p2p_data *p2p = wpa_s->global->p2p;
    6561             : 
    6562           0 :         if (p2p == NULL)
    6563           0 :                 return -1;
    6564             : 
    6565           0 :         if (hwaddr_aton(addr, peer))
    6566           0 :                 return -1;
    6567             : 
    6568           0 :         return p2p_unauthorize(p2p, peer);
    6569             : }
    6570             : 
    6571             : 
    6572             : /**
    6573             :  * wpas_p2p_disconnect - Disconnect from a P2P Group
    6574             :  * @wpa_s: Pointer to wpa_supplicant data
    6575             :  * Returns: 0 on success, -1 on failure
    6576             :  *
    6577             :  * This can be used to disconnect from a group in which the local end is a P2P
    6578             :  * Client or to end a P2P Group in case the local end is the Group Owner. If a
    6579             :  * virtual network interface was created for this group, that interface will be
    6580             :  * removed. Otherwise, only the configured P2P group network will be removed
    6581             :  * from the interface.
    6582             :  */
    6583         174 : int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
    6584             : {
    6585             : 
    6586         174 :         if (wpa_s == NULL)
    6587           2 :                 return -1;
    6588             : 
    6589         344 :         return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
    6590         172 :                 -1 : 0;
    6591             : }
    6592             : 
    6593             : 
    6594        1308 : int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
    6595             : {
    6596             :         int ret;
    6597             : 
    6598        1308 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    6599           2 :                 return 0;
    6600             : 
    6601        1306 :         ret = p2p_in_progress(wpa_s->global->p2p);
    6602        1306 :         if (ret == 0) {
    6603             :                 /*
    6604             :                  * Check whether there is an ongoing WPS provisioning step (or
    6605             :                  * other parts of group formation) on another interface since
    6606             :                  * p2p_in_progress() does not report this to avoid issues for
    6607             :                  * scans during such provisioning step.
    6608             :                  */
    6609        1535 :                 if (wpa_s->global->p2p_group_formation &&
    6610         229 :                     wpa_s->global->p2p_group_formation != wpa_s) {
    6611           1 :                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
    6612             :                                 "in group formation",
    6613             :                                 wpa_s->global->p2p_group_formation->ifname);
    6614           1 :                         ret = 1;
    6615             :                 }
    6616             :         }
    6617             : 
    6618        1306 :         if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
    6619             :                 struct os_reltime now;
    6620           0 :                 os_get_reltime(&now);
    6621           0 :                 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
    6622             :                                        P2P_MAX_INITIAL_CONN_WAIT_GO)) {
    6623             :                         /* Wait for the first client has expired */
    6624           0 :                         wpa_s->global->p2p_go_wait_client.sec = 0;
    6625             :                 } else {
    6626           0 :                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
    6627           0 :                         ret = 1;
    6628             :                 }
    6629             :         }
    6630             : 
    6631        1306 :         return ret;
    6632             : }
    6633             : 
    6634             : 
    6635         811 : void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
    6636             :                               struct wpa_ssid *ssid)
    6637             : {
    6638         816 :         if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
    6639           5 :             eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    6640           5 :                                  wpa_s->parent, NULL) > 0) {
    6641             :                 /**
    6642             :                  * Remove the network by scheduling the group formation
    6643             :                  * timeout to happen immediately. The teardown code
    6644             :                  * needs to be scheduled to run asynch later so that we
    6645             :                  * don't delete data from under ourselves unexpectedly.
    6646             :                  * Calling wpas_p2p_group_formation_timeout directly
    6647             :                  * causes a series of crashes in WPS failure scenarios.
    6648             :                  */
    6649           1 :                 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
    6650             :                            "P2P group network getting removed");
    6651           1 :                 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
    6652           1 :                                        wpa_s->parent, NULL);
    6653             :         }
    6654         811 : }
    6655             : 
    6656             : 
    6657         350 : struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
    6658             :                                           const u8 *addr, const u8 *ssid,
    6659             :                                           size_t ssid_len)
    6660             : {
    6661             :         struct wpa_ssid *s;
    6662             :         size_t i;
    6663             : 
    6664         383 :         for (s = wpa_s->conf->ssid; s; s = s->next) {
    6665         103 :                 if (s->disabled != 2)
    6666          29 :                         continue;
    6667          98 :                 if (ssid &&
    6668          48 :                     (ssid_len != s->ssid_len ||
    6669          24 :                      os_memcmp(ssid, s->ssid, ssid_len) != 0))
    6670           0 :                         continue;
    6671          74 :                 if (addr == NULL) {
    6672          20 :                         if (s->mode == WPAS_MODE_P2P_GO)
    6673          20 :                                 return s;
    6674           0 :                         continue;
    6675             :                 }
    6676          54 :                 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
    6677          27 :                         return s; /* peer is GO in the persistent group */
    6678          27 :                 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
    6679           0 :                         continue;
    6680          29 :                 for (i = 0; i < s->num_p2p_clients; i++) {
    6681          25 :                         if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
    6682             :                                       addr, ETH_ALEN) == 0)
    6683          23 :                                 return s; /* peer is P2P client in persistent
    6684             :                                            * group */
    6685             :                 }
    6686             :         }
    6687             : 
    6688         280 :         return NULL;
    6689             : }
    6690             : 
    6691             : 
    6692         134 : void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
    6693             :                                        const u8 *addr)
    6694             : {
    6695         134 :         if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
    6696         134 :                                  wpa_s->parent, NULL) > 0) {
    6697             :                 /*
    6698             :                  * This can happen if WPS provisioning step is not terminated
    6699             :                  * cleanly (e.g., P2P Client does not send WSC_Done). Since the
    6700             :                  * peer was able to connect, there is no need to time out group
    6701             :                  * formation after this, though. In addition, this is used with
    6702             :                  * the initial connection wait on the GO as a separate formation
    6703             :                  * timeout and as such, expected to be hit after the initial WPS
    6704             :                  * provisioning step.
    6705             :                  */
    6706          77 :                 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
    6707             :         }
    6708         134 :         if (!wpa_s->p2p_go_group_formation_completed) {
    6709          83 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
    6710          83 :                 wpa_s->p2p_go_group_formation_completed = 1;
    6711          83 :                 wpa_s->global->p2p_group_formation = NULL;
    6712          83 :                 wpa_s->p2p_in_provisioning = 0;
    6713          83 :                 wpa_s->p2p_in_invitation = 0;
    6714             :         }
    6715         134 :         wpa_s->global->p2p_go_wait_client.sec = 0;
    6716         134 :         if (addr == NULL)
    6717         152 :                 return;
    6718         116 :         wpas_p2p_add_persistent_group_client(wpa_s, addr);
    6719             : }
    6720             : 
    6721             : 
    6722           0 : static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
    6723             :                                         int group_added)
    6724             : {
    6725           0 :         struct wpa_supplicant *group = wpa_s;
    6726           0 :         if (wpa_s->global->p2p_group_formation)
    6727           0 :                 group = wpa_s->global->p2p_group_formation;
    6728           0 :         wpa_s = wpa_s->parent;
    6729           0 :         offchannel_send_action_done(wpa_s);
    6730           0 :         if (group_added)
    6731           0 :                 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
    6732           0 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
    6733           0 :         wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
    6734           0 :                          wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
    6735             :                          0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
    6736             :                          wpa_s->p2p_persistent_id,
    6737           0 :                          wpa_s->p2p_pd_before_go_neg,
    6738           0 :                          wpa_s->p2p_go_ht40,
    6739           0 :                          wpa_s->p2p_go_vht);
    6740           0 : }
    6741             : 
    6742             : 
    6743         308 : int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
    6744             : {
    6745         308 :         if (!wpa_s->p2p_fallback_to_go_neg ||
    6746           0 :             wpa_s->p2p_in_provisioning <= 5)
    6747         308 :                 return 0;
    6748             : 
    6749           0 :         if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
    6750           0 :                 return 0; /* peer operating as a GO */
    6751             : 
    6752           0 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
    6753             :                 "fallback to GO Negotiation");
    6754           0 :         wpas_p2p_fallback_to_go_neg(wpa_s, 1);
    6755             : 
    6756           0 :         return 1;
    6757             : }
    6758             : 
    6759             : 
    6760         207 : unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
    6761             : {
    6762             :         struct wpa_supplicant *ifs;
    6763             : 
    6764         207 :         if (wpa_s->wpa_state > WPA_SCANNING) {
    6765           5 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
    6766             :                         "concurrent operation",
    6767             :                         P2P_CONCURRENT_SEARCH_DELAY);
    6768           5 :                 return P2P_CONCURRENT_SEARCH_DELAY;
    6769             :         }
    6770             : 
    6771         404 :         dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
    6772             :                          radio_list) {
    6773         202 :                 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
    6774           0 :                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
    6775             :                                 "delay due to concurrent operation on "
    6776             :                                 "interface %s",
    6777             :                                 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
    6778           0 :                         return P2P_CONCURRENT_SEARCH_DELAY;
    6779             :                 }
    6780             :         }
    6781             : 
    6782         202 :         return 0;
    6783             : }
    6784             : 
    6785             : 
    6786           5 : static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
    6787             :                                      struct wpa_ssid *s, const u8 *addr,
    6788             :                                      int iface_addr)
    6789             : {
    6790             :         struct psk_list_entry *psk, *tmp;
    6791           5 :         int changed = 0;
    6792             : 
    6793          10 :         dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
    6794             :                               list) {
    6795           5 :                 if ((iface_addr && !psk->p2p &&
    6796           5 :                      os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
    6797          10 :                     (!iface_addr && psk->p2p &&
    6798           5 :                      os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
    6799           3 :                         wpa_dbg(wpa_s, MSG_DEBUG,
    6800             :                                 "P2P: Remove persistent group PSK list entry for "
    6801             :                                 MACSTR " p2p=%u",
    6802             :                                 MAC2STR(psk->addr), psk->p2p);
    6803           3 :                         dl_list_del(&psk->list);
    6804           3 :                         os_free(psk);
    6805           3 :                         changed++;
    6806             :                 }
    6807             :         }
    6808             : 
    6809           5 :         return changed;
    6810             : }
    6811             : 
    6812             : 
    6813           8 : void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
    6814             :                          const u8 *p2p_dev_addr,
    6815             :                          const u8 *psk, size_t psk_len)
    6816             : {
    6817           8 :         struct wpa_ssid *ssid = wpa_s->current_ssid;
    6818             :         struct wpa_ssid *persistent;
    6819             :         struct psk_list_entry *p, *last;
    6820             : 
    6821           8 :         if (psk_len != sizeof(p->psk))
    6822           0 :                 return;
    6823             : 
    6824           8 :         if (p2p_dev_addr) {
    6825           8 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
    6826             :                         " p2p_dev_addr=" MACSTR,
    6827             :                         MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
    6828           8 :                 if (is_zero_ether_addr(p2p_dev_addr))
    6829           1 :                         p2p_dev_addr = NULL;
    6830             :         } else {
    6831           0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
    6832             :                         MAC2STR(mac_addr));
    6833             :         }
    6834             : 
    6835           8 :         if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
    6836           4 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
    6837             :                 /* To be added to persistent group once created */
    6838           4 :                 if (wpa_s->global->add_psk == NULL) {
    6839           4 :                         wpa_s->global->add_psk = os_zalloc(sizeof(*p));
    6840           4 :                         if (wpa_s->global->add_psk == NULL)
    6841           0 :                                 return;
    6842             :                 }
    6843           4 :                 p = wpa_s->global->add_psk;
    6844           4 :                 if (p2p_dev_addr) {
    6845           4 :                         p->p2p = 1;
    6846           4 :                         os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
    6847             :                 } else {
    6848           0 :                         p->p2p = 0;
    6849           0 :                         os_memcpy(p->addr, mac_addr, ETH_ALEN);
    6850             :                 }
    6851           4 :                 os_memcpy(p->psk, psk, psk_len);
    6852           4 :                 return;
    6853             :         }
    6854             : 
    6855           4 :         if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
    6856           2 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
    6857           2 :                 return;
    6858             :         }
    6859             : 
    6860           2 :         persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
    6861             :                                              ssid->ssid_len);
    6862           2 :         if (!persistent) {
    6863           0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
    6864           0 :                 return;
    6865             :         }
    6866             : 
    6867           2 :         p = os_zalloc(sizeof(*p));
    6868           2 :         if (p == NULL)
    6869           0 :                 return;
    6870           2 :         if (p2p_dev_addr) {
    6871           2 :                 p->p2p = 1;
    6872           2 :                 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
    6873             :         } else {
    6874           0 :                 p->p2p = 0;
    6875           0 :                 os_memcpy(p->addr, mac_addr, ETH_ALEN);
    6876             :         }
    6877           2 :         os_memcpy(p->psk, psk, psk_len);
    6878             : 
    6879           2 :         if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
    6880           0 :             (last = dl_list_last(&persistent->psk_list,
    6881             :                                  struct psk_list_entry, list))) {
    6882           0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
    6883             :                         MACSTR " (p2p=%u) to make room for a new one",
    6884             :                         MAC2STR(last->addr), last->p2p);
    6885           0 :                 dl_list_del(&last->list);
    6886           0 :                 os_free(last);
    6887             :         }
    6888             : 
    6889           2 :         wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
    6890             :                                   p2p_dev_addr ? p2p_dev_addr : mac_addr,
    6891             :                                   p2p_dev_addr == NULL);
    6892           2 :         if (p2p_dev_addr) {
    6893           2 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
    6894             :                         MACSTR, MAC2STR(p2p_dev_addr));
    6895             :         } else {
    6896           0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
    6897             :                         MAC2STR(mac_addr));
    6898             :         }
    6899           2 :         dl_list_add(&persistent->psk_list, &p->list);
    6900             : 
    6901           2 :         if (wpa_s->parent->conf->update_config &&
    6902           0 :             wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
    6903           0 :                 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
    6904             : }
    6905             : 
    6906             : 
    6907           3 : static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
    6908             :                                 struct wpa_ssid *s, const u8 *addr,
    6909             :                                 int iface_addr)
    6910             : {
    6911             :         int res;
    6912             : 
    6913           3 :         res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
    6914           3 :         if (res > 0 && wpa_s->conf->update_config &&
    6915           0 :             wpa_config_write(wpa_s->confname, wpa_s->conf))
    6916           0 :                 wpa_dbg(wpa_s, MSG_DEBUG,
    6917             :                         "P2P: Failed to update configuration");
    6918           3 : }
    6919             : 
    6920             : 
    6921           4 : static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
    6922             :                                       const u8 *peer, int iface_addr)
    6923             : {
    6924             :         struct hostapd_data *hapd;
    6925             :         struct hostapd_wpa_psk *psk, *prev, *rem;
    6926             :         struct sta_info *sta;
    6927             : 
    6928           8 :         if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
    6929           4 :             wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
    6930           4 :                 return;
    6931             : 
    6932             :         /* Remove per-station PSK entry */
    6933           4 :         hapd = wpa_s->ap_iface->bss[0];
    6934           4 :         prev = NULL;
    6935           4 :         psk = hapd->conf->ssid.wpa_psk;
    6936          16 :         while (psk) {
    6937           8 :                 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
    6938           8 :                     (!iface_addr &&
    6939           8 :                      os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
    6940           3 :                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
    6941             :                                 MACSTR " iface_addr=%d",
    6942             :                                 MAC2STR(peer), iface_addr);
    6943           3 :                         if (prev)
    6944           0 :                                 prev->next = psk->next;
    6945             :                         else
    6946           3 :                                 hapd->conf->ssid.wpa_psk = psk->next;
    6947           3 :                         rem = psk;
    6948           3 :                         psk = psk->next;
    6949           3 :                         os_free(rem);
    6950             :                 } else {
    6951           5 :                         prev = psk;
    6952           5 :                         psk = psk->next;
    6953             :                 }
    6954             :         }
    6955             : 
    6956             :         /* Disconnect from group */
    6957           4 :         if (iface_addr)
    6958           0 :                 sta = ap_get_sta(hapd, peer);
    6959             :         else
    6960           4 :                 sta = ap_get_sta_p2p(hapd, peer);
    6961           4 :         if (sta) {
    6962           4 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
    6963             :                         " (iface_addr=%d) from group",
    6964             :                         MAC2STR(peer), iface_addr);
    6965           4 :                 hostapd_drv_sta_deauth(hapd, sta->addr,
    6966             :                                        WLAN_REASON_DEAUTH_LEAVING);
    6967           4 :                 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
    6968             :         }
    6969             : }
    6970             : 
    6971             : 
    6972           4 : void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
    6973             :                             int iface_addr)
    6974             : {
    6975             :         struct wpa_ssid *s;
    6976             :         struct wpa_supplicant *w;
    6977             : 
    6978           4 :         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
    6979             : 
    6980             :         /* Remove from any persistent group */
    6981          11 :         for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
    6982           7 :                 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
    6983           4 :                         continue;
    6984           3 :                 if (!iface_addr)
    6985           3 :                         wpas_remove_persistent_peer(wpa_s, s, peer, 0);
    6986           3 :                 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
    6987             :         }
    6988             : 
    6989             :         /* Remove from any operating group */
    6990           8 :         for (w = wpa_s->global->ifaces; w; w = w->next)
    6991           4 :                 wpas_p2p_remove_client_go(w, peer, iface_addr);
    6992           4 : }
    6993             : 
    6994             : 
    6995           2 : static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
    6996             : {
    6997           2 :         struct wpa_supplicant *wpa_s = eloop_ctx;
    6998           2 :         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
    6999           2 : }
    7000             : 
    7001             : 
    7002           0 : static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
    7003             : {
    7004           0 :         struct wpa_supplicant *wpa_s = eloop_ctx;
    7005             : 
    7006           0 :         wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
    7007           0 :         wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
    7008           0 : }
    7009             : 
    7010             : 
    7011           0 : int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
    7012             :                                         struct wpa_ssid *ssid)
    7013             : {
    7014             :         struct wpa_supplicant *iface;
    7015             : 
    7016           0 :         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
    7017           0 :                 if (!iface->current_ssid ||
    7018           0 :                     iface->current_ssid->frequency == freq ||
    7019           0 :                     (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
    7020           0 :                      !iface->current_ssid->p2p_group))
    7021           0 :                         continue;
    7022             : 
    7023             :                 /* Remove the connection with least priority */
    7024           0 :                 if (!wpas_is_p2p_prioritized(iface)) {
    7025             :                         /* STA connection has priority over existing
    7026             :                          * P2P connection, so remove the interface. */
    7027           0 :                         wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
    7028           0 :                         eloop_register_timeout(0, 0,
    7029             :                                                wpas_p2p_group_freq_conflict,
    7030             :                                                iface, NULL);
    7031             :                         /* If connection in progress is P2P connection, do not
    7032             :                          * proceed for the connection. */
    7033           0 :                         if (wpa_s == iface)
    7034           0 :                                 return -1;
    7035             :                         else
    7036           0 :                                 return 0;
    7037             :                 } else {
    7038             :                         /* P2P connection has priority, disable the STA network
    7039             :                          */
    7040           0 :                         wpa_supplicant_disable_network(wpa_s->global->ifaces,
    7041             :                                                        ssid);
    7042           0 :                         wpa_msg(wpa_s->global->ifaces, MSG_INFO,
    7043             :                                 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
    7044           0 :                         os_memset(wpa_s->global->ifaces->pending_bssid, 0,
    7045             :                                   ETH_ALEN);
    7046             :                         /* If P2P connection is in progress, continue
    7047             :                          * connecting...*/
    7048           0 :                         if (wpa_s == iface)
    7049           0 :                                 return 0;
    7050             :                         else
    7051           0 :                                 return -1;
    7052             :                 }
    7053             :         }
    7054             : 
    7055           0 :         return 0;
    7056             : }
    7057             : 
    7058             : 
    7059           7 : int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
    7060             : {
    7061           7 :         struct wpa_ssid *ssid = wpa_s->current_ssid;
    7062             : 
    7063           7 :         if (ssid == NULL || !ssid->p2p_group)
    7064           1 :                 return 0;
    7065             : 
    7066           8 :         if (wpa_s->p2p_last_4way_hs_fail &&
    7067           2 :             wpa_s->p2p_last_4way_hs_fail == ssid) {
    7068             :                 u8 go_dev_addr[ETH_ALEN];
    7069             :                 struct wpa_ssid *persistent;
    7070             : 
    7071           4 :                 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
    7072           2 :                                               ssid->ssid,
    7073             :                                               ssid->ssid_len) <= 0) {
    7074           0 :                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
    7075           0 :                         goto disconnect;
    7076             :                 }
    7077             : 
    7078           2 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
    7079             :                         MACSTR, MAC2STR(go_dev_addr));
    7080           4 :                 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
    7081           2 :                                                      ssid->ssid,
    7082             :                                                      ssid->ssid_len);
    7083           2 :                 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
    7084           0 :                         wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
    7085           0 :                         goto disconnect;
    7086             :                 }
    7087           2 :                 wpa_msg_global(wpa_s->parent, MSG_INFO,
    7088             :                                P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
    7089             :                                persistent->id);
    7090             :         disconnect:
    7091           2 :                 wpa_s->p2p_last_4way_hs_fail = NULL;
    7092             :                 /*
    7093             :                  * Remove the group from a timeout to avoid issues with caller
    7094             :                  * continuing to use the interface if this is on a P2P group
    7095             :                  * interface.
    7096             :                  */
    7097           2 :                 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
    7098             :                                        wpa_s, NULL);
    7099           2 :                 return 1;
    7100             :         }
    7101             : 
    7102           4 :         wpa_s->p2p_last_4way_hs_fail = ssid;
    7103           4 :         return 0;
    7104             : }
    7105             : 
    7106             : 
    7107             : #ifdef CONFIG_WPS_NFC
    7108             : 
    7109          29 : static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
    7110             :                                              struct wpabuf *p2p)
    7111             : {
    7112             :         struct wpabuf *ret;
    7113             :         size_t wsc_len;
    7114             : 
    7115          29 :         if (p2p == NULL) {
    7116           0 :                 wpabuf_free(wsc);
    7117           0 :                 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
    7118           0 :                 return NULL;
    7119             :         }
    7120             : 
    7121          29 :         wsc_len = wsc ? wpabuf_len(wsc) : 0;
    7122          29 :         ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
    7123          29 :         if (ret == NULL) {
    7124           0 :                 wpabuf_free(wsc);
    7125           0 :                 wpabuf_free(p2p);
    7126           0 :                 return NULL;
    7127             :         }
    7128             : 
    7129          29 :         wpabuf_put_be16(ret, wsc_len);
    7130          29 :         if (wsc)
    7131          28 :                 wpabuf_put_buf(ret, wsc);
    7132          29 :         wpabuf_put_be16(ret, wpabuf_len(p2p));
    7133          29 :         wpabuf_put_buf(ret, p2p);
    7134             : 
    7135          29 :         wpabuf_free(wsc);
    7136          29 :         wpabuf_free(p2p);
    7137          29 :         wpa_hexdump_buf(MSG_DEBUG,
    7138             :                         "P2P: Generated NFC connection handover message", ret);
    7139             : 
    7140          29 :         if (ndef && ret) {
    7141             :                 struct wpabuf *tmp;
    7142          26 :                 tmp = ndef_build_p2p(ret);
    7143          26 :                 wpabuf_free(ret);
    7144          26 :                 if (tmp == NULL) {
    7145           0 :                         wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
    7146           0 :                         return NULL;
    7147             :                 }
    7148          26 :                 ret = tmp;
    7149             :         }
    7150             : 
    7151          29 :         return ret;
    7152             : }
    7153             : 
    7154             : 
    7155          50 : static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
    7156             :                              struct wpa_ssid **ssid, u8 *go_dev_addr)
    7157             : {
    7158             :         struct wpa_supplicant *iface;
    7159             : 
    7160          50 :         if (go_dev_addr)
    7161          29 :                 os_memset(go_dev_addr, 0, ETH_ALEN);
    7162          50 :         if (ssid)
    7163          29 :                 *ssid = NULL;
    7164         196 :         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
    7165          61 :                 if (iface->wpa_state < WPA_ASSOCIATING ||
    7166          33 :                     iface->current_ssid == NULL || iface->assoc_freq == 0 ||
    7167          22 :                     !iface->current_ssid->p2p_group ||
    7168          11 :                     iface->current_ssid->mode != WPAS_MODE_INFRA)
    7169          48 :                         continue;
    7170           2 :                 if (ssid)
    7171           1 :                         *ssid = iface->current_ssid;
    7172           2 :                 if (go_dev_addr)
    7173           1 :                         os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
    7174           2 :                 return iface->assoc_freq;
    7175             :         }
    7176          48 :         return 0;
    7177             : }
    7178             : 
    7179             : 
    7180          10 : struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
    7181             :                                           int ndef)
    7182             : {
    7183             :         struct wpabuf *wsc, *p2p;
    7184             :         struct wpa_ssid *ssid;
    7185             :         u8 go_dev_addr[ETH_ALEN];
    7186          10 :         int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
    7187             : 
    7188          10 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
    7189           0 :                 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
    7190           0 :                 return NULL;
    7191             :         }
    7192             : 
    7193          11 :         if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
    7194           1 :             wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
    7195           1 :                            &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
    7196           0 :                 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
    7197           0 :                 return NULL;
    7198             :         }
    7199             : 
    7200          10 :         if (cli_freq == 0) {
    7201          18 :                 wsc = wps_build_nfc_handover_req_p2p(
    7202          18 :                         wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
    7203             :         } else
    7204           1 :                 wsc = NULL;
    7205          22 :         p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
    7206          11 :                                          go_dev_addr, ssid ? ssid->ssid : NULL,
    7207          11 :                                          ssid ? ssid->ssid_len : 0);
    7208             : 
    7209          10 :         return wpas_p2p_nfc_handover(ndef, wsc, p2p);
    7210             : }
    7211             : 
    7212             : 
    7213          19 : struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
    7214             :                                           int ndef, int tag)
    7215             : {
    7216             :         struct wpabuf *wsc, *p2p;
    7217             :         struct wpa_ssid *ssid;
    7218             :         u8 go_dev_addr[ETH_ALEN];
    7219          19 :         int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
    7220             : 
    7221          19 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    7222           0 :                 return NULL;
    7223             : 
    7224          21 :         if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
    7225           2 :             wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
    7226           2 :                            &wpa_s->conf->wps_nfc_dh_privkey) < 0)
    7227           0 :                 return NULL;
    7228             : 
    7229          19 :         if (cli_freq == 0) {
    7230          75 :                 wsc = wps_build_nfc_handover_sel_p2p(
    7231          19 :                         wpa_s->parent->wps,
    7232           9 :                         tag ? wpa_s->conf->wps_nfc_dev_pw_id :
    7233             :                         DEV_PW_NFC_CONNECTION_HANDOVER,
    7234          19 :                         wpa_s->conf->wps_nfc_dh_pubkey,
    7235           9 :                         tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
    7236             :         } else
    7237           0 :                 wsc = NULL;
    7238          38 :         p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
    7239          19 :                                          go_dev_addr, ssid ? ssid->ssid : NULL,
    7240          19 :                                          ssid ? ssid->ssid_len : 0);
    7241             : 
    7242          19 :         return wpas_p2p_nfc_handover(ndef, wsc, p2p);
    7243             : }
    7244             : 
    7245             : 
    7246           4 : static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
    7247             :                                    struct p2p_nfc_params *params)
    7248             : {
    7249           4 :         wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
    7250             :                    "connection handover (freq=%d)",
    7251             :                    params->go_freq);
    7252             : 
    7253           4 :         if (params->go_freq && params->go_ssid_len) {
    7254           4 :                 wpa_s->p2p_wps_method = WPS_NFC;
    7255           4 :                 wpa_s->pending_join_wps_method = WPS_NFC;
    7256           4 :                 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
    7257           4 :                 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
    7258             :                           ETH_ALEN);
    7259           8 :                 return wpas_p2p_join_start(wpa_s, params->go_freq,
    7260           4 :                                            params->go_ssid,
    7261             :                                            params->go_ssid_len);
    7262             :         }
    7263             : 
    7264           0 :         return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
    7265           0 :                                 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
    7266             :                                 params->go_freq, -1, 0, 1, 1);
    7267             : }
    7268             : 
    7269             : 
    7270           4 : static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
    7271             :                                   struct p2p_nfc_params *params, int tag)
    7272             : {
    7273             :         int res, persistent;
    7274             :         struct wpa_ssid *ssid;
    7275             : 
    7276           4 :         wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
    7277             :                    "connection handover");
    7278           4 :         for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
    7279           4 :                 ssid = wpa_s->current_ssid;
    7280           4 :                 if (ssid == NULL)
    7281           0 :                         continue;
    7282           4 :                 if (ssid->mode != WPAS_MODE_P2P_GO)
    7283           0 :                         continue;
    7284           4 :                 if (wpa_s->ap_iface == NULL)
    7285           0 :                         continue;
    7286           4 :                 break;
    7287             :         }
    7288           4 :         if (wpa_s == NULL) {
    7289           0 :                 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
    7290           0 :                 return -1;
    7291             :         }
    7292             : 
    7293           4 :         if (wpa_s->parent->p2p_oob_dev_pw_id !=
    7294           2 :             DEV_PW_NFC_CONNECTION_HANDOVER &&
    7295           2 :             !wpa_s->parent->p2p_oob_dev_pw) {
    7296           0 :                 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
    7297           0 :                 return -1;
    7298             :         }
    7299          12 :         res = wpas_ap_wps_add_nfc_pw(
    7300           4 :                 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
    7301           4 :                 wpa_s->parent->p2p_oob_dev_pw,
    7302           4 :                 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
    7303           4 :                 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
    7304           4 :         if (res)
    7305           0 :                 return res;
    7306             : 
    7307           4 :         if (!tag) {
    7308           2 :                 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
    7309           2 :                 return 0;
    7310             :         }
    7311             : 
    7312           4 :         if (!params->peer ||
    7313           2 :             !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
    7314           0 :                 return 0;
    7315             : 
    7316          12 :         wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
    7317          12 :                    " to join", MAC2STR(params->peer->p2p_device_addr));
    7318             : 
    7319           2 :         wpa_s->global->p2p_invite_group = wpa_s;
    7320           2 :         persistent = ssid->p2p_persistent_group &&
    7321           0 :                 wpas_p2p_get_persistent(wpa_s->parent,
    7322           0 :                                         params->peer->p2p_device_addr,
    7323           0 :                                         ssid->ssid, ssid->ssid_len);
    7324           2 :         wpa_s->parent->pending_invite_ssid_id = -1;
    7325             : 
    7326           6 :         return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
    7327           2 :                           P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
    7328           4 :                           ssid->ssid, ssid->ssid_len, ssid->frequency,
    7329           2 :                           wpa_s->global->p2p_dev_addr, persistent, 0,
    7330           2 :                           wpa_s->parent->p2p_oob_dev_pw_id);
    7331             : }
    7332             : 
    7333             : 
    7334           8 : static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
    7335             :                                     struct p2p_nfc_params *params,
    7336             :                                     int forced_freq)
    7337             : {
    7338           8 :         wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
    7339             :                    "connection handover");
    7340           8 :         return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
    7341           8 :                                 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
    7342             :                                 forced_freq, -1, 0, 1, 1);
    7343             : }
    7344             : 
    7345             : 
    7346           4 : static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
    7347             :                                     struct p2p_nfc_params *params,
    7348             :                                     int forced_freq)
    7349             : {
    7350             :         int res;
    7351             : 
    7352           4 :         wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
    7353             :                    "connection handover");
    7354           4 :         res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
    7355           4 :                                WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
    7356             :                                forced_freq, -1, 0, 1, 1);
    7357           4 :         if (res)
    7358           0 :                 return res;
    7359             : 
    7360           4 :         res = wpas_p2p_listen(wpa_s, 60);
    7361           4 :         if (res) {
    7362           0 :                 p2p_unauthorize(wpa_s->global->p2p,
    7363           0 :                                 params->peer->p2p_device_addr);
    7364             :         }
    7365             : 
    7366           4 :         return res;
    7367             : }
    7368             : 
    7369             : 
    7370          24 : static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
    7371             :                                             const struct wpabuf *data,
    7372             :                                             int sel, int tag, int forced_freq)
    7373             : {
    7374             :         const u8 *pos, *end;
    7375             :         u16 len, id;
    7376             :         struct p2p_nfc_params params;
    7377             :         int res;
    7378             : 
    7379          24 :         os_memset(&params, 0, sizeof(params));
    7380          24 :         params.sel = sel;
    7381             : 
    7382          24 :         wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
    7383             : 
    7384          24 :         pos = wpabuf_head(data);
    7385          24 :         end = pos + wpabuf_len(data);
    7386             : 
    7387          24 :         if (end - pos < 2) {
    7388           0 :                 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
    7389             :                            "attributes");
    7390           0 :                 return -1;
    7391             :         }
    7392          24 :         len = WPA_GET_BE16(pos);
    7393          24 :         pos += 2;
    7394          24 :         if (pos + len > end) {
    7395           0 :                 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
    7396             :                            "attributes");
    7397           0 :                 return -1;
    7398             :         }
    7399          24 :         params.wsc_attr = pos;
    7400          24 :         params.wsc_len = len;
    7401          24 :         pos += len;
    7402             : 
    7403          24 :         if (end - pos < 2) {
    7404           0 :                 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
    7405             :                            "attributes");
    7406           0 :                 return -1;
    7407             :         }
    7408          24 :         len = WPA_GET_BE16(pos);
    7409          24 :         pos += 2;
    7410          24 :         if (pos + len > end) {
    7411           0 :                 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
    7412             :                            "attributes");
    7413           0 :                 return -1;
    7414             :         }
    7415          24 :         params.p2p_attr = pos;
    7416          24 :         params.p2p_len = len;
    7417          24 :         pos += len;
    7418             : 
    7419          48 :         wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
    7420          24 :                     params.wsc_attr, params.wsc_len);
    7421          48 :         wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
    7422          24 :                     params.p2p_attr, params.p2p_len);
    7423          24 :         if (pos < end) {
    7424           0 :                 wpa_hexdump(MSG_DEBUG,
    7425             :                             "P2P: Ignored extra data after P2P attributes",
    7426           0 :                             pos, end - pos);
    7427             :         }
    7428             : 
    7429          24 :         res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
    7430          24 :         if (res)
    7431           0 :                 return res;
    7432             : 
    7433          24 :         if (params.next_step == NO_ACTION)
    7434           0 :                 return 0;
    7435             : 
    7436          24 :         if (params.next_step == BOTH_GO) {
    7437          12 :                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
    7438          12 :                         MAC2STR(params.peer->p2p_device_addr));
    7439           2 :                 return 0;
    7440             :         }
    7441             : 
    7442          22 :         if (params.next_step == PEER_CLIENT) {
    7443           1 :                 if (!is_zero_ether_addr(params.go_dev_addr)) {
    7444          13 :                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
    7445             :                                 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
    7446             :                                 " ssid=\"%s\"",
    7447           6 :                                 MAC2STR(params.peer->p2p_device_addr),
    7448             :                                 params.go_freq,
    7449           6 :                                 MAC2STR(params.go_dev_addr),
    7450             :                                 wpa_ssid_txt(params.go_ssid,
    7451             :                                              params.go_ssid_len));
    7452             :                 } else {
    7453           0 :                         wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
    7454             :                                 "peer=" MACSTR " freq=%d",
    7455           0 :                                 MAC2STR(params.peer->p2p_device_addr),
    7456             :                                 params.go_freq);
    7457             :                 }
    7458           1 :                 return 0;
    7459             :         }
    7460             : 
    7461          21 :         if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
    7462           6 :                 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
    7463           6 :                         MACSTR, MAC2STR(params.peer->p2p_device_addr));
    7464           1 :                 return 0;
    7465             :         }
    7466             : 
    7467          20 :         wpabuf_free(wpa_s->p2p_oob_dev_pw);
    7468          20 :         wpa_s->p2p_oob_dev_pw = NULL;
    7469             : 
    7470          20 :         if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
    7471           0 :                 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
    7472             :                            "received");
    7473           0 :                 return -1;
    7474             :         }
    7475             : 
    7476          20 :         id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
    7477          20 :         wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
    7478          20 :         wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
    7479             :                     params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
    7480          20 :         os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
    7481             :                   params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
    7482          20 :         wpa_s->p2p_peer_oob_pk_hash_known = 1;
    7483             : 
    7484          20 :         if (tag) {
    7485           8 :                 if (id < 0x10) {
    7486           0 :                         wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
    7487             :                                    "peer OOB Device Password Id %u", id);
    7488           0 :                         return -1;
    7489             :                 }
    7490           8 :                 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
    7491             :                            "Device Password Id %u", id);
    7492           8 :                 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
    7493             :                                 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
    7494           8 :                                 params.oob_dev_pw_len -
    7495             :                                 WPS_OOB_PUBKEY_HASH_LEN - 2);
    7496           8 :                 wpa_s->p2p_oob_dev_pw_id = id;
    7497           8 :                 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
    7498             :                         params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
    7499           8 :                         params.oob_dev_pw_len -
    7500             :                         WPS_OOB_PUBKEY_HASH_LEN - 2);
    7501           8 :                 if (wpa_s->p2p_oob_dev_pw == NULL)
    7502           0 :                         return -1;
    7503             : 
    7504           8 :                 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
    7505           0 :                     wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
    7506           0 :                                    &wpa_s->conf->wps_nfc_dh_privkey) < 0)
    7507           0 :                         return -1;
    7508             :         } else {
    7509          12 :                 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
    7510             :                            "without Device Password");
    7511          12 :                 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
    7512             :         }
    7513             : 
    7514          20 :         switch (params.next_step) {
    7515             :         case NO_ACTION:
    7516             :         case BOTH_GO:
    7517             :         case PEER_CLIENT:
    7518             :                 /* already covered above */
    7519           0 :                 return 0;
    7520             :         case JOIN_GROUP:
    7521           4 :                 return wpas_p2p_nfc_join_group(wpa_s, &params);
    7522             :         case AUTH_JOIN:
    7523           4 :                 return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
    7524             :         case INIT_GO_NEG:
    7525           8 :                 return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
    7526             :         case RESP_GO_NEG:
    7527             :                 /* TODO: use own OOB Dev Pw */
    7528           4 :                 return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
    7529             :         }
    7530             : 
    7531           0 :         return -1;
    7532             : }
    7533             : 
    7534             : 
    7535           8 : int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
    7536             :                              const struct wpabuf *data, int forced_freq)
    7537             : {
    7538           8 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    7539           0 :                 return -1;
    7540             : 
    7541           8 :         return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
    7542             : }
    7543             : 
    7544             : 
    7545          16 : int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
    7546             :                                  const struct wpabuf *req,
    7547             :                                  const struct wpabuf *sel, int forced_freq)
    7548             : {
    7549             :         struct wpabuf *tmp;
    7550             :         int ret;
    7551             : 
    7552          16 :         if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
    7553           0 :                 return -1;
    7554             : 
    7555          16 :         wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
    7556             : 
    7557          16 :         wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
    7558             :                           wpabuf_head(req), wpabuf_len(req));
    7559          16 :         wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
    7560             :                           wpabuf_head(sel), wpabuf_len(sel));
    7561          16 :         if (forced_freq)
    7562           0 :                 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
    7563          16 :         tmp = ndef_parse_p2p(init ? sel : req);
    7564          16 :         if (tmp == NULL) {
    7565           0 :                 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
    7566           0 :                 return -1;
    7567             :         }
    7568             : 
    7569          16 :         ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
    7570             :                                                forced_freq);
    7571          16 :         wpabuf_free(tmp);
    7572             : 
    7573          16 :         return ret;
    7574             : }
    7575             : 
    7576             : 
    7577          13 : int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
    7578             : {
    7579             :         const u8 *if_addr;
    7580          13 :         int go_intent = wpa_s->conf->p2p_go_intent;
    7581             :         struct wpa_supplicant *iface;
    7582             : 
    7583          13 :         if (wpa_s->global->p2p == NULL)
    7584           0 :                 return -1;
    7585             : 
    7586          13 :         if (!enabled) {
    7587           3 :                 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
    7588           6 :                 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
    7589             :                 {
    7590           3 :                         if (!iface->ap_iface)
    7591           3 :                                 continue;
    7592           0 :                         hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
    7593             :                 }
    7594           3 :                 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
    7595             :                                                  0, NULL);
    7596           3 :                 if (wpa_s->p2p_nfc_tag_enabled)
    7597           3 :                         wpas_p2p_remove_pending_group_interface(wpa_s);
    7598           3 :                 wpa_s->p2p_nfc_tag_enabled = 0;
    7599           3 :                 return 0;
    7600             :         }
    7601             : 
    7602          10 :         if (wpa_s->global->p2p_disabled)
    7603           0 :                 return -1;
    7604             : 
    7605          20 :         if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
    7606          20 :             wpa_s->conf->wps_nfc_dh_privkey == NULL ||
    7607          20 :             wpa_s->conf->wps_nfc_dev_pw == NULL ||
    7608          10 :             wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
    7609           0 :                 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
    7610             :                            "to allow static handover cases");
    7611           0 :                 return -1;
    7612             :         }
    7613             : 
    7614          10 :         wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
    7615             : 
    7616          10 :         wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
    7617          10 :         wpabuf_free(wpa_s->p2p_oob_dev_pw);
    7618          10 :         wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
    7619          10 :         if (wpa_s->p2p_oob_dev_pw == NULL)
    7620           0 :                 return -1;
    7621          10 :         wpa_s->p2p_peer_oob_pk_hash_known = 0;
    7622             : 
    7623          20 :         if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
    7624          10 :             wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
    7625             :                 /*
    7626             :                  * P2P Group Interface present and the command came on group
    7627             :                  * interface, so enable the token for the current interface.
    7628             :                  */
    7629           0 :                 wpa_s->create_p2p_iface = 0;
    7630             :         } else {
    7631          10 :                 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
    7632             :         }
    7633             : 
    7634          10 :         if (wpa_s->create_p2p_iface) {
    7635             :                 enum wpa_driver_if_type iftype;
    7636             :                 /* Prepare to add a new interface for the group */
    7637           3 :                 iftype = WPA_IF_P2P_GROUP;
    7638           3 :                 if (go_intent == 15)
    7639           0 :                         iftype = WPA_IF_P2P_GO;
    7640           3 :                 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
    7641           0 :                         wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
    7642             :                                    "interface for the group");
    7643           0 :                         return -1;
    7644             :                 }
    7645             : 
    7646           3 :                 if_addr = wpa_s->pending_interface_addr;
    7647             :         } else
    7648           7 :                 if_addr = wpa_s->own_addr;
    7649             : 
    7650          10 :         wpa_s->p2p_nfc_tag_enabled = enabled;
    7651             : 
    7652          20 :         for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
    7653             :                 struct hostapd_data *hapd;
    7654          10 :                 if (iface->ap_iface == NULL)
    7655           9 :                         continue;
    7656           1 :                 hapd = iface->ap_iface->bss[0];
    7657           1 :                 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
    7658           2 :                 hapd->conf->wps_nfc_dh_pubkey =
    7659           1 :                         wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
    7660           1 :                 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
    7661           2 :                 hapd->conf->wps_nfc_dh_privkey =
    7662           1 :                         wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
    7663           1 :                 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
    7664           2 :                 hapd->conf->wps_nfc_dev_pw =
    7665           1 :                         wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
    7666           1 :                 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
    7667             : 
    7668           1 :                 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
    7669           0 :                         wpa_dbg(iface, MSG_DEBUG,
    7670             :                                 "P2P: Failed to enable NFC Tag for GO");
    7671             :                 }
    7672             :         }
    7673          20 :         p2p_set_authorized_oob_dev_pw_id(
    7674          20 :                 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
    7675             :                 if_addr);
    7676             : 
    7677          10 :         return 0;
    7678             : }
    7679             : 
    7680             : #endif /* CONFIG_WPS_NFC */

Generated by: LCOV version 1.10