p2p_supplicant.c revision 61d9df3e62aaa0e87ad05452fcb95142159a17b6
1/*
2 * wpa_supplicant - P2P
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eloop.h"
13#include "common/ieee802_11_common.h"
14#include "common/ieee802_11_defs.h"
15#include "common/wpa_ctrl.h"
16#include "wps/wps_i.h"
17#include "p2p/p2p.h"
18#include "ap/hostapd.h"
19#include "ap/ap_config.h"
20#include "ap/p2p_hostapd.h"
21#include "eapol_supp/eapol_supp_sm.h"
22#include "rsn_supp/wpa.h"
23#include "wpa_supplicant_i.h"
24#include "driver_i.h"
25#include "ap.h"
26#include "config_ssid.h"
27#include "config.h"
28#include "notify.h"
29#include "scan.h"
30#include "bss.h"
31#include "offchannel.h"
32#include "wps_supplicant.h"
33#include "p2p_supplicant.h"
34
35
36/*
37 * How many times to try to scan to find the GO before giving up on join
38 * request.
39 */
40#define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
41
42#define P2P_AUTO_PD_SCAN_ATTEMPTS 5
43
44#ifndef P2P_MAX_CLIENT_IDLE
45/*
46 * How many seconds to try to reconnect to the GO when connection in P2P client
47 * role has been lost.
48 */
49#ifdef ANDROID_P2P
50#define P2P_MAX_CLIENT_IDLE 20
51#else
52#define P2P_MAX_CLIENT_IDLE 10
53#endif /* ANDROID_P2P */
54#endif /* P2P_MAX_CLIENT_IDLE */
55
56#ifndef P2P_MAX_INITIAL_CONN_WAIT
57/*
58 * How many seconds to wait for initial 4-way handshake to get completed after
59 * WPS provisioning step.
60 */
61#define P2P_MAX_INITIAL_CONN_WAIT 10
62#endif /* P2P_MAX_INITIAL_CONN_WAIT */
63
64#ifndef P2P_CONCURRENT_SEARCH_DELAY
65#ifdef ANDROID_P2P
66#define P2P_CONCURRENT_SEARCH_DELAY 0
67#else
68#define P2P_CONCURRENT_SEARCH_DELAY 500
69#endif
70#endif /* P2P_CONCURRENT_SEARCH_DELAY */
71
72enum p2p_group_removal_reason {
73	P2P_GROUP_REMOVAL_UNKNOWN,
74	P2P_GROUP_REMOVAL_SILENT,
75	P2P_GROUP_REMOVAL_FORMATION_FAILED,
76	P2P_GROUP_REMOVAL_REQUESTED,
77	P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
78	P2P_GROUP_REMOVAL_UNAVAILABLE,
79	P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
80#ifdef ANDROID_P2P
81	P2P_GROUP_REMOVAL_FREQ_CONFLICT
82#endif
83};
84
85#ifdef ANDROID_P2P
86static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s);
87#endif
88static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
89static struct wpa_supplicant *
90wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
91			 int go);
92static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
93static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
94static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
95static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
96			 const u8 *dev_addr, enum p2p_wps_method wps_method,
97			 int auto_join);
98static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx,
99					    void *timeout_ctx);
100static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
101static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
102static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
103static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
104static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
105					int group_added);
106
107#ifdef ANDROID_P2P
108static int wpas_global_scan_in_progress(struct wpa_supplicant *wpa_s)
109{
110	struct wpa_supplicant *iface = NULL;
111
112	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
113		if(iface->scanning  || iface->wpa_state == WPA_SCANNING) {
114			wpa_printf(MSG_DEBUG, "P2P: Scan in progress on %s,"
115			"defer P2P SEARCH", iface->ifname);
116			return 1;
117		}
118	}
119
120	return 0;
121}
122#endif
123
124static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
125				      struct wpa_scan_results *scan_res)
126{
127	size_t i;
128
129	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
130		return;
131
132	wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
133		   (int) scan_res->num);
134
135	for (i = 0; i < scan_res->num; i++) {
136		struct wpa_scan_res *bss = scan_res->res[i];
137		if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
138					 bss->freq, bss->level,
139					 (const u8 *) (bss + 1),
140					 bss->ie_len) > 0)
141			break;
142	}
143
144	p2p_scan_res_handled(wpa_s->global->p2p);
145}
146
147
148static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
149			 unsigned int num_req_dev_types,
150			 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
151{
152	struct wpa_supplicant *wpa_s = ctx;
153	struct wpa_supplicant *ifs;
154	struct wpa_driver_scan_params params;
155	int ret;
156	struct wpabuf *wps_ie, *ies;
157	int social_channels[] = { 2412, 2437, 2462, 0, 0 };
158	size_t ielen;
159
160	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
161		return -1;
162
163	for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
164		if (ifs->sta_scan_pending &&
165		    wpas_p2p_in_progress(wpa_s) == 2) {
166			wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
167				   "pending station mode scan to be "
168				   "completed on interface %s", ifs->ifname);
169			wpa_s->p2p_cb_on_scan_complete = 1;
170			wpa_supplicant_req_scan(ifs, 0, 0);
171			return 1;
172		}
173	}
174
175	os_memset(&params, 0, sizeof(params));
176
177	/* P2P Wildcard SSID */
178	params.num_ssids = 1;
179	params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
180	params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
181
182	wpa_s->wps->dev.p2p = 1;
183	wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
184					wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
185					num_req_dev_types, req_dev_types);
186	if (wps_ie == NULL)
187		return -1;
188
189	ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
190	ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
191	if (ies == NULL) {
192		wpabuf_free(wps_ie);
193		return -1;
194	}
195	wpabuf_put_buf(ies, wps_ie);
196	wpabuf_free(wps_ie);
197
198	p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
199
200	params.p2p_probe = 1;
201	params.extra_ies = wpabuf_head(ies);
202	params.extra_ies_len = wpabuf_len(ies);
203
204	switch (type) {
205	case P2P_SCAN_SOCIAL:
206		params.freqs = social_channels;
207		break;
208	case P2P_SCAN_FULL:
209		break;
210	case P2P_SCAN_SPECIFIC:
211		social_channels[0] = freq;
212		social_channels[1] = 0;
213		params.freqs = social_channels;
214		break;
215	case P2P_SCAN_SOCIAL_PLUS_ONE:
216		social_channels[3] = freq;
217		params.freqs = social_channels;
218		break;
219	}
220
221	ret = wpa_drv_scan(wpa_s, &params);
222
223	wpabuf_free(ies);
224
225	if (ret) {
226		if (wpa_s->scanning ||
227		    wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
228			wpa_s->p2p_cb_on_scan_complete = 1;
229			ret = 1;
230		}
231	} else
232		wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
233
234	return ret;
235}
236
237
238static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
239{
240	switch (p2p_group_interface) {
241	case P2P_GROUP_INTERFACE_PENDING:
242		return WPA_IF_P2P_GROUP;
243	case P2P_GROUP_INTERFACE_GO:
244		return WPA_IF_P2P_GO;
245	case P2P_GROUP_INTERFACE_CLIENT:
246		return WPA_IF_P2P_CLIENT;
247	}
248
249	return WPA_IF_P2P_GROUP;
250}
251
252
253static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
254						  const u8 *ssid,
255						  size_t ssid_len, int *go)
256{
257	struct wpa_ssid *s;
258
259	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
260		for (s = wpa_s->conf->ssid; s; s = s->next) {
261			if (s->disabled != 0 || !s->p2p_group ||
262			    s->ssid_len != ssid_len ||
263			    os_memcmp(ssid, s->ssid, ssid_len) != 0)
264				continue;
265			if (s->mode == WPAS_MODE_P2P_GO &&
266			    s != wpa_s->current_ssid)
267				continue;
268			if (go)
269				*go = s->mode == WPAS_MODE_P2P_GO;
270			return wpa_s;
271		}
272	}
273
274	return NULL;
275}
276
277
278static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
279				 enum p2p_group_removal_reason removal_reason)
280{
281	struct wpa_ssid *ssid;
282	char *gtype;
283	const char *reason;
284
285	ssid = wpa_s->current_ssid;
286#ifdef ANDROID_P2P
287	if ((ssid == NULL) && (wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
288#else
289	if (ssid == NULL) {
290#endif
291		/*
292		 * The current SSID was not known, but there may still be a
293		 * pending P2P group interface waiting for provisioning.
294		 */
295		ssid = wpa_s->conf->ssid;
296		while (ssid) {
297			if (ssid->p2p_group &&
298			    (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
299			     (ssid->key_mgmt & WPA_KEY_MGMT_WPS)))
300				break;
301			ssid = ssid->next;
302		}
303		if (ssid == NULL) {
304			wpa_printf(MSG_ERROR, "P2P: P2P group interface "
305				   "not found");
306			return -1;
307		}
308	}
309	if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
310		gtype = "GO";
311	else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
312		 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
313		wpa_s->reassociate = 0;
314		wpa_s->disconnected = 1;
315		wpa_supplicant_deauthenticate(wpa_s,
316					      WLAN_REASON_DEAUTH_LEAVING);
317		gtype = "client";
318	} else
319		gtype = "GO";
320	if (wpa_s->cross_connect_in_use) {
321		wpa_s->cross_connect_in_use = 0;
322		wpa_msg(wpa_s->parent, MSG_INFO,
323			P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
324			wpa_s->ifname, wpa_s->cross_connect_uplink);
325	}
326	switch (removal_reason) {
327	case P2P_GROUP_REMOVAL_REQUESTED:
328		reason = " reason=REQUESTED";
329		break;
330	case P2P_GROUP_REMOVAL_FORMATION_FAILED:
331		reason = " reason=FORMATION_FAILED";
332		break;
333	case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
334		reason = " reason=IDLE";
335		break;
336	case P2P_GROUP_REMOVAL_UNAVAILABLE:
337		reason = " reason=UNAVAILABLE";
338		break;
339	case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
340		reason = " reason=GO_ENDING_SESSION";
341		break;
342#ifdef ANDROID_P2P
343	case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
344		reason = " reason=FREQ_CONFLICT";
345		break;
346#endif
347	default:
348		reason = "";
349		break;
350	}
351	if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
352		wpa_msg(wpa_s->parent, MSG_INFO,
353			P2P_EVENT_GROUP_REMOVED "%s %s%s",
354			wpa_s->ifname, gtype, reason);
355	}
356
357	if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
358		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
359
360	if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
361		wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
362
363	if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
364		struct wpa_global *global;
365		char *ifname;
366		enum wpa_driver_if_type type;
367		wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
368			wpa_s->ifname);
369		global = wpa_s->global;
370		ifname = os_strdup(wpa_s->ifname);
371		type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
372		wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
373		wpa_s = global->ifaces;
374		if (wpa_s && ifname)
375			wpa_drv_if_remove(wpa_s, type, ifname);
376		os_free(ifname);
377		return 0;
378	}
379
380	wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
381	if (ssid && (ssid->p2p_group ||
382		     ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
383		     (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
384		int id = ssid->id;
385		if (ssid == wpa_s->current_ssid) {
386			wpa_sm_set_config(wpa_s->wpa, NULL);
387			eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
388			wpa_s->current_ssid = NULL;
389		}
390		/*
391		 * Networks objects created during any P2P activities are not
392		 * exposed out as they might/will confuse certain non-P2P aware
393		 * applications since these network objects won't behave like
394		 * regular ones.
395		 *
396		 * Likewise, we don't send out network removed signals for such
397		 * network objects.
398		 */
399		wpa_config_remove_network(wpa_s->conf, id);
400		wpa_supplicant_clear_status(wpa_s);
401		wpa_supplicant_cancel_sched_scan(wpa_s);
402	} else {
403		wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
404			   "found");
405	}
406	if (wpa_s->ap_iface)
407		wpa_supplicant_ap_deinit(wpa_s);
408	else
409		wpa_drv_deinit_p2p_cli(wpa_s);
410
411	return 0;
412}
413
414
415static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
416				     u8 *go_dev_addr,
417				     const u8 *ssid, size_t ssid_len)
418{
419	struct wpa_bss *bss;
420	const u8 *bssid;
421	struct wpabuf *p2p;
422	u8 group_capab;
423	const u8 *addr;
424
425	if (wpa_s->go_params)
426		bssid = wpa_s->go_params->peer_interface_addr;
427	else
428		bssid = wpa_s->bssid;
429
430	bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
431	if (bss == NULL) {
432		u8 iface_addr[ETH_ALEN];
433		if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
434					   iface_addr) == 0)
435			bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
436	}
437	if (bss == NULL) {
438		wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
439			   "group is persistent - BSS " MACSTR " not found",
440			   MAC2STR(bssid));
441		return 0;
442	}
443
444	p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
445	if (p2p == NULL) {
446		wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
447			   "group is persistent - BSS " MACSTR
448			   " did not include P2P IE", MAC2STR(bssid));
449		wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
450			    (u8 *) (bss + 1), bss->ie_len);
451		wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
452			    ((u8 *) bss + 1) + bss->ie_len,
453			    bss->beacon_ie_len);
454		return 0;
455	}
456
457	group_capab = p2p_get_group_capab(p2p);
458	addr = p2p_get_go_dev_addr(p2p);
459	wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
460		   "group_capab=0x%x", group_capab);
461	if (addr) {
462		os_memcpy(go_dev_addr, addr, ETH_ALEN);
463		wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
464			   MAC2STR(addr));
465	} else
466		os_memset(go_dev_addr, 0, ETH_ALEN);
467	wpabuf_free(p2p);
468
469	wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
470		   "go_dev_addr=" MACSTR,
471		   MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
472
473	return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
474}
475
476
477static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
478					   struct wpa_ssid *ssid,
479					   const u8 *go_dev_addr)
480{
481	struct wpa_ssid *s;
482	int changed = 0;
483
484	wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
485		   "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
486	for (s = wpa_s->conf->ssid; s; s = s->next) {
487		if (s->disabled == 2 &&
488		    os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
489		    s->ssid_len == ssid->ssid_len &&
490		    os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
491			break;
492	}
493
494	if (s) {
495		wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
496			   "entry");
497		if (ssid->passphrase && !s->passphrase)
498			changed = 1;
499		else if (ssid->passphrase && s->passphrase &&
500			 os_strcmp(ssid->passphrase, s->passphrase) != 0)
501			changed = 1;
502	} else {
503		wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
504			   "entry");
505		changed = 1;
506		s = wpa_config_add_network(wpa_s->conf);
507		if (s == NULL)
508			return -1;
509
510		/*
511		 * Instead of network_added we emit persistent_group_added
512		 * notification. Also to keep the defense checks in
513		 * persistent_group obj registration method, we set the
514		 * relevant flags in s to designate it as a persistent group.
515		 */
516		s->p2p_group = 1;
517		s->p2p_persistent_group = 1;
518		wpas_notify_persistent_group_added(wpa_s, s);
519		wpa_config_set_network_defaults(s);
520	}
521
522	s->p2p_group = 1;
523	s->p2p_persistent_group = 1;
524	s->disabled = 2;
525	s->bssid_set = 1;
526	os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
527	s->mode = ssid->mode;
528	s->auth_alg = WPA_AUTH_ALG_OPEN;
529	s->key_mgmt = WPA_KEY_MGMT_PSK;
530	s->proto = WPA_PROTO_RSN;
531	s->pairwise_cipher = WPA_CIPHER_CCMP;
532	s->export_keys = 1;
533	if (ssid->passphrase) {
534		os_free(s->passphrase);
535		s->passphrase = os_strdup(ssid->passphrase);
536	}
537	if (ssid->psk_set) {
538		s->psk_set = 1;
539		os_memcpy(s->psk, ssid->psk, 32);
540	}
541	if (s->passphrase && !s->psk_set)
542		wpa_config_update_psk(s);
543	if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
544		os_free(s->ssid);
545		s->ssid = os_malloc(ssid->ssid_len);
546	}
547	if (s->ssid) {
548		s->ssid_len = ssid->ssid_len;
549		os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
550	}
551
552#ifndef CONFIG_NO_CONFIG_WRITE
553	if (changed && wpa_s->conf->update_config &&
554	    wpa_config_write(wpa_s->confname, wpa_s->conf)) {
555		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
556	}
557#endif /* CONFIG_NO_CONFIG_WRITE */
558
559	return s->id;
560}
561
562
563static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
564						 const u8 *addr)
565{
566	struct wpa_ssid *ssid, *s;
567	u8 *n;
568	size_t i;
569	int found = 0;
570
571	ssid = wpa_s->current_ssid;
572	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
573	    !ssid->p2p_persistent_group)
574		return;
575
576	for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
577		if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
578			continue;
579
580		if (s->ssid_len == ssid->ssid_len &&
581		    os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
582			break;
583	}
584
585	if (s == NULL)
586		return;
587
588	for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
589		if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
590			      ETH_ALEN) != 0)
591			continue;
592
593		if (i == s->num_p2p_clients - 1)
594			return; /* already the most recent entry */
595
596		/* move the entry to mark it most recent */
597		os_memmove(s->p2p_client_list + i * ETH_ALEN,
598			   s->p2p_client_list + (i + 1) * ETH_ALEN,
599			   (s->num_p2p_clients - i - 1) * ETH_ALEN);
600		os_memcpy(s->p2p_client_list +
601			  (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
602		found = 1;
603		break;
604	}
605
606	if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
607		n = os_realloc_array(s->p2p_client_list,
608				     s->num_p2p_clients + 1, ETH_ALEN);
609		if (n == NULL)
610			return;
611		os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
612		s->p2p_client_list = n;
613		s->num_p2p_clients++;
614	} else if (!found) {
615		/* Not enough room for an additional entry - drop the oldest
616		 * entry */
617		os_memmove(s->p2p_client_list,
618			   s->p2p_client_list + ETH_ALEN,
619			   (s->num_p2p_clients - 1) * ETH_ALEN);
620		os_memcpy(s->p2p_client_list +
621			  (s->num_p2p_clients - 1) * ETH_ALEN,
622			  addr, ETH_ALEN);
623	}
624
625#ifndef CONFIG_NO_CONFIG_WRITE
626	if (wpa_s->parent->conf->update_config &&
627	    wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
628		wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
629#endif /* CONFIG_NO_CONFIG_WRITE */
630}
631
632
633static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
634					   int success)
635{
636	struct wpa_ssid *ssid;
637	const char *ssid_txt;
638	int client;
639	int persistent;
640	u8 go_dev_addr[ETH_ALEN];
641	int network_id = -1;
642
643	/*
644	 * This callback is likely called for the main interface. Update wpa_s
645	 * to use the group interface if a new interface was created for the
646	 * group.
647	 */
648	if (wpa_s->global->p2p_group_formation)
649		wpa_s = wpa_s->global->p2p_group_formation;
650	wpa_s->global->p2p_group_formation = NULL;
651	wpa_s->p2p_in_provisioning = 0;
652
653	if (!success) {
654		wpa_msg(wpa_s->parent, MSG_INFO,
655			P2P_EVENT_GROUP_FORMATION_FAILURE);
656		wpas_p2p_group_delete(wpa_s,
657				      P2P_GROUP_REMOVAL_FORMATION_FAILED);
658		return;
659	}
660
661	wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_FORMATION_SUCCESS);
662
663	ssid = wpa_s->current_ssid;
664	if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
665		ssid->mode = WPAS_MODE_P2P_GO;
666		p2p_group_notif_formation_done(wpa_s->p2p_group);
667		wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
668	}
669
670	persistent = 0;
671	if (ssid) {
672		ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
673		client = ssid->mode == WPAS_MODE_INFRA;
674		if (ssid->mode == WPAS_MODE_P2P_GO) {
675			persistent = ssid->p2p_persistent_group;
676			os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
677				  ETH_ALEN);
678		} else
679			persistent = wpas_p2p_persistent_group(wpa_s,
680							       go_dev_addr,
681							       ssid->ssid,
682							       ssid->ssid_len);
683	} else {
684		ssid_txt = "";
685		client = wpa_s->p2p_group_interface ==
686			P2P_GROUP_INTERFACE_CLIENT;
687		os_memset(go_dev_addr, 0, ETH_ALEN);
688	}
689
690	wpa_s->show_group_started = 0;
691	if (client) {
692		/*
693		 * Indicate event only after successfully completed 4-way
694		 * handshake, i.e., when the interface is ready for data
695		 * packets.
696		 */
697		wpa_s->show_group_started = 1;
698#ifdef ANDROID_P2P
699		/* For client Second phase of Group formation (4-way handshake) can be still pending
700		 * So we need to restore wpa_s->global->p2p_group_formation */
701		wpa_s->global->p2p_group_formation = wpa_s;
702#endif
703
704	} else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
705		char psk[65];
706		wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
707		wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
708			"%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr=" MACSTR
709			"%s",
710			wpa_s->ifname, ssid_txt, ssid->frequency, psk,
711			MAC2STR(go_dev_addr),
712			persistent ? " [PERSISTENT]" : "");
713		wpas_p2p_cross_connect_setup(wpa_s);
714		wpas_p2p_set_group_idle_timeout(wpa_s);
715	} else {
716		wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
717			"%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
718			"go_dev_addr=" MACSTR "%s",
719			wpa_s->ifname, ssid_txt, ssid ? ssid->frequency : 0,
720			ssid && ssid->passphrase ? ssid->passphrase : "",
721			MAC2STR(go_dev_addr),
722			persistent ? " [PERSISTENT]" : "");
723		wpas_p2p_cross_connect_setup(wpa_s);
724		wpas_p2p_set_group_idle_timeout(wpa_s);
725	}
726
727	if (persistent)
728		network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
729							     ssid, go_dev_addr);
730	if (network_id < 0 && ssid)
731		network_id = ssid->id;
732	if (!client)
733		wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
734}
735
736
737static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
738					   unsigned int freq,
739					   const u8 *dst, const u8 *src,
740					   const u8 *bssid,
741					   const u8 *data, size_t data_len,
742					   enum offchannel_send_action_result
743					   result)
744{
745	enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
746
747	if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
748		return;
749	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
750		return;
751
752	switch (result) {
753	case OFFCHANNEL_SEND_ACTION_SUCCESS:
754		res = P2P_SEND_ACTION_SUCCESS;
755		break;
756	case OFFCHANNEL_SEND_ACTION_NO_ACK:
757		res = P2P_SEND_ACTION_NO_ACK;
758		break;
759	case OFFCHANNEL_SEND_ACTION_FAILED:
760		res = P2P_SEND_ACTION_FAILED;
761		break;
762	}
763
764	p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
765
766	if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
767	    wpa_s->pending_pd_before_join &&
768	    (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
769	     os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
770		wpa_s->pending_pd_before_join = 0;
771		if (wpa_s->p2p_fallback_to_go_neg) {
772			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
773				"during p2p_connect-auto");
774			wpas_p2p_fallback_to_go_neg(wpa_s, 0);
775			return;
776		}
777
778		wpa_printf(MSG_DEBUG, "P2P: Starting pending "
779			   "join-existing-group operation (no ACK for PD "
780			   "Req)");
781		wpas_p2p_join_start(wpa_s);
782	}
783}
784
785
786static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
787			    const u8 *src, const u8 *bssid, const u8 *buf,
788			    size_t len, unsigned int wait_time)
789{
790	struct wpa_supplicant *wpa_s = ctx;
791	return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
792				      wait_time,
793				      wpas_p2p_send_action_tx_status, 1);
794}
795
796
797static void wpas_send_action_done(void *ctx)
798{
799	struct wpa_supplicant *wpa_s = ctx;
800	offchannel_send_action_done(wpa_s);
801}
802
803
804static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
805				    struct p2p_go_neg_results *params)
806{
807	if (wpa_s->go_params == NULL) {
808		wpa_s->go_params = os_malloc(sizeof(*params));
809		if (wpa_s->go_params == NULL)
810			return -1;
811	}
812	os_memcpy(wpa_s->go_params, params, sizeof(*params));
813	return 0;
814}
815
816
817static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
818				    struct p2p_go_neg_results *res)
819{
820	wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
821		   MAC2STR(res->peer_interface_addr));
822	wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
823			  res->ssid, res->ssid_len);
824	wpa_supplicant_ap_deinit(wpa_s);
825	wpas_copy_go_neg_results(wpa_s, res);
826	if (res->wps_method == WPS_PBC)
827		wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
828	else {
829		u16 dev_pw_id = DEV_PW_DEFAULT;
830		if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
831			dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
832		wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
833				   wpa_s->p2p_pin, 1, dev_pw_id);
834	}
835}
836
837
838static void p2p_go_configured(void *ctx, void *data)
839{
840	struct wpa_supplicant *wpa_s = ctx;
841	struct p2p_go_neg_results *params = data;
842	struct wpa_ssid *ssid;
843	int network_id = -1;
844
845	ssid = wpa_s->current_ssid;
846	if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
847		wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
848		if (wpa_s->global->p2p_group_formation == wpa_s)
849			wpa_s->global->p2p_group_formation = NULL;
850		wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
851			"%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
852			"go_dev_addr=" MACSTR "%s",
853			wpa_s->ifname,
854			wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
855			ssid->frequency,
856			params->passphrase ? params->passphrase : "",
857			MAC2STR(wpa_s->global->p2p_dev_addr),
858			params->persistent_group ? " [PERSISTENT]" : "");
859
860		if (params->persistent_group)
861			network_id = wpas_p2p_store_persistent_group(
862				wpa_s->parent, ssid,
863				wpa_s->global->p2p_dev_addr);
864		if (network_id < 0)
865			network_id = ssid->id;
866		wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
867		wpas_p2p_cross_connect_setup(wpa_s);
868		wpas_p2p_set_group_idle_timeout(wpa_s);
869		return;
870	}
871
872	wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
873	if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
874					      params->peer_interface_addr)) {
875		wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
876			   "filtering");
877		return;
878	}
879	if (params->wps_method == WPS_PBC)
880		wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
881					  params->peer_device_addr);
882	else if (wpa_s->p2p_pin[0])
883		wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
884					  wpa_s->p2p_pin, NULL, 0);
885	os_free(wpa_s->go_params);
886	wpa_s->go_params = NULL;
887}
888
889
890static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
891			      struct p2p_go_neg_results *params,
892			      int group_formation)
893{
894	struct wpa_ssid *ssid;
895
896	if (wpas_copy_go_neg_results(wpa_s, params) < 0)
897		return;
898
899	ssid = wpa_config_add_network(wpa_s->conf);
900	if (ssid == NULL)
901		return;
902
903	wpa_s->show_group_started = 0;
904
905	wpa_config_set_network_defaults(ssid);
906	ssid->temporary = 1;
907	ssid->p2p_group = 1;
908	ssid->p2p_persistent_group = params->persistent_group;
909	ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
910		WPAS_MODE_P2P_GO;
911	ssid->frequency = params->freq;
912	ssid->ht40 = params->ht40;
913	ssid->ssid = os_zalloc(params->ssid_len + 1);
914	if (ssid->ssid) {
915		os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
916		ssid->ssid_len = params->ssid_len;
917	}
918	ssid->auth_alg = WPA_AUTH_ALG_OPEN;
919	ssid->key_mgmt = WPA_KEY_MGMT_PSK;
920	ssid->proto = WPA_PROTO_RSN;
921	ssid->pairwise_cipher = WPA_CIPHER_CCMP;
922	ssid->passphrase = os_strdup(params->passphrase);
923	ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
924
925	wpa_s->ap_configured_cb = p2p_go_configured;
926	wpa_s->ap_configured_cb_ctx = wpa_s;
927	wpa_s->ap_configured_cb_data = wpa_s->go_params;
928	wpa_s->connect_without_scan = ssid;
929	wpa_s->reassociate = 1;
930	wpa_s->disconnected = 0;
931	wpa_supplicant_req_scan(wpa_s, 0, 0);
932}
933
934
935static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
936				  const struct wpa_supplicant *src)
937{
938	struct wpa_config *d;
939	const struct wpa_config *s;
940
941	d = dst->conf;
942	s = src->conf;
943
944#define C(n) if (s->n) d->n = os_strdup(s->n)
945	C(device_name);
946	C(manufacturer);
947	C(model_name);
948	C(model_number);
949	C(serial_number);
950	C(config_methods);
951#undef C
952
953	os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
954	os_memcpy(d->sec_device_type, s->sec_device_type,
955		  sizeof(d->sec_device_type));
956	d->num_sec_device_types = s->num_sec_device_types;
957
958	d->p2p_group_idle = s->p2p_group_idle;
959	d->p2p_intra_bss = s->p2p_intra_bss;
960	d->persistent_reconnect = s->persistent_reconnect;
961	d->max_num_sta = s->max_num_sta;
962	d->pbc_in_m1 = s->pbc_in_m1;
963}
964
965
966static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
967					enum wpa_driver_if_type type)
968{
969	char ifname[120], force_ifname[120];
970
971	if (wpa_s->pending_interface_name[0]) {
972		wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
973			   "- skip creation of a new one");
974		if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
975			wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
976				   "unknown?! ifname='%s'",
977				   wpa_s->pending_interface_name);
978			return -1;
979		}
980		return 0;
981	}
982
983	os_snprintf(ifname, sizeof(ifname), "p2p-%s-%d", wpa_s->ifname,
984		    wpa_s->p2p_group_idx);
985	if (os_strlen(ifname) >= IFNAMSIZ &&
986	    os_strlen(wpa_s->ifname) < IFNAMSIZ) {
987		/* Try to avoid going over the IFNAMSIZ length limit */
988		os_snprintf(ifname, sizeof(ifname), "p2p-%d",
989			    wpa_s->p2p_group_idx);
990	}
991	force_ifname[0] = '\0';
992
993	wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
994		   ifname);
995	wpa_s->p2p_group_idx++;
996
997	wpa_s->pending_interface_type = type;
998	if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
999			   wpa_s->pending_interface_addr, NULL) < 0) {
1000		wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1001			   "interface");
1002		return -1;
1003	}
1004
1005	if (force_ifname[0]) {
1006		wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1007			   force_ifname);
1008		os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1009			   sizeof(wpa_s->pending_interface_name));
1010	} else
1011		os_strlcpy(wpa_s->pending_interface_name, ifname,
1012			   sizeof(wpa_s->pending_interface_name));
1013	wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1014		   MACSTR, wpa_s->pending_interface_name,
1015		   MAC2STR(wpa_s->pending_interface_addr));
1016
1017	return 0;
1018}
1019
1020
1021static void wpas_p2p_remove_pending_group_interface(
1022	struct wpa_supplicant *wpa_s)
1023{
1024	if (!wpa_s->pending_interface_name[0] ||
1025	    is_zero_ether_addr(wpa_s->pending_interface_addr))
1026		return; /* No pending virtual interface */
1027
1028	wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1029		   wpa_s->pending_interface_name);
1030	wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1031			  wpa_s->pending_interface_name);
1032	os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1033	wpa_s->pending_interface_name[0] = '\0';
1034}
1035
1036
1037static struct wpa_supplicant *
1038wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1039{
1040	struct wpa_interface iface;
1041	struct wpa_supplicant *group_wpa_s;
1042
1043	if (!wpa_s->pending_interface_name[0]) {
1044		wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1045		if (!wpas_p2p_create_iface(wpa_s))
1046			return NULL;
1047		/*
1048		 * Something has forced us to remove the pending interface; try
1049		 * to create a new one and hope for the best that we will get
1050		 * the same local address.
1051		 */
1052		if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1053						 WPA_IF_P2P_CLIENT) < 0)
1054			return NULL;
1055	}
1056
1057	os_memset(&iface, 0, sizeof(iface));
1058	iface.ifname = wpa_s->pending_interface_name;
1059	iface.driver = wpa_s->driver->name;
1060	iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1061	iface.driver_param = wpa_s->conf->driver_param;
1062	group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1063	if (group_wpa_s == NULL) {
1064		wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1065			   "wpa_supplicant interface");
1066		return NULL;
1067	}
1068	wpa_s->pending_interface_name[0] = '\0';
1069	group_wpa_s->parent = wpa_s;
1070	group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1071		P2P_GROUP_INTERFACE_CLIENT;
1072	wpa_s->global->p2p_group_formation = group_wpa_s;
1073
1074	wpas_p2p_clone_config(group_wpa_s, wpa_s);
1075
1076	return group_wpa_s;
1077}
1078
1079
1080static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1081					     void *timeout_ctx)
1082{
1083	struct wpa_supplicant *wpa_s = eloop_ctx;
1084	wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1085	if (wpa_s->global->p2p)
1086		p2p_group_formation_failed(wpa_s->global->p2p);
1087	else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1088		wpa_drv_p2p_group_formation_failed(wpa_s);
1089	wpas_group_formation_completed(wpa_s, 0);
1090}
1091
1092
1093void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1094{
1095	struct wpa_supplicant *wpa_s = ctx;
1096
1097	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1098		wpa_drv_cancel_remain_on_channel(wpa_s);
1099		wpa_s->off_channel_freq = 0;
1100		wpa_s->roc_waiting_drv_freq = 0;
1101	}
1102
1103	if (res->status) {
1104		wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_FAILURE "status=%d",
1105			res->status);
1106		wpas_notify_p2p_go_neg_completed(wpa_s, res);
1107		wpas_p2p_remove_pending_group_interface(wpa_s);
1108		return;
1109	}
1110
1111	if (wpa_s->p2p_go_ht40)
1112		res->ht40 = 1;
1113
1114	wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS);
1115	wpas_notify_p2p_go_neg_completed(wpa_s, res);
1116
1117	if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1118		struct wpa_ssid *ssid;
1119		ssid = wpa_config_get_network(wpa_s->conf,
1120					      wpa_s->p2p_persistent_id);
1121		if (ssid && ssid->disabled == 2 &&
1122		    ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1123			size_t len = os_strlen(ssid->passphrase);
1124			wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1125				   "on requested persistent group");
1126			os_memcpy(res->passphrase, ssid->passphrase, len);
1127			res->passphrase[len] = '\0';
1128		}
1129	}
1130
1131	if (wpa_s->create_p2p_iface) {
1132		struct wpa_supplicant *group_wpa_s =
1133			wpas_p2p_init_group_interface(wpa_s, res->role_go);
1134		if (group_wpa_s == NULL) {
1135			wpas_p2p_remove_pending_group_interface(wpa_s);
1136			return;
1137		}
1138		if (group_wpa_s != wpa_s) {
1139			os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1140				  sizeof(group_wpa_s->p2p_pin));
1141			group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1142		}
1143		os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1144		wpa_s->pending_interface_name[0] = '\0';
1145		group_wpa_s->p2p_in_provisioning = 1;
1146
1147		if (res->role_go)
1148			wpas_start_wps_go(group_wpa_s, res, 1);
1149		else
1150			wpas_start_wps_enrollee(group_wpa_s, res);
1151	} else {
1152		wpa_s->p2p_in_provisioning = 1;
1153		wpa_s->global->p2p_group_formation = wpa_s;
1154
1155		if (res->role_go)
1156			wpas_start_wps_go(wpa_s, res, 1);
1157		else
1158			wpas_start_wps_enrollee(ctx, res);
1159	}
1160
1161	wpa_s->p2p_long_listen = 0;
1162	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1163
1164	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1165	eloop_register_timeout(15 + res->peer_config_timeout / 100,
1166			       (res->peer_config_timeout % 100) * 10000,
1167			       wpas_p2p_group_formation_timeout, wpa_s, NULL);
1168}
1169
1170
1171void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1172{
1173	struct wpa_supplicant *wpa_s = ctx;
1174	wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1175		" dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1176
1177	wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1178}
1179
1180
1181void wpas_dev_found(void *ctx, const u8 *addr,
1182		    const struct p2p_peer_info *info,
1183		    int new_device)
1184{
1185#ifndef CONFIG_NO_STDOUT_DEBUG
1186	struct wpa_supplicant *wpa_s = ctx;
1187	char devtype[WPS_DEV_TYPE_BUFSIZE];
1188
1189	wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1190		" p2p_dev_addr=" MACSTR
1191		" pri_dev_type=%s name='%s' config_methods=0x%x "
1192		"dev_capab=0x%x group_capab=0x%x",
1193		MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1194		wps_dev_type_bin2str(info->pri_dev_type, devtype,
1195				     sizeof(devtype)),
1196		info->device_name, info->config_methods,
1197		info->dev_capab, info->group_capab);
1198#endif /* CONFIG_NO_STDOUT_DEBUG */
1199
1200	wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1201}
1202
1203
1204static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1205{
1206	struct wpa_supplicant *wpa_s = ctx;
1207
1208	wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1209		"p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1210
1211	wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1212}
1213
1214
1215static int wpas_start_listen(void *ctx, unsigned int freq,
1216			     unsigned int duration,
1217			     const struct wpabuf *probe_resp_ie)
1218{
1219	struct wpa_supplicant *wpa_s = ctx;
1220
1221	wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1222
1223	if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1224		wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1225			   "report received Probe Request frames");
1226		return -1;
1227	}
1228
1229	wpa_s->pending_listen_freq = freq;
1230	wpa_s->pending_listen_duration = duration;
1231
1232	if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1233		wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1234			   "to remain on channel (%u MHz) for Listen "
1235			   "state", freq);
1236		wpa_s->pending_listen_freq = 0;
1237		return -1;
1238	}
1239	wpa_s->off_channel_freq = 0;
1240	wpa_s->roc_waiting_drv_freq = freq;
1241
1242	return 0;
1243}
1244
1245
1246static void wpas_stop_listen(void *ctx)
1247{
1248	struct wpa_supplicant *wpa_s = ctx;
1249	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1250		wpa_drv_cancel_remain_on_channel(wpa_s);
1251		wpa_s->off_channel_freq = 0;
1252		wpa_s->roc_waiting_drv_freq = 0;
1253	}
1254	wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1255	wpa_drv_probe_req_report(wpa_s, 0);
1256}
1257
1258
1259static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1260{
1261	struct wpa_supplicant *wpa_s = ctx;
1262	return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1263}
1264
1265
1266static struct p2p_srv_bonjour *
1267wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1268			     const struct wpabuf *query)
1269{
1270	struct p2p_srv_bonjour *bsrv;
1271	size_t len;
1272
1273	len = wpabuf_len(query);
1274	dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1275			 struct p2p_srv_bonjour, list) {
1276		if (len == wpabuf_len(bsrv->query) &&
1277		    os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1278			      len) == 0)
1279			return bsrv;
1280	}
1281	return NULL;
1282}
1283
1284
1285static struct p2p_srv_upnp *
1286wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1287			  const char *service)
1288{
1289	struct p2p_srv_upnp *usrv;
1290
1291	dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1292			 struct p2p_srv_upnp, list) {
1293		if (version == usrv->version &&
1294		    os_strcmp(service, usrv->service) == 0)
1295			return usrv;
1296	}
1297	return NULL;
1298}
1299
1300
1301static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1302					u8 srv_trans_id)
1303{
1304	u8 *len_pos;
1305
1306	if (wpabuf_tailroom(resp) < 5)
1307		return;
1308
1309	/* Length (to be filled) */
1310	len_pos = wpabuf_put(resp, 2);
1311	wpabuf_put_u8(resp, srv_proto);
1312	wpabuf_put_u8(resp, srv_trans_id);
1313	/* Status Code */
1314	wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1315	/* Response Data: empty */
1316	WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1317}
1318
1319
1320static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1321				struct wpabuf *resp, u8 srv_trans_id)
1322{
1323	struct p2p_srv_bonjour *bsrv;
1324	u8 *len_pos;
1325
1326	wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1327
1328	if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1329		wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1330		return;
1331	}
1332
1333	dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1334			 struct p2p_srv_bonjour, list) {
1335		if (wpabuf_tailroom(resp) <
1336		    5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1337			return;
1338		/* Length (to be filled) */
1339		len_pos = wpabuf_put(resp, 2);
1340		wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1341		wpabuf_put_u8(resp, srv_trans_id);
1342		/* Status Code */
1343		wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1344		wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1345				  wpabuf_head(bsrv->resp),
1346				  wpabuf_len(bsrv->resp));
1347		/* Response Data */
1348		wpabuf_put_buf(resp, bsrv->query); /* Key */
1349		wpabuf_put_buf(resp, bsrv->resp); /* Value */
1350		WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1351			     2);
1352	}
1353}
1354
1355
1356static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1357				struct wpabuf *resp, u8 srv_trans_id,
1358				const u8 *query, size_t query_len)
1359{
1360	struct p2p_srv_bonjour *bsrv;
1361	struct wpabuf buf;
1362	u8 *len_pos;
1363
1364	wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1365			  query, query_len);
1366	if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1367		wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1368		wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1369					    srv_trans_id);
1370		return;
1371	}
1372
1373	if (query_len == 0) {
1374		wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1375		return;
1376	}
1377
1378	if (wpabuf_tailroom(resp) < 5)
1379		return;
1380	/* Length (to be filled) */
1381	len_pos = wpabuf_put(resp, 2);
1382	wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1383	wpabuf_put_u8(resp, srv_trans_id);
1384
1385	wpabuf_set(&buf, query, query_len);
1386	bsrv = wpas_p2p_service_get_bonjour(wpa_s, &buf);
1387	if (bsrv == NULL) {
1388		wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1389			   "available");
1390
1391		/* Status Code */
1392		wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1393		/* Response Data: empty */
1394		WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1395			     2);
1396		return;
1397	}
1398
1399	/* Status Code */
1400	wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1401	wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1402			  wpabuf_head(bsrv->resp), wpabuf_len(bsrv->resp));
1403
1404	if (wpabuf_tailroom(resp) >=
1405	    wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp)) {
1406		/* Response Data */
1407		wpabuf_put_buf(resp, bsrv->query); /* Key */
1408		wpabuf_put_buf(resp, bsrv->resp); /* Value */
1409	}
1410	WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1411}
1412
1413
1414static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1415			     struct wpabuf *resp, u8 srv_trans_id)
1416{
1417	struct p2p_srv_upnp *usrv;
1418	u8 *len_pos;
1419
1420	wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1421
1422	if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1423		wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1424		return;
1425	}
1426
1427	dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1428			 struct p2p_srv_upnp, list) {
1429		if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1430			return;
1431
1432		/* Length (to be filled) */
1433		len_pos = wpabuf_put(resp, 2);
1434		wpabuf_put_u8(resp, P2P_SERV_UPNP);
1435		wpabuf_put_u8(resp, srv_trans_id);
1436
1437		/* Status Code */
1438		wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1439		/* Response Data */
1440		wpabuf_put_u8(resp, usrv->version);
1441		wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1442			   usrv->service);
1443		wpabuf_put_str(resp, usrv->service);
1444		WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1445			     2);
1446	}
1447}
1448
1449
1450static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1451			     struct wpabuf *resp, u8 srv_trans_id,
1452			     const u8 *query, size_t query_len)
1453{
1454	struct p2p_srv_upnp *usrv;
1455	u8 *len_pos;
1456	u8 version;
1457	char *str;
1458	int count = 0;
1459
1460	wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1461			  query, query_len);
1462
1463	if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1464		wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1465		wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1466					    srv_trans_id);
1467		return;
1468	}
1469
1470	if (query_len == 0) {
1471		wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1472		return;
1473	}
1474
1475	if (wpabuf_tailroom(resp) < 5)
1476		return;
1477
1478	/* Length (to be filled) */
1479	len_pos = wpabuf_put(resp, 2);
1480	wpabuf_put_u8(resp, P2P_SERV_UPNP);
1481	wpabuf_put_u8(resp, srv_trans_id);
1482
1483	version = query[0];
1484	str = os_malloc(query_len);
1485	if (str == NULL)
1486		return;
1487	os_memcpy(str, query + 1, query_len - 1);
1488	str[query_len - 1] = '\0';
1489
1490	dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1491			 struct p2p_srv_upnp, list) {
1492		if (version != usrv->version)
1493			continue;
1494
1495		if (os_strcmp(str, "ssdp:all") != 0 &&
1496		    os_strstr(usrv->service, str) == NULL)
1497			continue;
1498
1499		if (wpabuf_tailroom(resp) < 2)
1500			break;
1501		if (count == 0) {
1502			/* Status Code */
1503			wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1504			/* Response Data */
1505			wpabuf_put_u8(resp, version);
1506		} else
1507			wpabuf_put_u8(resp, ',');
1508
1509		count++;
1510
1511		wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1512			   usrv->service);
1513		if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1514			break;
1515		wpabuf_put_str(resp, usrv->service);
1516	}
1517	os_free(str);
1518
1519	if (count == 0) {
1520		wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1521			   "available");
1522		/* Status Code */
1523		wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1524		/* Response Data: empty */
1525	}
1526
1527	WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1528}
1529
1530
1531#ifdef CONFIG_WIFI_DISPLAY
1532static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1533			    struct wpabuf *resp, u8 srv_trans_id,
1534			    const u8 *query, size_t query_len)
1535{
1536	const u8 *pos;
1537	u8 role;
1538	u8 *len_pos;
1539
1540	wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1541
1542	if (!wpa_s->global->wifi_display) {
1543		wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1544		wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1545					    srv_trans_id);
1546		return;
1547	}
1548
1549	if (query_len < 1) {
1550		wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1551			   "Role");
1552		return;
1553	}
1554
1555	if (wpabuf_tailroom(resp) < 5)
1556		return;
1557
1558	pos = query;
1559	role = *pos++;
1560	wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1561
1562	/* TODO: role specific handling */
1563
1564	/* Length (to be filled) */
1565	len_pos = wpabuf_put(resp, 2);
1566	wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1567	wpabuf_put_u8(resp, srv_trans_id);
1568	wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1569
1570	while (pos < query + query_len) {
1571		if (*pos < MAX_WFD_SUBELEMS &&
1572		    wpa_s->global->wfd_subelem[*pos] &&
1573		    wpabuf_tailroom(resp) >=
1574		    wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1575			wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1576				   "subelement %u", *pos);
1577			wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
1578		}
1579		pos++;
1580	}
1581
1582	WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1583}
1584#endif /* CONFIG_WIFI_DISPLAY */
1585
1586
1587void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
1588		     u16 update_indic, const u8 *tlvs, size_t tlvs_len)
1589{
1590	struct wpa_supplicant *wpa_s = ctx;
1591	const u8 *pos = tlvs;
1592	const u8 *end = tlvs + tlvs_len;
1593	const u8 *tlv_end;
1594	u16 slen;
1595	struct wpabuf *resp;
1596	u8 srv_proto, srv_trans_id;
1597	size_t buf_len;
1598	char *buf;
1599
1600	wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
1601		    tlvs, tlvs_len);
1602	buf_len = 2 * tlvs_len + 1;
1603	buf = os_malloc(buf_len);
1604	if (buf) {
1605		wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1606		wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
1607			     MACSTR " %u %u %s",
1608			     freq, MAC2STR(sa), dialog_token, update_indic,
1609			     buf);
1610		os_free(buf);
1611	}
1612
1613	if (wpa_s->p2p_sd_over_ctrl_iface) {
1614		wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1615					   update_indic, tlvs, tlvs_len);
1616		return; /* to be processed by an external program */
1617	}
1618
1619	resp = wpabuf_alloc(10000);
1620	if (resp == NULL)
1621		return;
1622
1623	while (pos + 1 < end) {
1624		wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
1625		slen = WPA_GET_LE16(pos);
1626		pos += 2;
1627		if (pos + slen > end || slen < 2) {
1628			wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
1629				   "length");
1630			wpabuf_free(resp);
1631			return;
1632		}
1633		tlv_end = pos + slen;
1634
1635		srv_proto = *pos++;
1636		wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1637			   srv_proto);
1638		srv_trans_id = *pos++;
1639		wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1640			   srv_trans_id);
1641
1642		wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
1643			    pos, tlv_end - pos);
1644
1645
1646		if (wpa_s->force_long_sd) {
1647			wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
1648				   "response");
1649			wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1650			wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1651			goto done;
1652		}
1653
1654		switch (srv_proto) {
1655		case P2P_SERV_ALL_SERVICES:
1656			wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
1657				   "for all services");
1658			if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
1659			    dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1660				wpa_printf(MSG_DEBUG, "P2P: No service "
1661					   "discovery protocols available");
1662				wpas_sd_add_proto_not_avail(
1663					resp, P2P_SERV_ALL_SERVICES,
1664					srv_trans_id);
1665				break;
1666			}
1667			wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1668			wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1669			break;
1670		case P2P_SERV_BONJOUR:
1671			wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
1672					    pos, tlv_end - pos);
1673			break;
1674		case P2P_SERV_UPNP:
1675			wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
1676					 pos, tlv_end - pos);
1677			break;
1678#ifdef CONFIG_WIFI_DISPLAY
1679		case P2P_SERV_WIFI_DISPLAY:
1680			wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
1681					pos, tlv_end - pos);
1682			break;
1683#endif /* CONFIG_WIFI_DISPLAY */
1684		default:
1685			wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
1686				   "protocol %u", srv_proto);
1687			wpas_sd_add_proto_not_avail(resp, srv_proto,
1688						    srv_trans_id);
1689			break;
1690		}
1691
1692		pos = tlv_end;
1693	}
1694
1695done:
1696	wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
1697				   update_indic, tlvs, tlvs_len);
1698
1699	wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
1700
1701	wpabuf_free(resp);
1702}
1703
1704
1705void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
1706		      const u8 *tlvs, size_t tlvs_len)
1707{
1708	struct wpa_supplicant *wpa_s = ctx;
1709	const u8 *pos = tlvs;
1710	const u8 *end = tlvs + tlvs_len;
1711	const u8 *tlv_end;
1712	u16 slen;
1713	size_t buf_len;
1714	char *buf;
1715
1716	wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
1717		    tlvs, tlvs_len);
1718	if (tlvs_len > 1500) {
1719		/* TODO: better way for handling this */
1720		wpa_msg_ctrl(wpa_s, MSG_INFO,
1721			     P2P_EVENT_SERV_DISC_RESP MACSTR
1722			     " %u <long response: %u bytes>",
1723			     MAC2STR(sa), update_indic,
1724			     (unsigned int) tlvs_len);
1725	} else {
1726		buf_len = 2 * tlvs_len + 1;
1727		buf = os_malloc(buf_len);
1728		if (buf) {
1729			wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
1730			wpa_msg_ctrl(wpa_s, MSG_INFO,
1731				     P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
1732				     MAC2STR(sa), update_indic, buf);
1733			os_free(buf);
1734		}
1735	}
1736
1737	while (pos < end) {
1738		u8 srv_proto, srv_trans_id, status;
1739
1740		wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
1741		slen = WPA_GET_LE16(pos);
1742		pos += 2;
1743		if (pos + slen > end || slen < 3) {
1744			wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
1745				   "length");
1746			return;
1747		}
1748		tlv_end = pos + slen;
1749
1750		srv_proto = *pos++;
1751		wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
1752			   srv_proto);
1753		srv_trans_id = *pos++;
1754		wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
1755			   srv_trans_id);
1756		status = *pos++;
1757		wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
1758			   status);
1759
1760		wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
1761			    pos, tlv_end - pos);
1762
1763		pos = tlv_end;
1764	}
1765
1766	wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
1767}
1768
1769
1770u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
1771			const struct wpabuf *tlvs)
1772{
1773	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1774		return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
1775	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1776		return 0;
1777	return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
1778}
1779
1780
1781u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
1782			     u8 version, const char *query)
1783{
1784	struct wpabuf *tlvs;
1785	u64 ret;
1786
1787	tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
1788	if (tlvs == NULL)
1789		return 0;
1790	wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
1791	wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
1792	wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
1793	wpabuf_put_u8(tlvs, version);
1794	wpabuf_put_str(tlvs, query);
1795	ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
1796	wpabuf_free(tlvs);
1797	return ret;
1798}
1799
1800
1801#ifdef CONFIG_WIFI_DISPLAY
1802
1803static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
1804				   const struct wpabuf *tlvs)
1805{
1806	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1807		return 0;
1808	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1809		return 0;
1810	return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
1811}
1812
1813
1814#define MAX_WFD_SD_SUBELEMS 20
1815
1816static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
1817				const char *subelems)
1818{
1819	u8 *len;
1820	const char *pos;
1821	int val;
1822	int count = 0;
1823
1824	len = wpabuf_put(tlvs, 2);
1825	wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
1826	wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
1827
1828	wpabuf_put_u8(tlvs, role);
1829
1830	pos = subelems;
1831	while (*pos) {
1832		val = atoi(pos);
1833		if (val >= 0 && val < 256) {
1834			wpabuf_put_u8(tlvs, val);
1835			count++;
1836			if (count == MAX_WFD_SD_SUBELEMS)
1837				break;
1838		}
1839		pos = os_strchr(pos + 1, ',');
1840		if (pos == NULL)
1841			break;
1842		pos++;
1843	}
1844
1845	WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
1846}
1847
1848
1849u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
1850				     const u8 *dst, const char *role)
1851{
1852	struct wpabuf *tlvs;
1853	u64 ret;
1854	const char *subelems;
1855	u8 id = 1;
1856
1857	subelems = os_strchr(role, ' ');
1858	if (subelems == NULL)
1859		return 0;
1860	subelems++;
1861
1862	tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
1863	if (tlvs == NULL)
1864		return 0;
1865
1866	if (os_strstr(role, "[source]"))
1867		wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
1868	if (os_strstr(role, "[pri-sink]"))
1869		wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
1870	if (os_strstr(role, "[sec-sink]"))
1871		wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
1872	if (os_strstr(role, "[source+sink]"))
1873		wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
1874
1875	ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
1876	wpabuf_free(tlvs);
1877	return ret;
1878}
1879
1880#endif /* CONFIG_WIFI_DISPLAY */
1881
1882
1883int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
1884{
1885	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1886		return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
1887	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1888		return -1;
1889	return p2p_sd_cancel_request(wpa_s->global->p2p,
1890				     (void *) (uintptr_t) req);
1891}
1892
1893
1894void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
1895			  const u8 *dst, u8 dialog_token,
1896			  const struct wpabuf *resp_tlvs)
1897{
1898	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1899		wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
1900					resp_tlvs);
1901		return;
1902	}
1903	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
1904		return;
1905	p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
1906			resp_tlvs);
1907}
1908
1909#ifdef ANDROID_P2P
1910void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s, int action)
1911#else
1912void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
1913#endif
1914{
1915	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
1916		wpa_drv_p2p_service_update(wpa_s);
1917		return;
1918	}
1919	if (wpa_s->global->p2p)
1920#ifdef ANDROID_P2P
1921		p2p_sd_service_update(wpa_s->global->p2p, action);
1922#else
1923		p2p_sd_service_update(wpa_s->global->p2p);
1924#endif
1925}
1926
1927
1928static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
1929{
1930	dl_list_del(&bsrv->list);
1931	wpabuf_free(bsrv->query);
1932	wpabuf_free(bsrv->resp);
1933	os_free(bsrv);
1934}
1935
1936
1937static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
1938{
1939	dl_list_del(&usrv->list);
1940	os_free(usrv->service);
1941	os_free(usrv);
1942}
1943
1944
1945void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
1946{
1947	struct p2p_srv_bonjour *bsrv, *bn;
1948	struct p2p_srv_upnp *usrv, *un;
1949
1950	dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
1951			      struct p2p_srv_bonjour, list)
1952		wpas_p2p_srv_bonjour_free(bsrv);
1953
1954	dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
1955			      struct p2p_srv_upnp, list)
1956		wpas_p2p_srv_upnp_free(usrv);
1957
1958#ifdef ANDROID_P2P
1959	wpas_p2p_sd_service_update(wpa_s, SRV_FLUSH);
1960#else
1961	wpas_p2p_sd_service_update(wpa_s);
1962#endif
1963}
1964
1965
1966int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
1967				 struct wpabuf *query, struct wpabuf *resp)
1968{
1969	struct p2p_srv_bonjour *bsrv;
1970
1971	bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
1972	if (bsrv) {
1973		wpabuf_free(query);
1974		wpabuf_free(bsrv->resp);
1975		bsrv->resp = resp;
1976		return 0;
1977	}
1978
1979	bsrv = os_zalloc(sizeof(*bsrv));
1980	if (bsrv == NULL)
1981		return -1;
1982	bsrv->query = query;
1983	bsrv->resp = resp;
1984	dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
1985
1986#ifdef ANDROID_P2P
1987	wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
1988#else
1989	wpas_p2p_sd_service_update(wpa_s);
1990#endif
1991	return 0;
1992}
1993
1994
1995int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
1996				 const struct wpabuf *query)
1997{
1998	struct p2p_srv_bonjour *bsrv;
1999
2000	bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2001	if (bsrv == NULL)
2002		return -1;
2003	wpas_p2p_srv_bonjour_free(bsrv);
2004#ifdef ANDROID_P2P
2005	wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2006#else
2007	wpas_p2p_sd_service_update(wpa_s);
2008#endif
2009	return 0;
2010}
2011
2012
2013int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2014			      const char *service)
2015{
2016	struct p2p_srv_upnp *usrv;
2017
2018	if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2019		return 0; /* Already listed */
2020	usrv = os_zalloc(sizeof(*usrv));
2021	if (usrv == NULL)
2022		return -1;
2023	usrv->version = version;
2024	usrv->service = os_strdup(service);
2025	if (usrv->service == NULL) {
2026		os_free(usrv);
2027		return -1;
2028	}
2029	dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2030
2031#ifdef ANDROID_P2P
2032	wpas_p2p_sd_service_update(wpa_s, SRV_ADD);
2033#else
2034	wpas_p2p_sd_service_update(wpa_s);
2035#endif
2036	return 0;
2037}
2038
2039
2040int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2041			      const char *service)
2042{
2043	struct p2p_srv_upnp *usrv;
2044
2045	usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2046	if (usrv == NULL)
2047		return -1;
2048	wpas_p2p_srv_upnp_free(usrv);
2049#ifdef ANDROID_P2P
2050	wpas_p2p_sd_service_update(wpa_s, SRV_DEL);
2051#else
2052	wpas_p2p_sd_service_update(wpa_s);
2053#endif
2054	return 0;
2055}
2056
2057
2058static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2059					 const u8 *peer, const char *params,
2060					 unsigned int generated_pin)
2061{
2062	wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR " %08d%s",
2063		MAC2STR(peer), generated_pin, params);
2064}
2065
2066
2067static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2068					const u8 *peer, const char *params)
2069{
2070	wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR "%s",
2071		MAC2STR(peer), params);
2072}
2073
2074
2075void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2076			const u8 *dev_addr, const u8 *pri_dev_type,
2077			const char *dev_name, u16 supp_config_methods,
2078			u8 dev_capab, u8 group_capab, const u8 *group_id,
2079			size_t group_id_len)
2080{
2081	struct wpa_supplicant *wpa_s = ctx;
2082	char devtype[WPS_DEV_TYPE_BUFSIZE];
2083	char params[300];
2084	u8 empty_dev_type[8];
2085	unsigned int generated_pin = 0;
2086	struct wpa_supplicant *group = NULL;
2087
2088	if (group_id) {
2089		for (group = wpa_s->global->ifaces; group; group = group->next)
2090		{
2091			struct wpa_ssid *s = group->current_ssid;
2092			if (s != NULL &&
2093			    s->mode == WPAS_MODE_P2P_GO &&
2094			    group_id_len - ETH_ALEN == s->ssid_len &&
2095			    os_memcmp(group_id + ETH_ALEN, s->ssid,
2096				      s->ssid_len) == 0)
2097				break;
2098		}
2099	}
2100
2101	if (pri_dev_type == NULL) {
2102		os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2103		pri_dev_type = empty_dev_type;
2104	}
2105	os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2106		    " pri_dev_type=%s name='%s' config_methods=0x%x "
2107		    "dev_capab=0x%x group_capab=0x%x%s%s",
2108		    MAC2STR(dev_addr),
2109		    wps_dev_type_bin2str(pri_dev_type, devtype,
2110					 sizeof(devtype)),
2111		    dev_name, supp_config_methods, dev_capab, group_capab,
2112		    group ? " group=" : "",
2113		    group ? group->ifname : "");
2114	params[sizeof(params) - 1] = '\0';
2115
2116	if (config_methods & WPS_CONFIG_DISPLAY) {
2117		generated_pin = wps_generate_pin();
2118		wpas_prov_disc_local_display(wpa_s, peer, params,
2119					     generated_pin);
2120	} else if (config_methods & WPS_CONFIG_KEYPAD)
2121		wpas_prov_disc_local_keypad(wpa_s, peer, params);
2122	else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2123		wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ MACSTR
2124			"%s", MAC2STR(peer), params);
2125
2126	wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2127					    P2P_PROV_DISC_SUCCESS,
2128					    config_methods, generated_pin);
2129}
2130
2131
2132void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2133{
2134	struct wpa_supplicant *wpa_s = ctx;
2135	unsigned int generated_pin = 0;
2136	char params[20];
2137
2138	if (wpa_s->pending_pd_before_join &&
2139	    (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2140	     os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2141		wpa_s->pending_pd_before_join = 0;
2142		wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2143			   "join-existing-group operation");
2144		wpas_p2p_join_start(wpa_s);
2145		return;
2146	}
2147
2148	if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2149	    wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2150		os_snprintf(params, sizeof(params), " peer_go=%d",
2151			    wpa_s->pending_pd_use == AUTO_PD_JOIN);
2152	else
2153		params[0] = '\0';
2154
2155	if (config_methods & WPS_CONFIG_DISPLAY)
2156		wpas_prov_disc_local_keypad(wpa_s, peer, params);
2157	else if (config_methods & WPS_CONFIG_KEYPAD) {
2158		generated_pin = wps_generate_pin();
2159		wpas_prov_disc_local_display(wpa_s, peer, params,
2160					     generated_pin);
2161	} else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2162		wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP MACSTR
2163			"%s", MAC2STR(peer), params);
2164
2165	wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2166					    P2P_PROV_DISC_SUCCESS,
2167					    config_methods, generated_pin);
2168}
2169
2170
2171static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2172				enum p2p_prov_disc_status status)
2173{
2174	struct wpa_supplicant *wpa_s = ctx;
2175
2176	if (wpa_s->p2p_fallback_to_go_neg) {
2177		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2178			"failed - fall back to GO Negotiation");
2179		wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2180		return;
2181	}
2182
2183	wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2184		" p2p_dev_addr=" MACSTR " status=%d",
2185		MAC2STR(peer), status);
2186
2187	wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2188					    status, 0, 0);
2189}
2190
2191
2192static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2193				  const u8 *go_dev_addr, const u8 *ssid,
2194				  size_t ssid_len, int *go, u8 *group_bssid,
2195				  int *force_freq, int persistent_group)
2196{
2197	struct wpa_supplicant *wpa_s = ctx;
2198	struct wpa_ssid *s;
2199	u8 cur_bssid[ETH_ALEN];
2200	int res;
2201	struct wpa_supplicant *grp;
2202
2203	if (!persistent_group) {
2204		wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2205			   " to join an active group", MAC2STR(sa));
2206		if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2207		    (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2208		     == 0 ||
2209		     os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2210			wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2211				   "authorized invitation");
2212			goto accept_inv;
2213		}
2214		/*
2215		 * Do not accept the invitation automatically; notify user and
2216		 * request approval.
2217		 */
2218		return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2219	}
2220
2221	grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2222	if (grp) {
2223		wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2224			   "running persistent group");
2225		if (*go)
2226			os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2227		goto accept_inv;
2228	}
2229
2230	if (!wpa_s->conf->persistent_reconnect)
2231		return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2232
2233	for (s = wpa_s->conf->ssid; s; s = s->next) {
2234		if (s->disabled == 2 &&
2235		    os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2236		    s->ssid_len == ssid_len &&
2237		    os_memcmp(ssid, s->ssid, ssid_len) == 0)
2238			break;
2239	}
2240
2241	if (!s) {
2242		wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2243			   " requested reinvocation of an unknown group",
2244			   MAC2STR(sa));
2245		return P2P_SC_FAIL_UNKNOWN_GROUP;
2246	}
2247
2248	if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2249		*go = 1;
2250		if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2251			wpa_printf(MSG_DEBUG, "P2P: The only available "
2252				   "interface is already in use - reject "
2253				   "invitation");
2254			return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2255		}
2256		os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2257	} else if (s->mode == WPAS_MODE_P2P_GO) {
2258		*go = 1;
2259		if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2260		{
2261			wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2262				   "interface address for the group");
2263			return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2264		}
2265		os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2266			  ETH_ALEN);
2267	}
2268
2269accept_inv:
2270	if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, cur_bssid) == 0 &&
2271	    wpa_s->assoc_freq) {
2272		wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2273			   "the channel we are already using");
2274		*force_freq = wpa_s->assoc_freq;
2275	}
2276
2277	res = wpa_drv_shared_freq(wpa_s);
2278	if (res > 0) {
2279		wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match "
2280			   "with the channel we are already using on a "
2281			   "shared interface");
2282		*force_freq = res;
2283	}
2284
2285	return P2P_SC_SUCCESS;
2286}
2287
2288
2289static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2290				     const u8 *ssid, size_t ssid_len,
2291				     const u8 *go_dev_addr, u8 status,
2292				     int op_freq)
2293{
2294	struct wpa_supplicant *wpa_s = ctx;
2295	struct wpa_ssid *s;
2296
2297	for (s = wpa_s->conf->ssid; s; s = s->next) {
2298		if (s->disabled == 2 &&
2299		    s->ssid_len == ssid_len &&
2300		    os_memcmp(ssid, s->ssid, ssid_len) == 0)
2301			break;
2302	}
2303
2304	if (status == P2P_SC_SUCCESS) {
2305		wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2306			   " was accepted; op_freq=%d MHz",
2307			   MAC2STR(sa), op_freq);
2308		if (s) {
2309			wpas_p2p_group_add_persistent(
2310				wpa_s, s, s->mode == WPAS_MODE_P2P_GO, 0, 0);
2311		} else if (bssid) {
2312			wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2313				      wpa_s->p2p_wps_method, 0);
2314		}
2315		return;
2316	}
2317
2318	if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2319		wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2320			   " was rejected (status %u)", MAC2STR(sa), status);
2321		return;
2322	}
2323
2324	if (!s) {
2325		if (bssid) {
2326			wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2327				"sa=" MACSTR " go_dev_addr=" MACSTR
2328				" bssid=" MACSTR " unknown-network",
2329				MAC2STR(sa), MAC2STR(go_dev_addr),
2330				MAC2STR(bssid));
2331		} else {
2332			wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2333				"sa=" MACSTR " go_dev_addr=" MACSTR
2334				" unknown-network",
2335				MAC2STR(sa), MAC2STR(go_dev_addr));
2336		}
2337		return;
2338	}
2339
2340	wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED "sa=" MACSTR
2341		" persistent=%d", MAC2STR(sa), s->id);
2342}
2343
2344
2345static void wpas_invitation_result(void *ctx, int status, const u8 *bssid)
2346{
2347	struct wpa_supplicant *wpa_s = ctx;
2348	struct wpa_ssid *ssid;
2349
2350	if (bssid) {
2351		wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2352			"status=%d " MACSTR,
2353			status, MAC2STR(bssid));
2354	} else {
2355		wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2356			"status=%d ", status);
2357	}
2358	wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2359
2360	if (wpa_s->pending_invite_ssid_id == -1)
2361		return; /* Invitation to active group */
2362
2363	if (status != P2P_SC_SUCCESS) {
2364		wpas_p2p_remove_pending_group_interface(wpa_s);
2365		return;
2366	}
2367
2368	ssid = wpa_config_get_network(wpa_s->conf,
2369				      wpa_s->pending_invite_ssid_id);
2370	if (ssid == NULL) {
2371		wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2372			   "data matching with invitation");
2373		return;
2374	}
2375
2376	wpas_p2p_group_add_persistent(wpa_s, ssid,
2377				      ssid->mode == WPAS_MODE_P2P_GO, 0, 0);
2378}
2379
2380
2381static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2382				    unsigned int freq)
2383{
2384	unsigned int i;
2385
2386	if (global->p2p_disallow_freq == NULL)
2387		return 0;
2388
2389	for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2390		if (freq >= global->p2p_disallow_freq[i].min &&
2391		    freq <= global->p2p_disallow_freq[i].max)
2392			return 1;
2393	}
2394
2395	return 0;
2396}
2397
2398
2399static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2400{
2401	reg->channel[reg->channels] = chan;
2402	reg->channels++;
2403}
2404
2405
2406static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2407				     struct p2p_channels *chan)
2408{
2409	int i, cla = 0;
2410
2411	wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2412		   "band");
2413
2414	/* Operating class 81 - 2.4 GHz band channels 1..13 */
2415	chan->reg_class[cla].reg_class = 81;
2416	chan->reg_class[cla].channels = 0;
2417	for (i = 0; i < 11; i++) {
2418		if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2419			wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2420	}
2421	if (chan->reg_class[cla].channels)
2422		cla++;
2423
2424	wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2425		   "band");
2426
2427	/* Operating class 115 - 5 GHz, channels 36-48 */
2428	chan->reg_class[cla].reg_class = 115;
2429	chan->reg_class[cla].channels = 0;
2430	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2431		wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2432	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2433		wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2434	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2435		wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2436	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2437		wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2438	if (chan->reg_class[cla].channels)
2439		cla++;
2440
2441	wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2442		   "band");
2443
2444	/* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2445	chan->reg_class[cla].reg_class = 124;
2446	chan->reg_class[cla].channels = 0;
2447	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2448		wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2449	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2450		wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2451	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2452		wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2453	if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2454		wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2455	if (chan->reg_class[cla].channels)
2456		cla++;
2457
2458	chan->reg_classes = cla;
2459	return 0;
2460}
2461
2462
2463static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2464					  u16 num_modes,
2465					  enum hostapd_hw_mode mode)
2466{
2467	u16 i;
2468
2469	for (i = 0; i < num_modes; i++) {
2470		if (modes[i].mode == mode)
2471			return &modes[i];
2472	}
2473
2474	return NULL;
2475}
2476
2477
2478static int has_channel(struct wpa_global *global,
2479		       struct hostapd_hw_modes *mode, u8 chan, int *flags)
2480{
2481	int i;
2482	unsigned int freq;
2483
2484	freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
2485		chan * 5;
2486	if (wpas_p2p_disallowed_freq(global, freq))
2487		return 0;
2488
2489	for (i = 0; i < mode->num_channels; i++) {
2490		if (mode->channels[i].chan == chan) {
2491			if (flags)
2492				*flags = mode->channels[i].flag;
2493			return !(mode->channels[i].flag &
2494				 (HOSTAPD_CHAN_DISABLED |
2495				  HOSTAPD_CHAN_PASSIVE_SCAN |
2496				  HOSTAPD_CHAN_NO_IBSS |
2497				  HOSTAPD_CHAN_RADAR));
2498		}
2499	}
2500
2501	return 0;
2502}
2503
2504
2505struct p2p_oper_class_map {
2506	enum hostapd_hw_mode mode;
2507	u8 op_class;
2508	u8 min_chan;
2509	u8 max_chan;
2510	u8 inc;
2511	enum { BW20, BW40PLUS, BW40MINUS } bw;
2512};
2513
2514static struct p2p_oper_class_map op_class[] = {
2515	{ HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
2516	{ HOSTAPD_MODE_IEEE80211G, 82, 14, 14, 1, BW20 },
2517#if 0 /* Do not enable HT40 on 2 GHz for now */
2518	{ HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
2519	{ HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
2520#endif
2521	{ HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
2522	{ HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
2523	{ HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
2524	{ HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
2525	{ HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
2526	{ HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
2527	{ -1, 0, 0, 0, 0, BW20 }
2528};
2529
2530
2531static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
2532				   struct hostapd_hw_modes *mode,
2533				   u8 channel, u8 bw)
2534{
2535	int flag;
2536
2537	if (!has_channel(wpa_s->global, mode, channel, &flag))
2538		return -1;
2539	if (bw == BW40MINUS &&
2540	    (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
2541	     !has_channel(wpa_s->global, mode, channel - 4, NULL)))
2542		return 0;
2543	if (bw == BW40PLUS &&
2544	    (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
2545	     !has_channel(wpa_s->global, mode, channel + 4, NULL)))
2546		return 0;
2547	return 1;
2548}
2549
2550
2551static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
2552				   struct p2p_channels *chan)
2553{
2554	struct hostapd_hw_modes *mode;
2555	int cla, op;
2556
2557	if (wpa_s->hw.modes == NULL) {
2558		wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
2559			   "of all supported channels; assume dualband "
2560			   "support");
2561		return wpas_p2p_default_channels(wpa_s, chan);
2562	}
2563
2564	cla = 0;
2565
2566	for (op = 0; op_class[op].op_class; op++) {
2567		struct p2p_oper_class_map *o = &op_class[op];
2568		u8 ch;
2569		struct p2p_reg_class *reg = NULL;
2570
2571		mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
2572		if (mode == NULL)
2573			continue;
2574		for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2575			if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
2576				continue;
2577			if (reg == NULL) {
2578				wpa_printf(MSG_DEBUG, "P2P: Add operating "
2579					   "class %u", o->op_class);
2580				reg = &chan->reg_class[cla];
2581				cla++;
2582				reg->reg_class = o->op_class;
2583			}
2584			reg->channel[reg->channels] = ch;
2585			reg->channels++;
2586		}
2587		if (reg) {
2588			wpa_hexdump(MSG_DEBUG, "P2P: Channels",
2589				    reg->channel, reg->channels);
2590		}
2591	}
2592
2593	chan->reg_classes = cla;
2594
2595	return 0;
2596}
2597
2598
2599int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
2600			   struct hostapd_hw_modes *mode, u8 channel)
2601{
2602	int op, ret;
2603
2604	for (op = 0; op_class[op].op_class; op++) {
2605		struct p2p_oper_class_map *o = &op_class[op];
2606		u8 ch;
2607
2608		for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
2609			if (o->mode != HOSTAPD_MODE_IEEE80211A ||
2610			    o->bw == BW20 || ch != channel)
2611				continue;
2612			ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
2613			if (ret < 0)
2614				continue;
2615			else if (ret > 0)
2616				return (o->bw == BW40MINUS) ? -1 : 1;
2617			else
2618				return 0;
2619		}
2620	}
2621	return 0;
2622}
2623
2624
2625static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
2626			size_t buf_len)
2627{
2628	struct wpa_supplicant *wpa_s = ctx;
2629
2630	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2631		if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
2632			break;
2633	}
2634	if (wpa_s == NULL)
2635		return -1;
2636
2637	return wpa_drv_get_noa(wpa_s, buf, buf_len);
2638}
2639
2640
2641static int wpas_go_connected(void *ctx, const u8 *dev_addr)
2642{
2643	struct wpa_supplicant *wpa_s = ctx;
2644
2645	for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2646		struct wpa_ssid *ssid = wpa_s->current_ssid;
2647		if (ssid == NULL)
2648			continue;
2649		if (ssid->mode != WPAS_MODE_INFRA)
2650			continue;
2651		if (wpa_s->wpa_state != WPA_COMPLETED &&
2652		    wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
2653			continue;
2654		if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
2655			return 1;
2656	}
2657
2658	return 0;
2659}
2660
2661
2662/**
2663 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
2664 * @global: Pointer to global data from wpa_supplicant_init()
2665 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2666 * Returns: 0 on success, -1 on failure
2667 */
2668int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
2669{
2670	struct p2p_config p2p;
2671	unsigned int r;
2672	int i;
2673
2674	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
2675		return 0;
2676
2677	if (global->p2p)
2678		return 0;
2679
2680	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2681		struct p2p_params params;
2682
2683		wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
2684		os_memset(&params, 0, sizeof(params));
2685		params.dev_name = wpa_s->conf->device_name;
2686		os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
2687			  WPS_DEV_TYPE_LEN);
2688		params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2689		os_memcpy(params.sec_dev_type,
2690			  wpa_s->conf->sec_device_type,
2691			  params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2692
2693		if (wpa_drv_p2p_set_params(wpa_s, &params) < 0)
2694			return -1;
2695
2696		return 0;
2697	}
2698
2699	os_memset(&p2p, 0, sizeof(p2p));
2700	p2p.msg_ctx = wpa_s;
2701	p2p.cb_ctx = wpa_s;
2702	p2p.p2p_scan = wpas_p2p_scan;
2703	p2p.send_action = wpas_send_action;
2704	p2p.send_action_done = wpas_send_action_done;
2705	p2p.go_neg_completed = wpas_go_neg_completed;
2706	p2p.go_neg_req_rx = wpas_go_neg_req_rx;
2707	p2p.dev_found = wpas_dev_found;
2708	p2p.dev_lost = wpas_dev_lost;
2709	p2p.start_listen = wpas_start_listen;
2710	p2p.stop_listen = wpas_stop_listen;
2711	p2p.send_probe_resp = wpas_send_probe_resp;
2712	p2p.sd_request = wpas_sd_request;
2713	p2p.sd_response = wpas_sd_response;
2714	p2p.prov_disc_req = wpas_prov_disc_req;
2715	p2p.prov_disc_resp = wpas_prov_disc_resp;
2716	p2p.prov_disc_fail = wpas_prov_disc_fail;
2717	p2p.invitation_process = wpas_invitation_process;
2718	p2p.invitation_received = wpas_invitation_received;
2719	p2p.invitation_result = wpas_invitation_result;
2720	p2p.get_noa = wpas_get_noa;
2721	p2p.go_connected = wpas_go_connected;
2722
2723	os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
2724	os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
2725	p2p.dev_name = wpa_s->conf->device_name;
2726	p2p.manufacturer = wpa_s->conf->manufacturer;
2727	p2p.model_name = wpa_s->conf->model_name;
2728	p2p.model_number = wpa_s->conf->model_number;
2729	p2p.serial_number = wpa_s->conf->serial_number;
2730	if (wpa_s->wps) {
2731		os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
2732		p2p.config_methods = wpa_s->wps->config_methods;
2733	}
2734
2735	if (wpa_s->conf->p2p_listen_reg_class &&
2736	    wpa_s->conf->p2p_listen_channel) {
2737		p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
2738		p2p.channel = wpa_s->conf->p2p_listen_channel;
2739	} else {
2740		p2p.reg_class = 81;
2741		/*
2742		 * Pick one of the social channels randomly as the listen
2743		 * channel.
2744		 */
2745		os_get_random((u8 *) &r, sizeof(r));
2746		p2p.channel = 1 + (r % 3) * 5;
2747	}
2748	wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
2749
2750	if (wpa_s->conf->p2p_oper_reg_class &&
2751	    wpa_s->conf->p2p_oper_channel) {
2752		p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
2753		p2p.op_channel = wpa_s->conf->p2p_oper_channel;
2754		p2p.cfg_op_channel = 1;
2755		wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
2756			   "%d:%d", p2p.op_reg_class, p2p.op_channel);
2757
2758	} else {
2759		p2p.op_reg_class = 81;
2760		/*
2761		 * Use random operation channel from (1, 6, 11) if no other
2762		 * preference is indicated.
2763		 */
2764		os_get_random((u8 *) &r, sizeof(r));
2765		p2p.op_channel = 1 + (r % 3) * 5;
2766		p2p.cfg_op_channel = 0;
2767		wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
2768			   "%d:%d", p2p.op_reg_class, p2p.op_channel);
2769	}
2770	if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
2771		os_memcpy(p2p.country, wpa_s->conf->country, 2);
2772		p2p.country[2] = 0x04;
2773	} else
2774		os_memcpy(p2p.country, "XX\x04", 3);
2775
2776	if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
2777		wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
2778			   "channel list");
2779		return -1;
2780	}
2781
2782	os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
2783		  WPS_DEV_TYPE_LEN);
2784
2785	p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
2786	os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
2787		  p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
2788
2789	p2p.concurrent_operations = !!(wpa_s->drv_flags &
2790				       WPA_DRIVER_FLAGS_P2P_CONCURRENT);
2791
2792	p2p.max_peers = 100;
2793
2794	if (wpa_s->conf->p2p_ssid_postfix) {
2795		p2p.ssid_postfix_len =
2796			os_strlen(wpa_s->conf->p2p_ssid_postfix);
2797		if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
2798			p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
2799		os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
2800			  p2p.ssid_postfix_len);
2801	}
2802
2803	p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
2804
2805#ifdef ANDROID_P2P
2806	if(wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
2807		p2p.p2p_concurrency = P2P_MULTI_CHANNEL_CONCURRENT;
2808	else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT)
2809		p2p.p2p_concurrency = P2P_SINGLE_CHANNEL_CONCURRENT;
2810#endif
2811
2812	global->p2p = p2p_init(&p2p);
2813	if (global->p2p == NULL)
2814		return -1;
2815	global->p2p_init_wpa_s = wpa_s;
2816
2817	for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
2818		if (wpa_s->conf->wps_vendor_ext[i] == NULL)
2819			continue;
2820		p2p_add_wps_vendor_extension(
2821			global->p2p, wpa_s->conf->wps_vendor_ext[i]);
2822	}
2823
2824	return 0;
2825}
2826
2827
2828/**
2829 * wpas_p2p_deinit - Deinitialize per-interface P2P data
2830 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
2831 *
2832 * This function deinitialize per-interface P2P data.
2833 */
2834void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
2835{
2836	if (wpa_s->driver && wpa_s->drv_priv)
2837		wpa_drv_probe_req_report(wpa_s, 0);
2838
2839	if (wpa_s->go_params) {
2840		/* Clear any stored provisioning info */
2841		p2p_clear_provisioning_info(
2842			wpa_s->global->p2p,
2843			wpa_s->go_params->peer_device_addr);
2844	}
2845
2846	os_free(wpa_s->go_params);
2847	wpa_s->go_params = NULL;
2848	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
2849	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2850	eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
2851	wpa_s->p2p_long_listen = 0;
2852	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
2853	eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
2854	wpas_p2p_remove_pending_group_interface(wpa_s);
2855
2856	/* TODO: remove group interface from the driver if this wpa_s instance
2857	 * is on top of a P2P group interface */
2858}
2859
2860
2861/**
2862 * wpas_p2p_deinit_global - Deinitialize global P2P module
2863 * @global: Pointer to global data from wpa_supplicant_init()
2864 *
2865 * This function deinitializes the global (per device) P2P module.
2866 */
2867void wpas_p2p_deinit_global(struct wpa_global *global)
2868{
2869	struct wpa_supplicant *wpa_s, *tmp;
2870
2871	wpa_s = global->ifaces;
2872	if (wpa_s)
2873		wpas_p2p_service_flush(wpa_s);
2874
2875	if (global->p2p == NULL)
2876		return;
2877
2878	/* Remove remaining P2P group interfaces */
2879	while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
2880		wpa_s = wpa_s->next;
2881	while (wpa_s) {
2882		tmp = global->ifaces;
2883		while (tmp &&
2884		       (tmp == wpa_s ||
2885			tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
2886			tmp = tmp->next;
2887		}
2888		if (tmp == NULL)
2889			break;
2890		/* Disconnect from the P2P group and deinit the interface */
2891		wpas_p2p_disconnect(tmp);
2892	}
2893
2894	/*
2895	 * Deinit GO data on any possibly remaining interface (if main
2896	 * interface is used as GO).
2897	 */
2898	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
2899		if (wpa_s->ap_iface)
2900			wpas_p2p_group_deinit(wpa_s);
2901	}
2902
2903	p2p_deinit(global->p2p);
2904	global->p2p = NULL;
2905	global->p2p_init_wpa_s = NULL;
2906}
2907
2908
2909static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
2910{
2911	if (wpa_s->drv_flags &
2912	    (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
2913	     WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
2914		return 1; /* P2P group requires a new interface in every case
2915			   */
2916	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
2917		return 0; /* driver does not support concurrent operations */
2918	if (wpa_s->global->ifaces->next)
2919		return 1; /* more that one interface already in use */
2920	if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
2921		return 1; /* this interface is already in use */
2922	return 0;
2923}
2924
2925
2926static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
2927				 const u8 *peer_addr,
2928				 enum p2p_wps_method wps_method,
2929				 int go_intent, const u8 *own_interface_addr,
2930				 unsigned int force_freq, int persistent_group,
2931				 struct wpa_ssid *ssid)
2932{
2933	if (persistent_group && wpa_s->conf->persistent_reconnect)
2934		persistent_group = 2;
2935
2936	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2937		return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
2938					   go_intent, own_interface_addr,
2939					   force_freq, persistent_group);
2940	}
2941
2942	/*
2943	 * Increase GO config timeout if HT40 is used since it takes some time
2944	 * to scan channels for coex purposes before the BSS can be started.
2945	 */
2946	p2p_set_config_timeout(wpa_s->global->p2p,
2947			       wpa_s->p2p_go_ht40 ? 255 : 100, 20);
2948
2949	return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
2950			   go_intent, own_interface_addr, force_freq,
2951			   persistent_group, ssid ? ssid->ssid : NULL,
2952			   ssid ? ssid->ssid_len : 0,
2953			   wpa_s->p2p_pd_before_go_neg);
2954}
2955
2956
2957static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
2958				const u8 *peer_addr,
2959				enum p2p_wps_method wps_method,
2960				int go_intent, const u8 *own_interface_addr,
2961				unsigned int force_freq, int persistent_group,
2962				struct wpa_ssid *ssid)
2963{
2964	if (persistent_group && wpa_s->conf->persistent_reconnect)
2965		persistent_group = 2;
2966
2967	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2968		return -1;
2969
2970	return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
2971			     go_intent, own_interface_addr, force_freq,
2972			     persistent_group, ssid ? ssid->ssid : NULL,
2973			     ssid ? ssid->ssid_len : 0);
2974}
2975
2976
2977static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
2978{
2979	wpa_s->p2p_join_scan_count++;
2980	wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
2981		   wpa_s->p2p_join_scan_count);
2982	if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
2983		wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
2984			   " for join operationg - stop join attempt",
2985			   MAC2STR(wpa_s->pending_join_iface_addr));
2986		eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
2987		if (wpa_s->p2p_auto_pd) {
2988			wpa_s->p2p_auto_pd = 0;
2989			wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2990				" p2p_dev_addr=" MACSTR " status=N/A",
2991				MAC2STR(wpa_s->pending_join_dev_addr));
2992			return;
2993		}
2994		wpa_msg(wpa_s->parent, MSG_INFO,
2995			P2P_EVENT_GROUP_FORMATION_FAILURE);
2996	}
2997}
2998
2999
3000static void wpas_p2p_pd_before_join_timeout(void *eloop_ctx, void *timeout_ctx)
3001{
3002	struct wpa_supplicant *wpa_s = eloop_ctx;
3003	if (!wpa_s->pending_pd_before_join)
3004		return;
3005	/*
3006	 * Provision Discovery Response may have been lost - try to connect
3007	 * anyway since we do not need any information from this PD.
3008	 */
3009	wpa_printf(MSG_DEBUG, "P2P: PD timeout for join-existing-group - "
3010		   "try to connect anyway");
3011	wpas_p2p_join_start(wpa_s);
3012}
3013
3014
3015static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3016{
3017	struct wpa_supplicant *iface;
3018	int shared_freq;
3019	u8 bssid[ETH_ALEN];
3020
3021	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)
3022		return 0;
3023
3024	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
3025		if (!wpas_p2p_create_iface(wpa_s) && iface == wpa_s)
3026			continue;
3027		if (iface->current_ssid == NULL || iface->assoc_freq == 0)
3028			continue;
3029		if (iface->current_ssid->mode == WPAS_MODE_AP ||
3030		    iface->current_ssid->mode == WPAS_MODE_P2P_GO)
3031			shared_freq = iface->current_ssid->frequency;
3032		else if (wpa_drv_get_bssid(iface, bssid) == 0)
3033			shared_freq = iface->assoc_freq;
3034		else
3035			shared_freq = 0;
3036
3037		if (shared_freq && freq != shared_freq) {
3038			wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - %s "
3039				   "connected on %d MHz - new connection on "
3040				   "%d MHz", iface->ifname, shared_freq, freq);
3041			return 1;
3042		}
3043	}
3044
3045	shared_freq = wpa_drv_shared_freq(wpa_s);
3046	if (shared_freq > 0 && shared_freq != freq) {
3047		wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - shared "
3048			   "virtual interface connected on %d MHz - new "
3049			   "connection on %d MHz", shared_freq, freq);
3050		return 1;
3051	}
3052
3053	return 0;
3054}
3055
3056
3057static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3058			    const u8 *peer_dev_addr)
3059{
3060	struct wpa_bss *bss;
3061	int updated;
3062
3063	bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3064	if (bss == NULL)
3065		return -1;
3066	if (bss->last_update_idx < wpa_s->bss_update_idx) {
3067		wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3068			   "last scan");
3069		return 0;
3070	}
3071
3072	updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3073	wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3074		   "%ld.%06ld (%supdated in last scan)",
3075		   bss->last_update.sec, bss->last_update.usec,
3076		   updated ? "": "not ");
3077
3078	return updated;
3079}
3080
3081
3082static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3083				   struct wpa_scan_results *scan_res)
3084{
3085	struct wpa_bss *bss;
3086	int freq;
3087	u8 iface_addr[ETH_ALEN];
3088
3089	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3090
3091	if (wpa_s->global->p2p_disabled)
3092		return;
3093
3094	wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3095		   scan_res ? (int) scan_res->num : -1,
3096		   wpa_s->p2p_auto_join ? "auto_" : "");
3097
3098	if (scan_res)
3099		wpas_p2p_scan_res_handler(wpa_s, scan_res);
3100
3101	if (wpa_s->p2p_auto_pd) {
3102		int join = wpas_p2p_peer_go(wpa_s,
3103					    wpa_s->pending_join_dev_addr);
3104		if (join == 0 &&
3105		    wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3106			wpa_s->auto_pd_scan_retry++;
3107			bss = wpa_bss_get_bssid(wpa_s,
3108						wpa_s->pending_join_dev_addr);
3109			if (bss) {
3110				freq = bss->freq;
3111				wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3112					   "the peer " MACSTR " at %d MHz",
3113					   wpa_s->auto_pd_scan_retry,
3114					   MAC2STR(wpa_s->
3115						   pending_join_dev_addr),
3116					   freq);
3117				wpas_p2p_join_scan_req(wpa_s, freq);
3118				return;
3119			}
3120		}
3121
3122		if (join < 0)
3123			join = 0;
3124
3125		wpa_s->p2p_auto_pd = 0;
3126		wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3127		wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3128			   MAC2STR(wpa_s->pending_join_dev_addr), join);
3129		if (p2p_prov_disc_req(wpa_s->global->p2p,
3130				      wpa_s->pending_join_dev_addr,
3131				      wpa_s->pending_pd_config_methods, join,
3132				      0) < 0) {
3133			wpa_s->p2p_auto_pd = 0;
3134			wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3135				" p2p_dev_addr=" MACSTR " status=N/A",
3136				MAC2STR(wpa_s->pending_join_dev_addr));
3137		}
3138		return;
3139	}
3140
3141	if (wpa_s->p2p_auto_join) {
3142		int join = wpas_p2p_peer_go(wpa_s,
3143					    wpa_s->pending_join_dev_addr);
3144		if (join < 0) {
3145			wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3146				   "running a GO -> use GO Negotiation");
3147			wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3148					 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3149					 wpa_s->p2p_persistent_group, 0, 0, 0,
3150					 wpa_s->p2p_go_intent,
3151					 wpa_s->p2p_connect_freq,
3152					 wpa_s->p2p_persistent_id,
3153					 wpa_s->p2p_pd_before_go_neg,
3154					 wpa_s->p2p_go_ht40);
3155			return;
3156		}
3157
3158		wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3159			   "try to join the group", join ? "" :
3160			   " in older scan");
3161		if (!join)
3162			wpa_s->p2p_fallback_to_go_neg = 1;
3163	}
3164
3165	freq = p2p_get_oper_freq(wpa_s->global->p2p,
3166				 wpa_s->pending_join_iface_addr);
3167	if (freq < 0 &&
3168	    p2p_get_interface_addr(wpa_s->global->p2p,
3169				   wpa_s->pending_join_dev_addr,
3170				   iface_addr) == 0 &&
3171	    os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3172	{
3173		wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3174			   "address for join from " MACSTR " to " MACSTR
3175			   " based on newly discovered P2P peer entry",
3176			   MAC2STR(wpa_s->pending_join_iface_addr),
3177			   MAC2STR(iface_addr));
3178		os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3179			  ETH_ALEN);
3180
3181		freq = p2p_get_oper_freq(wpa_s->global->p2p,
3182					 wpa_s->pending_join_iface_addr);
3183	}
3184	if (freq >= 0) {
3185		wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3186			   "from P2P peer table: %d MHz", freq);
3187	}
3188	bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3189	if (bss) {
3190		freq = bss->freq;
3191		wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3192			   "from BSS table: %d MHz", freq);
3193	}
3194	if (freq > 0) {
3195		u16 method;
3196
3197		if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3198			wpa_msg(wpa_s->parent, MSG_INFO,
3199				P2P_EVENT_GROUP_FORMATION_FAILURE
3200				"reason=FREQ_CONFLICT");
3201			return;
3202		}
3203
3204		wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3205			   "prior to joining an existing group (GO " MACSTR
3206			   " freq=%u MHz)",
3207			   MAC2STR(wpa_s->pending_join_dev_addr), freq);
3208		wpa_s->pending_pd_before_join = 1;
3209
3210		switch (wpa_s->pending_join_wps_method) {
3211		case WPS_PIN_DISPLAY:
3212			method = WPS_CONFIG_KEYPAD;
3213			break;
3214		case WPS_PIN_KEYPAD:
3215			method = WPS_CONFIG_DISPLAY;
3216			break;
3217		case WPS_PBC:
3218			method = WPS_CONFIG_PUSHBUTTON;
3219			break;
3220		default:
3221			method = 0;
3222			break;
3223		}
3224
3225		if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3226					       wpa_s->pending_join_dev_addr) ==
3227		     method)) {
3228			/*
3229			 * We have already performed provision discovery for
3230			 * joining the group. Proceed directly to join
3231			 * operation without duplicated provision discovery. */
3232			wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3233				   "with " MACSTR " already done - proceed to "
3234				   "join",
3235				   MAC2STR(wpa_s->pending_join_dev_addr));
3236			wpa_s->pending_pd_before_join = 0;
3237			goto start;
3238		}
3239
3240		if (p2p_prov_disc_req(wpa_s->global->p2p,
3241				      wpa_s->pending_join_dev_addr, method, 1,
3242				      freq) < 0) {
3243			wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3244				   "Discovery Request before joining an "
3245				   "existing group");
3246			wpa_s->pending_pd_before_join = 0;
3247			goto start;
3248		}
3249
3250		/*
3251		 * Actual join operation will be started from the Action frame
3252		 * TX status callback (if no ACK is received) or when the
3253		 * Provision Discovery Response is received. Use a short
3254		 * timeout as a backup mechanism should the Provision Discovery
3255		 * Response be lost for any reason.
3256		 */
3257		eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s,
3258				     NULL);
3259		eloop_register_timeout(2, 0, wpas_p2p_pd_before_join_timeout,
3260				       wpa_s, NULL);
3261		return;
3262	}
3263
3264	wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3265	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3266	eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3267	wpas_p2p_check_join_scan_limit(wpa_s);
3268	return;
3269
3270start:
3271	/* Start join operation immediately */
3272	wpas_p2p_join_start(wpa_s);
3273}
3274
3275
3276static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3277{
3278	int ret;
3279	struct wpa_driver_scan_params params;
3280	struct wpabuf *wps_ie, *ies;
3281	size_t ielen;
3282	int freqs[2] = { 0, 0 };
3283#ifdef ANDROID_P2P
3284	int oper_freq;
3285
3286	/* If freq is not provided, check the operating freq of the GO and do a
3287	 * a directed scan to save time
3288	 */
3289	if(!freq) {
3290		freq = (oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
3291			 wpa_s->pending_join_iface_addr) == -1) ? 0 : oper_freq;
3292	}
3293#endif
3294	os_memset(&params, 0, sizeof(params));
3295
3296	/* P2P Wildcard SSID */
3297	params.num_ssids = 1;
3298	params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3299	params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3300
3301	wpa_s->wps->dev.p2p = 1;
3302	wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3303					wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3304					NULL);
3305	if (wps_ie == NULL) {
3306		wpas_p2p_scan_res_join(wpa_s, NULL);
3307		return;
3308	}
3309
3310	ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3311	ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
3312	if (ies == NULL) {
3313		wpabuf_free(wps_ie);
3314		wpas_p2p_scan_res_join(wpa_s, NULL);
3315		return;
3316	}
3317	wpabuf_put_buf(ies, wps_ie);
3318	wpabuf_free(wps_ie);
3319
3320	p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3321
3322	params.p2p_probe = 1;
3323	params.extra_ies = wpabuf_head(ies);
3324	params.extra_ies_len = wpabuf_len(ies);
3325	if (freq > 0) {
3326		freqs[0] = freq;
3327		params.freqs = freqs;
3328	}
3329
3330	/*
3331	 * Run a scan to update BSS table and start Provision Discovery once
3332	 * the new scan results become available.
3333	 */
3334	ret = wpa_drv_scan(wpa_s, &params);
3335	if (!ret)
3336		wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
3337
3338	wpabuf_free(ies);
3339
3340	if (ret) {
3341		wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3342			   "try again later");
3343		eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3344		eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3345		wpas_p2p_check_join_scan_limit(wpa_s);
3346	}
3347}
3348
3349
3350static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3351{
3352	struct wpa_supplicant *wpa_s = eloop_ctx;
3353	wpas_p2p_join_scan_req(wpa_s, 0);
3354}
3355
3356
3357static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
3358			 const u8 *dev_addr, enum p2p_wps_method wps_method,
3359			 int auto_join)
3360{
3361	wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
3362		   MACSTR " dev " MACSTR ")%s",
3363		   MAC2STR(iface_addr), MAC2STR(dev_addr),
3364		   auto_join ? " (auto_join)" : "");
3365
3366	wpa_s->p2p_auto_pd = 0;
3367	wpa_s->p2p_auto_join = !!auto_join;
3368	os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3369	os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3370	wpa_s->pending_join_wps_method = wps_method;
3371
3372	/* Make sure we are not running find during connection establishment */
3373	wpas_p2p_stop_find(wpa_s);
3374
3375	wpa_s->p2p_join_scan_count = 0;
3376	wpas_p2p_join_scan(wpa_s, NULL);
3377	return 0;
3378}
3379
3380
3381static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3382{
3383	struct wpa_supplicant *group;
3384	struct p2p_go_neg_results res;
3385	struct wpa_bss *bss;
3386
3387	eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
3388	group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3389	if (group == NULL)
3390		return -1;
3391	if (group != wpa_s) {
3392		os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3393			  sizeof(group->p2p_pin));
3394		group->p2p_wps_method = wpa_s->p2p_wps_method;
3395	}
3396
3397	group->p2p_in_provisioning = 1;
3398	group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
3399
3400	os_memset(&res, 0, sizeof(res));
3401	os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3402		  ETH_ALEN);
3403	res.wps_method = wpa_s->pending_join_wps_method;
3404	bss = wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr);
3405	if (bss) {
3406		res.freq = bss->freq;
3407		res.ssid_len = bss->ssid_len;
3408		os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3409	}
3410
3411	if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3412		wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3413			   "starting client");
3414		wpa_drv_cancel_remain_on_channel(wpa_s);
3415		wpa_s->off_channel_freq = 0;
3416		wpa_s->roc_waiting_drv_freq = 0;
3417	}
3418	wpas_start_wps_enrollee(group, &res);
3419
3420	/*
3421	 * Allow a longer timeout for join-a-running-group than normal 15
3422	 * second group formation timeout since the GO may not have authorized
3423	 * our connection yet.
3424	 */
3425	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3426	eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3427			       wpa_s, NULL);
3428
3429	return 0;
3430}
3431
3432
3433/**
3434 * wpas_p2p_connect - Request P2P Group Formation to be started
3435 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3436 * @peer_addr: Address of the peer P2P Device
3437 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
3438 * @persistent_group: Whether to create a persistent group
3439 * @auto_join: Whether to select join vs. GO Negotiation automatically
3440 * @join: Whether to join an existing group (as a client) instead of starting
3441 *	Group Owner negotiation; @peer_addr is BSSID in that case
3442 * @auth: Whether to only authorize the connection instead of doing that and
3443 *	initiating Group Owner negotiation
3444 * @go_intent: GO Intent or -1 to use default
3445 * @freq: Frequency for the group or 0 for auto-selection
3446 * @persistent_id: Persistent group credentials to use for forcing GO
3447 *	parameters or -1 to generate new values (SSID/passphrase)
3448 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
3449 *	interoperability workaround when initiating group formation
3450 * @ht40: Start GO with 40 MHz channel width
3451 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
3452 *	failure, -2 on failure due to channel not currently available,
3453 *	-3 if forced channel is not supported
3454 */
3455int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
3456		     const char *pin, enum p2p_wps_method wps_method,
3457		     int persistent_group, int auto_join, int join, int auth,
3458		     int go_intent, int freq, int persistent_id, int pd,
3459		     int ht40)
3460{
3461	int force_freq = 0, oper_freq = 0;
3462	u8 bssid[ETH_ALEN];
3463	int ret = 0;
3464	enum wpa_driver_if_type iftype;
3465	const u8 *if_addr;
3466	struct wpa_ssid *ssid = NULL;
3467
3468	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3469		return -1;
3470
3471	if (persistent_id >= 0) {
3472		ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
3473		if (ssid == NULL || ssid->disabled != 2 ||
3474		    ssid->mode != WPAS_MODE_P2P_GO)
3475			return -1;
3476	}
3477
3478	if (go_intent < 0)
3479		go_intent = wpa_s->conf->p2p_go_intent;
3480
3481	if (!auth)
3482		wpa_s->p2p_long_listen = 0;
3483
3484	wpa_s->p2p_wps_method = wps_method;
3485	wpa_s->p2p_persistent_group = !!persistent_group;
3486	wpa_s->p2p_persistent_id = persistent_id;
3487	wpa_s->p2p_go_intent = go_intent;
3488	wpa_s->p2p_connect_freq = freq;
3489	wpa_s->p2p_fallback_to_go_neg = 0;
3490	wpa_s->p2p_pd_before_go_neg = !!pd;
3491	wpa_s->p2p_go_ht40 = !!ht40;
3492
3493	if (pin)
3494		os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
3495	else if (wps_method == WPS_PIN_DISPLAY) {
3496		ret = wps_generate_pin();
3497		os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
3498			    ret);
3499		wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
3500			   wpa_s->p2p_pin);
3501	} else
3502		wpa_s->p2p_pin[0] = '\0';
3503
3504	if (join || auto_join) {
3505		u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
3506		if (auth) {
3507			wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
3508				   "connect a running group from " MACSTR,
3509				   MAC2STR(peer_addr));
3510			os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
3511			return ret;
3512		}
3513		os_memcpy(dev_addr, peer_addr, ETH_ALEN);
3514		if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
3515					   iface_addr) < 0) {
3516			os_memcpy(iface_addr, peer_addr, ETH_ALEN);
3517			p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
3518					 dev_addr);
3519		}
3520		if (auto_join) {
3521			os_get_time(&wpa_s->p2p_auto_started);
3522			wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
3523				   "%ld.%06ld",
3524				   wpa_s->p2p_auto_started.sec,
3525				   wpa_s->p2p_auto_started.usec);
3526		}
3527		if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
3528				  auto_join) < 0)
3529			return -1;
3530		return ret;
3531	}
3532
3533	if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3534	    wpa_s->assoc_freq)
3535		oper_freq = wpa_s->assoc_freq;
3536	else {
3537		oper_freq = wpa_drv_shared_freq(wpa_s);
3538		if (oper_freq < 0)
3539			oper_freq = 0;
3540	}
3541
3542	if (freq > 0) {
3543		if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3544			wpa_printf(MSG_DEBUG, "P2P: The forced channel "
3545				   "(%u MHz) is not supported for P2P uses",
3546				   freq);
3547			return -3;
3548		}
3549
3550		if (oper_freq > 0 && freq != oper_freq &&
3551		    !(wpa_s->drv_flags &
3552		      WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3553			wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3554				   "on %u MHz while connected on another "
3555				   "channel (%u MHz)", freq, oper_freq);
3556			return -2;
3557		}
3558		wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3559			   "requested channel (%u MHz)", freq);
3560		force_freq = freq;
3561	} else if (oper_freq > 0 &&
3562		   !p2p_supported_freq(wpa_s->global->p2p, oper_freq)) {
3563		if (!(wpa_s->drv_flags &
3564		      WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3565			wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group "
3566				   "while connected on non-P2P supported "
3567				   "channel (%u MHz)", oper_freq);
3568			return -2;
3569		}
3570		wpa_printf(MSG_DEBUG, "P2P: Current operating channel "
3571			   "(%u MHz) not available for P2P - try to use "
3572			   "another channel", oper_freq);
3573		force_freq = 0;
3574	} else if (oper_freq > 0) {
3575		wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
3576			   "channel we are already using (%u MHz) on another "
3577			   "interface", oper_freq);
3578		force_freq = oper_freq;
3579	}
3580
3581	wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
3582
3583	if (wpa_s->create_p2p_iface) {
3584		/* Prepare to add a new interface for the group */
3585		iftype = WPA_IF_P2P_GROUP;
3586		if (go_intent == 15)
3587			iftype = WPA_IF_P2P_GO;
3588		if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
3589			wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3590				   "interface for the group");
3591			return -1;
3592		}
3593
3594		if_addr = wpa_s->pending_interface_addr;
3595	} else
3596		if_addr = wpa_s->own_addr;
3597
3598	if (auth) {
3599		if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
3600					 go_intent, if_addr,
3601					 force_freq, persistent_group, ssid) <
3602		    0)
3603			return -1;
3604		return ret;
3605	}
3606
3607	if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
3608				  go_intent, if_addr, force_freq,
3609				  persistent_group, ssid) < 0) {
3610		if (wpa_s->create_p2p_iface)
3611			wpas_p2p_remove_pending_group_interface(wpa_s);
3612		return -1;
3613	}
3614	return ret;
3615}
3616
3617
3618/**
3619 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
3620 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3621 * @freq: Frequency of the channel in MHz
3622 * @duration: Duration of the stay on the channel in milliseconds
3623 *
3624 * This callback is called when the driver indicates that it has started the
3625 * requested remain-on-channel duration.
3626 */
3627void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3628				   unsigned int freq, unsigned int duration)
3629{
3630	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3631		return;
3632	if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
3633		p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
3634			      wpa_s->pending_listen_duration);
3635		wpa_s->pending_listen_freq = 0;
3636	}
3637}
3638
3639
3640static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
3641				 unsigned int timeout)
3642{
3643	/* Limit maximum Listen state time based on driver limitation. */
3644	if (timeout > wpa_s->max_remain_on_chan)
3645		timeout = wpa_s->max_remain_on_chan;
3646
3647	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3648		return wpa_drv_p2p_listen(wpa_s, timeout);
3649
3650	return p2p_listen(wpa_s->global->p2p, timeout);
3651}
3652
3653
3654/**
3655 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
3656 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3657 * @freq: Frequency of the channel in MHz
3658 *
3659 * This callback is called when the driver indicates that a remain-on-channel
3660 * operation has been completed, i.e., the duration on the requested channel
3661 * has timed out.
3662 */
3663void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
3664					  unsigned int freq)
3665{
3666	wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
3667		   "(p2p_long_listen=%d ms pending_action_tx=%p)",
3668		   wpa_s->p2p_long_listen, wpa_s->pending_action_tx);
3669	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3670		return;
3671	if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
3672		return; /* P2P module started a new operation */
3673	if (wpa_s->pending_action_tx)
3674		return;
3675	if (wpa_s->p2p_long_listen > 0)
3676		wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
3677	if (wpa_s->p2p_long_listen > 0) {
3678		wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
3679		wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
3680	}
3681}
3682
3683
3684/**
3685 * wpas_p2p_group_remove - Remove a P2P group
3686 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3687 * @ifname: Network interface name of the group interface or "*" to remove all
3688 *	groups
3689 * Returns: 0 on success, -1 on failure
3690 *
3691 * This function is used to remove a P2P group. This can be used to disconnect
3692 * from a group in which the local end is a P2P Client or to end a P2P Group in
3693 * case the local end is the Group Owner. If a virtual network interface was
3694 * created for this group, that interface will be removed. Otherwise, only the
3695 * configured P2P group network will be removed from the interface.
3696 */
3697int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
3698{
3699	struct wpa_global *global = wpa_s->global;
3700
3701	if (os_strcmp(ifname, "*") == 0) {
3702		struct wpa_supplicant *prev;
3703		wpa_s = global->ifaces;
3704		while (wpa_s) {
3705			prev = wpa_s;
3706			wpa_s = wpa_s->next;
3707			wpas_p2p_disconnect(prev);
3708		}
3709		return 0;
3710	}
3711
3712	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3713		if (os_strcmp(wpa_s->ifname, ifname) == 0)
3714			break;
3715	}
3716
3717	return wpas_p2p_disconnect(wpa_s);
3718}
3719
3720
3721static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
3722				   struct p2p_go_neg_results *params,
3723				   int freq, int ht40)
3724{
3725	u8 bssid[ETH_ALEN];
3726	int res;
3727
3728	os_memset(params, 0, sizeof(*params));
3729	params->role_go = 1;
3730	params->ht40 = ht40;
3731	if (freq) {
3732		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
3733			   "frequency %d MHz", freq);
3734		params->freq = freq;
3735	} else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
3736		   wpa_s->conf->p2p_oper_channel >= 1 &&
3737		   wpa_s->conf->p2p_oper_channel <= 11) {
3738		params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
3739		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3740			   "frequency %d MHz", params->freq);
3741	} else if (wpa_s->conf->p2p_oper_reg_class == 115 ||
3742		   wpa_s->conf->p2p_oper_reg_class == 124) {
3743		params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
3744		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
3745			   "frequency %d MHz", params->freq);
3746	} else if (wpa_s->conf->p2p_oper_channel == 0 &&
3747		   wpa_s->best_overall_freq > 0 &&
3748		   p2p_supported_freq(wpa_s->global->p2p,
3749				      wpa_s->best_overall_freq)) {
3750		params->freq = wpa_s->best_overall_freq;
3751		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
3752			   "channel %d MHz", params->freq);
3753	} else if (wpa_s->conf->p2p_oper_channel == 0 &&
3754		   wpa_s->best_24_freq > 0 &&
3755		   p2p_supported_freq(wpa_s->global->p2p,
3756				      wpa_s->best_24_freq)) {
3757		params->freq = wpa_s->best_24_freq;
3758		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
3759			   "channel %d MHz", params->freq);
3760	} else if (wpa_s->conf->p2p_oper_channel == 0 &&
3761		   wpa_s->best_5_freq > 0 &&
3762		   p2p_supported_freq(wpa_s->global->p2p,
3763				      wpa_s->best_5_freq)) {
3764		params->freq = wpa_s->best_5_freq;
3765		wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
3766			   "channel %d MHz", params->freq);
3767	} else {
3768		int chan;
3769		for (chan = 0; chan < 11; chan++) {
3770			params->freq = 2412 + chan * 5;
3771			if (!wpas_p2p_disallowed_freq(wpa_s->global,
3772						      params->freq))
3773				break;
3774		}
3775		if (chan == 11) {
3776			wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
3777				   "allowed");
3778			return -1;
3779		}
3780		wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
3781			   "known)", params->freq);
3782	}
3783
3784	if (wpa_s->current_ssid && wpa_drv_get_bssid(wpa_s, bssid) == 0 &&
3785	    wpa_s->assoc_freq && !freq) {
3786		wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3787			   "already using");
3788		params->freq = wpa_s->assoc_freq;
3789	}
3790
3791	res = wpa_drv_shared_freq(wpa_s);
3792	if (res > 0 && !freq) {
3793		wpa_printf(MSG_DEBUG, "P2P: Force GO on the channel we are "
3794			   "already using on a shared interface");
3795		params->freq = res;
3796	} else if (res > 0 && freq != res &&
3797		   !(wpa_s->drv_flags &
3798		     WPA_DRIVER_FLAGS_MULTI_CHANNEL_CONCURRENT)) {
3799		wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz "
3800			   "while connected on another channel (%u MHz)",
3801			   freq, res);
3802		return -1;
3803	}
3804
3805	return 0;
3806}
3807
3808
3809static struct wpa_supplicant *
3810wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
3811			 int go)
3812{
3813	struct wpa_supplicant *group_wpa_s;
3814
3815	if (!wpas_p2p_create_iface(wpa_s))
3816		return wpa_s;
3817
3818	if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
3819					 WPA_IF_P2P_CLIENT) < 0)
3820		return NULL;
3821	group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
3822	if (group_wpa_s == NULL) {
3823		wpas_p2p_remove_pending_group_interface(wpa_s);
3824		return NULL;
3825	}
3826
3827	return group_wpa_s;
3828}
3829
3830
3831/**
3832 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
3833 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3834 * @persistent_group: Whether to create a persistent group
3835 * @freq: Frequency for the group or 0 to indicate no hardcoding
3836 * Returns: 0 on success, -1 on failure
3837 *
3838 * This function creates a new P2P group with the local end as the Group Owner,
3839 * i.e., without using Group Owner Negotiation.
3840 */
3841int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
3842		       int freq, int ht40)
3843{
3844	struct p2p_go_neg_results params;
3845	unsigned int r;
3846
3847	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
3848		return -1;
3849
3850	/* Make sure we are not running find during connection establishment */
3851	wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
3852	wpas_p2p_stop_find(wpa_s);
3853
3854	if (freq == 2) {
3855		wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
3856			   "band");
3857		if (wpa_s->best_24_freq > 0 &&
3858		    p2p_supported_freq(wpa_s->global->p2p,
3859				       wpa_s->best_24_freq)) {
3860			freq = wpa_s->best_24_freq;
3861			wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
3862				   "channel: %d MHz", freq);
3863		} else {
3864			os_get_random((u8 *) &r, sizeof(r));
3865			freq = 2412 + (r % 3) * 25;
3866			wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
3867				   "channel: %d MHz", freq);
3868		}
3869	}
3870
3871	if (freq == 5) {
3872		wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
3873			   "band");
3874		if (wpa_s->best_5_freq > 0 &&
3875		    p2p_supported_freq(wpa_s->global->p2p,
3876				       wpa_s->best_5_freq)) {
3877			freq = wpa_s->best_5_freq;
3878			wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
3879				   "channel: %d MHz", freq);
3880		} else {
3881			os_get_random((u8 *) &r, sizeof(r));
3882			freq = 5180 + (r % 4) * 20;
3883			if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
3884				wpa_printf(MSG_DEBUG, "P2P: Could not select "
3885					   "5 GHz channel for P2P group");
3886				return -1;
3887			}
3888			wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
3889				   "channel: %d MHz", freq);
3890		}
3891	}
3892
3893	if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
3894		wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
3895			   "(%u MHz) is not supported for P2P uses",
3896			   freq);
3897		return -1;
3898	}
3899
3900	if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
3901		return -1;
3902	if (params.freq &&
3903	    !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
3904		wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
3905			   "(%u MHz) is not supported for P2P uses",
3906			   params.freq);
3907		return -1;
3908	}
3909	p2p_go_params(wpa_s->global->p2p, &params);
3910	params.persistent_group = persistent_group;
3911
3912	wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
3913	if (wpa_s == NULL)
3914		return -1;
3915	wpas_start_wps_go(wpa_s, &params, 0);
3916
3917	return 0;
3918}
3919
3920
3921static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
3922				 struct wpa_ssid *params, int addr_allocated)
3923{
3924	struct wpa_ssid *ssid;
3925
3926	wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
3927	if (wpa_s == NULL)
3928		return -1;
3929
3930	wpa_supplicant_ap_deinit(wpa_s);
3931
3932	ssid = wpa_config_add_network(wpa_s->conf);
3933	if (ssid == NULL)
3934		return -1;
3935	wpa_config_set_network_defaults(ssid);
3936	ssid->temporary = 1;
3937	ssid->proto = WPA_PROTO_RSN;
3938	ssid->pairwise_cipher = WPA_CIPHER_CCMP;
3939	ssid->group_cipher = WPA_CIPHER_CCMP;
3940	ssid->key_mgmt = WPA_KEY_MGMT_PSK;
3941	ssid->ssid = os_malloc(params->ssid_len);
3942	if (ssid->ssid == NULL) {
3943		wpa_config_remove_network(wpa_s->conf, ssid->id);
3944		return -1;
3945	}
3946	os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
3947	ssid->ssid_len = params->ssid_len;
3948	ssid->p2p_group = 1;
3949	ssid->export_keys = 1;
3950	if (params->psk_set) {
3951		os_memcpy(ssid->psk, params->psk, 32);
3952		ssid->psk_set = 1;
3953	}
3954	if (params->passphrase)
3955		ssid->passphrase = os_strdup(params->passphrase);
3956
3957	wpa_supplicant_select_network(wpa_s, ssid);
3958
3959	wpa_s->show_group_started = 1;
3960
3961	return 0;
3962}
3963
3964
3965int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
3966				  struct wpa_ssid *ssid, int addr_allocated,
3967				  int freq, int ht40)
3968{
3969	struct p2p_go_neg_results params;
3970	int go = 0;
3971
3972	if (ssid->disabled != 2 || ssid->ssid == NULL)
3973		return -1;
3974
3975	if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
3976	    go == (ssid->mode == WPAS_MODE_P2P_GO)) {
3977		wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
3978			   "already running");
3979		return 0;
3980	}
3981
3982	/* Make sure we are not running find during connection establishment */
3983	wpas_p2p_stop_find(wpa_s);
3984
3985	wpa_s->p2p_fallback_to_go_neg = 0;
3986
3987	if (ssid->mode == WPAS_MODE_INFRA)
3988		return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
3989
3990	if (ssid->mode != WPAS_MODE_P2P_GO)
3991		return -1;
3992
3993	if (wpas_p2p_init_go_params(wpa_s, &params, freq, ht40))
3994		return -1;
3995
3996	params.role_go = 1;
3997	if (ssid->passphrase == NULL ||
3998	    os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
3999		wpa_printf(MSG_DEBUG, "P2P: Invalid passphrase in persistent "
4000			   "group");
4001		return -1;
4002	}
4003	os_strlcpy(params.passphrase, ssid->passphrase,
4004		   sizeof(params.passphrase));
4005	os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4006	params.ssid_len = ssid->ssid_len;
4007	params.persistent_group = 1;
4008
4009	wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4010	if (wpa_s == NULL)
4011		return -1;
4012
4013	wpas_start_wps_go(wpa_s, &params, 0);
4014
4015	return 0;
4016}
4017
4018
4019static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4020			       struct wpabuf *proberesp_ies)
4021{
4022	struct wpa_supplicant *wpa_s = ctx;
4023	if (wpa_s->ap_iface) {
4024		struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
4025		if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4026			wpabuf_free(beacon_ies);
4027			wpabuf_free(proberesp_ies);
4028			return;
4029		}
4030		if (beacon_ies) {
4031			wpabuf_free(hapd->p2p_beacon_ie);
4032			hapd->p2p_beacon_ie = beacon_ies;
4033		}
4034		wpabuf_free(hapd->p2p_probe_resp_ie);
4035		hapd->p2p_probe_resp_ie = proberesp_ies;
4036	} else {
4037		wpabuf_free(beacon_ies);
4038		wpabuf_free(proberesp_ies);
4039	}
4040	wpa_supplicant_ap_update_beacon(wpa_s);
4041}
4042
4043
4044static void wpas_p2p_idle_update(void *ctx, int idle)
4045{
4046	struct wpa_supplicant *wpa_s = ctx;
4047	if (!wpa_s->ap_iface)
4048		return;
4049	wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4050	if (idle)
4051		wpas_p2p_set_group_idle_timeout(wpa_s);
4052	else
4053		eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4054}
4055
4056
4057struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
4058				       struct wpa_ssid *ssid)
4059{
4060	struct p2p_group *group;
4061	struct p2p_group_config *cfg;
4062
4063	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4064		return NULL;
4065	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4066		return NULL;
4067
4068	cfg = os_zalloc(sizeof(*cfg));
4069	if (cfg == NULL)
4070		return NULL;
4071
4072	if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4073		cfg->persistent_group = 2;
4074	else if (ssid->p2p_persistent_group)
4075		cfg->persistent_group = 1;
4076	os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4077	if (wpa_s->max_stations &&
4078	    wpa_s->max_stations < wpa_s->conf->max_num_sta)
4079		cfg->max_clients = wpa_s->max_stations;
4080	else
4081		cfg->max_clients = wpa_s->conf->max_num_sta;
4082	os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4083	cfg->ssid_len = ssid->ssid_len;
4084	cfg->cb_ctx = wpa_s;
4085	cfg->ie_update = wpas_p2p_ie_update;
4086	cfg->idle_update = wpas_p2p_idle_update;
4087
4088	group = p2p_group_init(wpa_s->global->p2p, cfg);
4089	if (group == NULL)
4090		os_free(cfg);
4091	if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4092		p2p_group_notif_formation_done(group);
4093	wpa_s->p2p_group = group;
4094	return group;
4095}
4096
4097
4098void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4099			  int registrar)
4100{
4101	struct wpa_ssid *ssid = wpa_s->current_ssid;
4102
4103	if (!wpa_s->p2p_in_provisioning) {
4104		wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4105			   "provisioning not in progress");
4106		return;
4107	}
4108
4109	if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4110		u8 go_dev_addr[ETH_ALEN];
4111		os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4112		wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4113					  ssid->ssid_len);
4114		/* Clear any stored provisioning info */
4115		p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4116	}
4117
4118	eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4119			     NULL);
4120	if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4121		/*
4122		 * Use a separate timeout for initial data connection to
4123		 * complete to allow the group to be removed automatically if
4124		 * something goes wrong in this step before the P2P group idle
4125		 * timeout mechanism is taken into use.
4126		 */
4127		eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4128				       wpas_p2p_group_formation_timeout,
4129				       wpa_s->parent, NULL);
4130	}
4131	if (wpa_s->global->p2p)
4132		p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4133	else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4134		wpa_drv_wps_success_cb(wpa_s, peer_addr);
4135	wpas_group_formation_completed(wpa_s, 1);
4136}
4137
4138
4139void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4140			 struct wps_event_fail *fail)
4141{
4142	if (!wpa_s->p2p_in_provisioning) {
4143		wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4144			   "provisioning not in progress");
4145		return;
4146	}
4147
4148	if (wpa_s->go_params) {
4149		p2p_clear_provisioning_info(
4150			wpa_s->global->p2p,
4151			wpa_s->go_params->peer_device_addr);
4152	}
4153
4154	wpas_notify_p2p_wps_failed(wpa_s, fail);
4155}
4156
4157
4158int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4159		       const char *config_method,
4160		       enum wpas_p2p_prov_disc_use use)
4161{
4162	u16 config_methods;
4163
4164	wpa_s->p2p_fallback_to_go_neg = 0;
4165	wpa_s->pending_pd_use = NORMAL_PD;
4166	if (os_strncmp(config_method, "display", 7) == 0)
4167		config_methods = WPS_CONFIG_DISPLAY;
4168	else if (os_strncmp(config_method, "keypad", 6) == 0)
4169		config_methods = WPS_CONFIG_KEYPAD;
4170	else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4171		 os_strncmp(config_method, "pushbutton", 10) == 0)
4172		config_methods = WPS_CONFIG_PUSHBUTTON;
4173	else {
4174		wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
4175		return -1;
4176	}
4177
4178	if (use == WPAS_P2P_PD_AUTO) {
4179		os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4180		wpa_s->pending_pd_config_methods = config_methods;
4181		wpa_s->p2p_auto_pd = 1;
4182		wpa_s->p2p_auto_join = 0;
4183		wpa_s->pending_pd_before_join = 0;
4184		wpa_s->auto_pd_scan_retry = 0;
4185		wpas_p2p_stop_find(wpa_s);
4186		wpa_s->p2p_join_scan_count = 0;
4187		os_get_time(&wpa_s->p2p_auto_started);
4188		wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4189			   wpa_s->p2p_auto_started.sec,
4190			   wpa_s->p2p_auto_started.usec);
4191		wpas_p2p_join_scan(wpa_s, NULL);
4192		return 0;
4193	}
4194
4195	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4196		return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
4197						 config_methods,
4198						 use == WPAS_P2P_PD_FOR_JOIN);
4199	}
4200
4201	if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4202		return -1;
4203
4204	return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
4205				 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4206				 0);
4207}
4208
4209
4210int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4211			      char *end)
4212{
4213	return p2p_scan_result_text(ies, ies_len, buf, end);
4214}
4215
4216
4217static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4218{
4219	if (!wpa_s->pending_action_tx)
4220		return;
4221
4222	wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4223		   "operation request");
4224	wpabuf_free(wpa_s->pending_action_tx);
4225	wpa_s->pending_action_tx = NULL;
4226}
4227
4228
4229int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4230		  enum p2p_discovery_type type,
4231		  unsigned int num_req_dev_types, const u8 *req_dev_types,
4232		  const u8 *dev_id, unsigned int search_delay)
4233{
4234	wpas_p2p_clear_pending_action_tx(wpa_s);
4235	wpa_s->p2p_long_listen = 0;
4236
4237	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4238		return wpa_drv_p2p_find(wpa_s, timeout, type);
4239
4240	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4241	    wpa_s->p2p_in_provisioning)
4242		return -1;
4243
4244	wpa_supplicant_cancel_sched_scan(wpa_s);
4245
4246	return p2p_find(wpa_s->global->p2p, timeout, type,
4247			num_req_dev_types, req_dev_types, dev_id,
4248			search_delay);
4249}
4250
4251
4252void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4253{
4254	wpas_p2p_clear_pending_action_tx(wpa_s);
4255	wpa_s->p2p_long_listen = 0;
4256	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4257	eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4258	wpa_s->p2p_cb_on_scan_complete = 0;
4259
4260	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4261		wpa_drv_p2p_stop_find(wpa_s);
4262		return;
4263	}
4264
4265	if (wpa_s->global->p2p)
4266		p2p_stop_find(wpa_s->global->p2p);
4267
4268	wpas_p2p_remove_pending_group_interface(wpa_s);
4269}
4270
4271
4272static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4273{
4274	struct wpa_supplicant *wpa_s = eloop_ctx;
4275	wpa_s->p2p_long_listen = 0;
4276}
4277
4278
4279int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
4280{
4281	int res;
4282
4283	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4284		return -1;
4285
4286	wpa_supplicant_cancel_sched_scan(wpa_s);
4287	wpas_p2p_clear_pending_action_tx(wpa_s);
4288
4289	if (timeout == 0) {
4290		/*
4291		 * This is a request for unlimited Listen state. However, at
4292		 * least for now, this is mapped to a Listen state for one
4293		 * hour.
4294		 */
4295		timeout = 3600;
4296	}
4297	eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4298	wpa_s->p2p_long_listen = 0;
4299
4300	/*
4301	 * Stop previous find/listen operation to avoid trying to request a new
4302	 * remain-on-channel operation while the driver is still running the
4303	 * previous one.
4304	 */
4305	if (wpa_s->global->p2p)
4306		p2p_stop_find(wpa_s->global->p2p);
4307
4308	res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
4309	if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
4310		wpa_s->p2p_long_listen = timeout * 1000;
4311		eloop_register_timeout(timeout, 0,
4312				       wpas_p2p_long_listen_timeout,
4313				       wpa_s, NULL);
4314	}
4315
4316	return res;
4317}
4318
4319
4320int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
4321			  u8 *buf, size_t len, int p2p_group)
4322{
4323	struct wpabuf *p2p_ie;
4324	int ret;
4325
4326	if (wpa_s->global->p2p_disabled)
4327		return -1;
4328	if (wpa_s->global->p2p == NULL)
4329		return -1;
4330	if (bss == NULL)
4331		return -1;
4332
4333	p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
4334	ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
4335			       p2p_group, p2p_ie);
4336	wpabuf_free(p2p_ie);
4337
4338	return ret;
4339}
4340
4341
4342int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
4343			  const u8 *dst, const u8 *bssid,
4344			  const u8 *ie, size_t ie_len, int ssi_signal)
4345{
4346	if (wpa_s->global->p2p_disabled)
4347		return 0;
4348	if (wpa_s->global->p2p == NULL)
4349		return 0;
4350
4351	switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
4352				 ie, ie_len)) {
4353	case P2P_PREQ_NOT_P2P:
4354		wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
4355				 ssi_signal);
4356		/* fall through */
4357	case P2P_PREQ_MALFORMED:
4358	case P2P_PREQ_NOT_LISTEN:
4359	case P2P_PREQ_NOT_PROCESSED:
4360	default: /* make gcc happy */
4361		return 0;
4362	case P2P_PREQ_PROCESSED:
4363		return 1;
4364	}
4365}
4366
4367
4368void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
4369			const u8 *sa, const u8 *bssid,
4370			u8 category, const u8 *data, size_t len, int freq)
4371{
4372	if (wpa_s->global->p2p_disabled)
4373		return;
4374	if (wpa_s->global->p2p == NULL)
4375		return;
4376
4377	p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
4378		      freq);
4379}
4380
4381
4382void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
4383{
4384	if (wpa_s->global->p2p_disabled)
4385		return;
4386	if (wpa_s->global->p2p == NULL)
4387		return;
4388
4389	p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4390}
4391
4392
4393void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
4394{
4395	p2p_group_deinit(wpa_s->p2p_group);
4396	wpa_s->p2p_group = NULL;
4397
4398	wpa_s->ap_configured_cb = NULL;
4399	wpa_s->ap_configured_cb_ctx = NULL;
4400	wpa_s->ap_configured_cb_data = NULL;
4401	wpa_s->connect_without_scan = NULL;
4402}
4403
4404
4405int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
4406{
4407	wpa_s->p2p_long_listen = 0;
4408
4409	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4410		return wpa_drv_p2p_reject(wpa_s, addr);
4411
4412	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4413		return -1;
4414
4415	return p2p_reject(wpa_s->global->p2p, addr);
4416}
4417
4418
4419/* Invite to reinvoke a persistent group */
4420int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4421		    struct wpa_ssid *ssid, const u8 *go_dev_addr)
4422{
4423	enum p2p_invite_role role;
4424	u8 *bssid = NULL;
4425
4426	if (ssid->mode == WPAS_MODE_P2P_GO) {
4427		role = P2P_INVITE_ROLE_GO;
4428		if (peer_addr == NULL) {
4429			wpa_printf(MSG_DEBUG, "P2P: Missing peer "
4430				   "address in invitation command");
4431			return -1;
4432		}
4433		if (wpas_p2p_create_iface(wpa_s)) {
4434			if (wpas_p2p_add_group_interface(wpa_s,
4435							 WPA_IF_P2P_GO) < 0) {
4436				wpa_printf(MSG_ERROR, "P2P: Failed to "
4437					   "allocate a new interface for the "
4438					   "group");
4439				return -1;
4440			}
4441			bssid = wpa_s->pending_interface_addr;
4442		} else
4443			bssid = wpa_s->own_addr;
4444	} else {
4445		role = P2P_INVITE_ROLE_CLIENT;
4446		peer_addr = ssid->bssid;
4447	}
4448	wpa_s->pending_invite_ssid_id = ssid->id;
4449
4450	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4451		return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4452					  ssid->ssid, ssid->ssid_len,
4453					  go_dev_addr, 1);
4454
4455	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4456		return -1;
4457
4458	return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4459			  ssid->ssid, ssid->ssid_len, 0, go_dev_addr, 1);
4460}
4461
4462
4463/* Invite to join an active group */
4464int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
4465			  const u8 *peer_addr, const u8 *go_dev_addr)
4466{
4467	struct wpa_global *global = wpa_s->global;
4468	enum p2p_invite_role role;
4469	u8 *bssid = NULL;
4470	struct wpa_ssid *ssid;
4471	int persistent;
4472
4473	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4474		if (os_strcmp(wpa_s->ifname, ifname) == 0)
4475			break;
4476	}
4477	if (wpa_s == NULL) {
4478		wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
4479		return -1;
4480	}
4481
4482	ssid = wpa_s->current_ssid;
4483	if (ssid == NULL) {
4484		wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
4485			   "invitation");
4486		return -1;
4487	}
4488
4489	persistent = ssid->p2p_persistent_group &&
4490		wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
4491					ssid->ssid, ssid->ssid_len);
4492
4493	if (ssid->mode == WPAS_MODE_P2P_GO) {
4494		role = P2P_INVITE_ROLE_ACTIVE_GO;
4495		bssid = wpa_s->own_addr;
4496		if (go_dev_addr == NULL)
4497			go_dev_addr = wpa_s->global->p2p_dev_addr;
4498	} else {
4499		role = P2P_INVITE_ROLE_CLIENT;
4500		if (wpa_s->wpa_state < WPA_ASSOCIATED) {
4501			wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
4502				   "invite to current group");
4503			return -1;
4504		}
4505		bssid = wpa_s->bssid;
4506		if (go_dev_addr == NULL &&
4507		    !is_zero_ether_addr(wpa_s->go_dev_addr))
4508			go_dev_addr = wpa_s->go_dev_addr;
4509	}
4510	wpa_s->parent->pending_invite_ssid_id = -1;
4511
4512	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4513		return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
4514					  ssid->ssid, ssid->ssid_len,
4515					  go_dev_addr, persistent);
4516
4517	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4518		return -1;
4519
4520	return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
4521			  ssid->ssid, ssid->ssid_len, wpa_s->assoc_freq,
4522			  go_dev_addr, persistent);
4523}
4524
4525
4526void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
4527{
4528	struct wpa_ssid *ssid = wpa_s->current_ssid;
4529	const char *ssid_txt;
4530	u8 go_dev_addr[ETH_ALEN];
4531	int network_id = -1;
4532	int persistent;
4533	int freq;
4534
4535	if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
4536		eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
4537				     wpa_s->parent, NULL);
4538	}
4539
4540	if (!wpa_s->show_group_started || !ssid)
4541		goto done;
4542
4543	wpa_s->show_group_started = 0;
4544
4545	ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
4546	os_memset(go_dev_addr, 0, ETH_ALEN);
4547	if (ssid->bssid_set)
4548		os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
4549	persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4550					       ssid->ssid_len);
4551	os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
4552
4553	if (wpa_s->global->p2p_group_formation == wpa_s)
4554		wpa_s->global->p2p_group_formation = NULL;
4555
4556	freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
4557		(int) wpa_s->assoc_freq;
4558	if (ssid->passphrase == NULL && ssid->psk_set) {
4559		char psk[65];
4560		wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
4561		wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4562			"%s client ssid=\"%s\" freq=%d psk=%s go_dev_addr="
4563			MACSTR "%s",
4564			wpa_s->ifname, ssid_txt, freq, psk,
4565			MAC2STR(go_dev_addr),
4566			persistent ? " [PERSISTENT]" : "");
4567	} else {
4568		wpa_msg(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
4569			"%s client ssid=\"%s\" freq=%d passphrase=\"%s\" "
4570			"go_dev_addr=" MACSTR "%s",
4571			wpa_s->ifname, ssid_txt, freq,
4572			ssid->passphrase ? ssid->passphrase : "",
4573			MAC2STR(go_dev_addr),
4574			persistent ? " [PERSISTENT]" : "");
4575	}
4576
4577	if (persistent)
4578		network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
4579							     ssid, go_dev_addr);
4580	if (network_id < 0)
4581		network_id = ssid->id;
4582	wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
4583
4584done:
4585	if (wpa_s->p2p_cb_on_scan_complete && !wpa_s->global->p2p_disabled &&
4586	    wpa_s->global->p2p != NULL) {
4587		wpa_s->p2p_cb_on_scan_complete = 0;
4588		if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
4589			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
4590				"continued after successful connection");
4591			p2p_increase_search_delay(
4592				wpa_s->global->p2p,
4593				wpas_p2p_search_delay(wpa_s));
4594		}
4595	}
4596}
4597
4598
4599int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
4600			  u32 interval1, u32 duration2, u32 interval2)
4601{
4602	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4603		return -1;
4604	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4605		return -1;
4606
4607	if (wpa_s->wpa_state < WPA_ASSOCIATED ||
4608	    wpa_s->current_ssid == NULL ||
4609	    wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
4610		return -1;
4611
4612	return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
4613				wpa_s->own_addr, wpa_s->assoc_freq,
4614				duration1, interval1, duration2, interval2);
4615}
4616
4617
4618int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
4619			unsigned int interval)
4620{
4621	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4622		return -1;
4623
4624	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4625		return -1;
4626
4627	return p2p_ext_listen(wpa_s->global->p2p, period, interval);
4628}
4629
4630
4631static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
4632{
4633	return wpa_s->current_ssid != NULL &&
4634		wpa_s->current_ssid->p2p_group &&
4635		wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
4636}
4637
4638
4639static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
4640{
4641	struct wpa_supplicant *wpa_s = eloop_ctx;
4642
4643	if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
4644		wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
4645			   "disabled");
4646		return;
4647	}
4648
4649	wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
4650		   "group");
4651	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
4652}
4653
4654
4655static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
4656{
4657	int timeout;
4658
4659	if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4660		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4661
4662	if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
4663		return;
4664
4665	timeout = wpa_s->conf->p2p_group_idle;
4666	if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
4667	    (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
4668	    timeout = P2P_MAX_CLIENT_IDLE;
4669
4670	if (timeout == 0)
4671		return;
4672
4673	if (timeout < 0) {
4674		if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
4675			timeout = 0; /* special client mode no-timeout */
4676		else
4677			return;
4678	}
4679
4680	if (wpa_s->p2p_in_provisioning) {
4681		/*
4682		 * Use the normal group formation timeout during the
4683		 * provisioning phase to avoid terminating this process too
4684		 * early due to group idle timeout.
4685		 */
4686		wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4687			   "during provisioning");
4688		return;
4689	}
4690#ifndef ANDROID_P2P
4691	if (wpa_s->show_group_started) {
4692		/*
4693		 * Use the normal group formation timeout between the end of
4694		 * the provisioning phase and completion of 4-way handshake to
4695		 * avoid terminating this process too early due to group idle
4696		 * timeout.
4697		 */
4698		wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
4699			   "while waiting for initial 4-way handshake to "
4700			   "complete");
4701		return;
4702	}
4703#endif
4704
4705	wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
4706		   timeout);
4707	eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
4708			       wpa_s, NULL);
4709}
4710
4711
4712void wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4713			   u16 reason_code, const u8 *ie, size_t ie_len,
4714			   int locally_generated)
4715{
4716	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4717		return;
4718	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4719		return;
4720
4721	if (!locally_generated)
4722		p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4723				 ie_len);
4724
4725	if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
4726	    wpa_s->current_ssid &&
4727	    wpa_s->current_ssid->p2p_group &&
4728	    wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
4729		wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
4730			   "session is ending");
4731		wpas_p2p_group_delete(wpa_s,
4732				      P2P_GROUP_REMOVAL_GO_ENDING_SESSION);
4733	}
4734}
4735
4736
4737void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
4738			     u16 reason_code, const u8 *ie, size_t ie_len,
4739			     int locally_generated)
4740{
4741	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4742		return;
4743	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4744		return;
4745
4746	if (!locally_generated)
4747		p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
4748				   ie_len);
4749}
4750
4751
4752void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
4753{
4754	struct p2p_data *p2p = wpa_s->global->p2p;
4755
4756	if (p2p == NULL)
4757		return;
4758
4759	if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4760		return;
4761
4762	if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
4763		p2p_set_dev_name(p2p, wpa_s->conf->device_name);
4764
4765	if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
4766		p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
4767
4768	if (wpa_s->wps &&
4769	    (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
4770		p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
4771
4772	if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
4773		p2p_set_uuid(p2p, wpa_s->wps->uuid);
4774
4775	if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
4776		p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
4777		p2p_set_model_name(p2p, wpa_s->conf->model_name);
4778		p2p_set_model_number(p2p, wpa_s->conf->model_number);
4779		p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
4780	}
4781
4782	if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
4783		p2p_set_sec_dev_types(p2p,
4784				      (void *) wpa_s->conf->sec_device_type,
4785				      wpa_s->conf->num_sec_device_types);
4786
4787	if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
4788		int i;
4789		p2p_remove_wps_vendor_extensions(p2p);
4790		for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4791			if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4792				continue;
4793			p2p_add_wps_vendor_extension(
4794				p2p, wpa_s->conf->wps_vendor_ext[i]);
4795		}
4796	}
4797
4798	if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
4799	    wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4800		char country[3];
4801		country[0] = wpa_s->conf->country[0];
4802		country[1] = wpa_s->conf->country[1];
4803		country[2] = 0x04;
4804		p2p_set_country(p2p, country);
4805	}
4806
4807	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
4808		p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
4809				     wpa_s->conf->p2p_ssid_postfix ?
4810				     os_strlen(wpa_s->conf->p2p_ssid_postfix) :
4811				     0);
4812	}
4813
4814	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
4815		p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
4816
4817	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
4818		u8 reg_class, channel;
4819		int ret;
4820		unsigned int r;
4821		if (wpa_s->conf->p2p_listen_reg_class &&
4822		    wpa_s->conf->p2p_listen_channel) {
4823			reg_class = wpa_s->conf->p2p_listen_reg_class;
4824			channel = wpa_s->conf->p2p_listen_channel;
4825		} else {
4826			reg_class = 81;
4827			/*
4828			 * Pick one of the social channels randomly as the
4829			 * listen channel.
4830			 */
4831			os_get_random((u8 *) &r, sizeof(r));
4832			channel = 1 + (r % 3) * 5;
4833		}
4834		ret = p2p_set_listen_channel(p2p, reg_class, channel);
4835		if (ret)
4836			wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
4837				   "failed: %d", ret);
4838	}
4839	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
4840		u8 op_reg_class, op_channel, cfg_op_channel;
4841		int ret = 0;
4842		unsigned int r;
4843		if (wpa_s->conf->p2p_oper_reg_class &&
4844		    wpa_s->conf->p2p_oper_channel) {
4845			op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4846			op_channel = wpa_s->conf->p2p_oper_channel;
4847			cfg_op_channel = 1;
4848		} else {
4849			op_reg_class = 81;
4850			/*
4851			 * Use random operation channel from (1, 6, 11)
4852			 *if no other preference is indicated.
4853			 */
4854			os_get_random((u8 *) &r, sizeof(r));
4855			op_channel = 1 + (r % 3) * 5;
4856			cfg_op_channel = 0;
4857		}
4858		ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
4859					   cfg_op_channel);
4860		if (ret)
4861			wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
4862				   "failed: %d", ret);
4863	}
4864
4865	if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
4866		if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
4867				      wpa_s->conf->p2p_pref_chan) < 0) {
4868			wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
4869				   "update failed");
4870		}
4871	}
4872}
4873
4874
4875int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
4876		     int duration)
4877{
4878	if (!wpa_s->ap_iface)
4879		return -1;
4880	return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
4881				   duration);
4882}
4883
4884
4885int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
4886{
4887	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4888		return -1;
4889	if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4890		return -1;
4891
4892	wpa_s->global->cross_connection = enabled;
4893	p2p_set_cross_connect(wpa_s->global->p2p, enabled);
4894
4895	if (!enabled) {
4896		struct wpa_supplicant *iface;
4897
4898		for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
4899		{
4900			if (iface->cross_connect_enabled == 0)
4901				continue;
4902
4903			iface->cross_connect_enabled = 0;
4904			iface->cross_connect_in_use = 0;
4905			wpa_msg(iface->parent, MSG_INFO,
4906				P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4907				iface->ifname, iface->cross_connect_uplink);
4908		}
4909	}
4910
4911	return 0;
4912}
4913
4914
4915static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
4916{
4917	struct wpa_supplicant *iface;
4918
4919	if (!uplink->global->cross_connection)
4920		return;
4921
4922	for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4923		if (!iface->cross_connect_enabled)
4924			continue;
4925		if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4926		    0)
4927			continue;
4928		if (iface->ap_iface == NULL)
4929			continue;
4930		if (iface->cross_connect_in_use)
4931			continue;
4932
4933		iface->cross_connect_in_use = 1;
4934		wpa_msg(iface->parent, MSG_INFO,
4935			P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
4936			iface->ifname, iface->cross_connect_uplink);
4937	}
4938}
4939
4940
4941static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
4942{
4943	struct wpa_supplicant *iface;
4944
4945	for (iface = uplink->global->ifaces; iface; iface = iface->next) {
4946		if (!iface->cross_connect_enabled)
4947			continue;
4948		if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
4949		    0)
4950			continue;
4951		if (!iface->cross_connect_in_use)
4952			continue;
4953
4954		wpa_msg(iface->parent, MSG_INFO,
4955			P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
4956			iface->ifname, iface->cross_connect_uplink);
4957		iface->cross_connect_in_use = 0;
4958	}
4959}
4960
4961
4962void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
4963{
4964	if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
4965	    wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
4966	    wpa_s->cross_connect_disallowed)
4967		wpas_p2p_disable_cross_connect(wpa_s);
4968	else
4969		wpas_p2p_enable_cross_connect(wpa_s);
4970	if (!wpa_s->ap_iface &&
4971	    eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
4972		wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
4973}
4974
4975
4976void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
4977{
4978	wpas_p2p_disable_cross_connect(wpa_s);
4979	if (!wpa_s->ap_iface &&
4980	    !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
4981					 wpa_s, NULL))
4982		wpas_p2p_set_group_idle_timeout(wpa_s);
4983}
4984
4985
4986static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
4987{
4988	struct wpa_supplicant *iface;
4989
4990	if (!wpa_s->global->cross_connection)
4991		return;
4992
4993	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
4994		if (iface == wpa_s)
4995			continue;
4996		if (iface->drv_flags &
4997		    WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
4998			continue;
4999		if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5000			continue;
5001
5002		wpa_s->cross_connect_enabled = 1;
5003		os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5004			   sizeof(wpa_s->cross_connect_uplink));
5005		wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5006			   "%s to %s whenever uplink is available",
5007			   wpa_s->ifname, wpa_s->cross_connect_uplink);
5008
5009		if (iface->ap_iface || iface->current_ssid == NULL ||
5010		    iface->current_ssid->mode != WPAS_MODE_INFRA ||
5011		    iface->cross_connect_disallowed ||
5012		    iface->wpa_state != WPA_COMPLETED)
5013			break;
5014
5015		wpa_s->cross_connect_in_use = 1;
5016		wpa_msg(wpa_s->parent, MSG_INFO,
5017			P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5018			wpa_s->ifname, wpa_s->cross_connect_uplink);
5019		break;
5020	}
5021}
5022
5023
5024int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5025{
5026	if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5027	    !wpa_s->p2p_in_provisioning)
5028		return 0; /* not P2P client operation */
5029
5030	wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5031		   "session overlap");
5032	if (wpa_s != wpa_s->parent)
5033		wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5034
5035	if (wpa_s->global->p2p)
5036		p2p_group_formation_failed(wpa_s->global->p2p);
5037
5038	eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5039			     wpa_s->parent, NULL);
5040
5041	wpas_group_formation_completed(wpa_s, 0);
5042	return 1;
5043}
5044
5045
5046void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5047{
5048	struct p2p_channels chan;
5049
5050	if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5051		return;
5052
5053	os_memset(&chan, 0, sizeof(chan));
5054	if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5055		wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5056			   "channel list");
5057		return;
5058	}
5059
5060	p2p_update_channel_list(wpa_s->global->p2p, &chan);
5061}
5062
5063
5064int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5065{
5066	struct wpa_global *global = wpa_s->global;
5067	int found = 0;
5068	const u8 *peer;
5069
5070	if (global->p2p == NULL)
5071		return -1;
5072
5073	wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5074
5075	if (wpa_s->pending_interface_name[0] &&
5076	    !is_zero_ether_addr(wpa_s->pending_interface_addr))
5077		found = 1;
5078
5079	peer = p2p_get_go_neg_peer(global->p2p);
5080	if (peer) {
5081		wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5082			   MACSTR, MAC2STR(peer));
5083		p2p_unauthorize(global->p2p, peer);
5084		found = 1;
5085	}
5086
5087	wpas_p2p_stop_find(wpa_s);
5088
5089	for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5090		if (wpa_s == global->p2p_group_formation &&
5091		    (wpa_s->p2p_in_provisioning ||
5092		     wpa_s->parent->pending_interface_type ==
5093		     WPA_IF_P2P_CLIENT)) {
5094			wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5095				   "formation found - cancelling",
5096				   wpa_s->ifname);
5097			found = 1;
5098			eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5099					     wpa_s->parent, NULL);
5100			wpas_p2p_group_delete(wpa_s,
5101					      P2P_GROUP_REMOVAL_REQUESTED);
5102			break;
5103		}
5104	}
5105
5106	if (!found) {
5107		wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5108		return -1;
5109	}
5110
5111	return 0;
5112}
5113
5114
5115void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5116{
5117	if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5118		return;
5119
5120	wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5121		   "being available anymore");
5122	wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
5123}
5124
5125
5126void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5127				   int freq_24, int freq_5, int freq_overall)
5128{
5129	struct p2p_data *p2p = wpa_s->global->p2p;
5130	if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5131		return;
5132	p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5133}
5134
5135
5136int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5137{
5138	u8 peer[ETH_ALEN];
5139	struct p2p_data *p2p = wpa_s->global->p2p;
5140
5141	if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5142		return -1;
5143
5144	if (hwaddr_aton(addr, peer))
5145		return -1;
5146
5147	return p2p_unauthorize(p2p, peer);
5148}
5149
5150
5151/**
5152 * wpas_p2p_disconnect - Disconnect from a P2P Group
5153 * @wpa_s: Pointer to wpa_supplicant data
5154 * Returns: 0 on success, -1 on failure
5155 *
5156 * This can be used to disconnect from a group in which the local end is a P2P
5157 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5158 * virtual network interface was created for this group, that interface will be
5159 * removed. Otherwise, only the configured P2P group network will be removed
5160 * from the interface.
5161 */
5162int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5163{
5164
5165	if (wpa_s == NULL)
5166		return -1;
5167
5168	return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED);
5169}
5170
5171
5172int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5173{
5174#ifdef ANDROID_P2P
5175	struct wpa_supplicant *group = wpa_s;
5176#endif
5177
5178	if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5179		return 0;
5180
5181#ifdef ANDROID_P2P
5182	while (group && (group->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)) {
5183		if(group->wpa_state == WPA_ASSOCIATED) {
5184			/* WPA_ASSOCIATED hasn't moved to WPA_COMPLETED. So it could be in WPS
5185			 * or 4Way Hanshake phase. Avoid allowing scan during this time critical
5186			 * phase
5187			 */
5188			wpa_printf(MSG_ERROR, "P2P: WPS/4way handshake in Progress."
5189			" Defer SCAN ");
5190			return 1;
5191		}
5192		group = group->next;
5193	}
5194#endif
5195
5196	return p2p_in_progress(wpa_s->global->p2p);
5197}
5198
5199
5200void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5201			      struct wpa_ssid *ssid)
5202{
5203	if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5204	    eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5205				 wpa_s->parent, NULL) > 0) {
5206		/**
5207		 * Remove the network by scheduling the group formation
5208		 * timeout to happen immediately. The teardown code
5209		 * needs to be scheduled to run asynch later so that we
5210		 * don't delete data from under ourselves unexpectedly.
5211		 * Calling wpas_p2p_group_formation_timeout directly
5212		 * causes a series of crashes in WPS failure scenarios.
5213		 */
5214		wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5215			   "P2P group network getting removed");
5216		eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5217				       wpa_s->parent, NULL);
5218	}
5219}
5220
5221
5222struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
5223					  const u8 *addr, const u8 *ssid,
5224					  size_t ssid_len)
5225{
5226	struct wpa_ssid *s;
5227	size_t i;
5228
5229	for (s = wpa_s->conf->ssid; s; s = s->next) {
5230		if (s->disabled != 2)
5231			continue;
5232		if (ssid &&
5233		    (ssid_len != s->ssid_len ||
5234		     os_memcmp(ssid, s->ssid, ssid_len) != 0))
5235			continue;
5236		if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
5237			return s; /* peer is GO in the persistent group */
5238		if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
5239			continue;
5240		for (i = 0; i < s->num_p2p_clients; i++) {
5241			if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
5242				      addr, ETH_ALEN) == 0)
5243				return s; /* peer is P2P client in persistent
5244					   * group */
5245		}
5246	}
5247
5248	return NULL;
5249}
5250
5251
5252void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
5253				       const u8 *addr)
5254{
5255	if (addr == NULL)
5256		return;
5257	wpas_p2p_add_persistent_group_client(wpa_s, addr);
5258}
5259
5260
5261static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
5262					int group_added)
5263{
5264	struct wpa_supplicant *group = wpa_s;
5265	eloop_cancel_timeout(wpas_p2p_pd_before_join_timeout, wpa_s, NULL);
5266	if (wpa_s->global->p2p_group_formation)
5267		group = wpa_s->global->p2p_group_formation;
5268	wpa_s = wpa_s->parent;
5269	offchannel_send_action_done(wpa_s);
5270	if (group_added)
5271		wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
5272	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
5273	wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
5274			 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
5275			 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
5276			 wpa_s->p2p_persistent_id,
5277			 wpa_s->p2p_pd_before_go_neg,
5278			 wpa_s->p2p_go_ht40);
5279}
5280
5281
5282int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
5283{
5284	if (!wpa_s->p2p_fallback_to_go_neg ||
5285	    wpa_s->p2p_in_provisioning <= 5)
5286		return 0;
5287
5288	if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
5289		return 0; /* peer operating as a GO */
5290
5291	wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
5292		"fallback to GO Negotiation");
5293	wpas_p2p_fallback_to_go_neg(wpa_s, 1);
5294
5295	return 1;
5296}
5297
5298
5299unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
5300{
5301	const char *rn, *rn2;
5302	struct wpa_supplicant *ifs;
5303
5304	if (wpa_s->wpa_state > WPA_SCANNING) {
5305		wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
5306			"concurrent operation",
5307			P2P_CONCURRENT_SEARCH_DELAY);
5308		return P2P_CONCURRENT_SEARCH_DELAY;
5309	}
5310
5311	if (!wpa_s->driver->get_radio_name)
5312		return 0;
5313	rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
5314	if (rn == NULL || rn[0] == '\0')
5315		return 0;
5316
5317	for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
5318		if (ifs == wpa_s || !ifs->driver->get_radio_name)
5319			continue;
5320
5321		rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
5322		if (!rn2 || os_strcmp(rn, rn2) != 0)
5323			continue;
5324		if (ifs->wpa_state > WPA_SCANNING) {
5325			wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
5326				"delay due to concurrent operation on "
5327				"interface %s",
5328				P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
5329			return P2P_CONCURRENT_SEARCH_DELAY;
5330		}
5331	}
5332
5333	return 0;
5334}
5335
5336#ifdef ANDROID_P2P
5337int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq)
5338{
5339	struct wpa_supplicant *iface = NULL;
5340	struct p2p_data *p2p = wpa_s->global->p2p;
5341
5342	for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5343		if((iface->p2p_group_interface) && (iface->current_ssid) &&
5344			(iface->current_ssid->frequency != freq)) {
5345
5346			if (iface->p2p_group_interface == P2P_GROUP_INTERFACE_GO) {
5347					/* Try to see whether we can move the GO. If it
5348					 * is not possible, remove the GO interface
5349					 */
5350					if(wpa_drv_switch_channel(iface, freq) == 0) {
5351							wpa_printf(MSG_ERROR, "P2P: GO Moved to freq(%d)", freq);
5352							iface->current_ssid->frequency = freq;
5353							continue;
5354					}
5355			}
5356
5357			/* If GO cannot be moved or if the conflicting interface is a
5358			 * P2P Client, remove the interface depending up on the connection
5359			 * priority */
5360			if(!wpas_is_p2p_prioritized(wpa_s)) {
5361				/* STA connection has priority over existing
5362				 * P2P connection. So remove the interface */
5363				wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to Single channel"
5364						"concurrent mode frequency conflict");
5365				wpas_p2p_group_delete(iface, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
5366			} else {
5367				/* Existing connection has the priority. Disable the newly
5368                 * selected network and let the application know about it.
5369 				 */
5370				return -1;
5371			}
5372		}
5373	}
5374	return 0;
5375}
5376#endif
5377