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