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