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