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