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