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 "eapol_supp/eapol_supp_sm.h"
26 #include "rsn_supp/wpa.h"
27 #include "wpa_supplicant_i.h"
28 #include "driver_i.h"
29 #include "ap.h"
30 #include "config_ssid.h"
31 #include "config.h"
32 #include "notify.h"
33 #include "scan.h"
34 #include "bss.h"
35 #include "offchannel.h"
36 #include "wps_supplicant.h"
37 #include "p2p_supplicant.h"
38 #include "wifi_display.h"
39 
40 
41 /*
42  * How many times to try to scan to find the GO before giving up on join
43  * request.
44  */
45 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
46 
47 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
48 
49 #ifndef P2P_MAX_CLIENT_IDLE
50 /*
51  * How many seconds to try to reconnect to the GO when connection in P2P client
52  * role has been lost.
53  */
54 #define P2P_MAX_CLIENT_IDLE 10
55 #endif /* P2P_MAX_CLIENT_IDLE */
56 
57 #ifndef P2P_MAX_INITIAL_CONN_WAIT
58 /*
59  * How many seconds to wait for initial 4-way handshake to get completed after
60  * WPS provisioning step.
61  */
62 #define P2P_MAX_INITIAL_CONN_WAIT 10
63 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
64 
65 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
66 /*
67  * How many seconds to wait for initial 4-way handshake to get completed after
68  * WPS provisioning step on the GO. This controls the extra time the P2P
69  * operation is considered to be in progress (e.g., to delay other scans) after
70  * WPS provisioning has been completed on the GO during group formation.
71  */
72 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
73 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
74 
75 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
76 /*
77  * How many seconds to wait for initial 4-way handshake to get completed after
78  * re-invocation of a persistent group on the GO when the client is expected
79  * to connect automatically (no user interaction).
80  */
81 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
82 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
83 
84 #ifndef P2P_CONCURRENT_SEARCH_DELAY
85 #define P2P_CONCURRENT_SEARCH_DELAY 500
86 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
87 
88 #define P2P_MGMT_DEVICE_PREFIX		"p2p-dev-"
89 
90 enum p2p_group_removal_reason {
91 	P2P_GROUP_REMOVAL_UNKNOWN,
92 	P2P_GROUP_REMOVAL_SILENT,
93 	P2P_GROUP_REMOVAL_FORMATION_FAILED,
94 	P2P_GROUP_REMOVAL_REQUESTED,
95 	P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
96 	P2P_GROUP_REMOVAL_UNAVAILABLE,
97 	P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
98 	P2P_GROUP_REMOVAL_PSK_FAILURE,
99 	P2P_GROUP_REMOVAL_FREQ_CONFLICT
100 };
101 
102 
103 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
104 static struct wpa_supplicant *
105 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
106 			 int go);
107 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
108 			       const u8 *ssid, size_t ssid_len);
109 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
110 				   const u8 *ssid, size_t ssid_len);
111 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
112 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
113 			 const u8 *dev_addr, enum p2p_wps_method wps_method,
114 			 int auto_join, int freq,
115 			 const u8 *ssid, size_t ssid_len);
116 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
117 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
118 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
119 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
120 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
121 					     void *timeout_ctx);
122 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
123 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
124 					int group_added);
125 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
126 
127 
128 /*
129  * Get the number of concurrent channels that the HW can operate, but that are
130  * currently not in use by any of the wpa_supplicant interfaces.
131  */
132 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
133 {
134 	int *freqs;
135 	int num, unused;
136 
137 	freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
138 	if (!freqs)
139 		return -1;
140 
141 	num = get_shared_radio_freqs(wpa_s, freqs,
142 				     wpa_s->num_multichan_concurrent);
143 	os_free(freqs);
144 
145 	unused = wpa_s->num_multichan_concurrent - num;
146 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
147 	return unused;
148 }
149 
150 
151 /*
152  * Get the frequencies that are currently in use by one or more of the virtual
153  * interfaces, and that are also valid for P2P operation.
154  */
155 static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
156 				     int *p2p_freqs, unsigned int len)
157 {
158 	int *freqs;
159 	unsigned int num, i, j;
160 
161 	freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
162 	if (!freqs)
163 		return -1;
164 
165 	num = get_shared_radio_freqs(wpa_s, freqs,
166 				     wpa_s->num_multichan_concurrent);
167 
168 	os_memset(p2p_freqs, 0, sizeof(int) * len);
169 
170 	for (i = 0, j = 0; i < num && j < len; i++) {
171 		if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
172 			p2p_freqs[j++] = freqs[i];
173 	}
174 
175 	os_free(freqs);
176 
177 	dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
178 
179 	return j;
180 }
181 
182 
183 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
184 					     int freq)
185 {
186 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
187 		return;
188 	if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
189 	    freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
190 	    wpas_p2p_num_unused_channels(wpa_s) > 0) {
191 		wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
192 			   freq);
193 		freq = 0;
194 	}
195 	p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
196 }
197 
198 
199 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
200 				      struct wpa_scan_results *scan_res)
201 {
202 	size_t i;
203 
204 	if (wpa_s->p2p_scan_work) {
205 		struct wpa_radio_work *work = wpa_s->p2p_scan_work;
206 		wpa_s->p2p_scan_work = NULL;
207 		radio_work_done(work);
208 	}
209 
210 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
211 		return;
212 
213 	wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
214 		   (int) scan_res->num);
215 
216 	for (i = 0; i < scan_res->num; i++) {
217 		struct wpa_scan_res *bss = scan_res->res[i];
218 		struct os_reltime time_tmp_age, entry_ts;
219 		const u8 *ies;
220 		size_t ies_len;
221 
222 		time_tmp_age.sec = bss->age / 1000;
223 		time_tmp_age.usec = (bss->age % 1000) * 1000;
224 		os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
225 
226 		ies = (const u8 *) (bss + 1);
227 		ies_len = bss->ie_len;
228 		if (bss->beacon_ie_len > 0 &&
229 		    !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
230 		    wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
231 			wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
232 				   MACSTR, MAC2STR(bss->bssid));
233 			ies = ies + ies_len;
234 			ies_len = bss->beacon_ie_len;
235 		}
236 
237 
238 		if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
239 					 bss->freq, &entry_ts, bss->level,
240 					 ies, ies_len) > 0)
241 			break;
242 	}
243 
244 	p2p_scan_res_handled(wpa_s->global->p2p);
245 }
246 
247 
248 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
249 {
250 	struct wpa_supplicant *wpa_s = work->wpa_s;
251 	struct wpa_driver_scan_params *params = work->ctx;
252 	int ret;
253 
254 	if (deinit) {
255 		wpa_scan_free_params(params);
256 		return;
257 	}
258 
259 	ret = wpa_drv_scan(wpa_s, params);
260 	wpa_scan_free_params(params);
261 	work->ctx = NULL;
262 	if (ret) {
263 		radio_work_done(work);
264 		return;
265 	}
266 
267 	os_get_reltime(&wpa_s->scan_trigger_time);
268 	wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
269 	wpa_s->own_scan_requested = 1;
270 	wpa_s->p2p_scan_work = work;
271 }
272 
273 
274 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
275 			 unsigned int num_req_dev_types,
276 			 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
277 {
278 	struct wpa_supplicant *wpa_s = ctx;
279 	struct wpa_driver_scan_params *params = NULL;
280 	struct wpabuf *wps_ie, *ies;
281 	size_t ielen;
282 	u8 *n;
283 
284 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
285 		return -1;
286 
287 	if (wpa_s->p2p_scan_work) {
288 		wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
289 		return -1;
290 	}
291 
292 	params = os_zalloc(sizeof(*params));
293 	if (params == NULL)
294 		return -1;
295 
296 	/* P2P Wildcard SSID */
297 	params->num_ssids = 1;
298 	n = os_malloc(P2P_WILDCARD_SSID_LEN);
299 	if (n == NULL)
300 		goto fail;
301 	os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
302 	params->ssids[0].ssid = n;
303 	params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
304 
305 	wpa_s->wps->dev.p2p = 1;
306 	wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
307 					wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
308 					num_req_dev_types, req_dev_types);
309 	if (wps_ie == NULL)
310 		goto fail;
311 
312 	ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
313 	ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
314 	if (ies == NULL) {
315 		wpabuf_free(wps_ie);
316 		goto fail;
317 	}
318 	wpabuf_put_buf(ies, wps_ie);
319 	wpabuf_free(wps_ie);
320 
321 	p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
322 
323 	params->p2p_probe = 1;
324 	n = os_malloc(wpabuf_len(ies));
325 	if (n == NULL) {
326 		wpabuf_free(ies);
327 		goto fail;
328 	}
329 	os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
330 	params->extra_ies = n;
331 	params->extra_ies_len = wpabuf_len(ies);
332 	wpabuf_free(ies);
333 
334 	switch (type) {
335 	case P2P_SCAN_SOCIAL:
336 		params->freqs = os_malloc(4 * sizeof(int));
337 		if (params->freqs == NULL)
338 			goto fail;
339 		params->freqs[0] = 2412;
340 		params->freqs[1] = 2437;
341 		params->freqs[2] = 2462;
342 		params->freqs[3] = 0;
343 		break;
344 	case P2P_SCAN_FULL:
345 		break;
346 	case P2P_SCAN_SOCIAL_PLUS_ONE:
347 		params->freqs = os_malloc(5 * sizeof(int));
348 		if (params->freqs == NULL)
349 			goto fail;
350 		params->freqs[0] = 2412;
351 		params->freqs[1] = 2437;
352 		params->freqs[2] = 2462;
353 		params->freqs[3] = freq;
354 		params->freqs[4] = 0;
355 		break;
356 	}
357 
358 	radio_remove_unstarted_work(wpa_s, "p2p-scan");
359 	if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
360 			   params) < 0)
361 		goto fail;
362 	return 0;
363 
364 fail:
365 	wpa_scan_free_params(params);
366 	return -1;
367 }
368 
369 
370 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
371 {
372 	switch (p2p_group_interface) {
373 	case P2P_GROUP_INTERFACE_PENDING:
374 		return WPA_IF_P2P_GROUP;
375 	case P2P_GROUP_INTERFACE_GO:
376 		return WPA_IF_P2P_GO;
377 	case P2P_GROUP_INTERFACE_CLIENT:
378 		return WPA_IF_P2P_CLIENT;
379 	}
380 
381 	return WPA_IF_P2P_GROUP;
382 }
383 
384 
385 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
386 						  const u8 *ssid,
387 						  size_t ssid_len, int *go)
388 {
389 	struct wpa_ssid *s;
390 
391 	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
392 		for (s = wpa_s->conf->ssid; s; s = s->next) {
393 			if (s->disabled != 0 || !s->p2p_group ||
394 			    s->ssid_len != ssid_len ||
395 			    os_memcmp(ssid, s->ssid, ssid_len) != 0)
396 				continue;
397 			if (s->mode == WPAS_MODE_P2P_GO &&
398 			    s != wpa_s->current_ssid)
399 				continue;
400 			if (go)
401 				*go = s->mode == WPAS_MODE_P2P_GO;
402 			return wpa_s;
403 		}
404 	}
405 
406 	return NULL;
407 }
408 
409 
410 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
411 				 enum p2p_group_removal_reason removal_reason)
412 {
413 	struct wpa_ssid *ssid;
414 	char *gtype;
415 	const char *reason;
416 
417 	ssid = wpa_s->current_ssid;
418 	if (ssid == NULL) {
419 		/*
420 		 * The current SSID was not known, but there may still be a
421 		 * pending P2P group interface waiting for provisioning or a
422 		 * P2P group that is trying to reconnect.
423 		 */
424 		ssid = wpa_s->conf->ssid;
425 		while (ssid) {
426 			if (ssid->p2p_group && ssid->disabled != 2)
427 				break;
428 			ssid = ssid->next;
429 		}
430 		if (ssid == NULL &&
431 			wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
432 		{
433 			wpa_printf(MSG_ERROR, "P2P: P2P group interface "
434 				   "not found");
435 			return -1;
436 		}
437 	}
438 	if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
439 		gtype = "GO";
440 	else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
441 		 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
442 		wpa_s->reassociate = 0;
443 		wpa_s->disconnected = 1;
444 		wpa_supplicant_deauthenticate(wpa_s,
445 					      WLAN_REASON_DEAUTH_LEAVING);
446 		gtype = "client";
447 	} else
448 		gtype = "GO";
449 	if (wpa_s->cross_connect_in_use) {
450 		wpa_s->cross_connect_in_use = 0;
451 		wpa_msg_global(wpa_s->parent, MSG_INFO,
452 			       P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
453 			       wpa_s->ifname, wpa_s->cross_connect_uplink);
454 	}
455 	switch (removal_reason) {
456 	case P2P_GROUP_REMOVAL_REQUESTED:
457 		reason = " reason=REQUESTED";
458 		break;
459 	case P2P_GROUP_REMOVAL_FORMATION_FAILED:
460 		reason = " reason=FORMATION_FAILED";
461 		break;
462 	case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
463 		reason = " reason=IDLE";
464 		break;
465 	case P2P_GROUP_REMOVAL_UNAVAILABLE:
466 		reason = " reason=UNAVAILABLE";
467 		break;
468 	case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
469 		reason = " reason=GO_ENDING_SESSION";
470 		break;
471 	case P2P_GROUP_REMOVAL_PSK_FAILURE:
472 		reason = " reason=PSK_FAILURE";
473 		break;
474 	case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
475 		reason = " reason=FREQ_CONFLICT";
476 		break;
477 	default:
478 		reason = "";
479 		break;
480 	}
481 	if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
482 		wpa_msg_global(wpa_s->parent, MSG_INFO,
483 			       P2P_EVENT_GROUP_REMOVED "%s %s%s",
484 			       wpa_s->ifname, gtype, reason);
485 	}
486 
487 	if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
488 		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
489 	if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
490 		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
491 	if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
492 				 wpa_s->parent, NULL) > 0) {
493 		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
494 			   "timeout");
495 		wpa_s->p2p_in_provisioning = 0;
496 	}
497 
498 	/*
499 	 * Make sure wait for the first client does not remain active after the
500 	 * group has been removed.
501 	 */
502 	wpa_s->global->p2p_go_wait_client.sec = 0;
503 
504 	if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
505 		wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
506 
507 	if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
508 		struct wpa_global *global;
509 		char *ifname;
510 		enum wpa_driver_if_type type;
511 		wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
512 			wpa_s->ifname);
513 		global = wpa_s->global;
514 		ifname = os_strdup(wpa_s->ifname);
515 		type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
516 		wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
517 		wpa_s = global->ifaces;
518 		if (wpa_s && ifname)
519 			wpa_drv_if_remove(wpa_s, type, ifname);
520 		os_free(ifname);
521 		return 1;
522 	}
523 
524 	if (!wpa_s->p2p_go_group_formation_completed) {
525 		wpa_s->global->p2p_group_formation = NULL;
526 		wpa_s->p2p_in_provisioning = 0;
527 	}
528 
529 	wpa_s->show_group_started = 0;
530 	os_free(wpa_s->go_params);
531 	wpa_s->go_params = NULL;
532 
533 	wpa_s->waiting_presence_resp = 0;
534 
535 	wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
536 	if (ssid && (ssid->p2p_group ||
537 		     ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
538 		     (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
539 		int id = ssid->id;
540 		if (ssid == wpa_s->current_ssid) {
541 			wpa_sm_set_config(wpa_s->wpa, NULL);
542 			eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
543 			wpa_s->current_ssid = NULL;
544 		}
545 		/*
546 		 * Networks objects created during any P2P activities are not
547 		 * exposed out as they might/will confuse certain non-P2P aware
548 		 * applications since these network objects won't behave like
549 		 * regular ones.
550 		 *
551 		 * Likewise, we don't send out network removed signals for such
552 		 * network objects.
553 		 */
554 		wpa_config_remove_network(wpa_s->conf, id);
555 		wpa_supplicant_clear_status(wpa_s);
556 		wpa_supplicant_cancel_sched_scan(wpa_s);
557 	} else {
558 		wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
559 			   "found");
560 	}
561 	if (wpa_s->ap_iface)
562 		wpa_supplicant_ap_deinit(wpa_s);
563 	else
564 		wpa_drv_deinit_p2p_cli(wpa_s);
565 
566 	return 0;
567 }
568 
569 
570 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
571 				     u8 *go_dev_addr,
572 				     const u8 *ssid, size_t ssid_len)
573 {
574 	struct wpa_bss *bss;
575 	const u8 *bssid;
576 	struct wpabuf *p2p;
577 	u8 group_capab;
578 	const u8 *addr;
579 
580 	if (wpa_s->go_params)
581 		bssid = wpa_s->go_params->peer_interface_addr;
582 	else
583 		bssid = wpa_s->bssid;
584 
585 	bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
586 	if (bss == NULL && wpa_s->go_params &&
587 	    !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
588 		bss = wpa_bss_get_p2p_dev_addr(
589 			wpa_s, wpa_s->go_params->peer_device_addr);
590 	if (bss == NULL) {
591 		u8 iface_addr[ETH_ALEN];
592 		if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
593 					   iface_addr) == 0)
594 			bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
595 	}
596 	if (bss == NULL) {
597 		wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
598 			   "group is persistent - BSS " MACSTR " not found",
599 			   MAC2STR(bssid));
600 		return 0;
601 	}
602 
603 	p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
604 	if (p2p == NULL)
605 		p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
606 							 P2P_IE_VENDOR_TYPE);
607 	if (p2p == NULL) {
608 		wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
609 			   "group is persistent - BSS " MACSTR
610 			   " did not include P2P IE", MAC2STR(bssid));
611 		wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
612 			    (u8 *) (bss + 1), bss->ie_len);
613 		wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
614 			    ((u8 *) bss + 1) + bss->ie_len,
615 			    bss->beacon_ie_len);
616 		return 0;
617 	}
618 
619 	group_capab = p2p_get_group_capab(p2p);
620 	addr = p2p_get_go_dev_addr(p2p);
621 	wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
622 		   "group_capab=0x%x", group_capab);
623 	if (addr) {
624 		os_memcpy(go_dev_addr, addr, ETH_ALEN);
625 		wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
626 			   MAC2STR(addr));
627 	} else
628 		os_memset(go_dev_addr, 0, ETH_ALEN);
629 	wpabuf_free(p2p);
630 
631 	wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
632 		   "go_dev_addr=" MACSTR,
633 		   MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
634 
635 	return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
636 }
637 
638 
639 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
640 					   struct wpa_ssid *ssid,
641 					   const u8 *go_dev_addr)
642 {
643 	struct wpa_ssid *s;
644 	int changed = 0;
645 
646 	wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
647 		   "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
648 	for (s = wpa_s->conf->ssid; s; s = s->next) {
649 		if (s->disabled == 2 &&
650 		    os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
651 		    s->ssid_len == ssid->ssid_len &&
652 		    os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
653 			break;
654 	}
655 
656 	if (s) {
657 		wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
658 			   "entry");
659 		if (ssid->passphrase && !s->passphrase)
660 			changed = 1;
661 		else if (ssid->passphrase && s->passphrase &&
662 			 os_strcmp(ssid->passphrase, s->passphrase) != 0)
663 			changed = 1;
664 	} else {
665 		wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
666 			   "entry");
667 		changed = 1;
668 		s = wpa_config_add_network(wpa_s->conf);
669 		if (s == NULL)
670 			return -1;
671 
672 		/*
673 		 * Instead of network_added we emit persistent_group_added
674 		 * notification. Also to keep the defense checks in
675 		 * persistent_group obj registration method, we set the
676 		 * relevant flags in s to designate it as a persistent group.
677 		 */
678 		s->p2p_group = 1;
679 		s->p2p_persistent_group = 1;
680 		wpas_notify_persistent_group_added(wpa_s, s);
681 		wpa_config_set_network_defaults(s);
682 	}
683 
684 	s->p2p_group = 1;
685 	s->p2p_persistent_group = 1;
686 	s->disabled = 2;
687 	s->bssid_set = 1;
688 	os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
689 	s->mode = ssid->mode;
690 	s->auth_alg = WPA_AUTH_ALG_OPEN;
691 	s->key_mgmt = WPA_KEY_MGMT_PSK;
692 	s->proto = WPA_PROTO_RSN;
693 	s->pairwise_cipher = WPA_CIPHER_CCMP;
694 	s->export_keys = 1;
695 	if (ssid->passphrase) {
696 		os_free(s->passphrase);
697 		s->passphrase = os_strdup(ssid->passphrase);
698 	}
699 	if (ssid->psk_set) {
700 		s->psk_set = 1;
701 		os_memcpy(s->psk, ssid->psk, 32);
702 	}
703 	if (s->passphrase && !s->psk_set)
704 		wpa_config_update_psk(s);
705 	if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
706 		os_free(s->ssid);
707 		s->ssid = os_malloc(ssid->ssid_len);
708 	}
709 	if (s->ssid) {
710 		s->ssid_len = ssid->ssid_len;
711 		os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
712 	}
713 	if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
714 		dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
715 		wpa_s->global->add_psk = NULL;
716 		changed = 1;
717 	}
718 
719 #ifndef CONFIG_NO_CONFIG_WRITE
720 	if (changed && wpa_s->conf->update_config &&
721 	    wpa_config_write(wpa_s->confname, wpa_s->conf)) {
722 		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
723 	}
724 #endif /* CONFIG_NO_CONFIG_WRITE */
725 
726 	return s->id;
727 }
728 
729 
730 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
731 						 const u8 *addr)
732 {
733 	struct wpa_ssid *ssid, *s;
734 	u8 *n;
735 	size_t i;
736 	int found = 0;
737 
738 	ssid = wpa_s->current_ssid;
739 	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
740 	    !ssid->p2p_persistent_group)
741 		return;
742 
743 	for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
744 		if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
745 			continue;
746 
747 		if (s->ssid_len == ssid->ssid_len &&
748 		    os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
749 			break;
750 	}
751 
752 	if (s == NULL)
753 		return;
754 
755 	for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
756 		if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
757 			      ETH_ALEN) != 0)
758 			continue;
759 
760 		if (i == s->num_p2p_clients - 1)
761 			return; /* already the most recent entry */
762 
763 		/* move the entry to mark it most recent */
764 		os_memmove(s->p2p_client_list + i * ETH_ALEN,
765 			   s->p2p_client_list + (i + 1) * ETH_ALEN,
766 			   (s->num_p2p_clients - i - 1) * ETH_ALEN);
767 		os_memcpy(s->p2p_client_list +
768 			  (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
769 		found = 1;
770 		break;
771 	}
772 
773 	if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
774 		n = os_realloc_array(s->p2p_client_list,
775 				     s->num_p2p_clients + 1, ETH_ALEN);
776 		if (n == NULL)
777 			return;
778 		os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
779 		s->p2p_client_list = n;
780 		s->num_p2p_clients++;
781 	} else if (!found) {
782 		/* Not enough room for an additional entry - drop the oldest
783 		 * entry */
784 		os_memmove(s->p2p_client_list,
785 			   s->p2p_client_list + ETH_ALEN,
786 			   (s->num_p2p_clients - 1) * ETH_ALEN);
787 		os_memcpy(s->p2p_client_list +
788 			  (s->num_p2p_clients - 1) * ETH_ALEN,
789 			  addr, ETH_ALEN);
790 	}
791 
792 #ifndef CONFIG_NO_CONFIG_WRITE
793 	if (wpa_s->parent->conf->update_config &&
794 	    wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
795 		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
796 #endif /* CONFIG_NO_CONFIG_WRITE */
797 }
798 
799 
800 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
801 					   int success)
802 {
803 	struct wpa_ssid *ssid;
804 	const char *ssid_txt;
805 	int client;
806 	int persistent;
807 	u8 go_dev_addr[ETH_ALEN];
808 	int network_id = -1;
809 
810 	/*
811 	 * This callback is likely called for the main interface. Update wpa_s
812 	 * to use the group interface if a new interface was created for the
813 	 * group.
814 	 */
815 	if (wpa_s->global->p2p_group_formation)
816 		wpa_s = wpa_s->global->p2p_group_formation;
817 	if (wpa_s->p2p_go_group_formation_completed) {
818 		wpa_s->global->p2p_group_formation = NULL;
819 		wpa_s->p2p_in_provisioning = 0;
820 	}
821 
822 	if (!success) {
823 		wpa_msg_global(wpa_s->parent, MSG_INFO,
824 			       P2P_EVENT_GROUP_FORMATION_FAILURE);
825 		wpas_p2p_group_delete(wpa_s,
826 				      P2P_GROUP_REMOVAL_FORMATION_FAILED);
827 		return;
828 	}
829 
830 	wpa_msg_global(wpa_s->parent, MSG_INFO,
831 		       P2P_EVENT_GROUP_FORMATION_SUCCESS);
832 
833 	ssid = wpa_s->current_ssid;
834 	if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
835 		ssid->mode = WPAS_MODE_P2P_GO;
836 		p2p_group_notif_formation_done(wpa_s->p2p_group);
837 		wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
838 	}
839 
840 	persistent = 0;
841 	if (ssid) {
842 		ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
843 		client = ssid->mode == WPAS_MODE_INFRA;
844 		if (ssid->mode == WPAS_MODE_P2P_GO) {
845 			persistent = ssid->p2p_persistent_group;
846 			os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
847 				  ETH_ALEN);
848 		} else
849 			persistent = wpas_p2p_persistent_group(wpa_s,
850 							       go_dev_addr,
851 							       ssid->ssid,
852 							       ssid->ssid_len);
853 	} else {
854 		ssid_txt = "";
855 		client = wpa_s->p2p_group_interface ==
856 			P2P_GROUP_INTERFACE_CLIENT;
857 		os_memset(go_dev_addr, 0, ETH_ALEN);
858 	}
859 
860 	wpa_s->show_group_started = 0;
861 	if (client) {
862 		/*
863 		 * Indicate event only after successfully completed 4-way
864 		 * handshake, i.e., when the interface is ready for data
865 		 * packets.
866 		 */
867 		wpa_s->show_group_started = 1;
868 	} else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
869 		char psk[65];
870 		wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
871 		wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
872 			       "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
873 			       MACSTR "%s",
874 			       wpa_s->ifname, ssid_txt, ssid->frequency, psk,
875 			       MAC2STR(go_dev_addr),
876 			       persistent ? " [PERSISTENT]" : "");
877 		wpas_p2p_cross_connect_setup(wpa_s);
878 		wpas_p2p_set_group_idle_timeout(wpa_s);
879 	} else {
880 		wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
881 			       "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
882 			       "go_dev_addr=" MACSTR "%s",
883 			       wpa_s->ifname, ssid_txt,
884 			       ssid ? ssid->frequency : 0,
885 			       ssid && ssid->passphrase ? ssid->passphrase : "",
886 			       MAC2STR(go_dev_addr),
887 			       persistent ? " [PERSISTENT]" : "");
888 		wpas_p2p_cross_connect_setup(wpa_s);
889 		wpas_p2p_set_group_idle_timeout(wpa_s);
890 	}
891 
892 	if (persistent)
893 		network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
894 							     ssid, go_dev_addr);
895 	else {
896 		os_free(wpa_s->global->add_psk);
897 		wpa_s->global->add_psk = NULL;
898 	}
899 	if (network_id < 0 && ssid)
900 		network_id = ssid->id;
901 	if (!client) {
902 		wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
903 		os_get_reltime(&wpa_s->global->p2p_go_wait_client);
904 	}
905 }
906 
907 
908 struct send_action_work {
909 	unsigned int freq;
910 	u8 dst[ETH_ALEN];
911 	u8 src[ETH_ALEN];
912 	u8 bssid[ETH_ALEN];
913 	size_t len;
914 	unsigned int wait_time;
915 	u8 buf[0];
916 };
917 
918 
919 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
920 					      void *timeout_ctx)
921 {
922 	struct wpa_supplicant *wpa_s = eloop_ctx;
923 
924 	if (!wpa_s->p2p_send_action_work)
925 		return;
926 
927 	wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
928 	os_free(wpa_s->p2p_send_action_work->ctx);
929 	radio_work_done(wpa_s->p2p_send_action_work);
930 	wpa_s->p2p_send_action_work = NULL;
931 }
932 
933 
934 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
935 					   unsigned int freq,
936 					   const u8 *dst, const u8 *src,
937 					   const u8 *bssid,
938 					   const u8 *data, size_t data_len,
939 					   enum offchannel_send_action_result
940 					   result)
941 {
942 	enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
943 
944 	if (wpa_s->p2p_send_action_work) {
945 		struct send_action_work *awork;
946 		awork = wpa_s->p2p_send_action_work->ctx;
947 		if (awork->wait_time == 0) {
948 			os_free(awork);
949 			radio_work_done(wpa_s->p2p_send_action_work);
950 			wpa_s->p2p_send_action_work = NULL;
951 		} else {
952 			/*
953 			 * In theory, this should not be needed, but number of
954 			 * places in the P2P code is still using non-zero wait
955 			 * time for the last Action frame in the sequence and
956 			 * some of these do not call send_action_done().
957 			 */
958 			eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
959 					     wpa_s, NULL);
960 			eloop_register_timeout(
961 				0, awork->wait_time * 1000,
962 				wpas_p2p_send_action_work_timeout,
963 				wpa_s, NULL);
964 		}
965 	}
966 
967 	if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
968 		return;
969 
970 	switch (result) {
971 	case OFFCHANNEL_SEND_ACTION_SUCCESS:
972 		res = P2P_SEND_ACTION_SUCCESS;
973 		break;
974 	case OFFCHANNEL_SEND_ACTION_NO_ACK:
975 		res = P2P_SEND_ACTION_NO_ACK;
976 		break;
977 	case OFFCHANNEL_SEND_ACTION_FAILED:
978 		res = P2P_SEND_ACTION_FAILED;
979 		break;
980 	}
981 
982 	p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
983 
984 	if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
985 	    wpa_s->pending_pd_before_join &&
986 	    (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
987 	     os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
988 	    wpa_s->p2p_fallback_to_go_neg) {
989 		wpa_s->pending_pd_before_join = 0;
990 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
991 			"during p2p_connect-auto");
992 		wpas_p2p_fallback_to_go_neg(wpa_s, 0);
993 		return;
994 	}
995 }
996 
997 
998 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
999 {
1000 	struct wpa_supplicant *wpa_s = work->wpa_s;
1001 	struct send_action_work *awork = work->ctx;
1002 
1003 	if (deinit) {
1004 		os_free(awork);
1005 		return;
1006 	}
1007 
1008 	if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1009 				   awork->bssid, awork->buf, awork->len,
1010 				   awork->wait_time,
1011 				   wpas_p2p_send_action_tx_status, 1) < 0) {
1012 		os_free(awork);
1013 		radio_work_done(work);
1014 		return;
1015 	}
1016 	wpa_s->p2p_send_action_work = work;
1017 }
1018 
1019 
1020 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1021 				 unsigned int freq, const u8 *dst,
1022 				 const u8 *src, const u8 *bssid, const u8 *buf,
1023 				 size_t len, unsigned int wait_time)
1024 {
1025 	struct send_action_work *awork;
1026 
1027 	if (wpa_s->p2p_send_action_work) {
1028 		wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1029 		return -1;
1030 	}
1031 
1032 	awork = os_zalloc(sizeof(*awork) + len);
1033 	if (awork == NULL)
1034 		return -1;
1035 
1036 	awork->freq = freq;
1037 	os_memcpy(awork->dst, dst, ETH_ALEN);
1038 	os_memcpy(awork->src, src, ETH_ALEN);
1039 	os_memcpy(awork->bssid, bssid, ETH_ALEN);
1040 	awork->len = len;
1041 	awork->wait_time = wait_time;
1042 	os_memcpy(awork->buf, buf, len);
1043 
1044 	if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1045 			   wpas_send_action_cb, awork) < 0) {
1046 		os_free(awork);
1047 		return -1;
1048 	}
1049 
1050 	return 0;
1051 }
1052 
1053 
1054 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1055 			    const u8 *src, const u8 *bssid, const u8 *buf,
1056 			    size_t len, unsigned int wait_time)
1057 {
1058 	struct wpa_supplicant *wpa_s = ctx;
1059 	int listen_freq = -1, send_freq = -1;
1060 
1061 	if (wpa_s->p2p_listen_work)
1062 		listen_freq = wpa_s->p2p_listen_work->freq;
1063 	if (wpa_s->p2p_send_action_work)
1064 		send_freq = wpa_s->p2p_send_action_work->freq;
1065 	if (listen_freq != (int) freq && send_freq != (int) freq) {
1066 		wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1067 			   listen_freq, send_freq);
1068 		return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1069 					     len, wait_time);
1070 	}
1071 
1072 	wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1073 	return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1074 				      wait_time,
1075 				      wpas_p2p_send_action_tx_status, 1);
1076 }
1077 
1078 
1079 static void wpas_send_action_done(void *ctx)
1080 {
1081 	struct wpa_supplicant *wpa_s = ctx;
1082 
1083 	if (wpa_s->p2p_send_action_work) {
1084 		eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1085 				     wpa_s, NULL);
1086 		os_free(wpa_s->p2p_send_action_work->ctx);
1087 		radio_work_done(wpa_s->p2p_send_action_work);
1088 		wpa_s->p2p_send_action_work = NULL;
1089 	}
1090 
1091 	offchannel_send_action_done(wpa_s);
1092 }
1093 
1094 
1095 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1096 				    struct p2p_go_neg_results *params)
1097 {
1098 	if (wpa_s->go_params == NULL) {
1099 		wpa_s->go_params = os_malloc(sizeof(*params));
1100 		if (wpa_s->go_params == NULL)
1101 			return -1;
1102 	}
1103 	os_memcpy(wpa_s->go_params, params, sizeof(*params));
1104 	return 0;
1105 }
1106 
1107 
1108 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1109 				    struct p2p_go_neg_results *res)
1110 {
1111 	wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1112 		   " dev_addr " MACSTR " wps_method %d",
1113 		   MAC2STR(res->peer_interface_addr),
1114 		   MAC2STR(res->peer_device_addr), res->wps_method);
1115 	wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1116 			  res->ssid, res->ssid_len);
1117 	wpa_supplicant_ap_deinit(wpa_s);
1118 	wpas_copy_go_neg_results(wpa_s, res);
1119 	if (res->wps_method == WPS_PBC) {
1120 		wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1121 #ifdef CONFIG_WPS_NFC
1122 	} else if (res->wps_method == WPS_NFC) {
1123 		wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1124 				   res->peer_interface_addr,
1125 				   wpa_s->parent->p2p_oob_dev_pw,
1126 				   wpa_s->parent->p2p_oob_dev_pw_id, 1,
1127 				   wpa_s->parent->p2p_oob_dev_pw_id ==
1128 				   DEV_PW_NFC_CONNECTION_HANDOVER ?
1129 				   wpa_s->parent->p2p_peer_oob_pubkey_hash :
1130 				   NULL,
1131 				   NULL, 0, 0);
1132 #endif /* CONFIG_WPS_NFC */
1133 	} else {
1134 		u16 dev_pw_id = DEV_PW_DEFAULT;
1135 		if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1136 			dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1137 		wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1138 				   wpa_s->p2p_pin, 1, dev_pw_id);
1139 	}
1140 }
1141 
1142 
1143 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1144 				  struct wpa_ssid *ssid)
1145 {
1146 	struct wpa_ssid *persistent;
1147 	struct psk_list_entry *psk;
1148 	struct hostapd_data *hapd;
1149 
1150 	if (!wpa_s->ap_iface)
1151 		return;
1152 
1153 	persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1154 					     ssid->ssid_len);
1155 	if (persistent == NULL)
1156 		return;
1157 
1158 	hapd = wpa_s->ap_iface->bss[0];
1159 
1160 	dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1161 			 list) {
1162 		struct hostapd_wpa_psk *hpsk;
1163 
1164 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1165 			MACSTR " psk=%d",
1166 			MAC2STR(psk->addr), psk->p2p);
1167 		hpsk = os_zalloc(sizeof(*hpsk));
1168 		if (hpsk == NULL)
1169 			break;
1170 		os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1171 		if (psk->p2p)
1172 			os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1173 		else
1174 			os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1175 		hpsk->next = hapd->conf->ssid.wpa_psk;
1176 		hapd->conf->ssid.wpa_psk = hpsk;
1177 	}
1178 }
1179 
1180 
1181 static void p2p_go_configured(void *ctx, void *data)
1182 {
1183 	struct wpa_supplicant *wpa_s = ctx;
1184 	struct p2p_go_neg_results *params = data;
1185 	struct wpa_ssid *ssid;
1186 	int network_id = -1;
1187 
1188 	ssid = wpa_s->current_ssid;
1189 	if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1190 		wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1191 		if (wpa_s->global->p2p_group_formation == wpa_s)
1192 			wpa_s->global->p2p_group_formation = NULL;
1193 		if (os_strlen(params->passphrase) > 0) {
1194 			wpa_msg_global(wpa_s->parent, MSG_INFO,
1195 				       P2P_EVENT_GROUP_STARTED
1196 				       "%s GO ssid=\"%s\" freq=%d "
1197 				       "passphrase=\"%s\" go_dev_addr=" MACSTR
1198 				       "%s", wpa_s->ifname,
1199 				       wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1200 				       ssid->frequency, params->passphrase,
1201 				       MAC2STR(wpa_s->global->p2p_dev_addr),
1202 				       params->persistent_group ?
1203 				       " [PERSISTENT]" : "");
1204 		} else {
1205 			char psk[65];
1206 			wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1207 					 sizeof(params->psk));
1208 			wpa_msg_global(wpa_s->parent, MSG_INFO,
1209 				       P2P_EVENT_GROUP_STARTED
1210 				       "%s GO ssid=\"%s\" freq=%d psk=%s "
1211 				       "go_dev_addr=" MACSTR "%s",
1212 				       wpa_s->ifname,
1213 				       wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1214 				       ssid->frequency, psk,
1215 				       MAC2STR(wpa_s->global->p2p_dev_addr),
1216 				       params->persistent_group ?
1217 				       " [PERSISTENT]" : "");
1218 		}
1219 
1220 		os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1221 		if (params->persistent_group) {
1222 			network_id = wpas_p2p_store_persistent_group(
1223 				wpa_s->parent, ssid,
1224 				wpa_s->global->p2p_dev_addr);
1225 			wpas_p2p_add_psk_list(wpa_s, ssid);
1226 		}
1227 		if (network_id < 0)
1228 			network_id = ssid->id;
1229 		wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1230 		wpas_p2p_cross_connect_setup(wpa_s);
1231 		wpas_p2p_set_group_idle_timeout(wpa_s);
1232 
1233 		if (wpa_s->p2p_first_connection_timeout) {
1234 			wpa_dbg(wpa_s, MSG_DEBUG,
1235 				"P2P: Start group formation timeout of %d seconds until first data connection on GO",
1236 				wpa_s->p2p_first_connection_timeout);
1237 			wpa_s->p2p_go_group_formation_completed = 0;
1238 			wpa_s->global->p2p_group_formation = wpa_s;
1239 			eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1240 					     wpa_s->parent, NULL);
1241 			eloop_register_timeout(
1242 				wpa_s->p2p_first_connection_timeout, 0,
1243 				wpas_p2p_group_formation_timeout,
1244 				wpa_s->parent, NULL);
1245 		}
1246 
1247 		return;
1248 	}
1249 
1250 	wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1251 	if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1252 					      params->peer_interface_addr)) {
1253 		wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1254 			   "filtering");
1255 		return;
1256 	}
1257 	if (params->wps_method == WPS_PBC) {
1258 		wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1259 					  params->peer_device_addr);
1260 #ifdef CONFIG_WPS_NFC
1261 	} else if (params->wps_method == WPS_NFC) {
1262 		if (wpa_s->parent->p2p_oob_dev_pw_id !=
1263 		    DEV_PW_NFC_CONNECTION_HANDOVER &&
1264 		    !wpa_s->parent->p2p_oob_dev_pw) {
1265 			wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1266 			return;
1267 		}
1268 		wpas_ap_wps_add_nfc_pw(
1269 			wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1270 			wpa_s->parent->p2p_oob_dev_pw,
1271 			wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1272 			wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1273 #endif /* CONFIG_WPS_NFC */
1274 	} else if (wpa_s->p2p_pin[0])
1275 		wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1276 					  wpa_s->p2p_pin, NULL, 0, 0);
1277 	os_free(wpa_s->go_params);
1278 	wpa_s->go_params = NULL;
1279 }
1280 
1281 
1282 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1283 			      struct p2p_go_neg_results *params,
1284 			      int group_formation)
1285 {
1286 	struct wpa_ssid *ssid;
1287 
1288 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1289 	if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1290 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1291 			"results");
1292 		return;
1293 	}
1294 
1295 	ssid = wpa_config_add_network(wpa_s->conf);
1296 	if (ssid == NULL) {
1297 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1298 		return;
1299 	}
1300 
1301 	wpa_s->show_group_started = 0;
1302 
1303 	wpa_config_set_network_defaults(ssid);
1304 	ssid->temporary = 1;
1305 	ssid->p2p_group = 1;
1306 	ssid->p2p_persistent_group = params->persistent_group;
1307 	ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1308 		WPAS_MODE_P2P_GO;
1309 	ssid->frequency = params->freq;
1310 	ssid->ht40 = params->ht40;
1311 	ssid->vht = params->vht;
1312 	ssid->ssid = os_zalloc(params->ssid_len + 1);
1313 	if (ssid->ssid) {
1314 		os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1315 		ssid->ssid_len = params->ssid_len;
1316 	}
1317 	ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1318 	ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1319 	ssid->proto = WPA_PROTO_RSN;
1320 	ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1321 	if (os_strlen(params->passphrase) > 0) {
1322 		ssid->passphrase = os_strdup(params->passphrase);
1323 		if (ssid->passphrase == NULL) {
1324 			wpa_msg_global(wpa_s, MSG_ERROR,
1325 				       "P2P: Failed to copy passphrase for GO");
1326 			wpa_config_remove_network(wpa_s->conf, ssid->id);
1327 			return;
1328 		}
1329 	} else
1330 		ssid->passphrase = NULL;
1331 	ssid->psk_set = params->psk_set;
1332 	if (ssid->psk_set)
1333 		os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1334 	else if (ssid->passphrase)
1335 		wpa_config_update_psk(ssid);
1336 	ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1337 
1338 	wpa_s->ap_configured_cb = p2p_go_configured;
1339 	wpa_s->ap_configured_cb_ctx = wpa_s;
1340 	wpa_s->ap_configured_cb_data = wpa_s->go_params;
1341 	wpa_s->connect_without_scan = ssid;
1342 	wpa_s->reassociate = 1;
1343 	wpa_s->disconnected = 0;
1344 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1345 		"start GO)");
1346 	wpa_supplicant_req_scan(wpa_s, 0, 0);
1347 }
1348 
1349 
1350 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1351 				  const struct wpa_supplicant *src)
1352 {
1353 	struct wpa_config *d;
1354 	const struct wpa_config *s;
1355 
1356 	d = dst->conf;
1357 	s = src->conf;
1358 
1359 #define C(n) if (s->n) d->n = os_strdup(s->n)
1360 	C(device_name);
1361 	C(manufacturer);
1362 	C(model_name);
1363 	C(model_number);
1364 	C(serial_number);
1365 	C(config_methods);
1366 #undef C
1367 
1368 	os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1369 	os_memcpy(d->sec_device_type, s->sec_device_type,
1370 		  sizeof(d->sec_device_type));
1371 	d->num_sec_device_types = s->num_sec_device_types;
1372 
1373 	d->p2p_group_idle = s->p2p_group_idle;
1374 	d->p2p_intra_bss = s->p2p_intra_bss;
1375 	d->persistent_reconnect = s->persistent_reconnect;
1376 	d->max_num_sta = s->max_num_sta;
1377 	d->pbc_in_m1 = s->pbc_in_m1;
1378 	d->ignore_old_scan_res = s->ignore_old_scan_res;
1379 	d->beacon_int = s->beacon_int;
1380 	d->dtim_period = s->dtim_period;
1381 	d->disassoc_low_ack = s->disassoc_low_ack;
1382 	d->disable_scan_offload = s->disable_scan_offload;
1383 
1384 	if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1385 		d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1386 		d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1387 	}
1388 }
1389 
1390 
1391 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1392 				      char *ifname, size_t len)
1393 {
1394 	char *ifname_ptr = wpa_s->ifname;
1395 
1396 	if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1397 		       os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1398 		ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1399 	}
1400 
1401 	os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1402 	if (os_strlen(ifname) >= IFNAMSIZ &&
1403 	    os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1404 		/* Try to avoid going over the IFNAMSIZ length limit */
1405 		os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1406 	}
1407 }
1408 
1409 
1410 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1411 					enum wpa_driver_if_type type)
1412 {
1413 	char ifname[120], force_ifname[120];
1414 
1415 	if (wpa_s->pending_interface_name[0]) {
1416 		wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1417 			   "- skip creation of a new one");
1418 		if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1419 			wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1420 				   "unknown?! ifname='%s'",
1421 				   wpa_s->pending_interface_name);
1422 			return -1;
1423 		}
1424 		return 0;
1425 	}
1426 
1427 	wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1428 	force_ifname[0] = '\0';
1429 
1430 	wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1431 		   ifname);
1432 	wpa_s->p2p_group_idx++;
1433 
1434 	wpa_s->pending_interface_type = type;
1435 	if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1436 			   wpa_s->pending_interface_addr, NULL) < 0) {
1437 		wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1438 			   "interface");
1439 		return -1;
1440 	}
1441 
1442 	if (force_ifname[0]) {
1443 		wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1444 			   force_ifname);
1445 		os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1446 			   sizeof(wpa_s->pending_interface_name));
1447 	} else
1448 		os_strlcpy(wpa_s->pending_interface_name, ifname,
1449 			   sizeof(wpa_s->pending_interface_name));
1450 	wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1451 		   MACSTR, wpa_s->pending_interface_name,
1452 		   MAC2STR(wpa_s->pending_interface_addr));
1453 
1454 	return 0;
1455 }
1456 
1457 
1458 static void wpas_p2p_remove_pending_group_interface(
1459 	struct wpa_supplicant *wpa_s)
1460 {
1461 	if (!wpa_s->pending_interface_name[0] ||
1462 	    is_zero_ether_addr(wpa_s->pending_interface_addr))
1463 		return; /* No pending virtual interface */
1464 
1465 	wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1466 		   wpa_s->pending_interface_name);
1467 	wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1468 			  wpa_s->pending_interface_name);
1469 	os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1470 	wpa_s->pending_interface_name[0] = '\0';
1471 }
1472 
1473 
1474 static struct wpa_supplicant *
1475 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1476 {
1477 	struct wpa_interface iface;
1478 	struct wpa_supplicant *group_wpa_s;
1479 
1480 	if (!wpa_s->pending_interface_name[0]) {
1481 		wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1482 		if (!wpas_p2p_create_iface(wpa_s))
1483 			return NULL;
1484 		/*
1485 		 * Something has forced us to remove the pending interface; try
1486 		 * to create a new one and hope for the best that we will get
1487 		 * the same local address.
1488 		 */
1489 		if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1490 						 WPA_IF_P2P_CLIENT) < 0)
1491 			return NULL;
1492 	}
1493 
1494 	os_memset(&iface, 0, sizeof(iface));
1495 	iface.ifname = wpa_s->pending_interface_name;
1496 	iface.driver = wpa_s->driver->name;
1497 	if (wpa_s->conf->ctrl_interface == NULL &&
1498 	    wpa_s->parent != wpa_s &&
1499 	    wpa_s->p2p_mgmt &&
1500 	    (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1501 		iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1502 	else
1503 		iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1504 	iface.driver_param = wpa_s->conf->driver_param;
1505 	group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1506 	if (group_wpa_s == NULL) {
1507 		wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1508 			   "wpa_supplicant interface");
1509 		return NULL;
1510 	}
1511 	wpa_s->pending_interface_name[0] = '\0';
1512 	group_wpa_s->parent = wpa_s;
1513 	group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1514 		P2P_GROUP_INTERFACE_CLIENT;
1515 	wpa_s->global->p2p_group_formation = group_wpa_s;
1516 
1517 	wpas_p2p_clone_config(group_wpa_s, wpa_s);
1518 
1519 	return group_wpa_s;
1520 }
1521 
1522 
1523 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1524 					     void *timeout_ctx)
1525 {
1526 	struct wpa_supplicant *wpa_s = eloop_ctx;
1527 	wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1528 	wpas_p2p_group_formation_failed(wpa_s);
1529 }
1530 
1531 
1532 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1533 {
1534 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1535 			     wpa_s->parent, NULL);
1536 	if (wpa_s->global->p2p)
1537 		p2p_group_formation_failed(wpa_s->global->p2p);
1538 	wpas_group_formation_completed(wpa_s, 0);
1539 }
1540 
1541 
1542 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1543 {
1544 	wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1545 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1546 			     wpa_s->parent, NULL);
1547 	eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1548 			       wpa_s->parent, NULL);
1549 	wpa_s->global->p2p_fail_on_wps_complete = 0;
1550 }
1551 
1552 
1553 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1554 {
1555 	if (wpa_s->global->p2p_group_formation != wpa_s)
1556 		return;
1557 	/* Speed up group formation timeout since this cannot succeed */
1558 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1559 			     wpa_s->parent, NULL);
1560 	eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1561 			       wpa_s->parent, NULL);
1562 }
1563 
1564 
1565 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1566 {
1567 	struct wpa_supplicant *wpa_s = ctx;
1568 
1569 	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1570 		wpa_drv_cancel_remain_on_channel(wpa_s);
1571 		wpa_s->off_channel_freq = 0;
1572 		wpa_s->roc_waiting_drv_freq = 0;
1573 	}
1574 
1575 	if (res->status) {
1576 		wpa_msg_global(wpa_s, MSG_INFO,
1577 			       P2P_EVENT_GO_NEG_FAILURE "status=%d",
1578 			       res->status);
1579 		wpas_notify_p2p_go_neg_completed(wpa_s, res);
1580 		wpas_p2p_remove_pending_group_interface(wpa_s);
1581 		return;
1582 	}
1583 
1584 	if (wpa_s->p2p_go_ht40)
1585 		res->ht40 = 1;
1586 	if (wpa_s->p2p_go_vht)
1587 		res->vht = 1;
1588 
1589 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1590 		       "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1591 		       " wps_method=%s",
1592 		       res->role_go ? "GO" : "client", res->freq, res->ht40,
1593 		       MAC2STR(res->peer_device_addr),
1594 		       MAC2STR(res->peer_interface_addr),
1595 		       p2p_wps_method_text(res->wps_method));
1596 	wpas_notify_p2p_go_neg_completed(wpa_s, res);
1597 
1598 	if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1599 		struct wpa_ssid *ssid;
1600 		ssid = wpa_config_get_network(wpa_s->conf,
1601 					      wpa_s->p2p_persistent_id);
1602 		if (ssid && ssid->disabled == 2 &&
1603 		    ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1604 			size_t len = os_strlen(ssid->passphrase);
1605 			wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1606 				   "on requested persistent group");
1607 			os_memcpy(res->passphrase, ssid->passphrase, len);
1608 			res->passphrase[len] = '\0';
1609 		}
1610 	}
1611 
1612 	if (wpa_s->create_p2p_iface) {
1613 		struct wpa_supplicant *group_wpa_s =
1614 			wpas_p2p_init_group_interface(wpa_s, res->role_go);
1615 		if (group_wpa_s == NULL) {
1616 			wpas_p2p_remove_pending_group_interface(wpa_s);
1617 			return;
1618 		}
1619 		if (group_wpa_s != wpa_s) {
1620 			os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1621 				  sizeof(group_wpa_s->p2p_pin));
1622 			group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1623 		}
1624 		os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1625 		wpa_s->pending_interface_name[0] = '\0';
1626 		group_wpa_s->p2p_in_provisioning = 1;
1627 
1628 		if (res->role_go)
1629 			wpas_start_wps_go(group_wpa_s, res, 1);
1630 		else
1631 			wpas_start_wps_enrollee(group_wpa_s, res);
1632 	} else {
1633 		wpa_s->p2p_in_provisioning = 1;
1634 		wpa_s->global->p2p_group_formation = wpa_s;
1635 
1636 		if (res->role_go)
1637 			wpas_start_wps_go(wpa_s, res, 1);
1638 		else
1639 			wpas_start_wps_enrollee(ctx, res);
1640 	}
1641 
1642 	wpa_s->p2p_long_listen = 0;
1643 	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1644 
1645 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1646 	eloop_register_timeout(15 + res->peer_config_timeout / 100,
1647 			       (res->peer_config_timeout % 100) * 10000,
1648 			       wpas_p2p_group_formation_timeout, wpa_s, NULL);
1649 }
1650 
1651 
1652 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1653 {
1654 	struct wpa_supplicant *wpa_s = ctx;
1655 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1656 		       " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1657 
1658 	wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1659 }
1660 
1661 
1662 static void wpas_dev_found(void *ctx, const u8 *addr,
1663 			   const struct p2p_peer_info *info,
1664 			   int new_device)
1665 {
1666 #ifndef CONFIG_NO_STDOUT_DEBUG
1667 	struct wpa_supplicant *wpa_s = ctx;
1668 	char devtype[WPS_DEV_TYPE_BUFSIZE];
1669 	char *wfd_dev_info_hex = NULL;
1670 
1671 #ifdef CONFIG_WIFI_DISPLAY
1672 	wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1673 						    WFD_SUBELEM_DEVICE_INFO);
1674 #endif /* CONFIG_WIFI_DISPLAY */
1675 
1676 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1677 		       " p2p_dev_addr=" MACSTR
1678 		       " pri_dev_type=%s name='%s' config_methods=0x%x "
1679 		       "dev_capab=0x%x group_capab=0x%x%s%s",
1680 		       MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1681 		       wps_dev_type_bin2str(info->pri_dev_type, devtype,
1682 					    sizeof(devtype)),
1683 		       info->device_name, info->config_methods,
1684 		       info->dev_capab, info->group_capab,
1685 		       wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1686 		       wfd_dev_info_hex ? wfd_dev_info_hex : "");
1687 
1688 	os_free(wfd_dev_info_hex);
1689 #endif /* CONFIG_NO_STDOUT_DEBUG */
1690 
1691 	wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1692 }
1693 
1694 
1695 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1696 {
1697 	struct wpa_supplicant *wpa_s = ctx;
1698 
1699 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1700 		       "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1701 
1702 	wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1703 }
1704 
1705 
1706 static void wpas_find_stopped(void *ctx)
1707 {
1708 	struct wpa_supplicant *wpa_s = ctx;
1709 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1710 }
1711 
1712 
1713 struct wpas_p2p_listen_work {
1714 	unsigned int freq;
1715 	unsigned int duration;
1716 	struct wpabuf *probe_resp_ie;
1717 };
1718 
1719 
1720 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1721 {
1722 	if (lwork == NULL)
1723 		return;
1724 	wpabuf_free(lwork->probe_resp_ie);
1725 	os_free(lwork);
1726 }
1727 
1728 
1729 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1730 {
1731 	struct wpas_p2p_listen_work *lwork;
1732 
1733 	if (!wpa_s->p2p_listen_work)
1734 		return;
1735 
1736 	lwork = wpa_s->p2p_listen_work->ctx;
1737 	wpas_p2p_listen_work_free(lwork);
1738 	radio_work_done(wpa_s->p2p_listen_work);
1739 	wpa_s->p2p_listen_work = NULL;
1740 }
1741 
1742 
1743 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1744 {
1745 	struct wpa_supplicant *wpa_s = work->wpa_s;
1746 	struct wpas_p2p_listen_work *lwork = work->ctx;
1747 
1748 	if (deinit) {
1749 		wpas_p2p_listen_work_free(lwork);
1750 		return;
1751 	}
1752 
1753 	wpa_s->p2p_listen_work = work;
1754 
1755 	wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1756 
1757 	if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1758 		wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1759 			   "report received Probe Request frames");
1760 		wpas_p2p_listen_work_done(wpa_s);
1761 		return;
1762 	}
1763 
1764 	wpa_s->pending_listen_freq = lwork->freq;
1765 	wpa_s->pending_listen_duration = lwork->duration;
1766 
1767 	if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1768 	{
1769 		wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1770 			   "to remain on channel (%u MHz) for Listen "
1771 			   "state", lwork->freq);
1772 		wpas_p2p_listen_work_done(wpa_s);
1773 		wpa_s->pending_listen_freq = 0;
1774 		return;
1775 	}
1776 	wpa_s->off_channel_freq = 0;
1777 	wpa_s->roc_waiting_drv_freq = lwork->freq;
1778 }
1779 
1780 
1781 static int wpas_start_listen(void *ctx, unsigned int freq,
1782 			     unsigned int duration,
1783 			     const struct wpabuf *probe_resp_ie)
1784 {
1785 	struct wpa_supplicant *wpa_s = ctx;
1786 	struct wpas_p2p_listen_work *lwork;
1787 
1788 	if (wpa_s->p2p_listen_work) {
1789 		wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
1790 		return -1;
1791 	}
1792 
1793 	lwork = os_zalloc(sizeof(*lwork));
1794 	if (lwork == NULL)
1795 		return -1;
1796 	lwork->freq = freq;
1797 	lwork->duration = duration;
1798 	if (probe_resp_ie) {
1799 		lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1800 		if (lwork->probe_resp_ie == NULL) {
1801 			wpas_p2p_listen_work_free(lwork);
1802 			return -1;
1803 		}
1804 	}
1805 
1806 	if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1807 			   lwork) < 0) {
1808 		wpas_p2p_listen_work_free(lwork);
1809 		return -1;
1810 	}
1811 
1812 	return 0;
1813 }
1814 
1815 
1816 static void wpas_stop_listen(void *ctx)
1817 {
1818 	struct wpa_supplicant *wpa_s = ctx;
1819 	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1820 		wpa_drv_cancel_remain_on_channel(wpa_s);
1821 		wpa_s->off_channel_freq = 0;
1822 		wpa_s->roc_waiting_drv_freq = 0;
1823 	}
1824 	wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1825 	wpa_drv_probe_req_report(wpa_s, 0);
1826 	wpas_p2p_listen_work_done(wpa_s);
1827 }
1828 
1829 
1830 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1831 {
1832 	struct wpa_supplicant *wpa_s = ctx;
1833 	return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1834 }
1835 
1836 
1837 /*
1838  * DNS Header section is used only to calculate compression pointers, so the
1839  * contents of this data does not matter, but the length needs to be reserved
1840  * in the virtual packet.
1841  */
1842 #define DNS_HEADER_LEN 12
1843 
1844 /*
1845  * 27-octet in-memory packet from P2P specification containing two implied
1846  * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1847  */
1848 #define P2P_SD_IN_MEMORY_LEN 27
1849 
1850 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1851 				       u8 **spos, const u8 *end)
1852 {
1853 	while (*spos < end) {
1854 		u8 val = ((*spos)[0] & 0xc0) >> 6;
1855 		int len;
1856 
1857 		if (val == 1 || val == 2) {
1858 			/* These are reserved values in RFC 1035 */
1859 			wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1860 				   "sequence starting with 0x%x", val);
1861 			return -1;
1862 		}
1863 
1864 		if (val == 3) {
1865 			u16 offset;
1866 			u8 *spos_tmp;
1867 
1868 			/* Offset */
1869 			if (*spos + 2 > end) {
1870 				wpa_printf(MSG_DEBUG, "P2P: No room for full "
1871 					   "DNS offset field");
1872 				return -1;
1873 			}
1874 
1875 			offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1876 			if (offset >= *spos - start) {
1877 				wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1878 					   "pointer offset %u", offset);
1879 				return -1;
1880 			}
1881 
1882 			(*spos) += 2;
1883 			spos_tmp = start + offset;
1884 			return p2p_sd_dns_uncompress_label(upos, uend, start,
1885 							   &spos_tmp,
1886 							   *spos - 2);
1887 		}
1888 
1889 		/* Label */
1890 		len = (*spos)[0] & 0x3f;
1891 		if (len == 0)
1892 			return 0;
1893 
1894 		(*spos)++;
1895 		if (*spos + len > end) {
1896 			wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1897 				   "sequence - no room for label with length "
1898 				   "%u", len);
1899 			return -1;
1900 		}
1901 
1902 		if (*upos + len + 2 > uend)
1903 			return -2;
1904 
1905 		os_memcpy(*upos, *spos, len);
1906 		*spos += len;
1907 		*upos += len;
1908 		(*upos)[0] = '.';
1909 		(*upos)++;
1910 		(*upos)[0] = '\0';
1911 	}
1912 
1913 	return 0;
1914 }
1915 
1916 
1917 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1918  * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1919  * not large enough */
1920 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1921 				 size_t msg_len, size_t offset)
1922 {
1923 	/* 27-octet in-memory packet from P2P specification */
1924 	const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1925 		"\x04_udp\xC0\x11\x00\x0C\x00\x01";
1926 	u8 *tmp, *end, *spos;
1927 	char *upos, *uend;
1928 	int ret = 0;
1929 
1930 	if (buf_len < 2)
1931 		return -1;
1932 	if (offset > msg_len)
1933 		return -1;
1934 
1935 	tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1936 	if (tmp == NULL)
1937 		return -1;
1938 	spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1939 	end = spos + msg_len;
1940 	spos += offset;
1941 
1942 	os_memset(tmp, 0, DNS_HEADER_LEN);
1943 	os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1944 	os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1945 
1946 	upos = buf;
1947 	uend = buf + buf_len;
1948 
1949 	ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1950 	if (ret) {
1951 		os_free(tmp);
1952 		return ret;
1953 	}
1954 
1955 	if (upos == buf) {
1956 		upos[0] = '.';
1957 		upos[1] = '\0';
1958 	} else if (upos[-1] == '.')
1959 		upos[-1] = '\0';
1960 
1961 	os_free(tmp);
1962 	return 0;
1963 }
1964 
1965 
1966 static struct p2p_srv_bonjour *
1967 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1968 			     const struct wpabuf *query)
1969 {
1970 	struct p2p_srv_bonjour *bsrv;
1971 	size_t len;
1972 
1973 	len = wpabuf_len(query);
1974 	dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1975 			 struct p2p_srv_bonjour, list) {
1976 		if (len == wpabuf_len(bsrv->query) &&
1977 		    os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1978 			      len) == 0)
1979 			return bsrv;
1980 	}
1981 	return NULL;
1982 }
1983 
1984 
1985 static struct p2p_srv_upnp *
1986 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1987 			  const char *service)
1988 {
1989 	struct p2p_srv_upnp *usrv;
1990 
1991 	dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1992 			 struct p2p_srv_upnp, list) {
1993 		if (version == usrv->version &&
1994 		    os_strcmp(service, usrv->service) == 0)
1995 			return usrv;
1996 	}
1997 	return NULL;
1998 }
1999 
2000 
2001 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2002 					u8 srv_trans_id)
2003 {
2004 	u8 *len_pos;
2005 
2006 	if (wpabuf_tailroom(resp) < 5)
2007 		return;
2008 
2009 	/* Length (to be filled) */
2010 	len_pos = wpabuf_put(resp, 2);
2011 	wpabuf_put_u8(resp, srv_proto);
2012 	wpabuf_put_u8(resp, srv_trans_id);
2013 	/* Status Code */
2014 	wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2015 	/* Response Data: empty */
2016 	WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2017 }
2018 
2019 
2020 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2021 				struct wpabuf *resp, u8 srv_trans_id)
2022 {
2023 	struct p2p_srv_bonjour *bsrv;
2024 	u8 *len_pos;
2025 
2026 	wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2027 
2028 	if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2029 		wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2030 		return;
2031 	}
2032 
2033 	dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2034 			 struct p2p_srv_bonjour, list) {
2035 		if (wpabuf_tailroom(resp) <
2036 		    5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2037 			return;
2038 		/* Length (to be filled) */
2039 		len_pos = wpabuf_put(resp, 2);
2040 		wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2041 		wpabuf_put_u8(resp, srv_trans_id);
2042 		/* Status Code */
2043 		wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2044 		wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2045 				  wpabuf_head(bsrv->resp),
2046 				  wpabuf_len(bsrv->resp));
2047 		/* Response Data */
2048 		wpabuf_put_buf(resp, bsrv->query); /* Key */
2049 		wpabuf_put_buf(resp, bsrv->resp); /* Value */
2050 		WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2051 			     2);
2052 	}
2053 }
2054 
2055 
2056 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2057 			       size_t query_len)
2058 {
2059 	char str_rx[256], str_srv[256];
2060 
2061 	if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2062 		return 0; /* Too short to include DNS Type and Version */
2063 	if (os_memcmp(query + query_len - 3,
2064 		      wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2065 		      3) != 0)
2066 		return 0; /* Mismatch in DNS Type or Version */
2067 	if (query_len == wpabuf_len(bsrv->query) &&
2068 	    os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2069 		return 1; /* Binary match */
2070 
2071 	if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2072 				  0))
2073 		return 0; /* Failed to uncompress query */
2074 	if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2075 				  wpabuf_head(bsrv->query),
2076 				  wpabuf_len(bsrv->query) - 3, 0))
2077 		return 0; /* Failed to uncompress service */
2078 
2079 	return os_strcmp(str_rx, str_srv) == 0;
2080 }
2081 
2082 
2083 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2084 				struct wpabuf *resp, u8 srv_trans_id,
2085 				const u8 *query, size_t query_len)
2086 {
2087 	struct p2p_srv_bonjour *bsrv;
2088 	u8 *len_pos;
2089 	int matches = 0;
2090 
2091 	wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2092 			  query, query_len);
2093 	if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2094 		wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2095 		wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2096 					    srv_trans_id);
2097 		return;
2098 	}
2099 
2100 	if (query_len == 0) {
2101 		wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2102 		return;
2103 	}
2104 
2105 	dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2106 			 struct p2p_srv_bonjour, list) {
2107 		if (!match_bonjour_query(bsrv, query, query_len))
2108 			continue;
2109 
2110 		if (wpabuf_tailroom(resp) <
2111 		    5 + query_len + wpabuf_len(bsrv->resp))
2112 			return;
2113 
2114 		matches++;
2115 
2116 		/* Length (to be filled) */
2117 		len_pos = wpabuf_put(resp, 2);
2118 		wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2119 		wpabuf_put_u8(resp, srv_trans_id);
2120 
2121 		/* Status Code */
2122 		wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2123 		wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2124 				  wpabuf_head(bsrv->resp),
2125 				  wpabuf_len(bsrv->resp));
2126 
2127 		/* Response Data */
2128 		wpabuf_put_data(resp, query, query_len); /* Key */
2129 		wpabuf_put_buf(resp, bsrv->resp); /* Value */
2130 
2131 		WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2132 	}
2133 
2134 	if (matches == 0) {
2135 		wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2136 			   "available");
2137 		if (wpabuf_tailroom(resp) < 5)
2138 			return;
2139 
2140 		/* Length (to be filled) */
2141 		len_pos = wpabuf_put(resp, 2);
2142 		wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2143 		wpabuf_put_u8(resp, srv_trans_id);
2144 
2145 		/* Status Code */
2146 		wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2147 		/* Response Data: empty */
2148 		WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2149 			     2);
2150 	}
2151 }
2152 
2153 
2154 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2155 			     struct wpabuf *resp, u8 srv_trans_id)
2156 {
2157 	struct p2p_srv_upnp *usrv;
2158 	u8 *len_pos;
2159 
2160 	wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2161 
2162 	if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2163 		wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2164 		return;
2165 	}
2166 
2167 	dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2168 			 struct p2p_srv_upnp, list) {
2169 		if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2170 			return;
2171 
2172 		/* Length (to be filled) */
2173 		len_pos = wpabuf_put(resp, 2);
2174 		wpabuf_put_u8(resp, P2P_SERV_UPNP);
2175 		wpabuf_put_u8(resp, srv_trans_id);
2176 
2177 		/* Status Code */
2178 		wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2179 		/* Response Data */
2180 		wpabuf_put_u8(resp, usrv->version);
2181 		wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2182 			   usrv->service);
2183 		wpabuf_put_str(resp, usrv->service);
2184 		WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2185 			     2);
2186 	}
2187 }
2188 
2189 
2190 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2191 			     struct wpabuf *resp, u8 srv_trans_id,
2192 			     const u8 *query, size_t query_len)
2193 {
2194 	struct p2p_srv_upnp *usrv;
2195 	u8 *len_pos;
2196 	u8 version;
2197 	char *str;
2198 	int count = 0;
2199 
2200 	wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2201 			  query, query_len);
2202 
2203 	if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2204 		wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2205 		wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2206 					    srv_trans_id);
2207 		return;
2208 	}
2209 
2210 	if (query_len == 0) {
2211 		wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2212 		return;
2213 	}
2214 
2215 	if (wpabuf_tailroom(resp) < 5)
2216 		return;
2217 
2218 	/* Length (to be filled) */
2219 	len_pos = wpabuf_put(resp, 2);
2220 	wpabuf_put_u8(resp, P2P_SERV_UPNP);
2221 	wpabuf_put_u8(resp, srv_trans_id);
2222 
2223 	version = query[0];
2224 	str = os_malloc(query_len);
2225 	if (str == NULL)
2226 		return;
2227 	os_memcpy(str, query + 1, query_len - 1);
2228 	str[query_len - 1] = '\0';
2229 
2230 	dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2231 			 struct p2p_srv_upnp, list) {
2232 		if (version != usrv->version)
2233 			continue;
2234 
2235 		if (os_strcmp(str, "ssdp:all") != 0 &&
2236 		    os_strstr(usrv->service, str) == NULL)
2237 			continue;
2238 
2239 		if (wpabuf_tailroom(resp) < 2)
2240 			break;
2241 		if (count == 0) {
2242 			/* Status Code */
2243 			wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2244 			/* Response Data */
2245 			wpabuf_put_u8(resp, version);
2246 		} else
2247 			wpabuf_put_u8(resp, ',');
2248 
2249 		count++;
2250 
2251 		wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2252 			   usrv->service);
2253 		if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2254 			break;
2255 		wpabuf_put_str(resp, usrv->service);
2256 	}
2257 	os_free(str);
2258 
2259 	if (count == 0) {
2260 		wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2261 			   "available");
2262 		/* Status Code */
2263 		wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2264 		/* Response Data: empty */
2265 	}
2266 
2267 	WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2268 }
2269 
2270 
2271 #ifdef CONFIG_WIFI_DISPLAY
2272 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2273 			    struct wpabuf *resp, u8 srv_trans_id,
2274 			    const u8 *query, size_t query_len)
2275 {
2276 	const u8 *pos;
2277 	u8 role;
2278 	u8 *len_pos;
2279 
2280 	wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2281 
2282 	if (!wpa_s->global->wifi_display) {
2283 		wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2284 		wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2285 					    srv_trans_id);
2286 		return;
2287 	}
2288 
2289 	if (query_len < 1) {
2290 		wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2291 			   "Role");
2292 		return;
2293 	}
2294 
2295 	if (wpabuf_tailroom(resp) < 5)
2296 		return;
2297 
2298 	pos = query;
2299 	role = *pos++;
2300 	wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2301 
2302 	/* TODO: role specific handling */
2303 
2304 	/* Length (to be filled) */
2305 	len_pos = wpabuf_put(resp, 2);
2306 	wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2307 	wpabuf_put_u8(resp, srv_trans_id);
2308 	wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2309 
2310 	while (pos < query + query_len) {
2311 		if (*pos < MAX_WFD_SUBELEMS &&
2312 		    wpa_s->global->wfd_subelem[*pos] &&
2313 		    wpabuf_tailroom(resp) >=
2314 		    wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2315 			wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2316 				   "subelement %u", *pos);
2317 			wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2318 		}
2319 		pos++;
2320 	}
2321 
2322 	WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2323 }
2324 #endif /* CONFIG_WIFI_DISPLAY */
2325 
2326 
2327 static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2328 			    u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2329 {
2330 	struct wpa_supplicant *wpa_s = ctx;
2331 	const u8 *pos = tlvs;
2332 	const u8 *end = tlvs + tlvs_len;
2333 	const u8 *tlv_end;
2334 	u16 slen;
2335 	struct wpabuf *resp;
2336 	u8 srv_proto, srv_trans_id;
2337 	size_t buf_len;
2338 	char *buf;
2339 
2340 	wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2341 		    tlvs, tlvs_len);
2342 	buf_len = 2 * tlvs_len + 1;
2343 	buf = os_malloc(buf_len);
2344 	if (buf) {
2345 		wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2346 		wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2347 			     MACSTR " %u %u %s",
2348 			     freq, MAC2STR(sa), dialog_token, update_indic,
2349 			     buf);
2350 		os_free(buf);
2351 	}
2352 
2353 	if (wpa_s->p2p_sd_over_ctrl_iface) {
2354 		wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2355 					   update_indic, tlvs, tlvs_len);
2356 		return; /* to be processed by an external program */
2357 	}
2358 
2359 	resp = wpabuf_alloc(10000);
2360 	if (resp == NULL)
2361 		return;
2362 
2363 	while (pos + 1 < end) {
2364 		wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2365 		slen = WPA_GET_LE16(pos);
2366 		pos += 2;
2367 		if (pos + slen > end || slen < 2) {
2368 			wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2369 				   "length");
2370 			wpabuf_free(resp);
2371 			return;
2372 		}
2373 		tlv_end = pos + slen;
2374 
2375 		srv_proto = *pos++;
2376 		wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2377 			   srv_proto);
2378 		srv_trans_id = *pos++;
2379 		wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2380 			   srv_trans_id);
2381 
2382 		wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2383 			    pos, tlv_end - pos);
2384 
2385 
2386 		if (wpa_s->force_long_sd) {
2387 			wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2388 				   "response");
2389 			wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2390 			wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2391 			goto done;
2392 		}
2393 
2394 		switch (srv_proto) {
2395 		case P2P_SERV_ALL_SERVICES:
2396 			wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2397 				   "for all services");
2398 			if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2399 			    dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2400 				wpa_printf(MSG_DEBUG, "P2P: No service "
2401 					   "discovery protocols available");
2402 				wpas_sd_add_proto_not_avail(
2403 					resp, P2P_SERV_ALL_SERVICES,
2404 					srv_trans_id);
2405 				break;
2406 			}
2407 			wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2408 			wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2409 			break;
2410 		case P2P_SERV_BONJOUR:
2411 			wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2412 					    pos, tlv_end - pos);
2413 			break;
2414 		case P2P_SERV_UPNP:
2415 			wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2416 					 pos, tlv_end - pos);
2417 			break;
2418 #ifdef CONFIG_WIFI_DISPLAY
2419 		case P2P_SERV_WIFI_DISPLAY:
2420 			wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2421 					pos, tlv_end - pos);
2422 			break;
2423 #endif /* CONFIG_WIFI_DISPLAY */
2424 		default:
2425 			wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2426 				   "protocol %u", srv_proto);
2427 			wpas_sd_add_proto_not_avail(resp, srv_proto,
2428 						    srv_trans_id);
2429 			break;
2430 		}
2431 
2432 		pos = tlv_end;
2433 	}
2434 
2435 done:
2436 	wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2437 				   update_indic, tlvs, tlvs_len);
2438 
2439 	wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2440 
2441 	wpabuf_free(resp);
2442 }
2443 
2444 
2445 static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2446 			     const u8 *tlvs, size_t tlvs_len)
2447 {
2448 	struct wpa_supplicant *wpa_s = ctx;
2449 	const u8 *pos = tlvs;
2450 	const u8 *end = tlvs + tlvs_len;
2451 	const u8 *tlv_end;
2452 	u16 slen;
2453 	size_t buf_len;
2454 	char *buf;
2455 
2456 	wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2457 		    tlvs, tlvs_len);
2458 	if (tlvs_len > 1500) {
2459 		/* TODO: better way for handling this */
2460 		wpa_msg_ctrl(wpa_s, MSG_INFO,
2461 			     P2P_EVENT_SERV_DISC_RESP MACSTR
2462 			     " %u <long response: %u bytes>",
2463 			     MAC2STR(sa), update_indic,
2464 			     (unsigned int) tlvs_len);
2465 	} else {
2466 		buf_len = 2 * tlvs_len + 1;
2467 		buf = os_malloc(buf_len);
2468 		if (buf) {
2469 			wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2470 			wpa_msg_ctrl(wpa_s, MSG_INFO,
2471 				     P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2472 				     MAC2STR(sa), update_indic, buf);
2473 			os_free(buf);
2474 		}
2475 	}
2476 
2477 	while (pos < end) {
2478 		u8 srv_proto, srv_trans_id, status;
2479 
2480 		wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2481 		slen = WPA_GET_LE16(pos);
2482 		pos += 2;
2483 		if (pos + slen > end || slen < 3) {
2484 			wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2485 				   "length");
2486 			return;
2487 		}
2488 		tlv_end = pos + slen;
2489 
2490 		srv_proto = *pos++;
2491 		wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2492 			   srv_proto);
2493 		srv_trans_id = *pos++;
2494 		wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2495 			   srv_trans_id);
2496 		status = *pos++;
2497 		wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2498 			   status);
2499 
2500 		wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2501 			    pos, tlv_end - pos);
2502 
2503 		pos = tlv_end;
2504 	}
2505 
2506 	wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2507 }
2508 
2509 
2510 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2511 			const struct wpabuf *tlvs)
2512 {
2513 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2514 		return 0;
2515 	return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2516 }
2517 
2518 
2519 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2520 			     u8 version, const char *query)
2521 {
2522 	struct wpabuf *tlvs;
2523 	u64 ret;
2524 
2525 	tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2526 	if (tlvs == NULL)
2527 		return 0;
2528 	wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2529 	wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2530 	wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2531 	wpabuf_put_u8(tlvs, version);
2532 	wpabuf_put_str(tlvs, query);
2533 	ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2534 	wpabuf_free(tlvs);
2535 	return ret;
2536 }
2537 
2538 
2539 #ifdef CONFIG_WIFI_DISPLAY
2540 
2541 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2542 				   const struct wpabuf *tlvs)
2543 {
2544 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2545 		return 0;
2546 	return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2547 }
2548 
2549 
2550 #define MAX_WFD_SD_SUBELEMS 20
2551 
2552 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2553 				const char *subelems)
2554 {
2555 	u8 *len;
2556 	const char *pos;
2557 	int val;
2558 	int count = 0;
2559 
2560 	len = wpabuf_put(tlvs, 2);
2561 	wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2562 	wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2563 
2564 	wpabuf_put_u8(tlvs, role);
2565 
2566 	pos = subelems;
2567 	while (*pos) {
2568 		val = atoi(pos);
2569 		if (val >= 0 && val < 256) {
2570 			wpabuf_put_u8(tlvs, val);
2571 			count++;
2572 			if (count == MAX_WFD_SD_SUBELEMS)
2573 				break;
2574 		}
2575 		pos = os_strchr(pos + 1, ',');
2576 		if (pos == NULL)
2577 			break;
2578 		pos++;
2579 	}
2580 
2581 	WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2582 }
2583 
2584 
2585 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2586 				     const u8 *dst, const char *role)
2587 {
2588 	struct wpabuf *tlvs;
2589 	u64 ret;
2590 	const char *subelems;
2591 	u8 id = 1;
2592 
2593 	subelems = os_strchr(role, ' ');
2594 	if (subelems == NULL)
2595 		return 0;
2596 	subelems++;
2597 
2598 	tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2599 	if (tlvs == NULL)
2600 		return 0;
2601 
2602 	if (os_strstr(role, "[source]"))
2603 		wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2604 	if (os_strstr(role, "[pri-sink]"))
2605 		wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2606 	if (os_strstr(role, "[sec-sink]"))
2607 		wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2608 	if (os_strstr(role, "[source+sink]"))
2609 		wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2610 
2611 	ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2612 	wpabuf_free(tlvs);
2613 	return ret;
2614 }
2615 
2616 #endif /* CONFIG_WIFI_DISPLAY */
2617 
2618 
2619 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2620 {
2621 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2622 		return -1;
2623 	return p2p_sd_cancel_request(wpa_s->global->p2p,
2624 				     (void *) (uintptr_t) req);
2625 }
2626 
2627 
2628 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2629 			  const u8 *dst, u8 dialog_token,
2630 			  const struct wpabuf *resp_tlvs)
2631 {
2632 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2633 		return;
2634 	p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2635 			resp_tlvs);
2636 }
2637 
2638 
2639 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2640 {
2641 	if (wpa_s->global->p2p)
2642 		p2p_sd_service_update(wpa_s->global->p2p);
2643 }
2644 
2645 
2646 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2647 {
2648 	dl_list_del(&bsrv->list);
2649 	wpabuf_free(bsrv->query);
2650 	wpabuf_free(bsrv->resp);
2651 	os_free(bsrv);
2652 }
2653 
2654 
2655 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2656 {
2657 	dl_list_del(&usrv->list);
2658 	os_free(usrv->service);
2659 	os_free(usrv);
2660 }
2661 
2662 
2663 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2664 {
2665 	struct p2p_srv_bonjour *bsrv, *bn;
2666 	struct p2p_srv_upnp *usrv, *un;
2667 
2668 	dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2669 			      struct p2p_srv_bonjour, list)
2670 		wpas_p2p_srv_bonjour_free(bsrv);
2671 
2672 	dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2673 			      struct p2p_srv_upnp, list)
2674 		wpas_p2p_srv_upnp_free(usrv);
2675 
2676 	wpas_p2p_sd_service_update(wpa_s);
2677 }
2678 
2679 
2680 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2681 				 struct wpabuf *query, struct wpabuf *resp)
2682 {
2683 	struct p2p_srv_bonjour *bsrv;
2684 
2685 	bsrv = os_zalloc(sizeof(*bsrv));
2686 	if (bsrv == NULL)
2687 		return -1;
2688 	bsrv->query = query;
2689 	bsrv->resp = resp;
2690 	dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2691 
2692 	wpas_p2p_sd_service_update(wpa_s);
2693 	return 0;
2694 }
2695 
2696 
2697 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2698 				 const struct wpabuf *query)
2699 {
2700 	struct p2p_srv_bonjour *bsrv;
2701 
2702 	bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2703 	if (bsrv == NULL)
2704 		return -1;
2705 	wpas_p2p_srv_bonjour_free(bsrv);
2706 	wpas_p2p_sd_service_update(wpa_s);
2707 	return 0;
2708 }
2709 
2710 
2711 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2712 			      const char *service)
2713 {
2714 	struct p2p_srv_upnp *usrv;
2715 
2716 	if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2717 		return 0; /* Already listed */
2718 	usrv = os_zalloc(sizeof(*usrv));
2719 	if (usrv == NULL)
2720 		return -1;
2721 	usrv->version = version;
2722 	usrv->service = os_strdup(service);
2723 	if (usrv->service == NULL) {
2724 		os_free(usrv);
2725 		return -1;
2726 	}
2727 	dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2728 
2729 	wpas_p2p_sd_service_update(wpa_s);
2730 	return 0;
2731 }
2732 
2733 
2734 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2735 			      const char *service)
2736 {
2737 	struct p2p_srv_upnp *usrv;
2738 
2739 	usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2740 	if (usrv == NULL)
2741 		return -1;
2742 	wpas_p2p_srv_upnp_free(usrv);
2743 	wpas_p2p_sd_service_update(wpa_s);
2744 	return 0;
2745 }
2746 
2747 
2748 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2749 					 const u8 *peer, const char *params,
2750 					 unsigned int generated_pin)
2751 {
2752 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2753 		       " %08d%s", MAC2STR(peer), generated_pin, params);
2754 }
2755 
2756 
2757 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2758 					const u8 *peer, const char *params)
2759 {
2760 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2761 		       "%s", MAC2STR(peer), params);
2762 }
2763 
2764 
2765 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2766 			       const u8 *dev_addr, const u8 *pri_dev_type,
2767 			       const char *dev_name, u16 supp_config_methods,
2768 			       u8 dev_capab, u8 group_capab, const u8 *group_id,
2769 			       size_t group_id_len)
2770 {
2771 	struct wpa_supplicant *wpa_s = ctx;
2772 	char devtype[WPS_DEV_TYPE_BUFSIZE];
2773 	char params[300];
2774 	u8 empty_dev_type[8];
2775 	unsigned int generated_pin = 0;
2776 	struct wpa_supplicant *group = NULL;
2777 
2778 	if (group_id) {
2779 		for (group = wpa_s->global->ifaces; group; group = group->next)
2780 		{
2781 			struct wpa_ssid *s = group->current_ssid;
2782 			if (s != NULL &&
2783 			    s->mode == WPAS_MODE_P2P_GO &&
2784 			    group_id_len - ETH_ALEN == s->ssid_len &&
2785 			    os_memcmp(group_id + ETH_ALEN, s->ssid,
2786 				      s->ssid_len) == 0)
2787 				break;
2788 		}
2789 	}
2790 
2791 	if (pri_dev_type == NULL) {
2792 		os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2793 		pri_dev_type = empty_dev_type;
2794 	}
2795 	os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2796 		    " pri_dev_type=%s name='%s' config_methods=0x%x "
2797 		    "dev_capab=0x%x group_capab=0x%x%s%s",
2798 		    MAC2STR(dev_addr),
2799 		    wps_dev_type_bin2str(pri_dev_type, devtype,
2800 					 sizeof(devtype)),
2801 		    dev_name, supp_config_methods, dev_capab, group_capab,
2802 		    group ? " group=" : "",
2803 		    group ? group->ifname : "");
2804 	params[sizeof(params) - 1] = '\0';
2805 
2806 	if (config_methods & WPS_CONFIG_DISPLAY) {
2807 		generated_pin = wps_generate_pin();
2808 		wpas_prov_disc_local_display(wpa_s, peer, params,
2809 					     generated_pin);
2810 	} else if (config_methods & WPS_CONFIG_KEYPAD)
2811 		wpas_prov_disc_local_keypad(wpa_s, peer, params);
2812 	else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2813 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2814 			       MACSTR "%s", MAC2STR(peer), params);
2815 
2816 	wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2817 					    P2P_PROV_DISC_SUCCESS,
2818 					    config_methods, generated_pin);
2819 }
2820 
2821 
2822 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2823 {
2824 	struct wpa_supplicant *wpa_s = ctx;
2825 	unsigned int generated_pin = 0;
2826 	char params[20];
2827 
2828 	if (wpa_s->pending_pd_before_join &&
2829 	    (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2830 	     os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2831 		wpa_s->pending_pd_before_join = 0;
2832 		wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2833 			   "join-existing-group operation");
2834 		wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2835 		return;
2836 	}
2837 
2838 	if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2839 	    wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2840 		os_snprintf(params, sizeof(params), " peer_go=%d",
2841 			    wpa_s->pending_pd_use == AUTO_PD_JOIN);
2842 	else
2843 		params[0] = '\0';
2844 
2845 	if (config_methods & WPS_CONFIG_DISPLAY)
2846 		wpas_prov_disc_local_keypad(wpa_s, peer, params);
2847 	else if (config_methods & WPS_CONFIG_KEYPAD) {
2848 		generated_pin = wps_generate_pin();
2849 		wpas_prov_disc_local_display(wpa_s, peer, params,
2850 					     generated_pin);
2851 	} else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2852 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2853 			       MACSTR "%s", MAC2STR(peer), params);
2854 
2855 	wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2856 					    P2P_PROV_DISC_SUCCESS,
2857 					    config_methods, generated_pin);
2858 }
2859 
2860 
2861 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2862 				enum p2p_prov_disc_status status)
2863 {
2864 	struct wpa_supplicant *wpa_s = ctx;
2865 
2866 	if (wpa_s->p2p_fallback_to_go_neg) {
2867 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2868 			"failed - fall back to GO Negotiation");
2869 		wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2870 		return;
2871 	}
2872 
2873 	if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2874 		wpa_s->pending_pd_before_join = 0;
2875 		wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2876 			   "join-existing-group operation (no ACK for PD "
2877 			   "Req attempts)");
2878 		wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2879 		return;
2880 	}
2881 
2882 	wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2883 		       " p2p_dev_addr=" MACSTR " status=%d",
2884 		       MAC2STR(peer), status);
2885 
2886 	wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2887 					    status, 0, 0);
2888 }
2889 
2890 
2891 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2892 {
2893 	if (channels == NULL)
2894 		return 1; /* Assume no restrictions */
2895 	return p2p_channels_includes_freq(channels, freq);
2896 
2897 }
2898 
2899 
2900 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2901 				  const u8 *go_dev_addr, const u8 *ssid,
2902 				  size_t ssid_len, int *go, u8 *group_bssid,
2903 				  int *force_freq, int persistent_group,
2904 				  const struct p2p_channels *channels,
2905 				  int dev_pw_id)
2906 {
2907 	struct wpa_supplicant *wpa_s = ctx;
2908 	struct wpa_ssid *s;
2909 	int res;
2910 	struct wpa_supplicant *grp;
2911 
2912 	if (!persistent_group) {
2913 		wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2914 			   " to join an active group (SSID: %s)",
2915 			   MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
2916 		if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2917 		    (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2918 		     == 0 ||
2919 		     os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2920 			wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2921 				   "authorized invitation");
2922 			goto accept_inv;
2923 		}
2924 
2925 #ifdef CONFIG_WPS_NFC
2926 		if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
2927 		    dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
2928 			wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2929 			wpa_s->parent->p2p_wps_method = WPS_NFC;
2930 			wpa_s->parent->pending_join_wps_method = WPS_NFC;
2931 			os_memcpy(wpa_s->parent->pending_join_dev_addr,
2932 				  go_dev_addr, ETH_ALEN);
2933 			os_memcpy(wpa_s->parent->pending_join_iface_addr,
2934 				  bssid, ETH_ALEN);
2935 			goto accept_inv;
2936 		}
2937 #endif /* CONFIG_WPS_NFC */
2938 
2939 		/*
2940 		 * Do not accept the invitation automatically; notify user and
2941 		 * request approval.
2942 		 */
2943 		return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2944 	}
2945 
2946 	grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2947 	if (grp) {
2948 		wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2949 			   "running persistent group");
2950 		if (*go)
2951 			os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2952 		goto accept_inv;
2953 	}
2954 
2955 	if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2956 	    os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2957 		wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2958 			   "invitation to re-invoke a persistent group");
2959 	} else if (!wpa_s->conf->persistent_reconnect)
2960 		return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2961 
2962 	for (s = wpa_s->conf->ssid; s; s = s->next) {
2963 		if (s->disabled == 2 &&
2964 		    os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2965 		    s->ssid_len == ssid_len &&
2966 		    os_memcmp(ssid, s->ssid, ssid_len) == 0)
2967 			break;
2968 	}
2969 
2970 	if (!s) {
2971 		wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2972 			   " requested reinvocation of an unknown group",
2973 			   MAC2STR(sa));
2974 		return P2P_SC_FAIL_UNKNOWN_GROUP;
2975 	}
2976 
2977 	if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2978 		*go = 1;
2979 		if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2980 			wpa_printf(MSG_DEBUG, "P2P: The only available "
2981 				   "interface is already in use - reject "
2982 				   "invitation");
2983 			return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2984 		}
2985 		os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2986 	} else if (s->mode == WPAS_MODE_P2P_GO) {
2987 		*go = 1;
2988 		if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2989 		{
2990 			wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2991 				   "interface address for the group");
2992 			return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2993 		}
2994 		os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2995 			  ETH_ALEN);
2996 	}
2997 
2998 accept_inv:
2999 	wpas_p2p_set_own_freq_preference(wpa_s, 0);
3000 
3001 	/* Get one of the frequencies currently in use */
3002 	if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
3003 		wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3004 		wpas_p2p_set_own_freq_preference(wpa_s, res);
3005 
3006 		if (wpa_s->num_multichan_concurrent < 2 ||
3007 		    wpas_p2p_num_unused_channels(wpa_s) < 1) {
3008 			wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3009 			*force_freq = res;
3010 		}
3011 	}
3012 
3013 	if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3014 	    wpas_p2p_num_unused_channels(wpa_s) > 0) {
3015 		if (*go == 0) {
3016 			/* We are the client */
3017 			wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3018 				   "running a GO but we are capable of MCC, "
3019 				   "figure out the best channel to use");
3020 			*force_freq = 0;
3021 		} else if (!freq_included(channels, *force_freq)) {
3022 			/* We are the GO, and *force_freq is not in the
3023 			 * intersection */
3024 			wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3025 				   "in intersection but we are capable of MCC, "
3026 				   "figure out the best channel to use",
3027 				   *force_freq);
3028 			*force_freq = 0;
3029 		}
3030 	}
3031 
3032 	return P2P_SC_SUCCESS;
3033 }
3034 
3035 
3036 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3037 				     const u8 *ssid, size_t ssid_len,
3038 				     const u8 *go_dev_addr, u8 status,
3039 				     int op_freq)
3040 {
3041 	struct wpa_supplicant *wpa_s = ctx;
3042 	struct wpa_ssid *s;
3043 
3044 	for (s = wpa_s->conf->ssid; s; s = s->next) {
3045 		if (s->disabled == 2 &&
3046 		    s->ssid_len == ssid_len &&
3047 		    os_memcmp(ssid, s->ssid, ssid_len) == 0)
3048 			break;
3049 	}
3050 
3051 	if (status == P2P_SC_SUCCESS) {
3052 		wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3053 			   " was accepted; op_freq=%d MHz, SSID=%s",
3054 			   MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3055 		if (s) {
3056 			int go = s->mode == WPAS_MODE_P2P_GO;
3057 			wpas_p2p_group_add_persistent(
3058 				wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
3059 				go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3060 		} else if (bssid) {
3061 			wpa_s->user_initiated_pd = 0;
3062 			wpas_p2p_join(wpa_s, bssid, go_dev_addr,
3063 				      wpa_s->p2p_wps_method, 0, op_freq,
3064 				      ssid, ssid_len);
3065 		}
3066 		return;
3067 	}
3068 
3069 	if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3070 		wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3071 			   " was rejected (status %u)", MAC2STR(sa), status);
3072 		return;
3073 	}
3074 
3075 	if (!s) {
3076 		if (bssid) {
3077 			wpa_msg_global(wpa_s, MSG_INFO,
3078 				       P2P_EVENT_INVITATION_RECEIVED
3079 				       "sa=" MACSTR " go_dev_addr=" MACSTR
3080 				       " bssid=" MACSTR " unknown-network",
3081 				       MAC2STR(sa), MAC2STR(go_dev_addr),
3082 				       MAC2STR(bssid));
3083 		} else {
3084 			wpa_msg_global(wpa_s, MSG_INFO,
3085 				       P2P_EVENT_INVITATION_RECEIVED
3086 				       "sa=" MACSTR " go_dev_addr=" MACSTR
3087 				       " unknown-network",
3088 				       MAC2STR(sa), MAC2STR(go_dev_addr));
3089 		}
3090 		return;
3091 	}
3092 
3093 	if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
3094 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3095 			       "sa=" MACSTR " persistent=%d freq=%d",
3096 			       MAC2STR(sa), s->id, op_freq);
3097 	} else {
3098 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3099 			       "sa=" MACSTR " persistent=%d",
3100 			       MAC2STR(sa), s->id);
3101 	}
3102 }
3103 
3104 
3105 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3106 					struct wpa_ssid *ssid,
3107 					const u8 *peer, int inv)
3108 {
3109 	size_t i;
3110 
3111 	if (ssid == NULL)
3112 		return;
3113 
3114 	for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3115 		if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3116 			      ETH_ALEN) == 0)
3117 			break;
3118 	}
3119 	if (i >= ssid->num_p2p_clients) {
3120 		if (ssid->mode != WPAS_MODE_P2P_GO &&
3121 		    os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3122 			wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3123 				   "due to invitation result", ssid->id);
3124 			wpas_notify_network_removed(wpa_s, ssid);
3125 			wpa_config_remove_network(wpa_s->conf, ssid->id);
3126 			return;
3127 		}
3128 		return; /* Peer not found in client list */
3129 	}
3130 
3131 	wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
3132 		   "group %d client list%s",
3133 		   MAC2STR(peer), ssid->id,
3134 		   inv ? " due to invitation result" : "");
3135 	os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3136 		   ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3137 		   (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3138 	ssid->num_p2p_clients--;
3139 #ifndef CONFIG_NO_CONFIG_WRITE
3140 	if (wpa_s->parent->conf->update_config &&
3141 	    wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3142 		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3143 #endif /* CONFIG_NO_CONFIG_WRITE */
3144 }
3145 
3146 
3147 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3148 					  const u8 *peer)
3149 {
3150 	struct wpa_ssid *ssid;
3151 
3152 	wpa_s = wpa_s->global->p2p_invite_group;
3153 	if (wpa_s == NULL)
3154 		return; /* No known invitation group */
3155 	ssid = wpa_s->current_ssid;
3156 	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3157 	    !ssid->p2p_persistent_group)
3158 		return; /* Not operating as a GO in persistent group */
3159 	ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3160 				       ssid->ssid, ssid->ssid_len);
3161 	wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3162 }
3163 
3164 
3165 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
3166 				   const struct p2p_channels *channels,
3167 				   const u8 *peer, int neg_freq)
3168 {
3169 	struct wpa_supplicant *wpa_s = ctx;
3170 	struct wpa_ssid *ssid;
3171 	int freq;
3172 
3173 	if (bssid) {
3174 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3175 			       "status=%d " MACSTR,
3176 			       status, MAC2STR(bssid));
3177 	} else {
3178 		wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3179 			       "status=%d ", status);
3180 	}
3181 	wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3182 
3183 	wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3184 		   status, MAC2STR(peer));
3185 	if (wpa_s->pending_invite_ssid_id == -1) {
3186 		if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3187 			wpas_remove_persistent_client(wpa_s, peer);
3188 		return; /* Invitation to active group */
3189 	}
3190 
3191 	if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3192 		wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3193 			   "invitation exchange to indicate readiness for "
3194 			   "re-invocation");
3195 	}
3196 
3197 	if (status != P2P_SC_SUCCESS) {
3198 		if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3199 			ssid = wpa_config_get_network(
3200 				wpa_s->conf, wpa_s->pending_invite_ssid_id);
3201 			wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3202 		}
3203 		wpas_p2p_remove_pending_group_interface(wpa_s);
3204 		return;
3205 	}
3206 
3207 	ssid = wpa_config_get_network(wpa_s->conf,
3208 				      wpa_s->pending_invite_ssid_id);
3209 	if (ssid == NULL) {
3210 		wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3211 			   "data matching with invitation");
3212 		return;
3213 	}
3214 
3215 	/*
3216 	 * The peer could have missed our ctrl::ack frame for Invitation
3217 	 * Response and continue retransmitting the frame. To reduce the
3218 	 * likelihood of the peer not getting successful TX status for the
3219 	 * Invitation Response frame, wait a short time here before starting
3220 	 * the persistent group so that we will remain on the current channel to
3221 	 * acknowledge any possible retransmission from the peer.
3222 	 */
3223 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3224 		"starting persistent group");
3225 	os_sleep(0, 50000);
3226 
3227 	freq = wpa_s->p2p_persistent_go_freq;
3228 	if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3229 	    freq_included(channels, neg_freq)) {
3230 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode",
3231 			neg_freq);
3232 		freq = neg_freq;
3233 	}
3234 
3235 	wpas_p2p_group_add_persistent(wpa_s, ssid,
3236 				      ssid->mode == WPAS_MODE_P2P_GO,
3237 				      freq,
3238 				      wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3239 				      channels,
3240 				      ssid->mode == WPAS_MODE_P2P_GO ?
3241 				      P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3242 				      0);
3243 }
3244 
3245 
3246 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3247 				    unsigned int freq)
3248 {
3249 	if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3250 		return 1;
3251 	return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3252 }
3253 
3254 
3255 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3256 {
3257 	reg->channel[reg->channels] = chan;
3258 	reg->channels++;
3259 }
3260 
3261 
3262 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3263 				     struct p2p_channels *chan,
3264 				     struct p2p_channels *cli_chan)
3265 {
3266 	int i, cla = 0;
3267 
3268 	os_memset(cli_chan, 0, sizeof(*cli_chan));
3269 
3270 	wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3271 		   "band");
3272 
3273 	/* Operating class 81 - 2.4 GHz band channels 1..13 */
3274 	chan->reg_class[cla].reg_class = 81;
3275 	chan->reg_class[cla].channels = 0;
3276 	for (i = 0; i < 11; i++) {
3277 		if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3278 			wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3279 	}
3280 	if (chan->reg_class[cla].channels)
3281 		cla++;
3282 
3283 	wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3284 		   "band");
3285 
3286 	/* Operating class 115 - 5 GHz, channels 36-48 */
3287 	chan->reg_class[cla].reg_class = 115;
3288 	chan->reg_class[cla].channels = 0;
3289 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3290 		wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3291 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3292 		wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3293 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3294 		wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3295 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3296 		wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3297 	if (chan->reg_class[cla].channels)
3298 		cla++;
3299 
3300 	wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3301 		   "band");
3302 
3303 	/* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3304 	chan->reg_class[cla].reg_class = 124;
3305 	chan->reg_class[cla].channels = 0;
3306 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3307 		wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3308 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3309 		wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3310 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3311 		wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3312 	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3313 		wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3314 	if (chan->reg_class[cla].channels)
3315 		cla++;
3316 
3317 	chan->reg_classes = cla;
3318 	return 0;
3319 }
3320 
3321 
3322 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3323 					  u16 num_modes,
3324 					  enum hostapd_hw_mode mode)
3325 {
3326 	u16 i;
3327 
3328 	for (i = 0; i < num_modes; i++) {
3329 		if (modes[i].mode == mode)
3330 			return &modes[i];
3331 	}
3332 
3333 	return NULL;
3334 }
3335 
3336 
3337 enum chan_allowed {
3338 	NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3339 };
3340 
3341 static int has_channel(struct wpa_global *global,
3342 		       struct hostapd_hw_modes *mode, u8 chan, int *flags)
3343 {
3344 	int i;
3345 	unsigned int freq;
3346 
3347 	freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3348 		chan * 5;
3349 	if (wpas_p2p_disallowed_freq(global, freq))
3350 		return NOT_ALLOWED;
3351 
3352 	for (i = 0; i < mode->num_channels; i++) {
3353 		if (mode->channels[i].chan == chan) {
3354 			if (flags)
3355 				*flags = mode->channels[i].flag;
3356 			if (mode->channels[i].flag &
3357 			    (HOSTAPD_CHAN_DISABLED |
3358 			     HOSTAPD_CHAN_RADAR))
3359 				return NOT_ALLOWED;
3360 			if (mode->channels[i].flag &
3361 			    (HOSTAPD_CHAN_PASSIVE_SCAN |
3362 			     HOSTAPD_CHAN_NO_IBSS))
3363 				return PASSIVE_ONLY;
3364 			return ALLOWED;
3365 		}
3366 	}
3367 
3368 	return NOT_ALLOWED;
3369 }
3370 
3371 
3372 struct p2p_oper_class_map {
3373 	enum hostapd_hw_mode mode;
3374 	u8 op_class;
3375 	u8 min_chan;
3376 	u8 max_chan;
3377 	u8 inc;
3378 	enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
3379 };
3380 
3381 static struct p2p_oper_class_map op_class[] = {
3382 	{ HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3383 #if 0 /* Do not enable HT40 on 2 GHz for now */
3384 	{ HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3385 	{ HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3386 #endif
3387 	{ HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3388 	{ HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3389 	{ HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3390 	{ HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3391 	{ HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3392 	{ HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3393 
3394 	/*
3395 	 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3396 	 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3397 	 * 80 MHz, but currently use the following definition for simplicity
3398 	 * (these center frequencies are not actual channels, which makes
3399 	 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3400 	 * removing invalid channels.
3401 	 */
3402 	{ HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3403 	{ -1, 0, 0, 0, 0, BW20 }
3404 };
3405 
3406 
3407 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3408 				     struct hostapd_hw_modes *mode,
3409 				     u8 channel)
3410 {
3411 	u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3412 	unsigned int i;
3413 
3414 	if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3415 		return 0;
3416 
3417 	for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3418 		/*
3419 		 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3420 		 * so the center channel is 6 channels away from the start/end.
3421 		 */
3422 		if (channel >= center_channels[i] - 6 &&
3423 		    channel <= center_channels[i] + 6)
3424 			return center_channels[i];
3425 
3426 	return 0;
3427 }
3428 
3429 
3430 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3431 					       struct hostapd_hw_modes *mode,
3432 					       u8 channel, u8 bw)
3433 {
3434 	u8 center_chan;
3435 	int i, flags;
3436 	enum chan_allowed res, ret = ALLOWED;
3437 
3438 	center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3439 	if (!center_chan)
3440 		return NOT_ALLOWED;
3441 	if (center_chan >= 58 && center_chan <= 138)
3442 		return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3443 
3444 	/* check all the channels are available */
3445 	for (i = 0; i < 4; i++) {
3446 		int adj_chan = center_chan - 6 + i * 4;
3447 
3448 		res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3449 		if (res == NOT_ALLOWED)
3450 			return NOT_ALLOWED;
3451 		if (res == PASSIVE_ONLY)
3452 			ret = PASSIVE_ONLY;
3453 
3454 		if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3455 			return NOT_ALLOWED;
3456 		if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3457 			return NOT_ALLOWED;
3458 		if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3459 			return NOT_ALLOWED;
3460 		if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3461 			return NOT_ALLOWED;
3462 	}
3463 
3464 	return ret;
3465 }
3466 
3467 
3468 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3469 						 struct hostapd_hw_modes *mode,
3470 						 u8 channel, u8 bw)
3471 {
3472 	int flag;
3473 	enum chan_allowed res, res2;
3474 
3475 	res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3476 	if (bw == BW40MINUS) {
3477 		if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3478 			return NOT_ALLOWED;
3479 		res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3480 	} else if (bw == BW40PLUS) {
3481 		if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3482 			return NOT_ALLOWED;
3483 		res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3484 	} else if (bw == BW80) {
3485 		res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3486 	}
3487 
3488 	if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3489 		return NOT_ALLOWED;
3490 	if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3491 		return PASSIVE_ONLY;
3492 	return res;
3493 }
3494 
3495 
3496 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3497 				   struct p2p_channels *chan,
3498 				   struct p2p_channels *cli_chan)
3499 {
3500 	struct hostapd_hw_modes *mode;
3501 	int cla, op, cli_cla;
3502 
3503 	if (wpa_s->hw.modes == NULL) {
3504 		wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3505 			   "of all supported channels; assume dualband "
3506 			   "support");
3507 		return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3508 	}
3509 
3510 	cla = cli_cla = 0;
3511 
3512 	for (op = 0; op_class[op].op_class; op++) {
3513 		struct p2p_oper_class_map *o = &op_class[op];
3514 		u8 ch;
3515 		struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3516 
3517 		mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3518 		if (mode == NULL)
3519 			continue;
3520 		for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3521 			enum chan_allowed res;
3522 			res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3523 			if (res == ALLOWED) {
3524 				if (reg == NULL) {
3525 					wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3526 						   o->op_class);
3527 					reg = &chan->reg_class[cla];
3528 					cla++;
3529 					reg->reg_class = o->op_class;
3530 				}
3531 				reg->channel[reg->channels] = ch;
3532 				reg->channels++;
3533 			} else if (res == PASSIVE_ONLY &&
3534 				   wpa_s->conf->p2p_add_cli_chan) {
3535 				if (cli_reg == NULL) {
3536 					wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3537 						   o->op_class);
3538 					cli_reg = &cli_chan->reg_class[cli_cla];
3539 					cli_cla++;
3540 					cli_reg->reg_class = o->op_class;
3541 				}
3542 				cli_reg->channel[cli_reg->channels] = ch;
3543 				cli_reg->channels++;
3544 			}
3545 		}
3546 		if (reg) {
3547 			wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3548 				    reg->channel, reg->channels);
3549 		}
3550 		if (cli_reg) {
3551 			wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3552 				    cli_reg->channel, cli_reg->channels);
3553 		}
3554 	}
3555 
3556 	chan->reg_classes = cla;
3557 	cli_chan->reg_classes = cli_cla;
3558 
3559 	return 0;
3560 }
3561 
3562 
3563 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3564 			   struct hostapd_hw_modes *mode, u8 channel)
3565 {
3566 	int op;
3567 	enum chan_allowed ret;
3568 
3569 	for (op = 0; op_class[op].op_class; op++) {
3570 		struct p2p_oper_class_map *o = &op_class[op];
3571 		u8 ch;
3572 
3573 		for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3574 			if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3575 			    o->bw == BW20 || ch != channel)
3576 				continue;
3577 			ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3578 			if (ret == ALLOWED)
3579 				return (o->bw == BW40MINUS) ? -1 : 1;
3580 		}
3581 	}
3582 	return 0;
3583 }
3584 
3585 
3586 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3587 			      struct hostapd_hw_modes *mode, u8 channel)
3588 {
3589 	if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3590 		return 0;
3591 
3592 	return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3593 }
3594 
3595 
3596 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3597 			size_t buf_len)
3598 {
3599 	struct wpa_supplicant *wpa_s = ctx;
3600 
3601 	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3602 		if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3603 			break;
3604 	}
3605 	if (wpa_s == NULL)
3606 		return -1;
3607 
3608 	return wpa_drv_get_noa(wpa_s, buf, buf_len);
3609 }
3610 
3611 
3612 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3613 {
3614 	struct wpa_supplicant *wpa_s = ctx;
3615 
3616 	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3617 		struct wpa_ssid *ssid = wpa_s->current_ssid;
3618 		if (ssid == NULL)
3619 			continue;
3620 		if (ssid->mode != WPAS_MODE_INFRA)
3621 			continue;
3622 		if (wpa_s->wpa_state != WPA_COMPLETED &&
3623 		    wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3624 			continue;
3625 		if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3626 			return 1;
3627 	}
3628 
3629 	return 0;
3630 }
3631 
3632 
3633 static int wpas_is_concurrent_session_active(void *ctx)
3634 {
3635 	struct wpa_supplicant *wpa_s = ctx;
3636 	struct wpa_supplicant *ifs;
3637 
3638 	for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3639 		if (ifs == wpa_s)
3640 			continue;
3641 		if (ifs->wpa_state > WPA_ASSOCIATED)
3642 			return 1;
3643 	}
3644 	return 0;
3645 }
3646 
3647 
3648 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3649 {
3650 	struct wpa_supplicant *wpa_s = ctx;
3651 	wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3652 }
3653 
3654 
3655 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3656 {
3657 	struct wpa_interface iface;
3658 	struct wpa_supplicant *p2pdev_wpa_s;
3659 	char ifname[100];
3660 	char force_name[100];
3661 	int ret;
3662 
3663 	os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3664 		    wpa_s->ifname);
3665 	force_name[0] = '\0';
3666 	wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3667 	ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3668 			     force_name, wpa_s->pending_interface_addr, NULL);
3669 	if (ret < 0) {
3670 		wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3671 		return ret;
3672 	}
3673 	os_strlcpy(wpa_s->pending_interface_name, ifname,
3674 		   sizeof(wpa_s->pending_interface_name));
3675 
3676 	os_memset(&iface, 0, sizeof(iface));
3677 	iface.p2p_mgmt = 1;
3678 	iface.ifname = wpa_s->pending_interface_name;
3679 	iface.driver = wpa_s->driver->name;
3680 	iface.driver_param = wpa_s->conf->driver_param;
3681 	iface.confname = wpa_s->confname;
3682 	p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3683 	if (!p2pdev_wpa_s) {
3684 		wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3685 		return -1;
3686 	}
3687 	p2pdev_wpa_s->parent = wpa_s;
3688 
3689 	wpa_s->pending_interface_name[0] = '\0';
3690 	return 0;
3691 }
3692 
3693 
3694 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3695 			       const u8 *noa, size_t noa_len)
3696 {
3697 	struct wpa_supplicant *wpa_s, *intf = ctx;
3698 	char hex[100];
3699 
3700 	for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3701 		if (wpa_s->waiting_presence_resp)
3702 			break;
3703 	}
3704 	if (!wpa_s) {
3705 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3706 		return;
3707 	}
3708 	wpa_s->waiting_presence_resp = 0;
3709 
3710 	wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3711 	wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3712 		" status=%u noa=%s", MAC2STR(src), status, hex);
3713 }
3714 
3715 
3716 /**
3717  * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3718  * @global: Pointer to global data from wpa_supplicant_init()
3719  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3720  * Returns: 0 on success, -1 on failure
3721  */
3722 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3723 {
3724 	struct p2p_config p2p;
3725 	unsigned int r;
3726 	int i;
3727 
3728 	if (wpa_s->conf->p2p_disabled)
3729 		return 0;
3730 
3731 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3732 		return 0;
3733 
3734 	if (global->p2p)
3735 		return 0;
3736 
3737 	os_memset(&p2p, 0, sizeof(p2p));
3738 	p2p.cb_ctx = wpa_s;
3739 	p2p.debug_print = wpas_p2p_debug_print;
3740 	p2p.p2p_scan = wpas_p2p_scan;
3741 	p2p.send_action = wpas_send_action;
3742 	p2p.send_action_done = wpas_send_action_done;
3743 	p2p.go_neg_completed = wpas_go_neg_completed;
3744 	p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3745 	p2p.dev_found = wpas_dev_found;
3746 	p2p.dev_lost = wpas_dev_lost;
3747 	p2p.find_stopped = wpas_find_stopped;
3748 	p2p.start_listen = wpas_start_listen;
3749 	p2p.stop_listen = wpas_stop_listen;
3750 	p2p.send_probe_resp = wpas_send_probe_resp;
3751 	p2p.sd_request = wpas_sd_request;
3752 	p2p.sd_response = wpas_sd_response;
3753 	p2p.prov_disc_req = wpas_prov_disc_req;
3754 	p2p.prov_disc_resp = wpas_prov_disc_resp;
3755 	p2p.prov_disc_fail = wpas_prov_disc_fail;
3756 	p2p.invitation_process = wpas_invitation_process;
3757 	p2p.invitation_received = wpas_invitation_received;
3758 	p2p.invitation_result = wpas_invitation_result;
3759 	p2p.get_noa = wpas_get_noa;
3760 	p2p.go_connected = wpas_go_connected;
3761 	p2p.presence_resp = wpas_presence_resp;
3762 	p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
3763 
3764 	os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
3765 	os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
3766 	p2p.dev_name = wpa_s->conf->device_name;
3767 	p2p.manufacturer = wpa_s->conf->manufacturer;
3768 	p2p.model_name = wpa_s->conf->model_name;
3769 	p2p.model_number = wpa_s->conf->model_number;
3770 	p2p.serial_number = wpa_s->conf->serial_number;
3771 	if (wpa_s->wps) {
3772 		os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3773 		p2p.config_methods = wpa_s->wps->config_methods;
3774 	}
3775 
3776 	if (wpa_s->conf->p2p_listen_reg_class &&
3777 	    wpa_s->conf->p2p_listen_channel) {
3778 		p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3779 		p2p.channel = wpa_s->conf->p2p_listen_channel;
3780 	} else {
3781 		p2p.reg_class = 81;
3782 		/*
3783 		 * Pick one of the social channels randomly as the listen
3784 		 * channel.
3785 		 */
3786 		os_get_random((u8 *) &r, sizeof(r));
3787 		p2p.channel = 1 + (r % 3) * 5;
3788 	}
3789 	wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3790 
3791 	if (wpa_s->conf->p2p_oper_reg_class &&
3792 	    wpa_s->conf->p2p_oper_channel) {
3793 		p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3794 		p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3795 		p2p.cfg_op_channel = 1;
3796 		wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3797 			   "%d:%d", p2p.op_reg_class, p2p.op_channel);
3798 
3799 	} else {
3800 		p2p.op_reg_class = 81;
3801 		/*
3802 		 * Use random operation channel from (1, 6, 11) if no other
3803 		 * preference is indicated.
3804 		 */
3805 		os_get_random((u8 *) &r, sizeof(r));
3806 		p2p.op_channel = 1 + (r % 3) * 5;
3807 		p2p.cfg_op_channel = 0;
3808 		wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3809 			   "%d:%d", p2p.op_reg_class, p2p.op_channel);
3810 	}
3811 
3812 	if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3813 		p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3814 		p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3815 	}
3816 
3817 	if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3818 		os_memcpy(p2p.country, wpa_s->conf->country, 2);
3819 		p2p.country[2] = 0x04;
3820 	} else
3821 		os_memcpy(p2p.country, "XX\x04", 3);
3822 
3823 	if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3824 		wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3825 			   "channel list");
3826 		return -1;
3827 	}
3828 
3829 	os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3830 		  WPS_DEV_TYPE_LEN);
3831 
3832 	p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3833 	os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3834 		  p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3835 
3836 	p2p.concurrent_operations = !!(wpa_s->drv_flags &
3837 				       WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3838 
3839 	p2p.max_peers = 100;
3840 
3841 	if (wpa_s->conf->p2p_ssid_postfix) {
3842 		p2p.ssid_postfix_len =
3843 			os_strlen(wpa_s->conf->p2p_ssid_postfix);
3844 		if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3845 			p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3846 		os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3847 			  p2p.ssid_postfix_len);
3848 	}
3849 
3850 	p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3851 
3852 	p2p.max_listen = wpa_s->max_remain_on_chan;
3853 
3854 	global->p2p = p2p_init(&p2p);
3855 	if (global->p2p == NULL)
3856 		return -1;
3857 	global->p2p_init_wpa_s = wpa_s;
3858 
3859 	for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3860 		if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3861 			continue;
3862 		p2p_add_wps_vendor_extension(
3863 			global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3864 	}
3865 
3866 	p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3867 
3868 	return 0;
3869 }
3870 
3871 
3872 /**
3873  * wpas_p2p_deinit - Deinitialize per-interface P2P data
3874  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3875  *
3876  * This function deinitialize per-interface P2P data.
3877  */
3878 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3879 {
3880 	if (wpa_s->driver && wpa_s->drv_priv)
3881 		wpa_drv_probe_req_report(wpa_s, 0);
3882 
3883 	if (wpa_s->go_params) {
3884 		/* Clear any stored provisioning info */
3885 		p2p_clear_provisioning_info(
3886 			wpa_s->global->p2p,
3887 			wpa_s->go_params->peer_device_addr);
3888 	}
3889 
3890 	os_free(wpa_s->go_params);
3891 	wpa_s->go_params = NULL;
3892 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3893 	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3894 	wpa_s->p2p_long_listen = 0;
3895 	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3896 	eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3897 	wpas_p2p_remove_pending_group_interface(wpa_s);
3898 	eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3899 	wpas_p2p_listen_work_done(wpa_s);
3900 	if (wpa_s->p2p_send_action_work) {
3901 		os_free(wpa_s->p2p_send_action_work->ctx);
3902 		radio_work_done(wpa_s->p2p_send_action_work);
3903 		wpa_s->p2p_send_action_work = NULL;
3904 	}
3905 	eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
3906 
3907 	wpabuf_free(wpa_s->p2p_oob_dev_pw);
3908 	wpa_s->p2p_oob_dev_pw = NULL;
3909 
3910 	/* TODO: remove group interface from the driver if this wpa_s instance
3911 	 * is on top of a P2P group interface */
3912 }
3913 
3914 
3915 /**
3916  * wpas_p2p_deinit_global - Deinitialize global P2P module
3917  * @global: Pointer to global data from wpa_supplicant_init()
3918  *
3919  * This function deinitializes the global (per device) P2P module.
3920  */
3921 void wpas_p2p_deinit_global(struct wpa_global *global)
3922 {
3923 	struct wpa_supplicant *wpa_s, *tmp;
3924 
3925 	wpa_s = global->ifaces;
3926 	if (wpa_s)
3927 		wpas_p2p_service_flush(wpa_s);
3928 
3929 	if (global->p2p == NULL)
3930 		return;
3931 
3932 	/* Remove remaining P2P group interfaces */
3933 	while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3934 		wpa_s = wpa_s->next;
3935 	while (wpa_s) {
3936 		tmp = global->ifaces;
3937 		while (tmp &&
3938 		       (tmp == wpa_s ||
3939 			tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3940 			tmp = tmp->next;
3941 		}
3942 		if (tmp == NULL)
3943 			break;
3944 		/* Disconnect from the P2P group and deinit the interface */
3945 		wpas_p2p_disconnect(tmp);
3946 	}
3947 
3948 	/*
3949 	 * Deinit GO data on any possibly remaining interface (if main
3950 	 * interface is used as GO).
3951 	 */
3952 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3953 		if (wpa_s->ap_iface)
3954 			wpas_p2p_group_deinit(wpa_s);
3955 	}
3956 
3957 	p2p_deinit(global->p2p);
3958 	global->p2p = NULL;
3959 	global->p2p_init_wpa_s = NULL;
3960 }
3961 
3962 
3963 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3964 {
3965 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3966 	    wpa_s->conf->p2p_no_group_iface)
3967 		return 0; /* separate interface disabled per configuration */
3968 	if (wpa_s->drv_flags &
3969 	    (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3970 	     WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3971 		return 1; /* P2P group requires a new interface in every case
3972 			   */
3973 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3974 		return 0; /* driver does not support concurrent operations */
3975 	if (wpa_s->global->ifaces->next)
3976 		return 1; /* more that one interface already in use */
3977 	if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3978 		return 1; /* this interface is already in use */
3979 	return 0;
3980 }
3981 
3982 
3983 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3984 				 const u8 *peer_addr,
3985 				 enum p2p_wps_method wps_method,
3986 				 int go_intent, const u8 *own_interface_addr,
3987 				 unsigned int force_freq, int persistent_group,
3988 				 struct wpa_ssid *ssid, unsigned int pref_freq)
3989 {
3990 	if (persistent_group && wpa_s->conf->persistent_reconnect)
3991 		persistent_group = 2;
3992 
3993 	/*
3994 	 * Increase GO config timeout if HT40 is used since it takes some time
3995 	 * to scan channels for coex purposes before the BSS can be started.
3996 	 */
3997 	p2p_set_config_timeout(wpa_s->global->p2p,
3998 			       wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3999 
4000 	return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4001 			   go_intent, own_interface_addr, force_freq,
4002 			   persistent_group, ssid ? ssid->ssid : NULL,
4003 			   ssid ? ssid->ssid_len : 0,
4004 			   wpa_s->p2p_pd_before_go_neg, pref_freq,
4005 			   wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4006 			   0);
4007 }
4008 
4009 
4010 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4011 				const u8 *peer_addr,
4012 				enum p2p_wps_method wps_method,
4013 				int go_intent, const u8 *own_interface_addr,
4014 				unsigned int force_freq, int persistent_group,
4015 				struct wpa_ssid *ssid, unsigned int pref_freq)
4016 {
4017 	if (persistent_group && wpa_s->conf->persistent_reconnect)
4018 		persistent_group = 2;
4019 
4020 	return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4021 			     go_intent, own_interface_addr, force_freq,
4022 			     persistent_group, ssid ? ssid->ssid : NULL,
4023 			     ssid ? ssid->ssid_len : 0, pref_freq,
4024 			     wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4025 			     0);
4026 }
4027 
4028 
4029 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4030 {
4031 	wpa_s->p2p_join_scan_count++;
4032 	wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4033 		   wpa_s->p2p_join_scan_count);
4034 	if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4035 		wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4036 			   " for join operationg - stop join attempt",
4037 			   MAC2STR(wpa_s->pending_join_iface_addr));
4038 		eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4039 		if (wpa_s->p2p_auto_pd) {
4040 			wpa_s->p2p_auto_pd = 0;
4041 			wpa_msg_global(wpa_s, MSG_INFO,
4042 				       P2P_EVENT_PROV_DISC_FAILURE
4043 				       " p2p_dev_addr=" MACSTR " status=N/A",
4044 				       MAC2STR(wpa_s->pending_join_dev_addr));
4045 			return;
4046 		}
4047 		wpa_msg_global(wpa_s->parent, MSG_INFO,
4048 			       P2P_EVENT_GROUP_FORMATION_FAILURE);
4049 	}
4050 }
4051 
4052 
4053 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4054 {
4055 	int *freqs, res, num, i;
4056 
4057 	if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4058 		/* Multiple channels are supported and not all are in use */
4059 		return 0;
4060 	}
4061 
4062 	freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4063 	if (!freqs)
4064 		return 1;
4065 
4066 	num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4067 					wpa_s->num_multichan_concurrent);
4068 	if (num < 0) {
4069 		res = 1;
4070 		goto exit_free;
4071 	}
4072 
4073 	for (i = 0; i < num; i++) {
4074 		if (freqs[i] == freq) {
4075 			wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4076 				   freq);
4077 			res = 0;
4078 			goto exit_free;
4079 		}
4080 	}
4081 
4082 	res = 1;
4083 
4084 exit_free:
4085 	os_free(freqs);
4086 	return res;
4087 }
4088 
4089 
4090 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4091 			    const u8 *peer_dev_addr)
4092 {
4093 	struct wpa_bss *bss;
4094 	int updated;
4095 
4096 	bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4097 	if (bss == NULL)
4098 		return -1;
4099 	if (bss->last_update_idx < wpa_s->bss_update_idx) {
4100 		wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4101 			   "last scan");
4102 		return 0;
4103 	}
4104 
4105 	updated = os_reltime_before(&wpa_s->p2p_auto_started,
4106 				    &bss->last_update);
4107 	wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4108 		   "%ld.%06ld (%supdated in last scan)",
4109 		   bss->last_update.sec, bss->last_update.usec,
4110 		   updated ? "": "not ");
4111 
4112 	return updated;
4113 }
4114 
4115 
4116 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4117 				   struct wpa_scan_results *scan_res)
4118 {
4119 	struct wpa_bss *bss = NULL;
4120 	int freq;
4121 	u8 iface_addr[ETH_ALEN];
4122 
4123 	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4124 
4125 	if (wpa_s->global->p2p_disabled)
4126 		return;
4127 
4128 	wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4129 		   scan_res ? (int) scan_res->num : -1,
4130 		   wpa_s->p2p_auto_join ? "auto_" : "");
4131 
4132 	if (scan_res)
4133 		wpas_p2p_scan_res_handler(wpa_s, scan_res);
4134 
4135 	if (wpa_s->p2p_auto_pd) {
4136 		int join = wpas_p2p_peer_go(wpa_s,
4137 					    wpa_s->pending_join_dev_addr);
4138 		if (join == 0 &&
4139 		    wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4140 			wpa_s->auto_pd_scan_retry++;
4141 			bss = wpa_bss_get_bssid_latest(
4142 				wpa_s, wpa_s->pending_join_dev_addr);
4143 			if (bss) {
4144 				freq = bss->freq;
4145 				wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4146 					   "the peer " MACSTR " at %d MHz",
4147 					   wpa_s->auto_pd_scan_retry,
4148 					   MAC2STR(wpa_s->
4149 						   pending_join_dev_addr),
4150 					   freq);
4151 				wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4152 				return;
4153 			}
4154 		}
4155 
4156 		if (join < 0)
4157 			join = 0;
4158 
4159 		wpa_s->p2p_auto_pd = 0;
4160 		wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4161 		wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4162 			   MAC2STR(wpa_s->pending_join_dev_addr), join);
4163 		if (p2p_prov_disc_req(wpa_s->global->p2p,
4164 				      wpa_s->pending_join_dev_addr,
4165 				      wpa_s->pending_pd_config_methods, join,
4166 				      0, wpa_s->user_initiated_pd) < 0) {
4167 			wpa_s->p2p_auto_pd = 0;
4168 			wpa_msg_global(wpa_s, MSG_INFO,
4169 				       P2P_EVENT_PROV_DISC_FAILURE
4170 				       " p2p_dev_addr=" MACSTR " status=N/A",
4171 				       MAC2STR(wpa_s->pending_join_dev_addr));
4172 		}
4173 		return;
4174 	}
4175 
4176 	if (wpa_s->p2p_auto_join) {
4177 		int join = wpas_p2p_peer_go(wpa_s,
4178 					    wpa_s->pending_join_dev_addr);
4179 		if (join < 0) {
4180 			wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4181 				   "running a GO -> use GO Negotiation");
4182 			wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4183 					 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4184 					 wpa_s->p2p_persistent_group, 0, 0, 0,
4185 					 wpa_s->p2p_go_intent,
4186 					 wpa_s->p2p_connect_freq,
4187 					 wpa_s->p2p_persistent_id,
4188 					 wpa_s->p2p_pd_before_go_neg,
4189 					 wpa_s->p2p_go_ht40,
4190 					 wpa_s->p2p_go_vht);
4191 			return;
4192 		}
4193 
4194 		wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4195 			   "try to join the group", join ? "" :
4196 			   " in older scan");
4197 		if (!join)
4198 			wpa_s->p2p_fallback_to_go_neg = 1;
4199 	}
4200 
4201 	freq = p2p_get_oper_freq(wpa_s->global->p2p,
4202 				 wpa_s->pending_join_iface_addr);
4203 	if (freq < 0 &&
4204 	    p2p_get_interface_addr(wpa_s->global->p2p,
4205 				   wpa_s->pending_join_dev_addr,
4206 				   iface_addr) == 0 &&
4207 	    os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4208 	{
4209 		wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4210 			   "address for join from " MACSTR " to " MACSTR
4211 			   " based on newly discovered P2P peer entry",
4212 			   MAC2STR(wpa_s->pending_join_iface_addr),
4213 			   MAC2STR(iface_addr));
4214 		os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4215 			  ETH_ALEN);
4216 
4217 		freq = p2p_get_oper_freq(wpa_s->global->p2p,
4218 					 wpa_s->pending_join_iface_addr);
4219 	}
4220 	if (freq >= 0) {
4221 		wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4222 			   "from P2P peer table: %d MHz", freq);
4223 	}
4224 	if (wpa_s->p2p_join_ssid_len) {
4225 		wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4226 			   MACSTR " and SSID %s",
4227 			   MAC2STR(wpa_s->pending_join_iface_addr),
4228 			   wpa_ssid_txt(wpa_s->p2p_join_ssid,
4229 					wpa_s->p2p_join_ssid_len));
4230 		bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4231 				  wpa_s->p2p_join_ssid,
4232 				  wpa_s->p2p_join_ssid_len);
4233 	}
4234 	if (!bss) {
4235 		wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4236 			   MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4237 		bss = wpa_bss_get_bssid_latest(wpa_s,
4238 					       wpa_s->pending_join_iface_addr);
4239 	}
4240 	if (bss) {
4241 		freq = bss->freq;
4242 		wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4243 			   "from BSS table: %d MHz (SSID %s)", freq,
4244 			   wpa_ssid_txt(bss->ssid, bss->ssid_len));
4245 	}
4246 	if (freq > 0) {
4247 		u16 method;
4248 
4249 		if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4250 			wpa_msg_global(wpa_s->parent, MSG_INFO,
4251 				       P2P_EVENT_GROUP_FORMATION_FAILURE
4252 				       "reason=FREQ_CONFLICT");
4253 			return;
4254 		}
4255 
4256 		wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4257 			   "prior to joining an existing group (GO " MACSTR
4258 			   " freq=%u MHz)",
4259 			   MAC2STR(wpa_s->pending_join_dev_addr), freq);
4260 		wpa_s->pending_pd_before_join = 1;
4261 
4262 		switch (wpa_s->pending_join_wps_method) {
4263 		case WPS_PIN_DISPLAY:
4264 			method = WPS_CONFIG_KEYPAD;
4265 			break;
4266 		case WPS_PIN_KEYPAD:
4267 			method = WPS_CONFIG_DISPLAY;
4268 			break;
4269 		case WPS_PBC:
4270 			method = WPS_CONFIG_PUSHBUTTON;
4271 			break;
4272 		default:
4273 			method = 0;
4274 			break;
4275 		}
4276 
4277 		if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4278 					       wpa_s->pending_join_dev_addr) ==
4279 		     method)) {
4280 			/*
4281 			 * We have already performed provision discovery for
4282 			 * joining the group. Proceed directly to join
4283 			 * operation without duplicated provision discovery. */
4284 			wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4285 				   "with " MACSTR " already done - proceed to "
4286 				   "join",
4287 				   MAC2STR(wpa_s->pending_join_dev_addr));
4288 			wpa_s->pending_pd_before_join = 0;
4289 			goto start;
4290 		}
4291 
4292 		if (p2p_prov_disc_req(wpa_s->global->p2p,
4293 				      wpa_s->pending_join_dev_addr, method, 1,
4294 				      freq, wpa_s->user_initiated_pd) < 0) {
4295 			wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4296 				   "Discovery Request before joining an "
4297 				   "existing group");
4298 			wpa_s->pending_pd_before_join = 0;
4299 			goto start;
4300 		}
4301 		return;
4302 	}
4303 
4304 	wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4305 	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4306 	eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4307 	wpas_p2p_check_join_scan_limit(wpa_s);
4308 	return;
4309 
4310 start:
4311 	/* Start join operation immediately */
4312 	wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4313 }
4314 
4315 
4316 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4317 				   const u8 *ssid, size_t ssid_len)
4318 {
4319 	int ret;
4320 	struct wpa_driver_scan_params params;
4321 	struct wpabuf *wps_ie, *ies;
4322 	size_t ielen;
4323 	int freqs[2] = { 0, 0 };
4324 
4325 	os_memset(&params, 0, sizeof(params));
4326 
4327 	/* P2P Wildcard SSID */
4328 	params.num_ssids = 1;
4329 	if (ssid && ssid_len) {
4330 		params.ssids[0].ssid = ssid;
4331 		params.ssids[0].ssid_len = ssid_len;
4332 		os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4333 		wpa_s->p2p_join_ssid_len = ssid_len;
4334 	} else {
4335 		params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4336 		params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4337 		wpa_s->p2p_join_ssid_len = 0;
4338 	}
4339 
4340 	wpa_s->wps->dev.p2p = 1;
4341 	wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4342 					wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4343 					NULL);
4344 	if (wps_ie == NULL) {
4345 		wpas_p2p_scan_res_join(wpa_s, NULL);
4346 		return;
4347 	}
4348 
4349 	ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4350 	ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4351 	if (ies == NULL) {
4352 		wpabuf_free(wps_ie);
4353 		wpas_p2p_scan_res_join(wpa_s, NULL);
4354 		return;
4355 	}
4356 	wpabuf_put_buf(ies, wps_ie);
4357 	wpabuf_free(wps_ie);
4358 
4359 	p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4360 
4361 	params.p2p_probe = 1;
4362 	params.extra_ies = wpabuf_head(ies);
4363 	params.extra_ies_len = wpabuf_len(ies);
4364 
4365 	if (!freq) {
4366 		int oper_freq;
4367 		/*
4368 		 * If freq is not provided, check the operating freq of the GO
4369 		 * and use a single channel scan on if possible.
4370 		 */
4371 		oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4372 					      wpa_s->pending_join_iface_addr);
4373 		if (oper_freq > 0)
4374 			freq = oper_freq;
4375 	}
4376 	if (freq > 0) {
4377 		freqs[0] = freq;
4378 		params.freqs = freqs;
4379 	}
4380 
4381 	/*
4382 	 * Run a scan to update BSS table and start Provision Discovery once
4383 	 * the new scan results become available.
4384 	 */
4385 	ret = wpa_drv_scan(wpa_s, &params);
4386 	if (!ret) {
4387 		os_get_reltime(&wpa_s->scan_trigger_time);
4388 		wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4389 		wpa_s->own_scan_requested = 1;
4390 	}
4391 
4392 	wpabuf_free(ies);
4393 
4394 	if (ret) {
4395 		wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4396 			   "try again later");
4397 		eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4398 		eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4399 		wpas_p2p_check_join_scan_limit(wpa_s);
4400 	}
4401 }
4402 
4403 
4404 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4405 {
4406 	struct wpa_supplicant *wpa_s = eloop_ctx;
4407 	wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4408 }
4409 
4410 
4411 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4412 			 const u8 *dev_addr, enum p2p_wps_method wps_method,
4413 			 int auto_join, int op_freq,
4414 			 const u8 *ssid, size_t ssid_len)
4415 {
4416 	wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4417 		   MACSTR " dev " MACSTR " op_freq=%d)%s",
4418 		   MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4419 		   auto_join ? " (auto_join)" : "");
4420 	if (ssid && ssid_len) {
4421 		wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4422 			   wpa_ssid_txt(ssid, ssid_len));
4423 	}
4424 
4425 	wpa_s->p2p_auto_pd = 0;
4426 	wpa_s->p2p_auto_join = !!auto_join;
4427 	os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4428 	os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4429 	wpa_s->pending_join_wps_method = wps_method;
4430 
4431 	/* Make sure we are not running find during connection establishment */
4432 	wpas_p2p_stop_find(wpa_s);
4433 
4434 	wpa_s->p2p_join_scan_count = 0;
4435 	wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4436 	return 0;
4437 }
4438 
4439 
4440 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4441 			       const u8 *ssid, size_t ssid_len)
4442 {
4443 	struct wpa_supplicant *group;
4444 	struct p2p_go_neg_results res;
4445 	struct wpa_bss *bss;
4446 
4447 	group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4448 	if (group == NULL)
4449 		return -1;
4450 	if (group != wpa_s) {
4451 		os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4452 			  sizeof(group->p2p_pin));
4453 		group->p2p_wps_method = wpa_s->p2p_wps_method;
4454 	} else {
4455 		/*
4456 		 * Need to mark the current interface for p2p_group_formation
4457 		 * when a separate group interface is not used. This is needed
4458 		 * to allow p2p_cancel stop a pending p2p_connect-join.
4459 		 * wpas_p2p_init_group_interface() addresses this for the case
4460 		 * where a separate group interface is used.
4461 		 */
4462 		wpa_s->global->p2p_group_formation = wpa_s;
4463 	}
4464 
4465 	group->p2p_in_provisioning = 1;
4466 	group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4467 
4468 	os_memset(&res, 0, sizeof(res));
4469 	os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4470 	os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4471 		  ETH_ALEN);
4472 	res.wps_method = wpa_s->pending_join_wps_method;
4473 	if (freq && ssid && ssid_len) {
4474 		res.freq = freq;
4475 		res.ssid_len = ssid_len;
4476 		os_memcpy(res.ssid, ssid, ssid_len);
4477 	} else {
4478 		bss = wpa_bss_get_bssid_latest(wpa_s,
4479 					       wpa_s->pending_join_iface_addr);
4480 		if (bss) {
4481 			res.freq = bss->freq;
4482 			res.ssid_len = bss->ssid_len;
4483 			os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4484 			wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4485 				   bss->freq,
4486 				   wpa_ssid_txt(bss->ssid, bss->ssid_len));
4487 		}
4488 	}
4489 
4490 	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4491 		wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4492 			   "starting client");
4493 		wpa_drv_cancel_remain_on_channel(wpa_s);
4494 		wpa_s->off_channel_freq = 0;
4495 		wpa_s->roc_waiting_drv_freq = 0;
4496 	}
4497 	wpas_start_wps_enrollee(group, &res);
4498 
4499 	/*
4500 	 * Allow a longer timeout for join-a-running-group than normal 15
4501 	 * second group formation timeout since the GO may not have authorized
4502 	 * our connection yet.
4503 	 */
4504 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4505 	eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4506 			       wpa_s, NULL);
4507 
4508 	return 0;
4509 }
4510 
4511 
4512 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4513 				int *force_freq, int *pref_freq, int go)
4514 {
4515 	int *freqs, res;
4516 	unsigned int freq_in_use = 0, num, i;
4517 
4518 	freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4519 	if (!freqs)
4520 		return -1;
4521 
4522 	num = get_shared_radio_freqs(wpa_s, freqs,
4523 				     wpa_s->num_multichan_concurrent);
4524 	wpa_printf(MSG_DEBUG,
4525 		   "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4526 		   freq, wpa_s->num_multichan_concurrent, num);
4527 
4528 	if (freq > 0) {
4529 		int ret;
4530 		if (go)
4531 			ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4532 		else
4533 			ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4534 		if (!ret) {
4535 			wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4536 				   "(%u MHz) is not supported for P2P uses",
4537 				   freq);
4538 			res = -3;
4539 			goto exit_free;
4540 		}
4541 
4542 		for (i = 0; i < num; i++) {
4543 			if (freqs[i] == freq)
4544 				freq_in_use = 1;
4545 		}
4546 
4547 		if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4548 			wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4549 				   freq);
4550 			res = -2;
4551 			goto exit_free;
4552 		}
4553 		wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4554 			   "requested channel (%u MHz)", freq);
4555 		*force_freq = freq;
4556 		goto exit_ok;
4557 	}
4558 
4559 	for (i = 0; i < num; i++) {
4560 		if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4561 			continue;
4562 
4563 		if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
4564 			wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4565 				   freqs[i]);
4566 			*pref_freq = freqs[i];
4567 		} else {
4568 			wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4569 				   freqs[i]);
4570 			*force_freq = freqs[i];
4571 		}
4572 		break;
4573 	}
4574 
4575 	if (i == num) {
4576 		if (num < wpa_s->num_multichan_concurrent && num > 0) {
4577 			wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4578 			*force_freq = 0;
4579 		} else if (num < wpa_s->num_multichan_concurrent) {
4580 			wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4581 			*force_freq = 0;
4582 		} else {
4583 			wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4584 			res = -2;
4585 			goto exit_free;
4586 		}
4587 	}
4588 
4589 exit_ok:
4590 	res = 0;
4591 exit_free:
4592 	os_free(freqs);
4593 	return res;
4594 }
4595 
4596 
4597 /**
4598  * wpas_p2p_connect - Request P2P Group Formation to be started
4599  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4600  * @peer_addr: Address of the peer P2P Device
4601  * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4602  * @persistent_group: Whether to create a persistent group
4603  * @auto_join: Whether to select join vs. GO Negotiation automatically
4604  * @join: Whether to join an existing group (as a client) instead of starting
4605  *	Group Owner negotiation; @peer_addr is BSSID in that case
4606  * @auth: Whether to only authorize the connection instead of doing that and
4607  *	initiating Group Owner negotiation
4608  * @go_intent: GO Intent or -1 to use default
4609  * @freq: Frequency for the group or 0 for auto-selection
4610  * @persistent_id: Persistent group credentials to use for forcing GO
4611  *	parameters or -1 to generate new values (SSID/passphrase)
4612  * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4613  *	interoperability workaround when initiating group formation
4614  * @ht40: Start GO with 40 MHz channel width
4615  * @vht:  Start GO with VHT support
4616  * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4617  *	failure, -2 on failure due to channel not currently available,
4618  *	-3 if forced channel is not supported
4619  */
4620 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4621 		     const char *pin, enum p2p_wps_method wps_method,
4622 		     int persistent_group, int auto_join, int join, int auth,
4623 		     int go_intent, int freq, int persistent_id, int pd,
4624 		     int ht40, int vht)
4625 {
4626 	int force_freq = 0, pref_freq = 0;
4627 	int ret = 0, res;
4628 	enum wpa_driver_if_type iftype;
4629 	const u8 *if_addr;
4630 	struct wpa_ssid *ssid = NULL;
4631 
4632 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4633 		return -1;
4634 
4635 	if (persistent_id >= 0) {
4636 		ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4637 		if (ssid == NULL || ssid->disabled != 2 ||
4638 		    ssid->mode != WPAS_MODE_P2P_GO)
4639 			return -1;
4640 	}
4641 
4642 	os_free(wpa_s->global->add_psk);
4643 	wpa_s->global->add_psk = NULL;
4644 
4645 	wpa_s->global->p2p_fail_on_wps_complete = 0;
4646 
4647 	if (go_intent < 0)
4648 		go_intent = wpa_s->conf->p2p_go_intent;
4649 
4650 	if (!auth)
4651 		wpa_s->p2p_long_listen = 0;
4652 
4653 	wpa_s->p2p_wps_method = wps_method;
4654 	wpa_s->p2p_persistent_group = !!persistent_group;
4655 	wpa_s->p2p_persistent_id = persistent_id;
4656 	wpa_s->p2p_go_intent = go_intent;
4657 	wpa_s->p2p_connect_freq = freq;
4658 	wpa_s->p2p_fallback_to_go_neg = 0;
4659 	wpa_s->p2p_pd_before_go_neg = !!pd;
4660 	wpa_s->p2p_go_ht40 = !!ht40;
4661 	wpa_s->p2p_go_vht = !!vht;
4662 
4663 	if (pin)
4664 		os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4665 	else if (wps_method == WPS_PIN_DISPLAY) {
4666 		ret = wps_generate_pin();
4667 		os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4668 			    ret);
4669 		wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4670 			   wpa_s->p2p_pin);
4671 	} else
4672 		wpa_s->p2p_pin[0] = '\0';
4673 
4674 	if (join || auto_join) {
4675 		u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4676 		if (auth) {
4677 			wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4678 				   "connect a running group from " MACSTR,
4679 				   MAC2STR(peer_addr));
4680 			os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4681 			return ret;
4682 		}
4683 		os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4684 		if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4685 					   iface_addr) < 0) {
4686 			os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4687 			p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4688 					 dev_addr);
4689 		}
4690 		if (auto_join) {
4691 			os_get_reltime(&wpa_s->p2p_auto_started);
4692 			wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4693 				   "%ld.%06ld",
4694 				   wpa_s->p2p_auto_started.sec,
4695 				   wpa_s->p2p_auto_started.usec);
4696 		}
4697 		wpa_s->user_initiated_pd = 1;
4698 		if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4699 				  auto_join, freq, NULL, 0) < 0)
4700 			return -1;
4701 		return ret;
4702 	}
4703 
4704 	res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4705 				   go_intent == 15);
4706 	if (res)
4707 		return res;
4708 	wpas_p2p_set_own_freq_preference(wpa_s,
4709 					 force_freq ? force_freq : pref_freq);
4710 
4711 	wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4712 
4713 	if (wpa_s->create_p2p_iface) {
4714 		/* Prepare to add a new interface for the group */
4715 		iftype = WPA_IF_P2P_GROUP;
4716 		if (go_intent == 15)
4717 			iftype = WPA_IF_P2P_GO;
4718 		if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4719 			wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4720 				   "interface for the group");
4721 			return -1;
4722 		}
4723 
4724 		if_addr = wpa_s->pending_interface_addr;
4725 	} else
4726 		if_addr = wpa_s->own_addr;
4727 
4728 	if (auth) {
4729 		if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
4730 					 go_intent, if_addr,
4731 					 force_freq, persistent_group, ssid,
4732 					 pref_freq) < 0)
4733 			return -1;
4734 		return ret;
4735 	}
4736 
4737 	if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4738 				  go_intent, if_addr, force_freq,
4739 				  persistent_group, ssid, pref_freq) < 0) {
4740 		if (wpa_s->create_p2p_iface)
4741 			wpas_p2p_remove_pending_group_interface(wpa_s);
4742 		return -1;
4743 	}
4744 	return ret;
4745 }
4746 
4747 
4748 /**
4749  * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4750  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4751  * @freq: Frequency of the channel in MHz
4752  * @duration: Duration of the stay on the channel in milliseconds
4753  *
4754  * This callback is called when the driver indicates that it has started the
4755  * requested remain-on-channel duration.
4756  */
4757 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4758 				   unsigned int freq, unsigned int duration)
4759 {
4760 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4761 		return;
4762 	if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4763 		p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4764 			      wpa_s->pending_listen_duration);
4765 		wpa_s->pending_listen_freq = 0;
4766 	}
4767 }
4768 
4769 
4770 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4771 				 unsigned int timeout)
4772 {
4773 	/* Limit maximum Listen state time based on driver limitation. */
4774 	if (timeout > wpa_s->max_remain_on_chan)
4775 		timeout = wpa_s->max_remain_on_chan;
4776 
4777 	return p2p_listen(wpa_s->global->p2p, timeout);
4778 }
4779 
4780 
4781 /**
4782  * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4783  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4784  * @freq: Frequency of the channel in MHz
4785  *
4786  * This callback is called when the driver indicates that a remain-on-channel
4787  * operation has been completed, i.e., the duration on the requested channel
4788  * has timed out.
4789  */
4790 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4791 					  unsigned int freq)
4792 {
4793 	wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4794 		   "(p2p_long_listen=%d ms pending_action_tx=%p)",
4795 		   wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
4796 	wpas_p2p_listen_work_done(wpa_s);
4797 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4798 		return;
4799 	if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4800 		return; /* P2P module started a new operation */
4801 	if (offchannel_pending_action_tx(wpa_s))
4802 		return;
4803 	if (wpa_s->p2p_long_listen > 0)
4804 		wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4805 	if (wpa_s->p2p_long_listen > 0) {
4806 		wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4807 		wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4808 	} else {
4809 		/*
4810 		 * When listen duration is over, stop listen & update p2p_state
4811 		 * to IDLE.
4812 		 */
4813 		p2p_stop_listen(wpa_s->global->p2p);
4814 	}
4815 }
4816 
4817 
4818 /**
4819  * wpas_p2p_group_remove - Remove a P2P group
4820  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4821  * @ifname: Network interface name of the group interface or "*" to remove all
4822  *	groups
4823  * Returns: 0 on success, -1 on failure
4824  *
4825  * This function is used to remove a P2P group. This can be used to disconnect
4826  * from a group in which the local end is a P2P Client or to end a P2P Group in
4827  * case the local end is the Group Owner. If a virtual network interface was
4828  * created for this group, that interface will be removed. Otherwise, only the
4829  * configured P2P group network will be removed from the interface.
4830  */
4831 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4832 {
4833 	struct wpa_global *global = wpa_s->global;
4834 
4835 	if (os_strcmp(ifname, "*") == 0) {
4836 		struct wpa_supplicant *prev;
4837 		wpa_s = global->ifaces;
4838 		while (wpa_s) {
4839 			prev = wpa_s;
4840 			wpa_s = wpa_s->next;
4841 			if (prev->p2p_group_interface !=
4842 			    NOT_P2P_GROUP_INTERFACE ||
4843 			    (prev->current_ssid &&
4844 			     prev->current_ssid->p2p_group))
4845 				wpas_p2p_disconnect(prev);
4846 		}
4847 		return 0;
4848 	}
4849 
4850 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4851 		if (os_strcmp(wpa_s->ifname, ifname) == 0)
4852 			break;
4853 	}
4854 
4855 	return wpas_p2p_disconnect(wpa_s);
4856 }
4857 
4858 
4859 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4860 {
4861 	unsigned int r;
4862 
4863 	if (freq == 2) {
4864 		wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4865 			   "band");
4866 		if (wpa_s->best_24_freq > 0 &&
4867 		    p2p_supported_freq_go(wpa_s->global->p2p,
4868 					  wpa_s->best_24_freq)) {
4869 			freq = wpa_s->best_24_freq;
4870 			wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4871 				   "channel: %d MHz", freq);
4872 		} else {
4873 			os_get_random((u8 *) &r, sizeof(r));
4874 			freq = 2412 + (r % 3) * 25;
4875 			wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4876 				   "channel: %d MHz", freq);
4877 		}
4878 	}
4879 
4880 	if (freq == 5) {
4881 		wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4882 			   "band");
4883 		if (wpa_s->best_5_freq > 0 &&
4884 		    p2p_supported_freq_go(wpa_s->global->p2p,
4885 				       wpa_s->best_5_freq)) {
4886 			freq = wpa_s->best_5_freq;
4887 			wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4888 				   "channel: %d MHz", freq);
4889 		} else {
4890 			os_get_random((u8 *) &r, sizeof(r));
4891 			freq = 5180 + (r % 4) * 20;
4892 			if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4893 				wpa_printf(MSG_DEBUG, "P2P: Could not select "
4894 					   "5 GHz channel for P2P group");
4895 				return -1;
4896 			}
4897 			wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4898 				   "channel: %d MHz", freq);
4899 		}
4900 	}
4901 
4902 	if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4903 		wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4904 			   "(%u MHz) is not supported for P2P uses",
4905 			   freq);
4906 		return -1;
4907 	}
4908 
4909 	return freq;
4910 }
4911 
4912 
4913 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4914 				   struct p2p_go_neg_results *params,
4915 				   int freq, int ht40, int vht,
4916 				   const struct p2p_channels *channels)
4917 {
4918 	int res, *freqs;
4919 	unsigned int pref_freq;
4920 	unsigned int num, i;
4921 
4922 	os_memset(params, 0, sizeof(*params));
4923 	params->role_go = 1;
4924 	params->ht40 = ht40;
4925 	params->vht = vht;
4926 	if (freq) {
4927 		if (!freq_included(channels, freq)) {
4928 			wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4929 				   "accepted", freq);
4930 			return -1;
4931 		}
4932 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4933 			   "frequency %d MHz", freq);
4934 		params->freq = freq;
4935 	} else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4936 		   wpa_s->conf->p2p_oper_channel >= 1 &&
4937 		   wpa_s->conf->p2p_oper_channel <= 11 &&
4938 		   freq_included(channels,
4939 				 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
4940 		params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4941 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4942 			   "frequency %d MHz", params->freq);
4943 	} else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4944 		    wpa_s->conf->p2p_oper_reg_class == 116 ||
4945 		    wpa_s->conf->p2p_oper_reg_class == 117 ||
4946 		    wpa_s->conf->p2p_oper_reg_class == 124 ||
4947 		    wpa_s->conf->p2p_oper_reg_class == 126 ||
4948 		    wpa_s->conf->p2p_oper_reg_class == 127) &&
4949 		   freq_included(channels,
4950 				 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
4951 		params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4952 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4953 			   "frequency %d MHz", params->freq);
4954 	} else if (wpa_s->conf->p2p_oper_channel == 0 &&
4955 		   wpa_s->best_overall_freq > 0 &&
4956 		   p2p_supported_freq_go(wpa_s->global->p2p,
4957 					 wpa_s->best_overall_freq) &&
4958 		   freq_included(channels, wpa_s->best_overall_freq)) {
4959 		params->freq = wpa_s->best_overall_freq;
4960 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4961 			   "channel %d MHz", params->freq);
4962 	} else if (wpa_s->conf->p2p_oper_channel == 0 &&
4963 		   wpa_s->best_24_freq > 0 &&
4964 		   p2p_supported_freq_go(wpa_s->global->p2p,
4965 					 wpa_s->best_24_freq) &&
4966 		   freq_included(channels, wpa_s->best_24_freq)) {
4967 		params->freq = wpa_s->best_24_freq;
4968 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4969 			   "channel %d MHz", params->freq);
4970 	} else if (wpa_s->conf->p2p_oper_channel == 0 &&
4971 		   wpa_s->best_5_freq > 0 &&
4972 		   p2p_supported_freq_go(wpa_s->global->p2p,
4973 					 wpa_s->best_5_freq) &&
4974 		   freq_included(channels, wpa_s->best_5_freq)) {
4975 		params->freq = wpa_s->best_5_freq;
4976 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4977 			   "channel %d MHz", params->freq);
4978 	} else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4979 						  channels))) {
4980 		params->freq = pref_freq;
4981 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4982 			   "channels", params->freq);
4983 	} else {
4984 		int chan;
4985 		for (chan = 0; chan < 11; chan++) {
4986 			params->freq = 2412 + chan * 5;
4987 			if (!wpas_p2p_disallowed_freq(wpa_s->global,
4988 						      params->freq) &&
4989 			    freq_included(channels, params->freq))
4990 				break;
4991 		}
4992 		if (chan == 11) {
4993 			wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4994 				   "allowed");
4995 			return -1;
4996 		}
4997 		wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4998 			   "known)", params->freq);
4999 	}
5000 
5001 	freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
5002 	if (!freqs)
5003 		return -1;
5004 
5005 	res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5006 					wpa_s->num_multichan_concurrent);
5007 	if (res < 0) {
5008 		os_free(freqs);
5009 		return -1;
5010 	}
5011 	num = res;
5012 
5013 	for (i = 0; i < num; i++) {
5014 		if (freq && freqs[i] == freq)
5015 			break;
5016 		if (!freq && freq_included(channels, freqs[i])) {
5017 			wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5018 				   freqs[i]);
5019 			params->freq = freqs[i];
5020 			break;
5021 		}
5022 	}
5023 
5024 	if (i == num) {
5025 		if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5026 			if (freq)
5027 				wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5028 			else
5029 				wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5030 			os_free(freqs);
5031 			return -1;
5032 		} else if (num == 0) {
5033 			wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5034 		} else {
5035 			wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5036 		}
5037 	}
5038 
5039 	os_free(freqs);
5040 	return 0;
5041 }
5042 
5043 
5044 static struct wpa_supplicant *
5045 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5046 			 int go)
5047 {
5048 	struct wpa_supplicant *group_wpa_s;
5049 
5050 	if (!wpas_p2p_create_iface(wpa_s)) {
5051 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5052 			"operations");
5053 		wpa_s->p2p_first_connection_timeout = 0;
5054 		return wpa_s;
5055 	}
5056 
5057 	if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5058 					 WPA_IF_P2P_CLIENT) < 0) {
5059 		wpa_msg_global(wpa_s, MSG_ERROR,
5060 			       "P2P: Failed to add group interface");
5061 		return NULL;
5062 	}
5063 	group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5064 	if (group_wpa_s == NULL) {
5065 		wpa_msg_global(wpa_s, MSG_ERROR,
5066 			       "P2P: Failed to initialize group interface");
5067 		wpas_p2p_remove_pending_group_interface(wpa_s);
5068 		return NULL;
5069 	}
5070 
5071 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5072 		group_wpa_s->ifname);
5073 	group_wpa_s->p2p_first_connection_timeout = 0;
5074 	return group_wpa_s;
5075 }
5076 
5077 
5078 /**
5079  * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5080  * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5081  * @persistent_group: Whether to create a persistent group
5082  * @freq: Frequency for the group or 0 to indicate no hardcoding
5083  * @ht40: Start GO with 40 MHz channel width
5084  * @vht:  Start GO with VHT support
5085  * Returns: 0 on success, -1 on failure
5086  *
5087  * This function creates a new P2P group with the local end as the Group Owner,
5088  * i.e., without using Group Owner Negotiation.
5089  */
5090 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5091 		       int freq, int ht40, int vht)
5092 {
5093 	struct p2p_go_neg_results params;
5094 
5095 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5096 		return -1;
5097 
5098 	os_free(wpa_s->global->add_psk);
5099 	wpa_s->global->add_psk = NULL;
5100 
5101 	/* Make sure we are not running find during connection establishment */
5102 	wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5103 	wpas_p2p_stop_find_oper(wpa_s);
5104 
5105 	freq = wpas_p2p_select_go_freq(wpa_s, freq);
5106 	if (freq < 0)
5107 		return -1;
5108 
5109 	if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, NULL))
5110 		return -1;
5111 	if (params.freq &&
5112 	    !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5113 		wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5114 			   "(%u MHz) is not supported for P2P uses",
5115 			   params.freq);
5116 		return -1;
5117 	}
5118 	p2p_go_params(wpa_s->global->p2p, &params);
5119 	params.persistent_group = persistent_group;
5120 
5121 	wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5122 	if (wpa_s == NULL)
5123 		return -1;
5124 	wpas_start_wps_go(wpa_s, &params, 0);
5125 
5126 	return 0;
5127 }
5128 
5129 
5130 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5131 				 struct wpa_ssid *params, int addr_allocated)
5132 {
5133 	struct wpa_ssid *ssid;
5134 
5135 	wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5136 	if (wpa_s == NULL)
5137 		return -1;
5138 	wpa_s->p2p_last_4way_hs_fail = NULL;
5139 
5140 	wpa_supplicant_ap_deinit(wpa_s);
5141 
5142 	ssid = wpa_config_add_network(wpa_s->conf);
5143 	if (ssid == NULL)
5144 		return -1;
5145 	wpa_config_set_network_defaults(ssid);
5146 	ssid->temporary = 1;
5147 	ssid->proto = WPA_PROTO_RSN;
5148 	ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5149 	ssid->group_cipher = WPA_CIPHER_CCMP;
5150 	ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5151 	ssid->ssid = os_malloc(params->ssid_len);
5152 	if (ssid->ssid == NULL) {
5153 		wpa_config_remove_network(wpa_s->conf, ssid->id);
5154 		return -1;
5155 	}
5156 	os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5157 	ssid->ssid_len = params->ssid_len;
5158 	ssid->p2p_group = 1;
5159 	ssid->export_keys = 1;
5160 	if (params->psk_set) {
5161 		os_memcpy(ssid->psk, params->psk, 32);
5162 		ssid->psk_set = 1;
5163 	}
5164 	if (params->passphrase)
5165 		ssid->passphrase = os_strdup(params->passphrase);
5166 
5167 	wpa_s->show_group_started = 1;
5168 
5169 	wpa_supplicant_select_network(wpa_s, ssid);
5170 
5171 	return 0;
5172 }
5173 
5174 
5175 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5176 				  struct wpa_ssid *ssid, int addr_allocated,
5177 				  int freq, int ht40, int vht,
5178 				  const struct p2p_channels *channels,
5179 				  int connection_timeout)
5180 {
5181 	struct p2p_go_neg_results params;
5182 	int go = 0;
5183 
5184 	if (ssid->disabled != 2 || ssid->ssid == NULL)
5185 		return -1;
5186 
5187 	if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5188 	    go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5189 		wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5190 			   "already running");
5191 		return 0;
5192 	}
5193 
5194 	os_free(wpa_s->global->add_psk);
5195 	wpa_s->global->add_psk = NULL;
5196 
5197 	/* Make sure we are not running find during connection establishment */
5198 	wpas_p2p_stop_find_oper(wpa_s);
5199 
5200 	wpa_s->p2p_fallback_to_go_neg = 0;
5201 
5202 	if (ssid->mode == WPAS_MODE_INFRA)
5203 		return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
5204 
5205 	if (ssid->mode != WPAS_MODE_P2P_GO)
5206 		return -1;
5207 
5208 	freq = wpas_p2p_select_go_freq(wpa_s, freq);
5209 	if (freq < 0)
5210 		return -1;
5211 
5212 	if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40, vht, channels))
5213 		return -1;
5214 
5215 	params.role_go = 1;
5216 	params.psk_set = ssid->psk_set;
5217 	if (params.psk_set)
5218 		os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5219 	if (ssid->passphrase) {
5220 		if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5221 			wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5222 				   "persistent group");
5223 			return -1;
5224 		}
5225 		os_strlcpy(params.passphrase, ssid->passphrase,
5226 			   sizeof(params.passphrase));
5227 	}
5228 	os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5229 	params.ssid_len = ssid->ssid_len;
5230 	params.persistent_group = 1;
5231 
5232 	wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5233 	if (wpa_s == NULL)
5234 		return -1;
5235 
5236 	wpa_s->p2p_first_connection_timeout = connection_timeout;
5237 	wpas_start_wps_go(wpa_s, &params, 0);
5238 
5239 	return 0;
5240 }
5241 
5242 
5243 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5244 			       struct wpabuf *proberesp_ies)
5245 {
5246 	struct wpa_supplicant *wpa_s = ctx;
5247 	if (wpa_s->ap_iface) {
5248 		struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5249 		if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5250 			wpabuf_free(beacon_ies);
5251 			wpabuf_free(proberesp_ies);
5252 			return;
5253 		}
5254 		if (beacon_ies) {
5255 			wpabuf_free(hapd->p2p_beacon_ie);
5256 			hapd->p2p_beacon_ie = beacon_ies;
5257 		}
5258 		wpabuf_free(hapd->p2p_probe_resp_ie);
5259 		hapd->p2p_probe_resp_ie = proberesp_ies;
5260 	} else {
5261 		wpabuf_free(beacon_ies);
5262 		wpabuf_free(proberesp_ies);
5263 	}
5264 	wpa_supplicant_ap_update_beacon(wpa_s);
5265 }
5266 
5267 
5268 static void wpas_p2p_idle_update(void *ctx, int idle)
5269 {
5270 	struct wpa_supplicant *wpa_s = ctx;
5271 	if (!wpa_s->ap_iface)
5272 		return;
5273 	wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
5274 	if (idle) {
5275 		if (wpa_s->global->p2p_fail_on_wps_complete &&
5276 		    wpa_s->p2p_in_provisioning) {
5277 			wpas_p2p_grpform_fail_after_wps(wpa_s);
5278 			return;
5279 		}
5280 		wpas_p2p_set_group_idle_timeout(wpa_s);
5281 	} else
5282 		eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5283 }
5284 
5285 
5286 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
5287 				       struct wpa_ssid *ssid)
5288 {
5289 	struct p2p_group *group;
5290 	struct p2p_group_config *cfg;
5291 
5292 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5293 		return NULL;
5294 
5295 	cfg = os_zalloc(sizeof(*cfg));
5296 	if (cfg == NULL)
5297 		return NULL;
5298 
5299 	if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
5300 		cfg->persistent_group = 2;
5301 	else if (ssid->p2p_persistent_group)
5302 		cfg->persistent_group = 1;
5303 	os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5304 	if (wpa_s->max_stations &&
5305 	    wpa_s->max_stations < wpa_s->conf->max_num_sta)
5306 		cfg->max_clients = wpa_s->max_stations;
5307 	else
5308 		cfg->max_clients = wpa_s->conf->max_num_sta;
5309 	os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5310 	cfg->ssid_len = ssid->ssid_len;
5311 	cfg->freq = ssid->frequency;
5312 	cfg->cb_ctx = wpa_s;
5313 	cfg->ie_update = wpas_p2p_ie_update;
5314 	cfg->idle_update = wpas_p2p_idle_update;
5315 
5316 	group = p2p_group_init(wpa_s->global->p2p, cfg);
5317 	if (group == NULL)
5318 		os_free(cfg);
5319 	if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
5320 		p2p_group_notif_formation_done(group);
5321 	wpa_s->p2p_group = group;
5322 	return group;
5323 }
5324 
5325 
5326 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5327 			  int registrar)
5328 {
5329 	struct wpa_ssid *ssid = wpa_s->current_ssid;
5330 
5331 	if (!wpa_s->p2p_in_provisioning) {
5332 		wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5333 			   "provisioning not in progress");
5334 		return;
5335 	}
5336 
5337 	if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5338 		u8 go_dev_addr[ETH_ALEN];
5339 		os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5340 		wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5341 					  ssid->ssid_len);
5342 		/* Clear any stored provisioning info */
5343 		p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5344 	}
5345 
5346 	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5347 			     NULL);
5348 	wpa_s->p2p_go_group_formation_completed = 1;
5349 	if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5350 		/*
5351 		 * Use a separate timeout for initial data connection to
5352 		 * complete to allow the group to be removed automatically if
5353 		 * something goes wrong in this step before the P2P group idle
5354 		 * timeout mechanism is taken into use.
5355 		 */
5356 		wpa_dbg(wpa_s, MSG_DEBUG,
5357 			"P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5358 			P2P_MAX_INITIAL_CONN_WAIT);
5359 		eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5360 				       wpas_p2p_group_formation_timeout,
5361 				       wpa_s->parent, NULL);
5362 	} else if (ssid) {
5363 		/*
5364 		 * Use a separate timeout for initial data connection to
5365 		 * complete to allow the group to be removed automatically if
5366 		 * the client does not complete data connection successfully.
5367 		 */
5368 		wpa_dbg(wpa_s, MSG_DEBUG,
5369 			"P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5370 			P2P_MAX_INITIAL_CONN_WAIT_GO);
5371 		eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5372 				       wpas_p2p_group_formation_timeout,
5373 				       wpa_s->parent, NULL);
5374 		/*
5375 		 * Complete group formation on first successful data connection
5376 		 */
5377 		wpa_s->p2p_go_group_formation_completed = 0;
5378 	}
5379 	if (wpa_s->global->p2p)
5380 		p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5381 	wpas_group_formation_completed(wpa_s, 1);
5382 }
5383 
5384 
5385 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5386 			 struct wps_event_fail *fail)
5387 {
5388 	if (!wpa_s->p2p_in_provisioning) {
5389 		wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5390 			   "provisioning not in progress");
5391 		return;
5392 	}
5393 
5394 	if (wpa_s->go_params) {
5395 		p2p_clear_provisioning_info(
5396 			wpa_s->global->p2p,
5397 			wpa_s->go_params->peer_device_addr);
5398 	}
5399 
5400 	wpas_notify_p2p_wps_failed(wpa_s, fail);
5401 
5402 	if (wpa_s == wpa_s->global->p2p_group_formation) {
5403 		/*
5404 		 * Allow some time for the failed WPS negotiation exchange to
5405 		 * complete, but remove the group since group formation cannot
5406 		 * succeed after provisioning failure.
5407 		 */
5408 		wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5409 		wpa_s->global->p2p_fail_on_wps_complete = 1;
5410 		eloop_deplete_timeout(0, 50000,
5411 				      wpas_p2p_group_formation_timeout,
5412 				      wpa_s->parent, NULL);
5413 	}
5414 }
5415 
5416 
5417 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5418 {
5419 	if (!wpa_s->global->p2p_fail_on_wps_complete ||
5420 	    !wpa_s->p2p_in_provisioning)
5421 		return 0;
5422 
5423 	wpas_p2p_grpform_fail_after_wps(wpa_s);
5424 
5425 	return 1;
5426 }
5427 
5428 
5429 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5430 		       const char *config_method,
5431 		       enum wpas_p2p_prov_disc_use use)
5432 {
5433 	u16 config_methods;
5434 
5435 	wpa_s->p2p_fallback_to_go_neg = 0;
5436 	wpa_s->pending_pd_use = NORMAL_PD;
5437 	if (os_strncmp(config_method, "display", 7) == 0)
5438 		config_methods = WPS_CONFIG_DISPLAY;
5439 	else if (os_strncmp(config_method, "keypad", 6) == 0)
5440 		config_methods = WPS_CONFIG_KEYPAD;
5441 	else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5442 		 os_strncmp(config_method, "pushbutton", 10) == 0)
5443 		config_methods = WPS_CONFIG_PUSHBUTTON;
5444 	else {
5445 		wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5446 		return -1;
5447 	}
5448 
5449 	if (use == WPAS_P2P_PD_AUTO) {
5450 		os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5451 		wpa_s->pending_pd_config_methods = config_methods;
5452 		wpa_s->p2p_auto_pd = 1;
5453 		wpa_s->p2p_auto_join = 0;
5454 		wpa_s->pending_pd_before_join = 0;
5455 		wpa_s->auto_pd_scan_retry = 0;
5456 		wpas_p2p_stop_find(wpa_s);
5457 		wpa_s->p2p_join_scan_count = 0;
5458 		os_get_reltime(&wpa_s->p2p_auto_started);
5459 		wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5460 			   wpa_s->p2p_auto_started.sec,
5461 			   wpa_s->p2p_auto_started.usec);
5462 		wpas_p2p_join_scan(wpa_s, NULL);
5463 		return 0;
5464 	}
5465 
5466 	if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5467 		return -1;
5468 
5469 	return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
5470 				 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5471 				 0, 1);
5472 }
5473 
5474 
5475 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5476 			      char *end)
5477 {
5478 	return p2p_scan_result_text(ies, ies_len, buf, end);
5479 }
5480 
5481 
5482 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5483 {
5484 	if (!offchannel_pending_action_tx(wpa_s))
5485 		return;
5486 
5487 	wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5488 		   "operation request");
5489 	offchannel_clear_pending_action_tx(wpa_s);
5490 }
5491 
5492 
5493 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5494 		  enum p2p_discovery_type type,
5495 		  unsigned int num_req_dev_types, const u8 *req_dev_types,
5496 		  const u8 *dev_id, unsigned int search_delay)
5497 {
5498 	wpas_p2p_clear_pending_action_tx(wpa_s);
5499 	wpa_s->p2p_long_listen = 0;
5500 
5501 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5502 	    wpa_s->p2p_in_provisioning)
5503 		return -1;
5504 
5505 	wpa_supplicant_cancel_sched_scan(wpa_s);
5506 
5507 	return p2p_find(wpa_s->global->p2p, timeout, type,
5508 			num_req_dev_types, req_dev_types, dev_id,
5509 			search_delay);
5510 }
5511 
5512 
5513 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
5514 {
5515 	wpas_p2p_clear_pending_action_tx(wpa_s);
5516 	wpa_s->p2p_long_listen = 0;
5517 	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5518 	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5519 
5520 	if (wpa_s->global->p2p)
5521 		p2p_stop_find(wpa_s->global->p2p);
5522 
5523 	return 0;
5524 }
5525 
5526 
5527 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5528 {
5529 	if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5530 		return;
5531 	wpas_p2p_remove_pending_group_interface(wpa_s);
5532 }
5533 
5534 
5535 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5536 {
5537 	struct wpa_supplicant *wpa_s = eloop_ctx;
5538 	wpa_s->p2p_long_listen = 0;
5539 }
5540 
5541 
5542 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5543 {
5544 	int res;
5545 
5546 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5547 		return -1;
5548 
5549 	wpa_supplicant_cancel_sched_scan(wpa_s);
5550 	wpas_p2p_clear_pending_action_tx(wpa_s);
5551 
5552 	if (timeout == 0) {
5553 		/*
5554 		 * This is a request for unlimited Listen state. However, at
5555 		 * least for now, this is mapped to a Listen state for one
5556 		 * hour.
5557 		 */
5558 		timeout = 3600;
5559 	}
5560 	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5561 	wpa_s->p2p_long_listen = 0;
5562 
5563 	/*
5564 	 * Stop previous find/listen operation to avoid trying to request a new
5565 	 * remain-on-channel operation while the driver is still running the
5566 	 * previous one.
5567 	 */
5568 	if (wpa_s->global->p2p)
5569 		p2p_stop_find(wpa_s->global->p2p);
5570 
5571 	res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5572 	if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5573 		wpa_s->p2p_long_listen = timeout * 1000;
5574 		eloop_register_timeout(timeout, 0,
5575 				       wpas_p2p_long_listen_timeout,
5576 				       wpa_s, NULL);
5577 	}
5578 
5579 	return res;
5580 }
5581 
5582 
5583 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5584 			  u8 *buf, size_t len, int p2p_group)
5585 {
5586 	struct wpabuf *p2p_ie;
5587 	int ret;
5588 
5589 	if (wpa_s->global->p2p_disabled)
5590 		return -1;
5591 	if (wpa_s->global->p2p == NULL)
5592 		return -1;
5593 	if (bss == NULL)
5594 		return -1;
5595 
5596 	p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5597 	ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5598 			       p2p_group, p2p_ie);
5599 	wpabuf_free(p2p_ie);
5600 
5601 	return ret;
5602 }
5603 
5604 
5605 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
5606 			  const u8 *dst, const u8 *bssid,
5607 			  const u8 *ie, size_t ie_len, int ssi_signal)
5608 {
5609 	if (wpa_s->global->p2p_disabled)
5610 		return 0;
5611 	if (wpa_s->global->p2p == NULL)
5612 		return 0;
5613 
5614 	switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5615 				 ie, ie_len)) {
5616 	case P2P_PREQ_NOT_P2P:
5617 		wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5618 				 ssi_signal);
5619 		/* fall through */
5620 	case P2P_PREQ_MALFORMED:
5621 	case P2P_PREQ_NOT_LISTEN:
5622 	case P2P_PREQ_NOT_PROCESSED:
5623 	default: /* make gcc happy */
5624 		return 0;
5625 	case P2P_PREQ_PROCESSED:
5626 		return 1;
5627 	}
5628 }
5629 
5630 
5631 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5632 			const u8 *sa, const u8 *bssid,
5633 			u8 category, const u8 *data, size_t len, int freq)
5634 {
5635 	if (wpa_s->global->p2p_disabled)
5636 		return;
5637 	if (wpa_s->global->p2p == NULL)
5638 		return;
5639 
5640 	p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5641 		      freq);
5642 }
5643 
5644 
5645 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5646 {
5647 	if (wpa_s->global->p2p_disabled)
5648 		return;
5649 	if (wpa_s->global->p2p == NULL)
5650 		return;
5651 
5652 	p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5653 }
5654 
5655 
5656 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5657 {
5658 	p2p_group_deinit(wpa_s->p2p_group);
5659 	wpa_s->p2p_group = NULL;
5660 
5661 	wpa_s->ap_configured_cb = NULL;
5662 	wpa_s->ap_configured_cb_ctx = NULL;
5663 	wpa_s->ap_configured_cb_data = NULL;
5664 	wpa_s->connect_without_scan = NULL;
5665 }
5666 
5667 
5668 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5669 {
5670 	wpa_s->p2p_long_listen = 0;
5671 
5672 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5673 		return -1;
5674 
5675 	return p2p_reject(wpa_s->global->p2p, addr);
5676 }
5677 
5678 
5679 /* Invite to reinvoke a persistent group */
5680 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5681 		    struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
5682 		    int ht40, int vht, int pref_freq)
5683 {
5684 	enum p2p_invite_role role;
5685 	u8 *bssid = NULL;
5686 	int force_freq = 0;
5687 	int res;
5688 	int no_pref_freq_given = pref_freq == 0;
5689 
5690 	wpa_s->global->p2p_invite_group = NULL;
5691 	if (peer_addr)
5692 		os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5693 	else
5694 		os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5695 
5696 	wpa_s->p2p_persistent_go_freq = freq;
5697 	wpa_s->p2p_go_ht40 = !!ht40;
5698 	if (ssid->mode == WPAS_MODE_P2P_GO) {
5699 		role = P2P_INVITE_ROLE_GO;
5700 		if (peer_addr == NULL) {
5701 			wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5702 				   "address in invitation command");
5703 			return -1;
5704 		}
5705 		if (wpas_p2p_create_iface(wpa_s)) {
5706 			if (wpas_p2p_add_group_interface(wpa_s,
5707 							 WPA_IF_P2P_GO) < 0) {
5708 				wpa_printf(MSG_ERROR, "P2P: Failed to "
5709 					   "allocate a new interface for the "
5710 					   "group");
5711 				return -1;
5712 			}
5713 			bssid = wpa_s->pending_interface_addr;
5714 		} else
5715 			bssid = wpa_s->own_addr;
5716 	} else {
5717 		role = P2P_INVITE_ROLE_CLIENT;
5718 		peer_addr = ssid->bssid;
5719 	}
5720 	wpa_s->pending_invite_ssid_id = ssid->id;
5721 
5722 	res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5723 				   role == P2P_INVITE_ROLE_GO);
5724 	if (res)
5725 		return res;
5726 
5727 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5728 		return -1;
5729 
5730 	if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5731 	    no_pref_freq_given && pref_freq > 0 &&
5732 	    wpa_s->num_multichan_concurrent > 1 &&
5733 	    wpas_p2p_num_unused_channels(wpa_s) > 0) {
5734 		wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5735 			   pref_freq);
5736 		pref_freq = 0;
5737 	}
5738 
5739 	return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5740 			  ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5741 			  1, pref_freq, -1);
5742 }
5743 
5744 
5745 /* Invite to join an active group */
5746 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5747 			  const u8 *peer_addr, const u8 *go_dev_addr)
5748 {
5749 	struct wpa_global *global = wpa_s->global;
5750 	enum p2p_invite_role role;
5751 	u8 *bssid = NULL;
5752 	struct wpa_ssid *ssid;
5753 	int persistent;
5754 	int freq = 0, force_freq = 0, pref_freq = 0;
5755 	int res;
5756 
5757 	wpa_s->p2p_persistent_go_freq = 0;
5758 	wpa_s->p2p_go_ht40 = 0;
5759 	wpa_s->p2p_go_vht = 0;
5760 
5761 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5762 		if (os_strcmp(wpa_s->ifname, ifname) == 0)
5763 			break;
5764 	}
5765 	if (wpa_s == NULL) {
5766 		wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5767 		return -1;
5768 	}
5769 
5770 	ssid = wpa_s->current_ssid;
5771 	if (ssid == NULL) {
5772 		wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5773 			   "invitation");
5774 		return -1;
5775 	}
5776 
5777 	wpa_s->global->p2p_invite_group = wpa_s;
5778 	persistent = ssid->p2p_persistent_group &&
5779 		wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5780 					ssid->ssid, ssid->ssid_len);
5781 
5782 	if (ssid->mode == WPAS_MODE_P2P_GO) {
5783 		role = P2P_INVITE_ROLE_ACTIVE_GO;
5784 		bssid = wpa_s->own_addr;
5785 		if (go_dev_addr == NULL)
5786 			go_dev_addr = wpa_s->global->p2p_dev_addr;
5787 		freq = ssid->frequency;
5788 	} else {
5789 		role = P2P_INVITE_ROLE_CLIENT;
5790 		if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5791 			wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5792 				   "invite to current group");
5793 			return -1;
5794 		}
5795 		bssid = wpa_s->bssid;
5796 		if (go_dev_addr == NULL &&
5797 		    !is_zero_ether_addr(wpa_s->go_dev_addr))
5798 			go_dev_addr = wpa_s->go_dev_addr;
5799 		freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5800 			(int) wpa_s->assoc_freq;
5801 	}
5802 	wpa_s->parent->pending_invite_ssid_id = -1;
5803 
5804 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5805 		return -1;
5806 
5807 	res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5808 				   role == P2P_INVITE_ROLE_ACTIVE_GO);
5809 	if (res)
5810 		return res;
5811 	wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
5812 
5813 	return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5814 			  ssid->ssid, ssid->ssid_len, force_freq,
5815 			  go_dev_addr, persistent, pref_freq, -1);
5816 }
5817 
5818 
5819 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5820 {
5821 	struct wpa_ssid *ssid = wpa_s->current_ssid;
5822 	const char *ssid_txt;
5823 	u8 go_dev_addr[ETH_ALEN];
5824 	int network_id = -1;
5825 	int persistent;
5826 	int freq;
5827 	u8 ip[3 * 4];
5828 	char ip_addr[100];
5829 
5830 	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5831 		eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5832 				     wpa_s->parent, NULL);
5833 	}
5834 
5835 	if (!wpa_s->show_group_started || !ssid)
5836 		return;
5837 
5838 	wpa_s->show_group_started = 0;
5839 
5840 	ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5841 	os_memset(go_dev_addr, 0, ETH_ALEN);
5842 	if (ssid->bssid_set)
5843 		os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5844 	persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5845 					       ssid->ssid_len);
5846 	os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5847 
5848 	if (wpa_s->global->p2p_group_formation == wpa_s)
5849 		wpa_s->global->p2p_group_formation = NULL;
5850 
5851 	freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5852 		(int) wpa_s->assoc_freq;
5853 
5854 	ip_addr[0] = '\0';
5855 	if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
5856 		os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
5857 			    "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
5858 			    ip[0], ip[1], ip[2], ip[3],
5859 			    ip[4], ip[5], ip[6], ip[7],
5860 			    ip[8], ip[9], ip[10], ip[11]);
5861 	}
5862 
5863 	if (ssid->passphrase == NULL && ssid->psk_set) {
5864 		char psk[65];
5865 		wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5866 		wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5867 			       "%s client ssid=\"%s\" freq=%d psk=%s "
5868 			       "go_dev_addr=" MACSTR "%s%s",
5869 			       wpa_s->ifname, ssid_txt, freq, psk,
5870 			       MAC2STR(go_dev_addr),
5871 			       persistent ? " [PERSISTENT]" : "", ip_addr);
5872 	} else {
5873 		wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5874 			       "%s client ssid=\"%s\" freq=%d "
5875 			       "passphrase=\"%s\" go_dev_addr=" MACSTR "%s%s",
5876 			       wpa_s->ifname, ssid_txt, freq,
5877 			       ssid->passphrase ? ssid->passphrase : "",
5878 			       MAC2STR(go_dev_addr),
5879 			       persistent ? " [PERSISTENT]" : "", ip_addr);
5880 	}
5881 
5882 	if (persistent)
5883 		network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5884 							     ssid, go_dev_addr);
5885 	if (network_id < 0)
5886 		network_id = ssid->id;
5887 	wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
5888 }
5889 
5890 
5891 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5892 			  u32 interval1, u32 duration2, u32 interval2)
5893 {
5894 	int ret;
5895 
5896 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5897 		return -1;
5898 
5899 	if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5900 	    wpa_s->current_ssid == NULL ||
5901 	    wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5902 		return -1;
5903 
5904 	ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5905 			       wpa_s->own_addr, wpa_s->assoc_freq,
5906 			       duration1, interval1, duration2, interval2);
5907 	if (ret == 0)
5908 		wpa_s->waiting_presence_resp = 1;
5909 
5910 	return ret;
5911 }
5912 
5913 
5914 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5915 			unsigned int interval)
5916 {
5917 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5918 		return -1;
5919 
5920 	return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5921 }
5922 
5923 
5924 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5925 {
5926 	if (wpa_s->current_ssid == NULL) {
5927 		/*
5928 		 * current_ssid can be cleared when P2P client interface gets
5929 		 * disconnected, so assume this interface was used as P2P
5930 		 * client.
5931 		 */
5932 		return 1;
5933 	}
5934 	return wpa_s->current_ssid->p2p_group &&
5935 		wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5936 }
5937 
5938 
5939 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5940 {
5941 	struct wpa_supplicant *wpa_s = eloop_ctx;
5942 
5943 	if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
5944 		wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5945 			   "disabled");
5946 		return;
5947 	}
5948 
5949 	wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5950 		   "group");
5951 	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
5952 }
5953 
5954 
5955 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5956 {
5957 	int timeout;
5958 
5959 	if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5960 		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5961 
5962 	if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5963 		return;
5964 
5965 	timeout = wpa_s->conf->p2p_group_idle;
5966 	if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5967 	    (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5968 	    timeout = P2P_MAX_CLIENT_IDLE;
5969 
5970 	if (timeout == 0)
5971 		return;
5972 
5973 	if (timeout < 0) {
5974 		if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5975 			timeout = 0; /* special client mode no-timeout */
5976 		else
5977 			return;
5978 	}
5979 
5980 	if (wpa_s->p2p_in_provisioning) {
5981 		/*
5982 		 * Use the normal group formation timeout during the
5983 		 * provisioning phase to avoid terminating this process too
5984 		 * early due to group idle timeout.
5985 		 */
5986 		wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5987 			   "during provisioning");
5988 		return;
5989 	}
5990 
5991 	if (wpa_s->show_group_started) {
5992 		/*
5993 		 * Use the normal group formation timeout between the end of
5994 		 * the provisioning phase and completion of 4-way handshake to
5995 		 * avoid terminating this process too early due to group idle
5996 		 * timeout.
5997 		 */
5998 		wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5999 			   "while waiting for initial 4-way handshake to "
6000 			   "complete");
6001 		return;
6002 	}
6003 
6004 	wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6005 		   timeout);
6006 	eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6007 			       wpa_s, NULL);
6008 }
6009 
6010 
6011 /* Returns 1 if the interface was removed */
6012 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6013 			  u16 reason_code, const u8 *ie, size_t ie_len,
6014 			  int locally_generated)
6015 {
6016 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6017 		return 0;
6018 
6019 	if (!locally_generated)
6020 		p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6021 				 ie_len);
6022 
6023 	if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6024 	    wpa_s->current_ssid &&
6025 	    wpa_s->current_ssid->p2p_group &&
6026 	    wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6027 		wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6028 			   "session is ending");
6029 		if (wpas_p2p_group_delete(wpa_s,
6030 					  P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6031 		    > 0)
6032 			return 1;
6033 	}
6034 
6035 	return 0;
6036 }
6037 
6038 
6039 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6040 			     u16 reason_code, const u8 *ie, size_t ie_len,
6041 			     int locally_generated)
6042 {
6043 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6044 		return;
6045 
6046 	if (!locally_generated)
6047 		p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6048 				   ie_len);
6049 }
6050 
6051 
6052 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6053 {
6054 	struct p2p_data *p2p = wpa_s->global->p2p;
6055 
6056 	if (p2p == NULL)
6057 		return;
6058 
6059 	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6060 		return;
6061 
6062 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6063 		p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6064 
6065 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6066 		p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6067 
6068 	if (wpa_s->wps &&
6069 	    (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6070 		p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6071 
6072 	if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6073 		p2p_set_uuid(p2p, wpa_s->wps->uuid);
6074 
6075 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6076 		p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6077 		p2p_set_model_name(p2p, wpa_s->conf->model_name);
6078 		p2p_set_model_number(p2p, wpa_s->conf->model_number);
6079 		p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6080 	}
6081 
6082 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6083 		p2p_set_sec_dev_types(p2p,
6084 				      (void *) wpa_s->conf->sec_device_type,
6085 				      wpa_s->conf->num_sec_device_types);
6086 
6087 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6088 		int i;
6089 		p2p_remove_wps_vendor_extensions(p2p);
6090 		for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6091 			if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6092 				continue;
6093 			p2p_add_wps_vendor_extension(
6094 				p2p, wpa_s->conf->wps_vendor_ext[i]);
6095 		}
6096 	}
6097 
6098 	if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6099 	    wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6100 		char country[3];
6101 		country[0] = wpa_s->conf->country[0];
6102 		country[1] = wpa_s->conf->country[1];
6103 		country[2] = 0x04;
6104 		p2p_set_country(p2p, country);
6105 	}
6106 
6107 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6108 		p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6109 				     wpa_s->conf->p2p_ssid_postfix ?
6110 				     os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6111 				     0);
6112 	}
6113 
6114 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6115 		p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6116 
6117 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6118 		u8 reg_class, channel;
6119 		int ret;
6120 		unsigned int r;
6121 		if (wpa_s->conf->p2p_listen_reg_class &&
6122 		    wpa_s->conf->p2p_listen_channel) {
6123 			reg_class = wpa_s->conf->p2p_listen_reg_class;
6124 			channel = wpa_s->conf->p2p_listen_channel;
6125 		} else {
6126 			reg_class = 81;
6127 			/*
6128 			 * Pick one of the social channels randomly as the
6129 			 * listen channel.
6130 			 */
6131 			os_get_random((u8 *) &r, sizeof(r));
6132 			channel = 1 + (r % 3) * 5;
6133 		}
6134 		ret = p2p_set_listen_channel(p2p, reg_class, channel);
6135 		if (ret)
6136 			wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6137 				   "failed: %d", ret);
6138 	}
6139 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6140 		u8 op_reg_class, op_channel, cfg_op_channel;
6141 		int ret = 0;
6142 		unsigned int r;
6143 		if (wpa_s->conf->p2p_oper_reg_class &&
6144 		    wpa_s->conf->p2p_oper_channel) {
6145 			op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6146 			op_channel = wpa_s->conf->p2p_oper_channel;
6147 			cfg_op_channel = 1;
6148 		} else {
6149 			op_reg_class = 81;
6150 			/*
6151 			 * Use random operation channel from (1, 6, 11)
6152 			 *if no other preference is indicated.
6153 			 */
6154 			os_get_random((u8 *) &r, sizeof(r));
6155 			op_channel = 1 + (r % 3) * 5;
6156 			cfg_op_channel = 0;
6157 		}
6158 		ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6159 					   cfg_op_channel);
6160 		if (ret)
6161 			wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6162 				   "failed: %d", ret);
6163 	}
6164 
6165 	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6166 		if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6167 				      wpa_s->conf->p2p_pref_chan) < 0) {
6168 			wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6169 				   "update failed");
6170 		}
6171 
6172 		if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6173 			wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6174 				   "update failed");
6175 		}
6176 	}
6177 }
6178 
6179 
6180 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6181 		     int duration)
6182 {
6183 	if (!wpa_s->ap_iface)
6184 		return -1;
6185 	return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6186 				   duration);
6187 }
6188 
6189 
6190 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6191 {
6192 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6193 		return -1;
6194 
6195 	wpa_s->global->cross_connection = enabled;
6196 	p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6197 
6198 	if (!enabled) {
6199 		struct wpa_supplicant *iface;
6200 
6201 		for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6202 		{
6203 			if (iface->cross_connect_enabled == 0)
6204 				continue;
6205 
6206 			iface->cross_connect_enabled = 0;
6207 			iface->cross_connect_in_use = 0;
6208 			wpa_msg_global(iface->parent, MSG_INFO,
6209 				       P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6210 				       iface->ifname,
6211 				       iface->cross_connect_uplink);
6212 		}
6213 	}
6214 
6215 	return 0;
6216 }
6217 
6218 
6219 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6220 {
6221 	struct wpa_supplicant *iface;
6222 
6223 	if (!uplink->global->cross_connection)
6224 		return;
6225 
6226 	for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6227 		if (!iface->cross_connect_enabled)
6228 			continue;
6229 		if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6230 		    0)
6231 			continue;
6232 		if (iface->ap_iface == NULL)
6233 			continue;
6234 		if (iface->cross_connect_in_use)
6235 			continue;
6236 
6237 		iface->cross_connect_in_use = 1;
6238 		wpa_msg_global(iface->parent, MSG_INFO,
6239 			       P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6240 			       iface->ifname, iface->cross_connect_uplink);
6241 	}
6242 }
6243 
6244 
6245 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6246 {
6247 	struct wpa_supplicant *iface;
6248 
6249 	for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6250 		if (!iface->cross_connect_enabled)
6251 			continue;
6252 		if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6253 		    0)
6254 			continue;
6255 		if (!iface->cross_connect_in_use)
6256 			continue;
6257 
6258 		wpa_msg_global(iface->parent, MSG_INFO,
6259 			       P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6260 			       iface->ifname, iface->cross_connect_uplink);
6261 		iface->cross_connect_in_use = 0;
6262 	}
6263 }
6264 
6265 
6266 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6267 {
6268 	if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6269 	    wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6270 	    wpa_s->cross_connect_disallowed)
6271 		wpas_p2p_disable_cross_connect(wpa_s);
6272 	else
6273 		wpas_p2p_enable_cross_connect(wpa_s);
6274 	if (!wpa_s->ap_iface &&
6275 	    eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6276 		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6277 }
6278 
6279 
6280 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6281 {
6282 	wpas_p2p_disable_cross_connect(wpa_s);
6283 	if (!wpa_s->ap_iface &&
6284 	    !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6285 					 wpa_s, NULL))
6286 		wpas_p2p_set_group_idle_timeout(wpa_s);
6287 }
6288 
6289 
6290 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6291 {
6292 	struct wpa_supplicant *iface;
6293 
6294 	if (!wpa_s->global->cross_connection)
6295 		return;
6296 
6297 	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6298 		if (iface == wpa_s)
6299 			continue;
6300 		if (iface->drv_flags &
6301 		    WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6302 			continue;
6303 		if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6304 			continue;
6305 
6306 		wpa_s->cross_connect_enabled = 1;
6307 		os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6308 			   sizeof(wpa_s->cross_connect_uplink));
6309 		wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6310 			   "%s to %s whenever uplink is available",
6311 			   wpa_s->ifname, wpa_s->cross_connect_uplink);
6312 
6313 		if (iface->ap_iface || iface->current_ssid == NULL ||
6314 		    iface->current_ssid->mode != WPAS_MODE_INFRA ||
6315 		    iface->cross_connect_disallowed ||
6316 		    iface->wpa_state != WPA_COMPLETED)
6317 			break;
6318 
6319 		wpa_s->cross_connect_in_use = 1;
6320 		wpa_msg_global(wpa_s->parent, MSG_INFO,
6321 			       P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6322 			       wpa_s->ifname, wpa_s->cross_connect_uplink);
6323 		break;
6324 	}
6325 }
6326 
6327 
6328 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6329 {
6330 	if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6331 	    !wpa_s->p2p_in_provisioning)
6332 		return 0; /* not P2P client operation */
6333 
6334 	wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6335 		   "session overlap");
6336 	if (wpa_s != wpa_s->parent)
6337 		wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
6338 	wpas_p2p_group_formation_failed(wpa_s);
6339 	return 1;
6340 }
6341 
6342 
6343 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6344 {
6345 	struct p2p_channels chan, cli_chan;
6346 
6347 	if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6348 		return;
6349 
6350 	os_memset(&chan, 0, sizeof(chan));
6351 	os_memset(&cli_chan, 0, sizeof(cli_chan));
6352 	if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
6353 		wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6354 			   "channel list");
6355 		return;
6356 	}
6357 
6358 	p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
6359 }
6360 
6361 
6362 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6363 				     struct wpa_scan_results *scan_res)
6364 {
6365 	wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6366 }
6367 
6368 
6369 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6370 {
6371 	struct wpa_global *global = wpa_s->global;
6372 	int found = 0;
6373 	const u8 *peer;
6374 
6375 	if (global->p2p == NULL)
6376 		return -1;
6377 
6378 	wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6379 
6380 	if (wpa_s->pending_interface_name[0] &&
6381 	    !is_zero_ether_addr(wpa_s->pending_interface_addr))
6382 		found = 1;
6383 
6384 	peer = p2p_get_go_neg_peer(global->p2p);
6385 	if (peer) {
6386 		wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6387 			   MACSTR, MAC2STR(peer));
6388 		p2p_unauthorize(global->p2p, peer);
6389 		found = 1;
6390 	}
6391 
6392 	if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6393 		wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6394 		wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6395 		found = 1;
6396 	}
6397 
6398 	if (wpa_s->pending_pd_before_join) {
6399 		wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6400 		wpa_s->pending_pd_before_join = 0;
6401 		found = 1;
6402 	}
6403 
6404 	wpas_p2p_stop_find(wpa_s);
6405 
6406 	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6407 		if (wpa_s == global->p2p_group_formation &&
6408 		    (wpa_s->p2p_in_provisioning ||
6409 		     wpa_s->parent->pending_interface_type ==
6410 		     WPA_IF_P2P_CLIENT)) {
6411 			wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6412 				   "formation found - cancelling",
6413 				   wpa_s->ifname);
6414 			found = 1;
6415 			eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6416 					     wpa_s->parent, NULL);
6417 			if (wpa_s->p2p_in_provisioning) {
6418 				wpas_group_formation_completed(wpa_s, 0);
6419 				break;
6420 			}
6421 			wpas_p2p_group_delete(wpa_s,
6422 					      P2P_GROUP_REMOVAL_REQUESTED);
6423 			break;
6424 		}
6425 	}
6426 
6427 	if (!found) {
6428 		wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6429 		return -1;
6430 	}
6431 
6432 	return 0;
6433 }
6434 
6435 
6436 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6437 {
6438 	if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6439 		return;
6440 
6441 	wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6442 		   "being available anymore");
6443 	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
6444 }
6445 
6446 
6447 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6448 				   int freq_24, int freq_5, int freq_overall)
6449 {
6450 	struct p2p_data *p2p = wpa_s->global->p2p;
6451 	if (p2p == NULL)
6452 		return;
6453 	p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6454 }
6455 
6456 
6457 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6458 {
6459 	u8 peer[ETH_ALEN];
6460 	struct p2p_data *p2p = wpa_s->global->p2p;
6461 
6462 	if (p2p == NULL)
6463 		return -1;
6464 
6465 	if (hwaddr_aton(addr, peer))
6466 		return -1;
6467 
6468 	return p2p_unauthorize(p2p, peer);
6469 }
6470 
6471 
6472 /**
6473  * wpas_p2p_disconnect - Disconnect from a P2P Group
6474  * @wpa_s: Pointer to wpa_supplicant data
6475  * Returns: 0 on success, -1 on failure
6476  *
6477  * This can be used to disconnect from a group in which the local end is a P2P
6478  * Client or to end a P2P Group in case the local end is the Group Owner. If a
6479  * virtual network interface was created for this group, that interface will be
6480  * removed. Otherwise, only the configured P2P group network will be removed
6481  * from the interface.
6482  */
6483 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6484 {
6485 
6486 	if (wpa_s == NULL)
6487 		return -1;
6488 
6489 	return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6490 		-1 : 0;
6491 }
6492 
6493 
6494 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6495 {
6496 	int ret;
6497 
6498 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6499 		return 0;
6500 
6501 	ret = p2p_in_progress(wpa_s->global->p2p);
6502 	if (ret == 0) {
6503 		/*
6504 		 * Check whether there is an ongoing WPS provisioning step (or
6505 		 * other parts of group formation) on another interface since
6506 		 * p2p_in_progress() does not report this to avoid issues for
6507 		 * scans during such provisioning step.
6508 		 */
6509 		if (wpa_s->global->p2p_group_formation &&
6510 		    wpa_s->global->p2p_group_formation != wpa_s) {
6511 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6512 				"in group formation",
6513 				wpa_s->global->p2p_group_formation->ifname);
6514 			ret = 1;
6515 		}
6516 	}
6517 
6518 	if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6519 		struct os_reltime now;
6520 		os_get_reltime(&now);
6521 		if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6522 				       P2P_MAX_INITIAL_CONN_WAIT_GO)) {
6523 			/* Wait for the first client has expired */
6524 			wpa_s->global->p2p_go_wait_client.sec = 0;
6525 		} else {
6526 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6527 			ret = 1;
6528 		}
6529 	}
6530 
6531 	return ret;
6532 }
6533 
6534 
6535 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6536 			      struct wpa_ssid *ssid)
6537 {
6538 	if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6539 	    eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6540 				 wpa_s->parent, NULL) > 0) {
6541 		/**
6542 		 * Remove the network by scheduling the group formation
6543 		 * timeout to happen immediately. The teardown code
6544 		 * needs to be scheduled to run asynch later so that we
6545 		 * don't delete data from under ourselves unexpectedly.
6546 		 * Calling wpas_p2p_group_formation_timeout directly
6547 		 * causes a series of crashes in WPS failure scenarios.
6548 		 */
6549 		wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6550 			   "P2P group network getting removed");
6551 		eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6552 				       wpa_s->parent, NULL);
6553 	}
6554 }
6555 
6556 
6557 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
6558 					  const u8 *addr, const u8 *ssid,
6559 					  size_t ssid_len)
6560 {
6561 	struct wpa_ssid *s;
6562 	size_t i;
6563 
6564 	for (s = wpa_s->conf->ssid; s; s = s->next) {
6565 		if (s->disabled != 2)
6566 			continue;
6567 		if (ssid &&
6568 		    (ssid_len != s->ssid_len ||
6569 		     os_memcmp(ssid, s->ssid, ssid_len) != 0))
6570 			continue;
6571 		if (addr == NULL) {
6572 			if (s->mode == WPAS_MODE_P2P_GO)
6573 				return s;
6574 			continue;
6575 		}
6576 		if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6577 			return s; /* peer is GO in the persistent group */
6578 		if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6579 			continue;
6580 		for (i = 0; i < s->num_p2p_clients; i++) {
6581 			if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6582 				      addr, ETH_ALEN) == 0)
6583 				return s; /* peer is P2P client in persistent
6584 					   * group */
6585 		}
6586 	}
6587 
6588 	return NULL;
6589 }
6590 
6591 
6592 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6593 				       const u8 *addr)
6594 {
6595 	if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6596 				 wpa_s->parent, NULL) > 0) {
6597 		/*
6598 		 * This can happen if WPS provisioning step is not terminated
6599 		 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6600 		 * peer was able to connect, there is no need to time out group
6601 		 * formation after this, though. In addition, this is used with
6602 		 * the initial connection wait on the GO as a separate formation
6603 		 * timeout and as such, expected to be hit after the initial WPS
6604 		 * provisioning step.
6605 		 */
6606 		wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6607 	}
6608 	if (!wpa_s->p2p_go_group_formation_completed) {
6609 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6610 		wpa_s->p2p_go_group_formation_completed = 1;
6611 		wpa_s->global->p2p_group_formation = NULL;
6612 		wpa_s->p2p_in_provisioning = 0;
6613 	}
6614 	wpa_s->global->p2p_go_wait_client.sec = 0;
6615 	if (addr == NULL)
6616 		return;
6617 	wpas_p2p_add_persistent_group_client(wpa_s, addr);
6618 }
6619 
6620 
6621 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6622 					int group_added)
6623 {
6624 	struct wpa_supplicant *group = wpa_s;
6625 	if (wpa_s->global->p2p_group_formation)
6626 		group = wpa_s->global->p2p_group_formation;
6627 	wpa_s = wpa_s->parent;
6628 	offchannel_send_action_done(wpa_s);
6629 	if (group_added)
6630 		wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
6631 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6632 	wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6633 			 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6634 			 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6635 			 wpa_s->p2p_persistent_id,
6636 			 wpa_s->p2p_pd_before_go_neg,
6637 			 wpa_s->p2p_go_ht40,
6638 			 wpa_s->p2p_go_vht);
6639 }
6640 
6641 
6642 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6643 {
6644 	if (!wpa_s->p2p_fallback_to_go_neg ||
6645 	    wpa_s->p2p_in_provisioning <= 5)
6646 		return 0;
6647 
6648 	if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6649 		return 0; /* peer operating as a GO */
6650 
6651 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6652 		"fallback to GO Negotiation");
6653 	wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6654 
6655 	return 1;
6656 }
6657 
6658 
6659 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6660 {
6661 	struct wpa_supplicant *ifs;
6662 
6663 	if (wpa_s->wpa_state > WPA_SCANNING) {
6664 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6665 			"concurrent operation",
6666 			P2P_CONCURRENT_SEARCH_DELAY);
6667 		return P2P_CONCURRENT_SEARCH_DELAY;
6668 	}
6669 
6670 	dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6671 			 radio_list) {
6672 		if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
6673 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6674 				"delay due to concurrent operation on "
6675 				"interface %s",
6676 				P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6677 			return P2P_CONCURRENT_SEARCH_DELAY;
6678 		}
6679 	}
6680 
6681 	return 0;
6682 }
6683 
6684 
6685 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6686 				     struct wpa_ssid *s, const u8 *addr,
6687 				     int iface_addr)
6688 {
6689 	struct psk_list_entry *psk, *tmp;
6690 	int changed = 0;
6691 
6692 	dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6693 			      list) {
6694 		if ((iface_addr && !psk->p2p &&
6695 		     os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6696 		    (!iface_addr && psk->p2p &&
6697 		     os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6698 			wpa_dbg(wpa_s, MSG_DEBUG,
6699 				"P2P: Remove persistent group PSK list entry for "
6700 				MACSTR " p2p=%u",
6701 				MAC2STR(psk->addr), psk->p2p);
6702 			dl_list_del(&psk->list);
6703 			os_free(psk);
6704 			changed++;
6705 		}
6706 	}
6707 
6708 	return changed;
6709 }
6710 
6711 
6712 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6713 			 const u8 *p2p_dev_addr,
6714 			 const u8 *psk, size_t psk_len)
6715 {
6716 	struct wpa_ssid *ssid = wpa_s->current_ssid;
6717 	struct wpa_ssid *persistent;
6718 	struct psk_list_entry *p;
6719 
6720 	if (psk_len != sizeof(p->psk))
6721 		return;
6722 
6723 	if (p2p_dev_addr) {
6724 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6725 			" p2p_dev_addr=" MACSTR,
6726 			MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6727 		if (is_zero_ether_addr(p2p_dev_addr))
6728 			p2p_dev_addr = NULL;
6729 	} else {
6730 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6731 			MAC2STR(mac_addr));
6732 	}
6733 
6734 	if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6735 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6736 		/* To be added to persistent group once created */
6737 		if (wpa_s->global->add_psk == NULL) {
6738 			wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6739 			if (wpa_s->global->add_psk == NULL)
6740 				return;
6741 		}
6742 		p = wpa_s->global->add_psk;
6743 		if (p2p_dev_addr) {
6744 			p->p2p = 1;
6745 			os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6746 		} else {
6747 			p->p2p = 0;
6748 			os_memcpy(p->addr, mac_addr, ETH_ALEN);
6749 		}
6750 		os_memcpy(p->psk, psk, psk_len);
6751 		return;
6752 	}
6753 
6754 	if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6755 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6756 		return;
6757 	}
6758 
6759 	persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6760 					     ssid->ssid_len);
6761 	if (!persistent) {
6762 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6763 		return;
6764 	}
6765 
6766 	p = os_zalloc(sizeof(*p));
6767 	if (p == NULL)
6768 		return;
6769 	if (p2p_dev_addr) {
6770 		p->p2p = 1;
6771 		os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6772 	} else {
6773 		p->p2p = 0;
6774 		os_memcpy(p->addr, mac_addr, ETH_ALEN);
6775 	}
6776 	os_memcpy(p->psk, psk, psk_len);
6777 
6778 	if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6779 		struct psk_list_entry *last;
6780 		last = dl_list_last(&persistent->psk_list,
6781 				    struct psk_list_entry, list);
6782 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6783 			MACSTR " (p2p=%u) to make room for a new one",
6784 			MAC2STR(last->addr), last->p2p);
6785 		dl_list_del(&last->list);
6786 		os_free(last);
6787 	}
6788 
6789 	wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6790 				  p2p_dev_addr ? p2p_dev_addr : mac_addr,
6791 				  p2p_dev_addr == NULL);
6792 	if (p2p_dev_addr) {
6793 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6794 			MACSTR, MAC2STR(p2p_dev_addr));
6795 	} else {
6796 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6797 			MAC2STR(mac_addr));
6798 	}
6799 	dl_list_add(&persistent->psk_list, &p->list);
6800 
6801 #ifndef CONFIG_NO_CONFIG_WRITE
6802 	if (wpa_s->parent->conf->update_config &&
6803 	    wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6804 		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6805 #endif /* CONFIG_NO_CONFIG_WRITE */
6806 }
6807 
6808 
6809 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6810 				struct wpa_ssid *s, const u8 *addr,
6811 				int iface_addr)
6812 {
6813 	int res;
6814 
6815 	res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6816 	if (res > 0) {
6817 #ifndef CONFIG_NO_CONFIG_WRITE
6818 		if (wpa_s->conf->update_config &&
6819 		    wpa_config_write(wpa_s->confname, wpa_s->conf))
6820 			wpa_dbg(wpa_s, MSG_DEBUG,
6821 				"P2P: Failed to update configuration");
6822 #endif /* CONFIG_NO_CONFIG_WRITE */
6823 	}
6824 }
6825 
6826 
6827 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6828 				      const u8 *peer, int iface_addr)
6829 {
6830 	struct hostapd_data *hapd;
6831 	struct hostapd_wpa_psk *psk, *prev, *rem;
6832 	struct sta_info *sta;
6833 
6834 	if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6835 	    wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6836 		return;
6837 
6838 	/* Remove per-station PSK entry */
6839 	hapd = wpa_s->ap_iface->bss[0];
6840 	prev = NULL;
6841 	psk = hapd->conf->ssid.wpa_psk;
6842 	while (psk) {
6843 		if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6844 		    (!iface_addr &&
6845 		     os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6846 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6847 				MACSTR " iface_addr=%d",
6848 				MAC2STR(peer), iface_addr);
6849 			if (prev)
6850 				prev->next = psk->next;
6851 			else
6852 				hapd->conf->ssid.wpa_psk = psk->next;
6853 			rem = psk;
6854 			psk = psk->next;
6855 			os_free(rem);
6856 		} else {
6857 			prev = psk;
6858 			psk = psk->next;
6859 		}
6860 	}
6861 
6862 	/* Disconnect from group */
6863 	if (iface_addr)
6864 		sta = ap_get_sta(hapd, peer);
6865 	else
6866 		sta = ap_get_sta_p2p(hapd, peer);
6867 	if (sta) {
6868 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6869 			" (iface_addr=%d) from group",
6870 			MAC2STR(peer), iface_addr);
6871 		hostapd_drv_sta_deauth(hapd, sta->addr,
6872 				       WLAN_REASON_DEAUTH_LEAVING);
6873 		ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6874 	}
6875 }
6876 
6877 
6878 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6879 			    int iface_addr)
6880 {
6881 	struct wpa_ssid *s;
6882 	struct wpa_supplicant *w;
6883 
6884 	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6885 
6886 	/* Remove from any persistent group */
6887 	for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6888 		if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6889 			continue;
6890 		if (!iface_addr)
6891 			wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6892 		wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6893 	}
6894 
6895 	/* Remove from any operating group */
6896 	for (w = wpa_s->global->ifaces; w; w = w->next)
6897 		wpas_p2p_remove_client_go(w, peer, iface_addr);
6898 }
6899 
6900 
6901 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6902 {
6903 	struct wpa_supplicant *wpa_s = eloop_ctx;
6904 	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6905 }
6906 
6907 
6908 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6909 {
6910 	struct wpa_supplicant *wpa_s = eloop_ctx;
6911 
6912 	wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6913 	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6914 }
6915 
6916 
6917 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6918 					struct wpa_ssid *ssid)
6919 {
6920 	struct wpa_supplicant *iface;
6921 
6922 	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6923 		if (!iface->current_ssid ||
6924 		    iface->current_ssid->frequency == freq ||
6925 		    (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
6926 		     !iface->current_ssid->p2p_group))
6927 			continue;
6928 
6929 		/* Remove the connection with least priority */
6930 		if (!wpas_is_p2p_prioritized(iface)) {
6931 			/* STA connection has priority over existing
6932 			 * P2P connection, so remove the interface. */
6933 			wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6934 			eloop_register_timeout(0, 0,
6935 					       wpas_p2p_group_freq_conflict,
6936 					       iface, NULL);
6937 			/* If connection in progress is P2P connection, do not
6938 			 * proceed for the connection. */
6939 			if (wpa_s == iface)
6940 				return -1;
6941 			else
6942 				return 0;
6943 		} else {
6944 			/* P2P connection has priority, disable the STA network
6945 			 */
6946 			wpa_supplicant_disable_network(wpa_s->global->ifaces,
6947 						       ssid);
6948 			wpa_msg(wpa_s->global->ifaces, MSG_INFO,
6949 				WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
6950 			os_memset(wpa_s->global->ifaces->pending_bssid, 0,
6951 				  ETH_ALEN);
6952 			/* If P2P connection is in progress, continue
6953 			 * connecting...*/
6954 			if (wpa_s == iface)
6955 				return 0;
6956 			else
6957 				return -1;
6958 		}
6959 	}
6960 
6961 	return 0;
6962 }
6963 
6964 
6965 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6966 {
6967 	struct wpa_ssid *ssid = wpa_s->current_ssid;
6968 
6969 	if (ssid == NULL || !ssid->p2p_group)
6970 		return 0;
6971 
6972 	if (wpa_s->p2p_last_4way_hs_fail &&
6973 	    wpa_s->p2p_last_4way_hs_fail == ssid) {
6974 		u8 go_dev_addr[ETH_ALEN];
6975 		struct wpa_ssid *persistent;
6976 
6977 		if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6978 					      ssid->ssid,
6979 					      ssid->ssid_len) <= 0) {
6980 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6981 			goto disconnect;
6982 		}
6983 
6984 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6985 			MACSTR, MAC2STR(go_dev_addr));
6986 		persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6987 						     ssid->ssid,
6988 						     ssid->ssid_len);
6989 		if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6990 			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6991 			goto disconnect;
6992 		}
6993 		wpa_msg_global(wpa_s->parent, MSG_INFO,
6994 			       P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6995 			       persistent->id);
6996 	disconnect:
6997 		wpa_s->p2p_last_4way_hs_fail = NULL;
6998 		/*
6999 		 * Remove the group from a timeout to avoid issues with caller
7000 		 * continuing to use the interface if this is on a P2P group
7001 		 * interface.
7002 		 */
7003 		eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7004 				       wpa_s, NULL);
7005 		return 1;
7006 	}
7007 
7008 	wpa_s->p2p_last_4way_hs_fail = ssid;
7009 	return 0;
7010 }
7011 
7012 
7013 #ifdef CONFIG_WPS_NFC
7014 
7015 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7016 					     struct wpabuf *p2p)
7017 {
7018 	struct wpabuf *ret;
7019 	size_t wsc_len;
7020 
7021 	if (p2p == NULL) {
7022 		wpabuf_free(wsc);
7023 		wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7024 		return NULL;
7025 	}
7026 
7027 	wsc_len = wsc ? wpabuf_len(wsc) : 0;
7028 	ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7029 	if (ret == NULL) {
7030 		wpabuf_free(wsc);
7031 		wpabuf_free(p2p);
7032 		return NULL;
7033 	}
7034 
7035 	wpabuf_put_be16(ret, wsc_len);
7036 	if (wsc)
7037 		wpabuf_put_buf(ret, wsc);
7038 	wpabuf_put_be16(ret, wpabuf_len(p2p));
7039 	wpabuf_put_buf(ret, p2p);
7040 
7041 	wpabuf_free(wsc);
7042 	wpabuf_free(p2p);
7043 	wpa_hexdump_buf(MSG_DEBUG,
7044 			"P2P: Generated NFC connection handover message", ret);
7045 
7046 	if (ndef && ret) {
7047 		struct wpabuf *tmp;
7048 		tmp = ndef_build_p2p(ret);
7049 		wpabuf_free(ret);
7050 		if (tmp == NULL) {
7051 			wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7052 			return NULL;
7053 		}
7054 		ret = tmp;
7055 	}
7056 
7057 	return ret;
7058 }
7059 
7060 
7061 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7062 			     struct wpa_ssid **ssid, u8 *go_dev_addr)
7063 {
7064 	struct wpa_supplicant *iface;
7065 
7066 	if (go_dev_addr)
7067 		os_memset(go_dev_addr, 0, ETH_ALEN);
7068 	if (ssid)
7069 		*ssid = NULL;
7070 	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7071 		if (iface->wpa_state < WPA_ASSOCIATING ||
7072 		    iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7073 		    !iface->current_ssid->p2p_group ||
7074 		    iface->current_ssid->mode != WPAS_MODE_INFRA)
7075 			continue;
7076 		if (ssid)
7077 			*ssid = iface->current_ssid;
7078 		if (go_dev_addr)
7079 			os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7080 		return iface->assoc_freq;
7081 	}
7082 	return 0;
7083 }
7084 
7085 
7086 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7087 					  int ndef)
7088 {
7089 	struct wpabuf *wsc, *p2p;
7090 	struct wpa_ssid *ssid;
7091 	u8 go_dev_addr[ETH_ALEN];
7092 	int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7093 
7094 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7095 		wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7096 		return NULL;
7097 	}
7098 
7099 	if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7100 	    wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7101 			   &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7102 		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7103 		return NULL;
7104 	}
7105 
7106 	if (cli_freq == 0) {
7107 		wsc = wps_build_nfc_handover_req_p2p(
7108 			wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7109 	} else
7110 		wsc = NULL;
7111 	p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7112 					 go_dev_addr, ssid ? ssid->ssid : NULL,
7113 					 ssid ? ssid->ssid_len : 0);
7114 
7115 	return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7116 }
7117 
7118 
7119 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7120 					  int ndef, int tag)
7121 {
7122 	struct wpabuf *wsc, *p2p;
7123 	struct wpa_ssid *ssid;
7124 	u8 go_dev_addr[ETH_ALEN];
7125 	int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7126 
7127 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7128 		return NULL;
7129 
7130 	if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7131 	    wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7132 			   &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7133 		return NULL;
7134 
7135 	if (cli_freq == 0) {
7136 		wsc = wps_build_nfc_handover_sel_p2p(
7137 			wpa_s->parent->wps,
7138 			tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7139 			DEV_PW_NFC_CONNECTION_HANDOVER,
7140 			wpa_s->conf->wps_nfc_dh_pubkey,
7141 			tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7142 	} else
7143 		wsc = NULL;
7144 	p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7145 					 go_dev_addr, ssid ? ssid->ssid : NULL,
7146 					 ssid ? ssid->ssid_len : 0);
7147 
7148 	return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7149 }
7150 
7151 
7152 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7153 				   struct p2p_nfc_params *params)
7154 {
7155 	wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7156 		   "connection handover (freq=%d)",
7157 		   params->go_freq);
7158 
7159 	if (params->go_freq && params->go_ssid_len) {
7160 		wpa_s->p2p_wps_method = WPS_NFC;
7161 		wpa_s->pending_join_wps_method = WPS_NFC;
7162 		os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7163 		os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7164 			  ETH_ALEN);
7165 		return wpas_p2p_join_start(wpa_s, params->go_freq,
7166 					   params->go_ssid,
7167 					   params->go_ssid_len);
7168 	}
7169 
7170 	return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7171 				WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7172 				params->go_freq, -1, 0, 1, 1);
7173 }
7174 
7175 
7176 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7177 				  struct p2p_nfc_params *params, int tag)
7178 {
7179 	int res, persistent;
7180 	struct wpa_ssid *ssid;
7181 
7182 	wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7183 		   "connection handover");
7184 	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7185 		ssid = wpa_s->current_ssid;
7186 		if (ssid == NULL)
7187 			continue;
7188 		if (ssid->mode != WPAS_MODE_P2P_GO)
7189 			continue;
7190 		if (wpa_s->ap_iface == NULL)
7191 			continue;
7192 		break;
7193 	}
7194 	if (wpa_s == NULL) {
7195 		wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7196 		return -1;
7197 	}
7198 
7199 	if (wpa_s->parent->p2p_oob_dev_pw_id !=
7200 	    DEV_PW_NFC_CONNECTION_HANDOVER &&
7201 	    !wpa_s->parent->p2p_oob_dev_pw) {
7202 		wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7203 		return -1;
7204 	}
7205 	res = wpas_ap_wps_add_nfc_pw(
7206 		wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7207 		wpa_s->parent->p2p_oob_dev_pw,
7208 		wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7209 		wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7210 	if (res)
7211 		return res;
7212 
7213 	if (!tag) {
7214 		wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7215 		return 0;
7216 	}
7217 
7218 	if (!params->peer ||
7219 	    !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7220 		return 0;
7221 
7222 	wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7223 		   " to join", MAC2STR(params->peer->p2p_device_addr));
7224 
7225 	wpa_s->global->p2p_invite_group = wpa_s;
7226 	persistent = ssid->p2p_persistent_group &&
7227 		wpas_p2p_get_persistent(wpa_s->parent,
7228 					params->peer->p2p_device_addr,
7229 					ssid->ssid, ssid->ssid_len);
7230 	wpa_s->parent->pending_invite_ssid_id = -1;
7231 
7232 	return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7233 			  P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7234 			  ssid->ssid, ssid->ssid_len, ssid->frequency,
7235 			  wpa_s->global->p2p_dev_addr, persistent, 0,
7236 			  wpa_s->parent->p2p_oob_dev_pw_id);
7237 }
7238 
7239 
7240 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7241 				    struct p2p_nfc_params *params,
7242 				    int forced_freq)
7243 {
7244 	wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7245 		   "connection handover");
7246 	return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7247 				WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7248 				forced_freq, -1, 0, 1, 1);
7249 }
7250 
7251 
7252 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7253 				    struct p2p_nfc_params *params,
7254 				    int forced_freq)
7255 {
7256 	int res;
7257 
7258 	wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7259 		   "connection handover");
7260 	res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7261 			       WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7262 			       forced_freq, -1, 0, 1, 1);
7263 	if (res)
7264 		return res;
7265 
7266 	res = wpas_p2p_listen(wpa_s, 60);
7267 	if (res) {
7268 		p2p_unauthorize(wpa_s->global->p2p,
7269 				params->peer->p2p_device_addr);
7270 	}
7271 
7272 	return res;
7273 }
7274 
7275 
7276 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7277 					    const struct wpabuf *data,
7278 					    int sel, int tag, int forced_freq)
7279 {
7280 	const u8 *pos, *end;
7281 	u16 len, id;
7282 	struct p2p_nfc_params params;
7283 	int res;
7284 
7285 	os_memset(&params, 0, sizeof(params));
7286 	params.sel = sel;
7287 
7288 	wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7289 
7290 	pos = wpabuf_head(data);
7291 	end = pos + wpabuf_len(data);
7292 
7293 	if (end - pos < 2) {
7294 		wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7295 			   "attributes");
7296 		return -1;
7297 	}
7298 	len = WPA_GET_BE16(pos);
7299 	pos += 2;
7300 	if (pos + len > end) {
7301 		wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7302 			   "attributes");
7303 		return -1;
7304 	}
7305 	params.wsc_attr = pos;
7306 	params.wsc_len = len;
7307 	pos += len;
7308 
7309 	if (end - pos < 2) {
7310 		wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7311 			   "attributes");
7312 		return -1;
7313 	}
7314 	len = WPA_GET_BE16(pos);
7315 	pos += 2;
7316 	if (pos + len > end) {
7317 		wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7318 			   "attributes");
7319 		return -1;
7320 	}
7321 	params.p2p_attr = pos;
7322 	params.p2p_len = len;
7323 	pos += len;
7324 
7325 	wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7326 		    params.wsc_attr, params.wsc_len);
7327 	wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7328 		    params.p2p_attr, params.p2p_len);
7329 	if (pos < end) {
7330 		wpa_hexdump(MSG_DEBUG,
7331 			    "P2P: Ignored extra data after P2P attributes",
7332 			    pos, end - pos);
7333 	}
7334 
7335 	res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, &params);
7336 	if (res)
7337 		return res;
7338 
7339 	if (params.next_step == NO_ACTION)
7340 		return 0;
7341 
7342 	if (params.next_step == BOTH_GO) {
7343 		wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7344 			MAC2STR(params.peer->p2p_device_addr));
7345 		return 0;
7346 	}
7347 
7348 	if (params.next_step == PEER_CLIENT) {
7349 		if (!is_zero_ether_addr(params.go_dev_addr)) {
7350 			wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7351 				"peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7352 				" ssid=\"%s\"",
7353 				MAC2STR(params.peer->p2p_device_addr),
7354 				params.go_freq,
7355 				MAC2STR(params.go_dev_addr),
7356 				wpa_ssid_txt(params.go_ssid,
7357 					     params.go_ssid_len));
7358 		} else {
7359 			wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7360 				"peer=" MACSTR " freq=%d",
7361 				MAC2STR(params.peer->p2p_device_addr),
7362 				params.go_freq);
7363 		}
7364 		return 0;
7365 	}
7366 
7367 	if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7368 		wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7369 			MACSTR, MAC2STR(params.peer->p2p_device_addr));
7370 		return 0;
7371 	}
7372 
7373 	wpabuf_free(wpa_s->p2p_oob_dev_pw);
7374 	wpa_s->p2p_oob_dev_pw = NULL;
7375 
7376 	if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7377 		wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7378 			   "received");
7379 		return -1;
7380 	}
7381 
7382 	id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7383 	wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7384 	wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7385 		    params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7386 	os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7387 		  params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7388 	wpa_s->p2p_peer_oob_pk_hash_known = 1;
7389 
7390 	if (tag) {
7391 		if (id < 0x10) {
7392 			wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7393 				   "peer OOB Device Password Id %u", id);
7394 			return -1;
7395 		}
7396 		wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7397 			   "Device Password Id %u", id);
7398 		wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7399 				params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7400 				params.oob_dev_pw_len -
7401 				WPS_OOB_PUBKEY_HASH_LEN - 2);
7402 		wpa_s->p2p_oob_dev_pw_id = id;
7403 		wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7404 			params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7405 			params.oob_dev_pw_len -
7406 			WPS_OOB_PUBKEY_HASH_LEN - 2);
7407 		if (wpa_s->p2p_oob_dev_pw == NULL)
7408 			return -1;
7409 
7410 		if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7411 		    wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7412 				   &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7413 			return -1;
7414 	} else {
7415 		wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7416 			   "without Device Password");
7417 		wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7418 	}
7419 
7420 	switch (params.next_step) {
7421 	case NO_ACTION:
7422 	case BOTH_GO:
7423 	case PEER_CLIENT:
7424 		/* already covered above */
7425 		return 0;
7426 	case JOIN_GROUP:
7427 		return wpas_p2p_nfc_join_group(wpa_s, &params);
7428 	case AUTH_JOIN:
7429 		return wpas_p2p_nfc_auth_join(wpa_s, &params, tag);
7430 	case INIT_GO_NEG:
7431 		return wpas_p2p_nfc_init_go_neg(wpa_s, &params, forced_freq);
7432 	case RESP_GO_NEG:
7433 		/* TODO: use own OOB Dev Pw */
7434 		return wpas_p2p_nfc_resp_go_neg(wpa_s, &params, forced_freq);
7435 	}
7436 
7437 	return -1;
7438 }
7439 
7440 
7441 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7442 			     const struct wpabuf *data, int forced_freq)
7443 {
7444 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7445 		return -1;
7446 
7447 	return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7448 }
7449 
7450 
7451 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7452 				 const struct wpabuf *req,
7453 				 const struct wpabuf *sel, int forced_freq)
7454 {
7455 	struct wpabuf *tmp;
7456 	int ret;
7457 
7458 	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7459 		return -1;
7460 
7461 	wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7462 
7463 	wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7464 			  wpabuf_head(req), wpabuf_len(req));
7465 	wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7466 			  wpabuf_head(sel), wpabuf_len(sel));
7467 	if (forced_freq)
7468 		wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7469 	tmp = ndef_parse_p2p(init ? sel : req);
7470 	if (tmp == NULL) {
7471 		wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7472 		return -1;
7473 	}
7474 
7475 	ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7476 					       forced_freq);
7477 	wpabuf_free(tmp);
7478 
7479 	return ret;
7480 }
7481 
7482 
7483 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7484 {
7485 	const u8 *if_addr;
7486 	int go_intent = wpa_s->conf->p2p_go_intent;
7487 	struct wpa_supplicant *iface;
7488 
7489 	if (wpa_s->global->p2p == NULL)
7490 		return -1;
7491 
7492 	if (!enabled) {
7493 		wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7494 		for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7495 		{
7496 			if (!iface->ap_iface)
7497 				continue;
7498 			hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7499 		}
7500 		p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7501 						 0, NULL);
7502 		if (wpa_s->p2p_nfc_tag_enabled)
7503 			wpas_p2p_remove_pending_group_interface(wpa_s);
7504 		wpa_s->p2p_nfc_tag_enabled = 0;
7505 		return 0;
7506 	}
7507 
7508 	if (wpa_s->global->p2p_disabled)
7509 		return -1;
7510 
7511 	if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7512 	    wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7513 	    wpa_s->conf->wps_nfc_dev_pw == NULL ||
7514 	    wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7515 		wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7516 			   "to allow static handover cases");
7517 		return -1;
7518 	}
7519 
7520 	wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7521 
7522 	wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7523 	wpabuf_free(wpa_s->p2p_oob_dev_pw);
7524 	wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7525 	if (wpa_s->p2p_oob_dev_pw == NULL)
7526 		return -1;
7527 	wpa_s->p2p_peer_oob_pk_hash_known = 0;
7528 
7529 	wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7530 
7531 	if (wpa_s->create_p2p_iface) {
7532 		enum wpa_driver_if_type iftype;
7533 		/* Prepare to add a new interface for the group */
7534 		iftype = WPA_IF_P2P_GROUP;
7535 		if (go_intent == 15)
7536 			iftype = WPA_IF_P2P_GO;
7537 		if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7538 			wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7539 				   "interface for the group");
7540 			return -1;
7541 		}
7542 
7543 		if_addr = wpa_s->pending_interface_addr;
7544 	} else
7545 		if_addr = wpa_s->own_addr;
7546 
7547 	wpa_s->p2p_nfc_tag_enabled = enabled;
7548 
7549 	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7550 		struct hostapd_data *hapd;
7551 		if (iface->ap_iface == NULL)
7552 			continue;
7553 		hapd = iface->ap_iface->bss[0];
7554 		wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7555 		hapd->conf->wps_nfc_dh_pubkey =
7556 			wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7557 		wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7558 		hapd->conf->wps_nfc_dh_privkey =
7559 			wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7560 		wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7561 		hapd->conf->wps_nfc_dev_pw =
7562 			wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7563 		hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7564 
7565 		if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7566 			wpa_dbg(iface, MSG_DEBUG,
7567 				"P2P: Failed to enable NFC Tag for GO");
7568 		}
7569 	}
7570 	p2p_set_authorized_oob_dev_pw_id(
7571 		wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
7572 		if_addr);
7573 
7574 	return 0;
7575 }
7576 
7577 #endif /* CONFIG_WPS_NFC */
7578