p2p.c revision 069fa2c39ec7120656a2ea8cca83c5ee41ddab60
1/*
2 * Wi-Fi Direct - P2P module
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
7 */
8
9#include "includes.h"
10
11#include "common.h"
12#include "eloop.h"
13#include "common/ieee802_11_defs.h"
14#include "common/ieee802_11_common.h"
15#include "common/wpa_ctrl.h"
16#include "wps/wps_i.h"
17#include "p2p_i.h"
18#include "p2p.h"
19
20
21static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
22static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
23static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
24				     const u8 *sa, const u8 *data, size_t len,
25				     int rx_freq);
26static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
27				      const u8 *sa, const u8 *data,
28				      size_t len);
29static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
30static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
31
32
33/*
34 * p2p_scan recovery timeout
35 *
36 * Many drivers are using 30 second timeout on scan results. Allow a bit larger
37 * timeout for this to avoid hitting P2P timeout unnecessarily.
38 */
39#define P2P_SCAN_TIMEOUT 35
40
41/**
42 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
43 * entries will be removed
44 */
45#ifdef ANDROID_P2P
46#define P2P_PEER_EXPIRATION_AGE 30
47#else
48#define P2P_PEER_EXPIRATION_AGE 300
49#endif
50
51#define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
52
53#ifdef ANDROID_P2P
54int p2p_connection_in_progress(struct p2p_data *p2p)
55{
56	int ret = 0;
57
58	switch (p2p->state) {
59		case P2P_CONNECT:
60		case P2P_CONNECT_LISTEN:
61		case P2P_GO_NEG:
62		case P2P_WAIT_PEER_CONNECT:
63		case P2P_WAIT_PEER_IDLE:
64		case P2P_PROVISIONING:
65		case P2P_INVITE:
66		case P2P_INVITE_LISTEN:
67			ret = 1;
68			break;
69
70		default:
71			wpa_printf(MSG_DEBUG, "p2p_connection_in_progress state %d", p2p->state);
72			ret = 0;
73	}
74
75	return ret;
76}
77#endif
78
79static void p2p_expire_peers(struct p2p_data *p2p)
80{
81	struct p2p_device *dev, *n;
82	struct os_time now;
83	size_t i;
84
85	os_get_time(&now);
86	dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
87		if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
88			continue;
89
90		if (p2p->cfg->go_connected &&
91		    p2p->cfg->go_connected(p2p->cfg->cb_ctx,
92					   dev->info.p2p_device_addr)) {
93			/*
94			 * We are connected as a client to a group in which the
95			 * peer is the GO, so do not expire the peer entry.
96			 */
97			os_get_time(&dev->last_seen);
98			continue;
99		}
100
101		for (i = 0; i < p2p->num_groups; i++) {
102			if (p2p_group_is_client_connected(
103				    p2p->groups[i], dev->info.p2p_device_addr))
104				break;
105		}
106		if (i < p2p->num_groups) {
107			/*
108			 * The peer is connected as a client in a group where
109			 * we are the GO, so do not expire the peer entry.
110			 */
111			os_get_time(&dev->last_seen);
112			continue;
113		}
114
115#ifdef ANDROID_P2P
116		/* If Connection is in progress, don't expire the peer
117		*/
118		if (p2p_connection_in_progress(p2p))
119			continue;
120#endif
121
122		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer "
123			"entry " MACSTR, MAC2STR(dev->info.p2p_device_addr));
124#ifdef ANDROID_P2P
125		/* SD_FAIR_POLICY: Update the current sd_dev_list pointer to next device */
126		if(&dev->list == p2p->sd_dev_list)
127			p2p->sd_dev_list = dev->list.next;
128#endif
129		dl_list_del(&dev->list);
130		p2p_device_free(p2p, dev);
131	}
132}
133
134
135static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
136{
137	struct p2p_data *p2p = eloop_ctx;
138	p2p_expire_peers(p2p);
139	eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
140			       p2p_expiration_timeout, p2p, NULL);
141}
142
143
144static const char * p2p_state_txt(int state)
145{
146	switch (state) {
147	case P2P_IDLE:
148		return "IDLE";
149	case P2P_SEARCH:
150		return "SEARCH";
151	case P2P_CONNECT:
152		return "CONNECT";
153	case P2P_CONNECT_LISTEN:
154		return "CONNECT_LISTEN";
155	case P2P_GO_NEG:
156		return "GO_NEG";
157	case P2P_LISTEN_ONLY:
158		return "LISTEN_ONLY";
159	case P2P_WAIT_PEER_CONNECT:
160		return "WAIT_PEER_CONNECT";
161	case P2P_WAIT_PEER_IDLE:
162		return "WAIT_PEER_IDLE";
163	case P2P_SD_DURING_FIND:
164		return "SD_DURING_FIND";
165	case P2P_PROVISIONING:
166		return "PROVISIONING";
167	case P2P_PD_DURING_FIND:
168		return "PD_DURING_FIND";
169	case P2P_INVITE:
170		return "INVITE";
171	case P2P_INVITE_LISTEN:
172		return "INVITE_LISTEN";
173	case P2P_SEARCH_WHEN_READY:
174		return "SEARCH_WHEN_READY";
175	case P2P_CONTINUE_SEARCH_WHEN_READY:
176		return "CONTINUE_SEARCH_WHEN_READY";
177	default:
178		return "?";
179	}
180}
181
182
183u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
184{
185	struct p2p_device *dev = NULL;
186
187	if (!addr || !p2p)
188		return 0;
189
190	dev = p2p_get_device(p2p, addr);
191	if (dev)
192		return dev->wps_prov_info;
193	else
194		return 0;
195}
196
197
198void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
199{
200	struct p2p_device *dev = NULL;
201
202	if (!addr || !p2p)
203		return;
204
205	dev = p2p_get_device(p2p, addr);
206	if (dev)
207		dev->wps_prov_info = 0;
208}
209
210
211void p2p_set_state(struct p2p_data *p2p, int new_state)
212{
213	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s",
214		p2p_state_txt(p2p->state), p2p_state_txt(new_state));
215	p2p->state = new_state;
216}
217
218
219void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
220{
221	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
222		"P2P: Set timeout (state=%s): %u.%06u sec",
223		p2p_state_txt(p2p->state), sec, usec);
224	eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
225	eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
226}
227
228
229void p2p_clear_timeout(struct p2p_data *p2p)
230{
231	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)",
232		p2p_state_txt(p2p->state));
233	eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
234}
235
236
237void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
238		       int status)
239{
240	struct p2p_go_neg_results res;
241	p2p_clear_timeout(p2p);
242	p2p_set_state(p2p, P2P_IDLE);
243	if (p2p->go_neg_peer)
244		p2p->go_neg_peer->wps_method = WPS_NOT_READY;
245	p2p->go_neg_peer = NULL;
246
247	os_memset(&res, 0, sizeof(res));
248	res.status = status;
249	if (peer) {
250		os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr,
251			  ETH_ALEN);
252		os_memcpy(res.peer_interface_addr, peer->intended_addr,
253			  ETH_ALEN);
254	}
255	p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
256}
257
258
259static void p2p_listen_in_find(struct p2p_data *p2p)
260{
261	unsigned int r, tu;
262	int freq;
263	struct wpabuf *ies;
264
265	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
266		"P2P: Starting short listen state (state=%s)",
267		p2p_state_txt(p2p->state));
268
269	freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
270				   p2p->cfg->channel);
271	if (freq < 0) {
272		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
273			"P2P: Unknown regulatory class/channel");
274		return;
275	}
276
277	os_get_random((u8 *) &r, sizeof(r));
278	tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
279	      p2p->min_disc_int) * 100;
280
281	p2p->pending_listen_freq = freq;
282	p2p->pending_listen_sec = 0;
283	p2p->pending_listen_usec = 1024 * tu;
284
285	ies = p2p_build_probe_resp_ies(p2p);
286	if (ies == NULL)
287		return;
288
289	if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
290		    ies) < 0) {
291		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
292			"P2P: Failed to start listen mode");
293		p2p->pending_listen_freq = 0;
294	}
295	wpabuf_free(ies);
296}
297
298
299int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
300{
301	int freq;
302	struct wpabuf *ies;
303
304	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
305		"P2P: Going to listen(only) state");
306
307	freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
308				   p2p->cfg->channel);
309	if (freq < 0) {
310		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
311			"P2P: Unknown regulatory class/channel");
312		return -1;
313	}
314
315	p2p->pending_listen_freq = freq;
316	p2p->pending_listen_sec = timeout / 1000;
317	p2p->pending_listen_usec = (timeout % 1000) * 1000;
318
319	if (p2p->p2p_scan_running) {
320		if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
321			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
322				"P2P: p2p_scan running - connect is already "
323				"pending - skip listen");
324			return 0;
325		}
326		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
327			"P2P: p2p_scan running - delay start of listen state");
328		p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
329		return 0;
330	}
331
332	ies = p2p_build_probe_resp_ies(p2p);
333	if (ies == NULL)
334		return -1;
335
336	if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
337		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
338			"P2P: Failed to start listen mode");
339		p2p->pending_listen_freq = 0;
340		wpabuf_free(ies);
341		return -1;
342	}
343	wpabuf_free(ies);
344
345	p2p_set_state(p2p, P2P_LISTEN_ONLY);
346
347	return 0;
348}
349
350
351static void p2p_device_clear_reported(struct p2p_data *p2p)
352{
353	struct p2p_device *dev;
354	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
355		dev->flags &= ~P2P_DEV_REPORTED;
356}
357
358
359/**
360 * p2p_get_device - Fetch a peer entry
361 * @p2p: P2P module context from p2p_init()
362 * @addr: P2P Device Address of the peer
363 * Returns: Pointer to the device entry or %NULL if not found
364 */
365struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
366{
367	struct p2p_device *dev;
368	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
369		if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
370			return dev;
371	}
372	return NULL;
373}
374
375
376/**
377 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
378 * @p2p: P2P module context from p2p_init()
379 * @addr: P2P Interface Address of the peer
380 * Returns: Pointer to the device entry or %NULL if not found
381 */
382struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
383					     const u8 *addr)
384{
385	struct p2p_device *dev;
386	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
387		if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
388			return dev;
389	}
390	return NULL;
391}
392
393
394/**
395 * p2p_create_device - Create a peer entry
396 * @p2p: P2P module context from p2p_init()
397 * @addr: P2P Device Address of the peer
398 * Returns: Pointer to the device entry or %NULL on failure
399 *
400 * If there is already an entry for the peer, it will be returned instead of
401 * creating a new one.
402 */
403static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
404					     const u8 *addr)
405{
406	struct p2p_device *dev, *oldest = NULL;
407	size_t count = 0;
408
409	dev = p2p_get_device(p2p, addr);
410	if (dev)
411		return dev;
412
413	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
414		count++;
415		if (oldest == NULL ||
416		    os_time_before(&dev->last_seen, &oldest->last_seen))
417			oldest = dev;
418	}
419	if (count + 1 > p2p->cfg->max_peers && oldest) {
420		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
421			"P2P: Remove oldest peer entry to make room for a new "
422			"peer");
423#ifdef ANDROID_P2P
424		/* SD_FAIR_POLICY: Update the current sd_dev_list pointer to next device */
425		if(&oldest->list == p2p->sd_dev_list)
426			p2p->sd_dev_list = oldest->list.next;
427#endif
428		dl_list_del(&oldest->list);
429		p2p_device_free(p2p, oldest);
430	}
431
432	dev = os_zalloc(sizeof(*dev));
433	if (dev == NULL)
434		return NULL;
435	dl_list_add(&p2p->devices, &dev->list);
436	os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
437
438	return dev;
439}
440
441
442static void p2p_copy_client_info(struct p2p_device *dev,
443				 struct p2p_client_info *cli)
444{
445	os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
446	dev->info.device_name[cli->dev_name_len] = '\0';
447	dev->info.dev_capab = cli->dev_capab;
448	dev->info.config_methods = cli->config_methods;
449	os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
450	dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
451	os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
452		  dev->info.wps_sec_dev_type_list_len);
453}
454
455
456static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
457				 const u8 *go_interface_addr, int freq,
458				 const u8 *gi, size_t gi_len)
459{
460	struct p2p_group_info info;
461	size_t c;
462	struct p2p_device *dev;
463
464	if (gi == NULL)
465		return 0;
466
467	if (p2p_group_info_parse(gi, gi_len, &info) < 0)
468		return -1;
469
470	/*
471	 * Clear old data for this group; if the devices are still in the
472	 * group, the information will be restored in the loop following this.
473	 */
474	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
475		if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
476			      ETH_ALEN) == 0) {
477			os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
478			os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
479		}
480	}
481
482	for (c = 0; c < info.num_clients; c++) {
483		struct p2p_client_info *cli = &info.client[c];
484		if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
485			      ETH_ALEN) == 0)
486			continue; /* ignore our own entry */
487		dev = p2p_get_device(p2p, cli->p2p_device_addr);
488		if (dev) {
489			if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
490					  P2P_DEV_PROBE_REQ_ONLY)) {
491				/*
492				 * Update information since we have not
493				 * received this directly from the client.
494				 */
495				p2p_copy_client_info(dev, cli);
496			} else {
497				/*
498				 * Need to update P2P Client Discoverability
499				 * flag since it is valid only in P2P Group
500				 * Info attribute.
501				 */
502				dev->info.dev_capab &=
503					~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
504				dev->info.dev_capab |=
505					cli->dev_capab &
506					P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
507			}
508			if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
509				dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
510			}
511		} else {
512			dev = p2p_create_device(p2p, cli->p2p_device_addr);
513			if (dev == NULL)
514				continue;
515			dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
516			p2p_copy_client_info(dev, cli);
517			dev->oper_freq = freq;
518			p2p->cfg->dev_found(p2p->cfg->cb_ctx,
519					    dev->info.p2p_device_addr,
520					    &dev->info, 1);
521			dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
522		}
523
524		os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
525			  ETH_ALEN);
526		os_get_time(&dev->last_seen);
527		os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
528		os_memcpy(dev->member_in_go_iface, go_interface_addr,
529			  ETH_ALEN);
530	}
531
532	return 0;
533}
534
535
536static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
537			      const struct p2p_message *msg)
538{
539	os_memcpy(dev->info.device_name, msg->device_name,
540		  sizeof(dev->info.device_name));
541
542	if (msg->manufacturer &&
543	    msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
544		os_memset(dev->info.manufacturer, 0,
545			  sizeof(dev->info.manufacturer));
546		os_memcpy(dev->info.manufacturer, msg->manufacturer,
547			  msg->manufacturer_len);
548	}
549
550	if (msg->model_name &&
551	    msg->model_name_len < sizeof(dev->info.model_name)) {
552		os_memset(dev->info.model_name, 0,
553			  sizeof(dev->info.model_name));
554		os_memcpy(dev->info.model_name, msg->model_name,
555			  msg->model_name_len);
556	}
557
558	if (msg->model_number &&
559	    msg->model_number_len < sizeof(dev->info.model_number)) {
560		os_memset(dev->info.model_number, 0,
561			  sizeof(dev->info.model_number));
562		os_memcpy(dev->info.model_number, msg->model_number,
563			  msg->model_number_len);
564	}
565
566	if (msg->serial_number &&
567	    msg->serial_number_len < sizeof(dev->info.serial_number)) {
568		os_memset(dev->info.serial_number, 0,
569			  sizeof(dev->info.serial_number));
570		os_memcpy(dev->info.serial_number, msg->serial_number,
571			  msg->serial_number_len);
572	}
573
574	if (msg->pri_dev_type)
575		os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
576			  sizeof(dev->info.pri_dev_type));
577	else if (msg->wps_pri_dev_type)
578		os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
579			  sizeof(dev->info.pri_dev_type));
580
581	if (msg->wps_sec_dev_type_list) {
582		os_memcpy(dev->info.wps_sec_dev_type_list,
583			  msg->wps_sec_dev_type_list,
584			  msg->wps_sec_dev_type_list_len);
585		dev->info.wps_sec_dev_type_list_len =
586			msg->wps_sec_dev_type_list_len;
587	}
588
589	if (msg->capability) {
590		/*
591		 * P2P Client Discoverability bit is reserved in all frames
592		 * that use this function, so do not change its value here.
593		 */
594		dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
595		dev->info.dev_capab |= msg->capability[0] &
596			~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
597		dev->info.group_capab = msg->capability[1];
598	}
599
600	if (msg->ext_listen_timing) {
601		dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
602		dev->ext_listen_interval =
603			WPA_GET_LE16(msg->ext_listen_timing + 2);
604	}
605
606	if (!probe_req) {
607		dev->info.config_methods = msg->config_methods ?
608			msg->config_methods : msg->wps_config_methods;
609	}
610}
611
612
613/**
614 * p2p_add_device - Add peer entries based on scan results or P2P frames
615 * @p2p: P2P module context from p2p_init()
616 * @addr: Source address of Beacon or Probe Response frame (may be either
617 *	P2P Device Address or P2P Interface Address)
618 * @level: Signal level (signal strength of the received frame from the peer)
619 * @freq: Frequency on which the Beacon or Probe Response frame was received
620 * @ies: IEs from the Beacon or Probe Response frame
621 * @ies_len: Length of ies buffer in octets
622 * @scan_res: Whether this was based on scan results
623 * Returns: 0 on success, -1 on failure
624 *
625 * If the scan result is for a GO, the clients in the group will also be added
626 * to the peer table. This function can also be used with some other frames
627 * like Provision Discovery Request that contains P2P Capability and P2P Device
628 * Info attributes.
629 */
630int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
631		   const u8 *ies, size_t ies_len, int scan_res)
632{
633	struct p2p_device *dev;
634	struct p2p_message msg;
635	const u8 *p2p_dev_addr;
636	int i;
637
638	os_memset(&msg, 0, sizeof(msg));
639	if (p2p_parse_ies(ies, ies_len, &msg)) {
640		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
641			"P2P: Failed to parse P2P IE for a device entry");
642		p2p_parse_free(&msg);
643		return -1;
644	}
645
646	if (msg.p2p_device_addr)
647		p2p_dev_addr = msg.p2p_device_addr;
648	else if (msg.device_id)
649		p2p_dev_addr = msg.device_id;
650	else {
651		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
652			"P2P: Ignore scan data without P2P Device Info or "
653			"P2P Device Id");
654		p2p_parse_free(&msg);
655		return -1;
656	}
657
658	if (!is_zero_ether_addr(p2p->peer_filter) &&
659	    os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
660		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
661			"filter for " MACSTR " due to peer filter",
662			MAC2STR(p2p_dev_addr));
663		return 0;
664	}
665
666	dev = p2p_create_device(p2p, p2p_dev_addr);
667	if (dev == NULL) {
668		p2p_parse_free(&msg);
669		return -1;
670	}
671	os_get_time(&dev->last_seen);
672	dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
673
674	if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
675		os_memcpy(dev->interface_addr, addr, ETH_ALEN);
676	if (msg.ssid &&
677	    (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
678	     os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
679	     != 0)) {
680		os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
681		dev->oper_ssid_len = msg.ssid[1];
682	}
683
684	if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
685	    *msg.ds_params >= 1 && *msg.ds_params <= 14) {
686		int ds_freq;
687		if (*msg.ds_params == 14)
688			ds_freq = 2484;
689		else
690			ds_freq = 2407 + *msg.ds_params * 5;
691		if (freq != ds_freq) {
692			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
693				"P2P: Update Listen frequency based on DS "
694				"Parameter Set IE: %d -> %d MHz",
695				freq, ds_freq);
696			freq = ds_freq;
697		}
698	}
699
700	if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
701		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
702			"P2P: Update Listen frequency based on scan "
703			"results (" MACSTR " %d -> %d MHz (DS param %d)",
704			MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
705			freq, msg.ds_params ? *msg.ds_params : -1);
706	}
707	if (scan_res) {
708		dev->listen_freq = freq;
709		if (msg.group_info)
710			dev->oper_freq = freq;
711	}
712	dev->info.level = level;
713
714	p2p_copy_wps_info(dev, 0, &msg);
715
716	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
717		wpabuf_free(dev->info.wps_vendor_ext[i]);
718		dev->info.wps_vendor_ext[i] = NULL;
719	}
720
721	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
722		if (msg.wps_vendor_ext[i] == NULL)
723			break;
724		dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
725			msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
726		if (dev->info.wps_vendor_ext[i] == NULL)
727			break;
728	}
729
730	if (msg.wfd_subelems) {
731		wpabuf_free(dev->info.wfd_subelems);
732		dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
733	}
734
735	if (scan_res) {
736		p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
737				      msg.group_info, msg.group_info_len);
738	}
739
740	p2p_parse_free(&msg);
741
742	if (p2p_pending_sd_req(p2p, dev))
743		dev->flags |= P2P_DEV_SD_SCHEDULE;
744
745	if (dev->flags & P2P_DEV_REPORTED)
746		return 0;
747
748	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
749		"P2P: Peer found with Listen frequency %d MHz", freq);
750	if (dev->flags & P2P_DEV_USER_REJECTED) {
751		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
752			"P2P: Do not report rejected device");
753		return 0;
754	}
755
756	p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
757			    !(dev->flags & P2P_DEV_REPORTED_ONCE));
758	dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
759
760	return 0;
761}
762
763
764static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
765{
766	int i;
767
768	if (p2p->go_neg_peer == dev) {
769		/*
770		 * If GO Negotiation is in progress, report that it has failed.
771		 */
772		p2p_go_neg_failed(p2p, dev, -1);
773		p2p->go_neg_peer = NULL;
774	}
775	if (p2p->invite_peer == dev)
776		p2p->invite_peer = NULL;
777	if (p2p->sd_peer == dev)
778		p2p->sd_peer = NULL;
779	if (p2p->pending_client_disc_go == dev)
780		p2p->pending_client_disc_go = NULL;
781
782	/* dev_lost() device, but only if it was previously dev_found() */
783	if (dev->flags & P2P_DEV_REPORTED_ONCE)
784		p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
785				   dev->info.p2p_device_addr);
786
787	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
788		wpabuf_free(dev->info.wps_vendor_ext[i]);
789		dev->info.wps_vendor_ext[i] = NULL;
790	}
791
792	wpabuf_free(dev->info.wfd_subelems);
793
794	os_free(dev);
795}
796
797
798static int p2p_get_next_prog_freq(struct p2p_data *p2p)
799{
800	struct p2p_channels *c;
801	struct p2p_reg_class *cla;
802	size_t cl, ch;
803	int found = 0;
804	u8 reg_class;
805	u8 channel;
806	int freq;
807
808	c = &p2p->cfg->channels;
809	for (cl = 0; cl < c->reg_classes; cl++) {
810		cla = &c->reg_class[cl];
811		if (cla->reg_class != p2p->last_prog_scan_class)
812			continue;
813		for (ch = 0; ch < cla->channels; ch++) {
814			if (cla->channel[ch] == p2p->last_prog_scan_chan) {
815				found = 1;
816				break;
817			}
818		}
819		if (found)
820			break;
821	}
822
823	if (!found) {
824		/* Start from beginning */
825		reg_class = c->reg_class[0].reg_class;
826		channel = c->reg_class[0].channel[0];
827	} else {
828		/* Pick the next channel */
829		ch++;
830		if (ch == cla->channels) {
831			cl++;
832			if (cl == c->reg_classes)
833				cl = 0;
834			ch = 0;
835		}
836		reg_class = c->reg_class[cl].reg_class;
837		channel = c->reg_class[cl].channel[ch];
838	}
839
840	freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
841	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
842		"channel: reg_class %u channel %u -> %d MHz",
843		reg_class, channel, freq);
844	p2p->last_prog_scan_class = reg_class;
845	p2p->last_prog_scan_chan = channel;
846
847	if (freq == 2412 || freq == 2437 || freq == 2462)
848		return 0; /* No need to add social channels */
849	return freq;
850}
851
852
853static void p2p_search(struct p2p_data *p2p)
854{
855	int freq = 0;
856	enum p2p_scan_type type;
857	u16 pw_id = DEV_PW_DEFAULT;
858	int res;
859
860	if (p2p->drv_in_listen) {
861		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
862			"in Listen state - wait for it to end before "
863			"continuing");
864		return;
865	}
866	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
867
868	if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
869	    (freq = p2p_get_next_prog_freq(p2p)) > 0) {
870		type = P2P_SCAN_SOCIAL_PLUS_ONE;
871		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
872			"(+ freq %u)", freq);
873	} else {
874		type = P2P_SCAN_SOCIAL;
875		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
876	}
877
878	res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
879				 p2p->num_req_dev_types, p2p->req_dev_types,
880				 p2p->find_dev_id, pw_id);
881	if (res < 0) {
882		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
883			"P2P: Scan request failed");
884		p2p_continue_find(p2p);
885	} else if (res == 1) {
886		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
887			"p2p_scan at this point - will try again after "
888			"previous scan completes");
889		p2p_set_state(p2p, P2P_CONTINUE_SEARCH_WHEN_READY);
890	} else {
891		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
892		p2p->p2p_scan_running = 1;
893		eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
894		eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
895				       p2p, NULL);
896	}
897}
898
899
900static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
901{
902	struct p2p_data *p2p = eloop_ctx;
903	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
904	p2p_stop_find(p2p);
905}
906
907
908static int p2p_run_after_scan(struct p2p_data *p2p)
909{
910	struct p2p_device *dev;
911	enum p2p_after_scan op;
912
913	if (p2p->after_scan_tx) {
914		/* TODO: schedule p2p_run_after_scan to be called from TX
915		 * status callback(?) */
916		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
917			"Action frame at p2p_scan completion");
918		p2p->cfg->send_action(p2p->cfg->cb_ctx,
919				      p2p->after_scan_tx->freq,
920				      p2p->after_scan_tx->dst,
921				      p2p->after_scan_tx->src,
922				      p2p->after_scan_tx->bssid,
923				      (u8 *) (p2p->after_scan_tx + 1),
924				      p2p->after_scan_tx->len,
925				      p2p->after_scan_tx->wait_time);
926		os_free(p2p->after_scan_tx);
927		p2p->after_scan_tx = NULL;
928#ifdef ANDROID_P2P
929		/* For SD frames, there is a scenario, where we can receive a SD request frame during p2p_scan.
930		 * At that moment, we will send the SD response from this context. After sending the SD response,
931		 * we need to continue p2p_find. But if we return 1 from here, p2p_find is going to be stopped.
932		 */
933		return 0;
934#else
935		return 1;
936#endif
937	}
938
939	op = p2p->start_after_scan;
940	p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
941	switch (op) {
942	case P2P_AFTER_SCAN_NOTHING:
943		break;
944	case P2P_AFTER_SCAN_LISTEN:
945		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
946			"requested Listen state");
947		p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
948			   p2p->pending_listen_usec / 1000);
949		return 1;
950	case P2P_AFTER_SCAN_CONNECT:
951		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
952			"requested connect with " MACSTR,
953			MAC2STR(p2p->after_scan_peer));
954		dev = p2p_get_device(p2p, p2p->after_scan_peer);
955		if (dev == NULL) {
956			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
957				"known anymore");
958			break;
959		}
960		p2p_connect_send(p2p, dev);
961		return 1;
962	}
963
964	return 0;
965}
966
967
968static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
969{
970	struct p2p_data *p2p = eloop_ctx;
971	int running;
972	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
973		"(running=%d)", p2p->p2p_scan_running);
974	running = p2p->p2p_scan_running;
975	/* Make sure we recover from missed scan results callback */
976	p2p->p2p_scan_running = 0;
977
978	if (running)
979		p2p_run_after_scan(p2p);
980}
981
982
983static void p2p_free_req_dev_types(struct p2p_data *p2p)
984{
985	p2p->num_req_dev_types = 0;
986	os_free(p2p->req_dev_types);
987	p2p->req_dev_types = NULL;
988}
989
990
991int p2p_find(struct p2p_data *p2p, unsigned int timeout,
992	     enum p2p_discovery_type type,
993	     unsigned int num_req_dev_types, const u8 *req_dev_types,
994	     const u8 *dev_id, unsigned int search_delay)
995{
996	int res;
997
998	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
999		type);
1000	if (p2p->p2p_scan_running) {
1001		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
1002			"already running");
1003	}
1004
1005	p2p_free_req_dev_types(p2p);
1006	if (req_dev_types && num_req_dev_types) {
1007		p2p->req_dev_types = os_malloc(num_req_dev_types *
1008					       WPS_DEV_TYPE_LEN);
1009		if (p2p->req_dev_types == NULL)
1010			return -1;
1011		os_memcpy(p2p->req_dev_types, req_dev_types,
1012			  num_req_dev_types * WPS_DEV_TYPE_LEN);
1013		p2p->num_req_dev_types = num_req_dev_types;
1014	}
1015
1016	if (dev_id) {
1017		os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1018		p2p->find_dev_id = p2p->find_dev_id_buf;
1019	} else
1020		p2p->find_dev_id = NULL;
1021
1022	p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1023	p2p_clear_timeout(p2p);
1024	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1025	p2p->find_type = type;
1026	p2p_device_clear_reported(p2p);
1027	p2p_set_state(p2p, P2P_SEARCH);
1028	p2p->search_delay = search_delay;
1029	p2p->in_search_delay = 0;
1030	eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1031	p2p->last_p2p_find_timeout = timeout;
1032	if (timeout)
1033		eloop_register_timeout(timeout, 0, p2p_find_timeout,
1034				       p2p, NULL);
1035	switch (type) {
1036	case P2P_FIND_START_WITH_FULL:
1037	case P2P_FIND_PROGRESSIVE:
1038		res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1039					 p2p->num_req_dev_types,
1040					 p2p->req_dev_types, dev_id,
1041					 DEV_PW_DEFAULT);
1042		break;
1043	case P2P_FIND_ONLY_SOCIAL:
1044		res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1045					 p2p->num_req_dev_types,
1046					 p2p->req_dev_types, dev_id,
1047					 DEV_PW_DEFAULT);
1048		break;
1049	default:
1050		return -1;
1051	}
1052
1053	if (res == 0) {
1054		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
1055		p2p->p2p_scan_running = 1;
1056		eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1057		eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1058				       p2p, NULL);
1059	} else if (res == 1) {
1060		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
1061			"p2p_scan at this point - will try again after "
1062			"previous scan completes");
1063		res = 0;
1064		p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
1065		eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1066	} else {
1067		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
1068			"p2p_scan");
1069		p2p_set_state(p2p, P2P_IDLE);
1070		eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1071	}
1072
1073	return res;
1074}
1075
1076#ifdef ANDROID_P2P
1077int p2p_search_pending(struct p2p_data *p2p)
1078{
1079	if(p2p == NULL)
1080		return 0;
1081
1082	if(p2p->state == P2P_SEARCH_WHEN_READY)
1083		return 1;
1084
1085	return 0;
1086}
1087#endif
1088
1089int p2p_other_scan_completed(struct p2p_data *p2p)
1090{
1091	if (p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY) {
1092		p2p_set_state(p2p, P2P_SEARCH);
1093		p2p_search(p2p);
1094		return 1;
1095	}
1096	if (p2p->state != P2P_SEARCH_WHEN_READY)
1097		return 0;
1098	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find "
1099		"now that previous scan was completed");
1100	if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
1101		     p2p->num_req_dev_types, p2p->req_dev_types,
1102		     p2p->find_dev_id, p2p->search_delay) < 0)
1103		return 0;
1104	return 1;
1105}
1106
1107
1108void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1109{
1110	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
1111	eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1112	p2p_clear_timeout(p2p);
1113	if (p2p->state == P2P_SEARCH)
1114		wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1115	p2p_set_state(p2p, P2P_IDLE);
1116	p2p_free_req_dev_types(p2p);
1117	p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1118	p2p->go_neg_peer = NULL;
1119	p2p->sd_peer = NULL;
1120	p2p->invite_peer = NULL;
1121	p2p_stop_listen_for_freq(p2p, freq);
1122}
1123
1124
1125void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1126{
1127	if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
1128		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
1129			"since we are on correct channel for response");
1130		return;
1131	}
1132	if (p2p->in_listen) {
1133		p2p->in_listen = 0;
1134		p2p_clear_timeout(p2p);
1135	}
1136	if (p2p->drv_in_listen) {
1137		/*
1138		 * The driver may not deliver callback to p2p_listen_end()
1139		 * when the operation gets canceled, so clear the internal
1140		 * variable that is tracking driver state.
1141		 */
1142		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear "
1143			"drv_in_listen (%d)", p2p->drv_in_listen);
1144		p2p->drv_in_listen = 0;
1145	}
1146	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1147}
1148
1149
1150void p2p_stop_find(struct p2p_data *p2p)
1151{
1152	p2p_stop_find_for_freq(p2p, 0);
1153}
1154
1155
1156static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
1157{
1158	if (force_freq) {
1159		u8 op_reg_class, op_channel;
1160		if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
1161					&op_reg_class, &op_channel) < 0) {
1162			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1163				"P2P: Unsupported frequency %u MHz",
1164				force_freq);
1165			return -1;
1166		}
1167		if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
1168					   op_channel)) {
1169			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1170				"P2P: Frequency %u MHz (oper_class %u "
1171				"channel %u) not allowed for P2P",
1172				force_freq, op_reg_class, op_channel);
1173			return -1;
1174		}
1175		p2p->op_reg_class = op_reg_class;
1176		p2p->op_channel = op_channel;
1177#ifndef ANDROID_P2P
1178		p2p->channels.reg_classes = 1;
1179		p2p->channels.reg_class[0].channels = 1;
1180		p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1181		p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
1182#else
1183		if(p2p->cfg->p2p_concurrency == P2P_MULTI_CHANNEL_CONCURRENT) {
1184			/* We we are requesting for a preferred channel. But since
1185			 * are multichannel concurrent, we have to poplulate the
1186			 * p2p_channels with list of channels that we support.
1187			 */
1188#ifdef ANDROID_P2P
1189			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "Full channel list");
1190#endif
1191			os_memcpy(&p2p->channels, &p2p->cfg->channels,
1192				sizeof(struct p2p_channels));
1193		} else {
1194#ifdef ANDROID_P2P
1195			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "Single channel list %d", p2p->op_channel);
1196#endif
1197			p2p->channels.reg_classes = 1;
1198			p2p->channels.reg_class[0].channels = 1;
1199			p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1200			p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
1201		}
1202#endif
1203	} else {
1204		u8 op_reg_class, op_channel;
1205
1206		if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1207		    p2p_supported_freq(p2p, p2p->best_freq_overall) &&
1208		    p2p_freq_to_channel(p2p->cfg->country,
1209					p2p->best_freq_overall,
1210					&op_reg_class, &op_channel) == 0) {
1211			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1212				"P2P: Select best overall channel as "
1213				"operating channel preference");
1214			p2p->op_reg_class = op_reg_class;
1215			p2p->op_channel = op_channel;
1216		} else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1217			   p2p_supported_freq(p2p, p2p->best_freq_5) &&
1218			   p2p_freq_to_channel(p2p->cfg->country,
1219					       p2p->best_freq_5,
1220					       &op_reg_class, &op_channel) ==
1221			   0) {
1222			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1223				"P2P: Select best 5 GHz channel as "
1224				"operating channel preference");
1225			p2p->op_reg_class = op_reg_class;
1226			p2p->op_channel = op_channel;
1227		} else if (!p2p->cfg->cfg_op_channel &&
1228			   p2p->best_freq_24 > 0 &&
1229			   p2p_supported_freq(p2p, p2p->best_freq_24) &&
1230			   p2p_freq_to_channel(p2p->cfg->country,
1231					       p2p->best_freq_24,
1232					       &op_reg_class, &op_channel) ==
1233			   0) {
1234			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1235				"P2P: Select best 2.4 GHz channel as "
1236				"operating channel preference");
1237			p2p->op_reg_class = op_reg_class;
1238			p2p->op_channel = op_channel;
1239		} else {
1240			p2p->op_reg_class = p2p->cfg->op_reg_class;
1241			p2p->op_channel = p2p->cfg->op_channel;
1242		}
1243
1244		os_memcpy(&p2p->channels, &p2p->cfg->channels,
1245			  sizeof(struct p2p_channels));
1246	}
1247	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1248		"P2P: Own preference for operation channel: "
1249		"Operating Class %u Channel %u%s",
1250		p2p->op_reg_class, p2p->op_channel,
1251		force_freq ? " (forced)" : "");
1252
1253	return 0;
1254}
1255
1256
1257static void p2p_set_dev_persistent(struct p2p_device *dev,
1258				   int persistent_group)
1259{
1260	switch (persistent_group) {
1261	case 0:
1262		dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1263				P2P_DEV_PREFER_PERSISTENT_RECONN);
1264		break;
1265	case 1:
1266		dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1267		dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1268		break;
1269	case 2:
1270		dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1271			P2P_DEV_PREFER_PERSISTENT_RECONN;
1272		break;
1273	}
1274}
1275
1276
1277int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1278		enum p2p_wps_method wps_method,
1279		int go_intent, const u8 *own_interface_addr,
1280		unsigned int force_freq, int persistent_group,
1281		const u8 *force_ssid, size_t force_ssid_len,
1282		int pd_before_go_neg)
1283{
1284	struct p2p_device *dev;
1285
1286	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1287		"P2P: Request to start group negotiation - peer=" MACSTR
1288		"  GO Intent=%d  Intended Interface Address=" MACSTR
1289		" wps_method=%d persistent_group=%d pd_before_go_neg=%d force_freq %d",
1290		MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1291		wps_method, persistent_group, pd_before_go_neg, force_freq);
1292
1293	if (p2p_prepare_channel(p2p, force_freq) < 0)
1294		return -1;
1295
1296	dev = p2p_get_device(p2p, peer_addr);
1297	if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1298		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1299			"P2P: Cannot connect to unknown P2P Device " MACSTR,
1300			MAC2STR(peer_addr));
1301		return -1;
1302	}
1303
1304	if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1305		if (!(dev->info.dev_capab &
1306		      P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1307			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1308				"P2P: Cannot connect to P2P Device " MACSTR
1309				" that is in a group and is not discoverable",
1310				MAC2STR(peer_addr));
1311			return -1;
1312		}
1313		if (dev->oper_freq <= 0) {
1314			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1315				"P2P: Cannot connect to P2P Device " MACSTR
1316				" with incomplete information",
1317				MAC2STR(peer_addr));
1318			return -1;
1319		}
1320
1321		/*
1322		 * First, try to connect directly. If the peer does not
1323		 * acknowledge frames, assume it is sleeping and use device
1324		 * discoverability via the GO at that point.
1325		 */
1326	}
1327
1328	p2p->ssid_set = 0;
1329	if (force_ssid) {
1330		wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1331				  force_ssid, force_ssid_len);
1332		os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1333		p2p->ssid_len = force_ssid_len;
1334		p2p->ssid_set = 1;
1335	}
1336
1337	dev->flags &= ~P2P_DEV_NOT_YET_READY;
1338	dev->flags &= ~P2P_DEV_USER_REJECTED;
1339	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1340	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1341	if (pd_before_go_neg)
1342		dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
1343	else
1344		dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
1345	dev->connect_reqs = 0;
1346	dev->go_neg_req_sent = 0;
1347	dev->go_state = UNKNOWN_GO;
1348	p2p_set_dev_persistent(dev, persistent_group);
1349	p2p->go_intent = go_intent;
1350	os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1351
1352	if (p2p->state != P2P_IDLE)
1353		p2p_stop_find(p2p);
1354
1355	if (p2p->after_scan_tx) {
1356		/*
1357		 * We need to drop the pending frame to avoid issues with the
1358		 * new GO Negotiation, e.g., when the pending frame was from a
1359		 * previous attempt at starting a GO Negotiation.
1360		 */
1361		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
1362			"previous pending Action frame TX that was waiting "
1363			"for p2p_scan completion");
1364		os_free(p2p->after_scan_tx);
1365		p2p->after_scan_tx = NULL;
1366	}
1367
1368	dev->wps_method = wps_method;
1369	dev->status = P2P_SC_SUCCESS;
1370
1371	if (force_freq)
1372		dev->flags |= P2P_DEV_FORCE_FREQ;
1373	else
1374		dev->flags &= ~P2P_DEV_FORCE_FREQ;
1375
1376	if (p2p->p2p_scan_running) {
1377		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1378			"P2P: p2p_scan running - delay connect send");
1379		p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1380		os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1381		return 0;
1382	}
1383	p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1384
1385	return p2p_connect_send(p2p, dev);
1386}
1387
1388
1389int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1390		  enum p2p_wps_method wps_method,
1391		  int go_intent, const u8 *own_interface_addr,
1392		  unsigned int force_freq, int persistent_group,
1393		  const u8 *force_ssid, size_t force_ssid_len)
1394{
1395	struct p2p_device *dev;
1396
1397	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1398		"P2P: Request to authorize group negotiation - peer=" MACSTR
1399		"  GO Intent=%d  Intended Interface Address=" MACSTR
1400		" wps_method=%d  persistent_group=%d",
1401		MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1402		wps_method, persistent_group);
1403
1404	if (p2p_prepare_channel(p2p, force_freq) < 0)
1405		return -1;
1406
1407	dev = p2p_get_device(p2p, peer_addr);
1408	if (dev == NULL) {
1409		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1410			"P2P: Cannot authorize unknown P2P Device " MACSTR,
1411			MAC2STR(peer_addr));
1412		return -1;
1413	}
1414
1415	p2p->ssid_set = 0;
1416	if (force_ssid) {
1417		wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1418				  force_ssid, force_ssid_len);
1419		os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1420		p2p->ssid_len = force_ssid_len;
1421		p2p->ssid_set = 1;
1422	}
1423
1424	dev->flags &= ~P2P_DEV_NOT_YET_READY;
1425	dev->flags &= ~P2P_DEV_USER_REJECTED;
1426	dev->go_neg_req_sent = 0;
1427	dev->go_state = UNKNOWN_GO;
1428	p2p_set_dev_persistent(dev, persistent_group);
1429	p2p->go_intent = go_intent;
1430	os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1431
1432	dev->wps_method = wps_method;
1433	dev->status = P2P_SC_SUCCESS;
1434
1435	if (force_freq)
1436		dev->flags |= P2P_DEV_FORCE_FREQ;
1437	else
1438		dev->flags &= ~P2P_DEV_FORCE_FREQ;
1439
1440	return 0;
1441}
1442
1443
1444void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1445		      struct p2p_device *dev, struct p2p_message *msg)
1446{
1447	os_get_time(&dev->last_seen);
1448
1449	p2p_copy_wps_info(dev, 0, msg);
1450
1451	if (msg->listen_channel) {
1452		int freq;
1453		freq = p2p_channel_to_freq((char *) msg->listen_channel,
1454					   msg->listen_channel[3],
1455					   msg->listen_channel[4]);
1456		if (freq < 0) {
1457			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1458				"P2P: Unknown peer Listen channel: "
1459				"country=%c%c(0x%02x) reg_class=%u channel=%u",
1460				msg->listen_channel[0],
1461				msg->listen_channel[1],
1462				msg->listen_channel[2],
1463				msg->listen_channel[3],
1464				msg->listen_channel[4]);
1465		} else {
1466			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1467				"peer " MACSTR " Listen channel: %u -> %u MHz",
1468				MAC2STR(dev->info.p2p_device_addr),
1469				dev->listen_freq, freq);
1470			dev->listen_freq = freq;
1471		}
1472	}
1473
1474	if (msg->wfd_subelems) {
1475		wpabuf_free(dev->info.wfd_subelems);
1476		dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1477	}
1478
1479	if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1480		dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1481		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1482			"P2P: Completed device entry based on data from "
1483			"GO Negotiation Request");
1484	} else {
1485		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1486			"P2P: Created device entry based on GO Neg Req: "
1487			MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1488			"listen_freq=%d",
1489			MAC2STR(dev->info.p2p_device_addr),
1490			dev->info.dev_capab, dev->info.group_capab,
1491			dev->info.device_name, dev->listen_freq);
1492	}
1493
1494	dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1495
1496	if (dev->flags & P2P_DEV_USER_REJECTED) {
1497		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1498			"P2P: Do not report rejected device");
1499		return;
1500	}
1501
1502	p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1503			    !(dev->flags & P2P_DEV_REPORTED_ONCE));
1504	dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1505}
1506
1507
1508void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1509{
1510	os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1511	p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1512	os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1513		  p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1514	*ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1515}
1516
1517
1518int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1519{
1520	p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1521	p2p_random(params->passphrase, 8);
1522	return 0;
1523}
1524
1525
1526void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1527{
1528	struct p2p_go_neg_results res;
1529	int go = peer->go_state == LOCAL_GO;
1530	struct p2p_channels intersection;
1531	int freqs;
1532	size_t i, j;
1533
1534	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1535		"P2P: GO Negotiation with " MACSTR " completed (%s will be "
1536		"GO)", MAC2STR(peer->info.p2p_device_addr),
1537		go ? "local end" : "peer");
1538
1539	os_memset(&res, 0, sizeof(res));
1540	res.role_go = go;
1541	os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1542	os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1543	res.wps_method = peer->wps_method;
1544	if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1545		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1546			res.persistent_group = 2;
1547		else
1548			res.persistent_group = 1;
1549	}
1550
1551	if (go) {
1552		/* Setup AP mode for WPS provisioning */
1553		res.freq = p2p_channel_to_freq(p2p->cfg->country,
1554					       p2p->op_reg_class,
1555					       p2p->op_channel);
1556		os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1557		res.ssid_len = p2p->ssid_len;
1558		p2p_random(res.passphrase, 8);
1559	} else {
1560		res.freq = peer->oper_freq;
1561		if (p2p->ssid_len) {
1562			os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1563			res.ssid_len = p2p->ssid_len;
1564		}
1565	}
1566
1567	p2p_channels_intersect(&p2p->channels, &peer->channels,
1568			       &intersection);
1569	freqs = 0;
1570	for (i = 0; i < intersection.reg_classes; i++) {
1571		struct p2p_reg_class *c = &intersection.reg_class[i];
1572		if (freqs + 1 == P2P_MAX_CHANNELS)
1573			break;
1574		for (j = 0; j < c->channels; j++) {
1575			int freq;
1576			if (freqs + 1 == P2P_MAX_CHANNELS)
1577				break;
1578			freq = p2p_channel_to_freq(peer->country, c->reg_class,
1579						   c->channel[j]);
1580			if (freq < 0)
1581				continue;
1582			res.freq_list[freqs++] = freq;
1583		}
1584	}
1585
1586	res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1587
1588	p2p_clear_timeout(p2p);
1589	p2p->ssid_set = 0;
1590	peer->go_neg_req_sent = 0;
1591	peer->wps_method = WPS_NOT_READY;
1592
1593	p2p_set_state(p2p, P2P_PROVISIONING);
1594	p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1595}
1596
1597
1598static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1599			      const u8 *data, size_t len, int rx_freq)
1600{
1601	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1602		"P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1603	wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1604
1605	if (len < 1)
1606		return;
1607
1608	switch (data[0]) {
1609	case P2P_GO_NEG_REQ:
1610		p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1611		break;
1612	case P2P_GO_NEG_RESP:
1613		p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1614		break;
1615	case P2P_GO_NEG_CONF:
1616		p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1617		break;
1618	case P2P_INVITATION_REQ:
1619		p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1620					   rx_freq);
1621		break;
1622	case P2P_INVITATION_RESP:
1623		p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1624		break;
1625	case P2P_PROV_DISC_REQ:
1626		p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1627		break;
1628	case P2P_PROV_DISC_RESP:
1629		p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1630		break;
1631	case P2P_DEV_DISC_REQ:
1632		p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1633		break;
1634	case P2P_DEV_DISC_RESP:
1635		p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1636		break;
1637	default:
1638		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1639			"P2P: Unsupported P2P Public Action frame type %d",
1640			data[0]);
1641		break;
1642	}
1643}
1644
1645
1646static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1647				 const u8 *sa, const u8 *bssid, const u8 *data,
1648				 size_t len, int freq)
1649{
1650	if (len < 1)
1651		return;
1652
1653	switch (data[0]) {
1654	case WLAN_PA_VENDOR_SPECIFIC:
1655		data++;
1656		len--;
1657		if (len < 3)
1658			return;
1659		if (WPA_GET_BE24(data) != OUI_WFA)
1660			return;
1661
1662		data += 3;
1663		len -= 3;
1664		if (len < 1)
1665			return;
1666
1667		if (*data != P2P_OUI_TYPE)
1668			return;
1669
1670		p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1671		break;
1672	case WLAN_PA_GAS_INITIAL_REQ:
1673		p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1674		break;
1675	case WLAN_PA_GAS_INITIAL_RESP:
1676		p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1677		break;
1678	case WLAN_PA_GAS_COMEBACK_REQ:
1679		p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1680		break;
1681	case WLAN_PA_GAS_COMEBACK_RESP:
1682		p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1683		break;
1684	}
1685}
1686
1687
1688void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1689		   const u8 *bssid, u8 category,
1690		   const u8 *data, size_t len, int freq)
1691{
1692	if (category == WLAN_ACTION_PUBLIC) {
1693		p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1694		return;
1695	}
1696
1697	if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1698		return;
1699
1700	if (len < 4)
1701		return;
1702
1703	if (WPA_GET_BE24(data) != OUI_WFA)
1704		return;
1705	data += 3;
1706	len -= 3;
1707
1708	if (*data != P2P_OUI_TYPE)
1709		return;
1710	data++;
1711	len--;
1712
1713	/* P2P action frame */
1714	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1715		"P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1716	wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1717
1718	if (len < 1)
1719		return;
1720	switch (data[0]) {
1721	case P2P_NOA:
1722		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1723			"P2P: Received P2P Action - Notice of Absence");
1724		/* TODO */
1725		break;
1726	case P2P_PRESENCE_REQ:
1727		p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1728		break;
1729	case P2P_PRESENCE_RESP:
1730		p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1731		break;
1732	case P2P_GO_DISC_REQ:
1733		p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1734		break;
1735	default:
1736		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1737			"P2P: Received P2P Action - unknown type %u", data[0]);
1738		break;
1739	}
1740}
1741
1742
1743static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1744{
1745	struct p2p_data *p2p = eloop_ctx;
1746	if (p2p->go_neg_peer == NULL)
1747		return;
1748	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1749	p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1750	p2p_connect_send(p2p, p2p->go_neg_peer);
1751}
1752
1753
1754static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1755{
1756	struct p2p_data *p2p = eloop_ctx;
1757	if (p2p->invite_peer == NULL)
1758		return;
1759	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1760	p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1761}
1762
1763
1764static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1765				       const u8 *ie, size_t ie_len)
1766{
1767	struct p2p_message msg;
1768	struct p2p_device *dev;
1769
1770	os_memset(&msg, 0, sizeof(msg));
1771	if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1772	{
1773		p2p_parse_free(&msg);
1774		return; /* not a P2P probe */
1775	}
1776
1777	if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1778	    os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1779	    != 0) {
1780		/* The Probe Request is not part of P2P Device Discovery. It is
1781		 * not known whether the source address of the frame is the P2P
1782		 * Device Address or P2P Interface Address. Do not add a new
1783		 * peer entry based on this frames.
1784		 */
1785		p2p_parse_free(&msg);
1786		return;
1787	}
1788
1789	dev = p2p_get_device(p2p, addr);
1790	if (dev) {
1791		if (dev->country[0] == 0 && msg.listen_channel)
1792			os_memcpy(dev->country, msg.listen_channel, 3);
1793		os_get_time(&dev->last_seen);
1794		p2p_parse_free(&msg);
1795		return; /* already known */
1796	}
1797
1798	dev = p2p_create_device(p2p, addr);
1799	if (dev == NULL) {
1800		p2p_parse_free(&msg);
1801		return;
1802	}
1803
1804	os_get_time(&dev->last_seen);
1805	dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1806
1807	if (msg.listen_channel) {
1808		os_memcpy(dev->country, msg.listen_channel, 3);
1809		dev->listen_freq = p2p_channel_to_freq(dev->country,
1810						       msg.listen_channel[3],
1811						       msg.listen_channel[4]);
1812	}
1813
1814	p2p_copy_wps_info(dev, 1, &msg);
1815
1816	if (msg.wfd_subelems) {
1817		wpabuf_free(dev->info.wfd_subelems);
1818		dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
1819	}
1820
1821	p2p_parse_free(&msg);
1822
1823	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1824		"P2P: Created device entry based on Probe Req: " MACSTR
1825		" dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1826		MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1827		dev->info.group_capab, dev->info.device_name,
1828		dev->listen_freq);
1829}
1830
1831
1832struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1833						const u8 *addr,
1834						struct p2p_message *msg)
1835{
1836	struct p2p_device *dev;
1837
1838	dev = p2p_get_device(p2p, addr);
1839	if (dev) {
1840		os_get_time(&dev->last_seen);
1841		return dev; /* already known */
1842	}
1843
1844	dev = p2p_create_device(p2p, addr);
1845	if (dev == NULL)
1846		return NULL;
1847
1848	p2p_add_dev_info(p2p, addr, dev, msg);
1849
1850	return dev;
1851}
1852
1853
1854static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1855{
1856	if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1857		return 1;
1858	if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1859	    WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1860	    WPA_GET_BE16(&req_dev_type[6]) == 0)
1861		return 1; /* Category match with wildcard OUI/sub-category */
1862	return 0;
1863}
1864
1865
1866int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1867			size_t num_req_dev_type)
1868{
1869	size_t i;
1870	for (i = 0; i < num_req_dev_type; i++) {
1871		if (dev_type_match(dev_type, req_dev_type[i]))
1872			return 1;
1873	}
1874	return 0;
1875}
1876
1877
1878/**
1879 * p2p_match_dev_type - Match local device type with requested type
1880 * @p2p: P2P module context from p2p_init()
1881 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1882 * Returns: 1 on match, 0 on mismatch
1883 *
1884 * This function can be used to match the Requested Device Type attribute in
1885 * WPS IE with the local device types for deciding whether to reply to a Probe
1886 * Request frame.
1887 */
1888int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1889{
1890	struct wps_parse_attr attr;
1891	size_t i;
1892
1893	if (wps_parse_msg(wps, &attr))
1894		return 1; /* assume no Requested Device Type attributes */
1895
1896	if (attr.num_req_dev_type == 0)
1897		return 1; /* no Requested Device Type attributes -> match */
1898
1899	if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1900				attr.num_req_dev_type))
1901		return 1; /* Own Primary Device Type matches */
1902
1903	for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1904		if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1905					attr.req_dev_type,
1906					attr.num_req_dev_type))
1907		return 1; /* Own Secondary Device Type matches */
1908
1909	/* No matching device type found */
1910	return 0;
1911}
1912
1913
1914struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1915{
1916	struct wpabuf *buf;
1917	u8 *len;
1918	int pw_id = -1;
1919	size_t extra = 0;
1920
1921#ifdef CONFIG_WIFI_DISPLAY
1922	if (p2p->wfd_ie_probe_resp)
1923		extra = wpabuf_len(p2p->wfd_ie_probe_resp);
1924#endif /* CONFIG_WIFI_DISPLAY */
1925
1926	buf = wpabuf_alloc(1000 + extra);
1927	if (buf == NULL)
1928		return NULL;
1929
1930	if (p2p->go_neg_peer) {
1931		/* Advertise immediate availability of WPS credential */
1932		pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
1933	}
1934
1935	p2p_build_wps_ie(p2p, buf, pw_id, 1);
1936
1937#ifdef CONFIG_WIFI_DISPLAY
1938	if (p2p->wfd_ie_probe_resp)
1939		wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
1940#endif /* CONFIG_WIFI_DISPLAY */
1941
1942	/* P2P IE */
1943	len = p2p_buf_add_ie_hdr(buf);
1944	p2p_buf_add_capability(buf, p2p->dev_capab &
1945			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
1946	if (p2p->ext_listen_interval)
1947		p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1948					      p2p->ext_listen_interval);
1949	p2p_buf_add_device_info(buf, p2p, NULL);
1950	p2p_buf_update_ie_hdr(buf, len);
1951
1952	return buf;
1953}
1954
1955
1956static int is_11b(u8 rate)
1957{
1958	return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
1959}
1960
1961
1962static int supp_rates_11b_only(struct ieee802_11_elems *elems)
1963{
1964	int num_11b = 0, num_others = 0;
1965	int i;
1966
1967	if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
1968		return 0;
1969
1970	for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
1971		if (is_11b(elems->supp_rates[i]))
1972			num_11b++;
1973		else
1974			num_others++;
1975	}
1976
1977	for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
1978	     i++) {
1979		if (is_11b(elems->ext_supp_rates[i]))
1980			num_11b++;
1981		else
1982			num_others++;
1983	}
1984
1985	return num_11b > 0 && num_others == 0;
1986}
1987
1988
1989static enum p2p_probe_req_status
1990p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
1991		const u8 *bssid, const u8 *ie, size_t ie_len)
1992{
1993	struct ieee802_11_elems elems;
1994	struct wpabuf *buf;
1995	struct ieee80211_mgmt *resp;
1996	struct p2p_message msg;
1997	struct wpabuf *ies;
1998
1999	if (!p2p->in_listen || !p2p->drv_in_listen) {
2000		/* not in Listen state - ignore Probe Request */
2001		return P2P_PREQ_NOT_LISTEN;
2002	}
2003
2004	if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2005	    ParseFailed) {
2006		/* Ignore invalid Probe Request frames */
2007		return P2P_PREQ_MALFORMED;
2008	}
2009
2010	if (elems.p2p == NULL) {
2011		/* not a P2P probe - ignore it */
2012		return P2P_PREQ_NOT_P2P;
2013	}
2014
2015	if (dst && !is_broadcast_ether_addr(dst) &&
2016	    os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2017		/* Not sent to the broadcast address or our P2P Device Address
2018		 */
2019		return P2P_PREQ_NOT_PROCESSED;
2020	}
2021
2022	if (bssid && !is_broadcast_ether_addr(bssid)) {
2023		/* Not sent to the Wildcard BSSID */
2024		return P2P_PREQ_NOT_PROCESSED;
2025	}
2026
2027	if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2028	    os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2029	    0) {
2030		/* not using P2P Wildcard SSID - ignore */
2031		return P2P_PREQ_NOT_PROCESSED;
2032	}
2033
2034	if (supp_rates_11b_only(&elems)) {
2035		/* Indicates support for 11b rates only */
2036		return P2P_PREQ_NOT_P2P;
2037	}
2038
2039	os_memset(&msg, 0, sizeof(msg));
2040	if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2041		/* Could not parse P2P attributes */
2042		return P2P_PREQ_NOT_P2P;
2043	}
2044
2045	if (msg.device_id &&
2046	    os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2047		/* Device ID did not match */
2048		p2p_parse_free(&msg);
2049		return P2P_PREQ_NOT_PROCESSED;
2050	}
2051
2052	/* Check Requested Device Type match */
2053	if (msg.wps_attributes &&
2054	    !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2055		/* No match with Requested Device Type */
2056		p2p_parse_free(&msg);
2057		return P2P_PREQ_NOT_PROCESSED;
2058	}
2059	p2p_parse_free(&msg);
2060
2061	if (!p2p->cfg->send_probe_resp) {
2062		/* Response generated elsewhere */
2063		return P2P_PREQ_NOT_PROCESSED;
2064	}
2065
2066	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2067		"P2P: Reply to P2P Probe Request in Listen state");
2068
2069	/*
2070	 * We do not really have a specific BSS that this frame is advertising,
2071	 * so build a frame that has some information in valid format. This is
2072	 * really only used for discovery purposes, not to learn exact BSS
2073	 * parameters.
2074	 */
2075	ies = p2p_build_probe_resp_ies(p2p);
2076	if (ies == NULL)
2077		return P2P_PREQ_NOT_PROCESSED;
2078
2079	buf = wpabuf_alloc(200 + wpabuf_len(ies));
2080	if (buf == NULL) {
2081		wpabuf_free(ies);
2082		return P2P_PREQ_NOT_PROCESSED;
2083	}
2084
2085	resp = NULL;
2086	resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
2087
2088	resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2089					   (WLAN_FC_STYPE_PROBE_RESP << 4));
2090	os_memcpy(resp->da, addr, ETH_ALEN);
2091	os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2092	os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2093	resp->u.probe_resp.beacon_int = host_to_le16(100);
2094	/* hardware or low-level driver will setup seq_ctrl and timestamp */
2095	resp->u.probe_resp.capab_info =
2096		host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2097			     WLAN_CAPABILITY_PRIVACY |
2098			     WLAN_CAPABILITY_SHORT_SLOT_TIME);
2099
2100	wpabuf_put_u8(buf, WLAN_EID_SSID);
2101	wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2102	wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2103
2104	wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2105	wpabuf_put_u8(buf, 8);
2106	wpabuf_put_u8(buf, (60 / 5) | 0x80);
2107	wpabuf_put_u8(buf, 90 / 5);
2108	wpabuf_put_u8(buf, (120 / 5) | 0x80);
2109	wpabuf_put_u8(buf, 180 / 5);
2110	wpabuf_put_u8(buf, (240 / 5) | 0x80);
2111	wpabuf_put_u8(buf, 360 / 5);
2112	wpabuf_put_u8(buf, 480 / 5);
2113	wpabuf_put_u8(buf, 540 / 5);
2114
2115	wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2116	wpabuf_put_u8(buf, 1);
2117	wpabuf_put_u8(buf, p2p->cfg->channel);
2118
2119	wpabuf_put_buf(buf, ies);
2120	wpabuf_free(ies);
2121
2122	p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
2123
2124	wpabuf_free(buf);
2125
2126	return P2P_PREQ_NOT_PROCESSED;
2127}
2128
2129
2130enum p2p_probe_req_status
2131p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2132		 const u8 *bssid, const u8 *ie, size_t ie_len)
2133{
2134	enum p2p_probe_req_status res;
2135
2136	p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2137
2138	res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
2139
2140	if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2141	    p2p->go_neg_peer &&
2142	    os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
2143	    == 0) {
2144		/* Received a Probe Request from GO Negotiation peer */
2145		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2146			"P2P: Found GO Negotiation peer - try to start GO "
2147			"negotiation from timeout");
2148		eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
2149		return P2P_PREQ_PROCESSED;
2150	}
2151
2152	if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2153	    p2p->invite_peer &&
2154	    os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2155	    == 0) {
2156		/* Received a Probe Request from Invite peer */
2157		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2158			"P2P: Found Invite peer - try to start Invite from "
2159			"timeout");
2160		eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
2161		return P2P_PREQ_PROCESSED;
2162	}
2163
2164	return res;
2165}
2166
2167
2168static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2169				    u8 *buf, size_t len, struct wpabuf *p2p_ie)
2170{
2171	struct wpabuf *tmp;
2172	u8 *lpos;
2173	size_t tmplen;
2174	int res;
2175	u8 group_capab;
2176
2177	if (p2p_ie == NULL)
2178		return 0; /* WLAN AP is not a P2P manager */
2179
2180	/*
2181	 * (Re)Association Request - P2P IE
2182	 * P2P Capability attribute (shall be present)
2183	 * P2P Interface attribute (present if concurrent device and
2184	 *	P2P Management is enabled)
2185	 */
2186	tmp = wpabuf_alloc(200);
2187	if (tmp == NULL)
2188		return -1;
2189
2190	lpos = p2p_buf_add_ie_hdr(tmp);
2191	group_capab = 0;
2192	if (p2p->num_groups > 0) {
2193		group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2194		if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2195		    (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2196		    p2p->cross_connect)
2197			group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2198	}
2199	p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2200	if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2201	    (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2202		p2p_buf_add_p2p_interface(tmp, p2p);
2203	p2p_buf_update_ie_hdr(tmp, lpos);
2204
2205	tmplen = wpabuf_len(tmp);
2206	if (tmplen > len)
2207		res = -1;
2208	else {
2209		os_memcpy(buf, wpabuf_head(tmp), tmplen);
2210		res = tmplen;
2211	}
2212	wpabuf_free(tmp);
2213
2214	return res;
2215}
2216
2217
2218int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2219		     size_t len, int p2p_group, struct wpabuf *p2p_ie)
2220{
2221	struct wpabuf *tmp;
2222	u8 *lpos;
2223	struct p2p_device *peer;
2224	size_t tmplen;
2225	int res;
2226	size_t extra = 0;
2227
2228	if (!p2p_group)
2229		return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2230
2231#ifdef CONFIG_WIFI_DISPLAY
2232	if (p2p->wfd_ie_assoc_req)
2233		extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2234#endif /* CONFIG_WIFI_DISPLAY */
2235
2236	/*
2237	 * (Re)Association Request - P2P IE
2238	 * P2P Capability attribute (shall be present)
2239	 * Extended Listen Timing (may be present)
2240	 * P2P Device Info attribute (shall be present)
2241	 */
2242	tmp = wpabuf_alloc(200 + extra);
2243	if (tmp == NULL)
2244		return -1;
2245
2246#ifdef CONFIG_WIFI_DISPLAY
2247	if (p2p->wfd_ie_assoc_req)
2248		wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2249#endif /* CONFIG_WIFI_DISPLAY */
2250
2251	peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2252
2253	lpos = p2p_buf_add_ie_hdr(tmp);
2254	p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2255	if (p2p->ext_listen_interval)
2256		p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2257					      p2p->ext_listen_interval);
2258	p2p_buf_add_device_info(tmp, p2p, peer);
2259	p2p_buf_update_ie_hdr(tmp, lpos);
2260
2261	tmplen = wpabuf_len(tmp);
2262	if (tmplen > len)
2263		res = -1;
2264	else {
2265		os_memcpy(buf, wpabuf_head(tmp), tmplen);
2266		res = tmplen;
2267	}
2268	wpabuf_free(tmp);
2269
2270	return res;
2271}
2272
2273
2274int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2275{
2276	struct wpabuf *p2p_ie;
2277	int ret;
2278
2279	p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2280	if (p2p_ie == NULL)
2281		return 0;
2282
2283	ret = p2p_attr_text(p2p_ie, buf, end);
2284	wpabuf_free(p2p_ie);
2285	return ret;
2286}
2287
2288
2289int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2290{
2291	struct p2p_message msg;
2292
2293	os_memset(&msg, 0, sizeof(msg));
2294	if (p2p_parse_p2p_ie(p2p_ie, &msg))
2295		return -1;
2296
2297	if (msg.p2p_device_addr) {
2298		os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2299		return 0;
2300	} else if (msg.device_id) {
2301		os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2302		return 0;
2303	}
2304	return -1;
2305}
2306
2307
2308int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2309{
2310	struct wpabuf *p2p_ie;
2311	int ret;
2312
2313	p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2314					     P2P_IE_VENDOR_TYPE);
2315	if (p2p_ie == NULL)
2316		return -1;
2317	ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
2318	wpabuf_free(p2p_ie);
2319	return ret;
2320}
2321
2322
2323static void p2p_clear_go_neg(struct p2p_data *p2p)
2324{
2325	p2p->go_neg_peer = NULL;
2326	p2p_clear_timeout(p2p);
2327	p2p_set_state(p2p, P2P_IDLE);
2328}
2329
2330
2331void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2332{
2333	if (p2p->go_neg_peer == NULL) {
2334		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2335			"P2P: No pending Group Formation - "
2336			"ignore WPS registration success notification");
2337		return; /* No pending Group Formation */
2338	}
2339
2340	if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2341	    0) {
2342		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2343			"P2P: Ignore WPS registration success notification "
2344			"for " MACSTR " (GO Negotiation peer " MACSTR ")",
2345			MAC2STR(mac_addr),
2346			MAC2STR(p2p->go_neg_peer->intended_addr));
2347		return; /* Ignore unexpected peer address */
2348	}
2349
2350	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2351		"P2P: Group Formation completed successfully with " MACSTR,
2352		MAC2STR(mac_addr));
2353
2354	p2p_clear_go_neg(p2p);
2355}
2356
2357
2358void p2p_group_formation_failed(struct p2p_data *p2p)
2359{
2360	if (p2p->go_neg_peer == NULL) {
2361		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2362			"P2P: No pending Group Formation - "
2363			"ignore group formation failure notification");
2364		return; /* No pending Group Formation */
2365	}
2366
2367	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2368		"P2P: Group Formation failed with " MACSTR,
2369		MAC2STR(p2p->go_neg_peer->intended_addr));
2370
2371	p2p_clear_go_neg(p2p);
2372}
2373
2374
2375struct p2p_data * p2p_init(const struct p2p_config *cfg)
2376{
2377	struct p2p_data *p2p;
2378
2379	if (cfg->max_peers < 1)
2380		return NULL;
2381
2382	p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2383	if (p2p == NULL)
2384		return NULL;
2385	p2p->cfg = (struct p2p_config *) (p2p + 1);
2386	os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2387	if (cfg->dev_name)
2388		p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2389	if (cfg->manufacturer)
2390		p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2391	if (cfg->model_name)
2392		p2p->cfg->model_name = os_strdup(cfg->model_name);
2393	if (cfg->model_number)
2394		p2p->cfg->model_number = os_strdup(cfg->model_number);
2395	if (cfg->serial_number)
2396		p2p->cfg->serial_number = os_strdup(cfg->serial_number);
2397	if (cfg->pref_chan) {
2398		p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2399						sizeof(struct p2p_channel));
2400		if (p2p->cfg->pref_chan) {
2401			os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2402				  cfg->num_pref_chan *
2403				  sizeof(struct p2p_channel));
2404		} else
2405			p2p->cfg->num_pref_chan = 0;
2406	}
2407
2408#ifdef ANDROID_P2P
2409	/* 100ms listen time is too less to receive the response frames in some scenarios
2410	 * increasing min listen time to 200ms.
2411	 */
2412	p2p->min_disc_int = 2;
2413	/* SD_FAIR_POLICY: Initializing the SD current serviced pointer to NULL */
2414	p2p->sd_dev_list = NULL;
2415#else
2416	p2p->min_disc_int = 1;
2417#endif
2418	p2p->max_disc_int = 3;
2419
2420	os_get_random(&p2p->next_tie_breaker, 1);
2421	p2p->next_tie_breaker &= 0x01;
2422	if (cfg->sd_request)
2423		p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2424	p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2425	if (cfg->concurrent_operations)
2426		p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2427	p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2428
2429	dl_list_init(&p2p->devices);
2430
2431	eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
2432			       p2p_expiration_timeout, p2p, NULL);
2433
2434	p2p->go_timeout = 100;
2435	p2p->client_timeout = 20;
2436
2437	return p2p;
2438}
2439
2440
2441void p2p_deinit(struct p2p_data *p2p)
2442{
2443#ifdef CONFIG_WIFI_DISPLAY
2444	wpabuf_free(p2p->wfd_ie_beacon);
2445	wpabuf_free(p2p->wfd_ie_probe_req);
2446	wpabuf_free(p2p->wfd_ie_probe_resp);
2447	wpabuf_free(p2p->wfd_ie_assoc_req);
2448	wpabuf_free(p2p->wfd_ie_invitation);
2449	wpabuf_free(p2p->wfd_ie_prov_disc_req);
2450	wpabuf_free(p2p->wfd_ie_prov_disc_resp);
2451	wpabuf_free(p2p->wfd_ie_go_neg);
2452	wpabuf_free(p2p->wfd_dev_info);
2453	wpabuf_free(p2p->wfd_assoc_bssid);
2454	wpabuf_free(p2p->wfd_coupled_sink_info);
2455#endif /* CONFIG_WIFI_DISPLAY */
2456
2457	eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
2458	eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2459	eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2460	p2p_flush(p2p);
2461	p2p_free_req_dev_types(p2p);
2462	os_free(p2p->cfg->dev_name);
2463	os_free(p2p->cfg->manufacturer);
2464	os_free(p2p->cfg->model_name);
2465	os_free(p2p->cfg->model_number);
2466	os_free(p2p->cfg->serial_number);
2467	os_free(p2p->cfg->pref_chan);
2468	os_free(p2p->groups);
2469	wpabuf_free(p2p->sd_resp);
2470	os_free(p2p->after_scan_tx);
2471	p2p_remove_wps_vendor_extensions(p2p);
2472	os_free(p2p);
2473}
2474
2475
2476void p2p_flush(struct p2p_data *p2p)
2477{
2478	struct p2p_device *dev, *prev;
2479	p2p_stop_find(p2p);
2480	dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2481			      list) {
2482		dl_list_del(&dev->list);
2483		p2p_device_free(p2p, dev);
2484	}
2485#ifdef ANDROID_P2P
2486	/* SD_FAIR_POLICY: Initializing the SD current serviced pointer to NULL */
2487	p2p->sd_dev_list = NULL;
2488#endif
2489	p2p_free_sd_queries(p2p);
2490	os_free(p2p->after_scan_tx);
2491	p2p->after_scan_tx = NULL;
2492}
2493
2494
2495int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2496{
2497	struct p2p_device *dev;
2498
2499	dev = p2p_get_device(p2p, addr);
2500	if (dev == NULL)
2501		return -1;
2502
2503	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
2504		MAC2STR(addr));
2505
2506	if (p2p->go_neg_peer == dev)
2507		p2p->go_neg_peer = NULL;
2508
2509	dev->wps_method = WPS_NOT_READY;
2510	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2511	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2512
2513	/* Check if after_scan_tx is for this peer. If so free it */
2514	if (p2p->after_scan_tx &&
2515	    os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2516		os_free(p2p->after_scan_tx);
2517		p2p->after_scan_tx = NULL;
2518	}
2519
2520	return 0;
2521}
2522
2523
2524int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2525{
2526	os_free(p2p->cfg->dev_name);
2527	if (dev_name) {
2528		p2p->cfg->dev_name = os_strdup(dev_name);
2529		if (p2p->cfg->dev_name == NULL)
2530			return -1;
2531	} else
2532		p2p->cfg->dev_name = NULL;
2533	return 0;
2534}
2535
2536
2537int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2538{
2539	os_free(p2p->cfg->manufacturer);
2540	p2p->cfg->manufacturer = NULL;
2541	if (manufacturer) {
2542		p2p->cfg->manufacturer = os_strdup(manufacturer);
2543		if (p2p->cfg->manufacturer == NULL)
2544			return -1;
2545	}
2546
2547	return 0;
2548}
2549
2550
2551int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2552{
2553	os_free(p2p->cfg->model_name);
2554	p2p->cfg->model_name = NULL;
2555	if (model_name) {
2556		p2p->cfg->model_name = os_strdup(model_name);
2557		if (p2p->cfg->model_name == NULL)
2558			return -1;
2559	}
2560
2561	return 0;
2562}
2563
2564
2565int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2566{
2567	os_free(p2p->cfg->model_number);
2568	p2p->cfg->model_number = NULL;
2569	if (model_number) {
2570		p2p->cfg->model_number = os_strdup(model_number);
2571		if (p2p->cfg->model_number == NULL)
2572			return -1;
2573	}
2574
2575	return 0;
2576}
2577
2578
2579int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2580{
2581	os_free(p2p->cfg->serial_number);
2582	p2p->cfg->serial_number = NULL;
2583	if (serial_number) {
2584		p2p->cfg->serial_number = os_strdup(serial_number);
2585		if (p2p->cfg->serial_number == NULL)
2586			return -1;
2587	}
2588
2589	return 0;
2590}
2591
2592
2593void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2594{
2595	p2p->cfg->config_methods = config_methods;
2596}
2597
2598
2599void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2600{
2601	os_memcpy(p2p->cfg->uuid, uuid, 16);
2602}
2603
2604
2605int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2606{
2607	os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2608	return 0;
2609}
2610
2611
2612int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2613			  size_t num_dev_types)
2614{
2615	if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2616		num_dev_types = P2P_SEC_DEVICE_TYPES;
2617	p2p->cfg->num_sec_dev_types = num_dev_types;
2618	os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2619	return 0;
2620}
2621
2622
2623void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2624{
2625	int i;
2626
2627	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2628		wpabuf_free(p2p->wps_vendor_ext[i]);
2629		p2p->wps_vendor_ext[i] = NULL;
2630	}
2631}
2632
2633
2634int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2635				 const struct wpabuf *vendor_ext)
2636{
2637	int i;
2638
2639	if (vendor_ext == NULL)
2640		return -1;
2641
2642	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2643		if (p2p->wps_vendor_ext[i] == NULL)
2644			break;
2645	}
2646	if (i >= P2P_MAX_WPS_VENDOR_EXT)
2647		return -1;
2648
2649	p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2650	if (p2p->wps_vendor_ext[i] == NULL)
2651		return -1;
2652
2653	return 0;
2654}
2655
2656
2657int p2p_set_country(struct p2p_data *p2p, const char *country)
2658{
2659	os_memcpy(p2p->cfg->country, country, 3);
2660	return 0;
2661}
2662
2663
2664void p2p_continue_find(struct p2p_data *p2p)
2665{
2666	struct p2p_device *dev;
2667#ifdef ANDROID_P2P
2668	int skip=1;
2669#endif
2670	p2p_set_state(p2p, P2P_SEARCH);
2671	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2672#ifdef ANDROID_P2P
2673		/* SD_FAIR_POLICY: We need to give chance to all devices in the device list
2674		 * There may be a scenario, where a particular peer device have
2675		 * not registered any query response. When we send a SD request to such device,
2676		 * no response will be received. And if we continue to get probe responses from that device,
2677		 * and if that device happens to be on top in our device list,
2678		 * we will always continue to send SD requests always to that peer only.
2679		 * We will not be able to send SD requests to other devices in that case.
2680		 * This implementation keeps track of last serviced peer device.
2681		 * And then takes the next one from the device list, in the next iteration.
2682		 */
2683		if (p2p->sd_dev_list && p2p->sd_dev_list != &p2p->devices) {
2684			if(skip) {
2685				if ((&dev->list == p2p->sd_dev_list) ) {
2686					skip = 0;
2687					if (dev->list.next == &p2p->devices)
2688						p2p->sd_dev_list = NULL;
2689				}
2690				continue;
2691			}
2692		}
2693		p2p->sd_dev_list = &dev->list;
2694		wpa_printf(MSG_DEBUG, "P2P: ### Servicing %p dev->flags 0x%x SD schedule %s devaddr " MACSTR,
2695			p2p->sd_dev_list, dev->flags, dev->flags & P2P_DEV_SD_SCHEDULE ? "TRUE": "FALSE",
2696			MAC2STR(dev->info.p2p_device_addr));
2697#endif
2698		if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2699			if (p2p_start_sd(p2p, dev) == 0)
2700				return;
2701			else
2702				break;
2703		} else if (dev->req_config_methods &&
2704			   !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2705			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2706				"pending Provision Discovery Request to "
2707				MACSTR " (config methods 0x%x)",
2708				MAC2STR(dev->info.p2p_device_addr),
2709				dev->req_config_methods);
2710			if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
2711				return;
2712		}
2713	}
2714
2715	p2p_listen_in_find(p2p);
2716}
2717
2718
2719static void p2p_sd_cb(struct p2p_data *p2p, int success)
2720{
2721	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2722		"P2P: Service Discovery Query TX callback: success=%d",
2723		success);
2724	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2725
2726	if (!success) {
2727		if (p2p->sd_peer) {
2728			p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2729			p2p->sd_peer = NULL;
2730		}
2731		p2p_continue_find(p2p);
2732		return;
2733	}
2734
2735	if (p2p->sd_peer == NULL) {
2736		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2737			"P2P: No SD peer entry known");
2738		p2p_continue_find(p2p);
2739		return;
2740	}
2741
2742	/* Wait for response from the peer */
2743	p2p_set_state(p2p, P2P_SD_DURING_FIND);
2744	p2p_set_timeout(p2p, 0, 200000);
2745}
2746
2747
2748/**
2749 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2750 * @p2p: P2P module context from p2p_init()
2751 */
2752static void p2p_retry_pd(struct p2p_data *p2p)
2753{
2754	struct p2p_device *dev;
2755
2756	if (p2p->state != P2P_IDLE)
2757		return;
2758
2759	/*
2760	 * Retry the prov disc req attempt only for the peer that the user had
2761	 * requested for and provided a join has not been initiated on it
2762	 * in the meantime.
2763	 */
2764
2765	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2766		if (os_memcmp(p2p->pending_pd_devaddr,
2767			      dev->info.p2p_device_addr, ETH_ALEN) != 0)
2768			continue;
2769		if (!dev->req_config_methods)
2770			continue;
2771		if (dev->flags & P2P_DEV_PD_FOR_JOIN)
2772			continue;
2773
2774		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2775			"pending Provision Discovery Request to "
2776			MACSTR " (config methods 0x%x)",
2777			MAC2STR(dev->info.p2p_device_addr),
2778			dev->req_config_methods);
2779		p2p_send_prov_disc_req(p2p, dev, 0, 0);
2780		return;
2781	}
2782}
2783
2784
2785static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2786{
2787	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2788		"P2P: Provision Discovery Request TX callback: success=%d",
2789		success);
2790
2791	/*
2792	 * Postpone resetting the pending action state till after we actually
2793	 * time out. This allows us to take some action like notifying any
2794	 * interested parties about no response to the request.
2795	 *
2796	 * When the timer (below) goes off we check in IDLE, SEARCH, or
2797	 * LISTEN_ONLY state, which are the only allowed states to issue a PD
2798	 * requests in, if this was still pending and then raise notification.
2799	 */
2800
2801	if (!success) {
2802		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2803
2804		if (p2p->user_initiated_pd &&
2805		    (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
2806		{
2807			/* Retry request from timeout to avoid busy loops */
2808			p2p->pending_action_state = P2P_PENDING_PD;
2809			p2p_set_timeout(p2p, 0, 50000);
2810		} else if (p2p->state != P2P_IDLE)
2811			p2p_continue_find(p2p);
2812		else if (p2p->user_initiated_pd) {
2813			p2p->pending_action_state = P2P_PENDING_PD;
2814			p2p_set_timeout(p2p, 0, 300000);
2815		}
2816		return;
2817	}
2818
2819	/*
2820	 * This postponing, of resetting pending_action_state, needs to be
2821	 * done only for user initiated PD requests and not internal ones.
2822	 */
2823	if (p2p->user_initiated_pd)
2824		p2p->pending_action_state = P2P_PENDING_PD;
2825	else
2826		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2827
2828	/* Wait for response from the peer */
2829	if (p2p->state == P2P_SEARCH)
2830		p2p_set_state(p2p, P2P_PD_DURING_FIND);
2831	p2p_set_timeout(p2p, 0, 200000);
2832}
2833
2834
2835int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
2836			 int level, const u8 *ies, size_t ies_len)
2837{
2838	p2p_add_device(p2p, bssid, freq, level, ies, ies_len, 1);
2839
2840	return 0;
2841}
2842
2843
2844void p2p_scan_res_handled(struct p2p_data *p2p)
2845{
2846	if (!p2p->p2p_scan_running) {
2847		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
2848			"running, but scan results received");
2849	}
2850	p2p->p2p_scan_running = 0;
2851	eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2852
2853	if (p2p_run_after_scan(p2p))
2854		return;
2855	if (p2p->state == P2P_SEARCH)
2856		p2p_continue_find(p2p);
2857}
2858
2859
2860void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
2861{
2862	u8 *len;
2863
2864#ifdef CONFIG_WIFI_DISPLAY
2865	if (p2p->wfd_ie_probe_req)
2866		wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
2867#endif /* CONFIG_WIFI_DISPLAY */
2868
2869	len = p2p_buf_add_ie_hdr(ies);
2870	p2p_buf_add_capability(ies, p2p->dev_capab &
2871			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
2872	if (dev_id)
2873		p2p_buf_add_device_id(ies, dev_id);
2874	if (p2p->cfg->reg_class && p2p->cfg->channel)
2875		p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2876					   p2p->cfg->reg_class,
2877					   p2p->cfg->channel);
2878	if (p2p->ext_listen_interval)
2879		p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2880					      p2p->ext_listen_interval);
2881	/* TODO: p2p_buf_add_operating_channel() if GO */
2882	p2p_buf_update_ie_hdr(ies, len);
2883}
2884
2885
2886size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
2887{
2888	size_t len = 100;
2889
2890#ifdef CONFIG_WIFI_DISPLAY
2891	if (p2p && p2p->wfd_ie_probe_req)
2892		len += wpabuf_len(p2p->wfd_ie_probe_req);
2893#endif /* CONFIG_WIFI_DISPLAY */
2894
2895	return len;
2896}
2897
2898
2899int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2900{
2901	return p2p_attr_text(p2p_ie, buf, end);
2902}
2903
2904
2905static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2906{
2907	struct p2p_device *dev = p2p->go_neg_peer;
2908
2909	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2910		"P2P: GO Negotiation Request TX callback: success=%d",
2911		success);
2912
2913	if (dev == NULL) {
2914		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2915			"P2P: No pending GO Negotiation");
2916		return;
2917	}
2918
2919	if (success) {
2920		if (dev->flags & P2P_DEV_USER_REJECTED) {
2921			p2p_set_state(p2p, P2P_IDLE);
2922			return;
2923		}
2924	} else if (dev->go_neg_req_sent) {
2925		/* Cancel the increment from p2p_connect_send() on failure */
2926		dev->go_neg_req_sent--;
2927	}
2928
2929	if (!success &&
2930	    (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2931	    !is_zero_ether_addr(dev->member_in_go_dev)) {
2932		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2933			"P2P: Peer " MACSTR " did not acknowledge request - "
2934			"try to use device discoverability through its GO",
2935			MAC2STR(dev->info.p2p_device_addr));
2936		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2937		p2p_send_dev_disc_req(p2p, dev);
2938		return;
2939	}
2940
2941	/*
2942	 * Use P2P find, if needed, to find the other device from its listen
2943	 * channel.
2944	 */
2945	p2p_set_state(p2p, P2P_CONNECT);
2946	p2p_set_timeout(p2p, 0, success ? 200000 : 100000);
2947}
2948
2949
2950static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2951{
2952	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2953		"P2P: GO Negotiation Response TX callback: success=%d",
2954		success);
2955	if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2956		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2957			"P2P: Ignore TX callback event - GO Negotiation is "
2958			"not running anymore");
2959		return;
2960	}
2961	p2p_set_state(p2p, P2P_CONNECT);
2962	p2p_set_timeout(p2p, 0, 250000);
2963}
2964
2965
2966static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2967{
2968	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2969		"P2P: GO Negotiation Response (failure) TX callback: "
2970		"success=%d", success);
2971	if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2972		p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2973				  p2p->go_neg_peer->status);
2974	}
2975}
2976
2977
2978static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
2979			       enum p2p_send_action_result result)
2980{
2981	struct p2p_device *dev;
2982
2983	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2984		"P2P: GO Negotiation Confirm TX callback: result=%d",
2985		result);
2986	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2987	if (result == P2P_SEND_ACTION_FAILED) {
2988		p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2989		return;
2990	}
2991	if (result == P2P_SEND_ACTION_NO_ACK) {
2992		/*
2993		 * It looks like the TX status for GO Negotiation Confirm is
2994		 * often showing failure even when the peer has actually
2995		 * received the frame. Since the peer may change channels
2996		 * immediately after having received the frame, we may not see
2997		 * an Ack for retries, so just dropping a single frame may
2998		 * trigger this. To allow the group formation to succeed if the
2999		 * peer did indeed receive the frame, continue regardless of
3000		 * the TX status.
3001		 */
3002		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3003			"P2P: Assume GO Negotiation Confirm TX was actually "
3004			"received by the peer even though Ack was not "
3005			"reported");
3006	}
3007
3008	dev = p2p->go_neg_peer;
3009	if (dev == NULL)
3010		return;
3011
3012	p2p_go_complete(p2p, dev);
3013}
3014
3015
3016void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3017			const u8 *src, const u8 *bssid,
3018			enum p2p_send_action_result result)
3019{
3020	enum p2p_pending_action_state state;
3021	int success;
3022
3023	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3024		"P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
3025		" src=" MACSTR " bssid=" MACSTR " result=%d",
3026		p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
3027		MAC2STR(bssid), result);
3028	success = result == P2P_SEND_ACTION_SUCCESS;
3029	state = p2p->pending_action_state;
3030	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3031	switch (state) {
3032	case P2P_NO_PENDING_ACTION:
3033		break;
3034	case P2P_PENDING_GO_NEG_REQUEST:
3035		p2p_go_neg_req_cb(p2p, success);
3036		break;
3037	case P2P_PENDING_GO_NEG_RESPONSE:
3038		p2p_go_neg_resp_cb(p2p, success);
3039		break;
3040	case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
3041		p2p_go_neg_resp_failure_cb(p2p, success);
3042		break;
3043	case P2P_PENDING_GO_NEG_CONFIRM:
3044		p2p_go_neg_conf_cb(p2p, result);
3045		break;
3046	case P2P_PENDING_SD:
3047		p2p_sd_cb(p2p, success);
3048		break;
3049	case P2P_PENDING_PD:
3050		p2p_prov_disc_cb(p2p, success);
3051		break;
3052	case P2P_PENDING_INVITATION_REQUEST:
3053		p2p_invitation_req_cb(p2p, success);
3054		break;
3055	case P2P_PENDING_INVITATION_RESPONSE:
3056		p2p_invitation_resp_cb(p2p, success);
3057		break;
3058	case P2P_PENDING_DEV_DISC_REQUEST:
3059		p2p_dev_disc_req_cb(p2p, success);
3060		break;
3061	case P2P_PENDING_DEV_DISC_RESPONSE:
3062		p2p_dev_disc_resp_cb(p2p, success);
3063		break;
3064	case P2P_PENDING_GO_DISC_REQ:
3065		p2p_go_disc_req_cb(p2p, success);
3066		break;
3067	}
3068}
3069
3070
3071void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3072		   unsigned int duration)
3073{
3074	if (freq == p2p->pending_client_disc_freq) {
3075		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3076			"P2P: Client discoverability remain-awake completed");
3077		p2p->pending_client_disc_freq = 0;
3078		return;
3079	}
3080
3081	if (freq != p2p->pending_listen_freq) {
3082		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3083			"P2P: Unexpected listen callback for freq=%u "
3084			"duration=%u (pending_listen_freq=%u)",
3085			freq, duration, p2p->pending_listen_freq);
3086		return;
3087	}
3088
3089	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3090		"P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
3091		"callback",
3092		p2p->pending_listen_sec, p2p->pending_listen_usec,
3093		p2p->pending_listen_freq);
3094	p2p->in_listen = 1;
3095	p2p->drv_in_listen = freq;
3096	if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3097		/*
3098		 * Add 20 msec extra wait to avoid race condition with driver
3099		 * remain-on-channel end event, i.e., give driver more time to
3100		 * complete the operation before our timeout expires.
3101		 */
3102		p2p_set_timeout(p2p, p2p->pending_listen_sec,
3103				p2p->pending_listen_usec + 20000);
3104	}
3105
3106	p2p->pending_listen_freq = 0;
3107}
3108
3109
3110int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3111{
3112	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
3113		"state (freq=%u)", freq);
3114	p2p->drv_in_listen = 0;
3115	if (p2p->in_listen)
3116		return 0; /* Internal timeout will trigger the next step */
3117
3118	if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3119		if (p2p->go_neg_peer->connect_reqs >= 120) {
3120			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3121				"P2P: Timeout on sending GO Negotiation "
3122				"Request without getting response");
3123			p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3124			return 0;
3125		}
3126
3127		p2p_set_state(p2p, P2P_CONNECT);
3128		p2p_connect_send(p2p, p2p->go_neg_peer);
3129		return 1;
3130	} else if (p2p->state == P2P_SEARCH) {
3131		if (p2p->p2p_scan_running) {
3132			 /*
3133			  * Search is already in progress. This can happen if
3134			  * an Action frame RX is reported immediately after
3135			  * the end of a remain-on-channel operation and the
3136			  * response frame to that is sent using an offchannel
3137			  * operation while in p2p_find. Avoid an attempt to
3138			  * restart a scan here.
3139			  */
3140			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan "
3141				"already in progress - do not try to start a "
3142				"new one");
3143			return 1;
3144		}
3145		if (p2p->pending_listen_freq) {
3146			/*
3147			 * Better wait a bit if the driver is unable to start
3148			 * offchannel operation for some reason. p2p_search()
3149			 * will be started from internal timeout.
3150			 */
3151			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Listen "
3152				"operation did not seem to start - delay "
3153				"search phase to avoid busy loop");
3154			p2p_set_timeout(p2p, 0, 100000);
3155			return 1;
3156		}
3157		if (p2p->search_delay) {
3158			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
3159				"search operation by %u ms",
3160				p2p->search_delay);
3161			p2p_set_timeout(p2p, p2p->search_delay / 1000,
3162					(p2p->search_delay % 1000) * 1000);
3163			return 1;
3164		}
3165		p2p_search(p2p);
3166		return 1;
3167	}
3168
3169	return 0;
3170}
3171
3172
3173static void p2p_timeout_connect(struct p2p_data *p2p)
3174{
3175	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3176	if (p2p->go_neg_peer &&
3177	    (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
3178		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Wait for GO "
3179			"Negotiation Confirm timed out - assume GO "
3180			"Negotiation failed");
3181		p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3182		return;
3183	}
3184	p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3185	p2p_listen_in_find(p2p);
3186}
3187
3188
3189static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3190{
3191	if (p2p->go_neg_peer) {
3192		if (p2p->drv_in_listen) {
3193			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
3194				"still in Listen state; wait for it to "
3195				"complete");
3196			return;
3197		}
3198
3199		if (p2p->go_neg_peer->connect_reqs >= 120) {
3200			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3201				"P2P: Timeout on sending GO Negotiation "
3202				"Request without getting response");
3203			p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3204			return;
3205		}
3206
3207		p2p_set_state(p2p, P2P_CONNECT);
3208		p2p_connect_send(p2p, p2p->go_neg_peer);
3209	} else
3210		p2p_set_state(p2p, P2P_IDLE);
3211}
3212
3213
3214static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3215{
3216	/*
3217	 * TODO: could remain constantly in Listen state for some time if there
3218	 * are no other concurrent uses for the radio. For now, go to listen
3219	 * state once per second to give other uses a chance to use the radio.
3220	 */
3221	p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
3222	p2p_set_timeout(p2p, 0, 500000);
3223}
3224
3225
3226static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3227{
3228	struct p2p_device *dev = p2p->go_neg_peer;
3229
3230	if (dev == NULL) {
3231		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3232			"P2P: Unknown GO Neg peer - stop GO Neg wait");
3233		return;
3234	}
3235
3236	dev->wait_count++;
3237	if (dev->wait_count >= 120) {
3238		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3239			"P2P: Timeout on waiting peer to become ready for GO "
3240			"Negotiation");
3241		p2p_go_neg_failed(p2p, dev, -1);
3242		return;
3243	}
3244
3245	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3246		"P2P: Go to Listen state while waiting for the peer to become "
3247		"ready for GO Negotiation");
3248	p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
3249	p2p_listen_in_find(p2p);
3250}
3251
3252
3253static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3254{
3255	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3256		"P2P: Service Discovery Query timeout");
3257	if (p2p->sd_peer) {
3258		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3259		p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
3260		p2p->sd_peer = NULL;
3261	}
3262	p2p_continue_find(p2p);
3263}
3264
3265
3266static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
3267{
3268	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3269		"P2P: Provision Discovery Request timeout");
3270	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3271	p2p_continue_find(p2p);
3272}
3273
3274
3275static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
3276{
3277	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3278
3279	/*
3280	 * For user initiated PD requests that we have not gotten any responses
3281	 * for while in IDLE state, we retry them a couple of times before
3282	 * giving up.
3283	 */
3284	if (!p2p->user_initiated_pd)
3285		return;
3286
3287	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3288		"P2P: User initiated Provision Discovery Request timeout");
3289
3290	if (p2p->pd_retries) {
3291		p2p->pd_retries--;
3292		p2p_retry_pd(p2p);
3293	} else {
3294		if (p2p->cfg->prov_disc_fail)
3295			p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
3296						 p2p->pending_pd_devaddr,
3297						 P2P_PROV_DISC_TIMEOUT);
3298		p2p_reset_pending_pd(p2p);
3299	}
3300}
3301
3302
3303static void p2p_timeout_invite(struct p2p_data *p2p)
3304{
3305	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3306	p2p_set_state(p2p, P2P_INVITE_LISTEN);
3307	if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
3308		/*
3309		 * Better remain on operating channel instead of listen channel
3310		 * when running a group.
3311		 */
3312		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
3313			"active GO role - wait on operating channel");
3314		p2p_set_timeout(p2p, 0, 100000);
3315		return;
3316	}
3317	p2p_listen_in_find(p2p);
3318}
3319
3320
3321static void p2p_timeout_invite_listen(struct p2p_data *p2p)
3322{
3323	if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
3324		p2p_set_state(p2p, P2P_INVITE);
3325		p2p_invite_send(p2p, p2p->invite_peer,
3326				p2p->invite_go_dev_addr);
3327	} else {
3328		if (p2p->invite_peer) {
3329			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3330				"P2P: Invitation Request retry limit reached");
3331			if (p2p->cfg->invitation_result)
3332				p2p->cfg->invitation_result(
3333					p2p->cfg->cb_ctx, -1, NULL);
3334		}
3335		p2p_set_state(p2p, P2P_IDLE);
3336	}
3337}
3338
3339
3340static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
3341{
3342	struct p2p_data *p2p = eloop_ctx;
3343
3344	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
3345		p2p_state_txt(p2p->state));
3346
3347	p2p->in_listen = 0;
3348
3349	switch (p2p->state) {
3350	case P2P_IDLE:
3351		/* Check if we timed out waiting for PD req */
3352		if (p2p->pending_action_state == P2P_PENDING_PD)
3353			p2p_timeout_prov_disc_req(p2p);
3354		break;
3355	case P2P_SEARCH:
3356		/* Check if we timed out waiting for PD req */
3357		if (p2p->pending_action_state == P2P_PENDING_PD)
3358			p2p_timeout_prov_disc_req(p2p);
3359		if (p2p->search_delay && !p2p->in_search_delay) {
3360			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
3361				"search operation by %u ms",
3362				p2p->search_delay);
3363			p2p->in_search_delay = 1;
3364			p2p_set_timeout(p2p, p2p->search_delay / 1000,
3365					(p2p->search_delay % 1000) * 1000);
3366			break;
3367		}
3368		p2p->in_search_delay = 0;
3369		p2p_search(p2p);
3370		break;
3371	case P2P_CONNECT:
3372		p2p_timeout_connect(p2p);
3373		break;
3374	case P2P_CONNECT_LISTEN:
3375		p2p_timeout_connect_listen(p2p);
3376		break;
3377	case P2P_GO_NEG:
3378		break;
3379	case P2P_LISTEN_ONLY:
3380		/* Check if we timed out waiting for PD req */
3381		if (p2p->pending_action_state == P2P_PENDING_PD)
3382			p2p_timeout_prov_disc_req(p2p);
3383
3384		if (p2p->ext_listen_only) {
3385			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3386				"P2P: Extended Listen Timing - Listen State "
3387				"completed");
3388			p2p->ext_listen_only = 0;
3389			p2p_set_state(p2p, P2P_IDLE);
3390		}
3391		break;
3392	case P2P_WAIT_PEER_CONNECT:
3393		p2p_timeout_wait_peer_connect(p2p);
3394		break;
3395	case P2P_WAIT_PEER_IDLE:
3396		p2p_timeout_wait_peer_idle(p2p);
3397		break;
3398	case P2P_SD_DURING_FIND:
3399		p2p_timeout_sd_during_find(p2p);
3400		break;
3401	case P2P_PROVISIONING:
3402		break;
3403	case P2P_PD_DURING_FIND:
3404		p2p_timeout_prov_disc_during_find(p2p);
3405		break;
3406	case P2P_INVITE:
3407		p2p_timeout_invite(p2p);
3408		break;
3409	case P2P_INVITE_LISTEN:
3410		p2p_timeout_invite_listen(p2p);
3411		break;
3412	case P2P_SEARCH_WHEN_READY:
3413		break;
3414	case P2P_CONTINUE_SEARCH_WHEN_READY:
3415		break;
3416	}
3417}
3418
3419
3420int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
3421{
3422	struct p2p_device *dev;
3423
3424	dev = p2p_get_device(p2p, peer_addr);
3425	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
3426		"connection attempts by peer " MACSTR, MAC2STR(peer_addr));
3427	if (dev == NULL) {
3428		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
3429			" unknown", MAC2STR(peer_addr));
3430		return -1;
3431	}
3432	dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
3433	dev->flags |= P2P_DEV_USER_REJECTED;
3434	return 0;
3435}
3436
3437
3438const char * p2p_wps_method_text(enum p2p_wps_method method)
3439{
3440	switch (method) {
3441	case WPS_NOT_READY:
3442		return "not-ready";
3443	case WPS_PIN_DISPLAY:
3444		return "Display";
3445	case WPS_PIN_KEYPAD:
3446		return "Keypad";
3447	case WPS_PBC:
3448		return "PBC";
3449	}
3450
3451	return "??";
3452}
3453
3454
3455static const char * p2p_go_state_text(enum p2p_go_state go_state)
3456{
3457	switch (go_state) {
3458	case UNKNOWN_GO:
3459		return "unknown";
3460	case LOCAL_GO:
3461		return "local";
3462	case  REMOTE_GO:
3463		return "remote";
3464	}
3465
3466	return "??";
3467}
3468
3469
3470const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
3471					       const u8 *addr, int next)
3472{
3473	struct p2p_device *dev;
3474
3475	if (addr)
3476		dev = p2p_get_device(p2p, addr);
3477	else
3478		dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3479
3480	if (dev && next) {
3481		dev = dl_list_first(&dev->list, struct p2p_device, list);
3482		if (&dev->list == &p2p->devices)
3483			dev = NULL;
3484	}
3485
3486	if (dev == NULL)
3487		return NULL;
3488
3489	return &dev->info;
3490}
3491
3492
3493int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
3494			  char *buf, size_t buflen)
3495{
3496	struct p2p_device *dev;
3497	int res;
3498	char *pos, *end;
3499	struct os_time now;
3500
3501	if (info == NULL)
3502		return -1;
3503
3504	dev = (struct p2p_device *) (((u8 *) info) -
3505				     offsetof(struct p2p_device, info));
3506
3507	pos = buf;
3508	end = buf + buflen;
3509
3510	os_get_time(&now);
3511	res = os_snprintf(pos, end - pos,
3512			  "age=%d\n"
3513			  "listen_freq=%d\n"
3514			  "wps_method=%s\n"
3515			  "interface_addr=" MACSTR "\n"
3516			  "member_in_go_dev=" MACSTR "\n"
3517			  "member_in_go_iface=" MACSTR "\n"
3518			  "go_neg_req_sent=%d\n"
3519			  "go_state=%s\n"
3520			  "dialog_token=%u\n"
3521			  "intended_addr=" MACSTR "\n"
3522			  "country=%c%c\n"
3523			  "oper_freq=%d\n"
3524			  "req_config_methods=0x%x\n"
3525			  "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
3526			  "status=%d\n"
3527			  "wait_count=%u\n"
3528			  "invitation_reqs=%u\n",
3529			  (int) (now.sec - dev->last_seen.sec),
3530			  dev->listen_freq,
3531			  p2p_wps_method_text(dev->wps_method),
3532			  MAC2STR(dev->interface_addr),
3533			  MAC2STR(dev->member_in_go_dev),
3534			  MAC2STR(dev->member_in_go_iface),
3535			  dev->go_neg_req_sent,
3536			  p2p_go_state_text(dev->go_state),
3537			  dev->dialog_token,
3538			  MAC2STR(dev->intended_addr),
3539			  dev->country[0] ? dev->country[0] : '_',
3540			  dev->country[1] ? dev->country[1] : '_',
3541			  dev->oper_freq,
3542			  dev->req_config_methods,
3543			  dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
3544			  "[PROBE_REQ_ONLY]" : "",
3545			  dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
3546			  dev->flags & P2P_DEV_NOT_YET_READY ?
3547			  "[NOT_YET_READY]" : "",
3548			  dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
3549			  dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
3550			  "",
3551			  dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
3552			  "[PD_PEER_DISPLAY]" : "",
3553			  dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
3554			  "[PD_PEER_KEYPAD]" : "",
3555			  dev->flags & P2P_DEV_USER_REJECTED ?
3556			  "[USER_REJECTED]" : "",
3557			  dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
3558			  "[PEER_WAITING_RESPONSE]" : "",
3559			  dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
3560			  "[PREFER_PERSISTENT_GROUP]" : "",
3561			  dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
3562			  "[WAIT_GO_NEG_RESPONSE]" : "",
3563			  dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
3564			  "[WAIT_GO_NEG_CONFIRM]" : "",
3565			  dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
3566			  "[GROUP_CLIENT_ONLY]" : "",
3567			  dev->flags & P2P_DEV_FORCE_FREQ ?
3568			  "[FORCE_FREQ]" : "",
3569			  dev->flags & P2P_DEV_PD_FOR_JOIN ?
3570			  "[PD_FOR_JOIN]" : "",
3571			  dev->status,
3572			  dev->wait_count,
3573			  dev->invitation_reqs);
3574	if (res < 0 || res >= end - pos)
3575		return pos - buf;
3576	pos += res;
3577
3578	if (dev->ext_listen_period) {
3579		res = os_snprintf(pos, end - pos,
3580				  "ext_listen_period=%u\n"
3581				  "ext_listen_interval=%u\n",
3582				  dev->ext_listen_period,
3583				  dev->ext_listen_interval);
3584		if (res < 0 || res >= end - pos)
3585			return pos - buf;
3586		pos += res;
3587	}
3588
3589	if (dev->oper_ssid_len) {
3590		res = os_snprintf(pos, end - pos,
3591				  "oper_ssid=%s\n",
3592				  wpa_ssid_txt(dev->oper_ssid,
3593					       dev->oper_ssid_len));
3594		if (res < 0 || res >= end - pos)
3595			return pos - buf;
3596		pos += res;
3597	}
3598
3599#ifdef CONFIG_WIFI_DISPLAY
3600	if (dev->info.wfd_subelems) {
3601		res = os_snprintf(pos, end - pos, "wfd_subelems=");
3602		if (res < 0 || res >= end - pos)
3603			return pos - buf;
3604		pos += res;
3605
3606		pos += wpa_snprintf_hex(pos, end - pos,
3607					wpabuf_head(dev->info.wfd_subelems),
3608					wpabuf_len(dev->info.wfd_subelems));
3609
3610		res = os_snprintf(pos, end - pos, "\n");
3611		if (res < 0 || res >= end - pos)
3612			return pos - buf;
3613		pos += res;
3614	}
3615#endif /* CONFIG_WIFI_DISPLAY */
3616
3617	return pos - buf;
3618}
3619
3620
3621int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
3622{
3623	return p2p_get_device(p2p, addr) != NULL;
3624}
3625
3626
3627void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
3628{
3629	if (enabled) {
3630		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3631			"discoverability enabled");
3632		p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3633	} else {
3634		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3635			"discoverability disabled");
3636		p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3637	}
3638}
3639
3640
3641static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
3642					      u32 duration2, u32 interval2)
3643{
3644	struct wpabuf *req;
3645	struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
3646	u8 *len;
3647
3648	req = wpabuf_alloc(100);
3649	if (req == NULL)
3650		return NULL;
3651
3652	if (duration1 || interval1) {
3653		os_memset(&desc1, 0, sizeof(desc1));
3654		desc1.count_type = 1;
3655		desc1.duration = duration1;
3656		desc1.interval = interval1;
3657		ptr1 = &desc1;
3658
3659		if (duration2 || interval2) {
3660			os_memset(&desc2, 0, sizeof(desc2));
3661			desc2.count_type = 2;
3662			desc2.duration = duration2;
3663			desc2.interval = interval2;
3664			ptr2 = &desc2;
3665		}
3666	}
3667
3668	p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
3669	len = p2p_buf_add_ie_hdr(req);
3670	p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
3671	p2p_buf_update_ie_hdr(req, len);
3672
3673	return req;
3674}
3675
3676
3677int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
3678		     const u8 *own_interface_addr, unsigned int freq,
3679		     u32 duration1, u32 interval1, u32 duration2,
3680		     u32 interval2)
3681{
3682	struct wpabuf *req;
3683
3684	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
3685		"GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
3686		"int1=%u dur2=%u int2=%u",
3687		MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
3688		freq, duration1, interval1, duration2, interval2);
3689
3690	req = p2p_build_presence_req(duration1, interval1, duration2,
3691				     interval2);
3692	if (req == NULL)
3693		return -1;
3694
3695	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3696	if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3697			    go_interface_addr,
3698			    wpabuf_head(req), wpabuf_len(req), 200) < 0) {
3699		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3700			"P2P: Failed to send Action frame");
3701	}
3702	wpabuf_free(req);
3703
3704	return 0;
3705}
3706
3707
3708static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3709					       size_t noa_len, u8 dialog_token)
3710{
3711	struct wpabuf *resp;
3712	u8 *len;
3713
3714	resp = wpabuf_alloc(100 + noa_len);
3715	if (resp == NULL)
3716		return NULL;
3717
3718	p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3719	len = p2p_buf_add_ie_hdr(resp);
3720	p2p_buf_add_status(resp, status);
3721	if (noa) {
3722		wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3723		wpabuf_put_le16(resp, noa_len);
3724		wpabuf_put_data(resp, noa, noa_len);
3725	} else
3726		p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3727	p2p_buf_update_ie_hdr(resp, len);
3728
3729	return resp;
3730}
3731
3732
3733static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3734				     const u8 *sa, const u8 *data, size_t len,
3735				     int rx_freq)
3736{
3737	struct p2p_message msg;
3738	u8 status;
3739	struct wpabuf *resp;
3740	size_t g;
3741	struct p2p_group *group = NULL;
3742	int parsed = 0;
3743	u8 noa[50];
3744	int noa_len;
3745
3746	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3747		"P2P: Received P2P Action - P2P Presence Request");
3748
3749	for (g = 0; g < p2p->num_groups; g++) {
3750		if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3751			      ETH_ALEN) == 0) {
3752			group = p2p->groups[g];
3753			break;
3754		}
3755	}
3756	if (group == NULL) {
3757		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3758			"P2P: Ignore P2P Presence Request for unknown group "
3759			MACSTR, MAC2STR(da));
3760		return;
3761	}
3762
3763	if (p2p_parse(data, len, &msg) < 0) {
3764		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3765			"P2P: Failed to parse P2P Presence Request");
3766		status = P2P_SC_FAIL_INVALID_PARAMS;
3767		goto fail;
3768	}
3769	parsed = 1;
3770
3771	if (msg.noa == NULL) {
3772		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3773			"P2P: No NoA attribute in P2P Presence Request");
3774		status = P2P_SC_FAIL_INVALID_PARAMS;
3775		goto fail;
3776	}
3777
3778	status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3779
3780fail:
3781	if (p2p->cfg->get_noa)
3782		noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3783					    sizeof(noa));
3784	else
3785		noa_len = -1;
3786	resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3787				       noa_len > 0 ? noa_len : 0,
3788				       msg.dialog_token);
3789	if (parsed)
3790		p2p_parse_free(&msg);
3791	if (resp == NULL)
3792		return;
3793
3794	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3795	if (p2p_send_action(p2p, rx_freq, sa, da, da,
3796			    wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
3797		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3798			"P2P: Failed to send Action frame");
3799	}
3800	wpabuf_free(resp);
3801}
3802
3803
3804static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3805				      const u8 *sa, const u8 *data, size_t len)
3806{
3807	struct p2p_message msg;
3808
3809	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3810		"P2P: Received P2P Action - P2P Presence Response");
3811
3812	if (p2p_parse(data, len, &msg) < 0) {
3813		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3814			"P2P: Failed to parse P2P Presence Response");
3815		return;
3816	}
3817
3818	if (msg.status == NULL || msg.noa == NULL) {
3819		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3820			"P2P: No Status or NoA attribute in P2P Presence "
3821			"Response");
3822		p2p_parse_free(&msg);
3823		return;
3824	}
3825
3826	if (*msg.status) {
3827		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3828			"P2P: P2P Presence Request was rejected: status %u",
3829			*msg.status);
3830		p2p_parse_free(&msg);
3831		return;
3832	}
3833
3834	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3835		"P2P: P2P Presence Request was accepted");
3836	wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
3837		    msg.noa, msg.noa_len);
3838	/* TODO: process NoA */
3839	p2p_parse_free(&msg);
3840}
3841
3842
3843static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3844{
3845	struct p2p_data *p2p = eloop_ctx;
3846
3847	if (p2p->ext_listen_interval) {
3848		/* Schedule next extended listen timeout */
3849		eloop_register_timeout(p2p->ext_listen_interval_sec,
3850				       p2p->ext_listen_interval_usec,
3851				       p2p_ext_listen_timeout, p2p, NULL);
3852	}
3853
3854	if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
3855		/*
3856		 * This should not really happen, but it looks like the Listen
3857		 * command may fail is something else (e.g., a scan) was
3858		 * running at an inconvenient time. As a workaround, allow new
3859		 * Extended Listen operation to be started.
3860		 */
3861		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
3862			"Extended Listen operation had not been completed - "
3863			"try again");
3864		p2p->ext_listen_only = 0;
3865		p2p_set_state(p2p, P2P_IDLE);
3866	}
3867
3868	if (p2p->state != P2P_IDLE) {
3869		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
3870			"Listen timeout in active state (%s)",
3871			p2p_state_txt(p2p->state));
3872		return;
3873	}
3874
3875	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
3876	p2p->ext_listen_only = 1;
3877	if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
3878		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
3879			"Listen state for Extended Listen Timing");
3880		p2p->ext_listen_only = 0;
3881	}
3882}
3883
3884
3885int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
3886		   unsigned int interval)
3887{
3888	if (period > 65535 || interval > 65535 || period > interval ||
3889	    (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
3890		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3891			"P2P: Invalid Extended Listen Timing request: "
3892			"period=%u interval=%u", period, interval);
3893		return -1;
3894	}
3895
3896	eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
3897
3898	if (interval == 0) {
3899		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3900			"P2P: Disabling Extended Listen Timing");
3901		p2p->ext_listen_period = 0;
3902		p2p->ext_listen_interval = 0;
3903		return 0;
3904	}
3905
3906	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3907		"P2P: Enabling Extended Listen Timing: period %u msec, "
3908		"interval %u msec", period, interval);
3909	p2p->ext_listen_period = period;
3910	p2p->ext_listen_interval = interval;
3911	p2p->ext_listen_interval_sec = interval / 1000;
3912	p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
3913
3914	eloop_register_timeout(p2p->ext_listen_interval_sec,
3915			       p2p->ext_listen_interval_usec,
3916			       p2p_ext_listen_timeout, p2p, NULL);
3917
3918	return 0;
3919}
3920
3921
3922void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3923		      const u8 *ie, size_t ie_len)
3924{
3925	struct p2p_message msg;
3926
3927	if (bssid == NULL || ie == NULL)
3928		return;
3929
3930	os_memset(&msg, 0, sizeof(msg));
3931	if (p2p_parse_ies(ie, ie_len, &msg))
3932		return;
3933	if (msg.minor_reason_code == NULL)
3934		return;
3935
3936	wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3937		"P2P: Deauthentication notification BSSID " MACSTR
3938		" reason_code=%u minor_reason_code=%u",
3939		MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3940
3941	p2p_parse_free(&msg);
3942}
3943
3944
3945void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3946			const u8 *ie, size_t ie_len)
3947{
3948	struct p2p_message msg;
3949
3950	if (bssid == NULL || ie == NULL)
3951		return;
3952
3953	os_memset(&msg, 0, sizeof(msg));
3954	if (p2p_parse_ies(ie, ie_len, &msg))
3955		return;
3956	if (msg.minor_reason_code == NULL)
3957		return;
3958
3959	wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3960		"P2P: Disassociation notification BSSID " MACSTR
3961		" reason_code=%u minor_reason_code=%u",
3962		MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3963
3964	p2p_parse_free(&msg);
3965}
3966
3967
3968void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
3969{
3970	if (enabled) {
3971		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3972			"Device operations enabled");
3973		p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
3974	} else {
3975		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3976			"Device operations disabled");
3977		p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
3978	}
3979}
3980
3981
3982int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
3983{
3984	if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
3985		return -1;
3986
3987	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
3988		"reg_class %u channel %u", reg_class, channel);
3989	p2p->cfg->reg_class = reg_class;
3990	p2p->cfg->channel = channel;
3991
3992	return 0;
3993}
3994
3995
3996int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
3997{
3998	wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
3999	if (postfix == NULL) {
4000		p2p->cfg->ssid_postfix_len = 0;
4001		return 0;
4002	}
4003	if (len > sizeof(p2p->cfg->ssid_postfix))
4004		return -1;
4005	os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4006	p2p->cfg->ssid_postfix_len = len;
4007	return 0;
4008}
4009
4010
4011int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4012			 int cfg_op_channel)
4013{
4014	if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
4015	    < 0)
4016		return -1;
4017
4018	wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
4019		"reg_class %u channel %u", op_reg_class, op_channel);
4020	p2p->cfg->op_reg_class = op_reg_class;
4021	p2p->cfg->op_channel = op_channel;
4022	p2p->cfg->cfg_op_channel = cfg_op_channel;
4023	return 0;
4024}
4025
4026
4027int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4028		      const struct p2p_channel *pref_chan)
4029{
4030	struct p2p_channel *n;
4031
4032	if (pref_chan) {
4033		n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
4034		if (n == NULL)
4035			return -1;
4036		os_memcpy(n, pref_chan,
4037			  num_pref_chan * sizeof(struct p2p_channel));
4038	} else
4039		n = NULL;
4040
4041	os_free(p2p->cfg->pref_chan);
4042	p2p->cfg->pref_chan = n;
4043	p2p->cfg->num_pref_chan = num_pref_chan;
4044
4045	return 0;
4046}
4047
4048
4049int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4050			   u8 *iface_addr)
4051{
4052	struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4053	if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4054		return -1;
4055	os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4056	return 0;
4057}
4058
4059
4060int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4061			   u8 *dev_addr)
4062{
4063	struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4064	if (dev == NULL)
4065		return -1;
4066	os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4067	return 0;
4068}
4069
4070
4071void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4072{
4073	os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4074	if (is_zero_ether_addr(p2p->peer_filter))
4075		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
4076			"filter");
4077	else
4078		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
4079			"filter for " MACSTR, MAC2STR(p2p->peer_filter));
4080}
4081
4082
4083void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4084{
4085	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
4086		enabled ? "enabled" : "disabled");
4087	if (p2p->cross_connect == enabled)
4088		return;
4089	p2p->cross_connect = enabled;
4090	/* TODO: may need to tear down any action group where we are GO(?) */
4091}
4092
4093
4094int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4095{
4096	struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4097	if (dev == NULL)
4098		return -1;
4099	if (dev->oper_freq <= 0)
4100		return -1;
4101	return dev->oper_freq;
4102}
4103
4104
4105void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4106{
4107	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
4108		enabled ? "enabled" : "disabled");
4109	p2p->cfg->p2p_intra_bss = enabled;
4110}
4111
4112
4113void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
4114{
4115	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
4116	os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
4117}
4118
4119
4120int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4121		    const u8 *src, const u8 *bssid, const u8 *buf,
4122		    size_t len, unsigned int wait_time)
4123{
4124	if (p2p->p2p_scan_running) {
4125		wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
4126			"frame TX until p2p_scan completes");
4127		if (p2p->after_scan_tx) {
4128			wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
4129				"previous pending Action frame TX");
4130			os_free(p2p->after_scan_tx);
4131		}
4132		p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
4133					       len);
4134		if (p2p->after_scan_tx == NULL)
4135			return -1;
4136		p2p->after_scan_tx->freq = freq;
4137		os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
4138		os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
4139		os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
4140		p2p->after_scan_tx->len = len;
4141		p2p->after_scan_tx->wait_time = wait_time;
4142		os_memcpy(p2p->after_scan_tx + 1, buf, len);
4143		return 0;
4144	}
4145
4146	return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4147				     buf, len, wait_time);
4148}
4149
4150
4151void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4152			   int freq_overall)
4153{
4154	wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
4155		"  5 GHz: %d,  overall: %d", freq_24, freq_5, freq_overall);
4156	p2p->best_freq_24 = freq_24;
4157	p2p->best_freq_5 = freq_5;
4158	p2p->best_freq_overall = freq_overall;
4159}
4160
4161
4162const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4163{
4164	if (p2p == NULL || p2p->go_neg_peer == NULL)
4165		return NULL;
4166	return p2p->go_neg_peer->info.p2p_device_addr;
4167}
4168
4169
4170const struct p2p_peer_info *
4171p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4172{
4173	struct p2p_device *dev;
4174
4175	if (addr) {
4176		dev = p2p_get_device(p2p, addr);
4177		if (!dev)
4178			return NULL;
4179
4180		if (!next) {
4181			if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
4182				return NULL;
4183
4184			return &dev->info;
4185		} else {
4186			do {
4187				dev = dl_list_first(&dev->list,
4188						    struct p2p_device,
4189						    list);
4190				if (&dev->list == &p2p->devices)
4191					return NULL;
4192			} while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
4193		}
4194	} else {
4195		dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4196		if (!dev)
4197			return NULL;
4198		while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
4199			dev = dl_list_first(&dev->list,
4200					    struct p2p_device,
4201					    list);
4202			if (&dev->list == &p2p->devices)
4203				return NULL;
4204		}
4205	}
4206
4207	return &dev->info;
4208}
4209
4210#ifdef ANDROID_P2P
4211int p2p_search_in_progress(struct p2p_data *p2p)
4212{
4213	if (p2p == NULL)
4214		return 0;
4215
4216	return p2p->state == P2P_SEARCH;
4217}
4218#endif
4219
4220int p2p_in_progress(struct p2p_data *p2p)
4221{
4222	if (p2p == NULL)
4223		return 0;
4224	if (p2p->state == P2P_SEARCH || p2p->state == P2P_SEARCH_WHEN_READY ||
4225	    p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY)
4226		return 2;
4227	return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
4228}
4229
4230
4231void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
4232			    u8 client_timeout)
4233{
4234	if (p2p) {
4235		p2p->go_timeout = go_timeout;
4236		p2p->client_timeout = client_timeout;
4237	}
4238}
4239
4240
4241void p2p_increase_search_delay(struct p2p_data *p2p, unsigned int delay)
4242{
4243	if (p2p && p2p->search_delay < delay)
4244		p2p->search_delay = delay;
4245}
4246
4247
4248#ifdef CONFIG_WIFI_DISPLAY
4249
4250static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
4251{
4252	size_t g;
4253	struct p2p_group *group;
4254
4255	for (g = 0; g < p2p->num_groups; g++) {
4256		group = p2p->groups[g];
4257		p2p_group_update_ies(group);
4258	}
4259}
4260
4261
4262int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
4263{
4264	wpabuf_free(p2p->wfd_ie_beacon);
4265	p2p->wfd_ie_beacon = ie;
4266	p2p_update_wfd_ie_groups(p2p);
4267	return 0;
4268}
4269
4270
4271int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
4272{
4273	wpabuf_free(p2p->wfd_ie_probe_req);
4274	p2p->wfd_ie_probe_req = ie;
4275	return 0;
4276}
4277
4278
4279int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
4280{
4281	wpabuf_free(p2p->wfd_ie_probe_resp);
4282	p2p->wfd_ie_probe_resp = ie;
4283	p2p_update_wfd_ie_groups(p2p);
4284	return 0;
4285}
4286
4287
4288int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
4289{
4290	wpabuf_free(p2p->wfd_ie_assoc_req);
4291	p2p->wfd_ie_assoc_req = ie;
4292	return 0;
4293}
4294
4295
4296int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
4297{
4298	wpabuf_free(p2p->wfd_ie_invitation);
4299	p2p->wfd_ie_invitation = ie;
4300	return 0;
4301}
4302
4303
4304int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
4305{
4306	wpabuf_free(p2p->wfd_ie_prov_disc_req);
4307	p2p->wfd_ie_prov_disc_req = ie;
4308	return 0;
4309}
4310
4311
4312int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
4313{
4314	wpabuf_free(p2p->wfd_ie_prov_disc_resp);
4315	p2p->wfd_ie_prov_disc_resp = ie;
4316	return 0;
4317}
4318
4319
4320int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
4321{
4322	wpabuf_free(p2p->wfd_ie_go_neg);
4323	p2p->wfd_ie_go_neg = ie;
4324	return 0;
4325}
4326
4327
4328int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
4329{
4330	wpabuf_free(p2p->wfd_dev_info);
4331	if (elem) {
4332		p2p->wfd_dev_info = wpabuf_dup(elem);
4333		if (p2p->wfd_dev_info == NULL)
4334			return -1;
4335	} else
4336		p2p->wfd_dev_info = NULL;
4337
4338	return 0;
4339}
4340
4341
4342int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
4343{
4344	wpabuf_free(p2p->wfd_assoc_bssid);
4345	if (elem) {
4346		p2p->wfd_assoc_bssid = wpabuf_dup(elem);
4347		if (p2p->wfd_assoc_bssid == NULL)
4348			return -1;
4349	} else
4350		p2p->wfd_assoc_bssid = NULL;
4351
4352	return 0;
4353}
4354
4355
4356int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
4357				  const struct wpabuf *elem)
4358{
4359	wpabuf_free(p2p->wfd_coupled_sink_info);
4360	if (elem) {
4361		p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
4362		if (p2p->wfd_coupled_sink_info == NULL)
4363			return -1;
4364	} else
4365		p2p->wfd_coupled_sink_info = NULL;
4366
4367	return 0;
4368}
4369
4370#endif /* CONFIG_WIFI_DISPLAY */
4371