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