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