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