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