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