xref: /freebsd/contrib/wpa/src/p2p/p2p.c (revision a90b9d0159070121c221b966469c3e36d912bf82)
1f05cddf9SRui Paulo /*
2f05cddf9SRui Paulo  * Wi-Fi Direct - P2P module
3f05cddf9SRui Paulo  * Copyright (c) 2009-2010, Atheros Communications
4f05cddf9SRui Paulo  *
5f05cddf9SRui Paulo  * This software may be distributed under the terms of the BSD license.
6f05cddf9SRui Paulo  * See README for more details.
7f05cddf9SRui Paulo  */
8f05cddf9SRui Paulo 
9f05cddf9SRui Paulo #include "includes.h"
10f05cddf9SRui Paulo 
11f05cddf9SRui Paulo #include "common.h"
12f05cddf9SRui Paulo #include "eloop.h"
13780fb4a2SCy Schubert #include "common/defs.h"
14f05cddf9SRui Paulo #include "common/ieee802_11_defs.h"
15f05cddf9SRui Paulo #include "common/ieee802_11_common.h"
16f05cddf9SRui Paulo #include "common/wpa_ctrl.h"
175b9c547cSRui Paulo #include "crypto/sha256.h"
185b9c547cSRui Paulo #include "crypto/crypto.h"
19f05cddf9SRui Paulo #include "wps/wps_i.h"
20f05cddf9SRui Paulo #include "p2p_i.h"
21f05cddf9SRui Paulo #include "p2p.h"
22f05cddf9SRui Paulo 
23f05cddf9SRui Paulo 
24f05cddf9SRui Paulo static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
25f05cddf9SRui Paulo static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
26f05cddf9SRui Paulo static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
27f05cddf9SRui Paulo 				     const u8 *sa, const u8 *data, size_t len,
28f05cddf9SRui Paulo 				     int rx_freq);
29f05cddf9SRui Paulo static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
30f05cddf9SRui Paulo 				      const u8 *sa, const u8 *data,
31f05cddf9SRui Paulo 				      size_t len);
32f05cddf9SRui Paulo static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
33f05cddf9SRui Paulo static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
34f05cddf9SRui Paulo 
35f05cddf9SRui Paulo 
36f05cddf9SRui Paulo /*
37f05cddf9SRui Paulo  * p2p_scan recovery timeout
38f05cddf9SRui Paulo  *
39f05cddf9SRui Paulo  * Many drivers are using 30 second timeout on scan results. Allow a bit larger
40f05cddf9SRui Paulo  * timeout for this to avoid hitting P2P timeout unnecessarily.
41f05cddf9SRui Paulo  */
42f05cddf9SRui Paulo #define P2P_SCAN_TIMEOUT 35
43f05cddf9SRui Paulo 
44f05cddf9SRui Paulo /**
45f05cddf9SRui Paulo  * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
46f05cddf9SRui Paulo  * entries will be removed
47f05cddf9SRui Paulo  */
485b9c547cSRui Paulo #ifndef P2P_PEER_EXPIRATION_AGE
495b9c547cSRui Paulo #define P2P_PEER_EXPIRATION_AGE 60
505b9c547cSRui Paulo #endif /* P2P_PEER_EXPIRATION_AGE */
51f05cddf9SRui Paulo 
52f05cddf9SRui Paulo 
p2p_expire_peers(struct p2p_data * p2p)53325151a3SRui Paulo void p2p_expire_peers(struct p2p_data *p2p)
54f05cddf9SRui Paulo {
55f05cddf9SRui Paulo 	struct p2p_device *dev, *n;
565b9c547cSRui Paulo 	struct os_reltime now;
57f05cddf9SRui Paulo 	size_t i;
58f05cddf9SRui Paulo 
595b9c547cSRui Paulo 	os_get_reltime(&now);
60f05cddf9SRui Paulo 	dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
61f05cddf9SRui Paulo 		if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
62f05cddf9SRui Paulo 			continue;
63f05cddf9SRui Paulo 
645b9c547cSRui Paulo 		if (dev == p2p->go_neg_peer) {
655b9c547cSRui Paulo 			/*
665b9c547cSRui Paulo 			 * GO Negotiation is in progress with the peer, so
675b9c547cSRui Paulo 			 * don't expire the peer entry until GO Negotiation
685b9c547cSRui Paulo 			 * fails or times out.
695b9c547cSRui Paulo 			 */
705b9c547cSRui Paulo 			continue;
715b9c547cSRui Paulo 		}
725b9c547cSRui Paulo 
73f05cddf9SRui Paulo 		if (p2p->cfg->go_connected &&
74f05cddf9SRui Paulo 		    p2p->cfg->go_connected(p2p->cfg->cb_ctx,
75f05cddf9SRui Paulo 					   dev->info.p2p_device_addr)) {
76f05cddf9SRui Paulo 			/*
77f05cddf9SRui Paulo 			 * We are connected as a client to a group in which the
78f05cddf9SRui Paulo 			 * peer is the GO, so do not expire the peer entry.
79f05cddf9SRui Paulo 			 */
805b9c547cSRui Paulo 			os_get_reltime(&dev->last_seen);
81f05cddf9SRui Paulo 			continue;
82f05cddf9SRui Paulo 		}
83f05cddf9SRui Paulo 
84f05cddf9SRui Paulo 		for (i = 0; i < p2p->num_groups; i++) {
85f05cddf9SRui Paulo 			if (p2p_group_is_client_connected(
86f05cddf9SRui Paulo 				    p2p->groups[i], dev->info.p2p_device_addr))
87f05cddf9SRui Paulo 				break;
88f05cddf9SRui Paulo 		}
89f05cddf9SRui Paulo 		if (i < p2p->num_groups) {
90f05cddf9SRui Paulo 			/*
91f05cddf9SRui Paulo 			 * The peer is connected as a client in a group where
92f05cddf9SRui Paulo 			 * we are the GO, so do not expire the peer entry.
93f05cddf9SRui Paulo 			 */
945b9c547cSRui Paulo 			os_get_reltime(&dev->last_seen);
95f05cddf9SRui Paulo 			continue;
96f05cddf9SRui Paulo 		}
97f05cddf9SRui Paulo 
985b9c547cSRui Paulo 		p2p_dbg(p2p, "Expiring old peer entry " MACSTR,
995b9c547cSRui Paulo 			MAC2STR(dev->info.p2p_device_addr));
100f05cddf9SRui Paulo 		dl_list_del(&dev->list);
101f05cddf9SRui Paulo 		p2p_device_free(p2p, dev);
102f05cddf9SRui Paulo 	}
103f05cddf9SRui Paulo }
104f05cddf9SRui Paulo 
105f05cddf9SRui Paulo 
p2p_state_txt(int state)106f05cddf9SRui Paulo static const char * p2p_state_txt(int state)
107f05cddf9SRui Paulo {
108f05cddf9SRui Paulo 	switch (state) {
109f05cddf9SRui Paulo 	case P2P_IDLE:
110f05cddf9SRui Paulo 		return "IDLE";
111f05cddf9SRui Paulo 	case P2P_SEARCH:
112f05cddf9SRui Paulo 		return "SEARCH";
113f05cddf9SRui Paulo 	case P2P_CONNECT:
114f05cddf9SRui Paulo 		return "CONNECT";
115f05cddf9SRui Paulo 	case P2P_CONNECT_LISTEN:
116f05cddf9SRui Paulo 		return "CONNECT_LISTEN";
117f05cddf9SRui Paulo 	case P2P_GO_NEG:
118f05cddf9SRui Paulo 		return "GO_NEG";
119f05cddf9SRui Paulo 	case P2P_LISTEN_ONLY:
120f05cddf9SRui Paulo 		return "LISTEN_ONLY";
121f05cddf9SRui Paulo 	case P2P_WAIT_PEER_CONNECT:
122f05cddf9SRui Paulo 		return "WAIT_PEER_CONNECT";
123f05cddf9SRui Paulo 	case P2P_WAIT_PEER_IDLE:
124f05cddf9SRui Paulo 		return "WAIT_PEER_IDLE";
125f05cddf9SRui Paulo 	case P2P_SD_DURING_FIND:
126f05cddf9SRui Paulo 		return "SD_DURING_FIND";
127f05cddf9SRui Paulo 	case P2P_PROVISIONING:
128f05cddf9SRui Paulo 		return "PROVISIONING";
129f05cddf9SRui Paulo 	case P2P_PD_DURING_FIND:
130f05cddf9SRui Paulo 		return "PD_DURING_FIND";
131f05cddf9SRui Paulo 	case P2P_INVITE:
132f05cddf9SRui Paulo 		return "INVITE";
133f05cddf9SRui Paulo 	case P2P_INVITE_LISTEN:
134f05cddf9SRui Paulo 		return "INVITE_LISTEN";
135f05cddf9SRui Paulo 	default:
136f05cddf9SRui Paulo 		return "?";
137f05cddf9SRui Paulo 	}
138f05cddf9SRui Paulo }
139f05cddf9SRui Paulo 
140f05cddf9SRui Paulo 
p2p_get_state_txt(struct p2p_data * p2p)1415b9c547cSRui Paulo const char * p2p_get_state_txt(struct p2p_data *p2p)
1425b9c547cSRui Paulo {
1435b9c547cSRui Paulo 	return p2p_state_txt(p2p->state);
1445b9c547cSRui Paulo }
1455b9c547cSRui Paulo 
1465b9c547cSRui Paulo 
p2p_get_p2ps_adv_list(struct p2p_data * p2p)1475b9c547cSRui Paulo struct p2ps_advertisement * p2p_get_p2ps_adv_list(struct p2p_data *p2p)
1485b9c547cSRui Paulo {
1495b9c547cSRui Paulo 	return p2p ? p2p->p2ps_adv_list : NULL;
1505b9c547cSRui Paulo }
1515b9c547cSRui Paulo 
1525b9c547cSRui Paulo 
p2p_set_intended_addr(struct p2p_data * p2p,const u8 * intended_addr)1535b9c547cSRui Paulo void p2p_set_intended_addr(struct p2p_data *p2p, const u8 *intended_addr)
1545b9c547cSRui Paulo {
1555b9c547cSRui Paulo 	if (p2p && intended_addr)
1565b9c547cSRui Paulo 		os_memcpy(p2p->intended_addr, intended_addr, ETH_ALEN);
1575b9c547cSRui Paulo }
1585b9c547cSRui Paulo 
1595b9c547cSRui Paulo 
p2p_get_provisioning_info(struct p2p_data * p2p,const u8 * addr)160f05cddf9SRui Paulo u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
161f05cddf9SRui Paulo {
162f05cddf9SRui Paulo 	struct p2p_device *dev = NULL;
163f05cddf9SRui Paulo 
164f05cddf9SRui Paulo 	if (!addr || !p2p)
165f05cddf9SRui Paulo 		return 0;
166f05cddf9SRui Paulo 
167f05cddf9SRui Paulo 	dev = p2p_get_device(p2p, addr);
168f05cddf9SRui Paulo 	if (dev)
169f05cddf9SRui Paulo 		return dev->wps_prov_info;
170f05cddf9SRui Paulo 	else
171f05cddf9SRui Paulo 		return 0;
172f05cddf9SRui Paulo }
173f05cddf9SRui Paulo 
174f05cddf9SRui Paulo 
p2p_clear_provisioning_info(struct p2p_data * p2p,const u8 * addr)175f05cddf9SRui Paulo void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
176f05cddf9SRui Paulo {
177f05cddf9SRui Paulo 	struct p2p_device *dev = NULL;
178f05cddf9SRui Paulo 
179f05cddf9SRui Paulo 	if (!addr || !p2p)
180f05cddf9SRui Paulo 		return;
181f05cddf9SRui Paulo 
182f05cddf9SRui Paulo 	dev = p2p_get_device(p2p, addr);
183f05cddf9SRui Paulo 	if (dev)
184f05cddf9SRui Paulo 		dev->wps_prov_info = 0;
185f05cddf9SRui Paulo }
186f05cddf9SRui Paulo 
187f05cddf9SRui Paulo 
p2p_set_state(struct p2p_data * p2p,int new_state)188f05cddf9SRui Paulo void p2p_set_state(struct p2p_data *p2p, int new_state)
189f05cddf9SRui Paulo {
1905b9c547cSRui Paulo 	p2p_dbg(p2p, "State %s -> %s",
191f05cddf9SRui Paulo 		p2p_state_txt(p2p->state), p2p_state_txt(new_state));
192f05cddf9SRui Paulo 	p2p->state = new_state;
1935b9c547cSRui Paulo 
1945b9c547cSRui Paulo 	if (new_state == P2P_IDLE && p2p->pending_channel) {
1955b9c547cSRui Paulo 		p2p_dbg(p2p, "Apply change in listen channel");
1965b9c547cSRui Paulo 		p2p->cfg->reg_class = p2p->pending_reg_class;
1975b9c547cSRui Paulo 		p2p->cfg->channel = p2p->pending_channel;
1985b9c547cSRui Paulo 		p2p->pending_reg_class = 0;
1995b9c547cSRui Paulo 		p2p->pending_channel = 0;
2005b9c547cSRui Paulo 	}
201f05cddf9SRui Paulo }
202f05cddf9SRui Paulo 
203f05cddf9SRui Paulo 
p2p_set_timeout(struct p2p_data * p2p,unsigned int sec,unsigned int usec)204f05cddf9SRui Paulo void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
205f05cddf9SRui Paulo {
2065b9c547cSRui Paulo 	p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec",
207f05cddf9SRui Paulo 		p2p_state_txt(p2p->state), sec, usec);
208f05cddf9SRui Paulo 	eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
209f05cddf9SRui Paulo 	eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
210f05cddf9SRui Paulo }
211f05cddf9SRui Paulo 
212f05cddf9SRui Paulo 
p2p_clear_timeout(struct p2p_data * p2p)213f05cddf9SRui Paulo void p2p_clear_timeout(struct p2p_data *p2p)
214f05cddf9SRui Paulo {
2155b9c547cSRui Paulo 	p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state));
216f05cddf9SRui Paulo 	eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
217f05cddf9SRui Paulo }
218f05cddf9SRui Paulo 
219f05cddf9SRui Paulo 
p2p_go_neg_failed(struct p2p_data * p2p,int status)2205b9c547cSRui Paulo void p2p_go_neg_failed(struct p2p_data *p2p, int status)
221f05cddf9SRui Paulo {
222f05cddf9SRui Paulo 	struct p2p_go_neg_results res;
2235b9c547cSRui Paulo 	struct p2p_device *peer = p2p->go_neg_peer;
2245b9c547cSRui Paulo 
2255b9c547cSRui Paulo 	if (!peer)
2265b9c547cSRui Paulo 		return;
2275b9c547cSRui Paulo 
2285b9c547cSRui Paulo 	eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
2295b9c547cSRui Paulo 	if (p2p->state != P2P_SEARCH) {
2305b9c547cSRui Paulo 		/*
2315b9c547cSRui Paulo 		 * Clear timeouts related to GO Negotiation if no new p2p_find
2325b9c547cSRui Paulo 		 * has been started.
2335b9c547cSRui Paulo 		 */
234f05cddf9SRui Paulo 		p2p_clear_timeout(p2p);
235f05cddf9SRui Paulo 		p2p_set_state(p2p, P2P_IDLE);
2365b9c547cSRui Paulo 	}
2375b9c547cSRui Paulo 
2385b9c547cSRui Paulo 	peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
2395b9c547cSRui Paulo 	peer->wps_method = WPS_NOT_READY;
2405b9c547cSRui Paulo 	peer->oob_pw_id = 0;
2415b9c547cSRui Paulo 	wpabuf_free(peer->go_neg_conf);
2425b9c547cSRui Paulo 	peer->go_neg_conf = NULL;
243f05cddf9SRui Paulo 	p2p->go_neg_peer = NULL;
244f05cddf9SRui Paulo 
245f05cddf9SRui Paulo 	os_memset(&res, 0, sizeof(res));
246f05cddf9SRui Paulo 	res.status = status;
2475b9c547cSRui Paulo 	os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
2485b9c547cSRui Paulo 	os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
249f05cddf9SRui Paulo 	p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
250f05cddf9SRui Paulo }
251f05cddf9SRui Paulo 
252f05cddf9SRui Paulo 
p2p_listen_in_find(struct p2p_data * p2p,int dev_disc)253f05cddf9SRui Paulo static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc)
254f05cddf9SRui Paulo {
255f05cddf9SRui Paulo 	unsigned int r, tu;
256f05cddf9SRui Paulo 	int freq;
257f05cddf9SRui Paulo 	struct wpabuf *ies;
258f05cddf9SRui Paulo 
2595b9c547cSRui Paulo 	p2p_dbg(p2p, "Starting short listen state (state=%s)",
260f05cddf9SRui Paulo 		p2p_state_txt(p2p->state));
261f05cddf9SRui Paulo 
2625b9c547cSRui Paulo 	if (p2p->pending_listen_freq) {
2635b9c547cSRui Paulo 		/* We have a pending p2p_listen request */
2645b9c547cSRui Paulo 		p2p_dbg(p2p, "p2p_listen command pending already");
265f05cddf9SRui Paulo 		return;
266f05cddf9SRui Paulo 	}
267f05cddf9SRui Paulo 
2685b9c547cSRui Paulo 	freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
2695b9c547cSRui Paulo 	if (freq < 0) {
2705b9c547cSRui Paulo 		p2p_dbg(p2p, "Unknown regulatory class/channel");
2715b9c547cSRui Paulo 		return;
2725b9c547cSRui Paulo 	}
2735b9c547cSRui Paulo 
2745b9c547cSRui Paulo 	if (os_get_random((u8 *) &r, sizeof(r)) < 0)
2755b9c547cSRui Paulo 		r = 0;
276f05cddf9SRui Paulo 	tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
277f05cddf9SRui Paulo 	      p2p->min_disc_int) * 100;
278f05cddf9SRui Paulo 	if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu)
279f05cddf9SRui Paulo 		tu = p2p->max_disc_tu;
280f05cddf9SRui Paulo 	if (!dev_disc && tu < 100)
281f05cddf9SRui Paulo 		tu = 100; /* Need to wait in non-device discovery use cases */
282f05cddf9SRui Paulo 	if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen)
283f05cddf9SRui Paulo 		tu = p2p->cfg->max_listen * 1000 / 1024;
284f05cddf9SRui Paulo 
285f05cddf9SRui Paulo 	if (tu == 0) {
2865b9c547cSRui Paulo 		p2p_dbg(p2p, "Skip listen state since duration was 0 TU");
287f05cddf9SRui Paulo 		p2p_set_timeout(p2p, 0, 0);
288f05cddf9SRui Paulo 		return;
289f05cddf9SRui Paulo 	}
290f05cddf9SRui Paulo 
291325151a3SRui Paulo 	ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
292f05cddf9SRui Paulo 	if (ies == NULL)
293f05cddf9SRui Paulo 		return;
294f05cddf9SRui Paulo 
2955b9c547cSRui Paulo 	p2p->pending_listen_freq = freq;
2965b9c547cSRui Paulo 	p2p->pending_listen_sec = 0;
2975b9c547cSRui Paulo 	p2p->pending_listen_usec = 1024 * tu;
2985b9c547cSRui Paulo 
299f05cddf9SRui Paulo 	if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
300f05cddf9SRui Paulo 		    ies) < 0) {
3015b9c547cSRui Paulo 		p2p_dbg(p2p, "Failed to start listen mode");
302f05cddf9SRui Paulo 		p2p->pending_listen_freq = 0;
303*a90b9d01SCy Schubert 	} else {
304*a90b9d01SCy Schubert 		p2p->pending_listen_wait_drv = true;
305f05cddf9SRui Paulo 	}
306f05cddf9SRui Paulo 	wpabuf_free(ies);
307f05cddf9SRui Paulo }
308f05cddf9SRui Paulo 
309f05cddf9SRui Paulo 
p2p_listen(struct p2p_data * p2p,unsigned int timeout)310f05cddf9SRui Paulo int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
311f05cddf9SRui Paulo {
312f05cddf9SRui Paulo 	int freq;
313f05cddf9SRui Paulo 	struct wpabuf *ies;
314f05cddf9SRui Paulo 
3155b9c547cSRui Paulo 	p2p_dbg(p2p, "Going to listen(only) state");
316f05cddf9SRui Paulo 
3175b9c547cSRui Paulo 	if (p2p->pending_listen_freq) {
3185b9c547cSRui Paulo 		/* We have a pending p2p_listen request */
3195b9c547cSRui Paulo 		p2p_dbg(p2p, "p2p_listen command pending already");
320f05cddf9SRui Paulo 		return -1;
321f05cddf9SRui Paulo 	}
322f05cddf9SRui Paulo 
3235b9c547cSRui Paulo 	freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
3245b9c547cSRui Paulo 	if (freq < 0) {
3255b9c547cSRui Paulo 		p2p_dbg(p2p, "Unknown regulatory class/channel");
3265b9c547cSRui Paulo 		return -1;
3275b9c547cSRui Paulo 	}
3285b9c547cSRui Paulo 
329f05cddf9SRui Paulo 	p2p->pending_listen_sec = timeout / 1000;
330f05cddf9SRui Paulo 	p2p->pending_listen_usec = (timeout % 1000) * 1000;
331f05cddf9SRui Paulo 
332f05cddf9SRui Paulo 	if (p2p->p2p_scan_running) {
333f05cddf9SRui Paulo 		if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
3345b9c547cSRui Paulo 			p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen");
335f05cddf9SRui Paulo 			return 0;
336f05cddf9SRui Paulo 		}
3375b9c547cSRui Paulo 		p2p_dbg(p2p, "p2p_scan running - delay start of listen state");
338f05cddf9SRui Paulo 		p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
339f05cddf9SRui Paulo 		return 0;
340f05cddf9SRui Paulo 	}
341f05cddf9SRui Paulo 
342325151a3SRui Paulo 	ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
343f05cddf9SRui Paulo 	if (ies == NULL)
344f05cddf9SRui Paulo 		return -1;
345f05cddf9SRui Paulo 
3465b9c547cSRui Paulo 	p2p->pending_listen_freq = freq;
3475b9c547cSRui Paulo 
348f05cddf9SRui Paulo 	if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
3495b9c547cSRui Paulo 		p2p_dbg(p2p, "Failed to start listen mode");
350f05cddf9SRui Paulo 		p2p->pending_listen_freq = 0;
351f05cddf9SRui Paulo 		wpabuf_free(ies);
352f05cddf9SRui Paulo 		return -1;
353f05cddf9SRui Paulo 	}
354*a90b9d01SCy Schubert 	p2p->pending_listen_wait_drv = true;
355f05cddf9SRui Paulo 	wpabuf_free(ies);
356f05cddf9SRui Paulo 
357f05cddf9SRui Paulo 	p2p_set_state(p2p, P2P_LISTEN_ONLY);
358f05cddf9SRui Paulo 
359f05cddf9SRui Paulo 	return 0;
360f05cddf9SRui Paulo }
361f05cddf9SRui Paulo 
362f05cddf9SRui Paulo 
p2p_device_clear_reported(struct p2p_data * p2p)363f05cddf9SRui Paulo static void p2p_device_clear_reported(struct p2p_data *p2p)
364f05cddf9SRui Paulo {
365f05cddf9SRui Paulo 	struct p2p_device *dev;
3665b9c547cSRui Paulo 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
367f05cddf9SRui Paulo 		dev->flags &= ~P2P_DEV_REPORTED;
3685b9c547cSRui Paulo 		dev->sd_reqs = 0;
3695b9c547cSRui Paulo 	}
370f05cddf9SRui Paulo }
371f05cddf9SRui Paulo 
372f05cddf9SRui Paulo 
373f05cddf9SRui Paulo /**
374f05cddf9SRui Paulo  * p2p_get_device - Fetch a peer entry
375f05cddf9SRui Paulo  * @p2p: P2P module context from p2p_init()
376f05cddf9SRui Paulo  * @addr: P2P Device Address of the peer
377f05cddf9SRui Paulo  * Returns: Pointer to the device entry or %NULL if not found
378f05cddf9SRui Paulo  */
p2p_get_device(struct p2p_data * p2p,const u8 * addr)379f05cddf9SRui Paulo struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
380f05cddf9SRui Paulo {
381f05cddf9SRui Paulo 	struct p2p_device *dev;
382f05cddf9SRui Paulo 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
383*a90b9d01SCy Schubert 		if (ether_addr_equal(dev->info.p2p_device_addr, addr))
384f05cddf9SRui Paulo 			return dev;
385f05cddf9SRui Paulo 	}
386f05cddf9SRui Paulo 	return NULL;
387f05cddf9SRui Paulo }
388f05cddf9SRui Paulo 
389f05cddf9SRui Paulo 
390f05cddf9SRui Paulo /**
391f05cddf9SRui Paulo  * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
392f05cddf9SRui Paulo  * @p2p: P2P module context from p2p_init()
393f05cddf9SRui Paulo  * @addr: P2P Interface Address of the peer
394f05cddf9SRui Paulo  * Returns: Pointer to the device entry or %NULL if not found
395f05cddf9SRui Paulo  */
p2p_get_device_interface(struct p2p_data * p2p,const u8 * addr)396f05cddf9SRui Paulo struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
397f05cddf9SRui Paulo 					     const u8 *addr)
398f05cddf9SRui Paulo {
399f05cddf9SRui Paulo 	struct p2p_device *dev;
400f05cddf9SRui Paulo 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
401*a90b9d01SCy Schubert 		if (ether_addr_equal(dev->interface_addr, addr))
402f05cddf9SRui Paulo 			return dev;
403f05cddf9SRui Paulo 	}
404f05cddf9SRui Paulo 	return NULL;
405f05cddf9SRui Paulo }
406f05cddf9SRui Paulo 
407f05cddf9SRui Paulo 
408f05cddf9SRui Paulo /**
409f05cddf9SRui Paulo  * p2p_create_device - Create a peer entry
410f05cddf9SRui Paulo  * @p2p: P2P module context from p2p_init()
411f05cddf9SRui Paulo  * @addr: P2P Device Address of the peer
412f05cddf9SRui Paulo  * Returns: Pointer to the device entry or %NULL on failure
413f05cddf9SRui Paulo  *
414f05cddf9SRui Paulo  * If there is already an entry for the peer, it will be returned instead of
415f05cddf9SRui Paulo  * creating a new one.
416f05cddf9SRui Paulo  */
p2p_create_device(struct p2p_data * p2p,const u8 * addr)417f05cddf9SRui Paulo static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
418f05cddf9SRui Paulo 					     const u8 *addr)
419f05cddf9SRui Paulo {
420f05cddf9SRui Paulo 	struct p2p_device *dev, *oldest = NULL;
421f05cddf9SRui Paulo 	size_t count = 0;
422f05cddf9SRui Paulo 
423f05cddf9SRui Paulo 	dev = p2p_get_device(p2p, addr);
424f05cddf9SRui Paulo 	if (dev)
425f05cddf9SRui Paulo 		return dev;
426f05cddf9SRui Paulo 
427f05cddf9SRui Paulo 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
428f05cddf9SRui Paulo 		count++;
429f05cddf9SRui Paulo 		if (oldest == NULL ||
4305b9c547cSRui Paulo 		    os_reltime_before(&dev->last_seen, &oldest->last_seen))
431f05cddf9SRui Paulo 			oldest = dev;
432f05cddf9SRui Paulo 	}
433f05cddf9SRui Paulo 	if (count + 1 > p2p->cfg->max_peers && oldest) {
434c1d255d3SCy Schubert 		p2p_dbg(p2p,
435c1d255d3SCy Schubert 			"Remove oldest peer entry to make room for a new peer "
436c1d255d3SCy Schubert 			MACSTR, MAC2STR(oldest->info.p2p_device_addr));
437f05cddf9SRui Paulo 		dl_list_del(&oldest->list);
438f05cddf9SRui Paulo 		p2p_device_free(p2p, oldest);
439f05cddf9SRui Paulo 	}
440f05cddf9SRui Paulo 
441f05cddf9SRui Paulo 	dev = os_zalloc(sizeof(*dev));
442f05cddf9SRui Paulo 	if (dev == NULL)
443f05cddf9SRui Paulo 		return NULL;
444f05cddf9SRui Paulo 	dl_list_add(&p2p->devices, &dev->list);
445f05cddf9SRui Paulo 	os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
446*a90b9d01SCy Schubert 	dev->support_6ghz = false;
447f05cddf9SRui Paulo 
448f05cddf9SRui Paulo 	return dev;
449f05cddf9SRui Paulo }
450f05cddf9SRui Paulo 
451f05cddf9SRui Paulo 
p2p_copy_client_info(struct p2p_device * dev,struct p2p_client_info * cli)452f05cddf9SRui Paulo static void p2p_copy_client_info(struct p2p_device *dev,
453f05cddf9SRui Paulo 				 struct p2p_client_info *cli)
454f05cddf9SRui Paulo {
455780fb4a2SCy Schubert 	p2p_copy_filter_devname(dev->info.device_name,
456780fb4a2SCy Schubert 				sizeof(dev->info.device_name),
457780fb4a2SCy Schubert 				cli->dev_name, cli->dev_name_len);
458f05cddf9SRui Paulo 	dev->info.dev_capab = cli->dev_capab;
459f05cddf9SRui Paulo 	dev->info.config_methods = cli->config_methods;
460f05cddf9SRui Paulo 	os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
461f05cddf9SRui Paulo 	dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
462c1d255d3SCy Schubert 	if (dev->info.wps_sec_dev_type_list_len > WPS_SEC_DEV_TYPE_MAX_LEN)
463c1d255d3SCy Schubert 		dev->info.wps_sec_dev_type_list_len = WPS_SEC_DEV_TYPE_MAX_LEN;
464f05cddf9SRui Paulo 	os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
465f05cddf9SRui Paulo 		  dev->info.wps_sec_dev_type_list_len);
466f05cddf9SRui Paulo }
467f05cddf9SRui Paulo 
468f05cddf9SRui Paulo 
p2p_add_group_clients(struct p2p_data * p2p,const u8 * go_dev_addr,const u8 * go_interface_addr,int freq,const u8 * gi,size_t gi_len,struct os_reltime * rx_time)469f05cddf9SRui Paulo static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
470f05cddf9SRui Paulo 				 const u8 *go_interface_addr, int freq,
471325151a3SRui Paulo 				 const u8 *gi, size_t gi_len,
472325151a3SRui Paulo 				 struct os_reltime *rx_time)
473f05cddf9SRui Paulo {
474f05cddf9SRui Paulo 	struct p2p_group_info info;
475f05cddf9SRui Paulo 	size_t c;
476f05cddf9SRui Paulo 	struct p2p_device *dev;
477f05cddf9SRui Paulo 
478f05cddf9SRui Paulo 	if (gi == NULL)
479f05cddf9SRui Paulo 		return 0;
480f05cddf9SRui Paulo 
481f05cddf9SRui Paulo 	if (p2p_group_info_parse(gi, gi_len, &info) < 0)
482f05cddf9SRui Paulo 		return -1;
483f05cddf9SRui Paulo 
484f05cddf9SRui Paulo 	/*
485f05cddf9SRui Paulo 	 * Clear old data for this group; if the devices are still in the
486f05cddf9SRui Paulo 	 * group, the information will be restored in the loop following this.
487f05cddf9SRui Paulo 	 */
488f05cddf9SRui Paulo 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
489*a90b9d01SCy Schubert 		if (ether_addr_equal(dev->member_in_go_iface,
490*a90b9d01SCy Schubert 				     go_interface_addr)) {
491f05cddf9SRui Paulo 			os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
492f05cddf9SRui Paulo 			os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
493f05cddf9SRui Paulo 		}
494f05cddf9SRui Paulo 	}
495f05cddf9SRui Paulo 
496f05cddf9SRui Paulo 	for (c = 0; c < info.num_clients; c++) {
497f05cddf9SRui Paulo 		struct p2p_client_info *cli = &info.client[c];
498*a90b9d01SCy Schubert 		if (ether_addr_equal(cli->p2p_device_addr, p2p->cfg->dev_addr))
499f05cddf9SRui Paulo 			continue; /* ignore our own entry */
500f05cddf9SRui Paulo 		dev = p2p_get_device(p2p, cli->p2p_device_addr);
501f05cddf9SRui Paulo 		if (dev) {
502f05cddf9SRui Paulo 			if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
503f05cddf9SRui Paulo 					  P2P_DEV_PROBE_REQ_ONLY)) {
504f05cddf9SRui Paulo 				/*
505f05cddf9SRui Paulo 				 * Update information since we have not
506f05cddf9SRui Paulo 				 * received this directly from the client.
507f05cddf9SRui Paulo 				 */
508f05cddf9SRui Paulo 				p2p_copy_client_info(dev, cli);
509f05cddf9SRui Paulo 			} else {
510f05cddf9SRui Paulo 				/*
511f05cddf9SRui Paulo 				 * Need to update P2P Client Discoverability
512f05cddf9SRui Paulo 				 * flag since it is valid only in P2P Group
513f05cddf9SRui Paulo 				 * Info attribute.
514f05cddf9SRui Paulo 				 */
515f05cddf9SRui Paulo 				dev->info.dev_capab &=
516f05cddf9SRui Paulo 					~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
517f05cddf9SRui Paulo 				dev->info.dev_capab |=
518f05cddf9SRui Paulo 					cli->dev_capab &
519f05cddf9SRui Paulo 					P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
520f05cddf9SRui Paulo 			}
521f05cddf9SRui Paulo 			if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
522f05cddf9SRui Paulo 				dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
523f05cddf9SRui Paulo 			}
524f05cddf9SRui Paulo 		} else {
525f05cddf9SRui Paulo 			dev = p2p_create_device(p2p, cli->p2p_device_addr);
526f05cddf9SRui Paulo 			if (dev == NULL)
527f05cddf9SRui Paulo 				continue;
528f05cddf9SRui Paulo 			dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
529f05cddf9SRui Paulo 			p2p_copy_client_info(dev, cli);
530f05cddf9SRui Paulo 			dev->oper_freq = freq;
531f05cddf9SRui Paulo 			p2p->cfg->dev_found(p2p->cfg->cb_ctx,
532f05cddf9SRui Paulo 					    dev->info.p2p_device_addr,
533f05cddf9SRui Paulo 					    &dev->info, 1);
534f05cddf9SRui Paulo 			dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
535f05cddf9SRui Paulo 		}
536f05cddf9SRui Paulo 
537f05cddf9SRui Paulo 		os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
538f05cddf9SRui Paulo 			  ETH_ALEN);
539325151a3SRui Paulo 		os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
540f05cddf9SRui Paulo 		os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
541f05cddf9SRui Paulo 		os_memcpy(dev->member_in_go_iface, go_interface_addr,
542f05cddf9SRui Paulo 			  ETH_ALEN);
543325151a3SRui Paulo 		dev->flags |= P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT;
544f05cddf9SRui Paulo 	}
545f05cddf9SRui Paulo 
546f05cddf9SRui Paulo 	return 0;
547f05cddf9SRui Paulo }
548f05cddf9SRui Paulo 
549f05cddf9SRui Paulo 
p2p_copy_wps_info(struct p2p_data * p2p,struct p2p_device * dev,int probe_req,const struct p2p_message * msg)5505b9c547cSRui Paulo static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev,
5515b9c547cSRui Paulo 			      int probe_req, const struct p2p_message *msg)
552f05cddf9SRui Paulo {
553f05cddf9SRui Paulo 	os_memcpy(dev->info.device_name, msg->device_name,
554f05cddf9SRui Paulo 		  sizeof(dev->info.device_name));
555f05cddf9SRui Paulo 
556f05cddf9SRui Paulo 	if (msg->manufacturer &&
557f05cddf9SRui Paulo 	    msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
558f05cddf9SRui Paulo 		os_memset(dev->info.manufacturer, 0,
559f05cddf9SRui Paulo 			  sizeof(dev->info.manufacturer));
560f05cddf9SRui Paulo 		os_memcpy(dev->info.manufacturer, msg->manufacturer,
561f05cddf9SRui Paulo 			  msg->manufacturer_len);
562f05cddf9SRui Paulo 	}
563f05cddf9SRui Paulo 
564f05cddf9SRui Paulo 	if (msg->model_name &&
565f05cddf9SRui Paulo 	    msg->model_name_len < sizeof(dev->info.model_name)) {
566f05cddf9SRui Paulo 		os_memset(dev->info.model_name, 0,
567f05cddf9SRui Paulo 			  sizeof(dev->info.model_name));
568f05cddf9SRui Paulo 		os_memcpy(dev->info.model_name, msg->model_name,
569f05cddf9SRui Paulo 			  msg->model_name_len);
570f05cddf9SRui Paulo 	}
571f05cddf9SRui Paulo 
572f05cddf9SRui Paulo 	if (msg->model_number &&
573f05cddf9SRui Paulo 	    msg->model_number_len < sizeof(dev->info.model_number)) {
574f05cddf9SRui Paulo 		os_memset(dev->info.model_number, 0,
575f05cddf9SRui Paulo 			  sizeof(dev->info.model_number));
576f05cddf9SRui Paulo 		os_memcpy(dev->info.model_number, msg->model_number,
577f05cddf9SRui Paulo 			  msg->model_number_len);
578f05cddf9SRui Paulo 	}
579f05cddf9SRui Paulo 
580f05cddf9SRui Paulo 	if (msg->serial_number &&
581f05cddf9SRui Paulo 	    msg->serial_number_len < sizeof(dev->info.serial_number)) {
582f05cddf9SRui Paulo 		os_memset(dev->info.serial_number, 0,
583f05cddf9SRui Paulo 			  sizeof(dev->info.serial_number));
584f05cddf9SRui Paulo 		os_memcpy(dev->info.serial_number, msg->serial_number,
585f05cddf9SRui Paulo 			  msg->serial_number_len);
586f05cddf9SRui Paulo 	}
587f05cddf9SRui Paulo 
588f05cddf9SRui Paulo 	if (msg->pri_dev_type)
589f05cddf9SRui Paulo 		os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
590f05cddf9SRui Paulo 			  sizeof(dev->info.pri_dev_type));
591f05cddf9SRui Paulo 	else if (msg->wps_pri_dev_type)
592f05cddf9SRui Paulo 		os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
593f05cddf9SRui Paulo 			  sizeof(dev->info.pri_dev_type));
594f05cddf9SRui Paulo 
595f05cddf9SRui Paulo 	if (msg->wps_sec_dev_type_list) {
596f05cddf9SRui Paulo 		os_memcpy(dev->info.wps_sec_dev_type_list,
597f05cddf9SRui Paulo 			  msg->wps_sec_dev_type_list,
598f05cddf9SRui Paulo 			  msg->wps_sec_dev_type_list_len);
599f05cddf9SRui Paulo 		dev->info.wps_sec_dev_type_list_len =
600f05cddf9SRui Paulo 			msg->wps_sec_dev_type_list_len;
601f05cddf9SRui Paulo 	}
602f05cddf9SRui Paulo 
603f05cddf9SRui Paulo 	if (msg->capability) {
604f05cddf9SRui Paulo 		/*
605f05cddf9SRui Paulo 		 * P2P Client Discoverability bit is reserved in all frames
606f05cddf9SRui Paulo 		 * that use this function, so do not change its value here.
607f05cddf9SRui Paulo 		 */
608f05cddf9SRui Paulo 		dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
609f05cddf9SRui Paulo 		dev->info.dev_capab |= msg->capability[0] &
610f05cddf9SRui Paulo 			~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
611f05cddf9SRui Paulo 		dev->info.group_capab = msg->capability[1];
612f05cddf9SRui Paulo 	}
613f05cddf9SRui Paulo 
614*a90b9d01SCy Schubert 	p2p_update_peer_6ghz_capab(dev, msg);
615*a90b9d01SCy Schubert 
616f05cddf9SRui Paulo 	if (msg->ext_listen_timing) {
617f05cddf9SRui Paulo 		dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
618f05cddf9SRui Paulo 		dev->ext_listen_interval =
619f05cddf9SRui Paulo 			WPA_GET_LE16(msg->ext_listen_timing + 2);
620f05cddf9SRui Paulo 	}
621f05cddf9SRui Paulo 
622f05cddf9SRui Paulo 	if (!probe_req) {
6235b9c547cSRui Paulo 		u16 new_config_methods;
6245b9c547cSRui Paulo 		new_config_methods = msg->config_methods ?
625f05cddf9SRui Paulo 			msg->config_methods : msg->wps_config_methods;
6265b9c547cSRui Paulo 		if (new_config_methods &&
6275b9c547cSRui Paulo 		    dev->info.config_methods != new_config_methods) {
6285b9c547cSRui Paulo 			p2p_dbg(p2p, "Update peer " MACSTR
6295b9c547cSRui Paulo 				" config_methods 0x%x -> 0x%x",
6305b9c547cSRui Paulo 				MAC2STR(dev->info.p2p_device_addr),
6315b9c547cSRui Paulo 				dev->info.config_methods,
6325b9c547cSRui Paulo 				new_config_methods);
6335b9c547cSRui Paulo 			dev->info.config_methods = new_config_methods;
634f05cddf9SRui Paulo 		}
635f05cddf9SRui Paulo 	}
6365b9c547cSRui Paulo }
6375b9c547cSRui Paulo 
6385b9c547cSRui Paulo 
p2p_update_peer_6ghz_capab(struct p2p_device * dev,const struct p2p_message * msg)639*a90b9d01SCy Schubert void p2p_update_peer_6ghz_capab(struct p2p_device *dev,
640*a90b9d01SCy Schubert 				const struct p2p_message *msg)
641*a90b9d01SCy Schubert {
642*a90b9d01SCy Schubert 	if (msg->capability &&
643*a90b9d01SCy Schubert 	    (msg->capability[0] & P2P_DEV_CAPAB_6GHZ_BAND_CAPABLE))
644*a90b9d01SCy Schubert 		dev->support_6ghz = true;
645*a90b9d01SCy Schubert }
646*a90b9d01SCy Schubert 
647*a90b9d01SCy Schubert 
p2p_update_peer_vendor_elems(struct p2p_device * dev,const u8 * ies,size_t ies_len)6485b9c547cSRui Paulo static void p2p_update_peer_vendor_elems(struct p2p_device *dev, const u8 *ies,
6495b9c547cSRui Paulo 					 size_t ies_len)
6505b9c547cSRui Paulo {
6515b9c547cSRui Paulo 	const u8 *pos, *end;
6525b9c547cSRui Paulo 	u8 id, len;
6535b9c547cSRui Paulo 
6545b9c547cSRui Paulo 	wpabuf_free(dev->info.vendor_elems);
6555b9c547cSRui Paulo 	dev->info.vendor_elems = NULL;
6565b9c547cSRui Paulo 
6575b9c547cSRui Paulo 	end = ies + ies_len;
6585b9c547cSRui Paulo 
659780fb4a2SCy Schubert 	for (pos = ies; end - pos > 1; pos += len) {
6605b9c547cSRui Paulo 		id = *pos++;
6615b9c547cSRui Paulo 		len = *pos++;
6625b9c547cSRui Paulo 
663780fb4a2SCy Schubert 		if (len > end - pos)
6645b9c547cSRui Paulo 			break;
6655b9c547cSRui Paulo 
6665b9c547cSRui Paulo 		if (id != WLAN_EID_VENDOR_SPECIFIC || len < 3)
6675b9c547cSRui Paulo 			continue;
6685b9c547cSRui Paulo 
6695b9c547cSRui Paulo 		if (len >= 4) {
6705b9c547cSRui Paulo 			u32 type = WPA_GET_BE32(pos);
6715b9c547cSRui Paulo 
6725b9c547cSRui Paulo 			if (type == WPA_IE_VENDOR_TYPE ||
6735b9c547cSRui Paulo 			    type == WMM_IE_VENDOR_TYPE ||
6745b9c547cSRui Paulo 			    type == WPS_IE_VENDOR_TYPE ||
6755b9c547cSRui Paulo 			    type == P2P_IE_VENDOR_TYPE ||
6765b9c547cSRui Paulo 			    type == WFD_IE_VENDOR_TYPE)
6775b9c547cSRui Paulo 				continue;
6785b9c547cSRui Paulo 		}
6795b9c547cSRui Paulo 
6805b9c547cSRui Paulo 		/* Unknown vendor element - make raw IE data available */
6815b9c547cSRui Paulo 		if (wpabuf_resize(&dev->info.vendor_elems, 2 + len) < 0)
6825b9c547cSRui Paulo 			break;
6835b9c547cSRui Paulo 		wpabuf_put_data(dev->info.vendor_elems, pos - 2, 2 + len);
684c1d255d3SCy Schubert 		if (wpabuf_size(dev->info.vendor_elems) > 2000)
685c1d255d3SCy Schubert 			break;
6865b9c547cSRui Paulo 	}
6875b9c547cSRui Paulo }
6885b9c547cSRui Paulo 
6895b9c547cSRui Paulo 
p2p_compare_wfd_info(struct p2p_device * dev,const struct p2p_message * msg)6905b9c547cSRui Paulo static int p2p_compare_wfd_info(struct p2p_device *dev,
6915b9c547cSRui Paulo 			      const struct p2p_message *msg)
6925b9c547cSRui Paulo {
6935b9c547cSRui Paulo 	if (dev->info.wfd_subelems && msg->wfd_subelems) {
6945b9c547cSRui Paulo 		if (dev->info.wfd_subelems->used != msg->wfd_subelems->used)
6955b9c547cSRui Paulo 			return 1;
6965b9c547cSRui Paulo 
6975b9c547cSRui Paulo 		return os_memcmp(dev->info.wfd_subelems->buf,
6985b9c547cSRui Paulo 				 msg->wfd_subelems->buf,
6995b9c547cSRui Paulo 				 dev->info.wfd_subelems->used);
7005b9c547cSRui Paulo 	}
7015b9c547cSRui Paulo 	if (dev->info.wfd_subelems || msg->wfd_subelems)
7025b9c547cSRui Paulo 		return 1;
7035b9c547cSRui Paulo 
7045b9c547cSRui Paulo 	return 0;
7055b9c547cSRui Paulo }
706f05cddf9SRui Paulo 
707f05cddf9SRui Paulo 
708f05cddf9SRui Paulo /**
709f05cddf9SRui Paulo  * p2p_add_device - Add peer entries based on scan results or P2P frames
710f05cddf9SRui Paulo  * @p2p: P2P module context from p2p_init()
711f05cddf9SRui Paulo  * @addr: Source address of Beacon or Probe Response frame (may be either
712f05cddf9SRui Paulo  *	P2P Device Address or P2P Interface Address)
713f05cddf9SRui Paulo  * @level: Signal level (signal strength of the received frame from the peer)
714f05cddf9SRui Paulo  * @freq: Frequency on which the Beacon or Probe Response frame was received
7155b9c547cSRui Paulo  * @rx_time: Time when the result was received
716f05cddf9SRui Paulo  * @ies: IEs from the Beacon or Probe Response frame
717f05cddf9SRui Paulo  * @ies_len: Length of ies buffer in octets
718f05cddf9SRui Paulo  * @scan_res: Whether this was based on scan results
719f05cddf9SRui Paulo  * Returns: 0 on success, -1 on failure
720f05cddf9SRui Paulo  *
721f05cddf9SRui Paulo  * If the scan result is for a GO, the clients in the group will also be added
722f05cddf9SRui Paulo  * to the peer table. This function can also be used with some other frames
723f05cddf9SRui Paulo  * like Provision Discovery Request that contains P2P Capability and P2P Device
724f05cddf9SRui Paulo  * Info attributes.
725f05cddf9SRui Paulo  */
p2p_add_device(struct p2p_data * p2p,const u8 * addr,int freq,struct os_reltime * rx_time,int level,const u8 * ies,size_t ies_len,int scan_res)726f05cddf9SRui Paulo int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
7275b9c547cSRui Paulo 		   struct os_reltime *rx_time, int level, const u8 *ies,
728f05cddf9SRui Paulo 		   size_t ies_len, int scan_res)
729f05cddf9SRui Paulo {
730f05cddf9SRui Paulo 	struct p2p_device *dev;
731f05cddf9SRui Paulo 	struct p2p_message msg;
732f05cddf9SRui Paulo 	const u8 *p2p_dev_addr;
7335b9c547cSRui Paulo 	int wfd_changed;
73485732ac8SCy Schubert 	int dev_name_changed;
735f05cddf9SRui Paulo 	int i;
7365b9c547cSRui Paulo 	struct os_reltime time_now;
737f05cddf9SRui Paulo 
738f05cddf9SRui Paulo 	os_memset(&msg, 0, sizeof(msg));
739f05cddf9SRui Paulo 	if (p2p_parse_ies(ies, ies_len, &msg)) {
7405b9c547cSRui Paulo 		p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
741f05cddf9SRui Paulo 		p2p_parse_free(&msg);
742f05cddf9SRui Paulo 		return -1;
743f05cddf9SRui Paulo 	}
744f05cddf9SRui Paulo 
745f05cddf9SRui Paulo 	if (msg.p2p_device_addr)
746f05cddf9SRui Paulo 		p2p_dev_addr = msg.p2p_device_addr;
747f05cddf9SRui Paulo 	else if (msg.device_id)
748f05cddf9SRui Paulo 		p2p_dev_addr = msg.device_id;
749f05cddf9SRui Paulo 	else {
7505b9c547cSRui Paulo 		p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
751f05cddf9SRui Paulo 		p2p_parse_free(&msg);
752f05cddf9SRui Paulo 		return -1;
753f05cddf9SRui Paulo 	}
754f05cddf9SRui Paulo 
755f05cddf9SRui Paulo 	if (!is_zero_ether_addr(p2p->peer_filter) &&
756*a90b9d01SCy Schubert 	    !ether_addr_equal(p2p_dev_addr, p2p->peer_filter)) {
7575b9c547cSRui Paulo 		p2p_dbg(p2p, "Do not add peer filter for " MACSTR
7585b9c547cSRui Paulo 			" due to peer filter", MAC2STR(p2p_dev_addr));
759f05cddf9SRui Paulo 		p2p_parse_free(&msg);
760f05cddf9SRui Paulo 		return 0;
761f05cddf9SRui Paulo 	}
762f05cddf9SRui Paulo 
763f05cddf9SRui Paulo 	dev = p2p_create_device(p2p, p2p_dev_addr);
764f05cddf9SRui Paulo 	if (dev == NULL) {
765f05cddf9SRui Paulo 		p2p_parse_free(&msg);
766f05cddf9SRui Paulo 		return -1;
767f05cddf9SRui Paulo 	}
768f05cddf9SRui Paulo 
7695b9c547cSRui Paulo 	if (rx_time == NULL) {
7705b9c547cSRui Paulo 		os_get_reltime(&time_now);
7715b9c547cSRui Paulo 		rx_time = &time_now;
7725b9c547cSRui Paulo 	}
773f05cddf9SRui Paulo 
774f05cddf9SRui Paulo 	/*
775f05cddf9SRui Paulo 	 * Update the device entry only if the new peer
776325151a3SRui Paulo 	 * entry is newer than the one previously stored, or if
777325151a3SRui Paulo 	 * the device was previously seen as a P2P Client in a group
778325151a3SRui Paulo 	 * and the new entry isn't older than a threshold.
779f05cddf9SRui Paulo 	 */
7805b9c547cSRui Paulo 	if (dev->last_seen.sec > 0 &&
781325151a3SRui Paulo 	    os_reltime_before(rx_time, &dev->last_seen) &&
782325151a3SRui Paulo 	    (!(dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT) ||
783325151a3SRui Paulo 	     os_reltime_expired(&dev->last_seen, rx_time,
784325151a3SRui Paulo 				P2P_DEV_GROUP_CLIENT_RESP_THRESHOLD))) {
785325151a3SRui Paulo 		p2p_dbg(p2p,
786325151a3SRui Paulo 			"Do not update peer entry based on old frame (rx_time=%u.%06u last_seen=%u.%06u flags=0x%x)",
7875b9c547cSRui Paulo 			(unsigned int) rx_time->sec,
7885b9c547cSRui Paulo 			(unsigned int) rx_time->usec,
7895b9c547cSRui Paulo 			(unsigned int) dev->last_seen.sec,
790325151a3SRui Paulo 			(unsigned int) dev->last_seen.usec,
791325151a3SRui Paulo 			dev->flags);
792f05cddf9SRui Paulo 		p2p_parse_free(&msg);
793f05cddf9SRui Paulo 		return -1;
794f05cddf9SRui Paulo 	}
795f05cddf9SRui Paulo 
7965b9c547cSRui Paulo 	os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
797f05cddf9SRui Paulo 
798325151a3SRui Paulo 	dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY |
799325151a3SRui Paulo 			P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT);
800f05cddf9SRui Paulo 
801*a90b9d01SCy Schubert 	if (!ether_addr_equal(addr, p2p_dev_addr))
802f05cddf9SRui Paulo 		os_memcpy(dev->interface_addr, addr, ETH_ALEN);
803f05cddf9SRui Paulo 	if (msg.ssid &&
804325151a3SRui Paulo 	    msg.ssid[1] <= sizeof(dev->oper_ssid) &&
805f05cddf9SRui Paulo 	    (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
806f05cddf9SRui Paulo 	     os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
807f05cddf9SRui Paulo 	     != 0)) {
808f05cddf9SRui Paulo 		os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
809f05cddf9SRui Paulo 		dev->oper_ssid_len = msg.ssid[1];
810f05cddf9SRui Paulo 	}
811f05cddf9SRui Paulo 
8125b9c547cSRui Paulo 	wpabuf_free(dev->info.p2ps_instance);
813780fb4a2SCy Schubert 	dev->info.p2ps_instance = NULL;
814780fb4a2SCy Schubert 	if (msg.adv_service_instance && msg.adv_service_instance_len)
8155b9c547cSRui Paulo 		dev->info.p2ps_instance = wpabuf_alloc_copy(
8165b9c547cSRui Paulo 			msg.adv_service_instance, msg.adv_service_instance_len);
8175b9c547cSRui Paulo 
818f05cddf9SRui Paulo 	if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
819f05cddf9SRui Paulo 	    *msg.ds_params >= 1 && *msg.ds_params <= 14) {
820f05cddf9SRui Paulo 		int ds_freq;
821f05cddf9SRui Paulo 		if (*msg.ds_params == 14)
822f05cddf9SRui Paulo 			ds_freq = 2484;
823f05cddf9SRui Paulo 		else
824f05cddf9SRui Paulo 			ds_freq = 2407 + *msg.ds_params * 5;
825f05cddf9SRui Paulo 		if (freq != ds_freq) {
8265b9c547cSRui Paulo 			p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
827f05cddf9SRui Paulo 				freq, ds_freq);
828f05cddf9SRui Paulo 			freq = ds_freq;
829f05cddf9SRui Paulo 		}
830f05cddf9SRui Paulo 	}
831f05cddf9SRui Paulo 
832f05cddf9SRui Paulo 	if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
8335b9c547cSRui Paulo 		p2p_dbg(p2p, "Update Listen frequency based on scan results ("
8345b9c547cSRui Paulo 			MACSTR " %d -> %d MHz (DS param %d)",
835f05cddf9SRui Paulo 			MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
836f05cddf9SRui Paulo 			freq, msg.ds_params ? *msg.ds_params : -1);
837f05cddf9SRui Paulo 	}
838f05cddf9SRui Paulo 	if (scan_res) {
839f05cddf9SRui Paulo 		dev->listen_freq = freq;
840f05cddf9SRui Paulo 		if (msg.group_info)
841f05cddf9SRui Paulo 			dev->oper_freq = freq;
842f05cddf9SRui Paulo 	}
843f05cddf9SRui Paulo 	dev->info.level = level;
844f05cddf9SRui Paulo 
84585732ac8SCy Schubert 	dev_name_changed = os_strncmp(dev->info.device_name, msg.device_name,
84685732ac8SCy Schubert 				      WPS_DEV_NAME_MAX_LEN) != 0;
84785732ac8SCy Schubert 
8485b9c547cSRui Paulo 	p2p_copy_wps_info(p2p, dev, 0, &msg);
849f05cddf9SRui Paulo 
850f05cddf9SRui Paulo 	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
851f05cddf9SRui Paulo 		wpabuf_free(dev->info.wps_vendor_ext[i]);
852f05cddf9SRui Paulo 		dev->info.wps_vendor_ext[i] = NULL;
853f05cddf9SRui Paulo 	}
854f05cddf9SRui Paulo 
855f05cddf9SRui Paulo 	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
856f05cddf9SRui Paulo 		if (msg.wps_vendor_ext[i] == NULL)
857f05cddf9SRui Paulo 			break;
858f05cddf9SRui Paulo 		dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
859f05cddf9SRui Paulo 			msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
860f05cddf9SRui Paulo 		if (dev->info.wps_vendor_ext[i] == NULL)
861f05cddf9SRui Paulo 			break;
862f05cddf9SRui Paulo 	}
863f05cddf9SRui Paulo 
8645b9c547cSRui Paulo 	wfd_changed = p2p_compare_wfd_info(dev, &msg);
8655b9c547cSRui Paulo 
86685732ac8SCy Schubert 	if (wfd_changed) {
867f05cddf9SRui Paulo 		wpabuf_free(dev->info.wfd_subelems);
86885732ac8SCy Schubert 		if (msg.wfd_subelems)
869f05cddf9SRui Paulo 			dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
87085732ac8SCy Schubert 		else
87185732ac8SCy Schubert 			dev->info.wfd_subelems = NULL;
872f05cddf9SRui Paulo 	}
873f05cddf9SRui Paulo 
874f05cddf9SRui Paulo 	if (scan_res) {
875f05cddf9SRui Paulo 		p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
876325151a3SRui Paulo 				      msg.group_info, msg.group_info_len,
877325151a3SRui Paulo 				      rx_time);
878f05cddf9SRui Paulo 	}
879f05cddf9SRui Paulo 
880f05cddf9SRui Paulo 	p2p_parse_free(&msg);
881f05cddf9SRui Paulo 
8825b9c547cSRui Paulo 	p2p_update_peer_vendor_elems(dev, ies, ies_len);
883f05cddf9SRui Paulo 
8845b9c547cSRui Paulo 	if (dev->flags & P2P_DEV_REPORTED && !wfd_changed &&
88585732ac8SCy Schubert 	    !dev_name_changed &&
8865b9c547cSRui Paulo 	    (!msg.adv_service_instance ||
8875b9c547cSRui Paulo 	     (dev->flags & P2P_DEV_P2PS_REPORTED)))
888f05cddf9SRui Paulo 		return 0;
889f05cddf9SRui Paulo 
8905b9c547cSRui Paulo 	p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
8915b9c547cSRui Paulo 		freq, (unsigned int) rx_time->sec,
8925b9c547cSRui Paulo 		(unsigned int) rx_time->usec);
893f05cddf9SRui Paulo 	if (dev->flags & P2P_DEV_USER_REJECTED) {
8945b9c547cSRui Paulo 		p2p_dbg(p2p, "Do not report rejected device");
8955b9c547cSRui Paulo 		return 0;
8965b9c547cSRui Paulo 	}
8975b9c547cSRui Paulo 
8985b9c547cSRui Paulo 	if (dev->info.config_methods == 0 &&
8995b9c547cSRui Paulo 	    (freq == 2412 || freq == 2437 || freq == 2462)) {
9005b9c547cSRui Paulo 		/*
9015b9c547cSRui Paulo 		 * If we have only seen a Beacon frame from a GO, we do not yet
9025b9c547cSRui Paulo 		 * know what WPS config methods it supports. Since some
9035b9c547cSRui Paulo 		 * applications use config_methods value from P2P-DEVICE-FOUND
9045b9c547cSRui Paulo 		 * events, postpone reporting this peer until we've fully
9055b9c547cSRui Paulo 		 * discovered its capabilities.
9065b9c547cSRui Paulo 		 *
9075b9c547cSRui Paulo 		 * At least for now, do this only if the peer was detected on
9085b9c547cSRui Paulo 		 * one of the social channels since that peer can be easily be
9095b9c547cSRui Paulo 		 * found again and there are no limitations of having to use
9105b9c547cSRui Paulo 		 * passive scan on this channels, so this can be done through
9115b9c547cSRui Paulo 		 * Probe Response frame that includes the config_methods
9125b9c547cSRui Paulo 		 * information.
9135b9c547cSRui Paulo 		 */
9145b9c547cSRui Paulo 		p2p_dbg(p2p, "Do not report peer " MACSTR
9155b9c547cSRui Paulo 			" with unknown config methods", MAC2STR(addr));
916f05cddf9SRui Paulo 		return 0;
917f05cddf9SRui Paulo 	}
918f05cddf9SRui Paulo 
919f05cddf9SRui Paulo 	p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
920f05cddf9SRui Paulo 			    !(dev->flags & P2P_DEV_REPORTED_ONCE));
921f05cddf9SRui Paulo 	dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
922f05cddf9SRui Paulo 
9235b9c547cSRui Paulo 	if (msg.adv_service_instance)
9245b9c547cSRui Paulo 		dev->flags |= P2P_DEV_P2PS_REPORTED;
9255b9c547cSRui Paulo 
926f05cddf9SRui Paulo 	return 0;
927f05cddf9SRui Paulo }
928f05cddf9SRui Paulo 
929f05cddf9SRui Paulo 
p2p_device_free(struct p2p_data * p2p,struct p2p_device * dev)930f05cddf9SRui Paulo static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
931f05cddf9SRui Paulo {
932f05cddf9SRui Paulo 	int i;
933f05cddf9SRui Paulo 
934f05cddf9SRui Paulo 	if (p2p->go_neg_peer == dev) {
935f05cddf9SRui Paulo 		/*
936f05cddf9SRui Paulo 		 * If GO Negotiation is in progress, report that it has failed.
937f05cddf9SRui Paulo 		 */
9385b9c547cSRui Paulo 		p2p_go_neg_failed(p2p, -1);
939f05cddf9SRui Paulo 	}
940f05cddf9SRui Paulo 	if (p2p->invite_peer == dev)
941f05cddf9SRui Paulo 		p2p->invite_peer = NULL;
942f05cddf9SRui Paulo 	if (p2p->sd_peer == dev)
943f05cddf9SRui Paulo 		p2p->sd_peer = NULL;
944f05cddf9SRui Paulo 	if (p2p->pending_client_disc_go == dev)
945f05cddf9SRui Paulo 		p2p->pending_client_disc_go = NULL;
946f05cddf9SRui Paulo 
947f05cddf9SRui Paulo 	/* dev_lost() device, but only if it was previously dev_found() */
948f05cddf9SRui Paulo 	if (dev->flags & P2P_DEV_REPORTED_ONCE)
949f05cddf9SRui Paulo 		p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
950f05cddf9SRui Paulo 				   dev->info.p2p_device_addr);
951f05cddf9SRui Paulo 
952f05cddf9SRui Paulo 	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
953f05cddf9SRui Paulo 		wpabuf_free(dev->info.wps_vendor_ext[i]);
954f05cddf9SRui Paulo 		dev->info.wps_vendor_ext[i] = NULL;
955f05cddf9SRui Paulo 	}
956f05cddf9SRui Paulo 
957f05cddf9SRui Paulo 	wpabuf_free(dev->info.wfd_subelems);
9585b9c547cSRui Paulo 	wpabuf_free(dev->info.vendor_elems);
9595b9c547cSRui Paulo 	wpabuf_free(dev->go_neg_conf);
9605b9c547cSRui Paulo 	wpabuf_free(dev->info.p2ps_instance);
961f05cddf9SRui Paulo 
962f05cddf9SRui Paulo 	os_free(dev);
963f05cddf9SRui Paulo }
964f05cddf9SRui Paulo 
965f05cddf9SRui Paulo 
p2p_get_next_prog_freq(struct p2p_data * p2p)966f05cddf9SRui Paulo static int p2p_get_next_prog_freq(struct p2p_data *p2p)
967f05cddf9SRui Paulo {
968f05cddf9SRui Paulo 	struct p2p_channels *c;
969f05cddf9SRui Paulo 	struct p2p_reg_class *cla;
970f05cddf9SRui Paulo 	size_t cl, ch;
971f05cddf9SRui Paulo 	int found = 0;
972f05cddf9SRui Paulo 	u8 reg_class;
973f05cddf9SRui Paulo 	u8 channel;
974f05cddf9SRui Paulo 	int freq;
975f05cddf9SRui Paulo 
976f05cddf9SRui Paulo 	c = &p2p->cfg->channels;
977f05cddf9SRui Paulo 	for (cl = 0; cl < c->reg_classes; cl++) {
978f05cddf9SRui Paulo 		cla = &c->reg_class[cl];
979f05cddf9SRui Paulo 		if (cla->reg_class != p2p->last_prog_scan_class)
980f05cddf9SRui Paulo 			continue;
981f05cddf9SRui Paulo 		for (ch = 0; ch < cla->channels; ch++) {
982f05cddf9SRui Paulo 			if (cla->channel[ch] == p2p->last_prog_scan_chan) {
983f05cddf9SRui Paulo 				found = 1;
984f05cddf9SRui Paulo 				break;
985f05cddf9SRui Paulo 			}
986f05cddf9SRui Paulo 		}
987f05cddf9SRui Paulo 		if (found)
988f05cddf9SRui Paulo 			break;
989f05cddf9SRui Paulo 	}
990f05cddf9SRui Paulo 
991f05cddf9SRui Paulo 	if (!found) {
992f05cddf9SRui Paulo 		/* Start from beginning */
993f05cddf9SRui Paulo 		reg_class = c->reg_class[0].reg_class;
994f05cddf9SRui Paulo 		channel = c->reg_class[0].channel[0];
995f05cddf9SRui Paulo 	} else {
996f05cddf9SRui Paulo 		/* Pick the next channel */
997f05cddf9SRui Paulo 		ch++;
998f05cddf9SRui Paulo 		if (ch == cla->channels) {
999f05cddf9SRui Paulo 			cl++;
1000f05cddf9SRui Paulo 			if (cl == c->reg_classes)
1001f05cddf9SRui Paulo 				cl = 0;
1002f05cddf9SRui Paulo 			ch = 0;
1003f05cddf9SRui Paulo 		}
1004f05cddf9SRui Paulo 		reg_class = c->reg_class[cl].reg_class;
1005f05cddf9SRui Paulo 		channel = c->reg_class[cl].channel[ch];
1006f05cddf9SRui Paulo 	}
1007f05cddf9SRui Paulo 
10085b9c547cSRui Paulo 	freq = p2p_channel_to_freq(reg_class, channel);
10095b9c547cSRui Paulo 	p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
1010f05cddf9SRui Paulo 		reg_class, channel, freq);
1011f05cddf9SRui Paulo 	p2p->last_prog_scan_class = reg_class;
1012f05cddf9SRui Paulo 	p2p->last_prog_scan_chan = channel;
1013f05cddf9SRui Paulo 
1014f05cddf9SRui Paulo 	if (freq == 2412 || freq == 2437 || freq == 2462)
1015f05cddf9SRui Paulo 		return 0; /* No need to add social channels */
1016f05cddf9SRui Paulo 	return freq;
1017f05cddf9SRui Paulo }
1018f05cddf9SRui Paulo 
1019f05cddf9SRui Paulo 
p2p_search(struct p2p_data * p2p)1020f05cddf9SRui Paulo static void p2p_search(struct p2p_data *p2p)
1021f05cddf9SRui Paulo {
1022f05cddf9SRui Paulo 	int freq = 0;
1023f05cddf9SRui Paulo 	enum p2p_scan_type type;
1024f05cddf9SRui Paulo 	u16 pw_id = DEV_PW_DEFAULT;
1025f05cddf9SRui Paulo 	int res;
1026f05cddf9SRui Paulo 
1027f05cddf9SRui Paulo 	if (p2p->drv_in_listen) {
10285b9c547cSRui Paulo 		p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
1029f05cddf9SRui Paulo 		return;
1030f05cddf9SRui Paulo 	}
1031f05cddf9SRui Paulo 	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1032*a90b9d01SCy Schubert 	p2p->pending_listen_wait_drv = false;
1033f05cddf9SRui Paulo 
103485732ac8SCy Schubert 	if (p2p->find_pending_full &&
103585732ac8SCy Schubert 	    (p2p->find_type == P2P_FIND_PROGRESSIVE ||
103685732ac8SCy Schubert 	     p2p->find_type == P2P_FIND_START_WITH_FULL)) {
103785732ac8SCy Schubert 		type = P2P_SCAN_FULL;
103885732ac8SCy Schubert 		p2p_dbg(p2p, "Starting search (pending full scan)");
103985732ac8SCy Schubert 		p2p->find_pending_full = 0;
104085732ac8SCy Schubert 	} else if ((p2p->find_type == P2P_FIND_PROGRESSIVE &&
104185732ac8SCy Schubert 	    (freq = p2p_get_next_prog_freq(p2p)) > 0) ||
104285732ac8SCy Schubert 	    (p2p->find_type == P2P_FIND_START_WITH_FULL &&
104385732ac8SCy Schubert 	     (freq = p2p->find_specified_freq) > 0)) {
1044f05cddf9SRui Paulo 		type = P2P_SCAN_SOCIAL_PLUS_ONE;
10455b9c547cSRui Paulo 		p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
1046f05cddf9SRui Paulo 	} else {
1047f05cddf9SRui Paulo 		type = P2P_SCAN_SOCIAL;
10485b9c547cSRui Paulo 		p2p_dbg(p2p, "Starting search");
1049f05cddf9SRui Paulo 	}
1050f05cddf9SRui Paulo 
1051f05cddf9SRui Paulo 	res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
1052f05cddf9SRui Paulo 				 p2p->num_req_dev_types, p2p->req_dev_types,
1053c1d255d3SCy Schubert 				 p2p->find_dev_id, pw_id, p2p->include_6ghz);
1054f05cddf9SRui Paulo 	if (res < 0) {
10555b9c547cSRui Paulo 		p2p_dbg(p2p, "Scan request schedule failed");
1056f05cddf9SRui Paulo 		p2p_continue_find(p2p);
1057f05cddf9SRui Paulo 	}
1058f05cddf9SRui Paulo }
1059f05cddf9SRui Paulo 
1060f05cddf9SRui Paulo 
p2p_find_timeout(void * eloop_ctx,void * timeout_ctx)1061f05cddf9SRui Paulo static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
1062f05cddf9SRui Paulo {
1063f05cddf9SRui Paulo 	struct p2p_data *p2p = eloop_ctx;
10645b9c547cSRui Paulo 	p2p_dbg(p2p, "Find timeout -> stop");
1065f05cddf9SRui Paulo 	p2p_stop_find(p2p);
1066f05cddf9SRui Paulo }
1067f05cddf9SRui Paulo 
1068f05cddf9SRui Paulo 
p2p_notify_scan_trigger_status(struct p2p_data * p2p,int status)10695b9c547cSRui Paulo void p2p_notify_scan_trigger_status(struct p2p_data *p2p, int status)
10705b9c547cSRui Paulo {
10715b9c547cSRui Paulo 	if (status != 0) {
10725b9c547cSRui Paulo 		p2p_dbg(p2p, "Scan request failed");
10735b9c547cSRui Paulo 		/* Do continue find even for the first p2p_find_scan */
10745b9c547cSRui Paulo 		p2p_continue_find(p2p);
10755b9c547cSRui Paulo 	} else {
10765b9c547cSRui Paulo 		p2p_dbg(p2p, "Running p2p_scan");
10775b9c547cSRui Paulo 		p2p->p2p_scan_running = 1;
10785b9c547cSRui Paulo 		eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
10795b9c547cSRui Paulo 		eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
10805b9c547cSRui Paulo 				       p2p, NULL);
10815b9c547cSRui Paulo 	}
10825b9c547cSRui Paulo }
10835b9c547cSRui Paulo 
10845b9c547cSRui Paulo 
p2p_run_after_scan(struct p2p_data * p2p)1085f05cddf9SRui Paulo static int p2p_run_after_scan(struct p2p_data *p2p)
1086f05cddf9SRui Paulo {
1087f05cddf9SRui Paulo 	struct p2p_device *dev;
1088f05cddf9SRui Paulo 	enum p2p_after_scan op;
1089f05cddf9SRui Paulo 
1090f05cddf9SRui Paulo 	op = p2p->start_after_scan;
1091f05cddf9SRui Paulo 	p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1092f05cddf9SRui Paulo 	switch (op) {
1093f05cddf9SRui Paulo 	case P2P_AFTER_SCAN_NOTHING:
1094f05cddf9SRui Paulo 		break;
1095f05cddf9SRui Paulo 	case P2P_AFTER_SCAN_LISTEN:
10965b9c547cSRui Paulo 		p2p_dbg(p2p, "Start previously requested Listen state");
1097f05cddf9SRui Paulo 		p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
1098f05cddf9SRui Paulo 			   p2p->pending_listen_usec / 1000);
1099f05cddf9SRui Paulo 		return 1;
1100f05cddf9SRui Paulo 	case P2P_AFTER_SCAN_CONNECT:
11015b9c547cSRui Paulo 		p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
1102f05cddf9SRui Paulo 			MAC2STR(p2p->after_scan_peer));
1103f05cddf9SRui Paulo 		dev = p2p_get_device(p2p, p2p->after_scan_peer);
1104f05cddf9SRui Paulo 		if (dev == NULL) {
11055b9c547cSRui Paulo 			p2p_dbg(p2p, "Peer not known anymore");
1106f05cddf9SRui Paulo 			break;
1107f05cddf9SRui Paulo 		}
1108f05cddf9SRui Paulo 		p2p_connect_send(p2p, dev);
1109f05cddf9SRui Paulo 		return 1;
1110f05cddf9SRui Paulo 	}
1111f05cddf9SRui Paulo 
1112f05cddf9SRui Paulo 	return 0;
1113f05cddf9SRui Paulo }
1114f05cddf9SRui Paulo 
1115f05cddf9SRui Paulo 
p2p_scan_timeout(void * eloop_ctx,void * timeout_ctx)1116f05cddf9SRui Paulo static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1117f05cddf9SRui Paulo {
1118f05cddf9SRui Paulo 	struct p2p_data *p2p = eloop_ctx;
1119f05cddf9SRui Paulo 	int running;
11205b9c547cSRui Paulo 	p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
1121f05cddf9SRui Paulo 	running = p2p->p2p_scan_running;
1122f05cddf9SRui Paulo 	/* Make sure we recover from missed scan results callback */
1123f05cddf9SRui Paulo 	p2p->p2p_scan_running = 0;
1124f05cddf9SRui Paulo 
1125f05cddf9SRui Paulo 	if (running)
1126f05cddf9SRui Paulo 		p2p_run_after_scan(p2p);
1127f05cddf9SRui Paulo }
1128f05cddf9SRui Paulo 
1129f05cddf9SRui Paulo 
p2p_free_req_dev_types(struct p2p_data * p2p)1130f05cddf9SRui Paulo static void p2p_free_req_dev_types(struct p2p_data *p2p)
1131f05cddf9SRui Paulo {
1132f05cddf9SRui Paulo 	p2p->num_req_dev_types = 0;
1133f05cddf9SRui Paulo 	os_free(p2p->req_dev_types);
1134f05cddf9SRui Paulo 	p2p->req_dev_types = NULL;
1135f05cddf9SRui Paulo }
1136f05cddf9SRui Paulo 
1137f05cddf9SRui Paulo 
p2ps_gen_hash(struct p2p_data * p2p,const char * str,u8 * hash)11385b9c547cSRui Paulo static int p2ps_gen_hash(struct p2p_data *p2p, const char *str, u8 *hash)
11395b9c547cSRui Paulo {
11405b9c547cSRui Paulo 	u8 buf[SHA256_MAC_LEN];
11415b9c547cSRui Paulo 	char str_buf[256];
11425b9c547cSRui Paulo 	const u8 *adv_array;
11435b9c547cSRui Paulo 	size_t i, adv_len;
11445b9c547cSRui Paulo 
11455b9c547cSRui Paulo 	if (!str || !hash)
11465b9c547cSRui Paulo 		return 0;
11475b9c547cSRui Paulo 
11485b9c547cSRui Paulo 	if (!str[0]) {
11495b9c547cSRui Paulo 		os_memcpy(hash, p2p->wild_card_hash, P2PS_HASH_LEN);
11505b9c547cSRui Paulo 		return 1;
11515b9c547cSRui Paulo 	}
11525b9c547cSRui Paulo 
11535b9c547cSRui Paulo 	adv_array = (u8 *) str_buf;
11545b9c547cSRui Paulo 	adv_len = os_strlen(str);
1155325151a3SRui Paulo 	if (adv_len >= sizeof(str_buf))
1156325151a3SRui Paulo 		return 0;
11575b9c547cSRui Paulo 
1158325151a3SRui Paulo 	for (i = 0; i < adv_len; i++) {
11595b9c547cSRui Paulo 		if (str[i] >= 'A' && str[i] <= 'Z')
11605b9c547cSRui Paulo 			str_buf[i] = str[i] - 'A' + 'a';
11615b9c547cSRui Paulo 		else
11625b9c547cSRui Paulo 			str_buf[i] = str[i];
11635b9c547cSRui Paulo 	}
11645b9c547cSRui Paulo 
11655b9c547cSRui Paulo 	if (sha256_vector(1, &adv_array, &adv_len, buf))
11665b9c547cSRui Paulo 		return 0;
11675b9c547cSRui Paulo 
11685b9c547cSRui Paulo 	os_memcpy(hash, buf, P2PS_HASH_LEN);
11695b9c547cSRui Paulo 	return 1;
11705b9c547cSRui Paulo }
11715b9c547cSRui Paulo 
11725b9c547cSRui Paulo 
p2p_find(struct p2p_data * p2p,unsigned int timeout,enum p2p_discovery_type type,unsigned int num_req_dev_types,const u8 * req_dev_types,const u8 * dev_id,unsigned int search_delay,u8 seek_count,const char ** seek,int freq,bool include_6ghz)1173f05cddf9SRui Paulo int p2p_find(struct p2p_data *p2p, unsigned int timeout,
1174f05cddf9SRui Paulo 	     enum p2p_discovery_type type,
1175f05cddf9SRui Paulo 	     unsigned int num_req_dev_types, const u8 *req_dev_types,
11765b9c547cSRui Paulo 	     const u8 *dev_id, unsigned int search_delay,
1177c1d255d3SCy Schubert 	     u8 seek_count, const char **seek, int freq, bool include_6ghz)
1178f05cddf9SRui Paulo {
1179f05cddf9SRui Paulo 	int res;
11804bc52338SCy Schubert 	struct os_reltime start;
1181f05cddf9SRui Paulo 
11825b9c547cSRui Paulo 	p2p_dbg(p2p, "Starting find (type=%d)", type);
1183f05cddf9SRui Paulo 	if (p2p->p2p_scan_running) {
11845b9c547cSRui Paulo 		p2p_dbg(p2p, "p2p_scan is already running");
1185f05cddf9SRui Paulo 	}
1186f05cddf9SRui Paulo 
1187f05cddf9SRui Paulo 	p2p_free_req_dev_types(p2p);
1188f05cddf9SRui Paulo 	if (req_dev_types && num_req_dev_types) {
118985732ac8SCy Schubert 		p2p->req_dev_types = os_memdup(req_dev_types,
119085732ac8SCy Schubert 					       num_req_dev_types *
1191f05cddf9SRui Paulo 					       WPS_DEV_TYPE_LEN);
1192f05cddf9SRui Paulo 		if (p2p->req_dev_types == NULL)
1193f05cddf9SRui Paulo 			return -1;
1194f05cddf9SRui Paulo 		p2p->num_req_dev_types = num_req_dev_types;
1195f05cddf9SRui Paulo 	}
1196f05cddf9SRui Paulo 
1197f05cddf9SRui Paulo 	if (dev_id) {
1198f05cddf9SRui Paulo 		os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1199f05cddf9SRui Paulo 		p2p->find_dev_id = p2p->find_dev_id_buf;
1200f05cddf9SRui Paulo 	} else
1201f05cddf9SRui Paulo 		p2p->find_dev_id = NULL;
1202c1d255d3SCy Schubert 	p2p->include_6ghz = p2p_wfd_enabled(p2p) && include_6ghz;
12035b9c547cSRui Paulo 	if (seek_count == 0 || !seek) {
12045b9c547cSRui Paulo 		/* Not an ASP search */
12055b9c547cSRui Paulo 		p2p->p2ps_seek = 0;
12065b9c547cSRui Paulo 	} else if (seek_count == 1 && seek && (!seek[0] || !seek[0][0])) {
12075b9c547cSRui Paulo 		/*
12085b9c547cSRui Paulo 		 * An empty seek string means no hash values, but still an ASP
12095b9c547cSRui Paulo 		 * search.
12105b9c547cSRui Paulo 		 */
1211325151a3SRui Paulo 		p2p_dbg(p2p, "ASP search");
12125b9c547cSRui Paulo 		p2p->p2ps_seek_count = 0;
12135b9c547cSRui Paulo 		p2p->p2ps_seek = 1;
12145b9c547cSRui Paulo 	} else if (seek && seek_count <= P2P_MAX_QUERY_HASH) {
12155b9c547cSRui Paulo 		u8 buf[P2PS_HASH_LEN];
1216325151a3SRui Paulo 		int i, count = 0;
12175b9c547cSRui Paulo 
12185b9c547cSRui Paulo 		for (i = 0; i < seek_count; i++) {
12195b9c547cSRui Paulo 			if (!p2ps_gen_hash(p2p, seek[i], buf))
12205b9c547cSRui Paulo 				continue;
12215b9c547cSRui Paulo 
1222325151a3SRui Paulo 			p2p_dbg(p2p, "Seek service %s hash " MACSTR,
1223325151a3SRui Paulo 				seek[i], MAC2STR(buf));
1224325151a3SRui Paulo 			os_memcpy(&p2p->p2ps_seek_hash[count * P2PS_HASH_LEN],
1225325151a3SRui Paulo 				  buf, P2PS_HASH_LEN);
1226325151a3SRui Paulo 			count++;
12275b9c547cSRui Paulo 		}
12285b9c547cSRui Paulo 
1229325151a3SRui Paulo 		p2p->p2ps_seek_count = count;
12305b9c547cSRui Paulo 		p2p->p2ps_seek = 1;
12315b9c547cSRui Paulo 	} else {
12325b9c547cSRui Paulo 		p2p->p2ps_seek_count = 0;
12335b9c547cSRui Paulo 		p2p->p2ps_seek = 1;
12345b9c547cSRui Paulo 	}
12355b9c547cSRui Paulo 
12365b9c547cSRui Paulo 	/* Special case to perform wildcard search */
12375b9c547cSRui Paulo 	if (p2p->p2ps_seek_count == 0 && p2p->p2ps_seek) {
12385b9c547cSRui Paulo 		p2p->p2ps_seek_count = 1;
1239325151a3SRui Paulo 		os_memcpy(&p2p->p2ps_seek_hash, p2p->wild_card_hash,
1240325151a3SRui Paulo 			  P2PS_HASH_LEN);
12415b9c547cSRui Paulo 	}
12425b9c547cSRui Paulo 
1243f05cddf9SRui Paulo 	p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1244f05cddf9SRui Paulo 	p2p_clear_timeout(p2p);
1245780fb4a2SCy Schubert 	if (p2p->pending_listen_freq) {
1246780fb4a2SCy Schubert 		p2p_dbg(p2p, "Clear pending_listen_freq for p2p_find");
1247780fb4a2SCy Schubert 		p2p->pending_listen_freq = 0;
1248780fb4a2SCy Schubert 	}
1249f05cddf9SRui Paulo 	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1250*a90b9d01SCy Schubert 	p2p->pending_listen_wait_drv = false;
125185732ac8SCy Schubert 	p2p->find_pending_full = 0;
1252f05cddf9SRui Paulo 	p2p->find_type = type;
125385732ac8SCy Schubert 	if (freq != 2412 && freq != 2437 && freq != 2462 && freq != 60480)
125485732ac8SCy Schubert 		p2p->find_specified_freq = freq;
125585732ac8SCy Schubert 	else
125685732ac8SCy Schubert 		p2p->find_specified_freq = 0;
1257f05cddf9SRui Paulo 	p2p_device_clear_reported(p2p);
1258780fb4a2SCy Schubert 	os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
1259f05cddf9SRui Paulo 	p2p_set_state(p2p, P2P_SEARCH);
1260f05cddf9SRui Paulo 	p2p->search_delay = search_delay;
1261f05cddf9SRui Paulo 	p2p->in_search_delay = 0;
1262f05cddf9SRui Paulo 	eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1263f05cddf9SRui Paulo 	p2p->last_p2p_find_timeout = timeout;
1264f05cddf9SRui Paulo 	if (timeout)
1265f05cddf9SRui Paulo 		eloop_register_timeout(timeout, 0, p2p_find_timeout,
1266f05cddf9SRui Paulo 				       p2p, NULL);
12674bc52338SCy Schubert 	os_get_reltime(&start);
1268f05cddf9SRui Paulo 	switch (type) {
1269f05cddf9SRui Paulo 	case P2P_FIND_START_WITH_FULL:
12705b9c547cSRui Paulo 		if (freq > 0) {
12715b9c547cSRui Paulo 			/*
12725b9c547cSRui Paulo 			 * Start with the specified channel and then move to
127385732ac8SCy Schubert 			 * scans for social channels and this specific channel.
12745b9c547cSRui Paulo 			 */
12755b9c547cSRui Paulo 			res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx,
12765b9c547cSRui Paulo 						 P2P_SCAN_SPECIFIC, freq,
12775b9c547cSRui Paulo 						 p2p->num_req_dev_types,
12785b9c547cSRui Paulo 						 p2p->req_dev_types, dev_id,
1279c1d255d3SCy Schubert 						 DEV_PW_DEFAULT,
1280c1d255d3SCy Schubert 						 p2p->include_6ghz);
12815b9c547cSRui Paulo 			break;
12825b9c547cSRui Paulo 		}
12835b9c547cSRui Paulo 		/* fall through */
1284f05cddf9SRui Paulo 	case P2P_FIND_PROGRESSIVE:
1285f05cddf9SRui Paulo 		res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1286f05cddf9SRui Paulo 					 p2p->num_req_dev_types,
1287f05cddf9SRui Paulo 					 p2p->req_dev_types, dev_id,
1288c1d255d3SCy Schubert 					 DEV_PW_DEFAULT, p2p->include_6ghz);
1289f05cddf9SRui Paulo 		break;
1290f05cddf9SRui Paulo 	case P2P_FIND_ONLY_SOCIAL:
1291f05cddf9SRui Paulo 		res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1292f05cddf9SRui Paulo 					 p2p->num_req_dev_types,
1293f05cddf9SRui Paulo 					 p2p->req_dev_types, dev_id,
1294c1d255d3SCy Schubert 					 DEV_PW_DEFAULT, p2p->include_6ghz);
1295f05cddf9SRui Paulo 		break;
1296f05cddf9SRui Paulo 	default:
1297f05cddf9SRui Paulo 		return -1;
1298f05cddf9SRui Paulo 	}
1299f05cddf9SRui Paulo 
13004bc52338SCy Schubert 	if (!res)
13014bc52338SCy Schubert 		p2p->find_start = start;
13024bc52338SCy Schubert 
13035b9c547cSRui Paulo 	if (res != 0 && p2p->p2p_scan_running) {
13045b9c547cSRui Paulo 		p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running");
13055b9c547cSRui Paulo 		/* wait for the previous p2p_scan to complete */
130685732ac8SCy Schubert 		if (type == P2P_FIND_PROGRESSIVE ||
130785732ac8SCy Schubert 		    (type == P2P_FIND_START_WITH_FULL && freq == 0))
130885732ac8SCy Schubert 			p2p->find_pending_full = 1;
13095b9c547cSRui Paulo 		res = 0; /* do not report failure */
13105b9c547cSRui Paulo 	} else if (res != 0) {
13115b9c547cSRui Paulo 		p2p_dbg(p2p, "Failed to start p2p_scan");
1312f05cddf9SRui Paulo 		p2p_set_state(p2p, P2P_IDLE);
1313f05cddf9SRui Paulo 		eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1314f05cddf9SRui Paulo 	}
1315f05cddf9SRui Paulo 
1316f05cddf9SRui Paulo 	return res;
1317f05cddf9SRui Paulo }
1318f05cddf9SRui Paulo 
1319f05cddf9SRui Paulo 
p2p_stop_find_for_freq(struct p2p_data * p2p,int freq)1320f05cddf9SRui Paulo void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1321f05cddf9SRui Paulo {
13225b9c547cSRui Paulo 	p2p_dbg(p2p, "Stopping find");
1323f05cddf9SRui Paulo 	eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1324f05cddf9SRui Paulo 	p2p_clear_timeout(p2p);
13255b9c547cSRui Paulo 	if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
13265b9c547cSRui Paulo 		p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
13275b9c547cSRui Paulo 
13285b9c547cSRui Paulo 	p2p->p2ps_seek_count = 0;
13295b9c547cSRui Paulo 
1330f05cddf9SRui Paulo 	p2p_set_state(p2p, P2P_IDLE);
1331f05cddf9SRui Paulo 	p2p_free_req_dev_types(p2p);
1332f05cddf9SRui Paulo 	p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
13335b9c547cSRui Paulo 	if (p2p->go_neg_peer)
13345b9c547cSRui Paulo 		p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
1335f05cddf9SRui Paulo 	p2p->go_neg_peer = NULL;
1336f05cddf9SRui Paulo 	p2p->sd_peer = NULL;
1337f05cddf9SRui Paulo 	p2p->invite_peer = NULL;
1338f05cddf9SRui Paulo 	p2p_stop_listen_for_freq(p2p, freq);
13395b9c547cSRui Paulo 	p2p->send_action_in_progress = 0;
1340f05cddf9SRui Paulo }
1341f05cddf9SRui Paulo 
1342f05cddf9SRui Paulo 
p2p_stop_listen_for_freq(struct p2p_data * p2p,int freq)1343f05cddf9SRui Paulo void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1344f05cddf9SRui Paulo {
1345*a90b9d01SCy Schubert 	p2p_dbg(p2p,
1346*a90b9d01SCy Schubert 		"%s(freq=%d) pending_listen_freq=%d in_listen=%d drv_in_listen=%d",
1347*a90b9d01SCy Schubert 		__func__, freq, p2p->pending_listen_freq, p2p->in_listen,
1348*a90b9d01SCy Schubert 		p2p->drv_in_listen);
13494b72b91aSCy Schubert 	if (freq > 0 &&
13504b72b91aSCy Schubert 	    ((p2p->drv_in_listen == freq && p2p->in_listen) ||
13514b72b91aSCy Schubert 	     p2p->pending_listen_freq == (unsigned int) freq)) {
13525b9c547cSRui Paulo 		p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
1353f05cddf9SRui Paulo 		return;
1354f05cddf9SRui Paulo 	}
1355f05cddf9SRui Paulo 	if (p2p->in_listen) {
1356f05cddf9SRui Paulo 		p2p->in_listen = 0;
1357f05cddf9SRui Paulo 		p2p_clear_timeout(p2p);
1358f05cddf9SRui Paulo 	}
1359f05cddf9SRui Paulo 	if (p2p->drv_in_listen) {
1360f05cddf9SRui Paulo 		/*
1361f05cddf9SRui Paulo 		 * The driver may not deliver callback to p2p_listen_end()
1362f05cddf9SRui Paulo 		 * when the operation gets canceled, so clear the internal
1363f05cddf9SRui Paulo 		 * variable that is tracking driver state.
1364f05cddf9SRui Paulo 		 */
13655b9c547cSRui Paulo 		p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
1366f05cddf9SRui Paulo 		p2p->drv_in_listen = 0;
1367f05cddf9SRui Paulo 	}
1368*a90b9d01SCy Schubert 	if (p2p->pending_listen_freq &&
1369*a90b9d01SCy Schubert 	    p2p->pending_listen_freq != (unsigned int) freq &&
1370*a90b9d01SCy Schubert 	    !p2p->drv_in_listen && p2p->pending_listen_wait_drv) {
1371*a90b9d01SCy Schubert 		p2p_dbg(p2p,
1372*a90b9d01SCy Schubert 			"Clear pending_listen_freq since the started listen did not complete before being stopped");
1373*a90b9d01SCy Schubert 		p2p->pending_listen_freq = 0;
1374*a90b9d01SCy Schubert 	}
1375f05cddf9SRui Paulo 	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1376*a90b9d01SCy Schubert 	p2p->pending_listen_wait_drv = false;
1377f05cddf9SRui Paulo }
1378f05cddf9SRui Paulo 
1379f05cddf9SRui Paulo 
p2p_stop_listen(struct p2p_data * p2p)13805b9c547cSRui Paulo void p2p_stop_listen(struct p2p_data *p2p)
13815b9c547cSRui Paulo {
13825b9c547cSRui Paulo 	if (p2p->state != P2P_LISTEN_ONLY) {
13835b9c547cSRui Paulo 		p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
13845b9c547cSRui Paulo 		return;
13855b9c547cSRui Paulo 	}
13865b9c547cSRui Paulo 
13875b9c547cSRui Paulo 	p2p_stop_listen_for_freq(p2p, 0);
13885b9c547cSRui Paulo 	p2p_set_state(p2p, P2P_IDLE);
13895b9c547cSRui Paulo }
13905b9c547cSRui Paulo 
13915b9c547cSRui Paulo 
p2p_stop_find(struct p2p_data * p2p)1392f05cddf9SRui Paulo void p2p_stop_find(struct p2p_data *p2p)
1393f05cddf9SRui Paulo {
13945b9c547cSRui Paulo 	p2p->pending_listen_freq = 0;
1395f05cddf9SRui Paulo 	p2p_stop_find_for_freq(p2p, 0);
1396f05cddf9SRui Paulo }
1397f05cddf9SRui Paulo 
1398f05cddf9SRui Paulo 
p2p_prepare_channel_pref(struct p2p_data * p2p,unsigned int force_freq,unsigned int pref_freq,int go)1399f05cddf9SRui Paulo static int p2p_prepare_channel_pref(struct p2p_data *p2p,
1400f05cddf9SRui Paulo 				    unsigned int force_freq,
14015b9c547cSRui Paulo 				    unsigned int pref_freq, int go)
1402f05cddf9SRui Paulo {
1403f05cddf9SRui Paulo 	u8 op_class, op_channel;
1404f05cddf9SRui Paulo 	unsigned int freq = force_freq ? force_freq : pref_freq;
1405f05cddf9SRui Paulo 
14065b9c547cSRui Paulo 	p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
14075b9c547cSRui Paulo 		force_freq, pref_freq, go);
14085b9c547cSRui Paulo 	if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
14095b9c547cSRui Paulo 		p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
1410f05cddf9SRui Paulo 		return -1;
1411f05cddf9SRui Paulo 	}
1412f05cddf9SRui Paulo 
14135b9c547cSRui Paulo 	if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
14145b9c547cSRui Paulo 	    (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
14155b9c547cSRui Paulo 					  op_channel))) {
14165b9c547cSRui Paulo 		p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
14175b9c547cSRui Paulo 			freq, op_class, op_channel);
1418f05cddf9SRui Paulo 		return -1;
1419f05cddf9SRui Paulo 	}
1420f05cddf9SRui Paulo 
1421f05cddf9SRui Paulo 	p2p->op_reg_class = op_class;
1422f05cddf9SRui Paulo 	p2p->op_channel = op_channel;
1423f05cddf9SRui Paulo 
1424f05cddf9SRui Paulo 	if (force_freq) {
1425f05cddf9SRui Paulo 		p2p->channels.reg_classes = 1;
1426f05cddf9SRui Paulo 		p2p->channels.reg_class[0].channels = 1;
1427f05cddf9SRui Paulo 		p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1428f05cddf9SRui Paulo 		p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
1429f05cddf9SRui Paulo 	} else {
1430c1d255d3SCy Schubert 		p2p_copy_channels(&p2p->channels, &p2p->cfg->channels,
1431c1d255d3SCy Schubert 				  p2p->allow_6ghz);
1432f05cddf9SRui Paulo 	}
1433f05cddf9SRui Paulo 
1434f05cddf9SRui Paulo 	return 0;
1435f05cddf9SRui Paulo }
1436f05cddf9SRui Paulo 
1437f05cddf9SRui Paulo 
p2p_prepare_channel_best(struct p2p_data * p2p)1438f05cddf9SRui Paulo static void p2p_prepare_channel_best(struct p2p_data *p2p)
1439f05cddf9SRui Paulo {
1440f05cddf9SRui Paulo 	u8 op_class, op_channel;
1441325151a3SRui Paulo 	const int op_classes_5ghz[] = { 124, 125, 115, 0 };
14425b9c547cSRui Paulo 	const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
14435b9c547cSRui Paulo 	const int op_classes_vht[] = { 128, 0 };
1444c1d255d3SCy Schubert 	const int op_classes_edmg[] = { 181, 182, 183, 0 };
1445c1d255d3SCy Schubert 	const int op_classes_6ghz[] = { 131, 0 };
14465b9c547cSRui Paulo 
14475b9c547cSRui Paulo 	p2p_dbg(p2p, "Prepare channel best");
1448f05cddf9SRui Paulo 
1449f05cddf9SRui Paulo 	if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1450f05cddf9SRui Paulo 	    p2p_supported_freq(p2p, p2p->best_freq_overall) &&
14515b9c547cSRui Paulo 	    p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
14525b9c547cSRui Paulo 	    == 0) {
14535b9c547cSRui Paulo 		p2p_dbg(p2p, "Select best overall channel as operating channel preference");
1454f05cddf9SRui Paulo 		p2p->op_reg_class = op_class;
1455f05cddf9SRui Paulo 		p2p->op_channel = op_channel;
1456f05cddf9SRui Paulo 	} else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1457f05cddf9SRui Paulo 		   p2p_supported_freq(p2p, p2p->best_freq_5) &&
14585b9c547cSRui Paulo 		   p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
14595b9c547cSRui Paulo 		   == 0) {
14605b9c547cSRui Paulo 		p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
1461f05cddf9SRui Paulo 		p2p->op_reg_class = op_class;
1462f05cddf9SRui Paulo 		p2p->op_channel = op_channel;
1463f05cddf9SRui Paulo 	} else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
1464f05cddf9SRui Paulo 		   p2p_supported_freq(p2p, p2p->best_freq_24) &&
14655b9c547cSRui Paulo 		   p2p_freq_to_channel(p2p->best_freq_24, &op_class,
14665b9c547cSRui Paulo 				       &op_channel) == 0) {
14675b9c547cSRui Paulo 		p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
1468f05cddf9SRui Paulo 		p2p->op_reg_class = op_class;
1469f05cddf9SRui Paulo 		p2p->op_channel = op_channel;
14705b9c547cSRui Paulo 	} else if (p2p->cfg->num_pref_chan > 0 &&
14715b9c547cSRui Paulo 		   p2p_channels_includes(&p2p->cfg->channels,
14725b9c547cSRui Paulo 					 p2p->cfg->pref_chan[0].op_class,
14735b9c547cSRui Paulo 					 p2p->cfg->pref_chan[0].chan)) {
14745b9c547cSRui Paulo 		p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
14755b9c547cSRui Paulo 		p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
14765b9c547cSRui Paulo 		p2p->op_channel = p2p->cfg->pref_chan[0].chan;
1477c1d255d3SCy Schubert 	} else if (p2p_channel_select(&p2p->cfg->channels, op_classes_edmg,
1478c1d255d3SCy Schubert 				      &p2p->op_reg_class, &p2p->op_channel) ==
1479c1d255d3SCy Schubert 		   0) {
1480c1d255d3SCy Schubert 		p2p_dbg(p2p, "Select possible EDMG channel (op_class %u channel %u) as operating channel preference",
1481c1d255d3SCy Schubert 			p2p->op_reg_class, p2p->op_channel);
1482c1d255d3SCy Schubert 	} else if (p2p->allow_6ghz &&
1483c1d255d3SCy Schubert 		   (p2p_channel_select(&p2p->cfg->channels, op_classes_6ghz,
1484c1d255d3SCy Schubert 				       &p2p->op_reg_class, &p2p->op_channel) ==
1485c1d255d3SCy Schubert 		    0)) {
1486c1d255d3SCy Schubert 		p2p_dbg(p2p, "Select possible 6 GHz channel (op_class %u channel %u) as operating channel preference",
1487c1d255d3SCy Schubert 			p2p->op_reg_class, p2p->op_channel);
14885b9c547cSRui Paulo 	} else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
14895b9c547cSRui Paulo 				      &p2p->op_reg_class, &p2p->op_channel) ==
14905b9c547cSRui Paulo 		   0) {
14915b9c547cSRui Paulo 		p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference",
14925b9c547cSRui Paulo 			p2p->op_reg_class, p2p->op_channel);
14935b9c547cSRui Paulo 	} else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40,
14945b9c547cSRui Paulo 				      &p2p->op_reg_class, &p2p->op_channel) ==
14955b9c547cSRui Paulo 		   0) {
14965b9c547cSRui Paulo 		p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
14975b9c547cSRui Paulo 			p2p->op_reg_class, p2p->op_channel);
14985b9c547cSRui Paulo 	} else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
14995b9c547cSRui Paulo 				      &p2p->op_reg_class, &p2p->op_channel) ==
15005b9c547cSRui Paulo 		   0) {
15015b9c547cSRui Paulo 		p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
15025b9c547cSRui Paulo 			p2p->op_reg_class, p2p->op_channel);
15035b9c547cSRui Paulo 	} else if (p2p_channels_includes(&p2p->cfg->channels,
15045b9c547cSRui Paulo 					 p2p->cfg->op_reg_class,
15055b9c547cSRui Paulo 					 p2p->cfg->op_channel)) {
15065b9c547cSRui Paulo 		p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
1507f05cddf9SRui Paulo 		p2p->op_reg_class = p2p->cfg->op_reg_class;
1508f05cddf9SRui Paulo 		p2p->op_channel = p2p->cfg->op_channel;
15095b9c547cSRui Paulo 	} else if (p2p_channel_random_social(&p2p->cfg->channels,
15105b9c547cSRui Paulo 					     &p2p->op_reg_class,
15114bc52338SCy Schubert 					     &p2p->op_channel,
15124bc52338SCy Schubert 					     NULL, NULL) == 0) {
15135b9c547cSRui Paulo 		p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference",
15145b9c547cSRui Paulo 			p2p->op_reg_class, p2p->op_channel);
15155b9c547cSRui Paulo 	} else {
15165b9c547cSRui Paulo 		/* Select any random available channel from the first available
15175b9c547cSRui Paulo 		 * operating class */
1518*a90b9d01SCy Schubert 		if (p2p_channel_select(&p2p->cfg->channels, NULL,
15195b9c547cSRui Paulo 				       &p2p->op_reg_class,
1520*a90b9d01SCy Schubert 				       &p2p->op_channel) == 0)
1521*a90b9d01SCy Schubert 			p2p_dbg(p2p,
1522*a90b9d01SCy Schubert 				"Select random available channel %d from operating class %d as operating channel preference",
15235b9c547cSRui Paulo 				p2p->op_channel, p2p->op_reg_class);
1524f05cddf9SRui Paulo 	}
1525f05cddf9SRui Paulo 
1526c1d255d3SCy Schubert 	p2p_copy_channels(&p2p->channels, &p2p->cfg->channels, p2p->allow_6ghz);
1527f05cddf9SRui Paulo }
1528f05cddf9SRui Paulo 
1529f05cddf9SRui Paulo 
1530f05cddf9SRui Paulo /**
1531780fb4a2SCy Schubert  * p2p_prepare_channel - Select operating channel for GO Negotiation or P2PS PD
1532f05cddf9SRui Paulo  * @p2p: P2P module context from p2p_init()
1533f05cddf9SRui Paulo  * @dev: Selected peer device
1534f05cddf9SRui Paulo  * @force_freq: Forced frequency in MHz or 0 if not forced
1535f05cddf9SRui Paulo  * @pref_freq: Preferred frequency in MHz or 0 if no preference
15365b9c547cSRui Paulo  * @go: Whether the local end will be forced to be GO
1537f05cddf9SRui Paulo  * Returns: 0 on success, -1 on failure (channel not supported for P2P)
1538f05cddf9SRui Paulo  *
1539f05cddf9SRui Paulo  * This function is used to do initial operating channel selection for GO
1540780fb4a2SCy Schubert  * Negotiation prior to having received peer information or for P2PS PD
1541780fb4a2SCy Schubert  * signalling. The selected channel may be further optimized in
1542780fb4a2SCy Schubert  * p2p_reselect_channel() once the peer information is available.
1543f05cddf9SRui Paulo  */
p2p_prepare_channel(struct p2p_data * p2p,struct p2p_device * dev,unsigned int force_freq,unsigned int pref_freq,int go)15445b9c547cSRui Paulo int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
15455b9c547cSRui Paulo 			unsigned int force_freq, unsigned int pref_freq, int go)
1546f05cddf9SRui Paulo {
15475b9c547cSRui Paulo 	p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
15485b9c547cSRui Paulo 		force_freq, pref_freq, go);
1549f05cddf9SRui Paulo 	if (force_freq || pref_freq) {
15505b9c547cSRui Paulo 		if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
15515b9c547cSRui Paulo 		    0)
1552f05cddf9SRui Paulo 			return -1;
1553f05cddf9SRui Paulo 	} else {
1554f05cddf9SRui Paulo 		p2p_prepare_channel_best(p2p);
1555f05cddf9SRui Paulo 	}
15565b9c547cSRui Paulo 	p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
15575b9c547cSRui Paulo 	if (go)
15585b9c547cSRui Paulo 		p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
15595b9c547cSRui Paulo 	else if (!force_freq)
15605b9c547cSRui Paulo 		p2p_channels_union_inplace(&p2p->channels,
15615b9c547cSRui Paulo 					   &p2p->cfg->cli_channels);
15625b9c547cSRui Paulo 	p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
15635b9c547cSRui Paulo 
15645b9c547cSRui Paulo 	p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
1565f05cddf9SRui Paulo 		p2p->op_reg_class, p2p->op_channel,
1566f05cddf9SRui Paulo 		force_freq ? " (forced)" : "");
1567f05cddf9SRui Paulo 
1568f05cddf9SRui Paulo 	if (force_freq)
1569f05cddf9SRui Paulo 		dev->flags |= P2P_DEV_FORCE_FREQ;
1570f05cddf9SRui Paulo 	else
1571f05cddf9SRui Paulo 		dev->flags &= ~P2P_DEV_FORCE_FREQ;
1572f05cddf9SRui Paulo 
1573f05cddf9SRui Paulo 	return 0;
1574f05cddf9SRui Paulo }
1575f05cddf9SRui Paulo 
1576f05cddf9SRui Paulo 
p2p_set_dev_persistent(struct p2p_device * dev,int persistent_group)1577f05cddf9SRui Paulo static void p2p_set_dev_persistent(struct p2p_device *dev,
1578f05cddf9SRui Paulo 				   int persistent_group)
1579f05cddf9SRui Paulo {
1580f05cddf9SRui Paulo 	switch (persistent_group) {
1581f05cddf9SRui Paulo 	case 0:
1582f05cddf9SRui Paulo 		dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1583f05cddf9SRui Paulo 				P2P_DEV_PREFER_PERSISTENT_RECONN);
1584f05cddf9SRui Paulo 		break;
1585f05cddf9SRui Paulo 	case 1:
1586f05cddf9SRui Paulo 		dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1587f05cddf9SRui Paulo 		dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1588f05cddf9SRui Paulo 		break;
1589f05cddf9SRui Paulo 	case 2:
1590f05cddf9SRui Paulo 		dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1591f05cddf9SRui Paulo 			P2P_DEV_PREFER_PERSISTENT_RECONN;
1592f05cddf9SRui Paulo 		break;
1593f05cddf9SRui Paulo 	}
1594f05cddf9SRui Paulo }
1595f05cddf9SRui Paulo 
1596f05cddf9SRui Paulo 
p2p_connect(struct p2p_data * p2p,const u8 * peer_addr,enum p2p_wps_method wps_method,int go_intent,const u8 * own_interface_addr,unsigned int force_freq,int persistent_group,const u8 * force_ssid,size_t force_ssid_len,int pd_before_go_neg,unsigned int pref_freq,u16 oob_pw_id)1597f05cddf9SRui Paulo int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1598f05cddf9SRui Paulo 		enum p2p_wps_method wps_method,
1599f05cddf9SRui Paulo 		int go_intent, const u8 *own_interface_addr,
1600f05cddf9SRui Paulo 		unsigned int force_freq, int persistent_group,
1601f05cddf9SRui Paulo 		const u8 *force_ssid, size_t force_ssid_len,
16025b9c547cSRui Paulo 		int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id)
1603f05cddf9SRui Paulo {
1604f05cddf9SRui Paulo 	struct p2p_device *dev;
1605f05cddf9SRui Paulo 
16065b9c547cSRui Paulo 	p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
1607f05cddf9SRui Paulo 		"  GO Intent=%d  Intended Interface Address=" MACSTR
16085b9c547cSRui Paulo 		" wps_method=%d persistent_group=%d pd_before_go_neg=%d "
1609c1d255d3SCy Schubert 		"oob_pw_id=%u allow_6ghz=%d",
1610f05cddf9SRui Paulo 		MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1611c1d255d3SCy Schubert 		wps_method, persistent_group, pd_before_go_neg, oob_pw_id,
1612c1d255d3SCy Schubert 		p2p->allow_6ghz);
1613f05cddf9SRui Paulo 
1614f05cddf9SRui Paulo 	dev = p2p_get_device(p2p, peer_addr);
1615f05cddf9SRui Paulo 	if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
16165b9c547cSRui Paulo 		p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
1617f05cddf9SRui Paulo 			MAC2STR(peer_addr));
1618f05cddf9SRui Paulo 		return -1;
1619f05cddf9SRui Paulo 	}
1620f05cddf9SRui Paulo 
16215b9c547cSRui Paulo 	if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
16225b9c547cSRui Paulo 				go_intent == 15) < 0)
1623f05cddf9SRui Paulo 		return -1;
1624f05cddf9SRui Paulo 
1625f05cddf9SRui Paulo 	if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1626f05cddf9SRui Paulo 		if (!(dev->info.dev_capab &
1627f05cddf9SRui Paulo 		      P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
16285b9c547cSRui Paulo 			p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
1629f05cddf9SRui Paulo 				" that is in a group and is not discoverable",
1630f05cddf9SRui Paulo 				MAC2STR(peer_addr));
1631f05cddf9SRui Paulo 			return -1;
1632f05cddf9SRui Paulo 		}
1633f05cddf9SRui Paulo 		if (dev->oper_freq <= 0) {
16345b9c547cSRui Paulo 			p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
1635f05cddf9SRui Paulo 				" with incomplete information",
1636f05cddf9SRui Paulo 				MAC2STR(peer_addr));
1637f05cddf9SRui Paulo 			return -1;
1638f05cddf9SRui Paulo 		}
1639f05cddf9SRui Paulo 
1640f05cddf9SRui Paulo 		/*
1641f05cddf9SRui Paulo 		 * First, try to connect directly. If the peer does not
1642f05cddf9SRui Paulo 		 * acknowledge frames, assume it is sleeping and use device
1643f05cddf9SRui Paulo 		 * discoverability via the GO at that point.
1644f05cddf9SRui Paulo 		 */
1645f05cddf9SRui Paulo 	}
1646f05cddf9SRui Paulo 
1647f05cddf9SRui Paulo 	p2p->ssid_set = 0;
1648f05cddf9SRui Paulo 	if (force_ssid) {
1649f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1650f05cddf9SRui Paulo 				  force_ssid, force_ssid_len);
1651f05cddf9SRui Paulo 		os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1652f05cddf9SRui Paulo 		p2p->ssid_len = force_ssid_len;
1653f05cddf9SRui Paulo 		p2p->ssid_set = 1;
1654f05cddf9SRui Paulo 	}
1655f05cddf9SRui Paulo 
1656f05cddf9SRui Paulo 	dev->flags &= ~P2P_DEV_NOT_YET_READY;
1657f05cddf9SRui Paulo 	dev->flags &= ~P2P_DEV_USER_REJECTED;
1658f05cddf9SRui Paulo 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1659f05cddf9SRui Paulo 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1660f05cddf9SRui Paulo 	if (pd_before_go_neg)
1661f05cddf9SRui Paulo 		dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
16625b9c547cSRui Paulo 	else {
1663f05cddf9SRui Paulo 		dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
16645b9c547cSRui Paulo 		/*
16655b9c547cSRui Paulo 		 * Assign dialog token and tie breaker here to use the same
16665b9c547cSRui Paulo 		 * values in each retry within the same GO Negotiation exchange.
16675b9c547cSRui Paulo 		 */
16685b9c547cSRui Paulo 		dev->dialog_token++;
16695b9c547cSRui Paulo 		if (dev->dialog_token == 0)
16705b9c547cSRui Paulo 			dev->dialog_token = 1;
16715b9c547cSRui Paulo 		dev->tie_breaker = p2p->next_tie_breaker;
16725b9c547cSRui Paulo 		p2p->next_tie_breaker = !p2p->next_tie_breaker;
16735b9c547cSRui Paulo 	}
1674f05cddf9SRui Paulo 	dev->connect_reqs = 0;
1675f05cddf9SRui Paulo 	dev->go_neg_req_sent = 0;
1676f05cddf9SRui Paulo 	dev->go_state = UNKNOWN_GO;
1677f05cddf9SRui Paulo 	p2p_set_dev_persistent(dev, persistent_group);
1678f05cddf9SRui Paulo 	p2p->go_intent = go_intent;
1679f05cddf9SRui Paulo 	os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1680f05cddf9SRui Paulo 
1681f05cddf9SRui Paulo 	if (p2p->state != P2P_IDLE)
1682f05cddf9SRui Paulo 		p2p_stop_find(p2p);
1683f05cddf9SRui Paulo 
1684f05cddf9SRui Paulo 	dev->wps_method = wps_method;
16855b9c547cSRui Paulo 	dev->oob_pw_id = oob_pw_id;
1686f05cddf9SRui Paulo 	dev->status = P2P_SC_SUCCESS;
1687f05cddf9SRui Paulo 
1688f05cddf9SRui Paulo 	if (p2p->p2p_scan_running) {
16895b9c547cSRui Paulo 		p2p_dbg(p2p, "p2p_scan running - delay connect send");
1690f05cddf9SRui Paulo 		p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1691f05cddf9SRui Paulo 		os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1692f05cddf9SRui Paulo 		return 0;
1693f05cddf9SRui Paulo 	}
1694f05cddf9SRui Paulo 
1695f05cddf9SRui Paulo 	return p2p_connect_send(p2p, dev);
1696f05cddf9SRui Paulo }
1697f05cddf9SRui Paulo 
1698f05cddf9SRui Paulo 
p2p_authorize(struct p2p_data * p2p,const u8 * peer_addr,enum p2p_wps_method wps_method,int go_intent,const u8 * own_interface_addr,unsigned int force_freq,int persistent_group,const u8 * force_ssid,size_t force_ssid_len,unsigned int pref_freq,u16 oob_pw_id)1699f05cddf9SRui Paulo int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1700f05cddf9SRui Paulo 		  enum p2p_wps_method wps_method,
1701f05cddf9SRui Paulo 		  int go_intent, const u8 *own_interface_addr,
1702f05cddf9SRui Paulo 		  unsigned int force_freq, int persistent_group,
1703f05cddf9SRui Paulo 		  const u8 *force_ssid, size_t force_ssid_len,
17045b9c547cSRui Paulo 		  unsigned int pref_freq, u16 oob_pw_id)
1705f05cddf9SRui Paulo {
1706f05cddf9SRui Paulo 	struct p2p_device *dev;
1707f05cddf9SRui Paulo 
17085b9c547cSRui Paulo 	p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
1709f05cddf9SRui Paulo 		"  GO Intent=%d  Intended Interface Address=" MACSTR
1710c1d255d3SCy Schubert 		" wps_method=%d  persistent_group=%d oob_pw_id=%u allow_6ghz=%d",
1711f05cddf9SRui Paulo 		MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1712c1d255d3SCy Schubert 		wps_method, persistent_group, oob_pw_id, p2p->allow_6ghz);
1713f05cddf9SRui Paulo 
1714f05cddf9SRui Paulo 	dev = p2p_get_device(p2p, peer_addr);
1715f05cddf9SRui Paulo 	if (dev == NULL) {
17165b9c547cSRui Paulo 		p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
1717f05cddf9SRui Paulo 			MAC2STR(peer_addr));
1718f05cddf9SRui Paulo 		return -1;
1719f05cddf9SRui Paulo 	}
1720f05cddf9SRui Paulo 
17215b9c547cSRui Paulo 	if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
17225b9c547cSRui Paulo 				15) < 0)
1723f05cddf9SRui Paulo 		return -1;
1724f05cddf9SRui Paulo 
1725f05cddf9SRui Paulo 	p2p->ssid_set = 0;
1726f05cddf9SRui Paulo 	if (force_ssid) {
1727f05cddf9SRui Paulo 		wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1728f05cddf9SRui Paulo 				  force_ssid, force_ssid_len);
1729f05cddf9SRui Paulo 		os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1730f05cddf9SRui Paulo 		p2p->ssid_len = force_ssid_len;
1731f05cddf9SRui Paulo 		p2p->ssid_set = 1;
1732f05cddf9SRui Paulo 	}
1733f05cddf9SRui Paulo 
1734f05cddf9SRui Paulo 	dev->flags &= ~P2P_DEV_NOT_YET_READY;
1735f05cddf9SRui Paulo 	dev->flags &= ~P2P_DEV_USER_REJECTED;
1736f05cddf9SRui Paulo 	dev->go_neg_req_sent = 0;
1737f05cddf9SRui Paulo 	dev->go_state = UNKNOWN_GO;
1738f05cddf9SRui Paulo 	p2p_set_dev_persistent(dev, persistent_group);
1739f05cddf9SRui Paulo 	p2p->go_intent = go_intent;
1740f05cddf9SRui Paulo 	os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1741f05cddf9SRui Paulo 
1742f05cddf9SRui Paulo 	dev->wps_method = wps_method;
17435b9c547cSRui Paulo 	dev->oob_pw_id = oob_pw_id;
1744f05cddf9SRui Paulo 	dev->status = P2P_SC_SUCCESS;
1745f05cddf9SRui Paulo 
1746f05cddf9SRui Paulo 	return 0;
1747f05cddf9SRui Paulo }
1748f05cddf9SRui Paulo 
1749f05cddf9SRui Paulo 
p2p_add_dev_info(struct p2p_data * p2p,const u8 * addr,struct p2p_device * dev,struct p2p_message * msg)1750f05cddf9SRui Paulo void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1751f05cddf9SRui Paulo 		      struct p2p_device *dev, struct p2p_message *msg)
1752f05cddf9SRui Paulo {
17535b9c547cSRui Paulo 	os_get_reltime(&dev->last_seen);
1754f05cddf9SRui Paulo 
17555b9c547cSRui Paulo 	p2p_copy_wps_info(p2p, dev, 0, msg);
1756f05cddf9SRui Paulo 
1757f05cddf9SRui Paulo 	if (msg->listen_channel) {
1758f05cddf9SRui Paulo 		int freq;
17595b9c547cSRui Paulo 		freq = p2p_channel_to_freq(msg->listen_channel[3],
1760f05cddf9SRui Paulo 					   msg->listen_channel[4]);
1761f05cddf9SRui Paulo 		if (freq < 0) {
17625b9c547cSRui Paulo 			p2p_dbg(p2p, "Unknown peer Listen channel: "
1763f05cddf9SRui Paulo 				"country=%c%c(0x%02x) reg_class=%u channel=%u",
1764f05cddf9SRui Paulo 				msg->listen_channel[0],
1765f05cddf9SRui Paulo 				msg->listen_channel[1],
1766f05cddf9SRui Paulo 				msg->listen_channel[2],
1767f05cddf9SRui Paulo 				msg->listen_channel[3],
1768f05cddf9SRui Paulo 				msg->listen_channel[4]);
1769f05cddf9SRui Paulo 		} else {
17705b9c547cSRui Paulo 			p2p_dbg(p2p, "Update peer " MACSTR
17715b9c547cSRui Paulo 				" Listen channel: %u -> %u MHz",
1772f05cddf9SRui Paulo 				MAC2STR(dev->info.p2p_device_addr),
1773f05cddf9SRui Paulo 				dev->listen_freq, freq);
1774f05cddf9SRui Paulo 			dev->listen_freq = freq;
1775f05cddf9SRui Paulo 		}
1776f05cddf9SRui Paulo 	}
1777f05cddf9SRui Paulo 
1778f05cddf9SRui Paulo 	if (msg->wfd_subelems) {
1779f05cddf9SRui Paulo 		wpabuf_free(dev->info.wfd_subelems);
1780f05cddf9SRui Paulo 		dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1781f05cddf9SRui Paulo 	}
1782f05cddf9SRui Paulo 
1783f05cddf9SRui Paulo 	if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1784f05cddf9SRui Paulo 		dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
17855b9c547cSRui Paulo 		p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
1786f05cddf9SRui Paulo 	} else {
17875b9c547cSRui Paulo 		p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
1788f05cddf9SRui Paulo 			MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1789f05cddf9SRui Paulo 			"listen_freq=%d",
1790f05cddf9SRui Paulo 			MAC2STR(dev->info.p2p_device_addr),
1791f05cddf9SRui Paulo 			dev->info.dev_capab, dev->info.group_capab,
1792f05cddf9SRui Paulo 			dev->info.device_name, dev->listen_freq);
1793f05cddf9SRui Paulo 	}
1794f05cddf9SRui Paulo 
1795f05cddf9SRui Paulo 	dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1796f05cddf9SRui Paulo 
1797f05cddf9SRui Paulo 	if (dev->flags & P2P_DEV_USER_REJECTED) {
17985b9c547cSRui Paulo 		p2p_dbg(p2p, "Do not report rejected device");
1799f05cddf9SRui Paulo 		return;
1800f05cddf9SRui Paulo 	}
1801f05cddf9SRui Paulo 
1802f05cddf9SRui Paulo 	p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1803f05cddf9SRui Paulo 			    !(dev->flags & P2P_DEV_REPORTED_ONCE));
1804f05cddf9SRui Paulo 	dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1805f05cddf9SRui Paulo }
1806f05cddf9SRui Paulo 
1807f05cddf9SRui Paulo 
p2p_build_ssid(struct p2p_data * p2p,u8 * ssid,size_t * ssid_len)1808f05cddf9SRui Paulo void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1809f05cddf9SRui Paulo {
1810f05cddf9SRui Paulo 	os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1811f05cddf9SRui Paulo 	p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1812f05cddf9SRui Paulo 	os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1813f05cddf9SRui Paulo 		  p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1814f05cddf9SRui Paulo 	*ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1815f05cddf9SRui Paulo }
1816f05cddf9SRui Paulo 
1817f05cddf9SRui Paulo 
p2p_go_params(struct p2p_data * p2p,struct p2p_go_neg_results * params)1818f05cddf9SRui Paulo int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1819f05cddf9SRui Paulo {
18205b9c547cSRui Paulo 	if (p2p->ssid_set) {
18215b9c547cSRui Paulo 		os_memcpy(params->ssid, p2p->ssid, p2p->ssid_len);
18225b9c547cSRui Paulo 		params->ssid_len = p2p->ssid_len;
18235b9c547cSRui Paulo 	} else {
1824f05cddf9SRui Paulo 		p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
18255b9c547cSRui Paulo 	}
18265b9c547cSRui Paulo 	p2p->ssid_set = 0;
18275b9c547cSRui Paulo 
18285b9c547cSRui Paulo 	p2p_random(params->passphrase, p2p->cfg->passphrase_len);
1829*a90b9d01SCy Schubert 	params->passphrase[p2p->cfg->passphrase_len] = '\0';
1830f05cddf9SRui Paulo 	return 0;
1831f05cddf9SRui Paulo }
1832f05cddf9SRui Paulo 
1833f05cddf9SRui Paulo 
p2p_go_complete(struct p2p_data * p2p,struct p2p_device * peer)1834f05cddf9SRui Paulo void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1835f05cddf9SRui Paulo {
1836f05cddf9SRui Paulo 	struct p2p_go_neg_results res;
1837f05cddf9SRui Paulo 	int go = peer->go_state == LOCAL_GO;
1838f05cddf9SRui Paulo 	struct p2p_channels intersection;
1839f05cddf9SRui Paulo 
18405b9c547cSRui Paulo 	p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
18415b9c547cSRui Paulo 		MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
1842f05cddf9SRui Paulo 
1843f05cddf9SRui Paulo 	os_memset(&res, 0, sizeof(res));
1844f05cddf9SRui Paulo 	res.role_go = go;
1845f05cddf9SRui Paulo 	os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1846f05cddf9SRui Paulo 	os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1847f05cddf9SRui Paulo 	res.wps_method = peer->wps_method;
1848f05cddf9SRui Paulo 	if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1849f05cddf9SRui Paulo 		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1850f05cddf9SRui Paulo 			res.persistent_group = 2;
1851f05cddf9SRui Paulo 		else
1852f05cddf9SRui Paulo 			res.persistent_group = 1;
1853f05cddf9SRui Paulo 	}
1854f05cddf9SRui Paulo 
1855f05cddf9SRui Paulo 	if (go) {
1856f05cddf9SRui Paulo 		/* Setup AP mode for WPS provisioning */
18575b9c547cSRui Paulo 		res.freq = p2p_channel_to_freq(p2p->op_reg_class,
1858f05cddf9SRui Paulo 					       p2p->op_channel);
1859f05cddf9SRui Paulo 		os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1860f05cddf9SRui Paulo 		res.ssid_len = p2p->ssid_len;
18615b9c547cSRui Paulo 		p2p_random(res.passphrase, p2p->cfg->passphrase_len);
1862*a90b9d01SCy Schubert 		res.passphrase[p2p->cfg->passphrase_len] = '\0';
1863f05cddf9SRui Paulo 	} else {
1864f05cddf9SRui Paulo 		res.freq = peer->oper_freq;
1865f05cddf9SRui Paulo 		if (p2p->ssid_len) {
1866f05cddf9SRui Paulo 			os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1867f05cddf9SRui Paulo 			res.ssid_len = p2p->ssid_len;
1868f05cddf9SRui Paulo 		}
1869f05cddf9SRui Paulo 	}
1870f05cddf9SRui Paulo 
18715b9c547cSRui Paulo 	p2p_channels_dump(p2p, "own channels", &p2p->channels);
18725b9c547cSRui Paulo 	p2p_channels_dump(p2p, "peer channels", &peer->channels);
1873f05cddf9SRui Paulo 	p2p_channels_intersect(&p2p->channels, &peer->channels,
1874f05cddf9SRui Paulo 			       &intersection);
18755b9c547cSRui Paulo 	if (go) {
18765b9c547cSRui Paulo 		p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
18775b9c547cSRui Paulo 		p2p_channels_dump(p2p, "intersection after no-GO removal",
18785b9c547cSRui Paulo 				  &intersection);
1879f05cddf9SRui Paulo 	}
18805b9c547cSRui Paulo 
18815b9c547cSRui Paulo 	p2p_channels_to_freqs(&intersection, res.freq_list,
18825b9c547cSRui Paulo 			      P2P_MAX_CHANNELS);
1883f05cddf9SRui Paulo 
1884f05cddf9SRui Paulo 	res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1885f05cddf9SRui Paulo 
1886f05cddf9SRui Paulo 	p2p_clear_timeout(p2p);
1887f05cddf9SRui Paulo 	p2p->ssid_set = 0;
1888f05cddf9SRui Paulo 	peer->go_neg_req_sent = 0;
188985732ac8SCy Schubert 	peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
1890f05cddf9SRui Paulo 	peer->wps_method = WPS_NOT_READY;
18915b9c547cSRui Paulo 	peer->oob_pw_id = 0;
18925b9c547cSRui Paulo 	wpabuf_free(peer->go_neg_conf);
18935b9c547cSRui Paulo 	peer->go_neg_conf = NULL;
1894f05cddf9SRui Paulo 
1895f05cddf9SRui Paulo 	p2p_set_state(p2p, P2P_PROVISIONING);
1896f05cddf9SRui Paulo 	p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1897f05cddf9SRui Paulo }
1898f05cddf9SRui Paulo 
1899f05cddf9SRui Paulo 
p2p_rx_p2p_action(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len,int rx_freq)1900f05cddf9SRui Paulo static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1901f05cddf9SRui Paulo 			      const u8 *data, size_t len, int rx_freq)
1902f05cddf9SRui Paulo {
19035b9c547cSRui Paulo 	p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
1904f05cddf9SRui Paulo 	wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1905f05cddf9SRui Paulo 
1906f05cddf9SRui Paulo 	if (len < 1)
1907f05cddf9SRui Paulo 		return;
1908f05cddf9SRui Paulo 
1909f05cddf9SRui Paulo 	switch (data[0]) {
1910f05cddf9SRui Paulo 	case P2P_GO_NEG_REQ:
1911f05cddf9SRui Paulo 		p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1912f05cddf9SRui Paulo 		break;
1913f05cddf9SRui Paulo 	case P2P_GO_NEG_RESP:
1914f05cddf9SRui Paulo 		p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1915f05cddf9SRui Paulo 		break;
1916f05cddf9SRui Paulo 	case P2P_GO_NEG_CONF:
1917f05cddf9SRui Paulo 		p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1918f05cddf9SRui Paulo 		break;
1919f05cddf9SRui Paulo 	case P2P_INVITATION_REQ:
1920f05cddf9SRui Paulo 		p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1921f05cddf9SRui Paulo 					   rx_freq);
1922f05cddf9SRui Paulo 		break;
1923f05cddf9SRui Paulo 	case P2P_INVITATION_RESP:
1924f05cddf9SRui Paulo 		p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1925f05cddf9SRui Paulo 		break;
1926f05cddf9SRui Paulo 	case P2P_PROV_DISC_REQ:
1927f05cddf9SRui Paulo 		p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1928f05cddf9SRui Paulo 		break;
1929f05cddf9SRui Paulo 	case P2P_PROV_DISC_RESP:
1930f05cddf9SRui Paulo 		p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1931f05cddf9SRui Paulo 		break;
1932f05cddf9SRui Paulo 	case P2P_DEV_DISC_REQ:
1933f05cddf9SRui Paulo 		p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1934f05cddf9SRui Paulo 		break;
1935f05cddf9SRui Paulo 	case P2P_DEV_DISC_RESP:
1936f05cddf9SRui Paulo 		p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1937f05cddf9SRui Paulo 		break;
1938f05cddf9SRui Paulo 	default:
19395b9c547cSRui Paulo 		p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
1940f05cddf9SRui Paulo 			data[0]);
1941f05cddf9SRui Paulo 		break;
1942f05cddf9SRui Paulo 	}
1943f05cddf9SRui Paulo }
1944f05cddf9SRui Paulo 
1945f05cddf9SRui Paulo 
p2p_rx_action_public(struct p2p_data * p2p,const u8 * da,const u8 * sa,const u8 * bssid,const u8 * data,size_t len,int freq)1946f05cddf9SRui Paulo static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1947f05cddf9SRui Paulo 				 const u8 *sa, const u8 *bssid, const u8 *data,
1948f05cddf9SRui Paulo 				 size_t len, int freq)
1949f05cddf9SRui Paulo {
1950f05cddf9SRui Paulo 	if (len < 1)
1951f05cddf9SRui Paulo 		return;
1952f05cddf9SRui Paulo 
1953f05cddf9SRui Paulo 	switch (data[0]) {
1954f05cddf9SRui Paulo 	case WLAN_PA_VENDOR_SPECIFIC:
1955f05cddf9SRui Paulo 		data++;
1956f05cddf9SRui Paulo 		len--;
19575b9c547cSRui Paulo 		if (len < 4)
1958f05cddf9SRui Paulo 			return;
19595b9c547cSRui Paulo 		if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
1960f05cddf9SRui Paulo 			return;
1961f05cddf9SRui Paulo 
19625b9c547cSRui Paulo 		data += 4;
19635b9c547cSRui Paulo 		len -= 4;
1964f05cddf9SRui Paulo 
19655b9c547cSRui Paulo 		p2p_rx_p2p_action(p2p, sa, data, len, freq);
1966f05cddf9SRui Paulo 		break;
1967f05cddf9SRui Paulo 	case WLAN_PA_GAS_INITIAL_REQ:
1968f05cddf9SRui Paulo 		p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1969f05cddf9SRui Paulo 		break;
1970f05cddf9SRui Paulo 	case WLAN_PA_GAS_INITIAL_RESP:
1971f05cddf9SRui Paulo 		p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1972f05cddf9SRui Paulo 		break;
1973f05cddf9SRui Paulo 	case WLAN_PA_GAS_COMEBACK_REQ:
1974f05cddf9SRui Paulo 		p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1975f05cddf9SRui Paulo 		break;
1976f05cddf9SRui Paulo 	case WLAN_PA_GAS_COMEBACK_RESP:
1977f05cddf9SRui Paulo 		p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1978f05cddf9SRui Paulo 		break;
1979f05cddf9SRui Paulo 	}
1980f05cddf9SRui Paulo }
1981f05cddf9SRui Paulo 
1982f05cddf9SRui Paulo 
p2p_rx_action(struct p2p_data * p2p,const u8 * da,const u8 * sa,const u8 * bssid,u8 category,const u8 * data,size_t len,int freq)1983f05cddf9SRui Paulo void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1984f05cddf9SRui Paulo 		   const u8 *bssid, u8 category,
1985f05cddf9SRui Paulo 		   const u8 *data, size_t len, int freq)
1986f05cddf9SRui Paulo {
1987f05cddf9SRui Paulo 	if (category == WLAN_ACTION_PUBLIC) {
1988f05cddf9SRui Paulo 		p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1989f05cddf9SRui Paulo 		return;
1990f05cddf9SRui Paulo 	}
1991f05cddf9SRui Paulo 
1992f05cddf9SRui Paulo 	if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1993f05cddf9SRui Paulo 		return;
1994f05cddf9SRui Paulo 
1995f05cddf9SRui Paulo 	if (len < 4)
1996f05cddf9SRui Paulo 		return;
1997f05cddf9SRui Paulo 
19985b9c547cSRui Paulo 	if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
1999f05cddf9SRui Paulo 		return;
20005b9c547cSRui Paulo 	data += 4;
20015b9c547cSRui Paulo 	len -= 4;
2002f05cddf9SRui Paulo 
2003f05cddf9SRui Paulo 	/* P2P action frame */
20045b9c547cSRui Paulo 	p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
2005f05cddf9SRui Paulo 	wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
2006f05cddf9SRui Paulo 
2007f05cddf9SRui Paulo 	if (len < 1)
2008f05cddf9SRui Paulo 		return;
2009f05cddf9SRui Paulo 	switch (data[0]) {
2010f05cddf9SRui Paulo 	case P2P_NOA:
20115b9c547cSRui Paulo 		p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
2012f05cddf9SRui Paulo 		/* TODO */
2013f05cddf9SRui Paulo 		break;
2014f05cddf9SRui Paulo 	case P2P_PRESENCE_REQ:
2015f05cddf9SRui Paulo 		p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
2016f05cddf9SRui Paulo 		break;
2017f05cddf9SRui Paulo 	case P2P_PRESENCE_RESP:
2018f05cddf9SRui Paulo 		p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
2019f05cddf9SRui Paulo 		break;
2020f05cddf9SRui Paulo 	case P2P_GO_DISC_REQ:
2021f05cddf9SRui Paulo 		p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
2022f05cddf9SRui Paulo 		break;
2023f05cddf9SRui Paulo 	default:
20245b9c547cSRui Paulo 		p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
2025f05cddf9SRui Paulo 		break;
2026f05cddf9SRui Paulo 	}
2027f05cddf9SRui Paulo }
2028f05cddf9SRui Paulo 
2029f05cddf9SRui Paulo 
p2p_go_neg_start(void * eloop_ctx,void * timeout_ctx)2030f05cddf9SRui Paulo static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
2031f05cddf9SRui Paulo {
2032f05cddf9SRui Paulo 	struct p2p_data *p2p = eloop_ctx;
2033f05cddf9SRui Paulo 	if (p2p->go_neg_peer == NULL)
2034f05cddf9SRui Paulo 		return;
20355b9c547cSRui Paulo 	if (p2p->pending_listen_freq) {
20365b9c547cSRui Paulo 		p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start");
20375b9c547cSRui Paulo 		p2p->pending_listen_freq = 0;
20385b9c547cSRui Paulo 	}
2039f05cddf9SRui Paulo 	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
2040*a90b9d01SCy Schubert 	p2p->pending_listen_wait_drv = false;
2041f05cddf9SRui Paulo 	p2p->go_neg_peer->status = P2P_SC_SUCCESS;
20425b9c547cSRui Paulo 	/*
20435b9c547cSRui Paulo 	 * Set new timeout to make sure a previously set one does not expire
20445b9c547cSRui Paulo 	 * too quickly while waiting for the GO Negotiation to complete.
20455b9c547cSRui Paulo 	 */
20465b9c547cSRui Paulo 	p2p_set_timeout(p2p, 0, 500000);
2047f05cddf9SRui Paulo 	p2p_connect_send(p2p, p2p->go_neg_peer);
2048f05cddf9SRui Paulo }
2049f05cddf9SRui Paulo 
2050f05cddf9SRui Paulo 
p2p_invite_start(void * eloop_ctx,void * timeout_ctx)2051f05cddf9SRui Paulo static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
2052f05cddf9SRui Paulo {
2053f05cddf9SRui Paulo 	struct p2p_data *p2p = eloop_ctx;
2054f05cddf9SRui Paulo 	if (p2p->invite_peer == NULL)
2055f05cddf9SRui Paulo 		return;
20565b9c547cSRui Paulo 	if (p2p->pending_listen_freq) {
20575b9c547cSRui Paulo 		p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start");
20585b9c547cSRui Paulo 		p2p->pending_listen_freq = 0;
20595b9c547cSRui Paulo 	}
2060f05cddf9SRui Paulo 	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
2061*a90b9d01SCy Schubert 	p2p->pending_listen_wait_drv = false;
20625b9c547cSRui Paulo 	p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
20635b9c547cSRui Paulo 			p2p->invite_dev_pw_id);
2064f05cddf9SRui Paulo }
2065f05cddf9SRui Paulo 
2066f05cddf9SRui Paulo 
p2p_add_dev_from_probe_req(struct p2p_data * p2p,const u8 * addr,const u8 * ie,size_t ie_len)2067f05cddf9SRui Paulo static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
2068f05cddf9SRui Paulo 				       const u8 *ie, size_t ie_len)
2069f05cddf9SRui Paulo {
2070f05cddf9SRui Paulo 	struct p2p_message msg;
2071f05cddf9SRui Paulo 	struct p2p_device *dev;
2072f05cddf9SRui Paulo 
2073f05cddf9SRui Paulo 	os_memset(&msg, 0, sizeof(msg));
2074f05cddf9SRui Paulo 	if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
2075f05cddf9SRui Paulo 	{
2076f05cddf9SRui Paulo 		p2p_parse_free(&msg);
2077f05cddf9SRui Paulo 		return; /* not a P2P probe */
2078f05cddf9SRui Paulo 	}
2079f05cddf9SRui Paulo 
2080f05cddf9SRui Paulo 	if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
2081f05cddf9SRui Paulo 	    os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
2082f05cddf9SRui Paulo 	    != 0) {
2083f05cddf9SRui Paulo 		/* The Probe Request is not part of P2P Device Discovery. It is
2084f05cddf9SRui Paulo 		 * not known whether the source address of the frame is the P2P
2085f05cddf9SRui Paulo 		 * Device Address or P2P Interface Address. Do not add a new
2086f05cddf9SRui Paulo 		 * peer entry based on this frames.
2087f05cddf9SRui Paulo 		 */
2088f05cddf9SRui Paulo 		p2p_parse_free(&msg);
2089f05cddf9SRui Paulo 		return;
2090f05cddf9SRui Paulo 	}
2091f05cddf9SRui Paulo 
2092f05cddf9SRui Paulo 	dev = p2p_get_device(p2p, addr);
2093f05cddf9SRui Paulo 	if (dev) {
2094780fb4a2SCy Schubert 		if (msg.listen_channel) {
2095780fb4a2SCy Schubert 			int freq;
2096780fb4a2SCy Schubert 
2097780fb4a2SCy Schubert 			if (dev->country[0] == 0)
2098f05cddf9SRui Paulo 				os_memcpy(dev->country, msg.listen_channel, 3);
2099780fb4a2SCy Schubert 
2100780fb4a2SCy Schubert 			freq = p2p_channel_to_freq(msg.listen_channel[3],
2101780fb4a2SCy Schubert 						   msg.listen_channel[4]);
2102780fb4a2SCy Schubert 
2103780fb4a2SCy Schubert 			if (freq > 0 && dev->listen_freq != freq) {
2104780fb4a2SCy Schubert 				p2p_dbg(p2p,
2105780fb4a2SCy Schubert 					"Updated peer " MACSTR " Listen channel (Probe Request): %d -> %d MHz",
2106780fb4a2SCy Schubert 					MAC2STR(addr), dev->listen_freq, freq);
2107780fb4a2SCy Schubert 				dev->listen_freq = freq;
2108780fb4a2SCy Schubert 			}
2109780fb4a2SCy Schubert 		}
2110780fb4a2SCy Schubert 
2111*a90b9d01SCy Schubert 		p2p_update_peer_6ghz_capab(dev, &msg);
21125b9c547cSRui Paulo 		os_get_reltime(&dev->last_seen);
2113f05cddf9SRui Paulo 		p2p_parse_free(&msg);
2114f05cddf9SRui Paulo 		return; /* already known */
2115f05cddf9SRui Paulo 	}
2116f05cddf9SRui Paulo 
2117f05cddf9SRui Paulo 	dev = p2p_create_device(p2p, addr);
2118f05cddf9SRui Paulo 	if (dev == NULL) {
2119f05cddf9SRui Paulo 		p2p_parse_free(&msg);
2120f05cddf9SRui Paulo 		return;
2121f05cddf9SRui Paulo 	}
2122f05cddf9SRui Paulo 
21235b9c547cSRui Paulo 	os_get_reltime(&dev->last_seen);
2124f05cddf9SRui Paulo 	dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
2125f05cddf9SRui Paulo 
2126f05cddf9SRui Paulo 	if (msg.listen_channel) {
2127f05cddf9SRui Paulo 		os_memcpy(dev->country, msg.listen_channel, 3);
21285b9c547cSRui Paulo 		dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
2129f05cddf9SRui Paulo 						       msg.listen_channel[4]);
2130f05cddf9SRui Paulo 	}
2131f05cddf9SRui Paulo 
21325b9c547cSRui Paulo 	p2p_copy_wps_info(p2p, dev, 1, &msg);
2133f05cddf9SRui Paulo 
2134f05cddf9SRui Paulo 	if (msg.wfd_subelems) {
2135f05cddf9SRui Paulo 		wpabuf_free(dev->info.wfd_subelems);
2136f05cddf9SRui Paulo 		dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
2137f05cddf9SRui Paulo 	}
2138f05cddf9SRui Paulo 
2139f05cddf9SRui Paulo 	p2p_parse_free(&msg);
2140f05cddf9SRui Paulo 
21415b9c547cSRui Paulo 	p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
2142f05cddf9SRui Paulo 		" dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
2143f05cddf9SRui Paulo 		MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
2144f05cddf9SRui Paulo 		dev->info.group_capab, dev->info.device_name,
2145f05cddf9SRui Paulo 		dev->listen_freq);
2146f05cddf9SRui Paulo }
2147f05cddf9SRui Paulo 
2148f05cddf9SRui Paulo 
p2p_add_dev_from_go_neg_req(struct p2p_data * p2p,const u8 * addr,struct p2p_message * msg)2149f05cddf9SRui Paulo struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
2150f05cddf9SRui Paulo 						const u8 *addr,
2151f05cddf9SRui Paulo 						struct p2p_message *msg)
2152f05cddf9SRui Paulo {
2153f05cddf9SRui Paulo 	struct p2p_device *dev;
2154f05cddf9SRui Paulo 
2155f05cddf9SRui Paulo 	dev = p2p_get_device(p2p, addr);
2156f05cddf9SRui Paulo 	if (dev) {
21575b9c547cSRui Paulo 		os_get_reltime(&dev->last_seen);
2158f05cddf9SRui Paulo 		return dev; /* already known */
2159f05cddf9SRui Paulo 	}
2160f05cddf9SRui Paulo 
2161f05cddf9SRui Paulo 	dev = p2p_create_device(p2p, addr);
2162f05cddf9SRui Paulo 	if (dev == NULL)
2163f05cddf9SRui Paulo 		return NULL;
2164f05cddf9SRui Paulo 
2165f05cddf9SRui Paulo 	p2p_add_dev_info(p2p, addr, dev, msg);
2166f05cddf9SRui Paulo 
2167f05cddf9SRui Paulo 	return dev;
2168f05cddf9SRui Paulo }
2169f05cddf9SRui Paulo 
2170f05cddf9SRui Paulo 
dev_type_match(const u8 * dev_type,const u8 * req_dev_type)2171f05cddf9SRui Paulo static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
2172f05cddf9SRui Paulo {
2173f05cddf9SRui Paulo 	if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
2174f05cddf9SRui Paulo 		return 1;
2175f05cddf9SRui Paulo 	if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
2176f05cddf9SRui Paulo 	    WPA_GET_BE32(&req_dev_type[2]) == 0 &&
2177f05cddf9SRui Paulo 	    WPA_GET_BE16(&req_dev_type[6]) == 0)
2178f05cddf9SRui Paulo 		return 1; /* Category match with wildcard OUI/sub-category */
2179f05cddf9SRui Paulo 	return 0;
2180f05cddf9SRui Paulo }
2181f05cddf9SRui Paulo 
2182f05cddf9SRui Paulo 
dev_type_list_match(const u8 * dev_type,const u8 * req_dev_type[],size_t num_req_dev_type)2183f05cddf9SRui Paulo int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
2184f05cddf9SRui Paulo 			size_t num_req_dev_type)
2185f05cddf9SRui Paulo {
2186f05cddf9SRui Paulo 	size_t i;
2187f05cddf9SRui Paulo 	for (i = 0; i < num_req_dev_type; i++) {
2188f05cddf9SRui Paulo 		if (dev_type_match(dev_type, req_dev_type[i]))
2189f05cddf9SRui Paulo 			return 1;
2190f05cddf9SRui Paulo 	}
2191f05cddf9SRui Paulo 	return 0;
2192f05cddf9SRui Paulo }
2193f05cddf9SRui Paulo 
2194f05cddf9SRui Paulo 
2195f05cddf9SRui Paulo /**
2196f05cddf9SRui Paulo  * p2p_match_dev_type - Match local device type with requested type
2197f05cddf9SRui Paulo  * @p2p: P2P module context from p2p_init()
2198f05cddf9SRui Paulo  * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
2199f05cddf9SRui Paulo  * Returns: 1 on match, 0 on mismatch
2200f05cddf9SRui Paulo  *
2201f05cddf9SRui Paulo  * This function can be used to match the Requested Device Type attribute in
2202f05cddf9SRui Paulo  * WPS IE with the local device types for deciding whether to reply to a Probe
2203f05cddf9SRui Paulo  * Request frame.
2204f05cddf9SRui Paulo  */
p2p_match_dev_type(struct p2p_data * p2p,struct wpabuf * wps)2205f05cddf9SRui Paulo int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
2206f05cddf9SRui Paulo {
2207f05cddf9SRui Paulo 	struct wps_parse_attr attr;
2208f05cddf9SRui Paulo 	size_t i;
2209f05cddf9SRui Paulo 
2210f05cddf9SRui Paulo 	if (wps_parse_msg(wps, &attr))
2211f05cddf9SRui Paulo 		return 1; /* assume no Requested Device Type attributes */
2212f05cddf9SRui Paulo 
2213f05cddf9SRui Paulo 	if (attr.num_req_dev_type == 0)
2214f05cddf9SRui Paulo 		return 1; /* no Requested Device Type attributes -> match */
2215f05cddf9SRui Paulo 
2216f05cddf9SRui Paulo 	if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
2217f05cddf9SRui Paulo 				attr.num_req_dev_type))
2218f05cddf9SRui Paulo 		return 1; /* Own Primary Device Type matches */
2219f05cddf9SRui Paulo 
22205b9c547cSRui Paulo 	for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) {
2221f05cddf9SRui Paulo 		if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
2222f05cddf9SRui Paulo 					attr.req_dev_type,
2223f05cddf9SRui Paulo 					attr.num_req_dev_type))
2224f05cddf9SRui Paulo 			return 1; /* Own Secondary Device Type matches */
22255b9c547cSRui Paulo 	}
2226f05cddf9SRui Paulo 
2227f05cddf9SRui Paulo 	/* No matching device type found */
2228f05cddf9SRui Paulo 	return 0;
2229f05cddf9SRui Paulo }
2230f05cddf9SRui Paulo 
2231f05cddf9SRui Paulo 
p2p_build_probe_resp_ies(struct p2p_data * p2p,const u8 * query_hash,u8 query_count)2232325151a3SRui Paulo struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p,
2233325151a3SRui Paulo 					 const u8 *query_hash,
2234325151a3SRui Paulo 					 u8 query_count)
2235f05cddf9SRui Paulo {
2236f05cddf9SRui Paulo 	struct wpabuf *buf;
2237f05cddf9SRui Paulo 	u8 *len;
2238f05cddf9SRui Paulo 	int pw_id = -1;
2239f05cddf9SRui Paulo 	size_t extra = 0;
2240f05cddf9SRui Paulo 
2241f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
2242f05cddf9SRui Paulo 	if (p2p->wfd_ie_probe_resp)
2243f05cddf9SRui Paulo 		extra = wpabuf_len(p2p->wfd_ie_probe_resp);
2244f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
2245f05cddf9SRui Paulo 
22465b9c547cSRui Paulo 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
22475b9c547cSRui Paulo 		extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
22485b9c547cSRui Paulo 
2249325151a3SRui Paulo 	if (query_count)
22505b9c547cSRui Paulo 		extra += MAX_SVC_ADV_IE_LEN;
22515b9c547cSRui Paulo 
2252f05cddf9SRui Paulo 	buf = wpabuf_alloc(1000 + extra);
2253f05cddf9SRui Paulo 	if (buf == NULL)
2254f05cddf9SRui Paulo 		return NULL;
2255f05cddf9SRui Paulo 
2256f05cddf9SRui Paulo 	if (p2p->go_neg_peer) {
2257f05cddf9SRui Paulo 		/* Advertise immediate availability of WPS credential */
2258f05cddf9SRui Paulo 		pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
2259f05cddf9SRui Paulo 	}
2260f05cddf9SRui Paulo 
22615b9c547cSRui Paulo 	if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
22625b9c547cSRui Paulo 		p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
22635b9c547cSRui Paulo 		wpabuf_free(buf);
22645b9c547cSRui Paulo 		return NULL;
22655b9c547cSRui Paulo 	}
2266f05cddf9SRui Paulo 
2267f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
2268f05cddf9SRui Paulo 	if (p2p->wfd_ie_probe_resp)
2269f05cddf9SRui Paulo 		wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
2270f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
2271f05cddf9SRui Paulo 
22725b9c547cSRui Paulo 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
22735b9c547cSRui Paulo 		wpabuf_put_buf(buf,
22745b9c547cSRui Paulo 			       p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
22755b9c547cSRui Paulo 
2276f05cddf9SRui Paulo 	/* P2P IE */
2277f05cddf9SRui Paulo 	len = p2p_buf_add_ie_hdr(buf);
2278f05cddf9SRui Paulo 	p2p_buf_add_capability(buf, p2p->dev_capab &
2279f05cddf9SRui Paulo 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
2280f05cddf9SRui Paulo 	if (p2p->ext_listen_interval)
2281f05cddf9SRui Paulo 		p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
2282f05cddf9SRui Paulo 					      p2p->ext_listen_interval);
2283f05cddf9SRui Paulo 	p2p_buf_add_device_info(buf, p2p, NULL);
2284f05cddf9SRui Paulo 	p2p_buf_update_ie_hdr(buf, len);
2285f05cddf9SRui Paulo 
2286325151a3SRui Paulo 	if (query_count) {
2287325151a3SRui Paulo 		p2p_buf_add_service_instance(buf, p2p, query_count, query_hash,
22885b9c547cSRui Paulo 					     p2p->p2ps_adv_list);
22895b9c547cSRui Paulo 	}
22905b9c547cSRui Paulo 
2291f05cddf9SRui Paulo 	return buf;
2292f05cddf9SRui Paulo }
2293f05cddf9SRui Paulo 
p2p_build_probe_resp_buf(struct p2p_data * p2p,struct wpabuf * buf,struct wpabuf * ies,const u8 * addr,int rx_freq)2294780fb4a2SCy Schubert static int p2p_build_probe_resp_buf(struct p2p_data *p2p, struct wpabuf *buf,
2295780fb4a2SCy Schubert 				    struct wpabuf *ies,
2296780fb4a2SCy Schubert 				    const u8 *addr, int rx_freq)
2297780fb4a2SCy Schubert {
2298780fb4a2SCy Schubert 	struct ieee80211_mgmt *resp;
2299780fb4a2SCy Schubert 	u8 channel, op_class;
2300780fb4a2SCy Schubert 
2301780fb4a2SCy Schubert 	resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
2302780fb4a2SCy Schubert 					u.probe_resp.variable));
2303780fb4a2SCy Schubert 
2304780fb4a2SCy Schubert 	resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2305780fb4a2SCy Schubert 					   (WLAN_FC_STYPE_PROBE_RESP << 4));
2306780fb4a2SCy Schubert 	os_memcpy(resp->da, addr, ETH_ALEN);
2307780fb4a2SCy Schubert 	os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2308780fb4a2SCy Schubert 	os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2309780fb4a2SCy Schubert 	resp->u.probe_resp.beacon_int = host_to_le16(100);
2310780fb4a2SCy Schubert 	/* hardware or low-level driver will setup seq_ctrl and timestamp */
2311780fb4a2SCy Schubert 	resp->u.probe_resp.capab_info =
2312780fb4a2SCy Schubert 	    host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2313780fb4a2SCy Schubert 		     WLAN_CAPABILITY_PRIVACY |
2314780fb4a2SCy Schubert 		     WLAN_CAPABILITY_SHORT_SLOT_TIME);
2315780fb4a2SCy Schubert 
2316780fb4a2SCy Schubert 	wpabuf_put_u8(buf, WLAN_EID_SSID);
2317780fb4a2SCy Schubert 	wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2318780fb4a2SCy Schubert 	wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2319780fb4a2SCy Schubert 
2320780fb4a2SCy Schubert 	wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2321780fb4a2SCy Schubert 	wpabuf_put_u8(buf, 8);
2322780fb4a2SCy Schubert 	wpabuf_put_u8(buf, (60 / 5) | 0x80);
2323780fb4a2SCy Schubert 	wpabuf_put_u8(buf, 90 / 5);
2324780fb4a2SCy Schubert 	wpabuf_put_u8(buf, (120 / 5) | 0x80);
2325780fb4a2SCy Schubert 	wpabuf_put_u8(buf, 180 / 5);
2326780fb4a2SCy Schubert 	wpabuf_put_u8(buf, (240 / 5) | 0x80);
2327780fb4a2SCy Schubert 	wpabuf_put_u8(buf, 360 / 5);
2328780fb4a2SCy Schubert 	wpabuf_put_u8(buf, 480 / 5);
2329780fb4a2SCy Schubert 	wpabuf_put_u8(buf, 540 / 5);
2330780fb4a2SCy Schubert 
2331780fb4a2SCy Schubert 	if (!rx_freq) {
2332780fb4a2SCy Schubert 		channel = p2p->cfg->channel;
2333780fb4a2SCy Schubert 	} else if (p2p_freq_to_channel(rx_freq, &op_class, &channel)) {
2334780fb4a2SCy Schubert 		p2p_err(p2p, "Failed to convert freq to channel");
2335780fb4a2SCy Schubert 		return -1;
2336780fb4a2SCy Schubert 	}
2337780fb4a2SCy Schubert 
2338780fb4a2SCy Schubert 	wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2339780fb4a2SCy Schubert 	wpabuf_put_u8(buf, 1);
2340780fb4a2SCy Schubert 	wpabuf_put_u8(buf, channel);
2341780fb4a2SCy Schubert 
2342780fb4a2SCy Schubert 	wpabuf_put_buf(buf, ies);
2343780fb4a2SCy Schubert 
2344780fb4a2SCy Schubert 	return 0;
2345780fb4a2SCy Schubert }
2346f05cddf9SRui Paulo 
p2p_service_find_asp(struct p2p_data * p2p,const u8 * hash)23475b9c547cSRui Paulo static int p2p_service_find_asp(struct p2p_data *p2p, const u8 *hash)
2348f05cddf9SRui Paulo {
23495b9c547cSRui Paulo 	struct p2ps_advertisement *adv_data;
2350325151a3SRui Paulo 	int any_wfa;
23515b9c547cSRui Paulo 
23525b9c547cSRui Paulo 	p2p_dbg(p2p, "ASP find - ASP list: %p", p2p->p2ps_adv_list);
23535b9c547cSRui Paulo 
2354325151a3SRui Paulo 	/* Wildcard org.wi-fi.wfds matches any WFA spec defined service */
2355325151a3SRui Paulo 	any_wfa = os_memcmp(hash, p2p->wild_card_hash, P2PS_HASH_LEN) == 0;
23565b9c547cSRui Paulo 
23575b9c547cSRui Paulo 	adv_data = p2p->p2ps_adv_list;
23585b9c547cSRui Paulo 	while (adv_data) {
23595b9c547cSRui Paulo 		if (os_memcmp(hash, adv_data->hash, P2PS_HASH_LEN) == 0)
2360325151a3SRui Paulo 			return 1; /* exact hash match */
2361325151a3SRui Paulo 		if (any_wfa &&
2362325151a3SRui Paulo 		    os_strncmp(adv_data->svc_name, P2PS_WILD_HASH_STR,
2363325151a3SRui Paulo 			       os_strlen(P2PS_WILD_HASH_STR)) == 0)
2364325151a3SRui Paulo 			return 1; /* WFA service match */
23655b9c547cSRui Paulo 		adv_data = adv_data->next;
2366f05cddf9SRui Paulo 	}
2367f05cddf9SRui Paulo 
2368f05cddf9SRui Paulo 	return 0;
2369f05cddf9SRui Paulo }
2370f05cddf9SRui Paulo 
2371f05cddf9SRui Paulo 
2372f05cddf9SRui Paulo static enum p2p_probe_req_status
p2p_reply_probe(struct p2p_data * p2p,const u8 * addr,const u8 * dst,const u8 * bssid,const u8 * ie,size_t ie_len,unsigned int rx_freq)2373f05cddf9SRui Paulo p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2374325151a3SRui Paulo 		const u8 *bssid, const u8 *ie, size_t ie_len,
2375325151a3SRui Paulo 		unsigned int rx_freq)
2376f05cddf9SRui Paulo {
2377f05cddf9SRui Paulo 	struct ieee802_11_elems elems;
2378f05cddf9SRui Paulo 	struct wpabuf *buf;
2379f05cddf9SRui Paulo 	struct p2p_message msg;
2380f05cddf9SRui Paulo 	struct wpabuf *ies;
2381f05cddf9SRui Paulo 
2382f05cddf9SRui Paulo 	if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2383f05cddf9SRui Paulo 	    ParseFailed) {
2384f05cddf9SRui Paulo 		/* Ignore invalid Probe Request frames */
23855b9c547cSRui Paulo 		p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it");
2386f05cddf9SRui Paulo 		return P2P_PREQ_MALFORMED;
2387f05cddf9SRui Paulo 	}
2388f05cddf9SRui Paulo 
2389f05cddf9SRui Paulo 	if (elems.p2p == NULL) {
2390f05cddf9SRui Paulo 		/* not a P2P probe - ignore it */
23915b9c547cSRui Paulo 		p2p_dbg(p2p, "Not a P2P probe - ignore it");
2392f05cddf9SRui Paulo 		return P2P_PREQ_NOT_P2P;
2393f05cddf9SRui Paulo 	}
2394f05cddf9SRui Paulo 
2395f05cddf9SRui Paulo 	if (dst && !is_broadcast_ether_addr(dst) &&
2396*a90b9d01SCy Schubert 	    !ether_addr_equal(dst, p2p->cfg->dev_addr)) {
2397f05cddf9SRui Paulo 		/* Not sent to the broadcast address or our P2P Device Address
2398f05cddf9SRui Paulo 		 */
23995b9c547cSRui Paulo 		p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it",
24005b9c547cSRui Paulo 			MAC2STR(dst));
2401f05cddf9SRui Paulo 		return P2P_PREQ_NOT_PROCESSED;
2402f05cddf9SRui Paulo 	}
2403f05cddf9SRui Paulo 
2404f05cddf9SRui Paulo 	if (bssid && !is_broadcast_ether_addr(bssid)) {
2405f05cddf9SRui Paulo 		/* Not sent to the Wildcard BSSID */
24065b9c547cSRui Paulo 		p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it",
24075b9c547cSRui Paulo 			MAC2STR(bssid));
2408f05cddf9SRui Paulo 		return P2P_PREQ_NOT_PROCESSED;
2409f05cddf9SRui Paulo 	}
2410f05cddf9SRui Paulo 
2411f05cddf9SRui Paulo 	if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2412f05cddf9SRui Paulo 	    os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2413f05cddf9SRui Paulo 	    0) {
2414f05cddf9SRui Paulo 		/* not using P2P Wildcard SSID - ignore */
24155b9c547cSRui Paulo 		p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it");
2416f05cddf9SRui Paulo 		return P2P_PREQ_NOT_PROCESSED;
2417f05cddf9SRui Paulo 	}
2418f05cddf9SRui Paulo 
2419f05cddf9SRui Paulo 	if (supp_rates_11b_only(&elems)) {
2420f05cddf9SRui Paulo 		/* Indicates support for 11b rates only */
24215b9c547cSRui Paulo 		p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it");
2422f05cddf9SRui Paulo 		return P2P_PREQ_NOT_P2P;
2423f05cddf9SRui Paulo 	}
2424f05cddf9SRui Paulo 
2425f05cddf9SRui Paulo 	os_memset(&msg, 0, sizeof(msg));
2426f05cddf9SRui Paulo 	if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2427f05cddf9SRui Paulo 		/* Could not parse P2P attributes */
24285b9c547cSRui Paulo 		p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it");
2429f05cddf9SRui Paulo 		return P2P_PREQ_NOT_P2P;
2430f05cddf9SRui Paulo 	}
2431f05cddf9SRui Paulo 
24325b9c547cSRui Paulo 	if (msg.service_hash && msg.service_hash_count) {
24335b9c547cSRui Paulo 		const u8 *hash = msg.service_hash;
24345b9c547cSRui Paulo 		u8 i;
2435325151a3SRui Paulo 		int p2ps_svc_found = 0;
24365b9c547cSRui Paulo 
2437325151a3SRui Paulo 		p2p_dbg(p2p, "in_listen=%d drv_in_listen=%d when received P2PS Probe Request at %u MHz; own Listen channel %u, pending listen freq %u MHz",
2438325151a3SRui Paulo 			p2p->in_listen, p2p->drv_in_listen, rx_freq,
2439325151a3SRui Paulo 			p2p->cfg->channel, p2p->pending_listen_freq);
2440325151a3SRui Paulo 
2441325151a3SRui Paulo 		if (!p2p->in_listen && !p2p->drv_in_listen &&
2442325151a3SRui Paulo 		    p2p->pending_listen_freq && rx_freq &&
2443325151a3SRui Paulo 		    rx_freq != p2p->pending_listen_freq) {
2444325151a3SRui Paulo 			p2p_dbg(p2p, "Do not reply to Probe Request frame that was received on %u MHz while waiting to start Listen state on %u MHz",
2445325151a3SRui Paulo 				rx_freq, p2p->pending_listen_freq);
2446325151a3SRui Paulo 			p2p_parse_free(&msg);
2447325151a3SRui Paulo 			return P2P_PREQ_NOT_LISTEN;
2448325151a3SRui Paulo 		}
2449325151a3SRui Paulo 
24505b9c547cSRui Paulo 		for (i = 0; i < msg.service_hash_count; i++) {
24515b9c547cSRui Paulo 			if (p2p_service_find_asp(p2p, hash)) {
2452325151a3SRui Paulo 				p2p_dbg(p2p, "Service Hash match found: "
2453325151a3SRui Paulo 					MACSTR, MAC2STR(hash));
2454325151a3SRui Paulo 				p2ps_svc_found = 1;
24555b9c547cSRui Paulo 				break;
24565b9c547cSRui Paulo 			}
24575b9c547cSRui Paulo 			hash += P2PS_HASH_LEN;
24585b9c547cSRui Paulo 		}
24595b9c547cSRui Paulo 
24605b9c547cSRui Paulo 		/* Probed hash unknown */
2461325151a3SRui Paulo 		if (!p2ps_svc_found) {
2462325151a3SRui Paulo 			p2p_dbg(p2p, "No Service Hash match found");
24635b9c547cSRui Paulo 			p2p_parse_free(&msg);
24645b9c547cSRui Paulo 			return P2P_PREQ_NOT_PROCESSED;
24655b9c547cSRui Paulo 		}
24665b9c547cSRui Paulo 	} else {
24675b9c547cSRui Paulo 		/* This is not a P2PS Probe Request */
24685b9c547cSRui Paulo 		p2p_dbg(p2p, "No P2PS Hash in Probe Request");
24695b9c547cSRui Paulo 
24705b9c547cSRui Paulo 		if (!p2p->in_listen || !p2p->drv_in_listen) {
24715b9c547cSRui Paulo 			/* not in Listen state - ignore Probe Request */
24725b9c547cSRui Paulo 			p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request",
24735b9c547cSRui Paulo 				p2p->in_listen, p2p->drv_in_listen);
24745b9c547cSRui Paulo 			p2p_parse_free(&msg);
24755b9c547cSRui Paulo 			return P2P_PREQ_NOT_LISTEN;
24765b9c547cSRui Paulo 		}
24775b9c547cSRui Paulo 	}
24785b9c547cSRui Paulo 
2479f05cddf9SRui Paulo 	if (msg.device_id &&
2480*a90b9d01SCy Schubert 	    !ether_addr_equal(msg.device_id, p2p->cfg->dev_addr)) {
2481f05cddf9SRui Paulo 		/* Device ID did not match */
24825b9c547cSRui Paulo 		p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it",
24835b9c547cSRui Paulo 			MAC2STR(msg.device_id));
2484f05cddf9SRui Paulo 		p2p_parse_free(&msg);
2485f05cddf9SRui Paulo 		return P2P_PREQ_NOT_PROCESSED;
2486f05cddf9SRui Paulo 	}
2487f05cddf9SRui Paulo 
2488f05cddf9SRui Paulo 	/* Check Requested Device Type match */
2489f05cddf9SRui Paulo 	if (msg.wps_attributes &&
2490f05cddf9SRui Paulo 	    !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2491f05cddf9SRui Paulo 		/* No match with Requested Device Type */
24924bc52338SCy Schubert 		p2p_dbg(p2p, "Probe Req requested Device Type did not match - ignore it");
2493f05cddf9SRui Paulo 		p2p_parse_free(&msg);
2494f05cddf9SRui Paulo 		return P2P_PREQ_NOT_PROCESSED;
2495f05cddf9SRui Paulo 	}
2496f05cddf9SRui Paulo 
2497f05cddf9SRui Paulo 	if (!p2p->cfg->send_probe_resp) {
2498f05cddf9SRui Paulo 		/* Response generated elsewhere */
24995b9c547cSRui Paulo 		p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response");
2500325151a3SRui Paulo 		p2p_parse_free(&msg);
2501f05cddf9SRui Paulo 		return P2P_PREQ_NOT_PROCESSED;
2502f05cddf9SRui Paulo 	}
2503f05cddf9SRui Paulo 
25045b9c547cSRui Paulo 	p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
2505f05cddf9SRui Paulo 
2506f05cddf9SRui Paulo 	/*
2507f05cddf9SRui Paulo 	 * We do not really have a specific BSS that this frame is advertising,
2508f05cddf9SRui Paulo 	 * so build a frame that has some information in valid format. This is
2509f05cddf9SRui Paulo 	 * really only used for discovery purposes, not to learn exact BSS
2510f05cddf9SRui Paulo 	 * parameters.
2511f05cddf9SRui Paulo 	 */
2512325151a3SRui Paulo 	ies = p2p_build_probe_resp_ies(p2p, msg.service_hash,
2513325151a3SRui Paulo 				       msg.service_hash_count);
2514325151a3SRui Paulo 	p2p_parse_free(&msg);
2515f05cddf9SRui Paulo 	if (ies == NULL)
2516f05cddf9SRui Paulo 		return P2P_PREQ_NOT_PROCESSED;
2517f05cddf9SRui Paulo 
2518f05cddf9SRui Paulo 	buf = wpabuf_alloc(200 + wpabuf_len(ies));
2519f05cddf9SRui Paulo 	if (buf == NULL) {
2520f05cddf9SRui Paulo 		wpabuf_free(ies);
2521f05cddf9SRui Paulo 		return P2P_PREQ_NOT_PROCESSED;
2522f05cddf9SRui Paulo 	}
2523f05cddf9SRui Paulo 
2524780fb4a2SCy Schubert 	if (p2p_build_probe_resp_buf(p2p, buf, ies, addr, rx_freq)) {
2525325151a3SRui Paulo 		wpabuf_free(ies);
2526325151a3SRui Paulo 		wpabuf_free(buf);
2527325151a3SRui Paulo 		return P2P_PREQ_NOT_PROCESSED;
2528325151a3SRui Paulo 	}
2529325151a3SRui Paulo 
2530f05cddf9SRui Paulo 	wpabuf_free(ies);
2531f05cddf9SRui Paulo 
2532325151a3SRui Paulo 	p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf, rx_freq);
2533f05cddf9SRui Paulo 
2534f05cddf9SRui Paulo 	wpabuf_free(buf);
2535f05cddf9SRui Paulo 
2536325151a3SRui Paulo 	return P2P_PREQ_PROCESSED;
2537f05cddf9SRui Paulo }
2538f05cddf9SRui Paulo 
2539f05cddf9SRui Paulo 
2540f05cddf9SRui Paulo enum p2p_probe_req_status
p2p_probe_req_rx(struct p2p_data * p2p,const u8 * addr,const u8 * dst,const u8 * bssid,const u8 * ie,size_t ie_len,unsigned int rx_freq,int p2p_lo_started)2541f05cddf9SRui Paulo p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2542325151a3SRui Paulo 		 const u8 *bssid, const u8 *ie, size_t ie_len,
2543780fb4a2SCy Schubert 		 unsigned int rx_freq, int p2p_lo_started)
2544f05cddf9SRui Paulo {
2545f05cddf9SRui Paulo 	enum p2p_probe_req_status res;
2546f05cddf9SRui Paulo 
2547f05cddf9SRui Paulo 	p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2548f05cddf9SRui Paulo 
2549780fb4a2SCy Schubert 	if (p2p_lo_started) {
2550780fb4a2SCy Schubert 		p2p_dbg(p2p,
2551780fb4a2SCy Schubert 			"Probe Response is offloaded, do not reply Probe Request");
2552780fb4a2SCy Schubert 		return P2P_PREQ_PROCESSED;
2553780fb4a2SCy Schubert 	}
2554780fb4a2SCy Schubert 
2555325151a3SRui Paulo 	res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len, rx_freq);
2556325151a3SRui Paulo 	if (res != P2P_PREQ_PROCESSED && res != P2P_PREQ_NOT_PROCESSED)
2557325151a3SRui Paulo 		return res;
2558f05cddf9SRui Paulo 
2559325151a3SRui Paulo 	/*
2560325151a3SRui Paulo 	 * Activate a pending GO Negotiation/Invite flow if a received Probe
2561325151a3SRui Paulo 	 * Request frame is from an expected peer. Some devices may share the
2562325151a3SRui Paulo 	 * same address for P2P and non-P2P STA running simultaneously. The
2563325151a3SRui Paulo 	 * P2P_PREQ_PROCESSED and P2P_PREQ_NOT_PROCESSED p2p_reply_probe()
2564325151a3SRui Paulo 	 * return values verified above ensure we are handling a Probe Request
2565325151a3SRui Paulo 	 * frame from a P2P peer.
2566325151a3SRui Paulo 	 */
2567f05cddf9SRui Paulo 	if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2568f05cddf9SRui Paulo 	    p2p->go_neg_peer &&
2569*a90b9d01SCy Schubert 	    ether_addr_equal(addr, p2p->go_neg_peer->info.p2p_device_addr) &&
25705b9c547cSRui Paulo 	    !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
2571f05cddf9SRui Paulo 		/* Received a Probe Request from GO Negotiation peer */
25725b9c547cSRui Paulo 		p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
25735b9c547cSRui Paulo 		eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
2574f05cddf9SRui Paulo 		eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
2575325151a3SRui Paulo 		return res;
2576f05cddf9SRui Paulo 	}
2577f05cddf9SRui Paulo 
2578f05cddf9SRui Paulo 	if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2579f05cddf9SRui Paulo 	    p2p->invite_peer &&
25805b9c547cSRui Paulo 	    (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) &&
2581*a90b9d01SCy Schubert 	    ether_addr_equal(addr, p2p->invite_peer->info.p2p_device_addr)) {
2582f05cddf9SRui Paulo 		/* Received a Probe Request from Invite peer */
25835b9c547cSRui Paulo 		p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
25845b9c547cSRui Paulo 		eloop_cancel_timeout(p2p_invite_start, p2p, NULL);
2585f05cddf9SRui Paulo 		eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
2586325151a3SRui Paulo 		return res;
2587f05cddf9SRui Paulo 	}
2588f05cddf9SRui Paulo 
2589f05cddf9SRui Paulo 	return res;
2590f05cddf9SRui Paulo }
2591f05cddf9SRui Paulo 
2592f05cddf9SRui Paulo 
p2p_assoc_req_ie_wlan_ap(struct p2p_data * p2p,const u8 * bssid,u8 * buf,size_t len,struct wpabuf * p2p_ie)2593f05cddf9SRui Paulo static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2594f05cddf9SRui Paulo 				    u8 *buf, size_t len, struct wpabuf *p2p_ie)
2595f05cddf9SRui Paulo {
2596f05cddf9SRui Paulo 	struct wpabuf *tmp;
2597f05cddf9SRui Paulo 	u8 *lpos;
2598f05cddf9SRui Paulo 	size_t tmplen;
2599f05cddf9SRui Paulo 	int res;
2600f05cddf9SRui Paulo 	u8 group_capab;
2601325151a3SRui Paulo 	struct p2p_message msg;
2602f05cddf9SRui Paulo 
2603f05cddf9SRui Paulo 	if (p2p_ie == NULL)
2604f05cddf9SRui Paulo 		return 0; /* WLAN AP is not a P2P manager */
2605f05cddf9SRui Paulo 
2606325151a3SRui Paulo 	os_memset(&msg, 0, sizeof(msg));
2607325151a3SRui Paulo 	if (p2p_parse_p2p_ie(p2p_ie, &msg) < 0)
2608325151a3SRui Paulo 		return 0;
2609325151a3SRui Paulo 
2610325151a3SRui Paulo 	p2p_dbg(p2p, "BSS P2P manageability %s",
2611325151a3SRui Paulo 		msg.manageability ? "enabled" : "disabled");
2612325151a3SRui Paulo 
2613325151a3SRui Paulo 	if (!msg.manageability)
2614325151a3SRui Paulo 		return 0;
2615325151a3SRui Paulo 
2616f05cddf9SRui Paulo 	/*
2617f05cddf9SRui Paulo 	 * (Re)Association Request - P2P IE
2618f05cddf9SRui Paulo 	 * P2P Capability attribute (shall be present)
2619f05cddf9SRui Paulo 	 * P2P Interface attribute (present if concurrent device and
2620f05cddf9SRui Paulo 	 *	P2P Management is enabled)
2621f05cddf9SRui Paulo 	 */
2622f05cddf9SRui Paulo 	tmp = wpabuf_alloc(200);
2623f05cddf9SRui Paulo 	if (tmp == NULL)
2624f05cddf9SRui Paulo 		return -1;
2625f05cddf9SRui Paulo 
2626f05cddf9SRui Paulo 	lpos = p2p_buf_add_ie_hdr(tmp);
2627f05cddf9SRui Paulo 	group_capab = 0;
2628f05cddf9SRui Paulo 	if (p2p->num_groups > 0) {
2629f05cddf9SRui Paulo 		group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2630f05cddf9SRui Paulo 		if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2631f05cddf9SRui Paulo 		    (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2632f05cddf9SRui Paulo 		    p2p->cross_connect)
2633f05cddf9SRui Paulo 			group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2634f05cddf9SRui Paulo 	}
2635f05cddf9SRui Paulo 	p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2636f05cddf9SRui Paulo 	if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2637f05cddf9SRui Paulo 	    (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2638f05cddf9SRui Paulo 		p2p_buf_add_p2p_interface(tmp, p2p);
2639f05cddf9SRui Paulo 	p2p_buf_update_ie_hdr(tmp, lpos);
2640f05cddf9SRui Paulo 
2641f05cddf9SRui Paulo 	tmplen = wpabuf_len(tmp);
2642f05cddf9SRui Paulo 	if (tmplen > len)
2643f05cddf9SRui Paulo 		res = -1;
2644f05cddf9SRui Paulo 	else {
2645f05cddf9SRui Paulo 		os_memcpy(buf, wpabuf_head(tmp), tmplen);
2646f05cddf9SRui Paulo 		res = tmplen;
2647f05cddf9SRui Paulo 	}
2648f05cddf9SRui Paulo 	wpabuf_free(tmp);
2649f05cddf9SRui Paulo 
2650f05cddf9SRui Paulo 	return res;
2651f05cddf9SRui Paulo }
2652f05cddf9SRui Paulo 
2653f05cddf9SRui Paulo 
p2p_assoc_req_ie(struct p2p_data * p2p,const u8 * bssid,u8 * buf,size_t len,int p2p_group,struct wpabuf * p2p_ie)2654f05cddf9SRui Paulo int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2655f05cddf9SRui Paulo 		     size_t len, int p2p_group, struct wpabuf *p2p_ie)
2656f05cddf9SRui Paulo {
2657f05cddf9SRui Paulo 	struct wpabuf *tmp;
2658f05cddf9SRui Paulo 	u8 *lpos;
2659f05cddf9SRui Paulo 	struct p2p_device *peer;
2660f05cddf9SRui Paulo 	size_t tmplen;
2661f05cddf9SRui Paulo 	int res;
2662f05cddf9SRui Paulo 	size_t extra = 0;
2663f05cddf9SRui Paulo 
2664f05cddf9SRui Paulo 	if (!p2p_group)
2665f05cddf9SRui Paulo 		return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2666f05cddf9SRui Paulo 
2667f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
2668f05cddf9SRui Paulo 	if (p2p->wfd_ie_assoc_req)
2669f05cddf9SRui Paulo 		extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2670f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
2671f05cddf9SRui Paulo 
26725b9c547cSRui Paulo 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
26735b9c547cSRui Paulo 		extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
26745b9c547cSRui Paulo 
2675f05cddf9SRui Paulo 	/*
2676f05cddf9SRui Paulo 	 * (Re)Association Request - P2P IE
2677f05cddf9SRui Paulo 	 * P2P Capability attribute (shall be present)
2678f05cddf9SRui Paulo 	 * Extended Listen Timing (may be present)
2679f05cddf9SRui Paulo 	 * P2P Device Info attribute (shall be present)
2680f05cddf9SRui Paulo 	 */
2681f05cddf9SRui Paulo 	tmp = wpabuf_alloc(200 + extra);
2682f05cddf9SRui Paulo 	if (tmp == NULL)
2683f05cddf9SRui Paulo 		return -1;
2684f05cddf9SRui Paulo 
2685f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
2686f05cddf9SRui Paulo 	if (p2p->wfd_ie_assoc_req)
2687f05cddf9SRui Paulo 		wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2688f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
2689f05cddf9SRui Paulo 
26905b9c547cSRui Paulo 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
26915b9c547cSRui Paulo 		wpabuf_put_buf(tmp,
26925b9c547cSRui Paulo 			       p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
26935b9c547cSRui Paulo 
2694f05cddf9SRui Paulo 	peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2695f05cddf9SRui Paulo 
2696f05cddf9SRui Paulo 	lpos = p2p_buf_add_ie_hdr(tmp);
2697f05cddf9SRui Paulo 	p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2698f05cddf9SRui Paulo 	if (p2p->ext_listen_interval)
2699f05cddf9SRui Paulo 		p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2700f05cddf9SRui Paulo 					      p2p->ext_listen_interval);
2701f05cddf9SRui Paulo 	p2p_buf_add_device_info(tmp, p2p, peer);
2702f05cddf9SRui Paulo 	p2p_buf_update_ie_hdr(tmp, lpos);
2703f05cddf9SRui Paulo 
2704f05cddf9SRui Paulo 	tmplen = wpabuf_len(tmp);
2705f05cddf9SRui Paulo 	if (tmplen > len)
2706f05cddf9SRui Paulo 		res = -1;
2707f05cddf9SRui Paulo 	else {
2708f05cddf9SRui Paulo 		os_memcpy(buf, wpabuf_head(tmp), tmplen);
2709f05cddf9SRui Paulo 		res = tmplen;
2710f05cddf9SRui Paulo 	}
2711f05cddf9SRui Paulo 	wpabuf_free(tmp);
2712f05cddf9SRui Paulo 
2713f05cddf9SRui Paulo 	return res;
2714f05cddf9SRui Paulo }
2715f05cddf9SRui Paulo 
2716f05cddf9SRui Paulo 
p2p_scan_result_text(const u8 * ies,size_t ies_len,char * buf,char * end)2717f05cddf9SRui Paulo int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2718f05cddf9SRui Paulo {
2719f05cddf9SRui Paulo 	struct wpabuf *p2p_ie;
2720f05cddf9SRui Paulo 	int ret;
2721f05cddf9SRui Paulo 
2722f05cddf9SRui Paulo 	p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2723f05cddf9SRui Paulo 	if (p2p_ie == NULL)
2724f05cddf9SRui Paulo 		return 0;
2725f05cddf9SRui Paulo 
2726f05cddf9SRui Paulo 	ret = p2p_attr_text(p2p_ie, buf, end);
2727f05cddf9SRui Paulo 	wpabuf_free(p2p_ie);
2728f05cddf9SRui Paulo 	return ret;
2729f05cddf9SRui Paulo }
2730f05cddf9SRui Paulo 
2731f05cddf9SRui Paulo 
27325b9c547cSRui Paulo struct p2ps_advertisement *
p2p_service_p2ps_id(struct p2p_data * p2p,u32 adv_id)27335b9c547cSRui Paulo p2p_service_p2ps_id(struct p2p_data *p2p, u32 adv_id)
27345b9c547cSRui Paulo {
27355b9c547cSRui Paulo 	struct p2ps_advertisement *adv_data;
27365b9c547cSRui Paulo 
27375b9c547cSRui Paulo 	if (!p2p)
27385b9c547cSRui Paulo 		return NULL;
27395b9c547cSRui Paulo 
27405b9c547cSRui Paulo 	adv_data = p2p->p2ps_adv_list;
27415b9c547cSRui Paulo 	while (adv_data) {
27425b9c547cSRui Paulo 		if (adv_data->id == adv_id)
27435b9c547cSRui Paulo 			return adv_data;
27445b9c547cSRui Paulo 		adv_data = adv_data->next;
27455b9c547cSRui Paulo 	}
27465b9c547cSRui Paulo 
27475b9c547cSRui Paulo 	return NULL;
27485b9c547cSRui Paulo }
27495b9c547cSRui Paulo 
27505b9c547cSRui Paulo 
p2p_service_del_asp(struct p2p_data * p2p,u32 adv_id)27515b9c547cSRui Paulo int p2p_service_del_asp(struct p2p_data *p2p, u32 adv_id)
27525b9c547cSRui Paulo {
27535b9c547cSRui Paulo 	struct p2ps_advertisement *adv_data;
27545b9c547cSRui Paulo 	struct p2ps_advertisement **prior;
27555b9c547cSRui Paulo 
27565b9c547cSRui Paulo 	if (!p2p)
27575b9c547cSRui Paulo 		return -1;
27585b9c547cSRui Paulo 
27595b9c547cSRui Paulo 	adv_data = p2p->p2ps_adv_list;
27605b9c547cSRui Paulo 	prior = &p2p->p2ps_adv_list;
27615b9c547cSRui Paulo 	while (adv_data) {
27625b9c547cSRui Paulo 		if (adv_data->id == adv_id) {
27635b9c547cSRui Paulo 			p2p_dbg(p2p, "Delete ASP adv_id=0x%x", adv_id);
27645b9c547cSRui Paulo 			*prior = adv_data->next;
27655b9c547cSRui Paulo 			os_free(adv_data);
27665b9c547cSRui Paulo 			return 0;
27675b9c547cSRui Paulo 		}
27685b9c547cSRui Paulo 		prior = &adv_data->next;
27695b9c547cSRui Paulo 		adv_data = adv_data->next;
27705b9c547cSRui Paulo 	}
27715b9c547cSRui Paulo 
27725b9c547cSRui Paulo 	return -1;
27735b9c547cSRui Paulo }
27745b9c547cSRui Paulo 
27755b9c547cSRui Paulo 
p2p_service_add_asp(struct p2p_data * p2p,int auto_accept,u32 adv_id,const char * adv_str,u8 svc_state,u16 config_methods,const char * svc_info,const u8 * cpt_priority)27765b9c547cSRui Paulo int p2p_service_add_asp(struct p2p_data *p2p, int auto_accept, u32 adv_id,
27775b9c547cSRui Paulo 			const char *adv_str, u8 svc_state, u16 config_methods,
2778325151a3SRui Paulo 			const char *svc_info, const u8 *cpt_priority)
27795b9c547cSRui Paulo {
27805b9c547cSRui Paulo 	struct p2ps_advertisement *adv_data, *tmp, **prev;
27815b9c547cSRui Paulo 	u8 buf[P2PS_HASH_LEN];
27825b9c547cSRui Paulo 	size_t adv_data_len, adv_len, info_len = 0;
2783325151a3SRui Paulo 	int i;
27845b9c547cSRui Paulo 
2785325151a3SRui Paulo 	if (!p2p || !adv_str || !adv_str[0] || !cpt_priority)
27865b9c547cSRui Paulo 		return -1;
27875b9c547cSRui Paulo 
27885b9c547cSRui Paulo 	if (!(config_methods & p2p->cfg->config_methods)) {
27895b9c547cSRui Paulo 		p2p_dbg(p2p, "Config methods not supported svc: 0x%x dev: 0x%x",
27905b9c547cSRui Paulo 			config_methods, p2p->cfg->config_methods);
27915b9c547cSRui Paulo 		return -1;
27925b9c547cSRui Paulo 	}
27935b9c547cSRui Paulo 
27945b9c547cSRui Paulo 	if (!p2ps_gen_hash(p2p, adv_str, buf))
27955b9c547cSRui Paulo 		return -1;
27965b9c547cSRui Paulo 
27975b9c547cSRui Paulo 	if (svc_info)
27985b9c547cSRui Paulo 		info_len = os_strlen(svc_info);
27995b9c547cSRui Paulo 	adv_len = os_strlen(adv_str);
28005b9c547cSRui Paulo 	adv_data_len = sizeof(struct p2ps_advertisement) + adv_len + 1 +
28015b9c547cSRui Paulo 		info_len + 1;
28025b9c547cSRui Paulo 
28035b9c547cSRui Paulo 	adv_data = os_zalloc(adv_data_len);
28045b9c547cSRui Paulo 	if (!adv_data)
28055b9c547cSRui Paulo 		return -1;
28065b9c547cSRui Paulo 
28075b9c547cSRui Paulo 	os_memcpy(adv_data->hash, buf, P2PS_HASH_LEN);
28085b9c547cSRui Paulo 	adv_data->id = adv_id;
28095b9c547cSRui Paulo 	adv_data->state = svc_state;
28105b9c547cSRui Paulo 	adv_data->config_methods = config_methods & p2p->cfg->config_methods;
28115b9c547cSRui Paulo 	adv_data->auto_accept = (u8) auto_accept;
28125b9c547cSRui Paulo 	os_memcpy(adv_data->svc_name, adv_str, adv_len);
28135b9c547cSRui Paulo 
2814325151a3SRui Paulo 	for (i = 0; cpt_priority[i] && i < P2PS_FEATURE_CAPAB_CPT_MAX; i++) {
2815325151a3SRui Paulo 		adv_data->cpt_priority[i] = cpt_priority[i];
2816325151a3SRui Paulo 		adv_data->cpt_mask |= cpt_priority[i];
2817325151a3SRui Paulo 	}
2818325151a3SRui Paulo 
28195b9c547cSRui Paulo 	if (svc_info && info_len) {
28205b9c547cSRui Paulo 		adv_data->svc_info = &adv_data->svc_name[adv_len + 1];
28215b9c547cSRui Paulo 		os_memcpy(adv_data->svc_info, svc_info, info_len);
28225b9c547cSRui Paulo 	}
28235b9c547cSRui Paulo 
28245b9c547cSRui Paulo 	/*
28255b9c547cSRui Paulo 	 * Group Advertisements by service string. They do not need to be
28265b9c547cSRui Paulo 	 * sorted, but groups allow easier Probe Response instance grouping
28275b9c547cSRui Paulo 	 */
28285b9c547cSRui Paulo 	tmp = p2p->p2ps_adv_list;
28295b9c547cSRui Paulo 	prev = &p2p->p2ps_adv_list;
28305b9c547cSRui Paulo 	while (tmp) {
28315b9c547cSRui Paulo 		if (tmp->id == adv_data->id) {
28325b9c547cSRui Paulo 			if (os_strcmp(tmp->svc_name, adv_data->svc_name) != 0) {
28335b9c547cSRui Paulo 				os_free(adv_data);
28345b9c547cSRui Paulo 				return -1;
28355b9c547cSRui Paulo 			}
28365b9c547cSRui Paulo 			adv_data->next = tmp->next;
28375b9c547cSRui Paulo 			*prev = adv_data;
28385b9c547cSRui Paulo 			os_free(tmp);
28395b9c547cSRui Paulo 			goto inserted;
28405b9c547cSRui Paulo 		} else {
28415b9c547cSRui Paulo 			if (os_strcmp(tmp->svc_name, adv_data->svc_name) == 0) {
28425b9c547cSRui Paulo 				adv_data->next = tmp->next;
28435b9c547cSRui Paulo 				tmp->next = adv_data;
28445b9c547cSRui Paulo 				goto inserted;
28455b9c547cSRui Paulo 			}
28465b9c547cSRui Paulo 		}
28475b9c547cSRui Paulo 		prev = &tmp->next;
28485b9c547cSRui Paulo 		tmp = tmp->next;
28495b9c547cSRui Paulo 	}
28505b9c547cSRui Paulo 
28515b9c547cSRui Paulo 	/* No svc_name match found */
28525b9c547cSRui Paulo 	adv_data->next = p2p->p2ps_adv_list;
28535b9c547cSRui Paulo 	p2p->p2ps_adv_list = adv_data;
28545b9c547cSRui Paulo 
28555b9c547cSRui Paulo inserted:
28565b9c547cSRui Paulo 	p2p_dbg(p2p,
2857325151a3SRui Paulo 		"Added ASP advertisement adv_id=0x%x config_methods=0x%x svc_state=0x%x adv_str='%s' cpt_mask=0x%x",
2858325151a3SRui Paulo 		adv_id, adv_data->config_methods, svc_state, adv_str,
2859325151a3SRui Paulo 		adv_data->cpt_mask);
28605b9c547cSRui Paulo 
28615b9c547cSRui Paulo 	return 0;
28625b9c547cSRui Paulo }
28635b9c547cSRui Paulo 
28645b9c547cSRui Paulo 
p2p_service_flush_asp(struct p2p_data * p2p)2865325151a3SRui Paulo void p2p_service_flush_asp(struct p2p_data *p2p)
2866325151a3SRui Paulo {
2867325151a3SRui Paulo 	struct p2ps_advertisement *adv, *prev;
2868325151a3SRui Paulo 
2869325151a3SRui Paulo 	if (!p2p)
2870325151a3SRui Paulo 		return;
2871325151a3SRui Paulo 
2872325151a3SRui Paulo 	adv = p2p->p2ps_adv_list;
2873325151a3SRui Paulo 	while (adv) {
2874325151a3SRui Paulo 		prev = adv;
2875325151a3SRui Paulo 		adv = adv->next;
2876325151a3SRui Paulo 		os_free(prev);
2877325151a3SRui Paulo 	}
2878325151a3SRui Paulo 
2879325151a3SRui Paulo 	p2p->p2ps_adv_list = NULL;
288085732ac8SCy Schubert 	p2ps_prov_free(p2p);
2881325151a3SRui Paulo 	p2p_dbg(p2p, "All ASP advertisements flushed");
2882325151a3SRui Paulo }
2883325151a3SRui Paulo 
2884325151a3SRui Paulo 
p2p_parse_dev_addr_in_p2p_ie(struct wpabuf * p2p_ie,u8 * dev_addr)2885f05cddf9SRui Paulo int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2886f05cddf9SRui Paulo {
2887f05cddf9SRui Paulo 	struct p2p_message msg;
2888f05cddf9SRui Paulo 
2889f05cddf9SRui Paulo 	os_memset(&msg, 0, sizeof(msg));
2890f05cddf9SRui Paulo 	if (p2p_parse_p2p_ie(p2p_ie, &msg))
2891f05cddf9SRui Paulo 		return -1;
2892f05cddf9SRui Paulo 
2893f05cddf9SRui Paulo 	if (msg.p2p_device_addr) {
2894f05cddf9SRui Paulo 		os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2895f05cddf9SRui Paulo 		return 0;
2896f05cddf9SRui Paulo 	} else if (msg.device_id) {
2897f05cddf9SRui Paulo 		os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2898f05cddf9SRui Paulo 		return 0;
2899f05cddf9SRui Paulo 	}
2900f05cddf9SRui Paulo 	return -1;
2901f05cddf9SRui Paulo }
2902f05cddf9SRui Paulo 
2903f05cddf9SRui Paulo 
p2p_parse_dev_addr(const u8 * ies,size_t ies_len,u8 * dev_addr)2904f05cddf9SRui Paulo int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2905f05cddf9SRui Paulo {
2906f05cddf9SRui Paulo 	struct wpabuf *p2p_ie;
2907f05cddf9SRui Paulo 	int ret;
2908f05cddf9SRui Paulo 
2909f05cddf9SRui Paulo 	p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2910f05cddf9SRui Paulo 					     P2P_IE_VENDOR_TYPE);
2911f05cddf9SRui Paulo 	if (p2p_ie == NULL)
2912f05cddf9SRui Paulo 		return -1;
2913f05cddf9SRui Paulo 	ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
2914f05cddf9SRui Paulo 	wpabuf_free(p2p_ie);
2915f05cddf9SRui Paulo 	return ret;
2916f05cddf9SRui Paulo }
2917f05cddf9SRui Paulo 
2918f05cddf9SRui Paulo 
p2p_clear_go_neg(struct p2p_data * p2p)2919f05cddf9SRui Paulo static void p2p_clear_go_neg(struct p2p_data *p2p)
2920f05cddf9SRui Paulo {
2921f05cddf9SRui Paulo 	p2p->go_neg_peer = NULL;
2922f05cddf9SRui Paulo 	p2p_clear_timeout(p2p);
2923f05cddf9SRui Paulo 	p2p_set_state(p2p, P2P_IDLE);
2924f05cddf9SRui Paulo }
2925f05cddf9SRui Paulo 
2926f05cddf9SRui Paulo 
p2p_wps_success_cb(struct p2p_data * p2p,const u8 * mac_addr)2927f05cddf9SRui Paulo void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2928f05cddf9SRui Paulo {
2929f05cddf9SRui Paulo 	if (p2p->go_neg_peer == NULL) {
29305b9c547cSRui Paulo 		p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
2931f05cddf9SRui Paulo 		return; /* No pending Group Formation */
2932f05cddf9SRui Paulo 	}
2933f05cddf9SRui Paulo 
2934*a90b9d01SCy Schubert 	if (!ether_addr_equal(mac_addr, p2p->go_neg_peer->intended_addr)) {
29355b9c547cSRui Paulo 		p2p_dbg(p2p, "Ignore WPS registration success notification for "
29365b9c547cSRui Paulo 			MACSTR " (GO Negotiation peer " MACSTR ")",
2937f05cddf9SRui Paulo 			MAC2STR(mac_addr),
2938f05cddf9SRui Paulo 			MAC2STR(p2p->go_neg_peer->intended_addr));
2939f05cddf9SRui Paulo 		return; /* Ignore unexpected peer address */
2940f05cddf9SRui Paulo 	}
2941f05cddf9SRui Paulo 
29425b9c547cSRui Paulo 	p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
2943f05cddf9SRui Paulo 		MAC2STR(mac_addr));
2944f05cddf9SRui Paulo 
2945f05cddf9SRui Paulo 	p2p_clear_go_neg(p2p);
2946f05cddf9SRui Paulo }
2947f05cddf9SRui Paulo 
2948f05cddf9SRui Paulo 
p2p_group_formation_failed(struct p2p_data * p2p)2949f05cddf9SRui Paulo void p2p_group_formation_failed(struct p2p_data *p2p)
2950f05cddf9SRui Paulo {
2951f05cddf9SRui Paulo 	if (p2p->go_neg_peer == NULL) {
29525b9c547cSRui Paulo 		p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
2953f05cddf9SRui Paulo 		return; /* No pending Group Formation */
2954f05cddf9SRui Paulo 	}
2955f05cddf9SRui Paulo 
29565b9c547cSRui Paulo 	p2p_dbg(p2p, "Group Formation failed with " MACSTR,
2957f05cddf9SRui Paulo 		MAC2STR(p2p->go_neg_peer->intended_addr));
2958f05cddf9SRui Paulo 
2959f05cddf9SRui Paulo 	p2p_clear_go_neg(p2p);
2960f05cddf9SRui Paulo }
2961f05cddf9SRui Paulo 
2962f05cddf9SRui Paulo 
is_p2p_6ghz_disabled(struct p2p_data * p2p)2963c1d255d3SCy Schubert bool is_p2p_6ghz_disabled(struct p2p_data *p2p)
2964c1d255d3SCy Schubert {
2965c1d255d3SCy Schubert 	if (p2p)
2966c1d255d3SCy Schubert 		return p2p->cfg->p2p_6ghz_disable;
2967c1d255d3SCy Schubert 	return false;
2968c1d255d3SCy Schubert }
2969c1d255d3SCy Schubert 
2970c1d255d3SCy Schubert 
p2p_init(const struct p2p_config * cfg)2971f05cddf9SRui Paulo struct p2p_data * p2p_init(const struct p2p_config *cfg)
2972f05cddf9SRui Paulo {
2973f05cddf9SRui Paulo 	struct p2p_data *p2p;
2974f05cddf9SRui Paulo 
29755b9c547cSRui Paulo 	if (cfg->max_peers < 1 ||
29765b9c547cSRui Paulo 	    cfg->passphrase_len < 8 || cfg->passphrase_len > 63)
2977f05cddf9SRui Paulo 		return NULL;
2978f05cddf9SRui Paulo 
2979f05cddf9SRui Paulo 	p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2980f05cddf9SRui Paulo 	if (p2p == NULL)
2981f05cddf9SRui Paulo 		return NULL;
2982f05cddf9SRui Paulo 	p2p->cfg = (struct p2p_config *) (p2p + 1);
2983f05cddf9SRui Paulo 	os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2984f05cddf9SRui Paulo 	if (cfg->dev_name)
2985f05cddf9SRui Paulo 		p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2986f05cddf9SRui Paulo 	if (cfg->manufacturer)
2987f05cddf9SRui Paulo 		p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2988f05cddf9SRui Paulo 	if (cfg->model_name)
2989f05cddf9SRui Paulo 		p2p->cfg->model_name = os_strdup(cfg->model_name);
2990f05cddf9SRui Paulo 	if (cfg->model_number)
2991f05cddf9SRui Paulo 		p2p->cfg->model_number = os_strdup(cfg->model_number);
2992f05cddf9SRui Paulo 	if (cfg->serial_number)
2993f05cddf9SRui Paulo 		p2p->cfg->serial_number = os_strdup(cfg->serial_number);
2994f05cddf9SRui Paulo 	if (cfg->pref_chan) {
2995f05cddf9SRui Paulo 		p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2996f05cddf9SRui Paulo 						sizeof(struct p2p_channel));
2997f05cddf9SRui Paulo 		if (p2p->cfg->pref_chan) {
2998f05cddf9SRui Paulo 			os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2999f05cddf9SRui Paulo 				  cfg->num_pref_chan *
3000f05cddf9SRui Paulo 				  sizeof(struct p2p_channel));
3001f05cddf9SRui Paulo 		} else
3002f05cddf9SRui Paulo 			p2p->cfg->num_pref_chan = 0;
3003f05cddf9SRui Paulo 	}
3004f05cddf9SRui Paulo 
30055b9c547cSRui Paulo 	p2ps_gen_hash(p2p, P2PS_WILD_HASH_STR, p2p->wild_card_hash);
30065b9c547cSRui Paulo 
3007f05cddf9SRui Paulo 	p2p->min_disc_int = 1;
3008f05cddf9SRui Paulo 	p2p->max_disc_int = 3;
3009f05cddf9SRui Paulo 	p2p->max_disc_tu = -1;
3010f05cddf9SRui Paulo 
30115b9c547cSRui Paulo 	if (os_get_random(&p2p->next_tie_breaker, 1) < 0)
30125b9c547cSRui Paulo 		p2p->next_tie_breaker = 0;
3013f05cddf9SRui Paulo 	p2p->next_tie_breaker &= 0x01;
3014f05cddf9SRui Paulo 	if (cfg->sd_request)
3015f05cddf9SRui Paulo 		p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
3016f05cddf9SRui Paulo 	p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
3017f05cddf9SRui Paulo 	if (cfg->concurrent_operations)
3018f05cddf9SRui Paulo 		p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
3019f05cddf9SRui Paulo 	p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3020f05cddf9SRui Paulo 
3021f05cddf9SRui Paulo 	dl_list_init(&p2p->devices);
3022f05cddf9SRui Paulo 
3023f05cddf9SRui Paulo 	p2p->go_timeout = 100;
3024f05cddf9SRui Paulo 	p2p->client_timeout = 20;
30255b9c547cSRui Paulo 	p2p->num_p2p_sd_queries = 0;
30265b9c547cSRui Paulo 
30275b9c547cSRui Paulo 	p2p_dbg(p2p, "initialized");
30285b9c547cSRui Paulo 	p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
30295b9c547cSRui Paulo 	p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
3030f05cddf9SRui Paulo 
3031f05cddf9SRui Paulo 	return p2p;
3032f05cddf9SRui Paulo }
3033f05cddf9SRui Paulo 
3034f05cddf9SRui Paulo 
p2p_deinit(struct p2p_data * p2p)3035f05cddf9SRui Paulo void p2p_deinit(struct p2p_data *p2p)
3036f05cddf9SRui Paulo {
3037f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
3038f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_beacon);
3039f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_probe_req);
3040f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_probe_resp);
3041f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_assoc_req);
3042f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_invitation);
3043f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_prov_disc_req);
3044f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_prov_disc_resp);
3045f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_go_neg);
3046f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_dev_info);
3047f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_assoc_bssid);
3048f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_coupled_sink_info);
304985732ac8SCy Schubert 	wpabuf_free(p2p->wfd_r2_dev_info);
3050f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
3051f05cddf9SRui Paulo 
3052f05cddf9SRui Paulo 	eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
30535b9c547cSRui Paulo 	eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
30545b9c547cSRui Paulo 	eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
3055f05cddf9SRui Paulo 	p2p_flush(p2p);
3056f05cddf9SRui Paulo 	p2p_free_req_dev_types(p2p);
3057f05cddf9SRui Paulo 	os_free(p2p->cfg->dev_name);
3058f05cddf9SRui Paulo 	os_free(p2p->cfg->manufacturer);
3059f05cddf9SRui Paulo 	os_free(p2p->cfg->model_name);
3060f05cddf9SRui Paulo 	os_free(p2p->cfg->model_number);
3061f05cddf9SRui Paulo 	os_free(p2p->cfg->serial_number);
3062f05cddf9SRui Paulo 	os_free(p2p->cfg->pref_chan);
3063f05cddf9SRui Paulo 	os_free(p2p->groups);
3064325151a3SRui Paulo 	p2ps_prov_free(p2p);
3065f05cddf9SRui Paulo 	wpabuf_free(p2p->sd_resp);
3066f05cddf9SRui Paulo 	p2p_remove_wps_vendor_extensions(p2p);
30675b9c547cSRui Paulo 	os_free(p2p->no_go_freq.range);
3068325151a3SRui Paulo 	p2p_service_flush_asp(p2p);
30695b9c547cSRui Paulo 
3070f05cddf9SRui Paulo 	os_free(p2p);
3071f05cddf9SRui Paulo }
3072f05cddf9SRui Paulo 
3073f05cddf9SRui Paulo 
p2p_flush(struct p2p_data * p2p)3074f05cddf9SRui Paulo void p2p_flush(struct p2p_data *p2p)
3075f05cddf9SRui Paulo {
3076f05cddf9SRui Paulo 	struct p2p_device *dev, *prev;
3077780fb4a2SCy Schubert 
3078780fb4a2SCy Schubert 	p2p_ext_listen(p2p, 0, 0);
3079f05cddf9SRui Paulo 	p2p_stop_find(p2p);
3080f05cddf9SRui Paulo 	dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
3081f05cddf9SRui Paulo 			      list) {
3082f05cddf9SRui Paulo 		dl_list_del(&dev->list);
3083f05cddf9SRui Paulo 		p2p_device_free(p2p, dev);
3084f05cddf9SRui Paulo 	}
3085f05cddf9SRui Paulo 	p2p_free_sd_queries(p2p);
3086325151a3SRui Paulo 	p2p->ssid_set = 0;
308785732ac8SCy Schubert 	p2ps_prov_free(p2p);
308885732ac8SCy Schubert 	p2p_reset_pending_pd(p2p);
308985732ac8SCy Schubert 	p2p->override_pref_op_class = 0;
309085732ac8SCy Schubert 	p2p->override_pref_channel = 0;
3091f05cddf9SRui Paulo }
3092f05cddf9SRui Paulo 
3093f05cddf9SRui Paulo 
p2p_unauthorize(struct p2p_data * p2p,const u8 * addr)3094f05cddf9SRui Paulo int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
3095f05cddf9SRui Paulo {
3096f05cddf9SRui Paulo 	struct p2p_device *dev;
3097f05cddf9SRui Paulo 
3098f05cddf9SRui Paulo 	dev = p2p_get_device(p2p, addr);
3099f05cddf9SRui Paulo 	if (dev == NULL)
3100f05cddf9SRui Paulo 		return -1;
3101f05cddf9SRui Paulo 
31025b9c547cSRui Paulo 	p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
3103f05cddf9SRui Paulo 
31045b9c547cSRui Paulo 	if (p2p->go_neg_peer == dev) {
31055b9c547cSRui Paulo 		eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
3106f05cddf9SRui Paulo 		p2p->go_neg_peer = NULL;
31075b9c547cSRui Paulo 	}
3108f05cddf9SRui Paulo 
3109f05cddf9SRui Paulo 	dev->wps_method = WPS_NOT_READY;
31105b9c547cSRui Paulo 	dev->oob_pw_id = 0;
3111f05cddf9SRui Paulo 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
3112f05cddf9SRui Paulo 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
3113f05cddf9SRui Paulo 
3114f05cddf9SRui Paulo 	return 0;
3115f05cddf9SRui Paulo }
3116f05cddf9SRui Paulo 
3117f05cddf9SRui Paulo 
p2p_set_dev_name(struct p2p_data * p2p,const char * dev_name)3118f05cddf9SRui Paulo int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
3119f05cddf9SRui Paulo {
3120f05cddf9SRui Paulo 	os_free(p2p->cfg->dev_name);
3121f05cddf9SRui Paulo 	if (dev_name) {
3122f05cddf9SRui Paulo 		p2p->cfg->dev_name = os_strdup(dev_name);
3123f05cddf9SRui Paulo 		if (p2p->cfg->dev_name == NULL)
3124f05cddf9SRui Paulo 			return -1;
3125f05cddf9SRui Paulo 	} else
3126f05cddf9SRui Paulo 		p2p->cfg->dev_name = NULL;
3127f05cddf9SRui Paulo 	return 0;
3128f05cddf9SRui Paulo }
3129f05cddf9SRui Paulo 
3130f05cddf9SRui Paulo 
p2p_set_manufacturer(struct p2p_data * p2p,const char * manufacturer)3131f05cddf9SRui Paulo int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
3132f05cddf9SRui Paulo {
3133f05cddf9SRui Paulo 	os_free(p2p->cfg->manufacturer);
3134f05cddf9SRui Paulo 	p2p->cfg->manufacturer = NULL;
3135f05cddf9SRui Paulo 	if (manufacturer) {
3136f05cddf9SRui Paulo 		p2p->cfg->manufacturer = os_strdup(manufacturer);
3137f05cddf9SRui Paulo 		if (p2p->cfg->manufacturer == NULL)
3138f05cddf9SRui Paulo 			return -1;
3139f05cddf9SRui Paulo 	}
3140f05cddf9SRui Paulo 
3141f05cddf9SRui Paulo 	return 0;
3142f05cddf9SRui Paulo }
3143f05cddf9SRui Paulo 
3144f05cddf9SRui Paulo 
p2p_set_model_name(struct p2p_data * p2p,const char * model_name)3145f05cddf9SRui Paulo int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
3146f05cddf9SRui Paulo {
3147f05cddf9SRui Paulo 	os_free(p2p->cfg->model_name);
3148f05cddf9SRui Paulo 	p2p->cfg->model_name = NULL;
3149f05cddf9SRui Paulo 	if (model_name) {
3150f05cddf9SRui Paulo 		p2p->cfg->model_name = os_strdup(model_name);
3151f05cddf9SRui Paulo 		if (p2p->cfg->model_name == NULL)
3152f05cddf9SRui Paulo 			return -1;
3153f05cddf9SRui Paulo 	}
3154f05cddf9SRui Paulo 
3155f05cddf9SRui Paulo 	return 0;
3156f05cddf9SRui Paulo }
3157f05cddf9SRui Paulo 
3158f05cddf9SRui Paulo 
p2p_set_model_number(struct p2p_data * p2p,const char * model_number)3159f05cddf9SRui Paulo int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
3160f05cddf9SRui Paulo {
3161f05cddf9SRui Paulo 	os_free(p2p->cfg->model_number);
3162f05cddf9SRui Paulo 	p2p->cfg->model_number = NULL;
3163f05cddf9SRui Paulo 	if (model_number) {
3164f05cddf9SRui Paulo 		p2p->cfg->model_number = os_strdup(model_number);
3165f05cddf9SRui Paulo 		if (p2p->cfg->model_number == NULL)
3166f05cddf9SRui Paulo 			return -1;
3167f05cddf9SRui Paulo 	}
3168f05cddf9SRui Paulo 
3169f05cddf9SRui Paulo 	return 0;
3170f05cddf9SRui Paulo }
3171f05cddf9SRui Paulo 
3172f05cddf9SRui Paulo 
p2p_set_serial_number(struct p2p_data * p2p,const char * serial_number)3173f05cddf9SRui Paulo int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
3174f05cddf9SRui Paulo {
3175f05cddf9SRui Paulo 	os_free(p2p->cfg->serial_number);
3176f05cddf9SRui Paulo 	p2p->cfg->serial_number = NULL;
3177f05cddf9SRui Paulo 	if (serial_number) {
3178f05cddf9SRui Paulo 		p2p->cfg->serial_number = os_strdup(serial_number);
3179f05cddf9SRui Paulo 		if (p2p->cfg->serial_number == NULL)
3180f05cddf9SRui Paulo 			return -1;
3181f05cddf9SRui Paulo 	}
3182f05cddf9SRui Paulo 
3183f05cddf9SRui Paulo 	return 0;
3184f05cddf9SRui Paulo }
3185f05cddf9SRui Paulo 
3186f05cddf9SRui Paulo 
p2p_set_config_methods(struct p2p_data * p2p,u16 config_methods)3187f05cddf9SRui Paulo void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
3188f05cddf9SRui Paulo {
3189f05cddf9SRui Paulo 	p2p->cfg->config_methods = config_methods;
3190f05cddf9SRui Paulo }
3191f05cddf9SRui Paulo 
3192f05cddf9SRui Paulo 
p2p_set_uuid(struct p2p_data * p2p,const u8 * uuid)3193f05cddf9SRui Paulo void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
3194f05cddf9SRui Paulo {
3195f05cddf9SRui Paulo 	os_memcpy(p2p->cfg->uuid, uuid, 16);
3196f05cddf9SRui Paulo }
3197f05cddf9SRui Paulo 
3198f05cddf9SRui Paulo 
p2p_set_pri_dev_type(struct p2p_data * p2p,const u8 * pri_dev_type)3199f05cddf9SRui Paulo int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
3200f05cddf9SRui Paulo {
3201f05cddf9SRui Paulo 	os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
3202f05cddf9SRui Paulo 	return 0;
3203f05cddf9SRui Paulo }
3204f05cddf9SRui Paulo 
3205f05cddf9SRui Paulo 
p2p_set_sec_dev_types(struct p2p_data * p2p,const u8 dev_types[][8],size_t num_dev_types)3206f05cddf9SRui Paulo int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
3207f05cddf9SRui Paulo 			  size_t num_dev_types)
3208f05cddf9SRui Paulo {
3209f05cddf9SRui Paulo 	if (num_dev_types > P2P_SEC_DEVICE_TYPES)
3210f05cddf9SRui Paulo 		num_dev_types = P2P_SEC_DEVICE_TYPES;
3211f05cddf9SRui Paulo 	p2p->cfg->num_sec_dev_types = num_dev_types;
3212f05cddf9SRui Paulo 	os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
3213f05cddf9SRui Paulo 	return 0;
3214f05cddf9SRui Paulo }
3215f05cddf9SRui Paulo 
3216f05cddf9SRui Paulo 
p2p_remove_wps_vendor_extensions(struct p2p_data * p2p)3217f05cddf9SRui Paulo void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
3218f05cddf9SRui Paulo {
3219f05cddf9SRui Paulo 	int i;
3220f05cddf9SRui Paulo 
3221f05cddf9SRui Paulo 	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3222f05cddf9SRui Paulo 		wpabuf_free(p2p->wps_vendor_ext[i]);
3223f05cddf9SRui Paulo 		p2p->wps_vendor_ext[i] = NULL;
3224f05cddf9SRui Paulo 	}
3225f05cddf9SRui Paulo }
3226f05cddf9SRui Paulo 
3227f05cddf9SRui Paulo 
p2p_add_wps_vendor_extension(struct p2p_data * p2p,const struct wpabuf * vendor_ext)3228f05cddf9SRui Paulo int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
3229f05cddf9SRui Paulo 				 const struct wpabuf *vendor_ext)
3230f05cddf9SRui Paulo {
3231f05cddf9SRui Paulo 	int i;
3232f05cddf9SRui Paulo 
3233f05cddf9SRui Paulo 	if (vendor_ext == NULL)
3234f05cddf9SRui Paulo 		return -1;
3235f05cddf9SRui Paulo 
3236f05cddf9SRui Paulo 	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3237f05cddf9SRui Paulo 		if (p2p->wps_vendor_ext[i] == NULL)
3238f05cddf9SRui Paulo 			break;
3239f05cddf9SRui Paulo 	}
3240f05cddf9SRui Paulo 	if (i >= P2P_MAX_WPS_VENDOR_EXT)
3241f05cddf9SRui Paulo 		return -1;
3242f05cddf9SRui Paulo 
3243f05cddf9SRui Paulo 	p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
3244f05cddf9SRui Paulo 	if (p2p->wps_vendor_ext[i] == NULL)
3245f05cddf9SRui Paulo 		return -1;
3246f05cddf9SRui Paulo 
3247f05cddf9SRui Paulo 	return 0;
3248f05cddf9SRui Paulo }
3249f05cddf9SRui Paulo 
3250f05cddf9SRui Paulo 
p2p_set_country(struct p2p_data * p2p,const char * country)3251f05cddf9SRui Paulo int p2p_set_country(struct p2p_data *p2p, const char *country)
3252f05cddf9SRui Paulo {
3253f05cddf9SRui Paulo 	os_memcpy(p2p->cfg->country, country, 3);
3254f05cddf9SRui Paulo 	return 0;
3255f05cddf9SRui Paulo }
3256f05cddf9SRui Paulo 
3257f05cddf9SRui Paulo 
p2p_pre_find_operation(struct p2p_data * p2p,struct p2p_device * dev)32585b9c547cSRui Paulo static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev)
3259f05cddf9SRui Paulo {
3260780fb4a2SCy Schubert 	int res;
3261780fb4a2SCy Schubert 
32625b9c547cSRui Paulo 	if (dev->sd_pending_bcast_queries == 0) {
32635b9c547cSRui Paulo 		/* Initialize with total number of registered broadcast
32645b9c547cSRui Paulo 		 * SD queries. */
32655b9c547cSRui Paulo 		dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries;
32665b9c547cSRui Paulo 	}
32675b9c547cSRui Paulo 
3268780fb4a2SCy Schubert 	res = p2p_start_sd(p2p, dev);
3269780fb4a2SCy Schubert 	if (res == -2)
3270780fb4a2SCy Schubert 		return -2;
3271780fb4a2SCy Schubert 	if (res == 0)
32725b9c547cSRui Paulo 		return 1;
32735b9c547cSRui Paulo 
32745b9c547cSRui Paulo 	if (dev->req_config_methods &&
3275f05cddf9SRui Paulo 	    !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
32765b9c547cSRui Paulo 		p2p_dbg(p2p, "Send pending Provision Discovery Request to "
3277f05cddf9SRui Paulo 			MACSTR " (config methods 0x%x)",
3278f05cddf9SRui Paulo 			MAC2STR(dev->info.p2p_device_addr),
3279f05cddf9SRui Paulo 			dev->req_config_methods);
3280f05cddf9SRui Paulo 		if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
32815b9c547cSRui Paulo 			return 1;
32825b9c547cSRui Paulo 	}
32835b9c547cSRui Paulo 
32845b9c547cSRui Paulo 	return 0;
32855b9c547cSRui Paulo }
32865b9c547cSRui Paulo 
32875b9c547cSRui Paulo 
p2p_continue_find(struct p2p_data * p2p)32885b9c547cSRui Paulo void p2p_continue_find(struct p2p_data *p2p)
32895b9c547cSRui Paulo {
32905b9c547cSRui Paulo 	struct p2p_device *dev;
3291780fb4a2SCy Schubert 	int found, res;
32925b9c547cSRui Paulo 
32935b9c547cSRui Paulo 	p2p_set_state(p2p, P2P_SEARCH);
32945b9c547cSRui Paulo 
32955b9c547cSRui Paulo 	/* Continue from the device following the last iteration */
32965b9c547cSRui Paulo 	found = 0;
32975b9c547cSRui Paulo 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
32985b9c547cSRui Paulo 		if (dev == p2p->last_p2p_find_oper) {
32995b9c547cSRui Paulo 			found = 1;
33005b9c547cSRui Paulo 			continue;
33015b9c547cSRui Paulo 		}
33025b9c547cSRui Paulo 		if (!found)
33035b9c547cSRui Paulo 			continue;
3304780fb4a2SCy Schubert 		res = p2p_pre_find_operation(p2p, dev);
3305780fb4a2SCy Schubert 		if (res > 0) {
33065b9c547cSRui Paulo 			p2p->last_p2p_find_oper = dev;
3307f05cddf9SRui Paulo 			return;
3308f05cddf9SRui Paulo 		}
3309780fb4a2SCy Schubert 		if (res == -2)
3310780fb4a2SCy Schubert 			goto skip_sd;
3311f05cddf9SRui Paulo 	}
3312f05cddf9SRui Paulo 
33135b9c547cSRui Paulo 	/*
33145b9c547cSRui Paulo 	 * Wrap around to the beginning of the list and continue until the last
33155b9c547cSRui Paulo 	 * iteration device.
33165b9c547cSRui Paulo 	 */
33175b9c547cSRui Paulo 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3318780fb4a2SCy Schubert 		res = p2p_pre_find_operation(p2p, dev);
3319780fb4a2SCy Schubert 		if (res > 0) {
33205b9c547cSRui Paulo 			p2p->last_p2p_find_oper = dev;
33215b9c547cSRui Paulo 			return;
33225b9c547cSRui Paulo 		}
3323780fb4a2SCy Schubert 		if (res == -2)
3324780fb4a2SCy Schubert 			goto skip_sd;
33255b9c547cSRui Paulo 		if (dev == p2p->last_p2p_find_oper)
33265b9c547cSRui Paulo 			break;
33275b9c547cSRui Paulo 	}
33285b9c547cSRui Paulo 
3329780fb4a2SCy Schubert skip_sd:
3330780fb4a2SCy Schubert 	os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
3331f05cddf9SRui Paulo 	p2p_listen_in_find(p2p, 1);
3332f05cddf9SRui Paulo }
3333f05cddf9SRui Paulo 
3334f05cddf9SRui Paulo 
p2p_sd_query_cb(struct p2p_data * p2p,int success)3335*a90b9d01SCy Schubert void p2p_sd_query_cb(struct p2p_data *p2p, int success)
3336f05cddf9SRui Paulo {
33375b9c547cSRui Paulo 	p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
3338f05cddf9SRui Paulo 		success);
3339f05cddf9SRui Paulo 	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3340f05cddf9SRui Paulo 
3341f05cddf9SRui Paulo 	if (!success) {
3342780fb4a2SCy Schubert 		if (p2p->sd_peer) {
3343780fb4a2SCy Schubert 			if (is_zero_ether_addr(p2p->sd_query_no_ack)) {
3344780fb4a2SCy Schubert 				os_memcpy(p2p->sd_query_no_ack,
3345780fb4a2SCy Schubert 					  p2p->sd_peer->info.p2p_device_addr,
3346780fb4a2SCy Schubert 					  ETH_ALEN);
3347780fb4a2SCy Schubert 				p2p_dbg(p2p,
3348780fb4a2SCy Schubert 					"First SD Query no-ACK in this search iteration: "
3349780fb4a2SCy Schubert 					MACSTR, MAC2STR(p2p->sd_query_no_ack));
3350780fb4a2SCy Schubert 			}
33515b9c547cSRui Paulo 			p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3352780fb4a2SCy Schubert 		}
3353f05cddf9SRui Paulo 		p2p->sd_peer = NULL;
33545b9c547cSRui Paulo 		if (p2p->state != P2P_IDLE)
3355f05cddf9SRui Paulo 			p2p_continue_find(p2p);
3356f05cddf9SRui Paulo 		return;
3357f05cddf9SRui Paulo 	}
3358f05cddf9SRui Paulo 
3359f05cddf9SRui Paulo 	if (p2p->sd_peer == NULL) {
33605b9c547cSRui Paulo 		p2p_dbg(p2p, "No SD peer entry known");
33615b9c547cSRui Paulo 		if (p2p->state != P2P_IDLE)
3362f05cddf9SRui Paulo 			p2p_continue_find(p2p);
3363f05cddf9SRui Paulo 		return;
3364f05cddf9SRui Paulo 	}
3365f05cddf9SRui Paulo 
33665b9c547cSRui Paulo 	if (p2p->sd_query && p2p->sd_query->for_all_peers) {
33675b9c547cSRui Paulo 		/* Update the pending broadcast SD query count for this device
33685b9c547cSRui Paulo 		 */
33695b9c547cSRui Paulo 		p2p->sd_peer->sd_pending_bcast_queries--;
33705b9c547cSRui Paulo 
33715b9c547cSRui Paulo 		/*
33725b9c547cSRui Paulo 		 * If there are no pending broadcast queries for this device,
33735b9c547cSRui Paulo 		 * mark it as done (-1).
33745b9c547cSRui Paulo 		 */
33755b9c547cSRui Paulo 		if (p2p->sd_peer->sd_pending_bcast_queries == 0)
33765b9c547cSRui Paulo 			p2p->sd_peer->sd_pending_bcast_queries = -1;
33775b9c547cSRui Paulo 	}
33785b9c547cSRui Paulo 
3379f05cddf9SRui Paulo 	/* Wait for response from the peer */
3380f05cddf9SRui Paulo 	p2p_set_state(p2p, P2P_SD_DURING_FIND);
3381f05cddf9SRui Paulo 	p2p_set_timeout(p2p, 0, 200000);
3382f05cddf9SRui Paulo }
3383f05cddf9SRui Paulo 
3384f05cddf9SRui Paulo 
3385f05cddf9SRui Paulo /**
3386f05cddf9SRui Paulo  * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
3387f05cddf9SRui Paulo  * @p2p: P2P module context from p2p_init()
3388f05cddf9SRui Paulo  */
p2p_retry_pd(struct p2p_data * p2p)3389f05cddf9SRui Paulo static void p2p_retry_pd(struct p2p_data *p2p)
3390f05cddf9SRui Paulo {
3391f05cddf9SRui Paulo 	struct p2p_device *dev;
3392f05cddf9SRui Paulo 
3393f05cddf9SRui Paulo 	/*
3394f05cddf9SRui Paulo 	 * Retry the prov disc req attempt only for the peer that the user had
3395f05cddf9SRui Paulo 	 * requested.
3396f05cddf9SRui Paulo 	 */
3397f05cddf9SRui Paulo 
3398f05cddf9SRui Paulo 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3399*a90b9d01SCy Schubert 		if (!ether_addr_equal(p2p->pending_pd_devaddr,
3400*a90b9d01SCy Schubert 				      dev->info.p2p_device_addr))
3401f05cddf9SRui Paulo 			continue;
3402f05cddf9SRui Paulo 		if (!dev->req_config_methods)
3403f05cddf9SRui Paulo 			continue;
3404f05cddf9SRui Paulo 
34055b9c547cSRui Paulo 		p2p_dbg(p2p, "Send pending Provision Discovery Request to "
3406f05cddf9SRui Paulo 			MACSTR " (config methods 0x%x)",
3407f05cddf9SRui Paulo 			MAC2STR(dev->info.p2p_device_addr),
3408f05cddf9SRui Paulo 			dev->req_config_methods);
3409f05cddf9SRui Paulo 		p2p_send_prov_disc_req(p2p, dev,
34105b9c547cSRui Paulo 				       dev->flags & P2P_DEV_PD_FOR_JOIN,
34115b9c547cSRui Paulo 				       p2p->pd_force_freq);
3412f05cddf9SRui Paulo 		return;
3413f05cddf9SRui Paulo 	}
3414f05cddf9SRui Paulo }
3415f05cddf9SRui Paulo 
3416f05cddf9SRui Paulo 
p2p_prov_disc_cb(struct p2p_data * p2p,int success)3417f05cddf9SRui Paulo static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
3418f05cddf9SRui Paulo {
34195b9c547cSRui Paulo 	p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
3420f05cddf9SRui Paulo 		success);
3421f05cddf9SRui Paulo 
3422f05cddf9SRui Paulo 	/*
3423f05cddf9SRui Paulo 	 * Postpone resetting the pending action state till after we actually
3424f05cddf9SRui Paulo 	 * time out. This allows us to take some action like notifying any
3425f05cddf9SRui Paulo 	 * interested parties about no response to the request.
3426f05cddf9SRui Paulo 	 *
3427f05cddf9SRui Paulo 	 * When the timer (below) goes off we check in IDLE, SEARCH, or
3428f05cddf9SRui Paulo 	 * LISTEN_ONLY state, which are the only allowed states to issue a PD
3429f05cddf9SRui Paulo 	 * requests in, if this was still pending and then raise notification.
3430f05cddf9SRui Paulo 	 */
3431f05cddf9SRui Paulo 
3432f05cddf9SRui Paulo 	if (!success) {
3433f05cddf9SRui Paulo 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3434f05cddf9SRui Paulo 
3435f05cddf9SRui Paulo 		if (p2p->user_initiated_pd &&
3436f05cddf9SRui Paulo 		    (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
3437f05cddf9SRui Paulo 		{
3438f05cddf9SRui Paulo 			/* Retry request from timeout to avoid busy loops */
3439f05cddf9SRui Paulo 			p2p->pending_action_state = P2P_PENDING_PD;
3440f05cddf9SRui Paulo 			p2p_set_timeout(p2p, 0, 50000);
3441f05cddf9SRui Paulo 		} else if (p2p->state != P2P_IDLE)
3442f05cddf9SRui Paulo 			p2p_continue_find(p2p);
3443f05cddf9SRui Paulo 		else if (p2p->user_initiated_pd) {
3444f05cddf9SRui Paulo 			p2p->pending_action_state = P2P_PENDING_PD;
3445f05cddf9SRui Paulo 			p2p_set_timeout(p2p, 0, 300000);
3446f05cddf9SRui Paulo 		}
3447f05cddf9SRui Paulo 		return;
3448f05cddf9SRui Paulo 	}
3449f05cddf9SRui Paulo 
3450f05cddf9SRui Paulo 	/*
3451780fb4a2SCy Schubert 	 * If after PD Request the peer doesn't expect to receive PD Response
3452780fb4a2SCy Schubert 	 * the PD Request ACK indicates a completion of the current PD. This
3453780fb4a2SCy Schubert 	 * happens only on the advertiser side sending the follow-on PD Request
3454780fb4a2SCy Schubert 	 * with the status different than 12 (Success: accepted by user).
3455780fb4a2SCy Schubert 	 */
3456780fb4a2SCy Schubert 	if (p2p->p2ps_prov && !p2p->p2ps_prov->pd_seeker &&
3457780fb4a2SCy Schubert 	    p2p->p2ps_prov->status != P2P_SC_SUCCESS_DEFERRED) {
3458780fb4a2SCy Schubert 		p2p_dbg(p2p, "P2PS PD completion on Follow-on PD Request ACK");
3459780fb4a2SCy Schubert 
3460780fb4a2SCy Schubert 		if (p2p->send_action_in_progress) {
3461780fb4a2SCy Schubert 			p2p->send_action_in_progress = 0;
3462780fb4a2SCy Schubert 			p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3463780fb4a2SCy Schubert 		}
3464780fb4a2SCy Schubert 
3465780fb4a2SCy Schubert 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3466780fb4a2SCy Schubert 
3467780fb4a2SCy Schubert 		if (p2p->cfg->p2ps_prov_complete) {
3468780fb4a2SCy Schubert 			p2p->cfg->p2ps_prov_complete(
3469780fb4a2SCy Schubert 				p2p->cfg->cb_ctx,
3470780fb4a2SCy Schubert 				p2p->p2ps_prov->status,
3471780fb4a2SCy Schubert 				p2p->p2ps_prov->adv_mac,
3472780fb4a2SCy Schubert 				p2p->p2ps_prov->adv_mac,
3473780fb4a2SCy Schubert 				p2p->p2ps_prov->session_mac,
3474780fb4a2SCy Schubert 				NULL, p2p->p2ps_prov->adv_id,
3475780fb4a2SCy Schubert 				p2p->p2ps_prov->session_id,
3476780fb4a2SCy Schubert 				0, 0, NULL, 0, 0, 0,
3477780fb4a2SCy Schubert 				NULL, NULL, 0, 0, NULL, 0);
3478780fb4a2SCy Schubert 		}
3479780fb4a2SCy Schubert 
3480780fb4a2SCy Schubert 		if (p2p->user_initiated_pd)
3481780fb4a2SCy Schubert 			p2p_reset_pending_pd(p2p);
3482780fb4a2SCy Schubert 
3483780fb4a2SCy Schubert 		p2ps_prov_free(p2p);
3484780fb4a2SCy Schubert 		return;
3485780fb4a2SCy Schubert 	}
3486780fb4a2SCy Schubert 
3487780fb4a2SCy Schubert 	/*
3488f05cddf9SRui Paulo 	 * This postponing, of resetting pending_action_state, needs to be
3489f05cddf9SRui Paulo 	 * done only for user initiated PD requests and not internal ones.
3490f05cddf9SRui Paulo 	 */
3491f05cddf9SRui Paulo 	if (p2p->user_initiated_pd)
3492f05cddf9SRui Paulo 		p2p->pending_action_state = P2P_PENDING_PD;
3493f05cddf9SRui Paulo 	else
3494f05cddf9SRui Paulo 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3495f05cddf9SRui Paulo 
3496f05cddf9SRui Paulo 	/* Wait for response from the peer */
3497f05cddf9SRui Paulo 	if (p2p->state == P2P_SEARCH)
3498f05cddf9SRui Paulo 		p2p_set_state(p2p, P2P_PD_DURING_FIND);
3499f05cddf9SRui Paulo 	p2p_set_timeout(p2p, 0, 200000);
3500f05cddf9SRui Paulo }
3501f05cddf9SRui Paulo 
3502f05cddf9SRui Paulo 
p2p_prov_disc_resp_cb(struct p2p_data * p2p,int success)35035b9c547cSRui Paulo static void p2p_prov_disc_resp_cb(struct p2p_data *p2p, int success)
35045b9c547cSRui Paulo {
35055b9c547cSRui Paulo 	p2p_dbg(p2p, "Provision Discovery Response TX callback: success=%d",
35065b9c547cSRui Paulo 		success);
35075b9c547cSRui Paulo 
35085b9c547cSRui Paulo 	if (p2p->send_action_in_progress) {
35095b9c547cSRui Paulo 		p2p->send_action_in_progress = 0;
35105b9c547cSRui Paulo 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
35115b9c547cSRui Paulo 	}
35125b9c547cSRui Paulo 
35135b9c547cSRui Paulo 	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
35145b9c547cSRui Paulo 
3515c1d255d3SCy Schubert 	if (!success) {
3516c1d255d3SCy Schubert 		if (p2p->state == P2P_SEARCH)
3517c1d255d3SCy Schubert 			p2p_continue_find(p2p);
3518206b73d0SCy Schubert 		return;
3519c1d255d3SCy Schubert 	}
35205b9c547cSRui Paulo 
35215b9c547cSRui Paulo 	if (!p2p->cfg->prov_disc_resp_cb ||
3522c1d255d3SCy Schubert 	    p2p->cfg->prov_disc_resp_cb(p2p->cfg->cb_ctx) < 1) {
3523c1d255d3SCy Schubert 		if (p2p->state == P2P_SEARCH)
3524c1d255d3SCy Schubert 			p2p_continue_find(p2p);
3525206b73d0SCy Schubert 		return;
3526c1d255d3SCy Schubert 	}
35275b9c547cSRui Paulo 
35285b9c547cSRui Paulo 	p2p_dbg(p2p,
35295b9c547cSRui Paulo 		"Post-Provision Discovery operations started - do not try to continue other P2P operations");
35305b9c547cSRui Paulo }
35315b9c547cSRui Paulo 
35325b9c547cSRui Paulo 
p2p_scan_res_handler(struct p2p_data * p2p,const u8 * bssid,int freq,struct os_reltime * rx_time,int level,const u8 * ies,size_t ies_len)3533f05cddf9SRui Paulo int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
35345b9c547cSRui Paulo 			 struct os_reltime *rx_time, int level, const u8 *ies,
3535f05cddf9SRui Paulo 			 size_t ies_len)
3536f05cddf9SRui Paulo {
35375b9c547cSRui Paulo 	if (os_reltime_before(rx_time, &p2p->find_start)) {
35385b9c547cSRui Paulo 		/*
35395b9c547cSRui Paulo 		 * The driver may have cached (e.g., in cfg80211 BSS table) the
35405b9c547cSRui Paulo 		 * scan results for relatively long time. To avoid reporting
35415b9c547cSRui Paulo 		 * stale information, update P2P peers only based on results
35425b9c547cSRui Paulo 		 * that have based on frames received after the last p2p_find
35435b9c547cSRui Paulo 		 * operation was started.
35445b9c547cSRui Paulo 		 */
35455b9c547cSRui Paulo 		p2p_dbg(p2p, "Ignore old scan result for " MACSTR
3546780fb4a2SCy Schubert 			" (rx_time=%u.%06u find_start=%u.%06u)",
35475b9c547cSRui Paulo 			MAC2STR(bssid), (unsigned int) rx_time->sec,
3548780fb4a2SCy Schubert 			(unsigned int) rx_time->usec,
3549780fb4a2SCy Schubert 			(unsigned int) p2p->find_start.sec,
3550780fb4a2SCy Schubert 			(unsigned int) p2p->find_start.usec);
35515b9c547cSRui Paulo 		return 0;
35525b9c547cSRui Paulo 	}
35535b9c547cSRui Paulo 
35545b9c547cSRui Paulo 	p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
3555f05cddf9SRui Paulo 
3556f05cddf9SRui Paulo 	return 0;
3557f05cddf9SRui Paulo }
3558f05cddf9SRui Paulo 
3559f05cddf9SRui Paulo 
p2p_scan_res_handled(struct p2p_data * p2p,unsigned int delay)3560c1d255d3SCy Schubert void p2p_scan_res_handled(struct p2p_data *p2p, unsigned int delay)
3561f05cddf9SRui Paulo {
3562f05cddf9SRui Paulo 	if (!p2p->p2p_scan_running) {
35635b9c547cSRui Paulo 		p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
3564f05cddf9SRui Paulo 	}
3565f05cddf9SRui Paulo 	p2p->p2p_scan_running = 0;
3566c1d255d3SCy Schubert 
3567c1d255d3SCy Schubert 	/* Use this delay only when p2p_find doesn't set it */
3568c1d255d3SCy Schubert 	if (!p2p->search_delay)
3569c1d255d3SCy Schubert 		p2p->search_delay = delay;
3570c1d255d3SCy Schubert 
3571f05cddf9SRui Paulo 	eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
3572f05cddf9SRui Paulo 
3573f05cddf9SRui Paulo 	if (p2p_run_after_scan(p2p))
3574f05cddf9SRui Paulo 		return;
3575f05cddf9SRui Paulo 	if (p2p->state == P2P_SEARCH)
3576f05cddf9SRui Paulo 		p2p_continue_find(p2p);
3577f05cddf9SRui Paulo }
3578f05cddf9SRui Paulo 
3579f05cddf9SRui Paulo 
p2p_scan_ie(struct p2p_data * p2p,struct wpabuf * ies,const u8 * dev_id,unsigned int bands)3580780fb4a2SCy Schubert void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id,
3581780fb4a2SCy Schubert 		 unsigned int bands)
3582f05cddf9SRui Paulo {
35835b9c547cSRui Paulo 	u8 dev_capab;
3584f05cddf9SRui Paulo 	u8 *len;
3585f05cddf9SRui Paulo 
3586f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
3587f05cddf9SRui Paulo 	if (p2p->wfd_ie_probe_req)
3588f05cddf9SRui Paulo 		wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
3589f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
3590f05cddf9SRui Paulo 
35915b9c547cSRui Paulo 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
35925b9c547cSRui Paulo 		wpabuf_put_buf(ies,
35935b9c547cSRui Paulo 			       p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
35945b9c547cSRui Paulo 
3595f05cddf9SRui Paulo 	len = p2p_buf_add_ie_hdr(ies);
35965b9c547cSRui Paulo 
35975b9c547cSRui Paulo 	dev_capab = p2p->dev_capab & ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
35985b9c547cSRui Paulo 
35995b9c547cSRui Paulo 	/* P2PS requires Probe Request frames to include SD bit */
36005b9c547cSRui Paulo 	if (p2p->p2ps_seek && p2p->p2ps_seek_count)
36015b9c547cSRui Paulo 		dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
36025b9c547cSRui Paulo 
36035b9c547cSRui Paulo 	p2p_buf_add_capability(ies, dev_capab, 0);
36045b9c547cSRui Paulo 
3605f05cddf9SRui Paulo 	if (dev_id)
3606f05cddf9SRui Paulo 		p2p_buf_add_device_id(ies, dev_id);
3607f05cddf9SRui Paulo 	if (p2p->cfg->reg_class && p2p->cfg->channel)
3608f05cddf9SRui Paulo 		p2p_buf_add_listen_channel(ies, p2p->cfg->country,
3609f05cddf9SRui Paulo 					   p2p->cfg->reg_class,
3610f05cddf9SRui Paulo 					   p2p->cfg->channel);
3611f05cddf9SRui Paulo 	if (p2p->ext_listen_interval)
3612f05cddf9SRui Paulo 		p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
3613f05cddf9SRui Paulo 					      p2p->ext_listen_interval);
36145b9c547cSRui Paulo 
3615780fb4a2SCy Schubert 	if (bands & BAND_60_GHZ)
3616780fb4a2SCy Schubert 		p2p_buf_add_device_info(ies, p2p, NULL);
3617780fb4a2SCy Schubert 
36185b9c547cSRui Paulo 	if (p2p->p2ps_seek && p2p->p2ps_seek_count)
36195b9c547cSRui Paulo 		p2p_buf_add_service_hash(ies, p2p);
36205b9c547cSRui Paulo 
3621f05cddf9SRui Paulo 	/* TODO: p2p_buf_add_operating_channel() if GO */
3622f05cddf9SRui Paulo 	p2p_buf_update_ie_hdr(ies, len);
3623f05cddf9SRui Paulo }
3624f05cddf9SRui Paulo 
3625f05cddf9SRui Paulo 
p2p_scan_ie_buf_len(struct p2p_data * p2p)3626f05cddf9SRui Paulo size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
3627f05cddf9SRui Paulo {
3628f05cddf9SRui Paulo 	size_t len = 100;
3629f05cddf9SRui Paulo 
3630f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
3631f05cddf9SRui Paulo 	if (p2p && p2p->wfd_ie_probe_req)
3632f05cddf9SRui Paulo 		len += wpabuf_len(p2p->wfd_ie_probe_req);
3633f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
3634f05cddf9SRui Paulo 
36355b9c547cSRui Paulo 	if (p2p && p2p->vendor_elem &&
36365b9c547cSRui Paulo 	    p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
36375b9c547cSRui Paulo 		len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
36385b9c547cSRui Paulo 
3639f05cddf9SRui Paulo 	return len;
3640f05cddf9SRui Paulo }
3641f05cddf9SRui Paulo 
3642f05cddf9SRui Paulo 
p2p_ie_text(struct wpabuf * p2p_ie,char * buf,char * end)3643f05cddf9SRui Paulo int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
3644f05cddf9SRui Paulo {
3645f05cddf9SRui Paulo 	return p2p_attr_text(p2p_ie, buf, end);
3646f05cddf9SRui Paulo }
3647f05cddf9SRui Paulo 
3648f05cddf9SRui Paulo 
p2p_go_neg_req_cb(struct p2p_data * p2p,int success)3649f05cddf9SRui Paulo static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
3650f05cddf9SRui Paulo {
3651f05cddf9SRui Paulo 	struct p2p_device *dev = p2p->go_neg_peer;
36525b9c547cSRui Paulo 	int timeout;
3653f05cddf9SRui Paulo 
36545b9c547cSRui Paulo 	p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
3655f05cddf9SRui Paulo 
3656f05cddf9SRui Paulo 	if (dev == NULL) {
36575b9c547cSRui Paulo 		p2p_dbg(p2p, "No pending GO Negotiation");
3658f05cddf9SRui Paulo 		return;
3659f05cddf9SRui Paulo 	}
3660f05cddf9SRui Paulo 
3661f05cddf9SRui Paulo 	if (success) {
3662f05cddf9SRui Paulo 		if (dev->flags & P2P_DEV_USER_REJECTED) {
3663f05cddf9SRui Paulo 			p2p_set_state(p2p, P2P_IDLE);
3664f05cddf9SRui Paulo 			return;
3665f05cddf9SRui Paulo 		}
3666f05cddf9SRui Paulo 	} else if (dev->go_neg_req_sent) {
3667f05cddf9SRui Paulo 		/* Cancel the increment from p2p_connect_send() on failure */
3668f05cddf9SRui Paulo 		dev->go_neg_req_sent--;
3669f05cddf9SRui Paulo 	}
3670f05cddf9SRui Paulo 
3671f05cddf9SRui Paulo 	if (!success &&
3672f05cddf9SRui Paulo 	    (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
3673f05cddf9SRui Paulo 	    !is_zero_ether_addr(dev->member_in_go_dev)) {
36745b9c547cSRui Paulo 		p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
3675f05cddf9SRui Paulo 			MAC2STR(dev->info.p2p_device_addr));
3676f05cddf9SRui Paulo 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3677f05cddf9SRui Paulo 		p2p_send_dev_disc_req(p2p, dev);
3678f05cddf9SRui Paulo 		return;
3679f05cddf9SRui Paulo 	}
3680f05cddf9SRui Paulo 
3681f05cddf9SRui Paulo 	/*
3682f05cddf9SRui Paulo 	 * Use P2P find, if needed, to find the other device from its listen
3683f05cddf9SRui Paulo 	 * channel.
3684f05cddf9SRui Paulo 	 */
3685f05cddf9SRui Paulo 	p2p_set_state(p2p, P2P_CONNECT);
36865b9c547cSRui Paulo 	timeout = success ? 500000 : 100000;
36875b9c547cSRui Paulo 	if (!success && p2p->go_neg_peer &&
36885b9c547cSRui Paulo 	    (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
36895b9c547cSRui Paulo 		unsigned int r;
36905b9c547cSRui Paulo 		/*
36915b9c547cSRui Paulo 		 * Peer is expected to wait our response and we will skip the
36925b9c547cSRui Paulo 		 * listen phase. Add some randomness to the wait time here to
36935b9c547cSRui Paulo 		 * make it less likely to hit cases where we could end up in
36945b9c547cSRui Paulo 		 * sync with peer not listening.
36955b9c547cSRui Paulo 		 */
36965b9c547cSRui Paulo 		if (os_get_random((u8 *) &r, sizeof(r)) < 0)
36975b9c547cSRui Paulo 			r = 0;
36985b9c547cSRui Paulo 		timeout += r % 100000;
36995b9c547cSRui Paulo 	}
37005b9c547cSRui Paulo 	p2p_set_timeout(p2p, 0, timeout);
3701f05cddf9SRui Paulo }
3702f05cddf9SRui Paulo 
3703f05cddf9SRui Paulo 
p2p_go_neg_resp_cb(struct p2p_data * p2p,int success)3704f05cddf9SRui Paulo static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
3705f05cddf9SRui Paulo {
37065b9c547cSRui Paulo 	p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
3707f05cddf9SRui Paulo 		success);
3708f05cddf9SRui Paulo 	if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
37095b9c547cSRui Paulo 		p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
3710f05cddf9SRui Paulo 		return;
3711f05cddf9SRui Paulo 	}
3712f05cddf9SRui Paulo 	p2p_set_state(p2p, P2P_CONNECT);
37135b9c547cSRui Paulo 	p2p_set_timeout(p2p, 0, 500000);
3714f05cddf9SRui Paulo }
3715f05cddf9SRui Paulo 
3716f05cddf9SRui Paulo 
p2p_go_neg_resp_failure_cb(struct p2p_data * p2p,int success,const u8 * addr)37175b9c547cSRui Paulo static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
37185b9c547cSRui Paulo 				       const u8 *addr)
3719f05cddf9SRui Paulo {
37205b9c547cSRui Paulo 	p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
3721f05cddf9SRui Paulo 	if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
37225b9c547cSRui Paulo 		p2p_go_neg_failed(p2p, p2p->go_neg_peer->status);
37235b9c547cSRui Paulo 		return;
3724f05cddf9SRui Paulo 	}
37255b9c547cSRui Paulo 
37265b9c547cSRui Paulo 	if (success) {
37275b9c547cSRui Paulo 		struct p2p_device *dev;
37285b9c547cSRui Paulo 		dev = p2p_get_device(p2p, addr);
37295b9c547cSRui Paulo 		if (dev &&
37305b9c547cSRui Paulo 		    dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
37315b9c547cSRui Paulo 			dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
37325b9c547cSRui Paulo 	}
37335b9c547cSRui Paulo 
37345b9c547cSRui Paulo 	if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
37355b9c547cSRui Paulo 		p2p_continue_find(p2p);
3736f05cddf9SRui Paulo }
3737f05cddf9SRui Paulo 
3738f05cddf9SRui Paulo 
p2p_go_neg_conf_cb(struct p2p_data * p2p,enum p2p_send_action_result result)3739f05cddf9SRui Paulo static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
3740f05cddf9SRui Paulo 			       enum p2p_send_action_result result)
3741f05cddf9SRui Paulo {
3742f05cddf9SRui Paulo 	struct p2p_device *dev;
3743f05cddf9SRui Paulo 
37445b9c547cSRui Paulo 	p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
3745f05cddf9SRui Paulo 	if (result == P2P_SEND_ACTION_FAILED) {
37465b9c547cSRui Paulo 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
37475b9c547cSRui Paulo 		p2p_go_neg_failed(p2p, -1);
3748f05cddf9SRui Paulo 		return;
3749f05cddf9SRui Paulo 	}
37505b9c547cSRui Paulo 
37515b9c547cSRui Paulo 	dev = p2p->go_neg_peer;
37525b9c547cSRui Paulo 
3753f05cddf9SRui Paulo 	if (result == P2P_SEND_ACTION_NO_ACK) {
3754f05cddf9SRui Paulo 		/*
37555b9c547cSRui Paulo 		 * Retry GO Negotiation Confirmation
37565b9c547cSRui Paulo 		 * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive
37575b9c547cSRui Paulo 		 * ACK for confirmation.
37585b9c547cSRui Paulo 		 */
37595b9c547cSRui Paulo 		if (dev && dev->go_neg_conf &&
37605b9c547cSRui Paulo 		    dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) {
37615b9c547cSRui Paulo 			p2p_dbg(p2p, "GO Negotiation Confirm retry %d",
37625b9c547cSRui Paulo 				dev->go_neg_conf_sent);
37635b9c547cSRui Paulo 			p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
37645b9c547cSRui Paulo 			if (p2p_send_action(p2p, dev->go_neg_conf_freq,
37655b9c547cSRui Paulo 					    dev->info.p2p_device_addr,
37665b9c547cSRui Paulo 					    p2p->cfg->dev_addr,
37675b9c547cSRui Paulo 					    dev->info.p2p_device_addr,
37685b9c547cSRui Paulo 					    wpabuf_head(dev->go_neg_conf),
37695b9c547cSRui Paulo 					    wpabuf_len(dev->go_neg_conf), 0) >=
37705b9c547cSRui Paulo 			    0) {
37715b9c547cSRui Paulo 				dev->go_neg_conf_sent++;
37725b9c547cSRui Paulo 				return;
37735b9c547cSRui Paulo 			}
37745b9c547cSRui Paulo 			p2p_dbg(p2p, "Failed to re-send Action frame");
37755b9c547cSRui Paulo 
37765b9c547cSRui Paulo 			/*
37775b9c547cSRui Paulo 			 * Continue with the assumption that the first attempt
37785b9c547cSRui Paulo 			 * went through and just the ACK frame was lost.
37795b9c547cSRui Paulo 			 */
37805b9c547cSRui Paulo 		}
37815b9c547cSRui Paulo 
37825b9c547cSRui Paulo 		/*
3783f05cddf9SRui Paulo 		 * It looks like the TX status for GO Negotiation Confirm is
3784f05cddf9SRui Paulo 		 * often showing failure even when the peer has actually
3785f05cddf9SRui Paulo 		 * received the frame. Since the peer may change channels
3786f05cddf9SRui Paulo 		 * immediately after having received the frame, we may not see
3787f05cddf9SRui Paulo 		 * an Ack for retries, so just dropping a single frame may
3788f05cddf9SRui Paulo 		 * trigger this. To allow the group formation to succeed if the
3789f05cddf9SRui Paulo 		 * peer did indeed receive the frame, continue regardless of
3790f05cddf9SRui Paulo 		 * the TX status.
3791f05cddf9SRui Paulo 		 */
37925b9c547cSRui Paulo 		p2p_dbg(p2p, "Assume GO Negotiation Confirm TX was actually received by the peer even though Ack was not reported");
3793f05cddf9SRui Paulo 	}
3794f05cddf9SRui Paulo 
37955b9c547cSRui Paulo 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
37965b9c547cSRui Paulo 
3797f05cddf9SRui Paulo 	if (dev == NULL)
3798f05cddf9SRui Paulo 		return;
3799f05cddf9SRui Paulo 
3800f05cddf9SRui Paulo 	p2p_go_complete(p2p, dev);
3801f05cddf9SRui Paulo }
3802f05cddf9SRui Paulo 
3803f05cddf9SRui Paulo 
p2p_send_action_cb(struct p2p_data * p2p,unsigned int freq,const u8 * dst,const u8 * src,const u8 * bssid,enum p2p_send_action_result result)3804f05cddf9SRui Paulo void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3805f05cddf9SRui Paulo 			const u8 *src, const u8 *bssid,
3806f05cddf9SRui Paulo 			enum p2p_send_action_result result)
3807f05cddf9SRui Paulo {
3808f05cddf9SRui Paulo 	enum p2p_pending_action_state state;
3809f05cddf9SRui Paulo 	int success;
3810f05cddf9SRui Paulo 
38115b9c547cSRui Paulo 	p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
38125b9c547cSRui Paulo 		" src=" MACSTR " bssid=" MACSTR " result=%d p2p_state=%s)",
3813f05cddf9SRui Paulo 		p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
38145b9c547cSRui Paulo 		MAC2STR(bssid), result, p2p_state_txt(p2p->state));
3815f05cddf9SRui Paulo 	success = result == P2P_SEND_ACTION_SUCCESS;
3816f05cddf9SRui Paulo 	state = p2p->pending_action_state;
3817f05cddf9SRui Paulo 	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3818f05cddf9SRui Paulo 	switch (state) {
3819f05cddf9SRui Paulo 	case P2P_NO_PENDING_ACTION:
38205b9c547cSRui Paulo 		if (p2p->send_action_in_progress) {
38215b9c547cSRui Paulo 			p2p->send_action_in_progress = 0;
38225b9c547cSRui Paulo 			p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
38235b9c547cSRui Paulo 		}
3824f05cddf9SRui Paulo 		break;
3825f05cddf9SRui Paulo 	case P2P_PENDING_GO_NEG_REQUEST:
3826f05cddf9SRui Paulo 		p2p_go_neg_req_cb(p2p, success);
3827f05cddf9SRui Paulo 		break;
3828f05cddf9SRui Paulo 	case P2P_PENDING_GO_NEG_RESPONSE:
3829f05cddf9SRui Paulo 		p2p_go_neg_resp_cb(p2p, success);
3830f05cddf9SRui Paulo 		break;
3831f05cddf9SRui Paulo 	case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
38325b9c547cSRui Paulo 		p2p_go_neg_resp_failure_cb(p2p, success, dst);
3833f05cddf9SRui Paulo 		break;
3834f05cddf9SRui Paulo 	case P2P_PENDING_GO_NEG_CONFIRM:
3835f05cddf9SRui Paulo 		p2p_go_neg_conf_cb(p2p, result);
3836f05cddf9SRui Paulo 		break;
3837f05cddf9SRui Paulo 	case P2P_PENDING_SD:
3838*a90b9d01SCy Schubert 		p2p_sd_query_cb(p2p, success);
3839f05cddf9SRui Paulo 		break;
3840f05cddf9SRui Paulo 	case P2P_PENDING_PD:
3841f05cddf9SRui Paulo 		p2p_prov_disc_cb(p2p, success);
3842f05cddf9SRui Paulo 		break;
38435b9c547cSRui Paulo 	case P2P_PENDING_PD_RESPONSE:
38445b9c547cSRui Paulo 		p2p_prov_disc_resp_cb(p2p, success);
38455b9c547cSRui Paulo 		break;
3846f05cddf9SRui Paulo 	case P2P_PENDING_INVITATION_REQUEST:
3847f05cddf9SRui Paulo 		p2p_invitation_req_cb(p2p, success);
3848f05cddf9SRui Paulo 		break;
3849f05cddf9SRui Paulo 	case P2P_PENDING_INVITATION_RESPONSE:
3850f05cddf9SRui Paulo 		p2p_invitation_resp_cb(p2p, success);
3851f05cddf9SRui Paulo 		break;
3852f05cddf9SRui Paulo 	case P2P_PENDING_DEV_DISC_REQUEST:
3853f05cddf9SRui Paulo 		p2p_dev_disc_req_cb(p2p, success);
3854f05cddf9SRui Paulo 		break;
3855f05cddf9SRui Paulo 	case P2P_PENDING_DEV_DISC_RESPONSE:
3856f05cddf9SRui Paulo 		p2p_dev_disc_resp_cb(p2p, success);
3857f05cddf9SRui Paulo 		break;
3858f05cddf9SRui Paulo 	case P2P_PENDING_GO_DISC_REQ:
3859f05cddf9SRui Paulo 		p2p_go_disc_req_cb(p2p, success);
3860f05cddf9SRui Paulo 		break;
3861f05cddf9SRui Paulo 	}
3862f05cddf9SRui Paulo }
3863f05cddf9SRui Paulo 
3864f05cddf9SRui Paulo 
p2p_listen_cb(struct p2p_data * p2p,unsigned int freq,unsigned int duration)3865f05cddf9SRui Paulo void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3866f05cddf9SRui Paulo 		   unsigned int duration)
3867f05cddf9SRui Paulo {
3868f05cddf9SRui Paulo 	if (freq == p2p->pending_client_disc_freq) {
38695b9c547cSRui Paulo 		p2p_dbg(p2p, "Client discoverability remain-awake completed");
3870f05cddf9SRui Paulo 		p2p->pending_client_disc_freq = 0;
3871f05cddf9SRui Paulo 		return;
3872f05cddf9SRui Paulo 	}
3873f05cddf9SRui Paulo 
3874f05cddf9SRui Paulo 	if (freq != p2p->pending_listen_freq) {
38755b9c547cSRui Paulo 		p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
3876f05cddf9SRui Paulo 			freq, duration, p2p->pending_listen_freq);
3877f05cddf9SRui Paulo 		return;
3878f05cddf9SRui Paulo 	}
3879f05cddf9SRui Paulo 
38805b9c547cSRui Paulo 	p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
3881f05cddf9SRui Paulo 		p2p->pending_listen_sec, p2p->pending_listen_usec,
3882f05cddf9SRui Paulo 		p2p->pending_listen_freq);
3883*a90b9d01SCy Schubert 	p2p->pending_listen_wait_drv = false;
3884f05cddf9SRui Paulo 	p2p->in_listen = 1;
3885f05cddf9SRui Paulo 	p2p->drv_in_listen = freq;
3886f05cddf9SRui Paulo 	if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3887f05cddf9SRui Paulo 		/*
3888f05cddf9SRui Paulo 		 * Add 20 msec extra wait to avoid race condition with driver
3889f05cddf9SRui Paulo 		 * remain-on-channel end event, i.e., give driver more time to
3890f05cddf9SRui Paulo 		 * complete the operation before our timeout expires.
3891f05cddf9SRui Paulo 		 */
3892f05cddf9SRui Paulo 		p2p_set_timeout(p2p, p2p->pending_listen_sec,
3893f05cddf9SRui Paulo 				p2p->pending_listen_usec + 20000);
3894f05cddf9SRui Paulo 	}
3895f05cddf9SRui Paulo 
3896f05cddf9SRui Paulo 	p2p->pending_listen_freq = 0;
3897f05cddf9SRui Paulo }
3898f05cddf9SRui Paulo 
3899f05cddf9SRui Paulo 
p2p_listen_end(struct p2p_data * p2p,unsigned int freq)3900f05cddf9SRui Paulo int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3901f05cddf9SRui Paulo {
39025b9c547cSRui Paulo 	p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
3903f05cddf9SRui Paulo 	p2p->drv_in_listen = 0;
3904f05cddf9SRui Paulo 	if (p2p->in_listen)
3905f05cddf9SRui Paulo 		return 0; /* Internal timeout will trigger the next step */
3906f05cddf9SRui Paulo 
390785732ac8SCy Schubert 	if (p2p->state == P2P_WAIT_PEER_CONNECT && p2p->go_neg_peer &&
390885732ac8SCy Schubert 	    p2p->pending_listen_freq) {
390985732ac8SCy Schubert 		/*
391085732ac8SCy Schubert 		 * Better wait a bit if the driver is unable to start
391185732ac8SCy Schubert 		 * offchannel operation for some reason to continue with
391285732ac8SCy Schubert 		 * P2P_WAIT_PEER_(IDLE/CONNECT) state transitions.
391385732ac8SCy Schubert 		 */
391485732ac8SCy Schubert 		p2p_dbg(p2p,
391585732ac8SCy Schubert 			"Listen operation did not seem to start - delay idle phase to avoid busy loop");
391685732ac8SCy Schubert 		p2p_set_timeout(p2p, 0, 100000);
391785732ac8SCy Schubert 		return 1;
391885732ac8SCy Schubert 	}
391985732ac8SCy Schubert 
3920f05cddf9SRui Paulo 	if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3921f05cddf9SRui Paulo 		if (p2p->go_neg_peer->connect_reqs >= 120) {
39225b9c547cSRui Paulo 			p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
39235b9c547cSRui Paulo 			p2p_go_neg_failed(p2p, -1);
3924f05cddf9SRui Paulo 			return 0;
3925f05cddf9SRui Paulo 		}
3926f05cddf9SRui Paulo 
3927f05cddf9SRui Paulo 		p2p_set_state(p2p, P2P_CONNECT);
3928f05cddf9SRui Paulo 		p2p_connect_send(p2p, p2p->go_neg_peer);
3929f05cddf9SRui Paulo 		return 1;
3930f05cddf9SRui Paulo 	} else if (p2p->state == P2P_SEARCH) {
3931f05cddf9SRui Paulo 		if (p2p->p2p_scan_running) {
3932f05cddf9SRui Paulo 			 /*
3933f05cddf9SRui Paulo 			  * Search is already in progress. This can happen if
3934f05cddf9SRui Paulo 			  * an Action frame RX is reported immediately after
3935f05cddf9SRui Paulo 			  * the end of a remain-on-channel operation and the
3936f05cddf9SRui Paulo 			  * response frame to that is sent using an offchannel
3937f05cddf9SRui Paulo 			  * operation while in p2p_find. Avoid an attempt to
3938f05cddf9SRui Paulo 			  * restart a scan here.
3939f05cddf9SRui Paulo 			  */
39405b9c547cSRui Paulo 			p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
3941f05cddf9SRui Paulo 			return 1;
3942f05cddf9SRui Paulo 		}
3943f05cddf9SRui Paulo 		if (p2p->pending_listen_freq) {
3944f05cddf9SRui Paulo 			/*
3945f05cddf9SRui Paulo 			 * Better wait a bit if the driver is unable to start
3946f05cddf9SRui Paulo 			 * offchannel operation for some reason. p2p_search()
3947f05cddf9SRui Paulo 			 * will be started from internal timeout.
3948f05cddf9SRui Paulo 			 */
39495b9c547cSRui Paulo 			p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
3950f05cddf9SRui Paulo 			p2p_set_timeout(p2p, 0, 100000);
3951f05cddf9SRui Paulo 			return 1;
3952f05cddf9SRui Paulo 		}
3953f05cddf9SRui Paulo 		if (p2p->search_delay) {
39545b9c547cSRui Paulo 			p2p_dbg(p2p, "Delay search operation by %u ms",
3955f05cddf9SRui Paulo 				p2p->search_delay);
3956f05cddf9SRui Paulo 			p2p_set_timeout(p2p, p2p->search_delay / 1000,
3957f05cddf9SRui Paulo 					(p2p->search_delay % 1000) * 1000);
3958f05cddf9SRui Paulo 			return 1;
3959f05cddf9SRui Paulo 		}
3960f05cddf9SRui Paulo 		p2p_search(p2p);
3961f05cddf9SRui Paulo 		return 1;
3962f05cddf9SRui Paulo 	}
3963f05cddf9SRui Paulo 
3964f05cddf9SRui Paulo 	return 0;
3965f05cddf9SRui Paulo }
3966f05cddf9SRui Paulo 
3967f05cddf9SRui Paulo 
p2p_timeout_connect(struct p2p_data * p2p)3968f05cddf9SRui Paulo static void p2p_timeout_connect(struct p2p_data *p2p)
3969f05cddf9SRui Paulo {
3970f05cddf9SRui Paulo 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3971f05cddf9SRui Paulo 	if (p2p->go_neg_peer &&
3972f05cddf9SRui Paulo 	    (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
39735b9c547cSRui Paulo 		p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
39745b9c547cSRui Paulo 		p2p_go_neg_failed(p2p, -1);
39755b9c547cSRui Paulo 		return;
39765b9c547cSRui Paulo 	}
39775b9c547cSRui Paulo 	if (p2p->go_neg_peer &&
39785b9c547cSRui Paulo 	    (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
39795b9c547cSRui Paulo 	    p2p->go_neg_peer->connect_reqs < 120) {
39805b9c547cSRui Paulo 		p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
39815b9c547cSRui Paulo 		p2p_connect_send(p2p, p2p->go_neg_peer);
39825b9c547cSRui Paulo 		return;
39835b9c547cSRui Paulo 	}
39845b9c547cSRui Paulo 	if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
39855b9c547cSRui Paulo 		p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
39865b9c547cSRui Paulo 		p2p_set_state(p2p, P2P_CONNECT_LISTEN);
39875b9c547cSRui Paulo 		p2p_set_timeout(p2p, 0, 30000);
3988f05cddf9SRui Paulo 		return;
3989f05cddf9SRui Paulo 	}
3990f05cddf9SRui Paulo 	p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3991f05cddf9SRui Paulo 	p2p_listen_in_find(p2p, 0);
3992f05cddf9SRui Paulo }
3993f05cddf9SRui Paulo 
3994f05cddf9SRui Paulo 
p2p_timeout_connect_listen(struct p2p_data * p2p)3995f05cddf9SRui Paulo static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3996f05cddf9SRui Paulo {
3997f05cddf9SRui Paulo 	if (p2p->go_neg_peer) {
3998f05cddf9SRui Paulo 		if (p2p->drv_in_listen) {
39995b9c547cSRui Paulo 			p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
4000f05cddf9SRui Paulo 			return;
4001f05cddf9SRui Paulo 		}
4002f05cddf9SRui Paulo 
4003f05cddf9SRui Paulo 		if (p2p->go_neg_peer->connect_reqs >= 120) {
40045b9c547cSRui Paulo 			p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
40055b9c547cSRui Paulo 			p2p_go_neg_failed(p2p, -1);
4006f05cddf9SRui Paulo 			return;
4007f05cddf9SRui Paulo 		}
4008f05cddf9SRui Paulo 
4009f05cddf9SRui Paulo 		p2p_set_state(p2p, P2P_CONNECT);
4010f05cddf9SRui Paulo 		p2p_connect_send(p2p, p2p->go_neg_peer);
4011f05cddf9SRui Paulo 	} else
4012f05cddf9SRui Paulo 		p2p_set_state(p2p, P2P_IDLE);
4013f05cddf9SRui Paulo }
4014f05cddf9SRui Paulo 
4015f05cddf9SRui Paulo 
p2p_timeout_wait_peer_connect(struct p2p_data * p2p)4016f05cddf9SRui Paulo static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
4017f05cddf9SRui Paulo {
4018f05cddf9SRui Paulo 	p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
40195b9c547cSRui Paulo 
40205b9c547cSRui Paulo 	if (p2p->cfg->is_concurrent_session_active &&
40215b9c547cSRui Paulo 	    p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
4022f05cddf9SRui Paulo 		p2p_set_timeout(p2p, 0, 500000);
40235b9c547cSRui Paulo 	else
40245b9c547cSRui Paulo 		p2p_set_timeout(p2p, 0, 200000);
4025f05cddf9SRui Paulo }
4026f05cddf9SRui Paulo 
4027f05cddf9SRui Paulo 
p2p_timeout_wait_peer_idle(struct p2p_data * p2p)4028f05cddf9SRui Paulo static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
4029f05cddf9SRui Paulo {
4030f05cddf9SRui Paulo 	struct p2p_device *dev = p2p->go_neg_peer;
4031f05cddf9SRui Paulo 
4032f05cddf9SRui Paulo 	if (dev == NULL) {
40335b9c547cSRui Paulo 		p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
4034f05cddf9SRui Paulo 		return;
4035f05cddf9SRui Paulo 	}
4036f05cddf9SRui Paulo 
40375b9c547cSRui Paulo 	p2p_dbg(p2p, "Go to Listen state while waiting for the peer to become ready for GO Negotiation");
4038c1d255d3SCy Schubert 	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
4039*a90b9d01SCy Schubert 	p2p->pending_listen_wait_drv = false;
4040c1d255d3SCy Schubert 	if (p2p->pending_listen_freq) {
4041c1d255d3SCy Schubert 		p2p_dbg(p2p, "Clear pending_listen_freq for %s", __func__);
4042c1d255d3SCy Schubert 		p2p->pending_listen_freq = 0;
4043c1d255d3SCy Schubert 	}
4044f05cddf9SRui Paulo 	p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
4045f05cddf9SRui Paulo 	p2p_listen_in_find(p2p, 0);
4046f05cddf9SRui Paulo }
4047f05cddf9SRui Paulo 
4048f05cddf9SRui Paulo 
p2p_timeout_sd_during_find(struct p2p_data * p2p)4049f05cddf9SRui Paulo static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
4050f05cddf9SRui Paulo {
40515b9c547cSRui Paulo 	p2p_dbg(p2p, "Service Discovery Query timeout");
4052f05cddf9SRui Paulo 	if (p2p->sd_peer) {
4053f05cddf9SRui Paulo 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4054f05cddf9SRui Paulo 		p2p->sd_peer = NULL;
4055f05cddf9SRui Paulo 	}
4056f05cddf9SRui Paulo 	p2p_continue_find(p2p);
4057f05cddf9SRui Paulo }
4058f05cddf9SRui Paulo 
4059f05cddf9SRui Paulo 
p2p_timeout_prov_disc_during_find(struct p2p_data * p2p)4060f05cddf9SRui Paulo static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
4061f05cddf9SRui Paulo {
40625b9c547cSRui Paulo 	p2p_dbg(p2p, "Provision Discovery Request timeout");
4063f05cddf9SRui Paulo 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4064f05cddf9SRui Paulo 	p2p_continue_find(p2p);
4065f05cddf9SRui Paulo }
4066f05cddf9SRui Paulo 
4067f05cddf9SRui Paulo 
p2p_timeout_prov_disc_req(struct p2p_data * p2p)4068f05cddf9SRui Paulo static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
4069f05cddf9SRui Paulo {
40705b9c547cSRui Paulo 	u32 adv_id = 0;
40715b9c547cSRui Paulo 	u8 *adv_mac = NULL;
40725b9c547cSRui Paulo 
4073f05cddf9SRui Paulo 	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4074f05cddf9SRui Paulo 
4075f05cddf9SRui Paulo 	/*
4076f05cddf9SRui Paulo 	 * For user initiated PD requests that we have not gotten any responses
4077f05cddf9SRui Paulo 	 * for while in IDLE state, we retry them a couple of times before
4078f05cddf9SRui Paulo 	 * giving up.
4079f05cddf9SRui Paulo 	 */
4080f05cddf9SRui Paulo 	if (!p2p->user_initiated_pd)
4081f05cddf9SRui Paulo 		return;
4082f05cddf9SRui Paulo 
40835b9c547cSRui Paulo 	p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
4084f05cddf9SRui Paulo 
4085f05cddf9SRui Paulo 	if (p2p->pd_retries) {
4086f05cddf9SRui Paulo 		p2p->pd_retries--;
4087f05cddf9SRui Paulo 		p2p_retry_pd(p2p);
4088f05cddf9SRui Paulo 	} else {
4089f05cddf9SRui Paulo 		struct p2p_device *dev;
4090f05cddf9SRui Paulo 		int for_join = 0;
4091f05cddf9SRui Paulo 
4092f05cddf9SRui Paulo 		dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
4093*a90b9d01SCy Schubert 			if (!ether_addr_equal(p2p->pending_pd_devaddr,
4094*a90b9d01SCy Schubert 					      dev->info.p2p_device_addr))
4095f05cddf9SRui Paulo 				continue;
4096f05cddf9SRui Paulo 			if (dev->req_config_methods &&
4097f05cddf9SRui Paulo 			    (dev->flags & P2P_DEV_PD_FOR_JOIN))
4098f05cddf9SRui Paulo 				for_join = 1;
4099f05cddf9SRui Paulo 		}
4100f05cddf9SRui Paulo 
41015b9c547cSRui Paulo 		if (p2p->p2ps_prov) {
41025b9c547cSRui Paulo 			adv_id = p2p->p2ps_prov->adv_id;
41035b9c547cSRui Paulo 			adv_mac = p2p->p2ps_prov->adv_mac;
41045b9c547cSRui Paulo 		}
41055b9c547cSRui Paulo 
4106f05cddf9SRui Paulo 		if (p2p->cfg->prov_disc_fail)
4107f05cddf9SRui Paulo 			p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
4108f05cddf9SRui Paulo 						 p2p->pending_pd_devaddr,
4109f05cddf9SRui Paulo 						 for_join ?
4110f05cddf9SRui Paulo 						 P2P_PROV_DISC_TIMEOUT_JOIN :
41115b9c547cSRui Paulo 						 P2P_PROV_DISC_TIMEOUT,
41125b9c547cSRui Paulo 						 adv_id, adv_mac, NULL);
4113f05cddf9SRui Paulo 		p2p_reset_pending_pd(p2p);
4114f05cddf9SRui Paulo 	}
4115f05cddf9SRui Paulo }
4116f05cddf9SRui Paulo 
4117f05cddf9SRui Paulo 
p2p_timeout_invite(struct p2p_data * p2p)4118f05cddf9SRui Paulo static void p2p_timeout_invite(struct p2p_data *p2p)
4119f05cddf9SRui Paulo {
4120f05cddf9SRui Paulo 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4121f05cddf9SRui Paulo 	p2p_set_state(p2p, P2P_INVITE_LISTEN);
4122f05cddf9SRui Paulo 	if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
4123f05cddf9SRui Paulo 		/*
4124f05cddf9SRui Paulo 		 * Better remain on operating channel instead of listen channel
4125f05cddf9SRui Paulo 		 * when running a group.
4126*a90b9d01SCy Schubert 		 * Wait 120 ms to let the P2P GO to send its beacon on the
4127*a90b9d01SCy Schubert 		 * intended TBTT.
4128f05cddf9SRui Paulo 		 */
41295b9c547cSRui Paulo 		p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
4130*a90b9d01SCy Schubert 		p2p_set_timeout(p2p, 0, 120000);
4131f05cddf9SRui Paulo 		return;
4132f05cddf9SRui Paulo 	}
4133f05cddf9SRui Paulo 	p2p_listen_in_find(p2p, 0);
4134f05cddf9SRui Paulo }
4135f05cddf9SRui Paulo 
4136f05cddf9SRui Paulo 
p2p_timeout_invite_listen(struct p2p_data * p2p)4137f05cddf9SRui Paulo static void p2p_timeout_invite_listen(struct p2p_data *p2p)
4138f05cddf9SRui Paulo {
4139f05cddf9SRui Paulo 	if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
4140f05cddf9SRui Paulo 		p2p_set_state(p2p, P2P_INVITE);
4141f05cddf9SRui Paulo 		p2p_invite_send(p2p, p2p->invite_peer,
41425b9c547cSRui Paulo 				p2p->invite_go_dev_addr, p2p->invite_dev_pw_id);
4143f05cddf9SRui Paulo 	} else {
4144f05cddf9SRui Paulo 		if (p2p->invite_peer) {
41455b9c547cSRui Paulo 			p2p_dbg(p2p, "Invitation Request retry limit reached");
4146f05cddf9SRui Paulo 			if (p2p->cfg->invitation_result)
4147f05cddf9SRui Paulo 				p2p->cfg->invitation_result(
41485b9c547cSRui Paulo 					p2p->cfg->cb_ctx, -1, NULL, NULL,
41495b9c547cSRui Paulo 					p2p->invite_peer->info.p2p_device_addr,
41505b9c547cSRui Paulo 					0, 0);
4151f05cddf9SRui Paulo 		}
4152f05cddf9SRui Paulo 		p2p_set_state(p2p, P2P_IDLE);
4153f05cddf9SRui Paulo 	}
4154f05cddf9SRui Paulo }
4155f05cddf9SRui Paulo 
4156f05cddf9SRui Paulo 
p2p_state_timeout(void * eloop_ctx,void * timeout_ctx)4157f05cddf9SRui Paulo static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
4158f05cddf9SRui Paulo {
4159f05cddf9SRui Paulo 	struct p2p_data *p2p = eloop_ctx;
4160f05cddf9SRui Paulo 
41615b9c547cSRui Paulo 	p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
4162f05cddf9SRui Paulo 
4163f05cddf9SRui Paulo 	p2p->in_listen = 0;
41645b9c547cSRui Paulo 	if (p2p->drv_in_listen) {
41655b9c547cSRui Paulo 		p2p_dbg(p2p, "Driver is still in listen state - stop it");
41665b9c547cSRui Paulo 		p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
4167*a90b9d01SCy Schubert 		p2p->pending_listen_wait_drv = false;
41685b9c547cSRui Paulo 	}
4169f05cddf9SRui Paulo 
4170f05cddf9SRui Paulo 	switch (p2p->state) {
4171f05cddf9SRui Paulo 	case P2P_IDLE:
4172f05cddf9SRui Paulo 		/* Check if we timed out waiting for PD req */
4173f05cddf9SRui Paulo 		if (p2p->pending_action_state == P2P_PENDING_PD)
4174f05cddf9SRui Paulo 			p2p_timeout_prov_disc_req(p2p);
4175f05cddf9SRui Paulo 		break;
4176f05cddf9SRui Paulo 	case P2P_SEARCH:
4177f05cddf9SRui Paulo 		/* Check if we timed out waiting for PD req */
4178f05cddf9SRui Paulo 		if (p2p->pending_action_state == P2P_PENDING_PD)
4179f05cddf9SRui Paulo 			p2p_timeout_prov_disc_req(p2p);
4180f05cddf9SRui Paulo 		if (p2p->search_delay && !p2p->in_search_delay) {
41815b9c547cSRui Paulo 			p2p_dbg(p2p, "Delay search operation by %u ms",
4182f05cddf9SRui Paulo 				p2p->search_delay);
4183f05cddf9SRui Paulo 			p2p->in_search_delay = 1;
4184f05cddf9SRui Paulo 			p2p_set_timeout(p2p, p2p->search_delay / 1000,
4185f05cddf9SRui Paulo 					(p2p->search_delay % 1000) * 1000);
4186f05cddf9SRui Paulo 			break;
4187f05cddf9SRui Paulo 		}
4188f05cddf9SRui Paulo 		p2p->in_search_delay = 0;
4189f05cddf9SRui Paulo 		p2p_search(p2p);
4190f05cddf9SRui Paulo 		break;
4191f05cddf9SRui Paulo 	case P2P_CONNECT:
4192f05cddf9SRui Paulo 		p2p_timeout_connect(p2p);
4193f05cddf9SRui Paulo 		break;
4194f05cddf9SRui Paulo 	case P2P_CONNECT_LISTEN:
4195f05cddf9SRui Paulo 		p2p_timeout_connect_listen(p2p);
4196f05cddf9SRui Paulo 		break;
4197f05cddf9SRui Paulo 	case P2P_GO_NEG:
4198f05cddf9SRui Paulo 		break;
4199f05cddf9SRui Paulo 	case P2P_LISTEN_ONLY:
4200f05cddf9SRui Paulo 		/* Check if we timed out waiting for PD req */
4201f05cddf9SRui Paulo 		if (p2p->pending_action_state == P2P_PENDING_PD)
4202f05cddf9SRui Paulo 			p2p_timeout_prov_disc_req(p2p);
4203f05cddf9SRui Paulo 
4204f05cddf9SRui Paulo 		if (p2p->ext_listen_only) {
42055b9c547cSRui Paulo 			p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
4206f05cddf9SRui Paulo 			p2p->ext_listen_only = 0;
4207f05cddf9SRui Paulo 			p2p_set_state(p2p, P2P_IDLE);
4208f05cddf9SRui Paulo 		}
4209f05cddf9SRui Paulo 		break;
4210f05cddf9SRui Paulo 	case P2P_WAIT_PEER_CONNECT:
4211f05cddf9SRui Paulo 		p2p_timeout_wait_peer_connect(p2p);
4212f05cddf9SRui Paulo 		break;
4213f05cddf9SRui Paulo 	case P2P_WAIT_PEER_IDLE:
4214f05cddf9SRui Paulo 		p2p_timeout_wait_peer_idle(p2p);
4215f05cddf9SRui Paulo 		break;
4216f05cddf9SRui Paulo 	case P2P_SD_DURING_FIND:
4217f05cddf9SRui Paulo 		p2p_timeout_sd_during_find(p2p);
4218f05cddf9SRui Paulo 		break;
4219f05cddf9SRui Paulo 	case P2P_PROVISIONING:
4220f05cddf9SRui Paulo 		break;
4221f05cddf9SRui Paulo 	case P2P_PD_DURING_FIND:
4222f05cddf9SRui Paulo 		p2p_timeout_prov_disc_during_find(p2p);
4223f05cddf9SRui Paulo 		break;
4224f05cddf9SRui Paulo 	case P2P_INVITE:
4225f05cddf9SRui Paulo 		p2p_timeout_invite(p2p);
4226f05cddf9SRui Paulo 		break;
4227f05cddf9SRui Paulo 	case P2P_INVITE_LISTEN:
4228f05cddf9SRui Paulo 		p2p_timeout_invite_listen(p2p);
4229f05cddf9SRui Paulo 		break;
4230f05cddf9SRui Paulo 	}
4231f05cddf9SRui Paulo }
4232f05cddf9SRui Paulo 
4233f05cddf9SRui Paulo 
p2p_reject(struct p2p_data * p2p,const u8 * peer_addr)4234f05cddf9SRui Paulo int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
4235f05cddf9SRui Paulo {
4236f05cddf9SRui Paulo 	struct p2p_device *dev;
4237f05cddf9SRui Paulo 
4238f05cddf9SRui Paulo 	dev = p2p_get_device(p2p, peer_addr);
42395b9c547cSRui Paulo 	p2p_dbg(p2p, "Local request to reject connection attempts by peer "
42405b9c547cSRui Paulo 		MACSTR, MAC2STR(peer_addr));
4241f05cddf9SRui Paulo 	if (dev == NULL) {
42425b9c547cSRui Paulo 		p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
4243f05cddf9SRui Paulo 		return -1;
4244f05cddf9SRui Paulo 	}
4245f05cddf9SRui Paulo 	dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
4246f05cddf9SRui Paulo 	dev->flags |= P2P_DEV_USER_REJECTED;
4247f05cddf9SRui Paulo 	return 0;
4248f05cddf9SRui Paulo }
4249f05cddf9SRui Paulo 
4250f05cddf9SRui Paulo 
p2p_wps_method_text(enum p2p_wps_method method)4251f05cddf9SRui Paulo const char * p2p_wps_method_text(enum p2p_wps_method method)
4252f05cddf9SRui Paulo {
4253f05cddf9SRui Paulo 	switch (method) {
4254f05cddf9SRui Paulo 	case WPS_NOT_READY:
4255f05cddf9SRui Paulo 		return "not-ready";
4256f05cddf9SRui Paulo 	case WPS_PIN_DISPLAY:
4257f05cddf9SRui Paulo 		return "Display";
4258f05cddf9SRui Paulo 	case WPS_PIN_KEYPAD:
4259f05cddf9SRui Paulo 		return "Keypad";
4260f05cddf9SRui Paulo 	case WPS_PBC:
4261f05cddf9SRui Paulo 		return "PBC";
42625b9c547cSRui Paulo 	case WPS_NFC:
42635b9c547cSRui Paulo 		return "NFC";
42645b9c547cSRui Paulo 	case WPS_P2PS:
42655b9c547cSRui Paulo 		return "P2PS";
4266f05cddf9SRui Paulo 	}
4267f05cddf9SRui Paulo 
4268f05cddf9SRui Paulo 	return "??";
4269f05cddf9SRui Paulo }
4270f05cddf9SRui Paulo 
4271f05cddf9SRui Paulo 
p2p_go_state_text(enum p2p_go_state go_state)4272f05cddf9SRui Paulo static const char * p2p_go_state_text(enum p2p_go_state go_state)
4273f05cddf9SRui Paulo {
4274f05cddf9SRui Paulo 	switch (go_state) {
4275f05cddf9SRui Paulo 	case UNKNOWN_GO:
4276f05cddf9SRui Paulo 		return "unknown";
4277f05cddf9SRui Paulo 	case LOCAL_GO:
4278f05cddf9SRui Paulo 		return "local";
4279f05cddf9SRui Paulo 	case  REMOTE_GO:
4280f05cddf9SRui Paulo 		return "remote";
4281f05cddf9SRui Paulo 	}
4282f05cddf9SRui Paulo 
4283f05cddf9SRui Paulo 	return "??";
4284f05cddf9SRui Paulo }
4285f05cddf9SRui Paulo 
4286f05cddf9SRui Paulo 
p2p_get_peer_info(struct p2p_data * p2p,const u8 * addr,int next)4287f05cddf9SRui Paulo const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
4288f05cddf9SRui Paulo 					       const u8 *addr, int next)
4289f05cddf9SRui Paulo {
4290f05cddf9SRui Paulo 	struct p2p_device *dev;
4291f05cddf9SRui Paulo 
4292f05cddf9SRui Paulo 	if (addr)
4293f05cddf9SRui Paulo 		dev = p2p_get_device(p2p, addr);
4294f05cddf9SRui Paulo 	else
4295f05cddf9SRui Paulo 		dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4296f05cddf9SRui Paulo 
4297f05cddf9SRui Paulo 	if (dev && next) {
4298f05cddf9SRui Paulo 		dev = dl_list_first(&dev->list, struct p2p_device, list);
4299f05cddf9SRui Paulo 		if (&dev->list == &p2p->devices)
4300f05cddf9SRui Paulo 			dev = NULL;
4301f05cddf9SRui Paulo 	}
4302f05cddf9SRui Paulo 
4303f05cddf9SRui Paulo 	if (dev == NULL)
4304f05cddf9SRui Paulo 		return NULL;
4305f05cddf9SRui Paulo 
4306f05cddf9SRui Paulo 	return &dev->info;
4307f05cddf9SRui Paulo }
4308f05cddf9SRui Paulo 
4309f05cddf9SRui Paulo 
p2p_get_peer_info_txt(const struct p2p_peer_info * info,char * buf,size_t buflen)4310f05cddf9SRui Paulo int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
4311f05cddf9SRui Paulo 			  char *buf, size_t buflen)
4312f05cddf9SRui Paulo {
4313f05cddf9SRui Paulo 	struct p2p_device *dev;
4314f05cddf9SRui Paulo 	int res;
4315f05cddf9SRui Paulo 	char *pos, *end;
43165b9c547cSRui Paulo 	struct os_reltime now;
4317f05cddf9SRui Paulo 
4318f05cddf9SRui Paulo 	if (info == NULL)
4319f05cddf9SRui Paulo 		return -1;
4320f05cddf9SRui Paulo 
4321f05cddf9SRui Paulo 	dev = (struct p2p_device *) (((u8 *) info) -
4322f05cddf9SRui Paulo 				     offsetof(struct p2p_device, info));
4323f05cddf9SRui Paulo 
4324f05cddf9SRui Paulo 	pos = buf;
4325f05cddf9SRui Paulo 	end = buf + buflen;
4326f05cddf9SRui Paulo 
43275b9c547cSRui Paulo 	os_get_reltime(&now);
4328f05cddf9SRui Paulo 	res = os_snprintf(pos, end - pos,
4329f05cddf9SRui Paulo 			  "age=%d\n"
4330f05cddf9SRui Paulo 			  "listen_freq=%d\n"
4331f05cddf9SRui Paulo 			  "wps_method=%s\n"
4332f05cddf9SRui Paulo 			  "interface_addr=" MACSTR "\n"
4333f05cddf9SRui Paulo 			  "member_in_go_dev=" MACSTR "\n"
4334f05cddf9SRui Paulo 			  "member_in_go_iface=" MACSTR "\n"
4335f05cddf9SRui Paulo 			  "go_neg_req_sent=%d\n"
4336f05cddf9SRui Paulo 			  "go_state=%s\n"
4337f05cddf9SRui Paulo 			  "dialog_token=%u\n"
4338f05cddf9SRui Paulo 			  "intended_addr=" MACSTR "\n"
4339f05cddf9SRui Paulo 			  "country=%c%c\n"
4340f05cddf9SRui Paulo 			  "oper_freq=%d\n"
4341f05cddf9SRui Paulo 			  "req_config_methods=0x%x\n"
4342325151a3SRui Paulo 			  "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
4343f05cddf9SRui Paulo 			  "status=%d\n"
4344f05cddf9SRui Paulo 			  "invitation_reqs=%u\n",
4345f05cddf9SRui Paulo 			  (int) (now.sec - dev->last_seen.sec),
4346f05cddf9SRui Paulo 			  dev->listen_freq,
4347f05cddf9SRui Paulo 			  p2p_wps_method_text(dev->wps_method),
4348f05cddf9SRui Paulo 			  MAC2STR(dev->interface_addr),
4349f05cddf9SRui Paulo 			  MAC2STR(dev->member_in_go_dev),
4350f05cddf9SRui Paulo 			  MAC2STR(dev->member_in_go_iface),
4351f05cddf9SRui Paulo 			  dev->go_neg_req_sent,
4352f05cddf9SRui Paulo 			  p2p_go_state_text(dev->go_state),
4353f05cddf9SRui Paulo 			  dev->dialog_token,
4354f05cddf9SRui Paulo 			  MAC2STR(dev->intended_addr),
4355f05cddf9SRui Paulo 			  dev->country[0] ? dev->country[0] : '_',
4356f05cddf9SRui Paulo 			  dev->country[1] ? dev->country[1] : '_',
4357f05cddf9SRui Paulo 			  dev->oper_freq,
4358f05cddf9SRui Paulo 			  dev->req_config_methods,
4359f05cddf9SRui Paulo 			  dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
4360f05cddf9SRui Paulo 			  "[PROBE_REQ_ONLY]" : "",
4361f05cddf9SRui Paulo 			  dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
4362f05cddf9SRui Paulo 			  dev->flags & P2P_DEV_NOT_YET_READY ?
4363f05cddf9SRui Paulo 			  "[NOT_YET_READY]" : "",
4364f05cddf9SRui Paulo 			  dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
4365f05cddf9SRui Paulo 			  "[PD_PEER_DISPLAY]" : "",
4366f05cddf9SRui Paulo 			  dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
4367f05cddf9SRui Paulo 			  "[PD_PEER_KEYPAD]" : "",
43685b9c547cSRui Paulo 			  dev->flags & P2P_DEV_PD_PEER_P2PS ?
43695b9c547cSRui Paulo 			  "[PD_PEER_P2PS]" : "",
4370f05cddf9SRui Paulo 			  dev->flags & P2P_DEV_USER_REJECTED ?
4371f05cddf9SRui Paulo 			  "[USER_REJECTED]" : "",
4372f05cddf9SRui Paulo 			  dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
4373f05cddf9SRui Paulo 			  "[PEER_WAITING_RESPONSE]" : "",
4374f05cddf9SRui Paulo 			  dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
4375f05cddf9SRui Paulo 			  "[PREFER_PERSISTENT_GROUP]" : "",
4376f05cddf9SRui Paulo 			  dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
4377f05cddf9SRui Paulo 			  "[WAIT_GO_NEG_RESPONSE]" : "",
4378f05cddf9SRui Paulo 			  dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
4379f05cddf9SRui Paulo 			  "[WAIT_GO_NEG_CONFIRM]" : "",
4380f05cddf9SRui Paulo 			  dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
4381f05cddf9SRui Paulo 			  "[GROUP_CLIENT_ONLY]" : "",
4382f05cddf9SRui Paulo 			  dev->flags & P2P_DEV_FORCE_FREQ ?
4383f05cddf9SRui Paulo 			  "[FORCE_FREQ]" : "",
4384f05cddf9SRui Paulo 			  dev->flags & P2P_DEV_PD_FOR_JOIN ?
4385f05cddf9SRui Paulo 			  "[PD_FOR_JOIN]" : "",
4386325151a3SRui Paulo 			  dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT ?
4387325151a3SRui Paulo 			  "[LAST_SEEN_AS_GROUP_CLIENT]" : "",
4388f05cddf9SRui Paulo 			  dev->status,
4389f05cddf9SRui Paulo 			  dev->invitation_reqs);
43905b9c547cSRui Paulo 	if (os_snprintf_error(end - pos, res))
4391f05cddf9SRui Paulo 		return pos - buf;
4392f05cddf9SRui Paulo 	pos += res;
4393f05cddf9SRui Paulo 
4394f05cddf9SRui Paulo 	if (dev->ext_listen_period) {
4395f05cddf9SRui Paulo 		res = os_snprintf(pos, end - pos,
4396f05cddf9SRui Paulo 				  "ext_listen_period=%u\n"
4397f05cddf9SRui Paulo 				  "ext_listen_interval=%u\n",
4398f05cddf9SRui Paulo 				  dev->ext_listen_period,
4399f05cddf9SRui Paulo 				  dev->ext_listen_interval);
44005b9c547cSRui Paulo 		if (os_snprintf_error(end - pos, res))
4401f05cddf9SRui Paulo 			return pos - buf;
4402f05cddf9SRui Paulo 		pos += res;
4403f05cddf9SRui Paulo 	}
4404f05cddf9SRui Paulo 
4405f05cddf9SRui Paulo 	if (dev->oper_ssid_len) {
4406f05cddf9SRui Paulo 		res = os_snprintf(pos, end - pos,
4407f05cddf9SRui Paulo 				  "oper_ssid=%s\n",
4408f05cddf9SRui Paulo 				  wpa_ssid_txt(dev->oper_ssid,
4409f05cddf9SRui Paulo 					       dev->oper_ssid_len));
44105b9c547cSRui Paulo 		if (os_snprintf_error(end - pos, res))
4411f05cddf9SRui Paulo 			return pos - buf;
4412f05cddf9SRui Paulo 		pos += res;
4413f05cddf9SRui Paulo 	}
4414f05cddf9SRui Paulo 
4415f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
4416f05cddf9SRui Paulo 	if (dev->info.wfd_subelems) {
4417f05cddf9SRui Paulo 		res = os_snprintf(pos, end - pos, "wfd_subelems=");
44185b9c547cSRui Paulo 		if (os_snprintf_error(end - pos, res))
4419f05cddf9SRui Paulo 			return pos - buf;
4420f05cddf9SRui Paulo 		pos += res;
4421f05cddf9SRui Paulo 
4422f05cddf9SRui Paulo 		pos += wpa_snprintf_hex(pos, end - pos,
4423f05cddf9SRui Paulo 					wpabuf_head(dev->info.wfd_subelems),
4424f05cddf9SRui Paulo 					wpabuf_len(dev->info.wfd_subelems));
4425f05cddf9SRui Paulo 
4426f05cddf9SRui Paulo 		res = os_snprintf(pos, end - pos, "\n");
44275b9c547cSRui Paulo 		if (os_snprintf_error(end - pos, res))
4428f05cddf9SRui Paulo 			return pos - buf;
4429f05cddf9SRui Paulo 		pos += res;
4430f05cddf9SRui Paulo 	}
4431f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
4432f05cddf9SRui Paulo 
4433f05cddf9SRui Paulo 	return pos - buf;
4434f05cddf9SRui Paulo }
4435f05cddf9SRui Paulo 
4436f05cddf9SRui Paulo 
p2p_peer_known(struct p2p_data * p2p,const u8 * addr)4437f05cddf9SRui Paulo int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
4438f05cddf9SRui Paulo {
4439f05cddf9SRui Paulo 	return p2p_get_device(p2p, addr) != NULL;
4440f05cddf9SRui Paulo }
4441f05cddf9SRui Paulo 
4442f05cddf9SRui Paulo 
p2p_set_client_discoverability(struct p2p_data * p2p,int enabled)4443f05cddf9SRui Paulo void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
4444f05cddf9SRui Paulo {
4445f05cddf9SRui Paulo 	if (enabled) {
44465b9c547cSRui Paulo 		p2p_dbg(p2p, "Client discoverability enabled");
4447f05cddf9SRui Paulo 		p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4448f05cddf9SRui Paulo 	} else {
44495b9c547cSRui Paulo 		p2p_dbg(p2p, "Client discoverability disabled");
4450f05cddf9SRui Paulo 		p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4451f05cddf9SRui Paulo 	}
4452f05cddf9SRui Paulo }
4453f05cddf9SRui Paulo 
4454f05cddf9SRui Paulo 
p2p_build_presence_req(u32 duration1,u32 interval1,u32 duration2,u32 interval2)4455f05cddf9SRui Paulo static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
4456f05cddf9SRui Paulo 					      u32 duration2, u32 interval2)
4457f05cddf9SRui Paulo {
4458f05cddf9SRui Paulo 	struct wpabuf *req;
4459f05cddf9SRui Paulo 	struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
4460f05cddf9SRui Paulo 	u8 *len;
4461f05cddf9SRui Paulo 
4462f05cddf9SRui Paulo 	req = wpabuf_alloc(100);
4463f05cddf9SRui Paulo 	if (req == NULL)
4464f05cddf9SRui Paulo 		return NULL;
4465f05cddf9SRui Paulo 
4466f05cddf9SRui Paulo 	if (duration1 || interval1) {
4467f05cddf9SRui Paulo 		os_memset(&desc1, 0, sizeof(desc1));
4468f05cddf9SRui Paulo 		desc1.count_type = 1;
4469f05cddf9SRui Paulo 		desc1.duration = duration1;
4470f05cddf9SRui Paulo 		desc1.interval = interval1;
4471f05cddf9SRui Paulo 		ptr1 = &desc1;
4472f05cddf9SRui Paulo 
4473f05cddf9SRui Paulo 		if (duration2 || interval2) {
4474f05cddf9SRui Paulo 			os_memset(&desc2, 0, sizeof(desc2));
4475f05cddf9SRui Paulo 			desc2.count_type = 2;
4476f05cddf9SRui Paulo 			desc2.duration = duration2;
4477f05cddf9SRui Paulo 			desc2.interval = interval2;
4478f05cddf9SRui Paulo 			ptr2 = &desc2;
4479f05cddf9SRui Paulo 		}
4480f05cddf9SRui Paulo 	}
4481f05cddf9SRui Paulo 
4482f05cddf9SRui Paulo 	p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
4483f05cddf9SRui Paulo 	len = p2p_buf_add_ie_hdr(req);
4484f05cddf9SRui Paulo 	p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
4485f05cddf9SRui Paulo 	p2p_buf_update_ie_hdr(req, len);
4486f05cddf9SRui Paulo 
4487f05cddf9SRui Paulo 	return req;
4488f05cddf9SRui Paulo }
4489f05cddf9SRui Paulo 
4490f05cddf9SRui Paulo 
p2p_presence_req(struct p2p_data * p2p,const u8 * go_interface_addr,const u8 * own_interface_addr,unsigned int freq,u32 duration1,u32 interval1,u32 duration2,u32 interval2)4491f05cddf9SRui Paulo int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
4492f05cddf9SRui Paulo 		     const u8 *own_interface_addr, unsigned int freq,
4493f05cddf9SRui Paulo 		     u32 duration1, u32 interval1, u32 duration2,
4494f05cddf9SRui Paulo 		     u32 interval2)
4495f05cddf9SRui Paulo {
4496f05cddf9SRui Paulo 	struct wpabuf *req;
4497f05cddf9SRui Paulo 
44985b9c547cSRui Paulo 	p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
44995b9c547cSRui Paulo 		" (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
45005b9c547cSRui Paulo 		"dur2=%u int2=%u",
4501f05cddf9SRui Paulo 		MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
4502f05cddf9SRui Paulo 		freq, duration1, interval1, duration2, interval2);
4503f05cddf9SRui Paulo 
4504f05cddf9SRui Paulo 	req = p2p_build_presence_req(duration1, interval1, duration2,
4505f05cddf9SRui Paulo 				     interval2);
4506f05cddf9SRui Paulo 	if (req == NULL)
4507f05cddf9SRui Paulo 		return -1;
4508f05cddf9SRui Paulo 
4509f05cddf9SRui Paulo 	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4510f05cddf9SRui Paulo 	if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
4511f05cddf9SRui Paulo 			    go_interface_addr,
4512f05cddf9SRui Paulo 			    wpabuf_head(req), wpabuf_len(req), 200) < 0) {
45135b9c547cSRui Paulo 		p2p_dbg(p2p, "Failed to send Action frame");
4514f05cddf9SRui Paulo 	}
4515f05cddf9SRui Paulo 	wpabuf_free(req);
4516f05cddf9SRui Paulo 
4517f05cddf9SRui Paulo 	return 0;
4518f05cddf9SRui Paulo }
4519f05cddf9SRui Paulo 
4520f05cddf9SRui Paulo 
p2p_build_presence_resp(u8 status,const u8 * noa,size_t noa_len,u8 dialog_token)4521f05cddf9SRui Paulo static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
4522f05cddf9SRui Paulo 					       size_t noa_len, u8 dialog_token)
4523f05cddf9SRui Paulo {
4524f05cddf9SRui Paulo 	struct wpabuf *resp;
4525f05cddf9SRui Paulo 	u8 *len;
4526f05cddf9SRui Paulo 
4527f05cddf9SRui Paulo 	resp = wpabuf_alloc(100 + noa_len);
4528f05cddf9SRui Paulo 	if (resp == NULL)
4529f05cddf9SRui Paulo 		return NULL;
4530f05cddf9SRui Paulo 
4531f05cddf9SRui Paulo 	p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
4532f05cddf9SRui Paulo 	len = p2p_buf_add_ie_hdr(resp);
4533f05cddf9SRui Paulo 	p2p_buf_add_status(resp, status);
4534f05cddf9SRui Paulo 	if (noa) {
4535f05cddf9SRui Paulo 		wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
4536f05cddf9SRui Paulo 		wpabuf_put_le16(resp, noa_len);
4537f05cddf9SRui Paulo 		wpabuf_put_data(resp, noa, noa_len);
4538f05cddf9SRui Paulo 	} else
4539f05cddf9SRui Paulo 		p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
4540f05cddf9SRui Paulo 	p2p_buf_update_ie_hdr(resp, len);
4541f05cddf9SRui Paulo 
4542f05cddf9SRui Paulo 	return resp;
4543f05cddf9SRui Paulo }
4544f05cddf9SRui Paulo 
4545f05cddf9SRui Paulo 
p2p_process_presence_req(struct p2p_data * p2p,const u8 * da,const u8 * sa,const u8 * data,size_t len,int rx_freq)4546f05cddf9SRui Paulo static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
4547f05cddf9SRui Paulo 				     const u8 *sa, const u8 *data, size_t len,
4548f05cddf9SRui Paulo 				     int rx_freq)
4549f05cddf9SRui Paulo {
4550f05cddf9SRui Paulo 	struct p2p_message msg;
4551f05cddf9SRui Paulo 	u8 status;
4552f05cddf9SRui Paulo 	struct wpabuf *resp;
4553f05cddf9SRui Paulo 	size_t g;
4554f05cddf9SRui Paulo 	struct p2p_group *group = NULL;
4555f05cddf9SRui Paulo 	int parsed = 0;
4556f05cddf9SRui Paulo 	u8 noa[50];
4557f05cddf9SRui Paulo 	int noa_len;
4558f05cddf9SRui Paulo 
45595b9c547cSRui Paulo 	p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
4560f05cddf9SRui Paulo 
4561f05cddf9SRui Paulo 	for (g = 0; g < p2p->num_groups; g++) {
4562*a90b9d01SCy Schubert 		if (ether_addr_equal(
4563*a90b9d01SCy Schubert 			    da, p2p_group_get_interface_addr(p2p->groups[g]))) {
4564f05cddf9SRui Paulo 			group = p2p->groups[g];
4565f05cddf9SRui Paulo 			break;
4566f05cddf9SRui Paulo 		}
4567f05cddf9SRui Paulo 	}
4568f05cddf9SRui Paulo 	if (group == NULL) {
45695b9c547cSRui Paulo 		p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
4570f05cddf9SRui Paulo 			MACSTR, MAC2STR(da));
4571f05cddf9SRui Paulo 		return;
4572f05cddf9SRui Paulo 	}
4573f05cddf9SRui Paulo 
4574f05cddf9SRui Paulo 	if (p2p_parse(data, len, &msg) < 0) {
45755b9c547cSRui Paulo 		p2p_dbg(p2p, "Failed to parse P2P Presence Request");
4576f05cddf9SRui Paulo 		status = P2P_SC_FAIL_INVALID_PARAMS;
4577f05cddf9SRui Paulo 		goto fail;
4578f05cddf9SRui Paulo 	}
4579f05cddf9SRui Paulo 	parsed = 1;
4580f05cddf9SRui Paulo 
4581f05cddf9SRui Paulo 	if (msg.noa == NULL) {
45825b9c547cSRui Paulo 		p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
4583f05cddf9SRui Paulo 		status = P2P_SC_FAIL_INVALID_PARAMS;
4584f05cddf9SRui Paulo 		goto fail;
4585f05cddf9SRui Paulo 	}
4586f05cddf9SRui Paulo 
4587f05cddf9SRui Paulo 	status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
4588f05cddf9SRui Paulo 
4589f05cddf9SRui Paulo fail:
4590f05cddf9SRui Paulo 	if (p2p->cfg->get_noa)
4591f05cddf9SRui Paulo 		noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
4592f05cddf9SRui Paulo 					    sizeof(noa));
4593f05cddf9SRui Paulo 	else
4594f05cddf9SRui Paulo 		noa_len = -1;
4595f05cddf9SRui Paulo 	resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
4596f05cddf9SRui Paulo 				       noa_len > 0 ? noa_len : 0,
4597f05cddf9SRui Paulo 				       msg.dialog_token);
4598f05cddf9SRui Paulo 	if (parsed)
4599f05cddf9SRui Paulo 		p2p_parse_free(&msg);
4600f05cddf9SRui Paulo 	if (resp == NULL)
4601f05cddf9SRui Paulo 		return;
4602f05cddf9SRui Paulo 
4603f05cddf9SRui Paulo 	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4604f05cddf9SRui Paulo 	if (p2p_send_action(p2p, rx_freq, sa, da, da,
4605f05cddf9SRui Paulo 			    wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
46065b9c547cSRui Paulo 		p2p_dbg(p2p, "Failed to send Action frame");
4607f05cddf9SRui Paulo 	}
4608f05cddf9SRui Paulo 	wpabuf_free(resp);
4609f05cddf9SRui Paulo }
4610f05cddf9SRui Paulo 
4611f05cddf9SRui Paulo 
p2p_process_presence_resp(struct p2p_data * p2p,const u8 * da,const u8 * sa,const u8 * data,size_t len)4612f05cddf9SRui Paulo static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
4613f05cddf9SRui Paulo 				      const u8 *sa, const u8 *data, size_t len)
4614f05cddf9SRui Paulo {
4615f05cddf9SRui Paulo 	struct p2p_message msg;
4616f05cddf9SRui Paulo 
46175b9c547cSRui Paulo 	p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
4618f05cddf9SRui Paulo 
4619f05cddf9SRui Paulo 	if (p2p_parse(data, len, &msg) < 0) {
46205b9c547cSRui Paulo 		p2p_dbg(p2p, "Failed to parse P2P Presence Response");
4621f05cddf9SRui Paulo 		return;
4622f05cddf9SRui Paulo 	}
4623f05cddf9SRui Paulo 
4624f05cddf9SRui Paulo 	if (msg.status == NULL || msg.noa == NULL) {
46255b9c547cSRui Paulo 		p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
4626f05cddf9SRui Paulo 		p2p_parse_free(&msg);
4627f05cddf9SRui Paulo 		return;
4628f05cddf9SRui Paulo 	}
4629f05cddf9SRui Paulo 
46305b9c547cSRui Paulo 	if (p2p->cfg->presence_resp) {
46315b9c547cSRui Paulo 		p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
46325b9c547cSRui Paulo 					msg.noa, msg.noa_len);
46335b9c547cSRui Paulo 	}
46345b9c547cSRui Paulo 
4635f05cddf9SRui Paulo 	if (*msg.status) {
46365b9c547cSRui Paulo 		p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
4637f05cddf9SRui Paulo 			*msg.status);
4638f05cddf9SRui Paulo 		p2p_parse_free(&msg);
4639f05cddf9SRui Paulo 		return;
4640f05cddf9SRui Paulo 	}
4641f05cddf9SRui Paulo 
46425b9c547cSRui Paulo 	p2p_dbg(p2p, "P2P Presence Request was accepted");
4643f05cddf9SRui Paulo 	wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
4644f05cddf9SRui Paulo 		    msg.noa, msg.noa_len);
4645f05cddf9SRui Paulo 	/* TODO: process NoA */
4646f05cddf9SRui Paulo 	p2p_parse_free(&msg);
4647f05cddf9SRui Paulo }
4648f05cddf9SRui Paulo 
4649f05cddf9SRui Paulo 
p2p_ext_listen_timeout(void * eloop_ctx,void * timeout_ctx)4650f05cddf9SRui Paulo static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4651f05cddf9SRui Paulo {
4652f05cddf9SRui Paulo 	struct p2p_data *p2p = eloop_ctx;
4653f05cddf9SRui Paulo 
4654f05cddf9SRui Paulo 	if (p2p->ext_listen_interval) {
4655f05cddf9SRui Paulo 		/* Schedule next extended listen timeout */
4656f05cddf9SRui Paulo 		eloop_register_timeout(p2p->ext_listen_interval_sec,
4657f05cddf9SRui Paulo 				       p2p->ext_listen_interval_usec,
4658f05cddf9SRui Paulo 				       p2p_ext_listen_timeout, p2p, NULL);
4659f05cddf9SRui Paulo 	}
4660f05cddf9SRui Paulo 
4661f05cddf9SRui Paulo 	if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
4662f05cddf9SRui Paulo 		/*
4663f05cddf9SRui Paulo 		 * This should not really happen, but it looks like the Listen
4664f05cddf9SRui Paulo 		 * command may fail is something else (e.g., a scan) was
4665f05cddf9SRui Paulo 		 * running at an inconvenient time. As a workaround, allow new
4666f05cddf9SRui Paulo 		 * Extended Listen operation to be started.
4667f05cddf9SRui Paulo 		 */
46685b9c547cSRui Paulo 		p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
4669f05cddf9SRui Paulo 		p2p->ext_listen_only = 0;
4670f05cddf9SRui Paulo 		p2p_set_state(p2p, P2P_IDLE);
4671f05cddf9SRui Paulo 	}
4672f05cddf9SRui Paulo 
4673*a90b9d01SCy Schubert 	if ((p2p->cfg->is_p2p_in_progress &&
4674*a90b9d01SCy Schubert 	     p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) ||
4675*a90b9d01SCy Schubert 	    (p2p->pending_action_state == P2P_PENDING_PD &&
4676*a90b9d01SCy Schubert 	     p2p->pd_retries > 0)) {
4677*a90b9d01SCy Schubert 		p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)",
4678*a90b9d01SCy Schubert 			p2p_state_txt(p2p->state));
4679*a90b9d01SCy Schubert 		return;
4680*a90b9d01SCy Schubert 	}
4681*a90b9d01SCy Schubert 
4682f05cddf9SRui Paulo 	if (p2p->state != P2P_IDLE) {
46835b9c547cSRui Paulo 		p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
4684f05cddf9SRui Paulo 		return;
4685f05cddf9SRui Paulo 	}
4686f05cddf9SRui Paulo 
46875b9c547cSRui Paulo 	p2p_dbg(p2p, "Extended Listen timeout");
4688f05cddf9SRui Paulo 	p2p->ext_listen_only = 1;
4689f05cddf9SRui Paulo 	if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
46905b9c547cSRui Paulo 		p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
4691f05cddf9SRui Paulo 		p2p->ext_listen_only = 0;
4692f05cddf9SRui Paulo 	}
4693f05cddf9SRui Paulo }
4694f05cddf9SRui Paulo 
4695f05cddf9SRui Paulo 
p2p_ext_listen(struct p2p_data * p2p,unsigned int period,unsigned int interval)4696f05cddf9SRui Paulo int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
4697f05cddf9SRui Paulo 		   unsigned int interval)
4698f05cddf9SRui Paulo {
4699f05cddf9SRui Paulo 	if (period > 65535 || interval > 65535 || period > interval ||
4700f05cddf9SRui Paulo 	    (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
47015b9c547cSRui Paulo 		p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
47025b9c547cSRui Paulo 			period, interval);
4703f05cddf9SRui Paulo 		return -1;
4704f05cddf9SRui Paulo 	}
4705f05cddf9SRui Paulo 
4706f05cddf9SRui Paulo 	eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
4707f05cddf9SRui Paulo 
4708f05cddf9SRui Paulo 	if (interval == 0) {
47095b9c547cSRui Paulo 		p2p_dbg(p2p, "Disabling Extended Listen Timing");
4710f05cddf9SRui Paulo 		p2p->ext_listen_period = 0;
4711f05cddf9SRui Paulo 		p2p->ext_listen_interval = 0;
4712f05cddf9SRui Paulo 		return 0;
4713f05cddf9SRui Paulo 	}
4714f05cddf9SRui Paulo 
47155b9c547cSRui Paulo 	p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
47165b9c547cSRui Paulo 		period, interval);
4717f05cddf9SRui Paulo 	p2p->ext_listen_period = period;
4718f05cddf9SRui Paulo 	p2p->ext_listen_interval = interval;
4719f05cddf9SRui Paulo 	p2p->ext_listen_interval_sec = interval / 1000;
4720f05cddf9SRui Paulo 	p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
4721f05cddf9SRui Paulo 
4722f05cddf9SRui Paulo 	eloop_register_timeout(p2p->ext_listen_interval_sec,
4723f05cddf9SRui Paulo 			       p2p->ext_listen_interval_usec,
4724f05cddf9SRui Paulo 			       p2p_ext_listen_timeout, p2p, NULL);
4725f05cddf9SRui Paulo 
4726f05cddf9SRui Paulo 	return 0;
4727f05cddf9SRui Paulo }
4728f05cddf9SRui Paulo 
4729f05cddf9SRui Paulo 
p2p_deauth_notif(struct p2p_data * p2p,const u8 * bssid,u16 reason_code,const u8 * ie,size_t ie_len)4730f05cddf9SRui Paulo void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4731f05cddf9SRui Paulo 		      const u8 *ie, size_t ie_len)
4732f05cddf9SRui Paulo {
4733f05cddf9SRui Paulo 	struct p2p_message msg;
4734f05cddf9SRui Paulo 
4735f05cddf9SRui Paulo 	if (bssid == NULL || ie == NULL)
4736f05cddf9SRui Paulo 		return;
4737f05cddf9SRui Paulo 
4738f05cddf9SRui Paulo 	os_memset(&msg, 0, sizeof(msg));
4739f05cddf9SRui Paulo 	if (p2p_parse_ies(ie, ie_len, &msg))
4740f05cddf9SRui Paulo 		return;
47415b9c547cSRui Paulo 	if (msg.minor_reason_code == NULL) {
47425b9c547cSRui Paulo 		p2p_parse_free(&msg);
4743f05cddf9SRui Paulo 		return;
47445b9c547cSRui Paulo 	}
4745f05cddf9SRui Paulo 
47465b9c547cSRui Paulo 	p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
4747f05cddf9SRui Paulo 		" reason_code=%u minor_reason_code=%u",
4748f05cddf9SRui Paulo 		MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4749f05cddf9SRui Paulo 
4750f05cddf9SRui Paulo 	p2p_parse_free(&msg);
4751f05cddf9SRui Paulo }
4752f05cddf9SRui Paulo 
4753f05cddf9SRui Paulo 
p2p_disassoc_notif(struct p2p_data * p2p,const u8 * bssid,u16 reason_code,const u8 * ie,size_t ie_len)4754f05cddf9SRui Paulo void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4755f05cddf9SRui Paulo 			const u8 *ie, size_t ie_len)
4756f05cddf9SRui Paulo {
4757f05cddf9SRui Paulo 	struct p2p_message msg;
4758f05cddf9SRui Paulo 
4759f05cddf9SRui Paulo 	if (bssid == NULL || ie == NULL)
4760f05cddf9SRui Paulo 		return;
4761f05cddf9SRui Paulo 
4762f05cddf9SRui Paulo 	os_memset(&msg, 0, sizeof(msg));
4763f05cddf9SRui Paulo 	if (p2p_parse_ies(ie, ie_len, &msg))
4764f05cddf9SRui Paulo 		return;
47655b9c547cSRui Paulo 	if (msg.minor_reason_code == NULL) {
47665b9c547cSRui Paulo 		p2p_parse_free(&msg);
4767f05cddf9SRui Paulo 		return;
47685b9c547cSRui Paulo 	}
4769f05cddf9SRui Paulo 
47705b9c547cSRui Paulo 	p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
4771f05cddf9SRui Paulo 		" reason_code=%u minor_reason_code=%u",
4772f05cddf9SRui Paulo 		MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4773f05cddf9SRui Paulo 
4774f05cddf9SRui Paulo 	p2p_parse_free(&msg);
4775f05cddf9SRui Paulo }
4776f05cddf9SRui Paulo 
4777f05cddf9SRui Paulo 
p2p_set_managed_oper(struct p2p_data * p2p,int enabled)4778f05cddf9SRui Paulo void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
4779f05cddf9SRui Paulo {
4780f05cddf9SRui Paulo 	if (enabled) {
47815b9c547cSRui Paulo 		p2p_dbg(p2p, "Managed P2P Device operations enabled");
4782f05cddf9SRui Paulo 		p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
4783f05cddf9SRui Paulo 	} else {
47845b9c547cSRui Paulo 		p2p_dbg(p2p, "Managed P2P Device operations disabled");
4785f05cddf9SRui Paulo 		p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
4786f05cddf9SRui Paulo 	}
4787f05cddf9SRui Paulo }
4788f05cddf9SRui Paulo 
4789f05cddf9SRui Paulo 
p2p_config_get_random_social(struct p2p_config * p2p,u8 * op_class,u8 * op_channel,struct wpa_freq_range_list * avoid_list,struct wpa_freq_range_list * disallow_list)47905b9c547cSRui Paulo int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class,
47914bc52338SCy Schubert 				 u8 *op_channel,
47924bc52338SCy Schubert 				 struct wpa_freq_range_list *avoid_list,
47934bc52338SCy Schubert 				 struct wpa_freq_range_list *disallow_list)
4794f05cddf9SRui Paulo {
47954bc52338SCy Schubert 	return p2p_channel_random_social(&p2p->channels, op_class, op_channel,
47964bc52338SCy Schubert 					 avoid_list, disallow_list);
47975b9c547cSRui Paulo }
47985b9c547cSRui Paulo 
47995b9c547cSRui Paulo 
p2p_set_listen_channel(struct p2p_data * p2p,u8 reg_class,u8 channel,u8 forced)48005b9c547cSRui Paulo int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel,
48015b9c547cSRui Paulo 			   u8 forced)
48025b9c547cSRui Paulo {
48035b9c547cSRui Paulo 	if (p2p_channel_to_freq(reg_class, channel) < 0)
4804f05cddf9SRui Paulo 		return -1;
4805f05cddf9SRui Paulo 
48065b9c547cSRui Paulo 	/*
48075b9c547cSRui Paulo 	 * Listen channel was set in configuration or set by control interface;
48085b9c547cSRui Paulo 	 * cannot override it.
48095b9c547cSRui Paulo 	 */
48105b9c547cSRui Paulo 	if (p2p->cfg->channel_forced && forced == 0) {
48115b9c547cSRui Paulo 		p2p_dbg(p2p,
48125b9c547cSRui Paulo 			"Listen channel was previously configured - do not override based on optimization");
48135b9c547cSRui Paulo 		return -1;
48145b9c547cSRui Paulo 	}
48155b9c547cSRui Paulo 
48165b9c547cSRui Paulo 	p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
48175b9c547cSRui Paulo 		reg_class, channel);
48185b9c547cSRui Paulo 
48195b9c547cSRui Paulo 	if (p2p->state == P2P_IDLE) {
4820f05cddf9SRui Paulo 		p2p->cfg->reg_class = reg_class;
4821f05cddf9SRui Paulo 		p2p->cfg->channel = channel;
48225b9c547cSRui Paulo 		p2p->cfg->channel_forced = forced;
48235b9c547cSRui Paulo 	} else {
48245b9c547cSRui Paulo 		p2p_dbg(p2p, "Defer setting listen channel");
48255b9c547cSRui Paulo 		p2p->pending_reg_class = reg_class;
48265b9c547cSRui Paulo 		p2p->pending_channel = channel;
48275b9c547cSRui Paulo 		p2p->pending_channel_forced = forced;
48285b9c547cSRui Paulo 	}
4829f05cddf9SRui Paulo 
4830f05cddf9SRui Paulo 	return 0;
4831f05cddf9SRui Paulo }
4832f05cddf9SRui Paulo 
4833f05cddf9SRui Paulo 
p2p_get_listen_channel(struct p2p_data * p2p)48345b9c547cSRui Paulo u8 p2p_get_listen_channel(struct p2p_data *p2p)
48355b9c547cSRui Paulo {
48365b9c547cSRui Paulo 	return p2p->cfg->channel;
48375b9c547cSRui Paulo }
48385b9c547cSRui Paulo 
48395b9c547cSRui Paulo 
p2p_set_ssid_postfix(struct p2p_data * p2p,const u8 * postfix,size_t len)4840f05cddf9SRui Paulo int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
4841f05cddf9SRui Paulo {
48425b9c547cSRui Paulo 	p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
4843f05cddf9SRui Paulo 	if (postfix == NULL) {
4844f05cddf9SRui Paulo 		p2p->cfg->ssid_postfix_len = 0;
4845f05cddf9SRui Paulo 		return 0;
4846f05cddf9SRui Paulo 	}
4847f05cddf9SRui Paulo 	if (len > sizeof(p2p->cfg->ssid_postfix))
4848f05cddf9SRui Paulo 		return -1;
4849f05cddf9SRui Paulo 	os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4850f05cddf9SRui Paulo 	p2p->cfg->ssid_postfix_len = len;
4851f05cddf9SRui Paulo 	return 0;
4852f05cddf9SRui Paulo }
4853f05cddf9SRui Paulo 
4854f05cddf9SRui Paulo 
p2p_set_oper_channel(struct p2p_data * p2p,u8 op_reg_class,u8 op_channel,int cfg_op_channel)4855f05cddf9SRui Paulo int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4856f05cddf9SRui Paulo 			 int cfg_op_channel)
4857f05cddf9SRui Paulo {
48585b9c547cSRui Paulo 	if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
4859f05cddf9SRui Paulo 		return -1;
4860f05cddf9SRui Paulo 
48615b9c547cSRui Paulo 	p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
48625b9c547cSRui Paulo 		op_reg_class, op_channel);
4863f05cddf9SRui Paulo 	p2p->cfg->op_reg_class = op_reg_class;
4864f05cddf9SRui Paulo 	p2p->cfg->op_channel = op_channel;
4865f05cddf9SRui Paulo 	p2p->cfg->cfg_op_channel = cfg_op_channel;
4866f05cddf9SRui Paulo 	return 0;
4867f05cddf9SRui Paulo }
4868f05cddf9SRui Paulo 
4869f05cddf9SRui Paulo 
p2p_set_pref_chan(struct p2p_data * p2p,unsigned int num_pref_chan,const struct p2p_channel * pref_chan)4870f05cddf9SRui Paulo int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4871f05cddf9SRui Paulo 		      const struct p2p_channel *pref_chan)
4872f05cddf9SRui Paulo {
4873f05cddf9SRui Paulo 	struct p2p_channel *n;
4874f05cddf9SRui Paulo 
4875f05cddf9SRui Paulo 	if (pref_chan) {
487685732ac8SCy Schubert 		n = os_memdup(pref_chan,
487785732ac8SCy Schubert 			      num_pref_chan * sizeof(struct p2p_channel));
4878f05cddf9SRui Paulo 		if (n == NULL)
4879f05cddf9SRui Paulo 			return -1;
4880f05cddf9SRui Paulo 	} else
4881f05cddf9SRui Paulo 		n = NULL;
4882f05cddf9SRui Paulo 
4883f05cddf9SRui Paulo 	os_free(p2p->cfg->pref_chan);
4884f05cddf9SRui Paulo 	p2p->cfg->pref_chan = n;
4885f05cddf9SRui Paulo 	p2p->cfg->num_pref_chan = num_pref_chan;
4886f05cddf9SRui Paulo 
4887f05cddf9SRui Paulo 	return 0;
4888f05cddf9SRui Paulo }
4889f05cddf9SRui Paulo 
4890f05cddf9SRui Paulo 
p2p_set_no_go_freq(struct p2p_data * p2p,const struct wpa_freq_range_list * list)48915b9c547cSRui Paulo int p2p_set_no_go_freq(struct p2p_data *p2p,
48925b9c547cSRui Paulo 		       const struct wpa_freq_range_list *list)
48935b9c547cSRui Paulo {
48945b9c547cSRui Paulo 	struct wpa_freq_range *tmp;
48955b9c547cSRui Paulo 
48965b9c547cSRui Paulo 	if (list == NULL || list->num == 0) {
48975b9c547cSRui Paulo 		os_free(p2p->no_go_freq.range);
48985b9c547cSRui Paulo 		p2p->no_go_freq.range = NULL;
48995b9c547cSRui Paulo 		p2p->no_go_freq.num = 0;
49005b9c547cSRui Paulo 		return 0;
49015b9c547cSRui Paulo 	}
49025b9c547cSRui Paulo 
49035b9c547cSRui Paulo 	tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
49045b9c547cSRui Paulo 	if (tmp == NULL)
49055b9c547cSRui Paulo 		return -1;
49065b9c547cSRui Paulo 	os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
49075b9c547cSRui Paulo 	os_free(p2p->no_go_freq.range);
49085b9c547cSRui Paulo 	p2p->no_go_freq.range = tmp;
49095b9c547cSRui Paulo 	p2p->no_go_freq.num = list->num;
49105b9c547cSRui Paulo 	p2p_dbg(p2p, "Updated no GO chan list");
49115b9c547cSRui Paulo 
49125b9c547cSRui Paulo 	return 0;
49135b9c547cSRui Paulo }
49145b9c547cSRui Paulo 
49155b9c547cSRui Paulo 
p2p_get_interface_addr(struct p2p_data * p2p,const u8 * dev_addr,u8 * iface_addr)4916f05cddf9SRui Paulo int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4917f05cddf9SRui Paulo 			   u8 *iface_addr)
4918f05cddf9SRui Paulo {
4919f05cddf9SRui Paulo 	struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4920f05cddf9SRui Paulo 	if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4921f05cddf9SRui Paulo 		return -1;
4922f05cddf9SRui Paulo 	os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4923f05cddf9SRui Paulo 	return 0;
4924f05cddf9SRui Paulo }
4925f05cddf9SRui Paulo 
4926f05cddf9SRui Paulo 
p2p_get_dev_addr(struct p2p_data * p2p,const u8 * iface_addr,u8 * dev_addr)4927f05cddf9SRui Paulo int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4928f05cddf9SRui Paulo 			   u8 *dev_addr)
4929f05cddf9SRui Paulo {
4930f05cddf9SRui Paulo 	struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4931f05cddf9SRui Paulo 	if (dev == NULL)
4932f05cddf9SRui Paulo 		return -1;
4933f05cddf9SRui Paulo 	os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4934f05cddf9SRui Paulo 	return 0;
4935f05cddf9SRui Paulo }
4936f05cddf9SRui Paulo 
4937f05cddf9SRui Paulo 
p2p_set_peer_filter(struct p2p_data * p2p,const u8 * addr)4938f05cddf9SRui Paulo void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4939f05cddf9SRui Paulo {
4940f05cddf9SRui Paulo 	os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4941f05cddf9SRui Paulo 	if (is_zero_ether_addr(p2p->peer_filter))
49425b9c547cSRui Paulo 		p2p_dbg(p2p, "Disable peer filter");
4943f05cddf9SRui Paulo 	else
49445b9c547cSRui Paulo 		p2p_dbg(p2p, "Enable peer filter for " MACSTR,
49455b9c547cSRui Paulo 			MAC2STR(p2p->peer_filter));
4946f05cddf9SRui Paulo }
4947f05cddf9SRui Paulo 
4948f05cddf9SRui Paulo 
p2p_set_cross_connect(struct p2p_data * p2p,int enabled)4949f05cddf9SRui Paulo void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4950f05cddf9SRui Paulo {
49515b9c547cSRui Paulo 	p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
4952f05cddf9SRui Paulo 	if (p2p->cross_connect == enabled)
4953f05cddf9SRui Paulo 		return;
4954f05cddf9SRui Paulo 	p2p->cross_connect = enabled;
4955f05cddf9SRui Paulo 	/* TODO: may need to tear down any action group where we are GO(?) */
4956f05cddf9SRui Paulo }
4957f05cddf9SRui Paulo 
4958f05cddf9SRui Paulo 
p2p_get_oper_freq(struct p2p_data * p2p,const u8 * iface_addr)4959f05cddf9SRui Paulo int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4960f05cddf9SRui Paulo {
4961f05cddf9SRui Paulo 	struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4962f05cddf9SRui Paulo 	if (dev == NULL)
4963f05cddf9SRui Paulo 		return -1;
4964f05cddf9SRui Paulo 	if (dev->oper_freq <= 0)
4965f05cddf9SRui Paulo 		return -1;
4966f05cddf9SRui Paulo 	return dev->oper_freq;
4967f05cddf9SRui Paulo }
4968f05cddf9SRui Paulo 
4969f05cddf9SRui Paulo 
p2p_set_intra_bss_dist(struct p2p_data * p2p,int enabled)4970f05cddf9SRui Paulo void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4971f05cddf9SRui Paulo {
49725b9c547cSRui Paulo 	p2p_dbg(p2p, "Intra BSS distribution %s",
4973f05cddf9SRui Paulo 		enabled ? "enabled" : "disabled");
4974f05cddf9SRui Paulo 	p2p->cfg->p2p_intra_bss = enabled;
4975f05cddf9SRui Paulo }
4976f05cddf9SRui Paulo 
4977f05cddf9SRui Paulo 
p2p_update_channel_list(struct p2p_data * p2p,const struct p2p_channels * chan,const struct p2p_channels * cli_chan)49785b9c547cSRui Paulo void p2p_update_channel_list(struct p2p_data *p2p,
49795b9c547cSRui Paulo 			     const struct p2p_channels *chan,
49805b9c547cSRui Paulo 			     const struct p2p_channels *cli_chan)
4981f05cddf9SRui Paulo {
49825b9c547cSRui Paulo 	p2p_dbg(p2p, "Update channel list");
4983f05cddf9SRui Paulo 	os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
49845b9c547cSRui Paulo 	p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
49855b9c547cSRui Paulo 	os_memcpy(&p2p->cfg->cli_channels, cli_chan,
49865b9c547cSRui Paulo 		  sizeof(struct p2p_channels));
49875b9c547cSRui Paulo 	p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
4988f05cddf9SRui Paulo }
4989f05cddf9SRui Paulo 
4990f05cddf9SRui Paulo 
p2p_send_action(struct p2p_data * p2p,unsigned int freq,const u8 * dst,const u8 * src,const u8 * bssid,const u8 * buf,size_t len,unsigned int wait_time)4991f05cddf9SRui Paulo int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4992f05cddf9SRui Paulo 		    const u8 *src, const u8 *bssid, const u8 *buf,
4993f05cddf9SRui Paulo 		    size_t len, unsigned int wait_time)
4994f05cddf9SRui Paulo {
49954bc52338SCy Schubert 	int res, scheduled;
49964bc52338SCy Schubert 
49974bc52338SCy Schubert 	res = p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
49984bc52338SCy Schubert 				    buf, len, wait_time, &scheduled);
49994bc52338SCy Schubert 	if (res == 0 && scheduled && p2p->in_listen && freq > 0 &&
5000c1d255d3SCy Schubert 	    p2p->drv_in_listen > 0 &&
50014bc52338SCy Schubert 	    (unsigned int) p2p->drv_in_listen != freq) {
50024bc52338SCy Schubert 		p2p_dbg(p2p,
50034bc52338SCy Schubert 			"Stop listen on %d MHz to allow a frame to be sent immediately on %d MHz",
50044bc52338SCy Schubert 			p2p->drv_in_listen, freq);
50054bc52338SCy Schubert 		p2p_stop_listen_for_freq(p2p, freq);
50064bc52338SCy Schubert 	}
50074bc52338SCy Schubert 	return res;
5008f05cddf9SRui Paulo }
5009f05cddf9SRui Paulo 
5010f05cddf9SRui Paulo 
p2p_set_best_channels(struct p2p_data * p2p,int freq_24,int freq_5,int freq_overall)5011f05cddf9SRui Paulo void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
5012f05cddf9SRui Paulo 			   int freq_overall)
5013f05cddf9SRui Paulo {
50145b9c547cSRui Paulo 	p2p_dbg(p2p, "Best channel: 2.4 GHz: %d,  5 GHz: %d,  overall: %d",
50155b9c547cSRui Paulo 		freq_24, freq_5, freq_overall);
5016f05cddf9SRui Paulo 	p2p->best_freq_24 = freq_24;
5017f05cddf9SRui Paulo 	p2p->best_freq_5 = freq_5;
5018f05cddf9SRui Paulo 	p2p->best_freq_overall = freq_overall;
5019f05cddf9SRui Paulo }
5020f05cddf9SRui Paulo 
5021f05cddf9SRui Paulo 
p2p_set_own_freq_preference(struct p2p_data * p2p,int freq)50225b9c547cSRui Paulo void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
50235b9c547cSRui Paulo {
50245b9c547cSRui Paulo 	p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
50255b9c547cSRui Paulo 	p2p->own_freq_preference = freq;
50265b9c547cSRui Paulo }
50275b9c547cSRui Paulo 
50285b9c547cSRui Paulo 
p2p_get_go_neg_peer(struct p2p_data * p2p)5029f05cddf9SRui Paulo const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
5030f05cddf9SRui Paulo {
5031f05cddf9SRui Paulo 	if (p2p == NULL || p2p->go_neg_peer == NULL)
5032f05cddf9SRui Paulo 		return NULL;
5033f05cddf9SRui Paulo 	return p2p->go_neg_peer->info.p2p_device_addr;
5034f05cddf9SRui Paulo }
5035f05cddf9SRui Paulo 
5036f05cddf9SRui Paulo 
5037f05cddf9SRui Paulo const struct p2p_peer_info *
p2p_get_peer_found(struct p2p_data * p2p,const u8 * addr,int next)5038f05cddf9SRui Paulo p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
5039f05cddf9SRui Paulo {
5040f05cddf9SRui Paulo 	struct p2p_device *dev;
5041f05cddf9SRui Paulo 
5042f05cddf9SRui Paulo 	if (addr) {
5043f05cddf9SRui Paulo 		dev = p2p_get_device(p2p, addr);
5044f05cddf9SRui Paulo 		if (!dev)
5045f05cddf9SRui Paulo 			return NULL;
5046f05cddf9SRui Paulo 
5047f05cddf9SRui Paulo 		if (!next) {
5048f05cddf9SRui Paulo 			if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
5049f05cddf9SRui Paulo 				return NULL;
5050f05cddf9SRui Paulo 
5051f05cddf9SRui Paulo 			return &dev->info;
5052f05cddf9SRui Paulo 		} else {
5053f05cddf9SRui Paulo 			do {
5054f05cddf9SRui Paulo 				dev = dl_list_first(&dev->list,
5055f05cddf9SRui Paulo 						    struct p2p_device,
5056f05cddf9SRui Paulo 						    list);
50575b9c547cSRui Paulo 				if (!dev || &dev->list == &p2p->devices)
5058f05cddf9SRui Paulo 					return NULL;
5059f05cddf9SRui Paulo 			} while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
5060f05cddf9SRui Paulo 		}
5061f05cddf9SRui Paulo 	} else {
5062f05cddf9SRui Paulo 		dev = dl_list_first(&p2p->devices, struct p2p_device, list);
5063f05cddf9SRui Paulo 		if (!dev)
5064f05cddf9SRui Paulo 			return NULL;
5065f05cddf9SRui Paulo 		while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
5066f05cddf9SRui Paulo 			dev = dl_list_first(&dev->list,
5067f05cddf9SRui Paulo 					    struct p2p_device,
5068f05cddf9SRui Paulo 					    list);
50695b9c547cSRui Paulo 			if (!dev || &dev->list == &p2p->devices)
5070f05cddf9SRui Paulo 				return NULL;
5071f05cddf9SRui Paulo 		}
5072f05cddf9SRui Paulo 	}
5073f05cddf9SRui Paulo 
5074f05cddf9SRui Paulo 	return &dev->info;
5075f05cddf9SRui Paulo }
5076f05cddf9SRui Paulo 
5077f05cddf9SRui Paulo 
p2p_in_progress(struct p2p_data * p2p)5078f05cddf9SRui Paulo int p2p_in_progress(struct p2p_data *p2p)
5079f05cddf9SRui Paulo {
5080f05cddf9SRui Paulo 	if (p2p == NULL)
5081f05cddf9SRui Paulo 		return 0;
50825b9c547cSRui Paulo 	if (p2p->state == P2P_SEARCH)
5083f05cddf9SRui Paulo 		return 2;
5084f05cddf9SRui Paulo 	return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
5085f05cddf9SRui Paulo }
5086f05cddf9SRui Paulo 
5087f05cddf9SRui Paulo 
p2p_set_config_timeout(struct p2p_data * p2p,u8 go_timeout,u8 client_timeout)5088f05cddf9SRui Paulo void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
5089f05cddf9SRui Paulo 			    u8 client_timeout)
5090f05cddf9SRui Paulo {
5091f05cddf9SRui Paulo 	if (p2p) {
5092f05cddf9SRui Paulo 		p2p->go_timeout = go_timeout;
5093f05cddf9SRui Paulo 		p2p->client_timeout = client_timeout;
5094f05cddf9SRui Paulo 	}
5095f05cddf9SRui Paulo }
5096f05cddf9SRui Paulo 
5097f05cddf9SRui Paulo 
5098f05cddf9SRui Paulo #ifdef CONFIG_WIFI_DISPLAY
5099f05cddf9SRui Paulo 
p2p_update_wfd_ie_groups(struct p2p_data * p2p)5100f05cddf9SRui Paulo static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
5101f05cddf9SRui Paulo {
5102f05cddf9SRui Paulo 	size_t g;
5103f05cddf9SRui Paulo 	struct p2p_group *group;
5104f05cddf9SRui Paulo 
5105f05cddf9SRui Paulo 	for (g = 0; g < p2p->num_groups; g++) {
5106f05cddf9SRui Paulo 		group = p2p->groups[g];
51075b9c547cSRui Paulo 		p2p_group_force_beacon_update_ies(group);
5108f05cddf9SRui Paulo 	}
5109f05cddf9SRui Paulo }
5110f05cddf9SRui Paulo 
5111f05cddf9SRui Paulo 
p2p_set_wfd_ie_beacon(struct p2p_data * p2p,struct wpabuf * ie)5112f05cddf9SRui Paulo int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
5113f05cddf9SRui Paulo {
5114f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_beacon);
5115f05cddf9SRui Paulo 	p2p->wfd_ie_beacon = ie;
5116f05cddf9SRui Paulo 	p2p_update_wfd_ie_groups(p2p);
5117f05cddf9SRui Paulo 	return 0;
5118f05cddf9SRui Paulo }
5119f05cddf9SRui Paulo 
5120f05cddf9SRui Paulo 
p2p_set_wfd_ie_probe_req(struct p2p_data * p2p,struct wpabuf * ie)5121f05cddf9SRui Paulo int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
5122f05cddf9SRui Paulo {
5123f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_probe_req);
5124f05cddf9SRui Paulo 	p2p->wfd_ie_probe_req = ie;
5125f05cddf9SRui Paulo 	return 0;
5126f05cddf9SRui Paulo }
5127f05cddf9SRui Paulo 
5128f05cddf9SRui Paulo 
p2p_set_wfd_ie_probe_resp(struct p2p_data * p2p,struct wpabuf * ie)5129f05cddf9SRui Paulo int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
5130f05cddf9SRui Paulo {
5131f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_probe_resp);
5132f05cddf9SRui Paulo 	p2p->wfd_ie_probe_resp = ie;
5133f05cddf9SRui Paulo 	p2p_update_wfd_ie_groups(p2p);
5134f05cddf9SRui Paulo 	return 0;
5135f05cddf9SRui Paulo }
5136f05cddf9SRui Paulo 
5137f05cddf9SRui Paulo 
p2p_set_wfd_ie_assoc_req(struct p2p_data * p2p,struct wpabuf * ie)5138f05cddf9SRui Paulo int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
5139f05cddf9SRui Paulo {
5140f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_assoc_req);
5141f05cddf9SRui Paulo 	p2p->wfd_ie_assoc_req = ie;
5142f05cddf9SRui Paulo 	return 0;
5143f05cddf9SRui Paulo }
5144f05cddf9SRui Paulo 
5145f05cddf9SRui Paulo 
p2p_set_wfd_ie_invitation(struct p2p_data * p2p,struct wpabuf * ie)5146f05cddf9SRui Paulo int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
5147f05cddf9SRui Paulo {
5148f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_invitation);
5149f05cddf9SRui Paulo 	p2p->wfd_ie_invitation = ie;
5150f05cddf9SRui Paulo 	return 0;
5151f05cddf9SRui Paulo }
5152f05cddf9SRui Paulo 
5153f05cddf9SRui Paulo 
p2p_set_wfd_ie_prov_disc_req(struct p2p_data * p2p,struct wpabuf * ie)5154f05cddf9SRui Paulo int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
5155f05cddf9SRui Paulo {
5156f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_prov_disc_req);
5157f05cddf9SRui Paulo 	p2p->wfd_ie_prov_disc_req = ie;
5158f05cddf9SRui Paulo 	return 0;
5159f05cddf9SRui Paulo }
5160f05cddf9SRui Paulo 
5161f05cddf9SRui Paulo 
p2p_set_wfd_ie_prov_disc_resp(struct p2p_data * p2p,struct wpabuf * ie)5162f05cddf9SRui Paulo int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
5163f05cddf9SRui Paulo {
5164f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_prov_disc_resp);
5165f05cddf9SRui Paulo 	p2p->wfd_ie_prov_disc_resp = ie;
5166f05cddf9SRui Paulo 	return 0;
5167f05cddf9SRui Paulo }
5168f05cddf9SRui Paulo 
5169f05cddf9SRui Paulo 
p2p_set_wfd_ie_go_neg(struct p2p_data * p2p,struct wpabuf * ie)5170f05cddf9SRui Paulo int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
5171f05cddf9SRui Paulo {
5172f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_ie_go_neg);
5173f05cddf9SRui Paulo 	p2p->wfd_ie_go_neg = ie;
5174f05cddf9SRui Paulo 	return 0;
5175f05cddf9SRui Paulo }
5176f05cddf9SRui Paulo 
5177f05cddf9SRui Paulo 
p2p_set_wfd_dev_info(struct p2p_data * p2p,const struct wpabuf * elem)5178f05cddf9SRui Paulo int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5179f05cddf9SRui Paulo {
5180f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_dev_info);
5181f05cddf9SRui Paulo 	if (elem) {
5182f05cddf9SRui Paulo 		p2p->wfd_dev_info = wpabuf_dup(elem);
5183f05cddf9SRui Paulo 		if (p2p->wfd_dev_info == NULL)
5184f05cddf9SRui Paulo 			return -1;
5185f05cddf9SRui Paulo 	} else
5186f05cddf9SRui Paulo 		p2p->wfd_dev_info = NULL;
5187f05cddf9SRui Paulo 
5188f05cddf9SRui Paulo 	return 0;
5189f05cddf9SRui Paulo }
5190f05cddf9SRui Paulo 
5191f05cddf9SRui Paulo 
p2p_set_wfd_r2_dev_info(struct p2p_data * p2p,const struct wpabuf * elem)519285732ac8SCy Schubert int p2p_set_wfd_r2_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
519385732ac8SCy Schubert {
519485732ac8SCy Schubert 	wpabuf_free(p2p->wfd_r2_dev_info);
519585732ac8SCy Schubert 	if (elem) {
519685732ac8SCy Schubert 		p2p->wfd_r2_dev_info = wpabuf_dup(elem);
519785732ac8SCy Schubert 		if (p2p->wfd_r2_dev_info == NULL)
519885732ac8SCy Schubert 			return -1;
519985732ac8SCy Schubert 	} else
520085732ac8SCy Schubert 		p2p->wfd_r2_dev_info = NULL;
520185732ac8SCy Schubert 
520285732ac8SCy Schubert 	return 0;
520385732ac8SCy Schubert }
520485732ac8SCy Schubert 
520585732ac8SCy Schubert 
p2p_set_wfd_assoc_bssid(struct p2p_data * p2p,const struct wpabuf * elem)5206f05cddf9SRui Paulo int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
5207f05cddf9SRui Paulo {
5208f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_assoc_bssid);
5209f05cddf9SRui Paulo 	if (elem) {
5210f05cddf9SRui Paulo 		p2p->wfd_assoc_bssid = wpabuf_dup(elem);
5211f05cddf9SRui Paulo 		if (p2p->wfd_assoc_bssid == NULL)
5212f05cddf9SRui Paulo 			return -1;
5213f05cddf9SRui Paulo 	} else
5214f05cddf9SRui Paulo 		p2p->wfd_assoc_bssid = NULL;
5215f05cddf9SRui Paulo 
5216f05cddf9SRui Paulo 	return 0;
5217f05cddf9SRui Paulo }
5218f05cddf9SRui Paulo 
5219f05cddf9SRui Paulo 
p2p_set_wfd_coupled_sink_info(struct p2p_data * p2p,const struct wpabuf * elem)5220f05cddf9SRui Paulo int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
5221f05cddf9SRui Paulo 				  const struct wpabuf *elem)
5222f05cddf9SRui Paulo {
5223f05cddf9SRui Paulo 	wpabuf_free(p2p->wfd_coupled_sink_info);
5224f05cddf9SRui Paulo 	if (elem) {
5225f05cddf9SRui Paulo 		p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
5226f05cddf9SRui Paulo 		if (p2p->wfd_coupled_sink_info == NULL)
5227f05cddf9SRui Paulo 			return -1;
5228f05cddf9SRui Paulo 	} else
5229f05cddf9SRui Paulo 		p2p->wfd_coupled_sink_info = NULL;
5230f05cddf9SRui Paulo 
5231f05cddf9SRui Paulo 	return 0;
5232f05cddf9SRui Paulo }
5233f05cddf9SRui Paulo 
5234f05cddf9SRui Paulo #endif /* CONFIG_WIFI_DISPLAY */
5235f05cddf9SRui Paulo 
5236f05cddf9SRui Paulo 
p2p_set_disc_int(struct p2p_data * p2p,int min_disc_int,int max_disc_int,int max_disc_tu)5237f05cddf9SRui Paulo int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
5238f05cddf9SRui Paulo 		     int max_disc_tu)
5239f05cddf9SRui Paulo {
5240f05cddf9SRui Paulo 	if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
5241f05cddf9SRui Paulo 		return -1;
5242f05cddf9SRui Paulo 
5243f05cddf9SRui Paulo 	p2p->min_disc_int = min_disc_int;
5244f05cddf9SRui Paulo 	p2p->max_disc_int = max_disc_int;
5245f05cddf9SRui Paulo 	p2p->max_disc_tu = max_disc_tu;
52465b9c547cSRui Paulo 	p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
52475b9c547cSRui Paulo 		min_disc_int, max_disc_int, max_disc_tu);
5248f05cddf9SRui Paulo 
5249f05cddf9SRui Paulo 	return 0;
5250f05cddf9SRui Paulo }
52515b9c547cSRui Paulo 
52525b9c547cSRui Paulo 
p2p_dbg(struct p2p_data * p2p,const char * fmt,...)52535b9c547cSRui Paulo void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
52545b9c547cSRui Paulo {
52555b9c547cSRui Paulo 	va_list ap;
52565b9c547cSRui Paulo 	char buf[500];
52575b9c547cSRui Paulo 
52585b9c547cSRui Paulo 	if (!p2p->cfg->debug_print)
52595b9c547cSRui Paulo 		return;
52605b9c547cSRui Paulo 
52615b9c547cSRui Paulo 	va_start(ap, fmt);
52625b9c547cSRui Paulo 	vsnprintf(buf, sizeof(buf), fmt, ap);
52635b9c547cSRui Paulo 	buf[sizeof(buf) - 1] = '\0';
52645b9c547cSRui Paulo 	va_end(ap);
52655b9c547cSRui Paulo 	p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
52665b9c547cSRui Paulo }
52675b9c547cSRui Paulo 
52685b9c547cSRui Paulo 
p2p_info(struct p2p_data * p2p,const char * fmt,...)52695b9c547cSRui Paulo void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
52705b9c547cSRui Paulo {
52715b9c547cSRui Paulo 	va_list ap;
52725b9c547cSRui Paulo 	char buf[500];
52735b9c547cSRui Paulo 
52745b9c547cSRui Paulo 	if (!p2p->cfg->debug_print)
52755b9c547cSRui Paulo 		return;
52765b9c547cSRui Paulo 
52775b9c547cSRui Paulo 	va_start(ap, fmt);
52785b9c547cSRui Paulo 	vsnprintf(buf, sizeof(buf), fmt, ap);
52795b9c547cSRui Paulo 	buf[sizeof(buf) - 1] = '\0';
52805b9c547cSRui Paulo 	va_end(ap);
52815b9c547cSRui Paulo 	p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
52825b9c547cSRui Paulo }
52835b9c547cSRui Paulo 
52845b9c547cSRui Paulo 
p2p_err(struct p2p_data * p2p,const char * fmt,...)52855b9c547cSRui Paulo void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
52865b9c547cSRui Paulo {
52875b9c547cSRui Paulo 	va_list ap;
52885b9c547cSRui Paulo 	char buf[500];
52895b9c547cSRui Paulo 
52905b9c547cSRui Paulo 	if (!p2p->cfg->debug_print)
52915b9c547cSRui Paulo 		return;
52925b9c547cSRui Paulo 
52935b9c547cSRui Paulo 	va_start(ap, fmt);
52945b9c547cSRui Paulo 	vsnprintf(buf, sizeof(buf), fmt, ap);
52955b9c547cSRui Paulo 	buf[sizeof(buf) - 1] = '\0';
52965b9c547cSRui Paulo 	va_end(ap);
52975b9c547cSRui Paulo 	p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
52985b9c547cSRui Paulo }
52995b9c547cSRui Paulo 
53005b9c547cSRui Paulo 
p2p_loop_on_known_peers(struct p2p_data * p2p,void (* peer_callback)(struct p2p_peer_info * peer,void * user_data),void * user_data)53015b9c547cSRui Paulo void p2p_loop_on_known_peers(struct p2p_data *p2p,
53025b9c547cSRui Paulo 			     void (*peer_callback)(struct p2p_peer_info *peer,
53035b9c547cSRui Paulo 						   void *user_data),
53045b9c547cSRui Paulo 			     void *user_data)
53055b9c547cSRui Paulo {
53065b9c547cSRui Paulo 	struct p2p_device *dev, *n;
53075b9c547cSRui Paulo 
53085b9c547cSRui Paulo 	dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
53095b9c547cSRui Paulo 		peer_callback(&dev->info, user_data);
53105b9c547cSRui Paulo 	}
53115b9c547cSRui Paulo }
53125b9c547cSRui Paulo 
53135b9c547cSRui Paulo 
53145b9c547cSRui Paulo #ifdef CONFIG_WPS_NFC
53155b9c547cSRui Paulo 
p2p_build_nfc_handover(struct p2p_data * p2p,int client_freq,const u8 * go_dev_addr,const u8 * ssid,size_t ssid_len)53165b9c547cSRui Paulo static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
53175b9c547cSRui Paulo 					      int client_freq,
53185b9c547cSRui Paulo 					      const u8 *go_dev_addr,
53195b9c547cSRui Paulo 					      const u8 *ssid, size_t ssid_len)
53205b9c547cSRui Paulo {
53215b9c547cSRui Paulo 	struct wpabuf *buf;
53225b9c547cSRui Paulo 	u8 op_class, channel;
53235b9c547cSRui Paulo 	enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
53245b9c547cSRui Paulo 
53255b9c547cSRui Paulo 	buf = wpabuf_alloc(1000);
53265b9c547cSRui Paulo 	if (buf == NULL)
53275b9c547cSRui Paulo 		return NULL;
53285b9c547cSRui Paulo 
53295b9c547cSRui Paulo 	op_class = p2p->cfg->reg_class;
53305b9c547cSRui Paulo 	channel = p2p->cfg->channel;
53315b9c547cSRui Paulo 
53325b9c547cSRui Paulo 	p2p_buf_add_capability(buf, p2p->dev_capab &
53335b9c547cSRui Paulo 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
53345b9c547cSRui Paulo 	p2p_buf_add_device_info(buf, p2p, NULL);
53355b9c547cSRui Paulo 
53365b9c547cSRui Paulo 	if (p2p->num_groups > 0) {
53375b9c547cSRui Paulo 		int freq = p2p_group_get_freq(p2p->groups[0]);
53385b9c547cSRui Paulo 		role = P2P_GO_IN_A_GROUP;
53395b9c547cSRui Paulo 		if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) {
53405b9c547cSRui Paulo 			p2p_dbg(p2p,
53415b9c547cSRui Paulo 				"Unknown GO operating frequency %d MHz for NFC handover",
53425b9c547cSRui Paulo 				freq);
53435b9c547cSRui Paulo 			wpabuf_free(buf);
53445b9c547cSRui Paulo 			return NULL;
53455b9c547cSRui Paulo 		}
53465b9c547cSRui Paulo 	} else if (client_freq > 0) {
53475b9c547cSRui Paulo 		role = P2P_CLIENT_IN_A_GROUP;
53485b9c547cSRui Paulo 		if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) {
53495b9c547cSRui Paulo 			p2p_dbg(p2p,
53505b9c547cSRui Paulo 				"Unknown client operating frequency %d MHz for NFC handover",
53515b9c547cSRui Paulo 				client_freq);
53525b9c547cSRui Paulo 			wpabuf_free(buf);
53535b9c547cSRui Paulo 			return NULL;
53545b9c547cSRui Paulo 		}
53555b9c547cSRui Paulo 	}
53565b9c547cSRui Paulo 
53575b9c547cSRui Paulo 	p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
53585b9c547cSRui Paulo 				       channel, role);
53595b9c547cSRui Paulo 
53605b9c547cSRui Paulo 	if (p2p->num_groups > 0) {
53615b9c547cSRui Paulo 		/* Limit number of clients to avoid very long message */
53625b9c547cSRui Paulo 		p2p_buf_add_group_info(p2p->groups[0], buf, 5);
53635b9c547cSRui Paulo 		p2p_group_buf_add_id(p2p->groups[0], buf);
53645b9c547cSRui Paulo 	} else if (client_freq > 0 &&
53655b9c547cSRui Paulo 		   go_dev_addr && !is_zero_ether_addr(go_dev_addr) &&
53665b9c547cSRui Paulo 		   ssid && ssid_len > 0) {
53675b9c547cSRui Paulo 		/*
53685b9c547cSRui Paulo 		 * Add the optional P2P Group ID to indicate in which group this
53695b9c547cSRui Paulo 		 * device is a P2P Client.
53705b9c547cSRui Paulo 		 */
53715b9c547cSRui Paulo 		p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len);
53725b9c547cSRui Paulo 	}
53735b9c547cSRui Paulo 
53745b9c547cSRui Paulo 	return buf;
53755b9c547cSRui Paulo }
53765b9c547cSRui Paulo 
53775b9c547cSRui Paulo 
p2p_build_nfc_handover_req(struct p2p_data * p2p,int client_freq,const u8 * go_dev_addr,const u8 * ssid,size_t ssid_len)53785b9c547cSRui Paulo struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
53795b9c547cSRui Paulo 					   int client_freq,
53805b9c547cSRui Paulo 					   const u8 *go_dev_addr,
53815b9c547cSRui Paulo 					   const u8 *ssid, size_t ssid_len)
53825b9c547cSRui Paulo {
53835b9c547cSRui Paulo 	return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
53845b9c547cSRui Paulo 				      ssid_len);
53855b9c547cSRui Paulo }
53865b9c547cSRui Paulo 
53875b9c547cSRui Paulo 
p2p_build_nfc_handover_sel(struct p2p_data * p2p,int client_freq,const u8 * go_dev_addr,const u8 * ssid,size_t ssid_len)53885b9c547cSRui Paulo struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
53895b9c547cSRui Paulo 					   int client_freq,
53905b9c547cSRui Paulo 					   const u8 *go_dev_addr,
53915b9c547cSRui Paulo 					   const u8 *ssid, size_t ssid_len)
53925b9c547cSRui Paulo {
53935b9c547cSRui Paulo 	return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
53945b9c547cSRui Paulo 				      ssid_len);
53955b9c547cSRui Paulo }
53965b9c547cSRui Paulo 
53975b9c547cSRui Paulo 
p2p_process_nfc_connection_handover(struct p2p_data * p2p,struct p2p_nfc_params * params)53985b9c547cSRui Paulo int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
53995b9c547cSRui Paulo 					struct p2p_nfc_params *params)
54005b9c547cSRui Paulo {
54015b9c547cSRui Paulo 	struct p2p_message msg;
54025b9c547cSRui Paulo 	struct p2p_device *dev;
54035b9c547cSRui Paulo 	const u8 *p2p_dev_addr;
54045b9c547cSRui Paulo 	int freq;
54055b9c547cSRui Paulo 	enum p2p_role_indication role;
54065b9c547cSRui Paulo 
54075b9c547cSRui Paulo 	params->next_step = NO_ACTION;
54085b9c547cSRui Paulo 
54095b9c547cSRui Paulo 	if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
54105b9c547cSRui Paulo 				   params->p2p_attr, params->p2p_len, &msg)) {
54115b9c547cSRui Paulo 		p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
54125b9c547cSRui Paulo 		p2p_parse_free(&msg);
54135b9c547cSRui Paulo 		return -1;
54145b9c547cSRui Paulo 	}
54155b9c547cSRui Paulo 
54165b9c547cSRui Paulo 	if (msg.p2p_device_addr)
54175b9c547cSRui Paulo 		p2p_dev_addr = msg.p2p_device_addr;
54185b9c547cSRui Paulo 	else if (msg.device_id)
54195b9c547cSRui Paulo 		p2p_dev_addr = msg.device_id;
54205b9c547cSRui Paulo 	else {
54215b9c547cSRui Paulo 		p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
54225b9c547cSRui Paulo 		p2p_parse_free(&msg);
54235b9c547cSRui Paulo 		return -1;
54245b9c547cSRui Paulo 	}
54255b9c547cSRui Paulo 
54265b9c547cSRui Paulo 	if (msg.oob_dev_password) {
54275b9c547cSRui Paulo 		os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
54285b9c547cSRui Paulo 			  msg.oob_dev_password_len);
54295b9c547cSRui Paulo 		params->oob_dev_pw_len = msg.oob_dev_password_len;
54305b9c547cSRui Paulo 	}
54315b9c547cSRui Paulo 
54325b9c547cSRui Paulo 	dev = p2p_create_device(p2p, p2p_dev_addr);
54335b9c547cSRui Paulo 	if (dev == NULL) {
54345b9c547cSRui Paulo 		p2p_parse_free(&msg);
54355b9c547cSRui Paulo 		return -1;
54365b9c547cSRui Paulo 	}
54375b9c547cSRui Paulo 
54385b9c547cSRui Paulo 	params->peer = &dev->info;
54395b9c547cSRui Paulo 
54405b9c547cSRui Paulo 	os_get_reltime(&dev->last_seen);
54415b9c547cSRui Paulo 	dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
54425b9c547cSRui Paulo 	p2p_copy_wps_info(p2p, dev, 0, &msg);
54435b9c547cSRui Paulo 
54445b9c547cSRui Paulo 	if (!msg.oob_go_neg_channel) {
54455b9c547cSRui Paulo 		p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
5446325151a3SRui Paulo 		p2p_parse_free(&msg);
54475b9c547cSRui Paulo 		return -1;
54485b9c547cSRui Paulo 	}
54495b9c547cSRui Paulo 
54505b9c547cSRui Paulo 	if (msg.oob_go_neg_channel[3] == 0 &&
54515b9c547cSRui Paulo 	    msg.oob_go_neg_channel[4] == 0)
54525b9c547cSRui Paulo 		freq = 0;
54535b9c547cSRui Paulo 	else
54545b9c547cSRui Paulo 		freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
54555b9c547cSRui Paulo 					   msg.oob_go_neg_channel[4]);
54565b9c547cSRui Paulo 	if (freq < 0) {
54575b9c547cSRui Paulo 		p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
5458325151a3SRui Paulo 		p2p_parse_free(&msg);
54595b9c547cSRui Paulo 		return -1;
54605b9c547cSRui Paulo 	}
54615b9c547cSRui Paulo 	role = msg.oob_go_neg_channel[5];
54625b9c547cSRui Paulo 
54635b9c547cSRui Paulo 	if (role == P2P_GO_IN_A_GROUP) {
54645b9c547cSRui Paulo 		p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
54655b9c547cSRui Paulo 		params->go_freq = freq;
54665b9c547cSRui Paulo 	} else if (role == P2P_CLIENT_IN_A_GROUP) {
54675b9c547cSRui Paulo 		p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz",
54685b9c547cSRui Paulo 			freq);
54695b9c547cSRui Paulo 		params->go_freq = freq;
54705b9c547cSRui Paulo 	} else
54715b9c547cSRui Paulo 		p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
54725b9c547cSRui Paulo 	dev->oob_go_neg_freq = freq;
54735b9c547cSRui Paulo 
54745b9c547cSRui Paulo 	if (!params->sel && role != P2P_GO_IN_A_GROUP) {
54755b9c547cSRui Paulo 		freq = p2p_channel_to_freq(p2p->cfg->reg_class,
54765b9c547cSRui Paulo 					   p2p->cfg->channel);
54775b9c547cSRui Paulo 		if (freq < 0) {
54785b9c547cSRui Paulo 			p2p_dbg(p2p, "Own listen channel not known");
5479325151a3SRui Paulo 			p2p_parse_free(&msg);
54805b9c547cSRui Paulo 			return -1;
54815b9c547cSRui Paulo 		}
54825b9c547cSRui Paulo 		p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
54835b9c547cSRui Paulo 		dev->oob_go_neg_freq = freq;
54845b9c547cSRui Paulo 	}
54855b9c547cSRui Paulo 
54865b9c547cSRui Paulo 	if (msg.group_id) {
54875b9c547cSRui Paulo 		os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
54885b9c547cSRui Paulo 		params->go_ssid_len = msg.group_id_len - ETH_ALEN;
54895b9c547cSRui Paulo 		os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
54905b9c547cSRui Paulo 			  params->go_ssid_len);
54915b9c547cSRui Paulo 	}
54925b9c547cSRui Paulo 
54935b9c547cSRui Paulo 	if (dev->flags & P2P_DEV_USER_REJECTED) {
54945b9c547cSRui Paulo 		p2p_dbg(p2p, "Do not report rejected device");
54955b9c547cSRui Paulo 		p2p_parse_free(&msg);
54965b9c547cSRui Paulo 		return 0;
54975b9c547cSRui Paulo 	}
54985b9c547cSRui Paulo 
54995b9c547cSRui Paulo 	if (!(dev->flags & P2P_DEV_REPORTED)) {
55005b9c547cSRui Paulo 		p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
55015b9c547cSRui Paulo 				    !(dev->flags & P2P_DEV_REPORTED_ONCE));
55025b9c547cSRui Paulo 		dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
55035b9c547cSRui Paulo 	}
55045b9c547cSRui Paulo 	p2p_parse_free(&msg);
55055b9c547cSRui Paulo 
55065b9c547cSRui Paulo 	if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
55075b9c547cSRui Paulo 		params->next_step = BOTH_GO;
55085b9c547cSRui Paulo 	else if (role == P2P_GO_IN_A_GROUP)
55095b9c547cSRui Paulo 		params->next_step = JOIN_GROUP;
55105b9c547cSRui Paulo 	else if (role == P2P_CLIENT_IN_A_GROUP) {
55115b9c547cSRui Paulo 		dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
55125b9c547cSRui Paulo 		params->next_step = PEER_CLIENT;
55135b9c547cSRui Paulo 	} else if (p2p->num_groups > 0)
55145b9c547cSRui Paulo 		params->next_step = AUTH_JOIN;
55155b9c547cSRui Paulo 	else if (params->sel)
55165b9c547cSRui Paulo 		params->next_step = INIT_GO_NEG;
55175b9c547cSRui Paulo 	else
55185b9c547cSRui Paulo 		params->next_step = RESP_GO_NEG;
55195b9c547cSRui Paulo 
55205b9c547cSRui Paulo 	return 0;
55215b9c547cSRui Paulo }
55225b9c547cSRui Paulo 
55235b9c547cSRui Paulo 
p2p_set_authorized_oob_dev_pw_id(struct p2p_data * p2p,u16 dev_pw_id,int go_intent,const u8 * own_interface_addr)55245b9c547cSRui Paulo void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
55255b9c547cSRui Paulo 				      int go_intent,
55265b9c547cSRui Paulo 				      const u8 *own_interface_addr)
55275b9c547cSRui Paulo {
55285b9c547cSRui Paulo 
55295b9c547cSRui Paulo 	p2p->authorized_oob_dev_pw_id = dev_pw_id;
55305b9c547cSRui Paulo 	if (dev_pw_id == 0) {
55315b9c547cSRui Paulo 		p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
55325b9c547cSRui Paulo 		return;
55335b9c547cSRui Paulo 	}
55345b9c547cSRui Paulo 
55355b9c547cSRui Paulo 	p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
55365b9c547cSRui Paulo 		dev_pw_id);
55375b9c547cSRui Paulo 
55385b9c547cSRui Paulo 	p2p->go_intent = go_intent;
55395b9c547cSRui Paulo 	os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
55405b9c547cSRui Paulo }
55415b9c547cSRui Paulo 
55425b9c547cSRui Paulo #endif /* CONFIG_WPS_NFC */
55435b9c547cSRui Paulo 
55445b9c547cSRui Paulo 
p2p_set_passphrase_len(struct p2p_data * p2p,unsigned int len)55455b9c547cSRui Paulo int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len)
55465b9c547cSRui Paulo {
55475b9c547cSRui Paulo 	if (len < 8 || len > 63)
55485b9c547cSRui Paulo 		return -1;
55495b9c547cSRui Paulo 	p2p->cfg->passphrase_len = len;
55505b9c547cSRui Paulo 	return 0;
55515b9c547cSRui Paulo }
55525b9c547cSRui Paulo 
55535b9c547cSRui Paulo 
p2p_set_vendor_elems(struct p2p_data * p2p,struct wpabuf ** vendor_elem)55545b9c547cSRui Paulo void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem)
55555b9c547cSRui Paulo {
55565b9c547cSRui Paulo 	p2p->vendor_elem = vendor_elem;
55575b9c547cSRui Paulo }
55585b9c547cSRui Paulo 
55595b9c547cSRui Paulo 
p2p_go_neg_wait_timeout(void * eloop_ctx,void * timeout_ctx)55605b9c547cSRui Paulo void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx)
55615b9c547cSRui Paulo {
55625b9c547cSRui Paulo 	struct p2p_data *p2p = eloop_ctx;
55635b9c547cSRui Paulo 
55645b9c547cSRui Paulo 	p2p_dbg(p2p,
55655b9c547cSRui Paulo 		"Timeout on waiting peer to become ready for GO Negotiation");
55665b9c547cSRui Paulo 	p2p_go_neg_failed(p2p, -1);
55675b9c547cSRui Paulo }
5568325151a3SRui Paulo 
5569325151a3SRui Paulo 
p2p_set_own_pref_freq_list(struct p2p_data * p2p,const struct weighted_pcl * pref_freq_list,unsigned int size)5570325151a3SRui Paulo void p2p_set_own_pref_freq_list(struct p2p_data *p2p,
5571*a90b9d01SCy Schubert 				const struct weighted_pcl *pref_freq_list,
5572325151a3SRui Paulo 				unsigned int size)
5573325151a3SRui Paulo {
5574325151a3SRui Paulo 	unsigned int i;
5575325151a3SRui Paulo 
5576325151a3SRui Paulo 	if (size > P2P_MAX_PREF_CHANNELS)
5577325151a3SRui Paulo 		size = P2P_MAX_PREF_CHANNELS;
5578325151a3SRui Paulo 	p2p->num_pref_freq = size;
5579*a90b9d01SCy Schubert 	os_memcpy(p2p->pref_freq_list, pref_freq_list,
5580*a90b9d01SCy Schubert 		  size * sizeof(struct weighted_pcl));
5581325151a3SRui Paulo 	for (i = 0; i < size; i++) {
5582325151a3SRui Paulo 		p2p_dbg(p2p, "Own preferred frequency list[%u]=%u MHz",
5583*a90b9d01SCy Schubert 			i, p2p->pref_freq_list[i].freq);
5584325151a3SRui Paulo 	}
5585325151a3SRui Paulo }
5586780fb4a2SCy Schubert 
5587780fb4a2SCy Schubert 
p2p_set_override_pref_op_chan(struct p2p_data * p2p,u8 op_class,u8 chan)558885732ac8SCy Schubert void p2p_set_override_pref_op_chan(struct p2p_data *p2p, u8 op_class,
558985732ac8SCy Schubert 				   u8 chan)
559085732ac8SCy Schubert {
559185732ac8SCy Schubert 	p2p->override_pref_op_class = op_class;
559285732ac8SCy Schubert 	p2p->override_pref_channel = chan;
559385732ac8SCy Schubert }
559485732ac8SCy Schubert 
559585732ac8SCy Schubert 
p2p_build_probe_resp_template(struct p2p_data * p2p,unsigned int freq)5596780fb4a2SCy Schubert struct wpabuf * p2p_build_probe_resp_template(struct p2p_data *p2p,
5597780fb4a2SCy Schubert 					      unsigned int freq)
5598780fb4a2SCy Schubert {
5599780fb4a2SCy Schubert 	struct wpabuf *ies, *buf;
5600780fb4a2SCy Schubert 	u8 addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5601780fb4a2SCy Schubert 	int ret;
5602780fb4a2SCy Schubert 
5603780fb4a2SCy Schubert 	ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
5604780fb4a2SCy Schubert 	if (!ies) {
5605780fb4a2SCy Schubert 		wpa_printf(MSG_ERROR,
5606780fb4a2SCy Schubert 			   "CTRL: Failed to build Probe Response IEs");
5607780fb4a2SCy Schubert 		return NULL;
5608780fb4a2SCy Schubert 	}
5609780fb4a2SCy Schubert 
5610780fb4a2SCy Schubert 	buf = wpabuf_alloc(200 + wpabuf_len(ies));
5611780fb4a2SCy Schubert 	if (!buf) {
5612780fb4a2SCy Schubert 		wpabuf_free(ies);
5613780fb4a2SCy Schubert 		return NULL;
5614780fb4a2SCy Schubert 	}
5615780fb4a2SCy Schubert 
5616780fb4a2SCy Schubert 	ret = p2p_build_probe_resp_buf(p2p, buf, ies, addr, freq);
5617780fb4a2SCy Schubert 	wpabuf_free(ies);
5618780fb4a2SCy Schubert 	if (ret) {
5619780fb4a2SCy Schubert 		wpabuf_free(buf);
5620780fb4a2SCy Schubert 		return NULL;
5621780fb4a2SCy Schubert 	}
5622780fb4a2SCy Schubert 
5623780fb4a2SCy Schubert 	return buf;
5624780fb4a2SCy Schubert }
5625c1d255d3SCy Schubert 
5626c1d255d3SCy Schubert 
p2p_is_peer_6ghz_capab(struct p2p_data * p2p,const u8 * addr)5627c1d255d3SCy Schubert bool p2p_is_peer_6ghz_capab(struct p2p_data *p2p, const u8 *addr)
5628c1d255d3SCy Schubert {
5629c1d255d3SCy Schubert 	struct p2p_device *dev;
5630c1d255d3SCy Schubert 
5631c1d255d3SCy Schubert 	dev = p2p_get_device(p2p, addr);
5632c1d255d3SCy Schubert 	if (!dev)
5633c1d255d3SCy Schubert 		return false;
5634c1d255d3SCy Schubert 
5635*a90b9d01SCy Schubert 	return dev->support_6ghz;
5636c1d255d3SCy Schubert }
5637c1d255d3SCy Schubert 
5638c1d255d3SCy Schubert 
p2p_set_6ghz_dev_capab(struct p2p_data * p2p,bool allow_6ghz)5639c1d255d3SCy Schubert void p2p_set_6ghz_dev_capab(struct p2p_data *p2p, bool allow_6ghz)
5640c1d255d3SCy Schubert {
5641c1d255d3SCy Schubert 	p2p->p2p_6ghz_capable = allow_6ghz;
5642c1d255d3SCy Schubert 	p2p->allow_6ghz = allow_6ghz;
5643c1d255d3SCy Schubert 	p2p_dbg(p2p, "Set 6 GHz capability to %d", allow_6ghz);
5644c1d255d3SCy Schubert 
5645c1d255d3SCy Schubert 	if (allow_6ghz)
5646c1d255d3SCy Schubert 		p2p->dev_capab |= P2P_DEV_CAPAB_6GHZ_BAND_CAPABLE;
5647c1d255d3SCy Schubert 	else
5648c1d255d3SCy Schubert 		p2p->dev_capab &= ~P2P_DEV_CAPAB_6GHZ_BAND_CAPABLE;
5649c1d255d3SCy Schubert }
5650c1d255d3SCy Schubert 
5651c1d255d3SCy Schubert 
is_p2p_6ghz_capable(struct p2p_data * p2p)5652c1d255d3SCy Schubert bool is_p2p_6ghz_capable(struct p2p_data *p2p)
5653c1d255d3SCy Schubert {
5654c1d255d3SCy Schubert 	return p2p->p2p_6ghz_capable;
5655c1d255d3SCy Schubert }
5656c1d255d3SCy Schubert 
5657c1d255d3SCy Schubert 
p2p_wfd_enabled(struct p2p_data * p2p)5658c1d255d3SCy Schubert bool p2p_wfd_enabled(struct p2p_data *p2p)
5659c1d255d3SCy Schubert {
5660c1d255d3SCy Schubert #ifdef CONFIG_WIFI_DISPLAY
5661c1d255d3SCy Schubert 	return p2p->wfd_ie_probe_req != NULL;
5662c1d255d3SCy Schubert #else /* CONFIG_WIFI_DISPLAY */
5663c1d255d3SCy Schubert 	return false;
5664c1d255d3SCy Schubert #endif /* CONFIG_WIFI_DISPLAY */
5665c1d255d3SCy Schubert }
5666c1d255d3SCy Schubert 
5667c1d255d3SCy Schubert 
p2p_peer_wfd_enabled(struct p2p_data * p2p,const u8 * peer_addr)5668c1d255d3SCy Schubert bool p2p_peer_wfd_enabled(struct p2p_data *p2p, const u8 *peer_addr)
5669c1d255d3SCy Schubert {
5670c1d255d3SCy Schubert #ifdef CONFIG_WIFI_DISPLAY
5671c1d255d3SCy Schubert 	struct p2p_device *dev;
5672c1d255d3SCy Schubert 
5673c1d255d3SCy Schubert 	dev = p2p_get_device(p2p, peer_addr);
5674c1d255d3SCy Schubert 	return dev && dev->info.wfd_subelems != NULL;
5675c1d255d3SCy Schubert #else /* CONFIG_WIFI_DISPLAY */
5676c1d255d3SCy Schubert 	return false;
5677c1d255d3SCy Schubert #endif /* CONFIG_WIFI_DISPLAY */
5678c1d255d3SCy Schubert }
5679c1d255d3SCy Schubert 
5680c1d255d3SCy Schubert 
is_p2p_allow_6ghz(struct p2p_data * p2p)5681c1d255d3SCy Schubert bool is_p2p_allow_6ghz(struct p2p_data *p2p)
5682c1d255d3SCy Schubert {
5683c1d255d3SCy Schubert 	return p2p->allow_6ghz;
5684c1d255d3SCy Schubert }
5685c1d255d3SCy Schubert 
5686c1d255d3SCy Schubert 
set_p2p_allow_6ghz(struct p2p_data * p2p,bool value)5687c1d255d3SCy Schubert void set_p2p_allow_6ghz(struct p2p_data *p2p, bool value)
5688c1d255d3SCy Schubert {
5689c1d255d3SCy Schubert 	p2p->allow_6ghz = value;
5690c1d255d3SCy Schubert }
5691