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 1426431149 Lines: 3917 4814 81.4 %
Date: 2015-03-15 Functions: 227 239 95.0 %

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

Generated by: LCOV version 1.10