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