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 1443382998 Lines: 3592 4433 81.0 %
Date: 2015-09-27 Functions: 202 212 95.3 %

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

Generated by: LCOV version 1.10