• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <log/log.h>
12 #include "common.h"
13 #include "eloop.h"
14 #include "common/defs.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/ieee802_11_common.h"
17 #include "common/wpa_ctrl.h"
18 #include "crypto/sha256.h"
19 #include "crypto/crypto.h"
20 #include "wps/wps_i.h"
21 #include "p2p_i.h"
22 #include "p2p.h"
23 
24 
25 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
26 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
27 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
28 				     const u8 *sa, const u8 *data, size_t len,
29 				     int rx_freq);
30 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
31 				      const u8 *sa, const u8 *data,
32 				      size_t len);
33 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
34 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
35 
36 
37 /*
38  * p2p_scan recovery timeout
39  *
40  * Many drivers are using 30 second timeout on scan results. Allow a bit larger
41  * timeout for this to avoid hitting P2P timeout unnecessarily.
42  */
43 #define P2P_SCAN_TIMEOUT 35
44 
45 /**
46  * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
47  * entries will be removed
48  */
49 #ifndef P2P_PEER_EXPIRATION_AGE
50 #define P2P_PEER_EXPIRATION_AGE 60
51 #endif /* P2P_PEER_EXPIRATION_AGE */
52 
53 
p2p_expire_peers(struct p2p_data * p2p)54 void p2p_expire_peers(struct p2p_data *p2p)
55 {
56 	struct p2p_device *dev, *n;
57 	struct os_reltime now;
58 	size_t i;
59 
60 	os_get_reltime(&now);
61 	dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
62 		if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
63 			continue;
64 
65 		if (dev == p2p->go_neg_peer) {
66 			/*
67 			 * GO Negotiation is in progress with the peer, so
68 			 * don't expire the peer entry until GO Negotiation
69 			 * fails or times out.
70 			 */
71 			continue;
72 		}
73 
74 		if (p2p->cfg->go_connected &&
75 		    p2p->cfg->go_connected(p2p->cfg->cb_ctx,
76 					   dev->info.p2p_device_addr)) {
77 			/*
78 			 * We are connected as a client to a group in which the
79 			 * peer is the GO, so do not expire the peer entry.
80 			 */
81 			os_get_reltime(&dev->last_seen);
82 			continue;
83 		}
84 
85 		for (i = 0; i < p2p->num_groups; i++) {
86 			if (p2p_group_is_client_connected(
87 				    p2p->groups[i], dev->info.p2p_device_addr))
88 				break;
89 		}
90 		if (i < p2p->num_groups) {
91 			/*
92 			 * The peer is connected as a client in a group where
93 			 * we are the GO, so do not expire the peer entry.
94 			 */
95 			os_get_reltime(&dev->last_seen);
96 			continue;
97 		}
98 
99 		p2p_dbg(p2p, "Expiring old peer entry " MACSTR,
100 			MAC2STR(dev->info.p2p_device_addr));
101 		dl_list_del(&dev->list);
102 		p2p_device_free(p2p, dev);
103 	}
104 }
105 
106 
p2p_state_txt(int state)107 static const char * p2p_state_txt(int state)
108 {
109 	switch (state) {
110 	case P2P_IDLE:
111 		return "IDLE";
112 	case P2P_SEARCH:
113 		return "SEARCH";
114 	case P2P_CONNECT:
115 		return "CONNECT";
116 	case P2P_CONNECT_LISTEN:
117 		return "CONNECT_LISTEN";
118 	case P2P_GO_NEG:
119 		return "GO_NEG";
120 	case P2P_LISTEN_ONLY:
121 		return "LISTEN_ONLY";
122 	case P2P_WAIT_PEER_CONNECT:
123 		return "WAIT_PEER_CONNECT";
124 	case P2P_WAIT_PEER_IDLE:
125 		return "WAIT_PEER_IDLE";
126 	case P2P_SD_DURING_FIND:
127 		return "SD_DURING_FIND";
128 	case P2P_PROVISIONING:
129 		return "PROVISIONING";
130 	case P2P_PD_DURING_FIND:
131 		return "PD_DURING_FIND";
132 	case P2P_INVITE:
133 		return "INVITE";
134 	case P2P_INVITE_LISTEN:
135 		return "INVITE_LISTEN";
136 	default:
137 		return "?";
138 	}
139 }
140 
141 
p2p_get_state_txt(struct p2p_data * p2p)142 const char * p2p_get_state_txt(struct p2p_data *p2p)
143 {
144 	return p2p_state_txt(p2p->state);
145 }
146 
147 
p2p_get_p2ps_adv_list(struct p2p_data * p2p)148 struct p2ps_advertisement * p2p_get_p2ps_adv_list(struct p2p_data *p2p)
149 {
150 	return p2p ? p2p->p2ps_adv_list : NULL;
151 }
152 
153 
p2p_set_intended_addr(struct p2p_data * p2p,const u8 * intended_addr)154 void p2p_set_intended_addr(struct p2p_data *p2p, const u8 *intended_addr)
155 {
156 	if (p2p && intended_addr)
157 		os_memcpy(p2p->intended_addr, intended_addr, ETH_ALEN);
158 }
159 
160 
p2p_get_provisioning_info(struct p2p_data * p2p,const u8 * addr)161 u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
162 {
163 	struct p2p_device *dev = NULL;
164 
165 	if (!addr || !p2p)
166 		return 0;
167 
168 	dev = p2p_get_device(p2p, addr);
169 	if (dev)
170 		return dev->wps_prov_info;
171 	else
172 		return 0;
173 }
174 
175 
p2p_clear_provisioning_info(struct p2p_data * p2p,const u8 * addr)176 void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
177 {
178 	struct p2p_device *dev = NULL;
179 
180 	if (!addr || !p2p)
181 		return;
182 
183 	dev = p2p_get_device(p2p, addr);
184 	if (dev)
185 		dev->wps_prov_info = 0;
186 }
187 
188 
p2p_set_state(struct p2p_data * p2p,int new_state)189 void p2p_set_state(struct p2p_data *p2p, int new_state)
190 {
191 	p2p_dbg(p2p, "State %s -> %s",
192 		p2p_state_txt(p2p->state), p2p_state_txt(new_state));
193 	p2p->state = new_state;
194 
195 	if (new_state == P2P_IDLE && p2p->pending_channel) {
196 		p2p_dbg(p2p, "Apply change in listen channel");
197 		p2p->cfg->reg_class = p2p->pending_reg_class;
198 		p2p->cfg->channel = p2p->pending_channel;
199 		p2p->pending_reg_class = 0;
200 		p2p->pending_channel = 0;
201 	}
202 }
203 
204 
p2p_set_timeout(struct p2p_data * p2p,unsigned int sec,unsigned int usec)205 void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
206 {
207 	p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec",
208 		p2p_state_txt(p2p->state), sec, usec);
209 	eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
210 	eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
211 }
212 
213 
p2p_clear_timeout(struct p2p_data * p2p)214 void p2p_clear_timeout(struct p2p_data *p2p)
215 {
216 	p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state));
217 	eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
218 }
219 
220 
p2p_go_neg_failed(struct p2p_data * p2p,int status)221 void p2p_go_neg_failed(struct p2p_data *p2p, int status)
222 {
223 	struct p2p_go_neg_results res;
224 	struct p2p_device *peer = p2p->go_neg_peer;
225 
226 	if (!peer)
227 		return;
228 
229 	eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
230 	if (p2p->state != P2P_SEARCH) {
231 		/*
232 		 * Clear timeouts related to GO Negotiation if no new p2p_find
233 		 * has been started.
234 		 */
235 		p2p_clear_timeout(p2p);
236 		p2p_set_state(p2p, P2P_IDLE);
237 	}
238 
239 	peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
240 	peer->wps_method = WPS_NOT_READY;
241 	peer->oob_pw_id = 0;
242 	wpabuf_free(peer->go_neg_conf);
243 	peer->go_neg_conf = NULL;
244 	p2p->go_neg_peer = NULL;
245 
246 	os_memset(&res, 0, sizeof(res));
247 	res.status = status;
248 	os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
249 	os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
250 	p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
251 }
252 
253 
p2p_listen_in_find(struct p2p_data * p2p,int dev_disc)254 static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc)
255 {
256 	unsigned int r, tu;
257 	int freq;
258 	struct wpabuf *ies;
259 
260 	p2p_dbg(p2p, "Starting short listen state (state=%s)",
261 		p2p_state_txt(p2p->state));
262 
263 	if (p2p->pending_listen_freq) {
264 		/* We have a pending p2p_listen request */
265 		p2p_dbg(p2p, "p2p_listen command pending already");
266 		return;
267 	}
268 
269 	freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
270 	if (freq < 0) {
271 		p2p_dbg(p2p, "Unknown regulatory class/channel");
272 		return;
273 	}
274 
275 	if (os_get_random((u8 *) &r, sizeof(r)) < 0)
276 		r = 0;
277 	tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
278 	      p2p->min_disc_int) * 100;
279 	if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu)
280 		tu = p2p->max_disc_tu;
281 	if (!dev_disc && tu < 100)
282 		tu = 100; /* Need to wait in non-device discovery use cases */
283 	if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen)
284 		tu = p2p->cfg->max_listen * 1000 / 1024;
285 
286 	if (tu == 0) {
287 		p2p_dbg(p2p, "Skip listen state since duration was 0 TU");
288 		p2p_set_timeout(p2p, 0, 0);
289 		return;
290 	}
291 
292 	ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
293 	if (ies == NULL)
294 		return;
295 
296 	p2p->pending_listen_freq = freq;
297 	p2p->pending_listen_sec = 0;
298 	p2p->pending_listen_usec = 1024 * tu;
299 
300 	if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
301 		    ies) < 0) {
302 		p2p_dbg(p2p, "Failed to start listen mode");
303 		p2p->pending_listen_freq = 0;
304 	}
305 	wpabuf_free(ies);
306 }
307 
308 
p2p_listen(struct p2p_data * p2p,unsigned int timeout)309 int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
310 {
311 	int freq;
312 	struct wpabuf *ies;
313 
314 	p2p_dbg(p2p, "Going to listen(only) state");
315 
316 	if (p2p->pending_listen_freq) {
317 		/* We have a pending p2p_listen request */
318 		p2p_dbg(p2p, "p2p_listen command pending already");
319 		return -1;
320 	}
321 
322 	freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
323 	if (freq < 0) {
324 		p2p_dbg(p2p, "Unknown regulatory class/channel");
325 		return -1;
326 	}
327 
328 	p2p->pending_listen_sec = timeout / 1000;
329 	p2p->pending_listen_usec = (timeout % 1000) * 1000;
330 
331 	if (p2p->p2p_scan_running) {
332 		if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
333 			p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen");
334 			return 0;
335 		}
336 		p2p_dbg(p2p, "p2p_scan running - delay start of listen state");
337 		p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
338 		return 0;
339 	}
340 
341 	ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
342 	if (ies == NULL)
343 		return -1;
344 
345 	p2p->pending_listen_freq = freq;
346 
347 	if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
348 		p2p_dbg(p2p, "Failed to start listen mode");
349 		p2p->pending_listen_freq = 0;
350 		wpabuf_free(ies);
351 		return -1;
352 	}
353 	wpabuf_free(ies);
354 
355 	p2p_set_state(p2p, P2P_LISTEN_ONLY);
356 
357 	return 0;
358 }
359 
360 
p2p_device_clear_reported(struct p2p_data * p2p)361 static void p2p_device_clear_reported(struct p2p_data *p2p)
362 {
363 	struct p2p_device *dev;
364 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
365 		dev->flags &= ~P2P_DEV_REPORTED;
366 		dev->sd_reqs = 0;
367 	}
368 }
369 
370 
371 /**
372  * p2p_get_device - Fetch a peer entry
373  * @p2p: P2P module context from p2p_init()
374  * @addr: P2P Device Address of the peer
375  * Returns: Pointer to the device entry or %NULL if not found
376  */
p2p_get_device(struct p2p_data * p2p,const u8 * addr)377 struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
378 {
379 	struct p2p_device *dev;
380 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
381 		if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
382 			return dev;
383 	}
384 	return NULL;
385 }
386 
387 
388 /**
389  * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
390  * @p2p: P2P module context from p2p_init()
391  * @addr: P2P Interface Address of the peer
392  * Returns: Pointer to the device entry or %NULL if not found
393  */
p2p_get_device_interface(struct p2p_data * p2p,const u8 * addr)394 struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
395 					     const u8 *addr)
396 {
397 	struct p2p_device *dev;
398 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
399 		if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
400 			return dev;
401 	}
402 	return NULL;
403 }
404 
405 
406 /**
407  * p2p_create_device - Create a peer entry
408  * @p2p: P2P module context from p2p_init()
409  * @addr: P2P Device Address of the peer
410  * Returns: Pointer to the device entry or %NULL on failure
411  *
412  * If there is already an entry for the peer, it will be returned instead of
413  * creating a new one.
414  */
p2p_create_device(struct p2p_data * p2p,const u8 * addr)415 static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
416 					     const u8 *addr)
417 {
418 	struct p2p_device *dev, *oldest = NULL;
419 	size_t count = 0;
420 
421 	dev = p2p_get_device(p2p, addr);
422 	if (dev)
423 		return dev;
424 
425 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
426 		count++;
427 		if (oldest == NULL ||
428 		    os_reltime_before(&dev->last_seen, &oldest->last_seen))
429 			oldest = dev;
430 	}
431 	if (count + 1 > p2p->cfg->max_peers && oldest) {
432 		p2p_dbg(p2p, "Remove oldest peer entry to make room for a new peer");
433 		dl_list_del(&oldest->list);
434 		p2p_device_free(p2p, oldest);
435 	}
436 
437 	dev = os_zalloc(sizeof(*dev));
438 	if (dev == NULL)
439 		return NULL;
440 	dl_list_add(&p2p->devices, &dev->list);
441 	os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
442 
443 	return dev;
444 }
445 
446 
p2p_copy_client_info(struct p2p_device * dev,struct p2p_client_info * cli)447 static void p2p_copy_client_info(struct p2p_device *dev,
448 				 struct p2p_client_info *cli)
449 {
450 	p2p_copy_filter_devname(dev->info.device_name,
451 				sizeof(dev->info.device_name),
452 				cli->dev_name, cli->dev_name_len);
453 	dev->info.dev_capab = cli->dev_capab;
454 	dev->info.config_methods = cli->config_methods;
455 	os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
456 	dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
457 	if (dev->info.wps_sec_dev_type_list_len > WPS_SEC_DEV_TYPE_MAX_LEN) {
458 		android_errorWriteLog(0x534e4554, "172937525");
459 		dev->info.wps_sec_dev_type_list_len = WPS_SEC_DEV_TYPE_MAX_LEN;
460 	}
461 	os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
462 		  dev->info.wps_sec_dev_type_list_len);
463 }
464 
465 
p2p_add_group_clients(struct p2p_data * p2p,const u8 * go_dev_addr,const u8 * go_interface_addr,int freq,const u8 * gi,size_t gi_len,struct os_reltime * rx_time)466 static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
467 				 const u8 *go_interface_addr, int freq,
468 				 const u8 *gi, size_t gi_len,
469 				 struct os_reltime *rx_time)
470 {
471 	struct p2p_group_info info;
472 	size_t c;
473 	struct p2p_device *dev;
474 
475 	if (gi == NULL)
476 		return 0;
477 
478 	if (p2p_group_info_parse(gi, gi_len, &info) < 0)
479 		return -1;
480 
481 	/*
482 	 * Clear old data for this group; if the devices are still in the
483 	 * group, the information will be restored in the loop following this.
484 	 */
485 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
486 		if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
487 			      ETH_ALEN) == 0) {
488 			os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
489 			os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
490 		}
491 	}
492 
493 	for (c = 0; c < info.num_clients; c++) {
494 		struct p2p_client_info *cli = &info.client[c];
495 		if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
496 			      ETH_ALEN) == 0)
497 			continue; /* ignore our own entry */
498 		dev = p2p_get_device(p2p, cli->p2p_device_addr);
499 		if (dev) {
500 			if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
501 					  P2P_DEV_PROBE_REQ_ONLY)) {
502 				/*
503 				 * Update information since we have not
504 				 * received this directly from the client.
505 				 */
506 				p2p_copy_client_info(dev, cli);
507 			} else {
508 				/*
509 				 * Need to update P2P Client Discoverability
510 				 * flag since it is valid only in P2P Group
511 				 * Info attribute.
512 				 */
513 				dev->info.dev_capab &=
514 					~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
515 				dev->info.dev_capab |=
516 					cli->dev_capab &
517 					P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
518 			}
519 			if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
520 				dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
521 			}
522 		} else {
523 			dev = p2p_create_device(p2p, cli->p2p_device_addr);
524 			if (dev == NULL)
525 				continue;
526 			dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
527 			p2p_copy_client_info(dev, cli);
528 			dev->oper_freq = freq;
529 			p2p->cfg->dev_found(p2p->cfg->cb_ctx,
530 					    dev->info.p2p_device_addr,
531 					    &dev->info, 1);
532 			dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
533 		}
534 
535 		os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
536 			  ETH_ALEN);
537 		os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
538 		os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
539 		os_memcpy(dev->member_in_go_iface, go_interface_addr,
540 			  ETH_ALEN);
541 		dev->flags |= P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT;
542 	}
543 
544 	return 0;
545 }
546 
547 
p2p_copy_wps_info(struct p2p_data * p2p,struct p2p_device * dev,int probe_req,const struct p2p_message * msg)548 static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev,
549 			      int probe_req, const struct p2p_message *msg)
550 {
551 	os_memcpy(dev->info.device_name, msg->device_name,
552 		  sizeof(dev->info.device_name));
553 
554 	if (msg->manufacturer &&
555 	    msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
556 		os_memset(dev->info.manufacturer, 0,
557 			  sizeof(dev->info.manufacturer));
558 		os_memcpy(dev->info.manufacturer, msg->manufacturer,
559 			  msg->manufacturer_len);
560 	}
561 
562 	if (msg->model_name &&
563 	    msg->model_name_len < sizeof(dev->info.model_name)) {
564 		os_memset(dev->info.model_name, 0,
565 			  sizeof(dev->info.model_name));
566 		os_memcpy(dev->info.model_name, msg->model_name,
567 			  msg->model_name_len);
568 	}
569 
570 	if (msg->model_number &&
571 	    msg->model_number_len < sizeof(dev->info.model_number)) {
572 		os_memset(dev->info.model_number, 0,
573 			  sizeof(dev->info.model_number));
574 		os_memcpy(dev->info.model_number, msg->model_number,
575 			  msg->model_number_len);
576 	}
577 
578 	if (msg->serial_number &&
579 	    msg->serial_number_len < sizeof(dev->info.serial_number)) {
580 		os_memset(dev->info.serial_number, 0,
581 			  sizeof(dev->info.serial_number));
582 		os_memcpy(dev->info.serial_number, msg->serial_number,
583 			  msg->serial_number_len);
584 	}
585 
586 	if (msg->pri_dev_type)
587 		os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
588 			  sizeof(dev->info.pri_dev_type));
589 	else if (msg->wps_pri_dev_type)
590 		os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
591 			  sizeof(dev->info.pri_dev_type));
592 
593 	if (msg->wps_sec_dev_type_list) {
594 		os_memcpy(dev->info.wps_sec_dev_type_list,
595 			  msg->wps_sec_dev_type_list,
596 			  msg->wps_sec_dev_type_list_len);
597 		dev->info.wps_sec_dev_type_list_len =
598 			msg->wps_sec_dev_type_list_len;
599 	}
600 
601 	if (msg->capability) {
602 		/*
603 		 * P2P Client Discoverability bit is reserved in all frames
604 		 * that use this function, so do not change its value here.
605 		 */
606 		dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
607 		dev->info.dev_capab |= msg->capability[0] &
608 			~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
609 		dev->info.group_capab = msg->capability[1];
610 	}
611 
612 	if (msg->ext_listen_timing) {
613 		dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
614 		dev->ext_listen_interval =
615 			WPA_GET_LE16(msg->ext_listen_timing + 2);
616 	}
617 
618 	if (!probe_req) {
619 		u16 new_config_methods;
620 		new_config_methods = msg->config_methods ?
621 			msg->config_methods : msg->wps_config_methods;
622 		if (new_config_methods &&
623 		    dev->info.config_methods != new_config_methods) {
624 			p2p_dbg(p2p, "Update peer " MACSTR
625 				" config_methods 0x%x -> 0x%x",
626 				MAC2STR(dev->info.p2p_device_addr),
627 				dev->info.config_methods,
628 				new_config_methods);
629 			dev->info.config_methods = new_config_methods;
630 		}
631 	}
632 }
633 
634 
p2p_update_peer_vendor_elems(struct p2p_device * dev,const u8 * ies,size_t ies_len)635 static void p2p_update_peer_vendor_elems(struct p2p_device *dev, const u8 *ies,
636 					 size_t ies_len)
637 {
638 	const u8 *pos, *end;
639 	u8 id, len;
640 
641 	wpabuf_free(dev->info.vendor_elems);
642 	dev->info.vendor_elems = NULL;
643 
644 	end = ies + ies_len;
645 
646 	for (pos = ies; end - pos > 1; pos += len) {
647 		id = *pos++;
648 		len = *pos++;
649 
650 		if (len > end - pos)
651 			break;
652 
653 		if (id != WLAN_EID_VENDOR_SPECIFIC || len < 3)
654 			continue;
655 
656 		if (len >= 4) {
657 			u32 type = WPA_GET_BE32(pos);
658 
659 			if (type == WPA_IE_VENDOR_TYPE ||
660 			    type == WMM_IE_VENDOR_TYPE ||
661 			    type == WPS_IE_VENDOR_TYPE ||
662 			    type == P2P_IE_VENDOR_TYPE ||
663 			    type == WFD_IE_VENDOR_TYPE)
664 				continue;
665 		}
666 
667 		/* Unknown vendor element - make raw IE data available */
668 		if (wpabuf_resize(&dev->info.vendor_elems, 2 + len) < 0)
669 			break;
670 		wpabuf_put_data(dev->info.vendor_elems, pos - 2, 2 + len);
671 		if (wpabuf_size(dev->info.vendor_elems) > 2000)
672 			break;
673 	}
674 }
675 
676 
p2p_compare_wfd_info(struct p2p_device * dev,const struct p2p_message * msg)677 static int p2p_compare_wfd_info(struct p2p_device *dev,
678 			      const struct p2p_message *msg)
679 {
680 	if (dev->info.wfd_subelems && msg->wfd_subelems) {
681 		if (dev->info.wfd_subelems->used != msg->wfd_subelems->used)
682 			return 1;
683 
684 		return os_memcmp(dev->info.wfd_subelems->buf,
685 				 msg->wfd_subelems->buf,
686 				 dev->info.wfd_subelems->used);
687 	}
688 	if (dev->info.wfd_subelems || msg->wfd_subelems)
689 		return 1;
690 
691 	return 0;
692 }
693 
694 
695 /**
696  * p2p_add_device - Add peer entries based on scan results or P2P frames
697  * @p2p: P2P module context from p2p_init()
698  * @addr: Source address of Beacon or Probe Response frame (may be either
699  *	P2P Device Address or P2P Interface Address)
700  * @level: Signal level (signal strength of the received frame from the peer)
701  * @freq: Frequency on which the Beacon or Probe Response frame was received
702  * @rx_time: Time when the result was received
703  * @ies: IEs from the Beacon or Probe Response frame
704  * @ies_len: Length of ies buffer in octets
705  * @scan_res: Whether this was based on scan results
706  * Returns: 0 on success, -1 on failure
707  *
708  * If the scan result is for a GO, the clients in the group will also be added
709  * to the peer table. This function can also be used with some other frames
710  * like Provision Discovery Request that contains P2P Capability and P2P Device
711  * Info attributes.
712  */
p2p_add_device(struct p2p_data * p2p,const u8 * addr,int freq,struct os_reltime * rx_time,int level,const u8 * ies,size_t ies_len,int scan_res)713 int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
714 		   struct os_reltime *rx_time, int level, const u8 *ies,
715 		   size_t ies_len, int scan_res)
716 {
717 	struct p2p_device *dev;
718 	struct p2p_message msg;
719 	const u8 *p2p_dev_addr;
720 	int wfd_changed;
721 	int dev_name_changed;
722 	int i;
723 	struct os_reltime time_now;
724 
725 	os_memset(&msg, 0, sizeof(msg));
726 	if (p2p_parse_ies(ies, ies_len, &msg)) {
727 		p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
728 		p2p_parse_free(&msg);
729 		return -1;
730 	}
731 
732 	if (msg.p2p_device_addr)
733 		p2p_dev_addr = msg.p2p_device_addr;
734 	else if (msg.device_id)
735 		p2p_dev_addr = msg.device_id;
736 	else {
737 		p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
738 		p2p_parse_free(&msg);
739 		return -1;
740 	}
741 
742 	if (!is_zero_ether_addr(p2p->peer_filter) &&
743 	    os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
744 		p2p_dbg(p2p, "Do not add peer filter for " MACSTR
745 			" due to peer filter", MAC2STR(p2p_dev_addr));
746 		p2p_parse_free(&msg);
747 		return 0;
748 	}
749 
750 	dev = p2p_create_device(p2p, p2p_dev_addr);
751 	if (dev == NULL) {
752 		p2p_parse_free(&msg);
753 		return -1;
754 	}
755 
756 	if (rx_time == NULL) {
757 		os_get_reltime(&time_now);
758 		rx_time = &time_now;
759 	}
760 
761 	/*
762 	 * Update the device entry only if the new peer
763 	 * entry is newer than the one previously stored, or if
764 	 * the device was previously seen as a P2P Client in a group
765 	 * and the new entry isn't older than a threshold.
766 	 */
767 	if (dev->last_seen.sec > 0 &&
768 	    os_reltime_before(rx_time, &dev->last_seen) &&
769 	    (!(dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT) ||
770 	     os_reltime_expired(&dev->last_seen, rx_time,
771 				P2P_DEV_GROUP_CLIENT_RESP_THRESHOLD))) {
772 		p2p_dbg(p2p,
773 			"Do not update peer entry based on old frame (rx_time=%u.%06u last_seen=%u.%06u flags=0x%x)",
774 			(unsigned int) rx_time->sec,
775 			(unsigned int) rx_time->usec,
776 			(unsigned int) dev->last_seen.sec,
777 			(unsigned int) dev->last_seen.usec,
778 			dev->flags);
779 		p2p_parse_free(&msg);
780 		return -1;
781 	}
782 
783 	os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
784 
785 	dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY |
786 			P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT);
787 
788 	if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
789 		os_memcpy(dev->interface_addr, addr, ETH_ALEN);
790 	if (msg.ssid &&
791 	    msg.ssid[1] <= sizeof(dev->oper_ssid) &&
792 	    (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
793 	     os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
794 	     != 0)) {
795 		os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
796 		dev->oper_ssid_len = msg.ssid[1];
797 	}
798 
799 	wpabuf_free(dev->info.p2ps_instance);
800 	dev->info.p2ps_instance = NULL;
801 	if (msg.adv_service_instance && msg.adv_service_instance_len)
802 		dev->info.p2ps_instance = wpabuf_alloc_copy(
803 			msg.adv_service_instance, msg.adv_service_instance_len);
804 
805 	if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
806 	    *msg.ds_params >= 1 && *msg.ds_params <= 14) {
807 		int ds_freq;
808 		if (*msg.ds_params == 14)
809 			ds_freq = 2484;
810 		else
811 			ds_freq = 2407 + *msg.ds_params * 5;
812 		if (freq != ds_freq) {
813 			p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
814 				freq, ds_freq);
815 			freq = ds_freq;
816 		}
817 	}
818 
819 	if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
820 		p2p_dbg(p2p, "Update Listen frequency based on scan results ("
821 			MACSTR " %d -> %d MHz (DS param %d)",
822 			MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
823 			freq, msg.ds_params ? *msg.ds_params : -1);
824 	}
825 	if (scan_res) {
826 		dev->listen_freq = freq;
827 		if (msg.group_info)
828 			dev->oper_freq = freq;
829 	}
830 	dev->info.level = level;
831 
832 	dev_name_changed = os_strncmp(dev->info.device_name, msg.device_name,
833 				      WPS_DEV_NAME_MAX_LEN) != 0;
834 
835 	p2p_copy_wps_info(p2p, dev, 0, &msg);
836 
837 	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
838 		wpabuf_free(dev->info.wps_vendor_ext[i]);
839 		dev->info.wps_vendor_ext[i] = NULL;
840 	}
841 
842 	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
843 		if (msg.wps_vendor_ext[i] == NULL)
844 			break;
845 		dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
846 			msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
847 		if (dev->info.wps_vendor_ext[i] == NULL)
848 			break;
849 	}
850 
851 	wfd_changed = p2p_compare_wfd_info(dev, &msg);
852 
853 	if (wfd_changed) {
854 		wpabuf_free(dev->info.wfd_subelems);
855 		if (msg.wfd_subelems)
856 			dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
857 		else
858 			dev->info.wfd_subelems = NULL;
859 	}
860 
861 	if (scan_res) {
862 		p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
863 				      msg.group_info, msg.group_info_len,
864 				      rx_time);
865 	}
866 
867 	p2p_parse_free(&msg);
868 
869 	p2p_update_peer_vendor_elems(dev, ies, ies_len);
870 
871 	if (dev->flags & P2P_DEV_REPORTED && !wfd_changed &&
872 	    !dev_name_changed &&
873 	    (!msg.adv_service_instance ||
874 	     (dev->flags & P2P_DEV_P2PS_REPORTED)))
875 		return 0;
876 
877 	p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
878 		freq, (unsigned int) rx_time->sec,
879 		(unsigned int) rx_time->usec);
880 	if (dev->flags & P2P_DEV_USER_REJECTED) {
881 		p2p_dbg(p2p, "Do not report rejected device");
882 		return 0;
883 	}
884 
885 	if (dev->info.config_methods == 0 &&
886 	    (freq == 2412 || freq == 2437 || freq == 2462)) {
887 		/*
888 		 * If we have only seen a Beacon frame from a GO, we do not yet
889 		 * know what WPS config methods it supports. Since some
890 		 * applications use config_methods value from P2P-DEVICE-FOUND
891 		 * events, postpone reporting this peer until we've fully
892 		 * discovered its capabilities.
893 		 *
894 		 * At least for now, do this only if the peer was detected on
895 		 * one of the social channels since that peer can be easily be
896 		 * found again and there are no limitations of having to use
897 		 * passive scan on this channels, so this can be done through
898 		 * Probe Response frame that includes the config_methods
899 		 * information.
900 		 */
901 		p2p_dbg(p2p, "Do not report peer " MACSTR
902 			" with unknown config methods", MAC2STR(addr));
903 		return 0;
904 	}
905 
906 	p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
907 			    !(dev->flags & P2P_DEV_REPORTED_ONCE));
908 	dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
909 
910 	if (msg.adv_service_instance)
911 		dev->flags |= P2P_DEV_P2PS_REPORTED;
912 
913 	return 0;
914 }
915 
916 
p2p_device_free(struct p2p_data * p2p,struct p2p_device * dev)917 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
918 {
919 	int i;
920 
921 	if (p2p->go_neg_peer == dev) {
922 		/*
923 		 * If GO Negotiation is in progress, report that it has failed.
924 		 */
925 		p2p_go_neg_failed(p2p, -1);
926 	}
927 	if (p2p->invite_peer == dev)
928 		p2p->invite_peer = NULL;
929 	if (p2p->sd_peer == dev)
930 		p2p->sd_peer = NULL;
931 	if (p2p->pending_client_disc_go == dev)
932 		p2p->pending_client_disc_go = NULL;
933 
934 	/* dev_lost() device, but only if it was previously dev_found() */
935 	if (dev->flags & P2P_DEV_REPORTED_ONCE)
936 		p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
937 				   dev->info.p2p_device_addr);
938 
939 	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
940 		wpabuf_free(dev->info.wps_vendor_ext[i]);
941 		dev->info.wps_vendor_ext[i] = NULL;
942 	}
943 
944 	wpabuf_free(dev->info.wfd_subelems);
945 	wpabuf_free(dev->info.vendor_elems);
946 	wpabuf_free(dev->go_neg_conf);
947 	wpabuf_free(dev->info.p2ps_instance);
948 
949 	os_free(dev);
950 }
951 
952 
p2p_get_next_prog_freq(struct p2p_data * p2p)953 static int p2p_get_next_prog_freq(struct p2p_data *p2p)
954 {
955 	struct p2p_channels *c;
956 	struct p2p_reg_class *cla;
957 	size_t cl, ch;
958 	int found = 0;
959 	u8 reg_class;
960 	u8 channel;
961 	int freq;
962 
963 	c = &p2p->cfg->channels;
964 	for (cl = 0; cl < c->reg_classes; cl++) {
965 		cla = &c->reg_class[cl];
966 		if (cla->reg_class != p2p->last_prog_scan_class)
967 			continue;
968 		for (ch = 0; ch < cla->channels; ch++) {
969 			if (cla->channel[ch] == p2p->last_prog_scan_chan) {
970 				found = 1;
971 				break;
972 			}
973 		}
974 		if (found)
975 			break;
976 	}
977 
978 	if (!found) {
979 		/* Start from beginning */
980 		reg_class = c->reg_class[0].reg_class;
981 		channel = c->reg_class[0].channel[0];
982 	} else {
983 		/* Pick the next channel */
984 		ch++;
985 		if (ch == cla->channels) {
986 			cl++;
987 			if (cl == c->reg_classes)
988 				cl = 0;
989 			ch = 0;
990 		}
991 		reg_class = c->reg_class[cl].reg_class;
992 		channel = c->reg_class[cl].channel[ch];
993 	}
994 
995 	freq = p2p_channel_to_freq(reg_class, channel);
996 	p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
997 		reg_class, channel, freq);
998 	p2p->last_prog_scan_class = reg_class;
999 	p2p->last_prog_scan_chan = channel;
1000 
1001 	if (freq == 2412 || freq == 2437 || freq == 2462)
1002 		return 0; /* No need to add social channels */
1003 	return freq;
1004 }
1005 
1006 
p2p_search(struct p2p_data * p2p)1007 static void p2p_search(struct p2p_data *p2p)
1008 {
1009 	int freq = 0;
1010 	enum p2p_scan_type type;
1011 	u16 pw_id = DEV_PW_DEFAULT;
1012 	int res;
1013 
1014 	if (p2p->drv_in_listen) {
1015 		p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
1016 		return;
1017 	}
1018 	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1019 
1020 	if (p2p->find_pending_full &&
1021 	    (p2p->find_type == P2P_FIND_PROGRESSIVE ||
1022 	     p2p->find_type == P2P_FIND_START_WITH_FULL)) {
1023 		type = P2P_SCAN_FULL;
1024 		p2p_dbg(p2p, "Starting search (pending full scan)");
1025 		p2p->find_pending_full = 0;
1026 	} else if ((p2p->find_type == P2P_FIND_PROGRESSIVE &&
1027 	    (freq = p2p_get_next_prog_freq(p2p)) > 0) ||
1028 	    (p2p->find_type == P2P_FIND_START_WITH_FULL &&
1029 	     (freq = p2p->find_specified_freq) > 0)) {
1030 		type = P2P_SCAN_SOCIAL_PLUS_ONE;
1031 		p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
1032 	} else {
1033 		type = P2P_SCAN_SOCIAL;
1034 		p2p_dbg(p2p, "Starting search");
1035 	}
1036 
1037 	res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
1038 				 p2p->num_req_dev_types, p2p->req_dev_types,
1039 				 p2p->find_dev_id, pw_id);
1040 	if (res < 0) {
1041 		p2p_dbg(p2p, "Scan request schedule failed");
1042 		p2p_continue_find(p2p);
1043 	}
1044 }
1045 
1046 
p2p_find_timeout(void * eloop_ctx,void * timeout_ctx)1047 static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
1048 {
1049 	struct p2p_data *p2p = eloop_ctx;
1050 	p2p_dbg(p2p, "Find timeout -> stop");
1051 	p2p_stop_find(p2p);
1052 }
1053 
1054 
p2p_notify_scan_trigger_status(struct p2p_data * p2p,int status)1055 void p2p_notify_scan_trigger_status(struct p2p_data *p2p, int status)
1056 {
1057 	if (status != 0) {
1058 		p2p_dbg(p2p, "Scan request failed");
1059 		/* Do continue find even for the first p2p_find_scan */
1060 		p2p_continue_find(p2p);
1061 	} else {
1062 		p2p_dbg(p2p, "Running p2p_scan");
1063 		p2p->p2p_scan_running = 1;
1064 		eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1065 		eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1066 				       p2p, NULL);
1067 	}
1068 }
1069 
1070 
p2p_run_after_scan(struct p2p_data * p2p)1071 static int p2p_run_after_scan(struct p2p_data *p2p)
1072 {
1073 	struct p2p_device *dev;
1074 	enum p2p_after_scan op;
1075 
1076 	op = p2p->start_after_scan;
1077 	p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1078 	switch (op) {
1079 	case P2P_AFTER_SCAN_NOTHING:
1080 		break;
1081 	case P2P_AFTER_SCAN_LISTEN:
1082 		p2p_dbg(p2p, "Start previously requested Listen state");
1083 		p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
1084 			   p2p->pending_listen_usec / 1000);
1085 		return 1;
1086 	case P2P_AFTER_SCAN_CONNECT:
1087 		p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
1088 			MAC2STR(p2p->after_scan_peer));
1089 		dev = p2p_get_device(p2p, p2p->after_scan_peer);
1090 		if (dev == NULL) {
1091 			p2p_dbg(p2p, "Peer not known anymore");
1092 			break;
1093 		}
1094 		p2p_connect_send(p2p, dev);
1095 		return 1;
1096 	}
1097 
1098 	return 0;
1099 }
1100 
1101 
p2p_scan_timeout(void * eloop_ctx,void * timeout_ctx)1102 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1103 {
1104 	struct p2p_data *p2p = eloop_ctx;
1105 	int running;
1106 	p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
1107 	running = p2p->p2p_scan_running;
1108 	/* Make sure we recover from missed scan results callback */
1109 	p2p->p2p_scan_running = 0;
1110 
1111 	if (running)
1112 		p2p_run_after_scan(p2p);
1113 }
1114 
1115 
p2p_free_req_dev_types(struct p2p_data * p2p)1116 static void p2p_free_req_dev_types(struct p2p_data *p2p)
1117 {
1118 	p2p->num_req_dev_types = 0;
1119 	os_free(p2p->req_dev_types);
1120 	p2p->req_dev_types = NULL;
1121 }
1122 
1123 
p2ps_gen_hash(struct p2p_data * p2p,const char * str,u8 * hash)1124 static int p2ps_gen_hash(struct p2p_data *p2p, const char *str, u8 *hash)
1125 {
1126 	u8 buf[SHA256_MAC_LEN];
1127 	char str_buf[256];
1128 	const u8 *adv_array;
1129 	size_t i, adv_len;
1130 
1131 	if (!str || !hash)
1132 		return 0;
1133 
1134 	if (!str[0]) {
1135 		os_memcpy(hash, p2p->wild_card_hash, P2PS_HASH_LEN);
1136 		return 1;
1137 	}
1138 
1139 	adv_array = (u8 *) str_buf;
1140 	adv_len = os_strlen(str);
1141 	if (adv_len >= sizeof(str_buf))
1142 		return 0;
1143 
1144 	for (i = 0; i < adv_len; i++) {
1145 		if (str[i] >= 'A' && str[i] <= 'Z')
1146 			str_buf[i] = str[i] - 'A' + 'a';
1147 		else
1148 			str_buf[i] = str[i];
1149 	}
1150 
1151 	if (sha256_vector(1, &adv_array, &adv_len, buf))
1152 		return 0;
1153 
1154 	os_memcpy(hash, buf, P2PS_HASH_LEN);
1155 	return 1;
1156 }
1157 
1158 
p2p_find(struct p2p_data * p2p,unsigned int timeout,enum p2p_discovery_type type,unsigned int num_req_dev_types,const u8 * req_dev_types,const u8 * dev_id,unsigned int search_delay,u8 seek_count,const char ** seek,int freq)1159 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
1160 	     enum p2p_discovery_type type,
1161 	     unsigned int num_req_dev_types, const u8 *req_dev_types,
1162 	     const u8 *dev_id, unsigned int search_delay,
1163 	     u8 seek_count, const char **seek, int freq)
1164 {
1165 	int res;
1166 	struct os_reltime start;
1167 
1168 	p2p_dbg(p2p, "Starting find (type=%d)", type);
1169 	if (p2p->p2p_scan_running) {
1170 		p2p_dbg(p2p, "p2p_scan is already running");
1171 	}
1172 
1173 	p2p_free_req_dev_types(p2p);
1174 	if (req_dev_types && num_req_dev_types) {
1175 		p2p->req_dev_types = os_memdup(req_dev_types,
1176 					       num_req_dev_types *
1177 					       WPS_DEV_TYPE_LEN);
1178 		if (p2p->req_dev_types == NULL)
1179 			return -1;
1180 		p2p->num_req_dev_types = num_req_dev_types;
1181 	}
1182 
1183 	if (dev_id) {
1184 		os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1185 		p2p->find_dev_id = p2p->find_dev_id_buf;
1186 	} else
1187 		p2p->find_dev_id = NULL;
1188 
1189 	if (seek_count == 0 || !seek) {
1190 		/* Not an ASP search */
1191 		p2p->p2ps_seek = 0;
1192 	} else if (seek_count == 1 && seek && (!seek[0] || !seek[0][0])) {
1193 		/*
1194 		 * An empty seek string means no hash values, but still an ASP
1195 		 * search.
1196 		 */
1197 		p2p_dbg(p2p, "ASP search");
1198 		p2p->p2ps_seek_count = 0;
1199 		p2p->p2ps_seek = 1;
1200 	} else if (seek && seek_count <= P2P_MAX_QUERY_HASH) {
1201 		u8 buf[P2PS_HASH_LEN];
1202 		int i, count = 0;
1203 
1204 		for (i = 0; i < seek_count; i++) {
1205 			if (!p2ps_gen_hash(p2p, seek[i], buf))
1206 				continue;
1207 
1208 			p2p_dbg(p2p, "Seek service %s hash " MACSTR,
1209 				seek[i], MAC2STR(buf));
1210 			os_memcpy(&p2p->p2ps_seek_hash[count * P2PS_HASH_LEN],
1211 				  buf, P2PS_HASH_LEN);
1212 			count++;
1213 		}
1214 
1215 		p2p->p2ps_seek_count = count;
1216 		p2p->p2ps_seek = 1;
1217 	} else {
1218 		p2p->p2ps_seek_count = 0;
1219 		p2p->p2ps_seek = 1;
1220 	}
1221 
1222 	/* Special case to perform wildcard search */
1223 	if (p2p->p2ps_seek_count == 0 && p2p->p2ps_seek) {
1224 		p2p->p2ps_seek_count = 1;
1225 		os_memcpy(&p2p->p2ps_seek_hash, p2p->wild_card_hash,
1226 			  P2PS_HASH_LEN);
1227 	}
1228 
1229 	p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1230 	p2p_clear_timeout(p2p);
1231 	if (p2p->pending_listen_freq) {
1232 		p2p_dbg(p2p, "Clear pending_listen_freq for p2p_find");
1233 		p2p->pending_listen_freq = 0;
1234 	}
1235 	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1236 	p2p->find_pending_full = 0;
1237 	p2p->find_type = type;
1238 	if (freq != 2412 && freq != 2437 && freq != 2462 && freq != 60480)
1239 		p2p->find_specified_freq = freq;
1240 	else
1241 		p2p->find_specified_freq = 0;
1242 	p2p_device_clear_reported(p2p);
1243 	os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
1244 	p2p_set_state(p2p, P2P_SEARCH);
1245 	p2p->search_delay = search_delay;
1246 	p2p->in_search_delay = 0;
1247 	eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1248 	p2p->last_p2p_find_timeout = timeout;
1249 	if (timeout)
1250 		eloop_register_timeout(timeout, 0, p2p_find_timeout,
1251 				       p2p, NULL);
1252 	os_get_reltime(&start);
1253 	switch (type) {
1254 	case P2P_FIND_START_WITH_FULL:
1255 		if (freq > 0) {
1256 			/*
1257 			 * Start with the specified channel and then move to
1258 			 * scans for social channels and this specific channel.
1259 			 */
1260 			res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx,
1261 						 P2P_SCAN_SPECIFIC, freq,
1262 						 p2p->num_req_dev_types,
1263 						 p2p->req_dev_types, dev_id,
1264 						 DEV_PW_DEFAULT);
1265 			break;
1266 		}
1267 		/* fall through */
1268 	case P2P_FIND_PROGRESSIVE:
1269 		res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1270 					 p2p->num_req_dev_types,
1271 					 p2p->req_dev_types, dev_id,
1272 					 DEV_PW_DEFAULT);
1273 		break;
1274 	case P2P_FIND_ONLY_SOCIAL:
1275 		res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1276 					 p2p->num_req_dev_types,
1277 					 p2p->req_dev_types, dev_id,
1278 					 DEV_PW_DEFAULT);
1279 		break;
1280 	default:
1281 		return -1;
1282 	}
1283 
1284 	if (!res)
1285 		p2p->find_start = start;
1286 
1287 	if (res != 0 && p2p->p2p_scan_running) {
1288 		p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running");
1289 		/* wait for the previous p2p_scan to complete */
1290 		if (type == P2P_FIND_PROGRESSIVE ||
1291 		    (type == P2P_FIND_START_WITH_FULL && freq == 0))
1292 			p2p->find_pending_full = 1;
1293 		res = 0; /* do not report failure */
1294 	} else if (res != 0) {
1295 		p2p_dbg(p2p, "Failed to start p2p_scan");
1296 		p2p_set_state(p2p, P2P_IDLE);
1297 		eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1298 	}
1299 
1300 	return res;
1301 }
1302 
1303 
p2p_stop_find_for_freq(struct p2p_data * p2p,int freq)1304 void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1305 {
1306 	p2p_dbg(p2p, "Stopping find");
1307 	eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1308 	p2p_clear_timeout(p2p);
1309 	if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
1310 		p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
1311 
1312 	p2p->p2ps_seek_count = 0;
1313 
1314 	p2p_set_state(p2p, P2P_IDLE);
1315 	p2p_free_req_dev_types(p2p);
1316 	p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1317 	if (p2p->go_neg_peer)
1318 		p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
1319 	p2p->go_neg_peer = NULL;
1320 	p2p->sd_peer = NULL;
1321 	p2p->invite_peer = NULL;
1322 	p2p_stop_listen_for_freq(p2p, freq);
1323 	p2p->send_action_in_progress = 0;
1324 }
1325 
1326 
p2p_stop_listen_for_freq(struct p2p_data * p2p,int freq)1327 void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1328 {
1329 	if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
1330 		p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
1331 		return;
1332 	}
1333 	if (p2p->in_listen) {
1334 		p2p->in_listen = 0;
1335 		p2p_clear_timeout(p2p);
1336 	}
1337 	if (p2p->drv_in_listen) {
1338 		/*
1339 		 * The driver may not deliver callback to p2p_listen_end()
1340 		 * when the operation gets canceled, so clear the internal
1341 		 * variable that is tracking driver state.
1342 		 */
1343 		p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
1344 		p2p->drv_in_listen = 0;
1345 	}
1346 	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1347 }
1348 
1349 
p2p_stop_listen(struct p2p_data * p2p)1350 void p2p_stop_listen(struct p2p_data *p2p)
1351 {
1352 	if (p2p->state != P2P_LISTEN_ONLY) {
1353 		p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
1354 		return;
1355 	}
1356 
1357 	p2p_stop_listen_for_freq(p2p, 0);
1358 	p2p_set_state(p2p, P2P_IDLE);
1359 }
1360 
1361 
p2p_stop_find(struct p2p_data * p2p)1362 void p2p_stop_find(struct p2p_data *p2p)
1363 {
1364 	p2p->pending_listen_freq = 0;
1365 	p2p_stop_find_for_freq(p2p, 0);
1366 }
1367 
1368 
p2p_prepare_channel_pref(struct p2p_data * p2p,unsigned int force_freq,unsigned int pref_freq,int go)1369 static int p2p_prepare_channel_pref(struct p2p_data *p2p,
1370 				    unsigned int force_freq,
1371 				    unsigned int pref_freq, int go)
1372 {
1373 	u8 op_class, op_channel;
1374 	unsigned int freq = force_freq ? force_freq : pref_freq;
1375 
1376 	p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
1377 		force_freq, pref_freq, go);
1378 	if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
1379 		p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
1380 		return -1;
1381 	}
1382 
1383 	if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
1384 	    (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
1385 					  op_channel))) {
1386 		p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
1387 			freq, op_class, op_channel);
1388 		return -1;
1389 	}
1390 
1391 	p2p->op_reg_class = op_class;
1392 	p2p->op_channel = op_channel;
1393 
1394 	if (force_freq) {
1395 		p2p->channels.reg_classes = 1;
1396 		p2p->channels.reg_class[0].channels = 1;
1397 		p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1398 		p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
1399 	} else {
1400 		os_memcpy(&p2p->channels, &p2p->cfg->channels,
1401 			  sizeof(struct p2p_channels));
1402 	}
1403 
1404 	return 0;
1405 }
1406 
1407 
p2p_prepare_channel_best(struct p2p_data * p2p)1408 static void p2p_prepare_channel_best(struct p2p_data *p2p)
1409 {
1410 	u8 op_class, op_channel;
1411 	const int op_classes_5ghz[] = { 124, 125, 115, 0 };
1412 	const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
1413 	const int op_classes_vht[] = { 128, 0 };
1414 	const int op_classes_edmg[] = { 181, 182, 183, 0 };
1415 
1416 	p2p_dbg(p2p, "Prepare channel best");
1417 
1418 	if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1419 	    p2p_supported_freq(p2p, p2p->best_freq_overall) &&
1420 	    p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
1421 	    == 0) {
1422 		p2p_dbg(p2p, "Select best overall channel as operating channel preference");
1423 		p2p->op_reg_class = op_class;
1424 		p2p->op_channel = op_channel;
1425 	} else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1426 		   p2p_supported_freq(p2p, p2p->best_freq_5) &&
1427 		   p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
1428 		   == 0) {
1429 		p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
1430 		p2p->op_reg_class = op_class;
1431 		p2p->op_channel = op_channel;
1432 	} else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
1433 		   p2p_supported_freq(p2p, p2p->best_freq_24) &&
1434 		   p2p_freq_to_channel(p2p->best_freq_24, &op_class,
1435 				       &op_channel) == 0) {
1436 		p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
1437 		p2p->op_reg_class = op_class;
1438 		p2p->op_channel = op_channel;
1439 	} else if (p2p->cfg->num_pref_chan > 0 &&
1440 		   p2p_channels_includes(&p2p->cfg->channels,
1441 					 p2p->cfg->pref_chan[0].op_class,
1442 					 p2p->cfg->pref_chan[0].chan)) {
1443 		p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
1444 		p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
1445 		p2p->op_channel = p2p->cfg->pref_chan[0].chan;
1446 	} else if (p2p_channel_select(&p2p->cfg->channels, op_classes_edmg,
1447 				      &p2p->op_reg_class, &p2p->op_channel) ==
1448 		   0) {
1449 		p2p_dbg(p2p, "Select possible EDMG channel (op_class %u channel %u) as operating channel preference",
1450 			p2p->op_reg_class, p2p->op_channel);
1451 	} else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
1452 				      &p2p->op_reg_class, &p2p->op_channel) ==
1453 		   0) {
1454 		p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference",
1455 			p2p->op_reg_class, p2p->op_channel);
1456 	} else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40,
1457 				      &p2p->op_reg_class, &p2p->op_channel) ==
1458 		   0) {
1459 		p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
1460 			p2p->op_reg_class, p2p->op_channel);
1461 	} else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
1462 				      &p2p->op_reg_class, &p2p->op_channel) ==
1463 		   0) {
1464 		p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
1465 			p2p->op_reg_class, p2p->op_channel);
1466 	} else if (p2p_channels_includes(&p2p->cfg->channels,
1467 					 p2p->cfg->op_reg_class,
1468 					 p2p->cfg->op_channel)) {
1469 		p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
1470 		p2p->op_reg_class = p2p->cfg->op_reg_class;
1471 		p2p->op_channel = p2p->cfg->op_channel;
1472 	} else if (p2p_channel_random_social(&p2p->cfg->channels,
1473 					     &p2p->op_reg_class,
1474 					     &p2p->op_channel,
1475 					     NULL, NULL) == 0) {
1476 		p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference",
1477 			p2p->op_reg_class, p2p->op_channel);
1478 	} else {
1479 		/* Select any random available channel from the first available
1480 		 * operating class */
1481 		p2p_channel_select(&p2p->cfg->channels, NULL,
1482 				   &p2p->op_reg_class,
1483 				   &p2p->op_channel);
1484 		p2p_dbg(p2p, "Select random available channel %d from operating class %d as operating channel preference",
1485 			p2p->op_channel, p2p->op_reg_class);
1486 	}
1487 
1488 	os_memcpy(&p2p->channels, &p2p->cfg->channels,
1489 		  sizeof(struct p2p_channels));
1490 }
1491 
1492 
1493 /**
1494  * p2p_prepare_channel - Select operating channel for GO Negotiation or P2PS PD
1495  * @p2p: P2P module context from p2p_init()
1496  * @dev: Selected peer device
1497  * @force_freq: Forced frequency in MHz or 0 if not forced
1498  * @pref_freq: Preferred frequency in MHz or 0 if no preference
1499  * @go: Whether the local end will be forced to be GO
1500  * Returns: 0 on success, -1 on failure (channel not supported for P2P)
1501  *
1502  * This function is used to do initial operating channel selection for GO
1503  * Negotiation prior to having received peer information or for P2PS PD
1504  * signalling. The selected channel may be further optimized in
1505  * p2p_reselect_channel() once the peer information is available.
1506  */
p2p_prepare_channel(struct p2p_data * p2p,struct p2p_device * dev,unsigned int force_freq,unsigned int pref_freq,int go)1507 int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
1508 			unsigned int force_freq, unsigned int pref_freq, int go)
1509 {
1510 	p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
1511 		force_freq, pref_freq, go);
1512 	if (force_freq || pref_freq) {
1513 		if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
1514 		    0)
1515 			return -1;
1516 	} else {
1517 		p2p_prepare_channel_best(p2p);
1518 	}
1519 	p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
1520 	if (go)
1521 		p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
1522 	else if (!force_freq)
1523 		p2p_channels_union_inplace(&p2p->channels,
1524 					   &p2p->cfg->cli_channels);
1525 	p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
1526 
1527 	p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
1528 		p2p->op_reg_class, p2p->op_channel,
1529 		force_freq ? " (forced)" : "");
1530 
1531 	if (force_freq)
1532 		dev->flags |= P2P_DEV_FORCE_FREQ;
1533 	else
1534 		dev->flags &= ~P2P_DEV_FORCE_FREQ;
1535 
1536 	return 0;
1537 }
1538 
1539 
p2p_set_dev_persistent(struct p2p_device * dev,int persistent_group)1540 static void p2p_set_dev_persistent(struct p2p_device *dev,
1541 				   int persistent_group)
1542 {
1543 	switch (persistent_group) {
1544 	case 0:
1545 		dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1546 				P2P_DEV_PREFER_PERSISTENT_RECONN);
1547 		break;
1548 	case 1:
1549 		dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1550 		dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1551 		break;
1552 	case 2:
1553 		dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1554 			P2P_DEV_PREFER_PERSISTENT_RECONN;
1555 		break;
1556 	}
1557 }
1558 
1559 
p2p_connect(struct p2p_data * p2p,const u8 * peer_addr,enum p2p_wps_method wps_method,int go_intent,const u8 * own_interface_addr,unsigned int force_freq,int persistent_group,const u8 * force_ssid,size_t force_ssid_len,int pd_before_go_neg,unsigned int pref_freq,u16 oob_pw_id)1560 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1561 		enum p2p_wps_method wps_method,
1562 		int go_intent, const u8 *own_interface_addr,
1563 		unsigned int force_freq, int persistent_group,
1564 		const u8 *force_ssid, size_t force_ssid_len,
1565 		int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id)
1566 {
1567 	struct p2p_device *dev;
1568 
1569 	p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
1570 		"  GO Intent=%d  Intended Interface Address=" MACSTR
1571 		" wps_method=%d persistent_group=%d pd_before_go_neg=%d "
1572 		"oob_pw_id=%u",
1573 		MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1574 		wps_method, persistent_group, pd_before_go_neg, oob_pw_id);
1575 
1576 	dev = p2p_get_device(p2p, peer_addr);
1577 	if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1578 		p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
1579 			MAC2STR(peer_addr));
1580 		return -1;
1581 	}
1582 
1583 	if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
1584 				go_intent == 15) < 0)
1585 		return -1;
1586 
1587 	if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1588 		if (!(dev->info.dev_capab &
1589 		      P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1590 			p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
1591 				" that is in a group and is not discoverable",
1592 				MAC2STR(peer_addr));
1593 			return -1;
1594 		}
1595 		if (dev->oper_freq <= 0) {
1596 			p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
1597 				" with incomplete information",
1598 				MAC2STR(peer_addr));
1599 			return -1;
1600 		}
1601 
1602 		/*
1603 		 * First, try to connect directly. If the peer does not
1604 		 * acknowledge frames, assume it is sleeping and use device
1605 		 * discoverability via the GO at that point.
1606 		 */
1607 	}
1608 
1609 	p2p->ssid_set = 0;
1610 	if (force_ssid) {
1611 		wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1612 				  force_ssid, force_ssid_len);
1613 		os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1614 		p2p->ssid_len = force_ssid_len;
1615 		p2p->ssid_set = 1;
1616 	}
1617 
1618 	dev->flags &= ~P2P_DEV_NOT_YET_READY;
1619 	dev->flags &= ~P2P_DEV_USER_REJECTED;
1620 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1621 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1622 	if (pd_before_go_neg)
1623 		dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
1624 	else {
1625 		dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
1626 		/*
1627 		 * Assign dialog token and tie breaker here to use the same
1628 		 * values in each retry within the same GO Negotiation exchange.
1629 		 */
1630 		dev->dialog_token++;
1631 		if (dev->dialog_token == 0)
1632 			dev->dialog_token = 1;
1633 		dev->tie_breaker = p2p->next_tie_breaker;
1634 		p2p->next_tie_breaker = !p2p->next_tie_breaker;
1635 	}
1636 	dev->connect_reqs = 0;
1637 	dev->go_neg_req_sent = 0;
1638 	dev->go_state = UNKNOWN_GO;
1639 	p2p_set_dev_persistent(dev, persistent_group);
1640 	p2p->go_intent = go_intent;
1641 	os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1642 
1643 	if (p2p->state != P2P_IDLE)
1644 		p2p_stop_find(p2p);
1645 
1646 	dev->wps_method = wps_method;
1647 	dev->oob_pw_id = oob_pw_id;
1648 	dev->status = P2P_SC_SUCCESS;
1649 
1650 	if (p2p->p2p_scan_running) {
1651 		p2p_dbg(p2p, "p2p_scan running - delay connect send");
1652 		p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1653 		os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1654 		return 0;
1655 	}
1656 
1657 	return p2p_connect_send(p2p, dev);
1658 }
1659 
1660 
p2p_authorize(struct p2p_data * p2p,const u8 * peer_addr,enum p2p_wps_method wps_method,int go_intent,const u8 * own_interface_addr,unsigned int force_freq,int persistent_group,const u8 * force_ssid,size_t force_ssid_len,unsigned int pref_freq,u16 oob_pw_id)1661 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1662 		  enum p2p_wps_method wps_method,
1663 		  int go_intent, const u8 *own_interface_addr,
1664 		  unsigned int force_freq, int persistent_group,
1665 		  const u8 *force_ssid, size_t force_ssid_len,
1666 		  unsigned int pref_freq, u16 oob_pw_id)
1667 {
1668 	struct p2p_device *dev;
1669 
1670 	p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
1671 		"  GO Intent=%d  Intended Interface Address=" MACSTR
1672 		" wps_method=%d  persistent_group=%d oob_pw_id=%u",
1673 		MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1674 		wps_method, persistent_group, oob_pw_id);
1675 
1676 	dev = p2p_get_device(p2p, peer_addr);
1677 	if (dev == NULL) {
1678 		p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
1679 			MAC2STR(peer_addr));
1680 		return -1;
1681 	}
1682 
1683 	if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
1684 				15) < 0)
1685 		return -1;
1686 
1687 	p2p->ssid_set = 0;
1688 	if (force_ssid) {
1689 		wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1690 				  force_ssid, force_ssid_len);
1691 		os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1692 		p2p->ssid_len = force_ssid_len;
1693 		p2p->ssid_set = 1;
1694 	}
1695 
1696 	dev->flags &= ~P2P_DEV_NOT_YET_READY;
1697 	dev->flags &= ~P2P_DEV_USER_REJECTED;
1698 	dev->go_neg_req_sent = 0;
1699 	dev->go_state = UNKNOWN_GO;
1700 	p2p_set_dev_persistent(dev, persistent_group);
1701 	p2p->go_intent = go_intent;
1702 	os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1703 
1704 	dev->wps_method = wps_method;
1705 	dev->oob_pw_id = oob_pw_id;
1706 	dev->status = P2P_SC_SUCCESS;
1707 
1708 	return 0;
1709 }
1710 
1711 
p2p_add_dev_info(struct p2p_data * p2p,const u8 * addr,struct p2p_device * dev,struct p2p_message * msg)1712 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1713 		      struct p2p_device *dev, struct p2p_message *msg)
1714 {
1715 	os_get_reltime(&dev->last_seen);
1716 
1717 	p2p_copy_wps_info(p2p, dev, 0, msg);
1718 
1719 	if (msg->listen_channel) {
1720 		int freq;
1721 		freq = p2p_channel_to_freq(msg->listen_channel[3],
1722 					   msg->listen_channel[4]);
1723 		if (freq < 0) {
1724 			p2p_dbg(p2p, "Unknown peer Listen channel: "
1725 				"country=%c%c(0x%02x) reg_class=%u channel=%u",
1726 				msg->listen_channel[0],
1727 				msg->listen_channel[1],
1728 				msg->listen_channel[2],
1729 				msg->listen_channel[3],
1730 				msg->listen_channel[4]);
1731 		} else {
1732 			p2p_dbg(p2p, "Update peer " MACSTR
1733 				" Listen channel: %u -> %u MHz",
1734 				MAC2STR(dev->info.p2p_device_addr),
1735 				dev->listen_freq, freq);
1736 			dev->listen_freq = freq;
1737 		}
1738 	}
1739 
1740 	if (msg->wfd_subelems) {
1741 		wpabuf_free(dev->info.wfd_subelems);
1742 		dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1743 	}
1744 
1745 	if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1746 		dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1747 		p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
1748 	} else {
1749 		p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
1750 			MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1751 			"listen_freq=%d",
1752 			MAC2STR(dev->info.p2p_device_addr),
1753 			dev->info.dev_capab, dev->info.group_capab,
1754 			dev->info.device_name, dev->listen_freq);
1755 	}
1756 
1757 	dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1758 
1759 	if (dev->flags & P2P_DEV_USER_REJECTED) {
1760 		p2p_dbg(p2p, "Do not report rejected device");
1761 		return;
1762 	}
1763 
1764 	p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1765 			    !(dev->flags & P2P_DEV_REPORTED_ONCE));
1766 	dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1767 }
1768 
1769 
p2p_build_ssid(struct p2p_data * p2p,u8 * ssid,size_t * ssid_len)1770 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1771 {
1772 	os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1773 	p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1774 	os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1775 		  p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1776 	*ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1777 }
1778 
1779 
p2p_go_params(struct p2p_data * p2p,struct p2p_go_neg_results * params)1780 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1781 {
1782 	if (p2p->ssid_set) {
1783 		os_memcpy(params->ssid, p2p->ssid, p2p->ssid_len);
1784 		params->ssid_len = p2p->ssid_len;
1785 	} else {
1786 		p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1787 	}
1788 	p2p->ssid_set = 0;
1789 
1790 	if (p2p->passphrase_set) {
1791 		os_memcpy(params->passphrase, p2p->passphrase, os_strlen(p2p->passphrase));
1792 	} else {
1793 		p2p_random(params->passphrase, p2p->cfg->passphrase_len);
1794 	}
1795 	p2p->passphrase_set = 0;
1796 	return 0;
1797 }
1798 
1799 
p2p_go_complete(struct p2p_data * p2p,struct p2p_device * peer)1800 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1801 {
1802 	struct p2p_go_neg_results res;
1803 	int go = peer->go_state == LOCAL_GO;
1804 	struct p2p_channels intersection;
1805 
1806 	p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
1807 		MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
1808 
1809 	os_memset(&res, 0, sizeof(res));
1810 	res.role_go = go;
1811 	os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1812 	os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1813 	res.wps_method = peer->wps_method;
1814 	if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1815 		if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1816 			res.persistent_group = 2;
1817 		else
1818 			res.persistent_group = 1;
1819 	}
1820 
1821 	if (go) {
1822 		/* Setup AP mode for WPS provisioning */
1823 		res.freq = p2p_channel_to_freq(p2p->op_reg_class,
1824 					       p2p->op_channel);
1825 		os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1826 		res.ssid_len = p2p->ssid_len;
1827 		p2p_random(res.passphrase, p2p->cfg->passphrase_len);
1828 	} else {
1829 		res.freq = peer->oper_freq;
1830 		if (p2p->ssid_len) {
1831 			os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1832 			res.ssid_len = p2p->ssid_len;
1833 		}
1834 	}
1835 
1836 	p2p_channels_dump(p2p, "own channels", &p2p->channels);
1837 	p2p_channels_dump(p2p, "peer channels", &peer->channels);
1838 	p2p_channels_intersect(&p2p->channels, &peer->channels,
1839 			       &intersection);
1840 	if (go) {
1841 		p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
1842 		p2p_channels_dump(p2p, "intersection after no-GO removal",
1843 				  &intersection);
1844 	}
1845 
1846 	p2p_channels_to_freqs(&intersection, res.freq_list,
1847 			      P2P_MAX_CHANNELS);
1848 
1849 	res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1850 
1851 	p2p_clear_timeout(p2p);
1852 	p2p->ssid_set = 0;
1853 	peer->go_neg_req_sent = 0;
1854 	peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
1855 	peer->wps_method = WPS_NOT_READY;
1856 	peer->oob_pw_id = 0;
1857 	wpabuf_free(peer->go_neg_conf);
1858 	peer->go_neg_conf = NULL;
1859 
1860 	p2p_set_state(p2p, P2P_PROVISIONING);
1861 	p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1862 }
1863 
1864 
p2p_rx_p2p_action(struct p2p_data * p2p,const u8 * sa,const u8 * data,size_t len,int rx_freq)1865 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1866 			      const u8 *data, size_t len, int rx_freq)
1867 {
1868 	p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
1869 	wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1870 
1871 	if (len < 1)
1872 		return;
1873 
1874 	switch (data[0]) {
1875 	case P2P_GO_NEG_REQ:
1876 		p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1877 		break;
1878 	case P2P_GO_NEG_RESP:
1879 		p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1880 		break;
1881 	case P2P_GO_NEG_CONF:
1882 		p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1883 		break;
1884 	case P2P_INVITATION_REQ:
1885 		p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1886 					   rx_freq);
1887 		break;
1888 	case P2P_INVITATION_RESP:
1889 		p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1890 		break;
1891 	case P2P_PROV_DISC_REQ:
1892 		p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1893 		break;
1894 	case P2P_PROV_DISC_RESP:
1895 		p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1896 		break;
1897 	case P2P_DEV_DISC_REQ:
1898 		p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1899 		break;
1900 	case P2P_DEV_DISC_RESP:
1901 		p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1902 		break;
1903 	default:
1904 		p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
1905 			data[0]);
1906 		break;
1907 	}
1908 }
1909 
1910 
p2p_rx_action_public(struct p2p_data * p2p,const u8 * da,const u8 * sa,const u8 * bssid,const u8 * data,size_t len,int freq)1911 static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1912 				 const u8 *sa, const u8 *bssid, const u8 *data,
1913 				 size_t len, int freq)
1914 {
1915 	if (len < 1)
1916 		return;
1917 
1918 	switch (data[0]) {
1919 	case WLAN_PA_VENDOR_SPECIFIC:
1920 		data++;
1921 		len--;
1922 		if (len < 4)
1923 			return;
1924 		if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
1925 			return;
1926 
1927 		data += 4;
1928 		len -= 4;
1929 
1930 		p2p_rx_p2p_action(p2p, sa, data, len, freq);
1931 		break;
1932 	case WLAN_PA_GAS_INITIAL_REQ:
1933 		p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1934 		break;
1935 	case WLAN_PA_GAS_INITIAL_RESP:
1936 		p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1937 		break;
1938 	case WLAN_PA_GAS_COMEBACK_REQ:
1939 		p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1940 		break;
1941 	case WLAN_PA_GAS_COMEBACK_RESP:
1942 		p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1943 		break;
1944 	}
1945 }
1946 
1947 
p2p_rx_action(struct p2p_data * p2p,const u8 * da,const u8 * sa,const u8 * bssid,u8 category,const u8 * data,size_t len,int freq)1948 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1949 		   const u8 *bssid, u8 category,
1950 		   const u8 *data, size_t len, int freq)
1951 {
1952 	if (category == WLAN_ACTION_PUBLIC) {
1953 		p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1954 		return;
1955 	}
1956 
1957 	if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1958 		return;
1959 
1960 	if (len < 4)
1961 		return;
1962 
1963 	if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
1964 		return;
1965 	data += 4;
1966 	len -= 4;
1967 
1968 	/* P2P action frame */
1969 	p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
1970 	wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1971 
1972 	if (len < 1)
1973 		return;
1974 	switch (data[0]) {
1975 	case P2P_NOA:
1976 		p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
1977 		/* TODO */
1978 		break;
1979 	case P2P_PRESENCE_REQ:
1980 		p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1981 		break;
1982 	case P2P_PRESENCE_RESP:
1983 		p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1984 		break;
1985 	case P2P_GO_DISC_REQ:
1986 		p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1987 		break;
1988 	default:
1989 		p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
1990 		break;
1991 	}
1992 }
1993 
1994 
p2p_go_neg_start(void * eloop_ctx,void * timeout_ctx)1995 static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1996 {
1997 	struct p2p_data *p2p = eloop_ctx;
1998 	if (p2p->go_neg_peer == NULL)
1999 		return;
2000 	if (p2p->pending_listen_freq) {
2001 		p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start");
2002 		p2p->pending_listen_freq = 0;
2003 	}
2004 	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
2005 	p2p->go_neg_peer->status = P2P_SC_SUCCESS;
2006 	/*
2007 	 * Set new timeout to make sure a previously set one does not expire
2008 	 * too quickly while waiting for the GO Negotiation to complete.
2009 	 */
2010 	p2p_set_timeout(p2p, 0, 500000);
2011 	p2p_connect_send(p2p, p2p->go_neg_peer);
2012 }
2013 
2014 
p2p_invite_start(void * eloop_ctx,void * timeout_ctx)2015 static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
2016 {
2017 	struct p2p_data *p2p = eloop_ctx;
2018 	if (p2p->invite_peer == NULL)
2019 		return;
2020 	if (p2p->pending_listen_freq) {
2021 		p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start");
2022 		p2p->pending_listen_freq = 0;
2023 	}
2024 	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
2025 	p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
2026 			p2p->invite_dev_pw_id);
2027 }
2028 
2029 
p2p_add_dev_from_probe_req(struct p2p_data * p2p,const u8 * addr,const u8 * ie,size_t ie_len)2030 static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
2031 				       const u8 *ie, size_t ie_len)
2032 {
2033 	struct p2p_message msg;
2034 	struct p2p_device *dev;
2035 
2036 	os_memset(&msg, 0, sizeof(msg));
2037 	if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
2038 	{
2039 		p2p_parse_free(&msg);
2040 		return; /* not a P2P probe */
2041 	}
2042 
2043 	if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
2044 	    os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
2045 	    != 0) {
2046 		/* The Probe Request is not part of P2P Device Discovery. It is
2047 		 * not known whether the source address of the frame is the P2P
2048 		 * Device Address or P2P Interface Address. Do not add a new
2049 		 * peer entry based on this frames.
2050 		 */
2051 		p2p_parse_free(&msg);
2052 		return;
2053 	}
2054 
2055 	dev = p2p_get_device(p2p, addr);
2056 	if (dev) {
2057 		if (msg.listen_channel) {
2058 			int freq;
2059 
2060 			if (dev->country[0] == 0)
2061 				os_memcpy(dev->country, msg.listen_channel, 3);
2062 
2063 			freq = p2p_channel_to_freq(msg.listen_channel[3],
2064 						   msg.listen_channel[4]);
2065 
2066 			if (freq > 0 && dev->listen_freq != freq) {
2067 				p2p_dbg(p2p,
2068 					"Updated peer " MACSTR " Listen channel (Probe Request): %d -> %d MHz",
2069 					MAC2STR(addr), dev->listen_freq, freq);
2070 				dev->listen_freq = freq;
2071 			}
2072 		}
2073 
2074 		os_get_reltime(&dev->last_seen);
2075 		p2p_parse_free(&msg);
2076 		return; /* already known */
2077 	}
2078 
2079 	dev = p2p_create_device(p2p, addr);
2080 	if (dev == NULL) {
2081 		p2p_parse_free(&msg);
2082 		return;
2083 	}
2084 
2085 	os_get_reltime(&dev->last_seen);
2086 	dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
2087 
2088 	if (msg.listen_channel) {
2089 		os_memcpy(dev->country, msg.listen_channel, 3);
2090 		dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
2091 						       msg.listen_channel[4]);
2092 	}
2093 
2094 	p2p_copy_wps_info(p2p, dev, 1, &msg);
2095 
2096 	if (msg.wfd_subelems) {
2097 		wpabuf_free(dev->info.wfd_subelems);
2098 		dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
2099 	}
2100 
2101 	p2p_parse_free(&msg);
2102 
2103 	p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
2104 		" dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
2105 		MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
2106 		dev->info.group_capab, dev->info.device_name,
2107 		dev->listen_freq);
2108 }
2109 
2110 
p2p_add_dev_from_go_neg_req(struct p2p_data * p2p,const u8 * addr,struct p2p_message * msg)2111 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
2112 						const u8 *addr,
2113 						struct p2p_message *msg)
2114 {
2115 	struct p2p_device *dev;
2116 
2117 	dev = p2p_get_device(p2p, addr);
2118 	if (dev) {
2119 		os_get_reltime(&dev->last_seen);
2120 		return dev; /* already known */
2121 	}
2122 
2123 	dev = p2p_create_device(p2p, addr);
2124 	if (dev == NULL)
2125 		return NULL;
2126 
2127 	p2p_add_dev_info(p2p, addr, dev, msg);
2128 
2129 	return dev;
2130 }
2131 
2132 
dev_type_match(const u8 * dev_type,const u8 * req_dev_type)2133 static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
2134 {
2135 	if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
2136 		return 1;
2137 	if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
2138 	    WPA_GET_BE32(&req_dev_type[2]) == 0 &&
2139 	    WPA_GET_BE16(&req_dev_type[6]) == 0)
2140 		return 1; /* Category match with wildcard OUI/sub-category */
2141 	return 0;
2142 }
2143 
2144 
dev_type_list_match(const u8 * dev_type,const u8 * req_dev_type[],size_t num_req_dev_type)2145 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
2146 			size_t num_req_dev_type)
2147 {
2148 	size_t i;
2149 	for (i = 0; i < num_req_dev_type; i++) {
2150 		if (dev_type_match(dev_type, req_dev_type[i]))
2151 			return 1;
2152 	}
2153 	return 0;
2154 }
2155 
2156 
2157 /**
2158  * p2p_match_dev_type - Match local device type with requested type
2159  * @p2p: P2P module context from p2p_init()
2160  * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
2161  * Returns: 1 on match, 0 on mismatch
2162  *
2163  * This function can be used to match the Requested Device Type attribute in
2164  * WPS IE with the local device types for deciding whether to reply to a Probe
2165  * Request frame.
2166  */
p2p_match_dev_type(struct p2p_data * p2p,struct wpabuf * wps)2167 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
2168 {
2169 	struct wps_parse_attr attr;
2170 	size_t i;
2171 
2172 	if (wps_parse_msg(wps, &attr))
2173 		return 1; /* assume no Requested Device Type attributes */
2174 
2175 	if (attr.num_req_dev_type == 0)
2176 		return 1; /* no Requested Device Type attributes -> match */
2177 
2178 	if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
2179 				attr.num_req_dev_type))
2180 		return 1; /* Own Primary Device Type matches */
2181 
2182 	for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) {
2183 		if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
2184 					attr.req_dev_type,
2185 					attr.num_req_dev_type))
2186 			return 1; /* Own Secondary Device Type matches */
2187 	}
2188 
2189 	/* No matching device type found */
2190 	return 0;
2191 }
2192 
2193 
p2p_build_probe_resp_ies(struct p2p_data * p2p,const u8 * query_hash,u8 query_count)2194 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p,
2195 					 const u8 *query_hash,
2196 					 u8 query_count)
2197 {
2198 	struct wpabuf *buf;
2199 	u8 *len;
2200 	int pw_id = -1;
2201 	size_t extra = 0;
2202 
2203 #ifdef CONFIG_WIFI_DISPLAY
2204 	if (p2p->wfd_ie_probe_resp)
2205 		extra = wpabuf_len(p2p->wfd_ie_probe_resp);
2206 #endif /* CONFIG_WIFI_DISPLAY */
2207 
2208 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2209 		extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2210 
2211 	if (query_count)
2212 		extra += MAX_SVC_ADV_IE_LEN;
2213 
2214 	buf = wpabuf_alloc(1000 + extra);
2215 	if (buf == NULL)
2216 		return NULL;
2217 
2218 	if (p2p->go_neg_peer) {
2219 		/* Advertise immediate availability of WPS credential */
2220 		pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
2221 	}
2222 
2223 	if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
2224 		p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
2225 		wpabuf_free(buf);
2226 		return NULL;
2227 	}
2228 
2229 #ifdef CONFIG_WIFI_DISPLAY
2230 	if (p2p->wfd_ie_probe_resp)
2231 		wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
2232 #endif /* CONFIG_WIFI_DISPLAY */
2233 
2234 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2235 		wpabuf_put_buf(buf,
2236 			       p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2237 
2238 	/* P2P IE */
2239 	len = p2p_buf_add_ie_hdr(buf);
2240 	p2p_buf_add_capability(buf, p2p->dev_capab &
2241 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
2242 	if (p2p->ext_listen_interval)
2243 		p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
2244 					      p2p->ext_listen_interval);
2245 	p2p_buf_add_device_info(buf, p2p, NULL);
2246 	p2p_buf_update_ie_hdr(buf, len);
2247 
2248 	if (query_count) {
2249 		p2p_buf_add_service_instance(buf, p2p, query_count, query_hash,
2250 					     p2p->p2ps_adv_list);
2251 	}
2252 
2253 	return buf;
2254 }
2255 
p2p_build_probe_resp_buf(struct p2p_data * p2p,struct wpabuf * buf,struct wpabuf * ies,const u8 * addr,int rx_freq)2256 static int p2p_build_probe_resp_buf(struct p2p_data *p2p, struct wpabuf *buf,
2257 				    struct wpabuf *ies,
2258 				    const u8 *addr, int rx_freq)
2259 {
2260 	struct ieee80211_mgmt *resp;
2261 	u8 channel, op_class;
2262 
2263 	resp = wpabuf_put(buf, offsetof(struct ieee80211_mgmt,
2264 					u.probe_resp.variable));
2265 
2266 	resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2267 					   (WLAN_FC_STYPE_PROBE_RESP << 4));
2268 	os_memcpy(resp->da, addr, ETH_ALEN);
2269 	os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2270 	os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2271 	resp->u.probe_resp.beacon_int = host_to_le16(100);
2272 	/* hardware or low-level driver will setup seq_ctrl and timestamp */
2273 	resp->u.probe_resp.capab_info =
2274 	    host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2275 		     WLAN_CAPABILITY_PRIVACY |
2276 		     WLAN_CAPABILITY_SHORT_SLOT_TIME);
2277 
2278 	wpabuf_put_u8(buf, WLAN_EID_SSID);
2279 	wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2280 	wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2281 
2282 	wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2283 	wpabuf_put_u8(buf, 8);
2284 	wpabuf_put_u8(buf, (60 / 5) | 0x80);
2285 	wpabuf_put_u8(buf, 90 / 5);
2286 	wpabuf_put_u8(buf, (120 / 5) | 0x80);
2287 	wpabuf_put_u8(buf, 180 / 5);
2288 	wpabuf_put_u8(buf, (240 / 5) | 0x80);
2289 	wpabuf_put_u8(buf, 360 / 5);
2290 	wpabuf_put_u8(buf, 480 / 5);
2291 	wpabuf_put_u8(buf, 540 / 5);
2292 
2293 	if (!rx_freq) {
2294 		channel = p2p->cfg->channel;
2295 	} else if (p2p_freq_to_channel(rx_freq, &op_class, &channel)) {
2296 		p2p_err(p2p, "Failed to convert freq to channel");
2297 		return -1;
2298 	}
2299 
2300 	wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2301 	wpabuf_put_u8(buf, 1);
2302 	wpabuf_put_u8(buf, channel);
2303 
2304 	wpabuf_put_buf(buf, ies);
2305 
2306 	return 0;
2307 }
2308 
p2p_service_find_asp(struct p2p_data * p2p,const u8 * hash)2309 static int p2p_service_find_asp(struct p2p_data *p2p, const u8 *hash)
2310 {
2311 	struct p2ps_advertisement *adv_data;
2312 	int any_wfa;
2313 
2314 	p2p_dbg(p2p, "ASP find - ASP list: %p", p2p->p2ps_adv_list);
2315 
2316 	/* Wildcard org.wi-fi.wfds matches any WFA spec defined service */
2317 	any_wfa = os_memcmp(hash, p2p->wild_card_hash, P2PS_HASH_LEN) == 0;
2318 
2319 	adv_data = p2p->p2ps_adv_list;
2320 	while (adv_data) {
2321 		if (os_memcmp(hash, adv_data->hash, P2PS_HASH_LEN) == 0)
2322 			return 1; /* exact hash match */
2323 		if (any_wfa &&
2324 		    os_strncmp(adv_data->svc_name, P2PS_WILD_HASH_STR,
2325 			       os_strlen(P2PS_WILD_HASH_STR)) == 0)
2326 			return 1; /* WFA service match */
2327 		adv_data = adv_data->next;
2328 	}
2329 
2330 	return 0;
2331 }
2332 
2333 
2334 static enum p2p_probe_req_status
p2p_reply_probe(struct p2p_data * p2p,const u8 * addr,const u8 * dst,const u8 * bssid,const u8 * ie,size_t ie_len,unsigned int rx_freq)2335 p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2336 		const u8 *bssid, const u8 *ie, size_t ie_len,
2337 		unsigned int rx_freq)
2338 {
2339 	struct ieee802_11_elems elems;
2340 	struct wpabuf *buf;
2341 	struct p2p_message msg;
2342 	struct wpabuf *ies;
2343 
2344 	if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2345 	    ParseFailed) {
2346 		/* Ignore invalid Probe Request frames */
2347 		p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it");
2348 		return P2P_PREQ_MALFORMED;
2349 	}
2350 
2351 	if (elems.p2p == NULL) {
2352 		/* not a P2P probe - ignore it */
2353 		p2p_dbg(p2p, "Not a P2P probe - ignore it");
2354 		return P2P_PREQ_NOT_P2P;
2355 	}
2356 
2357 	if (dst && !is_broadcast_ether_addr(dst) &&
2358 	    os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2359 		/* Not sent to the broadcast address or our P2P Device Address
2360 		 */
2361 		p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it",
2362 			MAC2STR(dst));
2363 		return P2P_PREQ_NOT_PROCESSED;
2364 	}
2365 
2366 	if (bssid && !is_broadcast_ether_addr(bssid)) {
2367 		/* Not sent to the Wildcard BSSID */
2368 		p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it",
2369 			MAC2STR(bssid));
2370 		return P2P_PREQ_NOT_PROCESSED;
2371 	}
2372 
2373 	if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2374 	    os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2375 	    0) {
2376 		/* not using P2P Wildcard SSID - ignore */
2377 		p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it");
2378 		return P2P_PREQ_NOT_PROCESSED;
2379 	}
2380 
2381 	if (supp_rates_11b_only(&elems)) {
2382 		/* Indicates support for 11b rates only */
2383 		p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it");
2384 		return P2P_PREQ_NOT_P2P;
2385 	}
2386 
2387 	os_memset(&msg, 0, sizeof(msg));
2388 	if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2389 		/* Could not parse P2P attributes */
2390 		p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it");
2391 		return P2P_PREQ_NOT_P2P;
2392 	}
2393 
2394 	if (msg.service_hash && msg.service_hash_count) {
2395 		const u8 *hash = msg.service_hash;
2396 		u8 i;
2397 		int p2ps_svc_found = 0;
2398 
2399 		p2p_dbg(p2p, "in_listen=%d drv_in_listen=%d when received P2PS Probe Request at %u MHz; own Listen channel %u, pending listen freq %u MHz",
2400 			p2p->in_listen, p2p->drv_in_listen, rx_freq,
2401 			p2p->cfg->channel, p2p->pending_listen_freq);
2402 
2403 		if (!p2p->in_listen && !p2p->drv_in_listen &&
2404 		    p2p->pending_listen_freq && rx_freq &&
2405 		    rx_freq != p2p->pending_listen_freq) {
2406 			p2p_dbg(p2p, "Do not reply to Probe Request frame that was received on %u MHz while waiting to start Listen state on %u MHz",
2407 				rx_freq, p2p->pending_listen_freq);
2408 			p2p_parse_free(&msg);
2409 			return P2P_PREQ_NOT_LISTEN;
2410 		}
2411 
2412 		for (i = 0; i < msg.service_hash_count; i++) {
2413 			if (p2p_service_find_asp(p2p, hash)) {
2414 				p2p_dbg(p2p, "Service Hash match found: "
2415 					MACSTR, MAC2STR(hash));
2416 				p2ps_svc_found = 1;
2417 				break;
2418 			}
2419 			hash += P2PS_HASH_LEN;
2420 		}
2421 
2422 		/* Probed hash unknown */
2423 		if (!p2ps_svc_found) {
2424 			p2p_dbg(p2p, "No Service Hash match found");
2425 			p2p_parse_free(&msg);
2426 			return P2P_PREQ_NOT_PROCESSED;
2427 		}
2428 	} else {
2429 		/* This is not a P2PS Probe Request */
2430 		p2p_dbg(p2p, "No P2PS Hash in Probe Request");
2431 
2432 		if (!p2p->in_listen || !p2p->drv_in_listen) {
2433 			/* not in Listen state - ignore Probe Request */
2434 			p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request",
2435 				p2p->in_listen, p2p->drv_in_listen);
2436 			p2p_parse_free(&msg);
2437 			return P2P_PREQ_NOT_LISTEN;
2438 		}
2439 	}
2440 
2441 	if (msg.device_id &&
2442 	    os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2443 		/* Device ID did not match */
2444 		p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it",
2445 			MAC2STR(msg.device_id));
2446 		p2p_parse_free(&msg);
2447 		return P2P_PREQ_NOT_PROCESSED;
2448 	}
2449 
2450 	/* Check Requested Device Type match */
2451 	if (msg.wps_attributes &&
2452 	    !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2453 		/* No match with Requested Device Type */
2454 		p2p_dbg(p2p, "Probe Req requested Device Type did not match - ignore it");
2455 		p2p_parse_free(&msg);
2456 		return P2P_PREQ_NOT_PROCESSED;
2457 	}
2458 
2459 	if (!p2p->cfg->send_probe_resp) {
2460 		/* Response generated elsewhere */
2461 		p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response");
2462 		p2p_parse_free(&msg);
2463 		return P2P_PREQ_NOT_PROCESSED;
2464 	}
2465 
2466 	p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
2467 
2468 	/*
2469 	 * We do not really have a specific BSS that this frame is advertising,
2470 	 * so build a frame that has some information in valid format. This is
2471 	 * really only used for discovery purposes, not to learn exact BSS
2472 	 * parameters.
2473 	 */
2474 	ies = p2p_build_probe_resp_ies(p2p, msg.service_hash,
2475 				       msg.service_hash_count);
2476 	p2p_parse_free(&msg);
2477 	if (ies == NULL)
2478 		return P2P_PREQ_NOT_PROCESSED;
2479 
2480 	buf = wpabuf_alloc(200 + wpabuf_len(ies));
2481 	if (buf == NULL) {
2482 		wpabuf_free(ies);
2483 		return P2P_PREQ_NOT_PROCESSED;
2484 	}
2485 
2486 	if (p2p_build_probe_resp_buf(p2p, buf, ies, addr, rx_freq)) {
2487 		wpabuf_free(ies);
2488 		wpabuf_free(buf);
2489 		return P2P_PREQ_NOT_PROCESSED;
2490 	}
2491 
2492 	wpabuf_free(ies);
2493 
2494 	p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf, rx_freq);
2495 
2496 	wpabuf_free(buf);
2497 
2498 	return P2P_PREQ_PROCESSED;
2499 }
2500 
2501 
2502 enum p2p_probe_req_status
p2p_probe_req_rx(struct p2p_data * p2p,const u8 * addr,const u8 * dst,const u8 * bssid,const u8 * ie,size_t ie_len,unsigned int rx_freq,int p2p_lo_started)2503 p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2504 		 const u8 *bssid, const u8 *ie, size_t ie_len,
2505 		 unsigned int rx_freq, int p2p_lo_started)
2506 {
2507 	enum p2p_probe_req_status res;
2508 
2509 	p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2510 
2511 	if (p2p_lo_started) {
2512 		p2p_dbg(p2p,
2513 			"Probe Response is offloaded, do not reply Probe Request");
2514 		return P2P_PREQ_PROCESSED;
2515 	}
2516 
2517 	res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len, rx_freq);
2518 	if (res != P2P_PREQ_PROCESSED && res != P2P_PREQ_NOT_PROCESSED)
2519 		return res;
2520 
2521 	/*
2522 	 * Activate a pending GO Negotiation/Invite flow if a received Probe
2523 	 * Request frame is from an expected peer. Some devices may share the
2524 	 * same address for P2P and non-P2P STA running simultaneously. The
2525 	 * P2P_PREQ_PROCESSED and P2P_PREQ_NOT_PROCESSED p2p_reply_probe()
2526 	 * return values verified above ensure we are handling a Probe Request
2527 	 * frame from a P2P peer.
2528 	 */
2529 	if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2530 	    p2p->go_neg_peer &&
2531 	    os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
2532 	    == 0 &&
2533 	    !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
2534 		/* Received a Probe Request from GO Negotiation peer */
2535 		p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
2536 		eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
2537 		eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
2538 		return res;
2539 	}
2540 
2541 	if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2542 	    p2p->invite_peer &&
2543 	    (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) &&
2544 	    os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2545 	    == 0) {
2546 		/* Received a Probe Request from Invite peer */
2547 		p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
2548 		eloop_cancel_timeout(p2p_invite_start, p2p, NULL);
2549 		eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
2550 		return res;
2551 	}
2552 
2553 	return res;
2554 }
2555 
2556 
p2p_assoc_req_ie_wlan_ap(struct p2p_data * p2p,const u8 * bssid,u8 * buf,size_t len,struct wpabuf * p2p_ie)2557 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2558 				    u8 *buf, size_t len, struct wpabuf *p2p_ie)
2559 {
2560 	struct wpabuf *tmp;
2561 	u8 *lpos;
2562 	size_t tmplen;
2563 	int res;
2564 	u8 group_capab;
2565 	struct p2p_message msg;
2566 
2567 	if (p2p_ie == NULL)
2568 		return 0; /* WLAN AP is not a P2P manager */
2569 
2570 	os_memset(&msg, 0, sizeof(msg));
2571 	if (p2p_parse_p2p_ie(p2p_ie, &msg) < 0)
2572 		return 0;
2573 
2574 	p2p_dbg(p2p, "BSS P2P manageability %s",
2575 		msg.manageability ? "enabled" : "disabled");
2576 
2577 	if (!msg.manageability)
2578 		return 0;
2579 
2580 	/*
2581 	 * (Re)Association Request - P2P IE
2582 	 * P2P Capability attribute (shall be present)
2583 	 * P2P Interface attribute (present if concurrent device and
2584 	 *	P2P Management is enabled)
2585 	 */
2586 	tmp = wpabuf_alloc(200);
2587 	if (tmp == NULL)
2588 		return -1;
2589 
2590 	lpos = p2p_buf_add_ie_hdr(tmp);
2591 	group_capab = 0;
2592 	if (p2p->num_groups > 0) {
2593 		group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2594 		if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2595 		    (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2596 		    p2p->cross_connect)
2597 			group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2598 	}
2599 	p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2600 	if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2601 	    (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2602 		p2p_buf_add_p2p_interface(tmp, p2p);
2603 	p2p_buf_update_ie_hdr(tmp, lpos);
2604 
2605 	tmplen = wpabuf_len(tmp);
2606 	if (tmplen > len)
2607 		res = -1;
2608 	else {
2609 		os_memcpy(buf, wpabuf_head(tmp), tmplen);
2610 		res = tmplen;
2611 	}
2612 	wpabuf_free(tmp);
2613 
2614 	return res;
2615 }
2616 
2617 
p2p_assoc_req_ie(struct p2p_data * p2p,const u8 * bssid,u8 * buf,size_t len,int p2p_group,struct wpabuf * p2p_ie)2618 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2619 		     size_t len, int p2p_group, struct wpabuf *p2p_ie)
2620 {
2621 	struct wpabuf *tmp;
2622 	u8 *lpos;
2623 	struct p2p_device *peer;
2624 	size_t tmplen;
2625 	int res;
2626 	size_t extra = 0;
2627 
2628 	if (!p2p_group)
2629 		return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2630 
2631 #ifdef CONFIG_WIFI_DISPLAY
2632 	if (p2p->wfd_ie_assoc_req)
2633 		extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2634 #endif /* CONFIG_WIFI_DISPLAY */
2635 
2636 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2637 		extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2638 
2639 	/*
2640 	 * (Re)Association Request - P2P IE
2641 	 * P2P Capability attribute (shall be present)
2642 	 * Extended Listen Timing (may be present)
2643 	 * P2P Device Info attribute (shall be present)
2644 	 */
2645 	tmp = wpabuf_alloc(200 + extra);
2646 	if (tmp == NULL)
2647 		return -1;
2648 
2649 #ifdef CONFIG_WIFI_DISPLAY
2650 	if (p2p->wfd_ie_assoc_req)
2651 		wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2652 #endif /* CONFIG_WIFI_DISPLAY */
2653 
2654 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2655 		wpabuf_put_buf(tmp,
2656 			       p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2657 
2658 	peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2659 
2660 	lpos = p2p_buf_add_ie_hdr(tmp);
2661 	p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2662 	if (p2p->ext_listen_interval)
2663 		p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2664 					      p2p->ext_listen_interval);
2665 	p2p_buf_add_device_info(tmp, p2p, peer);
2666 	p2p_buf_update_ie_hdr(tmp, lpos);
2667 
2668 	tmplen = wpabuf_len(tmp);
2669 	if (tmplen > len)
2670 		res = -1;
2671 	else {
2672 		os_memcpy(buf, wpabuf_head(tmp), tmplen);
2673 		res = tmplen;
2674 	}
2675 	wpabuf_free(tmp);
2676 
2677 	return res;
2678 }
2679 
2680 
p2p_scan_result_text(const u8 * ies,size_t ies_len,char * buf,char * end)2681 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2682 {
2683 	struct wpabuf *p2p_ie;
2684 	int ret;
2685 
2686 	p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2687 	if (p2p_ie == NULL)
2688 		return 0;
2689 
2690 	ret = p2p_attr_text(p2p_ie, buf, end);
2691 	wpabuf_free(p2p_ie);
2692 	return ret;
2693 }
2694 
2695 
2696 struct p2ps_advertisement *
p2p_service_p2ps_id(struct p2p_data * p2p,u32 adv_id)2697 p2p_service_p2ps_id(struct p2p_data *p2p, u32 adv_id)
2698 {
2699 	struct p2ps_advertisement *adv_data;
2700 
2701 	if (!p2p)
2702 		return NULL;
2703 
2704 	adv_data = p2p->p2ps_adv_list;
2705 	while (adv_data) {
2706 		if (adv_data->id == adv_id)
2707 			return adv_data;
2708 		adv_data = adv_data->next;
2709 	}
2710 
2711 	return NULL;
2712 }
2713 
2714 
p2p_service_del_asp(struct p2p_data * p2p,u32 adv_id)2715 int p2p_service_del_asp(struct p2p_data *p2p, u32 adv_id)
2716 {
2717 	struct p2ps_advertisement *adv_data;
2718 	struct p2ps_advertisement **prior;
2719 
2720 	if (!p2p)
2721 		return -1;
2722 
2723 	adv_data = p2p->p2ps_adv_list;
2724 	prior = &p2p->p2ps_adv_list;
2725 	while (adv_data) {
2726 		if (adv_data->id == adv_id) {
2727 			p2p_dbg(p2p, "Delete ASP adv_id=0x%x", adv_id);
2728 			*prior = adv_data->next;
2729 			os_free(adv_data);
2730 			return 0;
2731 		}
2732 		prior = &adv_data->next;
2733 		adv_data = adv_data->next;
2734 	}
2735 
2736 	return -1;
2737 }
2738 
2739 
p2p_service_add_asp(struct p2p_data * p2p,int auto_accept,u32 adv_id,const char * adv_str,u8 svc_state,u16 config_methods,const char * svc_info,const u8 * cpt_priority)2740 int p2p_service_add_asp(struct p2p_data *p2p, int auto_accept, u32 adv_id,
2741 			const char *adv_str, u8 svc_state, u16 config_methods,
2742 			const char *svc_info, const u8 *cpt_priority)
2743 {
2744 	struct p2ps_advertisement *adv_data, *tmp, **prev;
2745 	u8 buf[P2PS_HASH_LEN];
2746 	size_t adv_data_len, adv_len, info_len = 0;
2747 	int i;
2748 
2749 	if (!p2p || !adv_str || !adv_str[0] || !cpt_priority)
2750 		return -1;
2751 
2752 	if (!(config_methods & p2p->cfg->config_methods)) {
2753 		p2p_dbg(p2p, "Config methods not supported svc: 0x%x dev: 0x%x",
2754 			config_methods, p2p->cfg->config_methods);
2755 		return -1;
2756 	}
2757 
2758 	if (!p2ps_gen_hash(p2p, adv_str, buf))
2759 		return -1;
2760 
2761 	if (svc_info)
2762 		info_len = os_strlen(svc_info);
2763 	adv_len = os_strlen(adv_str);
2764 	adv_data_len = sizeof(struct p2ps_advertisement) + adv_len + 1 +
2765 		info_len + 1;
2766 
2767 	adv_data = os_zalloc(adv_data_len);
2768 	if (!adv_data)
2769 		return -1;
2770 
2771 	os_memcpy(adv_data->hash, buf, P2PS_HASH_LEN);
2772 	adv_data->id = adv_id;
2773 	adv_data->state = svc_state;
2774 	adv_data->config_methods = config_methods & p2p->cfg->config_methods;
2775 	adv_data->auto_accept = (u8) auto_accept;
2776 	os_memcpy(adv_data->svc_name, adv_str, adv_len);
2777 
2778 	for (i = 0; cpt_priority[i] && i < P2PS_FEATURE_CAPAB_CPT_MAX; i++) {
2779 		adv_data->cpt_priority[i] = cpt_priority[i];
2780 		adv_data->cpt_mask |= cpt_priority[i];
2781 	}
2782 
2783 	if (svc_info && info_len) {
2784 		adv_data->svc_info = &adv_data->svc_name[adv_len + 1];
2785 		os_memcpy(adv_data->svc_info, svc_info, info_len);
2786 	}
2787 
2788 	/*
2789 	 * Group Advertisements by service string. They do not need to be
2790 	 * sorted, but groups allow easier Probe Response instance grouping
2791 	 */
2792 	tmp = p2p->p2ps_adv_list;
2793 	prev = &p2p->p2ps_adv_list;
2794 	while (tmp) {
2795 		if (tmp->id == adv_data->id) {
2796 			if (os_strcmp(tmp->svc_name, adv_data->svc_name) != 0) {
2797 				os_free(adv_data);
2798 				return -1;
2799 			}
2800 			adv_data->next = tmp->next;
2801 			*prev = adv_data;
2802 			os_free(tmp);
2803 			goto inserted;
2804 		} else {
2805 			if (os_strcmp(tmp->svc_name, adv_data->svc_name) == 0) {
2806 				adv_data->next = tmp->next;
2807 				tmp->next = adv_data;
2808 				goto inserted;
2809 			}
2810 		}
2811 		prev = &tmp->next;
2812 		tmp = tmp->next;
2813 	}
2814 
2815 	/* No svc_name match found */
2816 	adv_data->next = p2p->p2ps_adv_list;
2817 	p2p->p2ps_adv_list = adv_data;
2818 
2819 inserted:
2820 	p2p_dbg(p2p,
2821 		"Added ASP advertisement adv_id=0x%x config_methods=0x%x svc_state=0x%x adv_str='%s' cpt_mask=0x%x",
2822 		adv_id, adv_data->config_methods, svc_state, adv_str,
2823 		adv_data->cpt_mask);
2824 
2825 	return 0;
2826 }
2827 
2828 
p2p_service_flush_asp(struct p2p_data * p2p)2829 void p2p_service_flush_asp(struct p2p_data *p2p)
2830 {
2831 	struct p2ps_advertisement *adv, *prev;
2832 
2833 	if (!p2p)
2834 		return;
2835 
2836 	adv = p2p->p2ps_adv_list;
2837 	while (adv) {
2838 		prev = adv;
2839 		adv = adv->next;
2840 		os_free(prev);
2841 	}
2842 
2843 	p2p->p2ps_adv_list = NULL;
2844 	p2ps_prov_free(p2p);
2845 	p2p_dbg(p2p, "All ASP advertisements flushed");
2846 }
2847 
2848 
p2p_parse_dev_addr_in_p2p_ie(struct wpabuf * p2p_ie,u8 * dev_addr)2849 int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2850 {
2851 	struct p2p_message msg;
2852 
2853 	os_memset(&msg, 0, sizeof(msg));
2854 	if (p2p_parse_p2p_ie(p2p_ie, &msg))
2855 		return -1;
2856 
2857 	if (msg.p2p_device_addr) {
2858 		os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2859 		return 0;
2860 	} else if (msg.device_id) {
2861 		os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2862 		return 0;
2863 	}
2864 	return -1;
2865 }
2866 
2867 
p2p_parse_dev_addr(const u8 * ies,size_t ies_len,u8 * dev_addr)2868 int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2869 {
2870 	struct wpabuf *p2p_ie;
2871 	int ret;
2872 
2873 	p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2874 					     P2P_IE_VENDOR_TYPE);
2875 	if (p2p_ie == NULL)
2876 		return -1;
2877 	ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
2878 	wpabuf_free(p2p_ie);
2879 	return ret;
2880 }
2881 
2882 
p2p_clear_go_neg(struct p2p_data * p2p)2883 static void p2p_clear_go_neg(struct p2p_data *p2p)
2884 {
2885 	p2p->go_neg_peer = NULL;
2886 	p2p_clear_timeout(p2p);
2887 	p2p_set_state(p2p, P2P_IDLE);
2888 }
2889 
2890 
p2p_wps_success_cb(struct p2p_data * p2p,const u8 * mac_addr)2891 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2892 {
2893 	if (p2p->go_neg_peer == NULL) {
2894 		p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
2895 		return; /* No pending Group Formation */
2896 	}
2897 
2898 	if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2899 	    0) {
2900 		p2p_dbg(p2p, "Ignore WPS registration success notification for "
2901 			MACSTR " (GO Negotiation peer " MACSTR ")",
2902 			MAC2STR(mac_addr),
2903 			MAC2STR(p2p->go_neg_peer->intended_addr));
2904 		return; /* Ignore unexpected peer address */
2905 	}
2906 
2907 	p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
2908 		MAC2STR(mac_addr));
2909 
2910 	p2p_clear_go_neg(p2p);
2911 }
2912 
2913 
p2p_group_formation_failed(struct p2p_data * p2p)2914 void p2p_group_formation_failed(struct p2p_data *p2p)
2915 {
2916 	if (p2p->go_neg_peer == NULL) {
2917 		p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
2918 		return; /* No pending Group Formation */
2919 	}
2920 
2921 	p2p_dbg(p2p, "Group Formation failed with " MACSTR,
2922 		MAC2STR(p2p->go_neg_peer->intended_addr));
2923 
2924 	p2p_clear_go_neg(p2p);
2925 }
2926 
2927 
is_p2p_6ghz_disabled(struct p2p_data * p2p)2928 bool is_p2p_6ghz_disabled(struct p2p_data *p2p)
2929 {
2930 	if (p2p)
2931 		return p2p->cfg->p2p_6ghz_disable;
2932 	return false;
2933 }
2934 
2935 
p2p_init(const struct p2p_config * cfg)2936 struct p2p_data * p2p_init(const struct p2p_config *cfg)
2937 {
2938 	struct p2p_data *p2p;
2939 
2940 	if (cfg->max_peers < 1 ||
2941 	    cfg->passphrase_len < 8 || cfg->passphrase_len > 63)
2942 		return NULL;
2943 
2944 	p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2945 	if (p2p == NULL)
2946 		return NULL;
2947 	p2p->cfg = (struct p2p_config *) (p2p + 1);
2948 	os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2949 	if (cfg->dev_name)
2950 		p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2951 	if (cfg->manufacturer)
2952 		p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2953 	if (cfg->model_name)
2954 		p2p->cfg->model_name = os_strdup(cfg->model_name);
2955 	if (cfg->model_number)
2956 		p2p->cfg->model_number = os_strdup(cfg->model_number);
2957 	if (cfg->serial_number)
2958 		p2p->cfg->serial_number = os_strdup(cfg->serial_number);
2959 	if (cfg->pref_chan) {
2960 		p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2961 						sizeof(struct p2p_channel));
2962 		if (p2p->cfg->pref_chan) {
2963 			os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2964 				  cfg->num_pref_chan *
2965 				  sizeof(struct p2p_channel));
2966 		} else
2967 			p2p->cfg->num_pref_chan = 0;
2968 	}
2969 
2970 	p2ps_gen_hash(p2p, P2PS_WILD_HASH_STR, p2p->wild_card_hash);
2971 
2972 	p2p->min_disc_int = 1;
2973 	p2p->max_disc_int = 3;
2974 	p2p->max_disc_tu = -1;
2975 
2976 	if (os_get_random(&p2p->next_tie_breaker, 1) < 0)
2977 		p2p->next_tie_breaker = 0;
2978 	p2p->next_tie_breaker &= 0x01;
2979 	if (cfg->sd_request)
2980 		p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2981 	p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2982 	if (cfg->concurrent_operations)
2983 		p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2984 	p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2985 
2986 	dl_list_init(&p2p->devices);
2987 
2988 	p2p->go_timeout = 100;
2989 	p2p->client_timeout = 20;
2990 	p2p->num_p2p_sd_queries = 0;
2991 
2992 	p2p_dbg(p2p, "initialized");
2993 	p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
2994 	p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
2995 
2996 	return p2p;
2997 }
2998 
2999 
p2p_deinit(struct p2p_data * p2p)3000 void p2p_deinit(struct p2p_data *p2p)
3001 {
3002 #ifdef CONFIG_WIFI_DISPLAY
3003 	wpabuf_free(p2p->wfd_ie_beacon);
3004 	wpabuf_free(p2p->wfd_ie_probe_req);
3005 	wpabuf_free(p2p->wfd_ie_probe_resp);
3006 	wpabuf_free(p2p->wfd_ie_assoc_req);
3007 	wpabuf_free(p2p->wfd_ie_invitation);
3008 	wpabuf_free(p2p->wfd_ie_prov_disc_req);
3009 	wpabuf_free(p2p->wfd_ie_prov_disc_resp);
3010 	wpabuf_free(p2p->wfd_ie_go_neg);
3011 	wpabuf_free(p2p->wfd_dev_info);
3012 	wpabuf_free(p2p->wfd_assoc_bssid);
3013 	wpabuf_free(p2p->wfd_coupled_sink_info);
3014 	wpabuf_free(p2p->wfd_r2_dev_info);
3015 #endif /* CONFIG_WIFI_DISPLAY */
3016 
3017 	eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
3018 	eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
3019 	eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
3020 	p2p_flush(p2p);
3021 	p2p_free_req_dev_types(p2p);
3022 	os_free(p2p->cfg->dev_name);
3023 	os_free(p2p->cfg->manufacturer);
3024 	os_free(p2p->cfg->model_name);
3025 	os_free(p2p->cfg->model_number);
3026 	os_free(p2p->cfg->serial_number);
3027 	os_free(p2p->cfg->pref_chan);
3028 	os_free(p2p->groups);
3029 	p2ps_prov_free(p2p);
3030 	wpabuf_free(p2p->sd_resp);
3031 	p2p_remove_wps_vendor_extensions(p2p);
3032 	os_free(p2p->no_go_freq.range);
3033 	p2p_service_flush_asp(p2p);
3034 
3035 	os_free(p2p);
3036 }
3037 
3038 
p2p_flush(struct p2p_data * p2p)3039 void p2p_flush(struct p2p_data *p2p)
3040 {
3041 	struct p2p_device *dev, *prev;
3042 
3043 	p2p_ext_listen(p2p, 0, 0);
3044 	p2p_stop_find(p2p);
3045 	dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
3046 			      list) {
3047 		dl_list_del(&dev->list);
3048 		p2p_device_free(p2p, dev);
3049 	}
3050 	p2p_free_sd_queries(p2p);
3051 	p2p->ssid_set = 0;
3052 	p2ps_prov_free(p2p);
3053 	p2p_reset_pending_pd(p2p);
3054 	p2p->override_pref_op_class = 0;
3055 	p2p->override_pref_channel = 0;
3056 }
3057 
3058 
p2p_unauthorize(struct p2p_data * p2p,const u8 * addr)3059 int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
3060 {
3061 	struct p2p_device *dev;
3062 
3063 	dev = p2p_get_device(p2p, addr);
3064 	if (dev == NULL)
3065 		return -1;
3066 
3067 	p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
3068 
3069 	if (p2p->go_neg_peer == dev) {
3070 		eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
3071 		p2p->go_neg_peer = NULL;
3072 	}
3073 
3074 	dev->wps_method = WPS_NOT_READY;
3075 	dev->oob_pw_id = 0;
3076 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
3077 	dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
3078 
3079 	return 0;
3080 }
3081 
3082 
p2p_set_dev_name(struct p2p_data * p2p,const char * dev_name)3083 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
3084 {
3085 	os_free(p2p->cfg->dev_name);
3086 	if (dev_name) {
3087 		p2p->cfg->dev_name = os_strdup(dev_name);
3088 		if (p2p->cfg->dev_name == NULL)
3089 			return -1;
3090 	} else
3091 		p2p->cfg->dev_name = NULL;
3092 	return 0;
3093 }
3094 
3095 
p2p_set_manufacturer(struct p2p_data * p2p,const char * manufacturer)3096 int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
3097 {
3098 	os_free(p2p->cfg->manufacturer);
3099 	p2p->cfg->manufacturer = NULL;
3100 	if (manufacturer) {
3101 		p2p->cfg->manufacturer = os_strdup(manufacturer);
3102 		if (p2p->cfg->manufacturer == NULL)
3103 			return -1;
3104 	}
3105 
3106 	return 0;
3107 }
3108 
3109 
p2p_set_model_name(struct p2p_data * p2p,const char * model_name)3110 int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
3111 {
3112 	os_free(p2p->cfg->model_name);
3113 	p2p->cfg->model_name = NULL;
3114 	if (model_name) {
3115 		p2p->cfg->model_name = os_strdup(model_name);
3116 		if (p2p->cfg->model_name == NULL)
3117 			return -1;
3118 	}
3119 
3120 	return 0;
3121 }
3122 
3123 
p2p_set_model_number(struct p2p_data * p2p,const char * model_number)3124 int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
3125 {
3126 	os_free(p2p->cfg->model_number);
3127 	p2p->cfg->model_number = NULL;
3128 	if (model_number) {
3129 		p2p->cfg->model_number = os_strdup(model_number);
3130 		if (p2p->cfg->model_number == NULL)
3131 			return -1;
3132 	}
3133 
3134 	return 0;
3135 }
3136 
3137 
p2p_set_serial_number(struct p2p_data * p2p,const char * serial_number)3138 int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
3139 {
3140 	os_free(p2p->cfg->serial_number);
3141 	p2p->cfg->serial_number = NULL;
3142 	if (serial_number) {
3143 		p2p->cfg->serial_number = os_strdup(serial_number);
3144 		if (p2p->cfg->serial_number == NULL)
3145 			return -1;
3146 	}
3147 
3148 	return 0;
3149 }
3150 
3151 
p2p_set_config_methods(struct p2p_data * p2p,u16 config_methods)3152 void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
3153 {
3154 	p2p->cfg->config_methods = config_methods;
3155 }
3156 
3157 
p2p_set_uuid(struct p2p_data * p2p,const u8 * uuid)3158 void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
3159 {
3160 	os_memcpy(p2p->cfg->uuid, uuid, 16);
3161 }
3162 
3163 
p2p_set_pri_dev_type(struct p2p_data * p2p,const u8 * pri_dev_type)3164 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
3165 {
3166 	os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
3167 	return 0;
3168 }
3169 
3170 
p2p_set_sec_dev_types(struct p2p_data * p2p,const u8 dev_types[][8],size_t num_dev_types)3171 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
3172 			  size_t num_dev_types)
3173 {
3174 	if (num_dev_types > P2P_SEC_DEVICE_TYPES)
3175 		num_dev_types = P2P_SEC_DEVICE_TYPES;
3176 	p2p->cfg->num_sec_dev_types = num_dev_types;
3177 	os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
3178 	return 0;
3179 }
3180 
3181 
p2p_remove_wps_vendor_extensions(struct p2p_data * p2p)3182 void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
3183 {
3184 	int i;
3185 
3186 	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3187 		wpabuf_free(p2p->wps_vendor_ext[i]);
3188 		p2p->wps_vendor_ext[i] = NULL;
3189 	}
3190 }
3191 
3192 
p2p_add_wps_vendor_extension(struct p2p_data * p2p,const struct wpabuf * vendor_ext)3193 int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
3194 				 const struct wpabuf *vendor_ext)
3195 {
3196 	int i;
3197 
3198 	if (vendor_ext == NULL)
3199 		return -1;
3200 
3201 	for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
3202 		if (p2p->wps_vendor_ext[i] == NULL)
3203 			break;
3204 	}
3205 	if (i >= P2P_MAX_WPS_VENDOR_EXT)
3206 		return -1;
3207 
3208 	p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
3209 	if (p2p->wps_vendor_ext[i] == NULL)
3210 		return -1;
3211 
3212 	return 0;
3213 }
3214 
3215 
p2p_set_country(struct p2p_data * p2p,const char * country)3216 int p2p_set_country(struct p2p_data *p2p, const char *country)
3217 {
3218 	os_memcpy(p2p->cfg->country, country, 3);
3219 	return 0;
3220 }
3221 
3222 
p2p_pre_find_operation(struct p2p_data * p2p,struct p2p_device * dev)3223 static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev)
3224 {
3225 	int res;
3226 
3227 	if (dev->sd_pending_bcast_queries == 0) {
3228 		/* Initialize with total number of registered broadcast
3229 		 * SD queries. */
3230 		dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries;
3231 	}
3232 
3233 	res = p2p_start_sd(p2p, dev);
3234 	if (res == -2)
3235 		return -2;
3236 	if (res == 0)
3237 		return 1;
3238 
3239 	if (dev->req_config_methods &&
3240 	    !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
3241 		p2p_dbg(p2p, "Send pending Provision Discovery Request to "
3242 			MACSTR " (config methods 0x%x)",
3243 			MAC2STR(dev->info.p2p_device_addr),
3244 			dev->req_config_methods);
3245 		if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
3246 			return 1;
3247 	}
3248 
3249 	return 0;
3250 }
3251 
3252 
p2p_continue_find(struct p2p_data * p2p)3253 void p2p_continue_find(struct p2p_data *p2p)
3254 {
3255 	struct p2p_device *dev;
3256 	int found, res;
3257 
3258 	p2p_set_state(p2p, P2P_SEARCH);
3259 
3260 	/* Continue from the device following the last iteration */
3261 	found = 0;
3262 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3263 		if (dev == p2p->last_p2p_find_oper) {
3264 			found = 1;
3265 			continue;
3266 		}
3267 		if (!found)
3268 			continue;
3269 		res = p2p_pre_find_operation(p2p, dev);
3270 		if (res > 0) {
3271 			p2p->last_p2p_find_oper = dev;
3272 			return;
3273 		}
3274 		if (res == -2)
3275 			goto skip_sd;
3276 	}
3277 
3278 	/*
3279 	 * Wrap around to the beginning of the list and continue until the last
3280 	 * iteration device.
3281 	 */
3282 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3283 		res = p2p_pre_find_operation(p2p, dev);
3284 		if (res > 0) {
3285 			p2p->last_p2p_find_oper = dev;
3286 			return;
3287 		}
3288 		if (res == -2)
3289 			goto skip_sd;
3290 		if (dev == p2p->last_p2p_find_oper)
3291 			break;
3292 	}
3293 
3294 skip_sd:
3295 	os_memset(p2p->sd_query_no_ack, 0, ETH_ALEN);
3296 	p2p_listen_in_find(p2p, 1);
3297 }
3298 
3299 
p2p_sd_cb(struct p2p_data * p2p,int success)3300 static void p2p_sd_cb(struct p2p_data *p2p, int success)
3301 {
3302 	p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
3303 		success);
3304 	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3305 
3306 	if (!success) {
3307 		if (p2p->sd_peer) {
3308 			if (is_zero_ether_addr(p2p->sd_query_no_ack)) {
3309 				os_memcpy(p2p->sd_query_no_ack,
3310 					  p2p->sd_peer->info.p2p_device_addr,
3311 					  ETH_ALEN);
3312 				p2p_dbg(p2p,
3313 					"First SD Query no-ACK in this search iteration: "
3314 					MACSTR, MAC2STR(p2p->sd_query_no_ack));
3315 			}
3316 			p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3317 		}
3318 		p2p->sd_peer = NULL;
3319 		if (p2p->state != P2P_IDLE)
3320 			p2p_continue_find(p2p);
3321 		return;
3322 	}
3323 
3324 	if (p2p->sd_peer == NULL) {
3325 		p2p_dbg(p2p, "No SD peer entry known");
3326 		if (p2p->state != P2P_IDLE)
3327 			p2p_continue_find(p2p);
3328 		return;
3329 	}
3330 
3331 	if (p2p->sd_query && p2p->sd_query->for_all_peers) {
3332 		/* Update the pending broadcast SD query count for this device
3333 		 */
3334 		p2p->sd_peer->sd_pending_bcast_queries--;
3335 
3336 		/*
3337 		 * If there are no pending broadcast queries for this device,
3338 		 * mark it as done (-1).
3339 		 */
3340 		if (p2p->sd_peer->sd_pending_bcast_queries == 0)
3341 			p2p->sd_peer->sd_pending_bcast_queries = -1;
3342 	}
3343 
3344 	/* Wait for response from the peer */
3345 	p2p_set_state(p2p, P2P_SD_DURING_FIND);
3346 	p2p_set_timeout(p2p, 0, 200000);
3347 }
3348 
3349 
3350 /**
3351  * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
3352  * @p2p: P2P module context from p2p_init()
3353  */
p2p_retry_pd(struct p2p_data * p2p)3354 static void p2p_retry_pd(struct p2p_data *p2p)
3355 {
3356 	struct p2p_device *dev;
3357 
3358 	/*
3359 	 * Retry the prov disc req attempt only for the peer that the user had
3360 	 * requested.
3361 	 */
3362 
3363 	dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3364 		if (os_memcmp(p2p->pending_pd_devaddr,
3365 			      dev->info.p2p_device_addr, ETH_ALEN) != 0)
3366 			continue;
3367 		if (!dev->req_config_methods)
3368 			continue;
3369 
3370 		p2p_dbg(p2p, "Send pending Provision Discovery Request to "
3371 			MACSTR " (config methods 0x%x)",
3372 			MAC2STR(dev->info.p2p_device_addr),
3373 			dev->req_config_methods);
3374 		p2p_send_prov_disc_req(p2p, dev,
3375 				       dev->flags & P2P_DEV_PD_FOR_JOIN,
3376 				       p2p->pd_force_freq);
3377 		return;
3378 	}
3379 }
3380 
3381 
p2p_prov_disc_cb(struct p2p_data * p2p,int success)3382 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
3383 {
3384 	p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
3385 		success);
3386 
3387 	/*
3388 	 * Postpone resetting the pending action state till after we actually
3389 	 * time out. This allows us to take some action like notifying any
3390 	 * interested parties about no response to the request.
3391 	 *
3392 	 * When the timer (below) goes off we check in IDLE, SEARCH, or
3393 	 * LISTEN_ONLY state, which are the only allowed states to issue a PD
3394 	 * requests in, if this was still pending and then raise notification.
3395 	 */
3396 
3397 	if (!success) {
3398 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3399 
3400 		if (p2p->user_initiated_pd &&
3401 		    (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
3402 		{
3403 			/* Retry request from timeout to avoid busy loops */
3404 			p2p->pending_action_state = P2P_PENDING_PD;
3405 			p2p_set_timeout(p2p, 0, 50000);
3406 		} else if (p2p->state != P2P_IDLE)
3407 			p2p_continue_find(p2p);
3408 		else if (p2p->user_initiated_pd) {
3409 			p2p->pending_action_state = P2P_PENDING_PD;
3410 			p2p_set_timeout(p2p, 0, 300000);
3411 		}
3412 		return;
3413 	}
3414 
3415 	/*
3416 	 * If after PD Request the peer doesn't expect to receive PD Response
3417 	 * the PD Request ACK indicates a completion of the current PD. This
3418 	 * happens only on the advertiser side sending the follow-on PD Request
3419 	 * with the status different than 12 (Success: accepted by user).
3420 	 */
3421 	if (p2p->p2ps_prov && !p2p->p2ps_prov->pd_seeker &&
3422 	    p2p->p2ps_prov->status != P2P_SC_SUCCESS_DEFERRED) {
3423 		p2p_dbg(p2p, "P2PS PD completion on Follow-on PD Request ACK");
3424 
3425 		if (p2p->send_action_in_progress) {
3426 			p2p->send_action_in_progress = 0;
3427 			p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3428 		}
3429 
3430 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3431 
3432 		if (p2p->cfg->p2ps_prov_complete) {
3433 			p2p->cfg->p2ps_prov_complete(
3434 				p2p->cfg->cb_ctx,
3435 				p2p->p2ps_prov->status,
3436 				p2p->p2ps_prov->adv_mac,
3437 				p2p->p2ps_prov->adv_mac,
3438 				p2p->p2ps_prov->session_mac,
3439 				NULL, p2p->p2ps_prov->adv_id,
3440 				p2p->p2ps_prov->session_id,
3441 				0, 0, NULL, 0, 0, 0,
3442 				NULL, NULL, 0, 0, NULL, 0);
3443 		}
3444 
3445 		if (p2p->user_initiated_pd)
3446 			p2p_reset_pending_pd(p2p);
3447 
3448 		p2ps_prov_free(p2p);
3449 		return;
3450 	}
3451 
3452 	/*
3453 	 * This postponing, of resetting pending_action_state, needs to be
3454 	 * done only for user initiated PD requests and not internal ones.
3455 	 */
3456 	if (p2p->user_initiated_pd)
3457 		p2p->pending_action_state = P2P_PENDING_PD;
3458 	else
3459 		p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3460 
3461 	/* Wait for response from the peer */
3462 	if (p2p->state == P2P_SEARCH)
3463 		p2p_set_state(p2p, P2P_PD_DURING_FIND);
3464 	p2p_set_timeout(p2p, 0, 200000);
3465 }
3466 
3467 
p2p_prov_disc_resp_cb(struct p2p_data * p2p,int success)3468 static void p2p_prov_disc_resp_cb(struct p2p_data *p2p, int success)
3469 {
3470 	p2p_dbg(p2p, "Provision Discovery Response TX callback: success=%d",
3471 		success);
3472 
3473 	if (p2p->send_action_in_progress) {
3474 		p2p->send_action_in_progress = 0;
3475 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3476 	}
3477 
3478 	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3479 
3480 	if (!success) {
3481 		if (p2p->state == P2P_SEARCH)
3482 			p2p_continue_find(p2p);
3483 		return;
3484 	}
3485 
3486 	if (!p2p->cfg->prov_disc_resp_cb ||
3487 	    p2p->cfg->prov_disc_resp_cb(p2p->cfg->cb_ctx) < 1) {
3488 		if (p2p->state == P2P_SEARCH)
3489 			p2p_continue_find(p2p);
3490 		return;
3491 	}
3492 
3493 	p2p_dbg(p2p,
3494 		"Post-Provision Discovery operations started - do not try to continue other P2P operations");
3495 }
3496 
3497 
p2p_scan_res_handler(struct p2p_data * p2p,const u8 * bssid,int freq,struct os_reltime * rx_time,int level,const u8 * ies,size_t ies_len)3498 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
3499 			 struct os_reltime *rx_time, int level, const u8 *ies,
3500 			 size_t ies_len)
3501 {
3502 	if (os_reltime_before(rx_time, &p2p->find_start)) {
3503 		/*
3504 		 * The driver may have cached (e.g., in cfg80211 BSS table) the
3505 		 * scan results for relatively long time. To avoid reporting
3506 		 * stale information, update P2P peers only based on results
3507 		 * that have based on frames received after the last p2p_find
3508 		 * operation was started.
3509 		 */
3510 		p2p_dbg(p2p, "Ignore old scan result for " MACSTR
3511 			" (rx_time=%u.%06u find_start=%u.%06u)",
3512 			MAC2STR(bssid), (unsigned int) rx_time->sec,
3513 			(unsigned int) rx_time->usec,
3514 			(unsigned int) p2p->find_start.sec,
3515 			(unsigned int) p2p->find_start.usec);
3516 		return 0;
3517 	}
3518 
3519 	p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
3520 
3521 	return 0;
3522 }
3523 
3524 
p2p_scan_res_handled(struct p2p_data * p2p,unsigned int delay)3525 void p2p_scan_res_handled(struct p2p_data *p2p, unsigned int delay)
3526 {
3527 	if (!p2p->p2p_scan_running) {
3528 		p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
3529 	}
3530 	p2p->p2p_scan_running = 0;
3531 
3532 	/* Use this delay only when p2p_find doesn't set it */
3533 	if (!p2p->search_delay)
3534 		p2p->search_delay = delay;
3535 
3536 	eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
3537 
3538 	if (p2p_run_after_scan(p2p))
3539 		return;
3540 	if (p2p->state == P2P_SEARCH)
3541 		p2p_continue_find(p2p);
3542 }
3543 
3544 
p2p_scan_ie(struct p2p_data * p2p,struct wpabuf * ies,const u8 * dev_id,unsigned int bands)3545 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id,
3546 		 unsigned int bands)
3547 {
3548 	u8 dev_capab;
3549 	u8 *len;
3550 
3551 #ifdef CONFIG_WIFI_DISPLAY
3552 	if (p2p->wfd_ie_probe_req)
3553 		wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
3554 #endif /* CONFIG_WIFI_DISPLAY */
3555 
3556 	if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3557 		wpabuf_put_buf(ies,
3558 			       p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3559 
3560 	len = p2p_buf_add_ie_hdr(ies);
3561 
3562 	dev_capab = p2p->dev_capab & ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3563 
3564 	/* P2PS requires Probe Request frames to include SD bit */
3565 	if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3566 		dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
3567 
3568 	p2p_buf_add_capability(ies, dev_capab, 0);
3569 
3570 	if (dev_id)
3571 		p2p_buf_add_device_id(ies, dev_id);
3572 	if (p2p->cfg->reg_class && p2p->cfg->channel)
3573 		p2p_buf_add_listen_channel(ies, p2p->cfg->country,
3574 					   p2p->cfg->reg_class,
3575 					   p2p->cfg->channel);
3576 	if (p2p->ext_listen_interval)
3577 		p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
3578 					      p2p->ext_listen_interval);
3579 
3580 	if (bands & BAND_60_GHZ)
3581 		p2p_buf_add_device_info(ies, p2p, NULL);
3582 
3583 	if (p2p->p2ps_seek && p2p->p2ps_seek_count)
3584 		p2p_buf_add_service_hash(ies, p2p);
3585 
3586 	/* TODO: p2p_buf_add_operating_channel() if GO */
3587 	p2p_buf_update_ie_hdr(ies, len);
3588 }
3589 
3590 
p2p_scan_ie_buf_len(struct p2p_data * p2p)3591 size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
3592 {
3593 	size_t len = 100;
3594 
3595 #ifdef CONFIG_WIFI_DISPLAY
3596 	if (p2p && p2p->wfd_ie_probe_req)
3597 		len += wpabuf_len(p2p->wfd_ie_probe_req);
3598 #endif /* CONFIG_WIFI_DISPLAY */
3599 
3600 	if (p2p && p2p->vendor_elem &&
3601 	    p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3602 		len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3603 
3604 	return len;
3605 }
3606 
3607 
p2p_ie_text(struct wpabuf * p2p_ie,char * buf,char * end)3608 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
3609 {
3610 	return p2p_attr_text(p2p_ie, buf, end);
3611 }
3612 
3613 
p2p_go_neg_req_cb(struct p2p_data * p2p,int success)3614 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
3615 {
3616 	struct p2p_device *dev = p2p->go_neg_peer;
3617 	int timeout;
3618 
3619 	p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
3620 
3621 	if (dev == NULL) {
3622 		p2p_dbg(p2p, "No pending GO Negotiation");
3623 		return;
3624 	}
3625 
3626 	if (success) {
3627 		if (dev->flags & P2P_DEV_USER_REJECTED) {
3628 			p2p_set_state(p2p, P2P_IDLE);
3629 			return;
3630 		}
3631 	} else if (dev->go_neg_req_sent) {
3632 		/* Cancel the increment from p2p_connect_send() on failure */
3633 		dev->go_neg_req_sent--;
3634 	}
3635 
3636 	if (!success &&
3637 	    (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
3638 	    !is_zero_ether_addr(dev->member_in_go_dev)) {
3639 		p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
3640 			MAC2STR(dev->info.p2p_device_addr));
3641 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3642 		p2p_send_dev_disc_req(p2p, dev);
3643 		return;
3644 	}
3645 
3646 	/*
3647 	 * Use P2P find, if needed, to find the other device from its listen
3648 	 * channel.
3649 	 */
3650 	p2p_set_state(p2p, P2P_CONNECT);
3651 	timeout = success ? 500000 : 100000;
3652 	if (!success && p2p->go_neg_peer &&
3653 	    (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
3654 		unsigned int r;
3655 		/*
3656 		 * Peer is expected to wait our response and we will skip the
3657 		 * listen phase. Add some randomness to the wait time here to
3658 		 * make it less likely to hit cases where we could end up in
3659 		 * sync with peer not listening.
3660 		 */
3661 		if (os_get_random((u8 *) &r, sizeof(r)) < 0)
3662 			r = 0;
3663 		timeout += r % 100000;
3664 	}
3665 	p2p_set_timeout(p2p, 0, timeout);
3666 }
3667 
3668 
p2p_go_neg_resp_cb(struct p2p_data * p2p,int success)3669 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
3670 {
3671 	p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
3672 		success);
3673 	if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
3674 		p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
3675 		return;
3676 	}
3677 	p2p_set_state(p2p, P2P_CONNECT);
3678 	p2p_set_timeout(p2p, 0, 500000);
3679 }
3680 
3681 
p2p_go_neg_resp_failure_cb(struct p2p_data * p2p,int success,const u8 * addr)3682 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
3683 				       const u8 *addr)
3684 {
3685 	p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
3686 	if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
3687 		p2p_go_neg_failed(p2p, p2p->go_neg_peer->status);
3688 		return;
3689 	}
3690 
3691 	if (success) {
3692 		struct p2p_device *dev;
3693 		dev = p2p_get_device(p2p, addr);
3694 		if (dev &&
3695 		    dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE)
3696 			dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
3697 	}
3698 
3699 	if (p2p->state == P2P_SEARCH || p2p->state == P2P_SD_DURING_FIND)
3700 		p2p_continue_find(p2p);
3701 }
3702 
3703 
p2p_go_neg_conf_cb(struct p2p_data * p2p,enum p2p_send_action_result result)3704 static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
3705 			       enum p2p_send_action_result result)
3706 {
3707 	struct p2p_device *dev;
3708 
3709 	p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
3710 	if (result == P2P_SEND_ACTION_FAILED) {
3711 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3712 		p2p_go_neg_failed(p2p, -1);
3713 		return;
3714 	}
3715 
3716 	dev = p2p->go_neg_peer;
3717 
3718 	if (result == P2P_SEND_ACTION_NO_ACK) {
3719 		/*
3720 		 * Retry GO Negotiation Confirmation
3721 		 * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive
3722 		 * ACK for confirmation.
3723 		 */
3724 		if (dev && dev->go_neg_conf &&
3725 		    dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) {
3726 			p2p_dbg(p2p, "GO Negotiation Confirm retry %d",
3727 				dev->go_neg_conf_sent);
3728 			p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
3729 			if (p2p_send_action(p2p, dev->go_neg_conf_freq,
3730 					    dev->info.p2p_device_addr,
3731 					    p2p->cfg->dev_addr,
3732 					    dev->info.p2p_device_addr,
3733 					    wpabuf_head(dev->go_neg_conf),
3734 					    wpabuf_len(dev->go_neg_conf), 0) >=
3735 			    0) {
3736 				dev->go_neg_conf_sent++;
3737 				return;
3738 			}
3739 			p2p_dbg(p2p, "Failed to re-send Action frame");
3740 
3741 			/*
3742 			 * Continue with the assumption that the first attempt
3743 			 * went through and just the ACK frame was lost.
3744 			 */
3745 		}
3746 
3747 		/*
3748 		 * It looks like the TX status for GO Negotiation Confirm is
3749 		 * often showing failure even when the peer has actually
3750 		 * received the frame. Since the peer may change channels
3751 		 * immediately after having received the frame, we may not see
3752 		 * an Ack for retries, so just dropping a single frame may
3753 		 * trigger this. To allow the group formation to succeed if the
3754 		 * peer did indeed receive the frame, continue regardless of
3755 		 * the TX status.
3756 		 */
3757 		p2p_dbg(p2p, "Assume GO Negotiation Confirm TX was actually received by the peer even though Ack was not reported");
3758 	}
3759 
3760 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3761 
3762 	if (dev == NULL)
3763 		return;
3764 
3765 	p2p_go_complete(p2p, dev);
3766 }
3767 
3768 
p2p_send_action_cb(struct p2p_data * p2p,unsigned int freq,const u8 * dst,const u8 * src,const u8 * bssid,enum p2p_send_action_result result)3769 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3770 			const u8 *src, const u8 *bssid,
3771 			enum p2p_send_action_result result)
3772 {
3773 	enum p2p_pending_action_state state;
3774 	int success;
3775 
3776 	p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
3777 		" src=" MACSTR " bssid=" MACSTR " result=%d p2p_state=%s)",
3778 		p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
3779 		MAC2STR(bssid), result, p2p_state_txt(p2p->state));
3780 	success = result == P2P_SEND_ACTION_SUCCESS;
3781 	state = p2p->pending_action_state;
3782 	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3783 	switch (state) {
3784 	case P2P_NO_PENDING_ACTION:
3785 		if (p2p->send_action_in_progress) {
3786 			p2p->send_action_in_progress = 0;
3787 			p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3788 		}
3789 		break;
3790 	case P2P_PENDING_GO_NEG_REQUEST:
3791 		p2p_go_neg_req_cb(p2p, success);
3792 		break;
3793 	case P2P_PENDING_GO_NEG_RESPONSE:
3794 		p2p_go_neg_resp_cb(p2p, success);
3795 		break;
3796 	case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
3797 		p2p_go_neg_resp_failure_cb(p2p, success, dst);
3798 		break;
3799 	case P2P_PENDING_GO_NEG_CONFIRM:
3800 		p2p_go_neg_conf_cb(p2p, result);
3801 		break;
3802 	case P2P_PENDING_SD:
3803 		p2p_sd_cb(p2p, success);
3804 		break;
3805 	case P2P_PENDING_PD:
3806 		p2p_prov_disc_cb(p2p, success);
3807 		break;
3808 	case P2P_PENDING_PD_RESPONSE:
3809 		p2p_prov_disc_resp_cb(p2p, success);
3810 		break;
3811 	case P2P_PENDING_INVITATION_REQUEST:
3812 		p2p_invitation_req_cb(p2p, success);
3813 		break;
3814 	case P2P_PENDING_INVITATION_RESPONSE:
3815 		p2p_invitation_resp_cb(p2p, success);
3816 		break;
3817 	case P2P_PENDING_DEV_DISC_REQUEST:
3818 		p2p_dev_disc_req_cb(p2p, success);
3819 		break;
3820 	case P2P_PENDING_DEV_DISC_RESPONSE:
3821 		p2p_dev_disc_resp_cb(p2p, success);
3822 		break;
3823 	case P2P_PENDING_GO_DISC_REQ:
3824 		p2p_go_disc_req_cb(p2p, success);
3825 		break;
3826 	}
3827 }
3828 
3829 
p2p_listen_cb(struct p2p_data * p2p,unsigned int freq,unsigned int duration)3830 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3831 		   unsigned int duration)
3832 {
3833 	if (freq == p2p->pending_client_disc_freq) {
3834 		p2p_dbg(p2p, "Client discoverability remain-awake completed");
3835 		p2p->pending_client_disc_freq = 0;
3836 		return;
3837 	}
3838 
3839 	if (freq != p2p->pending_listen_freq) {
3840 		p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
3841 			freq, duration, p2p->pending_listen_freq);
3842 		return;
3843 	}
3844 
3845 	p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
3846 		p2p->pending_listen_sec, p2p->pending_listen_usec,
3847 		p2p->pending_listen_freq);
3848 	p2p->in_listen = 1;
3849 	p2p->drv_in_listen = freq;
3850 	if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3851 		/*
3852 		 * Add 20 msec extra wait to avoid race condition with driver
3853 		 * remain-on-channel end event, i.e., give driver more time to
3854 		 * complete the operation before our timeout expires.
3855 		 */
3856 		p2p_set_timeout(p2p, p2p->pending_listen_sec,
3857 				p2p->pending_listen_usec + 20000);
3858 	}
3859 
3860 	p2p->pending_listen_freq = 0;
3861 }
3862 
3863 
p2p_listen_end(struct p2p_data * p2p,unsigned int freq)3864 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3865 {
3866 	p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
3867 	p2p->drv_in_listen = 0;
3868 	if (p2p->in_listen)
3869 		return 0; /* Internal timeout will trigger the next step */
3870 
3871 	if (p2p->state == P2P_WAIT_PEER_CONNECT && p2p->go_neg_peer &&
3872 	    p2p->pending_listen_freq) {
3873 		/*
3874 		 * Better wait a bit if the driver is unable to start
3875 		 * offchannel operation for some reason to continue with
3876 		 * P2P_WAIT_PEER_(IDLE/CONNECT) state transitions.
3877 		 */
3878 		p2p_dbg(p2p,
3879 			"Listen operation did not seem to start - delay idle phase to avoid busy loop");
3880 		p2p_set_timeout(p2p, 0, 100000);
3881 		return 1;
3882 	}
3883 
3884 	if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3885 		if (p2p->go_neg_peer->connect_reqs >= 120) {
3886 			p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
3887 			p2p_go_neg_failed(p2p, -1);
3888 			return 0;
3889 		}
3890 
3891 		p2p_set_state(p2p, P2P_CONNECT);
3892 		p2p_connect_send(p2p, p2p->go_neg_peer);
3893 		return 1;
3894 	} else if (p2p->state == P2P_SEARCH) {
3895 		if (p2p->p2p_scan_running) {
3896 			 /*
3897 			  * Search is already in progress. This can happen if
3898 			  * an Action frame RX is reported immediately after
3899 			  * the end of a remain-on-channel operation and the
3900 			  * response frame to that is sent using an offchannel
3901 			  * operation while in p2p_find. Avoid an attempt to
3902 			  * restart a scan here.
3903 			  */
3904 			p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
3905 			return 1;
3906 		}
3907 		if (p2p->pending_listen_freq) {
3908 			/*
3909 			 * Better wait a bit if the driver is unable to start
3910 			 * offchannel operation for some reason. p2p_search()
3911 			 * will be started from internal timeout.
3912 			 */
3913 			p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
3914 			p2p_set_timeout(p2p, 0, 100000);
3915 			return 1;
3916 		}
3917 		if (p2p->search_delay) {
3918 			p2p_dbg(p2p, "Delay search operation by %u ms",
3919 				p2p->search_delay);
3920 			p2p_set_timeout(p2p, p2p->search_delay / 1000,
3921 					(p2p->search_delay % 1000) * 1000);
3922 			return 1;
3923 		}
3924 		p2p_search(p2p);
3925 		return 1;
3926 	}
3927 
3928 	return 0;
3929 }
3930 
3931 
p2p_timeout_connect(struct p2p_data * p2p)3932 static void p2p_timeout_connect(struct p2p_data *p2p)
3933 {
3934 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3935 	if (p2p->go_neg_peer &&
3936 	    (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
3937 		p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
3938 		p2p_go_neg_failed(p2p, -1);
3939 		return;
3940 	}
3941 	if (p2p->go_neg_peer &&
3942 	    (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
3943 	    p2p->go_neg_peer->connect_reqs < 120) {
3944 		p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
3945 		p2p_connect_send(p2p, p2p->go_neg_peer);
3946 		return;
3947 	}
3948 	if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
3949 		p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
3950 		p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3951 		p2p_set_timeout(p2p, 0, 30000);
3952 		return;
3953 	}
3954 	p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3955 	p2p_listen_in_find(p2p, 0);
3956 }
3957 
3958 
p2p_timeout_connect_listen(struct p2p_data * p2p)3959 static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3960 {
3961 	if (p2p->go_neg_peer) {
3962 		if (p2p->drv_in_listen) {
3963 			p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
3964 			return;
3965 		}
3966 
3967 		if (p2p->go_neg_peer->connect_reqs >= 120) {
3968 			p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
3969 			p2p_go_neg_failed(p2p, -1);
3970 			return;
3971 		}
3972 
3973 		p2p_set_state(p2p, P2P_CONNECT);
3974 		p2p_connect_send(p2p, p2p->go_neg_peer);
3975 	} else
3976 		p2p_set_state(p2p, P2P_IDLE);
3977 }
3978 
3979 
p2p_timeout_wait_peer_connect(struct p2p_data * p2p)3980 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3981 {
3982 	p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
3983 
3984 	if (p2p->cfg->is_concurrent_session_active &&
3985 	    p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
3986 		p2p_set_timeout(p2p, 0, 500000);
3987 	else
3988 		p2p_set_timeout(p2p, 0, 200000);
3989 }
3990 
3991 
p2p_timeout_wait_peer_idle(struct p2p_data * p2p)3992 static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3993 {
3994 	struct p2p_device *dev = p2p->go_neg_peer;
3995 
3996 	if (dev == NULL) {
3997 		p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
3998 		return;
3999 	}
4000 
4001 	p2p_dbg(p2p, "Go to Listen state while waiting for the peer to become ready for GO Negotiation");
4002 	p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
4003 	p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
4004 	p2p_listen_in_find(p2p, 0);
4005 }
4006 
4007 
p2p_timeout_sd_during_find(struct p2p_data * p2p)4008 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
4009 {
4010 	p2p_dbg(p2p, "Service Discovery Query timeout");
4011 	if (p2p->sd_peer) {
4012 		p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4013 		p2p->sd_peer = NULL;
4014 	}
4015 	p2p_continue_find(p2p);
4016 }
4017 
4018 
p2p_timeout_prov_disc_during_find(struct p2p_data * p2p)4019 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
4020 {
4021 	p2p_dbg(p2p, "Provision Discovery Request timeout");
4022 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4023 	p2p_continue_find(p2p);
4024 }
4025 
4026 
p2p_timeout_prov_disc_req(struct p2p_data * p2p)4027 static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
4028 {
4029 	u32 adv_id = 0;
4030 	u8 *adv_mac = NULL;
4031 
4032 	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4033 
4034 	/*
4035 	 * For user initiated PD requests that we have not gotten any responses
4036 	 * for while in IDLE state, we retry them a couple of times before
4037 	 * giving up.
4038 	 */
4039 	if (!p2p->user_initiated_pd)
4040 		return;
4041 
4042 	p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
4043 
4044 	if (p2p->pd_retries) {
4045 		p2p->pd_retries--;
4046 		p2p_retry_pd(p2p);
4047 	} else {
4048 		struct p2p_device *dev;
4049 		int for_join = 0;
4050 
4051 		dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
4052 			if (os_memcmp(p2p->pending_pd_devaddr,
4053 				      dev->info.p2p_device_addr, ETH_ALEN) != 0)
4054 				continue;
4055 			if (dev->req_config_methods &&
4056 			    (dev->flags & P2P_DEV_PD_FOR_JOIN))
4057 				for_join = 1;
4058 		}
4059 
4060 		if (p2p->p2ps_prov) {
4061 			adv_id = p2p->p2ps_prov->adv_id;
4062 			adv_mac = p2p->p2ps_prov->adv_mac;
4063 		}
4064 
4065 		if (p2p->cfg->prov_disc_fail)
4066 			p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
4067 						 p2p->pending_pd_devaddr,
4068 						 for_join ?
4069 						 P2P_PROV_DISC_TIMEOUT_JOIN :
4070 						 P2P_PROV_DISC_TIMEOUT,
4071 						 adv_id, adv_mac, NULL);
4072 		p2p_reset_pending_pd(p2p);
4073 	}
4074 }
4075 
4076 
p2p_timeout_invite(struct p2p_data * p2p)4077 static void p2p_timeout_invite(struct p2p_data *p2p)
4078 {
4079 	p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
4080 	p2p_set_state(p2p, P2P_INVITE_LISTEN);
4081 	if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
4082 		/*
4083 		 * Better remain on operating channel instead of listen channel
4084 		 * when running a group.
4085 		 */
4086 		p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
4087 		p2p_set_timeout(p2p, 0, 100000);
4088 		return;
4089 	}
4090 	p2p_listen_in_find(p2p, 0);
4091 }
4092 
4093 
p2p_timeout_invite_listen(struct p2p_data * p2p)4094 static void p2p_timeout_invite_listen(struct p2p_data *p2p)
4095 {
4096 	if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
4097 		p2p_set_state(p2p, P2P_INVITE);
4098 		p2p_invite_send(p2p, p2p->invite_peer,
4099 				p2p->invite_go_dev_addr, p2p->invite_dev_pw_id);
4100 	} else {
4101 		if (p2p->invite_peer) {
4102 			p2p_dbg(p2p, "Invitation Request retry limit reached");
4103 			if (p2p->cfg->invitation_result)
4104 				p2p->cfg->invitation_result(
4105 					p2p->cfg->cb_ctx, -1, NULL, NULL,
4106 					p2p->invite_peer->info.p2p_device_addr,
4107 					0, 0);
4108 		}
4109 		p2p_set_state(p2p, P2P_IDLE);
4110 	}
4111 }
4112 
4113 
p2p_state_timeout(void * eloop_ctx,void * timeout_ctx)4114 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
4115 {
4116 	struct p2p_data *p2p = eloop_ctx;
4117 
4118 	p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
4119 
4120 	p2p->in_listen = 0;
4121 	if (p2p->drv_in_listen) {
4122 		p2p_dbg(p2p, "Driver is still in listen state - stop it");
4123 		p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
4124 	}
4125 
4126 	switch (p2p->state) {
4127 	case P2P_IDLE:
4128 		/* Check if we timed out waiting for PD req */
4129 		if (p2p->pending_action_state == P2P_PENDING_PD)
4130 			p2p_timeout_prov_disc_req(p2p);
4131 		break;
4132 	case P2P_SEARCH:
4133 		/* Check if we timed out waiting for PD req */
4134 		if (p2p->pending_action_state == P2P_PENDING_PD)
4135 			p2p_timeout_prov_disc_req(p2p);
4136 		if (p2p->search_delay && !p2p->in_search_delay) {
4137 			p2p_dbg(p2p, "Delay search operation by %u ms",
4138 				p2p->search_delay);
4139 			p2p->in_search_delay = 1;
4140 			p2p_set_timeout(p2p, p2p->search_delay / 1000,
4141 					(p2p->search_delay % 1000) * 1000);
4142 			break;
4143 		}
4144 		p2p->in_search_delay = 0;
4145 		p2p_search(p2p);
4146 		break;
4147 	case P2P_CONNECT:
4148 		p2p_timeout_connect(p2p);
4149 		break;
4150 	case P2P_CONNECT_LISTEN:
4151 		p2p_timeout_connect_listen(p2p);
4152 		break;
4153 	case P2P_GO_NEG:
4154 		break;
4155 	case P2P_LISTEN_ONLY:
4156 		/* Check if we timed out waiting for PD req */
4157 		if (p2p->pending_action_state == P2P_PENDING_PD)
4158 			p2p_timeout_prov_disc_req(p2p);
4159 
4160 		if (p2p->ext_listen_only) {
4161 			p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
4162 			p2p->ext_listen_only = 0;
4163 			p2p_set_state(p2p, P2P_IDLE);
4164 		}
4165 		break;
4166 	case P2P_WAIT_PEER_CONNECT:
4167 		p2p_timeout_wait_peer_connect(p2p);
4168 		break;
4169 	case P2P_WAIT_PEER_IDLE:
4170 		p2p_timeout_wait_peer_idle(p2p);
4171 		break;
4172 	case P2P_SD_DURING_FIND:
4173 		p2p_timeout_sd_during_find(p2p);
4174 		break;
4175 	case P2P_PROVISIONING:
4176 		break;
4177 	case P2P_PD_DURING_FIND:
4178 		p2p_timeout_prov_disc_during_find(p2p);
4179 		break;
4180 	case P2P_INVITE:
4181 		p2p_timeout_invite(p2p);
4182 		break;
4183 	case P2P_INVITE_LISTEN:
4184 		p2p_timeout_invite_listen(p2p);
4185 		break;
4186 	}
4187 }
4188 
4189 
p2p_reject(struct p2p_data * p2p,const u8 * peer_addr)4190 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
4191 {
4192 	struct p2p_device *dev;
4193 
4194 	dev = p2p_get_device(p2p, peer_addr);
4195 	p2p_dbg(p2p, "Local request to reject connection attempts by peer "
4196 		MACSTR, MAC2STR(peer_addr));
4197 	if (dev == NULL) {
4198 		p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
4199 		return -1;
4200 	}
4201 	dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
4202 	dev->flags |= P2P_DEV_USER_REJECTED;
4203 	return 0;
4204 }
4205 
4206 
p2p_wps_method_text(enum p2p_wps_method method)4207 const char * p2p_wps_method_text(enum p2p_wps_method method)
4208 {
4209 	switch (method) {
4210 	case WPS_NOT_READY:
4211 		return "not-ready";
4212 	case WPS_PIN_DISPLAY:
4213 		return "Display";
4214 	case WPS_PIN_KEYPAD:
4215 		return "Keypad";
4216 	case WPS_PBC:
4217 		return "PBC";
4218 	case WPS_NFC:
4219 		return "NFC";
4220 	case WPS_P2PS:
4221 		return "P2PS";
4222 	}
4223 
4224 	return "??";
4225 }
4226 
4227 
p2p_go_state_text(enum p2p_go_state go_state)4228 static const char * p2p_go_state_text(enum p2p_go_state go_state)
4229 {
4230 	switch (go_state) {
4231 	case UNKNOWN_GO:
4232 		return "unknown";
4233 	case LOCAL_GO:
4234 		return "local";
4235 	case  REMOTE_GO:
4236 		return "remote";
4237 	}
4238 
4239 	return "??";
4240 }
4241 
4242 
p2p_get_peer_info(struct p2p_data * p2p,const u8 * addr,int next)4243 const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
4244 					       const u8 *addr, int next)
4245 {
4246 	struct p2p_device *dev;
4247 
4248 	if (addr)
4249 		dev = p2p_get_device(p2p, addr);
4250 	else
4251 		dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4252 
4253 	if (dev && next) {
4254 		dev = dl_list_first(&dev->list, struct p2p_device, list);
4255 		if (&dev->list == &p2p->devices)
4256 			dev = NULL;
4257 	}
4258 
4259 	if (dev == NULL)
4260 		return NULL;
4261 
4262 	return &dev->info;
4263 }
4264 
4265 
p2p_get_peer_info_txt(const struct p2p_peer_info * info,char * buf,size_t buflen)4266 int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
4267 			  char *buf, size_t buflen)
4268 {
4269 	struct p2p_device *dev;
4270 	int res;
4271 	char *pos, *end;
4272 	struct os_reltime now;
4273 
4274 	if (info == NULL)
4275 		return -1;
4276 
4277 	dev = (struct p2p_device *) (((u8 *) info) -
4278 				     offsetof(struct p2p_device, info));
4279 
4280 	pos = buf;
4281 	end = buf + buflen;
4282 
4283 	os_get_reltime(&now);
4284 	res = os_snprintf(pos, end - pos,
4285 			  "age=%d\n"
4286 			  "listen_freq=%d\n"
4287 			  "wps_method=%s\n"
4288 			  "interface_addr=" MACSTR "\n"
4289 			  "member_in_go_dev=" MACSTR "\n"
4290 			  "member_in_go_iface=" MACSTR "\n"
4291 			  "go_neg_req_sent=%d\n"
4292 			  "go_state=%s\n"
4293 			  "dialog_token=%u\n"
4294 			  "intended_addr=" MACSTR "\n"
4295 			  "country=%c%c\n"
4296 			  "oper_freq=%d\n"
4297 			  "req_config_methods=0x%x\n"
4298 			  "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
4299 			  "status=%d\n"
4300 			  "invitation_reqs=%u\n",
4301 			  (int) (now.sec - dev->last_seen.sec),
4302 			  dev->listen_freq,
4303 			  p2p_wps_method_text(dev->wps_method),
4304 			  MAC2STR(dev->interface_addr),
4305 			  MAC2STR(dev->member_in_go_dev),
4306 			  MAC2STR(dev->member_in_go_iface),
4307 			  dev->go_neg_req_sent,
4308 			  p2p_go_state_text(dev->go_state),
4309 			  dev->dialog_token,
4310 			  MAC2STR(dev->intended_addr),
4311 			  dev->country[0] ? dev->country[0] : '_',
4312 			  dev->country[1] ? dev->country[1] : '_',
4313 			  dev->oper_freq,
4314 			  dev->req_config_methods,
4315 			  dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
4316 			  "[PROBE_REQ_ONLY]" : "",
4317 			  dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
4318 			  dev->flags & P2P_DEV_NOT_YET_READY ?
4319 			  "[NOT_YET_READY]" : "",
4320 			  dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
4321 			  "[PD_PEER_DISPLAY]" : "",
4322 			  dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
4323 			  "[PD_PEER_KEYPAD]" : "",
4324 			  dev->flags & P2P_DEV_PD_PEER_P2PS ?
4325 			  "[PD_PEER_P2PS]" : "",
4326 			  dev->flags & P2P_DEV_USER_REJECTED ?
4327 			  "[USER_REJECTED]" : "",
4328 			  dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
4329 			  "[PEER_WAITING_RESPONSE]" : "",
4330 			  dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
4331 			  "[PREFER_PERSISTENT_GROUP]" : "",
4332 			  dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
4333 			  "[WAIT_GO_NEG_RESPONSE]" : "",
4334 			  dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
4335 			  "[WAIT_GO_NEG_CONFIRM]" : "",
4336 			  dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
4337 			  "[GROUP_CLIENT_ONLY]" : "",
4338 			  dev->flags & P2P_DEV_FORCE_FREQ ?
4339 			  "[FORCE_FREQ]" : "",
4340 			  dev->flags & P2P_DEV_PD_FOR_JOIN ?
4341 			  "[PD_FOR_JOIN]" : "",
4342 			  dev->flags & P2P_DEV_LAST_SEEN_AS_GROUP_CLIENT ?
4343 			  "[LAST_SEEN_AS_GROUP_CLIENT]" : "",
4344 			  dev->status,
4345 			  dev->invitation_reqs);
4346 	if (os_snprintf_error(end - pos, res))
4347 		return pos - buf;
4348 	pos += res;
4349 
4350 	if (dev->ext_listen_period) {
4351 		res = os_snprintf(pos, end - pos,
4352 				  "ext_listen_period=%u\n"
4353 				  "ext_listen_interval=%u\n",
4354 				  dev->ext_listen_period,
4355 				  dev->ext_listen_interval);
4356 		if (os_snprintf_error(end - pos, res))
4357 			return pos - buf;
4358 		pos += res;
4359 	}
4360 
4361 	if (dev->oper_ssid_len) {
4362 		res = os_snprintf(pos, end - pos,
4363 				  "oper_ssid=%s\n",
4364 				  wpa_ssid_txt(dev->oper_ssid,
4365 					       dev->oper_ssid_len));
4366 		if (os_snprintf_error(end - pos, res))
4367 			return pos - buf;
4368 		pos += res;
4369 	}
4370 
4371 #ifdef CONFIG_WIFI_DISPLAY
4372 	if (dev->info.wfd_subelems) {
4373 		res = os_snprintf(pos, end - pos, "wfd_subelems=");
4374 		if (os_snprintf_error(end - pos, res))
4375 			return pos - buf;
4376 		pos += res;
4377 
4378 		pos += wpa_snprintf_hex(pos, end - pos,
4379 					wpabuf_head(dev->info.wfd_subelems),
4380 					wpabuf_len(dev->info.wfd_subelems));
4381 
4382 		res = os_snprintf(pos, end - pos, "\n");
4383 		if (os_snprintf_error(end - pos, res))
4384 			return pos - buf;
4385 		pos += res;
4386 	}
4387 #endif /* CONFIG_WIFI_DISPLAY */
4388 
4389 	return pos - buf;
4390 }
4391 
4392 
p2p_peer_known(struct p2p_data * p2p,const u8 * addr)4393 int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
4394 {
4395 	return p2p_get_device(p2p, addr) != NULL;
4396 }
4397 
4398 
p2p_set_client_discoverability(struct p2p_data * p2p,int enabled)4399 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
4400 {
4401 	if (enabled) {
4402 		p2p_dbg(p2p, "Client discoverability enabled");
4403 		p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4404 	} else {
4405 		p2p_dbg(p2p, "Client discoverability disabled");
4406 		p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
4407 	}
4408 }
4409 
4410 
p2p_build_presence_req(u32 duration1,u32 interval1,u32 duration2,u32 interval2)4411 static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
4412 					      u32 duration2, u32 interval2)
4413 {
4414 	struct wpabuf *req;
4415 	struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
4416 	u8 *len;
4417 
4418 	req = wpabuf_alloc(100);
4419 	if (req == NULL)
4420 		return NULL;
4421 
4422 	if (duration1 || interval1) {
4423 		os_memset(&desc1, 0, sizeof(desc1));
4424 		desc1.count_type = 1;
4425 		desc1.duration = duration1;
4426 		desc1.interval = interval1;
4427 		ptr1 = &desc1;
4428 
4429 		if (duration2 || interval2) {
4430 			os_memset(&desc2, 0, sizeof(desc2));
4431 			desc2.count_type = 2;
4432 			desc2.duration = duration2;
4433 			desc2.interval = interval2;
4434 			ptr2 = &desc2;
4435 		}
4436 	}
4437 
4438 	p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
4439 	len = p2p_buf_add_ie_hdr(req);
4440 	p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
4441 	p2p_buf_update_ie_hdr(req, len);
4442 
4443 	return req;
4444 }
4445 
4446 
p2p_presence_req(struct p2p_data * p2p,const u8 * go_interface_addr,const u8 * own_interface_addr,unsigned int freq,u32 duration1,u32 interval1,u32 duration2,u32 interval2)4447 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
4448 		     const u8 *own_interface_addr, unsigned int freq,
4449 		     u32 duration1, u32 interval1, u32 duration2,
4450 		     u32 interval2)
4451 {
4452 	struct wpabuf *req;
4453 
4454 	p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
4455 		" (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
4456 		"dur2=%u int2=%u",
4457 		MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
4458 		freq, duration1, interval1, duration2, interval2);
4459 
4460 	req = p2p_build_presence_req(duration1, interval1, duration2,
4461 				     interval2);
4462 	if (req == NULL)
4463 		return -1;
4464 
4465 	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4466 	if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
4467 			    go_interface_addr,
4468 			    wpabuf_head(req), wpabuf_len(req), 200) < 0) {
4469 		p2p_dbg(p2p, "Failed to send Action frame");
4470 	}
4471 	wpabuf_free(req);
4472 
4473 	return 0;
4474 }
4475 
4476 
p2p_build_presence_resp(u8 status,const u8 * noa,size_t noa_len,u8 dialog_token)4477 static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
4478 					       size_t noa_len, u8 dialog_token)
4479 {
4480 	struct wpabuf *resp;
4481 	u8 *len;
4482 
4483 	resp = wpabuf_alloc(100 + noa_len);
4484 	if (resp == NULL)
4485 		return NULL;
4486 
4487 	p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
4488 	len = p2p_buf_add_ie_hdr(resp);
4489 	p2p_buf_add_status(resp, status);
4490 	if (noa) {
4491 		wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
4492 		wpabuf_put_le16(resp, noa_len);
4493 		wpabuf_put_data(resp, noa, noa_len);
4494 	} else
4495 		p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
4496 	p2p_buf_update_ie_hdr(resp, len);
4497 
4498 	return resp;
4499 }
4500 
4501 
p2p_process_presence_req(struct p2p_data * p2p,const u8 * da,const u8 * sa,const u8 * data,size_t len,int rx_freq)4502 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
4503 				     const u8 *sa, const u8 *data, size_t len,
4504 				     int rx_freq)
4505 {
4506 	struct p2p_message msg;
4507 	u8 status;
4508 	struct wpabuf *resp;
4509 	size_t g;
4510 	struct p2p_group *group = NULL;
4511 	int parsed = 0;
4512 	u8 noa[50];
4513 	int noa_len;
4514 
4515 	p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
4516 
4517 	for (g = 0; g < p2p->num_groups; g++) {
4518 		if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
4519 			      ETH_ALEN) == 0) {
4520 			group = p2p->groups[g];
4521 			break;
4522 		}
4523 	}
4524 	if (group == NULL) {
4525 		p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
4526 			MACSTR, MAC2STR(da));
4527 		return;
4528 	}
4529 
4530 	if (p2p_parse(data, len, &msg) < 0) {
4531 		p2p_dbg(p2p, "Failed to parse P2P Presence Request");
4532 		status = P2P_SC_FAIL_INVALID_PARAMS;
4533 		goto fail;
4534 	}
4535 	parsed = 1;
4536 
4537 	if (msg.noa == NULL) {
4538 		p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
4539 		status = P2P_SC_FAIL_INVALID_PARAMS;
4540 		goto fail;
4541 	}
4542 
4543 	status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
4544 
4545 fail:
4546 	if (p2p->cfg->get_noa)
4547 		noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
4548 					    sizeof(noa));
4549 	else
4550 		noa_len = -1;
4551 	resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
4552 				       noa_len > 0 ? noa_len : 0,
4553 				       msg.dialog_token);
4554 	if (parsed)
4555 		p2p_parse_free(&msg);
4556 	if (resp == NULL)
4557 		return;
4558 
4559 	p2p->pending_action_state = P2P_NO_PENDING_ACTION;
4560 	if (p2p_send_action(p2p, rx_freq, sa, da, da,
4561 			    wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
4562 		p2p_dbg(p2p, "Failed to send Action frame");
4563 	}
4564 	wpabuf_free(resp);
4565 }
4566 
4567 
p2p_process_presence_resp(struct p2p_data * p2p,const u8 * da,const u8 * sa,const u8 * data,size_t len)4568 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
4569 				      const u8 *sa, const u8 *data, size_t len)
4570 {
4571 	struct p2p_message msg;
4572 
4573 	p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
4574 
4575 	if (p2p_parse(data, len, &msg) < 0) {
4576 		p2p_dbg(p2p, "Failed to parse P2P Presence Response");
4577 		return;
4578 	}
4579 
4580 	if (msg.status == NULL || msg.noa == NULL) {
4581 		p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
4582 		p2p_parse_free(&msg);
4583 		return;
4584 	}
4585 
4586 	if (p2p->cfg->presence_resp) {
4587 		p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
4588 					msg.noa, msg.noa_len);
4589 	}
4590 
4591 	if (*msg.status) {
4592 		p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
4593 			*msg.status);
4594 		p2p_parse_free(&msg);
4595 		return;
4596 	}
4597 
4598 	p2p_dbg(p2p, "P2P Presence Request was accepted");
4599 	wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
4600 		    msg.noa, msg.noa_len);
4601 	/* TODO: process NoA */
4602 	p2p_parse_free(&msg);
4603 }
4604 
4605 
p2p_ext_listen_timeout(void * eloop_ctx,void * timeout_ctx)4606 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4607 {
4608 	struct p2p_data *p2p = eloop_ctx;
4609 
4610 	if (p2p->ext_listen_interval) {
4611 		/* Schedule next extended listen timeout */
4612 		eloop_register_timeout(p2p->ext_listen_interval_sec,
4613 				       p2p->ext_listen_interval_usec,
4614 				       p2p_ext_listen_timeout, p2p, NULL);
4615 	}
4616 
4617 	if ((p2p->cfg->is_p2p_in_progress &&
4618 	     p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) ||
4619 	    (p2p->pending_action_state == P2P_PENDING_PD &&
4620 	     p2p->pd_retries > 0)) {
4621 		p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)",
4622 			p2p_state_txt(p2p->state));
4623 		return;
4624 	}
4625 
4626 	if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
4627 		/*
4628 		 * This should not really happen, but it looks like the Listen
4629 		 * command may fail is something else (e.g., a scan) was
4630 		 * running at an inconvenient time. As a workaround, allow new
4631 		 * Extended Listen operation to be started.
4632 		 */
4633 		p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
4634 		p2p->ext_listen_only = 0;
4635 		p2p_set_state(p2p, P2P_IDLE);
4636 	}
4637 
4638 	if (p2p->state != P2P_IDLE) {
4639 		p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
4640 		return;
4641 	}
4642 
4643 	p2p_dbg(p2p, "Extended Listen timeout");
4644 	p2p->ext_listen_only = 1;
4645 	if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
4646 		p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
4647 		p2p->ext_listen_only = 0;
4648 	}
4649 }
4650 
4651 
p2p_ext_listen(struct p2p_data * p2p,unsigned int period,unsigned int interval)4652 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
4653 		   unsigned int interval)
4654 {
4655 	if (period > 65535 || interval > 65535 || period > interval ||
4656 	    (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
4657 		p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
4658 			period, interval);
4659 		return -1;
4660 	}
4661 
4662 	eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
4663 
4664 	if (interval == 0) {
4665 		p2p_dbg(p2p, "Disabling Extended Listen Timing");
4666 		p2p->ext_listen_period = 0;
4667 		p2p->ext_listen_interval = 0;
4668 		return 0;
4669 	}
4670 
4671 	p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
4672 		period, interval);
4673 	p2p->ext_listen_period = period;
4674 	p2p->ext_listen_interval = interval;
4675 	p2p->ext_listen_interval_sec = interval / 1000;
4676 	p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
4677 
4678 	eloop_register_timeout(p2p->ext_listen_interval_sec,
4679 			       p2p->ext_listen_interval_usec,
4680 			       p2p_ext_listen_timeout, p2p, NULL);
4681 
4682 	return 0;
4683 }
4684 
4685 
p2p_deauth_notif(struct p2p_data * p2p,const u8 * bssid,u16 reason_code,const u8 * ie,size_t ie_len)4686 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4687 		      const u8 *ie, size_t ie_len)
4688 {
4689 	struct p2p_message msg;
4690 
4691 	if (bssid == NULL || ie == NULL)
4692 		return;
4693 
4694 	os_memset(&msg, 0, sizeof(msg));
4695 	if (p2p_parse_ies(ie, ie_len, &msg))
4696 		return;
4697 	if (msg.minor_reason_code == NULL) {
4698 		p2p_parse_free(&msg);
4699 		return;
4700 	}
4701 
4702 	p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
4703 		" reason_code=%u minor_reason_code=%u",
4704 		MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4705 
4706 	p2p_parse_free(&msg);
4707 }
4708 
4709 
p2p_disassoc_notif(struct p2p_data * p2p,const u8 * bssid,u16 reason_code,const u8 * ie,size_t ie_len)4710 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4711 			const u8 *ie, size_t ie_len)
4712 {
4713 	struct p2p_message msg;
4714 
4715 	if (bssid == NULL || ie == NULL)
4716 		return;
4717 
4718 	os_memset(&msg, 0, sizeof(msg));
4719 	if (p2p_parse_ies(ie, ie_len, &msg))
4720 		return;
4721 	if (msg.minor_reason_code == NULL) {
4722 		p2p_parse_free(&msg);
4723 		return;
4724 	}
4725 
4726 	p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
4727 		" reason_code=%u minor_reason_code=%u",
4728 		MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4729 
4730 	p2p_parse_free(&msg);
4731 }
4732 
4733 
p2p_set_managed_oper(struct p2p_data * p2p,int enabled)4734 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
4735 {
4736 	if (enabled) {
4737 		p2p_dbg(p2p, "Managed P2P Device operations enabled");
4738 		p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
4739 	} else {
4740 		p2p_dbg(p2p, "Managed P2P Device operations disabled");
4741 		p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
4742 	}
4743 }
4744 
4745 
p2p_config_get_random_social(struct p2p_config * p2p,u8 * op_class,u8 * op_channel,struct wpa_freq_range_list * avoid_list,struct wpa_freq_range_list * disallow_list)4746 int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class,
4747 				 u8 *op_channel,
4748 				 struct wpa_freq_range_list *avoid_list,
4749 				 struct wpa_freq_range_list *disallow_list)
4750 {
4751 	return p2p_channel_random_social(&p2p->channels, op_class, op_channel,
4752 					 avoid_list, disallow_list);
4753 }
4754 
4755 
p2p_set_listen_channel(struct p2p_data * p2p,u8 reg_class,u8 channel,u8 forced)4756 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel,
4757 			   u8 forced)
4758 {
4759 	if (p2p_channel_to_freq(reg_class, channel) < 0)
4760 		return -1;
4761 
4762 	/*
4763 	 * Listen channel was set in configuration or set by control interface;
4764 	 * cannot override it.
4765 	 */
4766 	if (p2p->cfg->channel_forced && forced == 0) {
4767 		p2p_dbg(p2p,
4768 			"Listen channel was previously configured - do not override based on optimization");
4769 		return -1;
4770 	}
4771 
4772 	p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
4773 		reg_class, channel);
4774 
4775 	if (p2p->state == P2P_IDLE) {
4776 		p2p->cfg->reg_class = reg_class;
4777 		p2p->cfg->channel = channel;
4778 		p2p->cfg->channel_forced = forced;
4779 	} else {
4780 		p2p_dbg(p2p, "Defer setting listen channel");
4781 		p2p->pending_reg_class = reg_class;
4782 		p2p->pending_channel = channel;
4783 		p2p->pending_channel_forced = forced;
4784 	}
4785 
4786 	return 0;
4787 }
4788 
4789 
p2p_get_listen_channel(struct p2p_data * p2p)4790 u8 p2p_get_listen_channel(struct p2p_data *p2p)
4791 {
4792 	return p2p->cfg->channel;
4793 }
4794 
4795 
p2p_set_ssid_postfix(struct p2p_data * p2p,const u8 * postfix,size_t len)4796 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
4797 {
4798 	p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
4799 	if (postfix == NULL) {
4800 		p2p->cfg->ssid_postfix_len = 0;
4801 		return 0;
4802 	}
4803 	if (len > sizeof(p2p->cfg->ssid_postfix))
4804 		return -1;
4805 	os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4806 	p2p->cfg->ssid_postfix_len = len;
4807 	return 0;
4808 }
4809 
4810 
p2p_set_oper_channel(struct p2p_data * p2p,u8 op_reg_class,u8 op_channel,int cfg_op_channel)4811 int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4812 			 int cfg_op_channel)
4813 {
4814 	if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
4815 		return -1;
4816 
4817 	p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
4818 		op_reg_class, op_channel);
4819 	p2p->cfg->op_reg_class = op_reg_class;
4820 	p2p->cfg->op_channel = op_channel;
4821 	p2p->cfg->cfg_op_channel = cfg_op_channel;
4822 	return 0;
4823 }
4824 
4825 
p2p_set_pref_chan(struct p2p_data * p2p,unsigned int num_pref_chan,const struct p2p_channel * pref_chan)4826 int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4827 		      const struct p2p_channel *pref_chan)
4828 {
4829 	struct p2p_channel *n;
4830 
4831 	if (pref_chan) {
4832 		n = os_memdup(pref_chan,
4833 			      num_pref_chan * sizeof(struct p2p_channel));
4834 		if (n == NULL)
4835 			return -1;
4836 	} else
4837 		n = NULL;
4838 
4839 	os_free(p2p->cfg->pref_chan);
4840 	p2p->cfg->pref_chan = n;
4841 	p2p->cfg->num_pref_chan = num_pref_chan;
4842 
4843 	return 0;
4844 }
4845 
4846 
p2p_set_no_go_freq(struct p2p_data * p2p,const struct wpa_freq_range_list * list)4847 int p2p_set_no_go_freq(struct p2p_data *p2p,
4848 		       const struct wpa_freq_range_list *list)
4849 {
4850 	struct wpa_freq_range *tmp;
4851 
4852 	if (list == NULL || list->num == 0) {
4853 		os_free(p2p->no_go_freq.range);
4854 		p2p->no_go_freq.range = NULL;
4855 		p2p->no_go_freq.num = 0;
4856 		return 0;
4857 	}
4858 
4859 	tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
4860 	if (tmp == NULL)
4861 		return -1;
4862 	os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
4863 	os_free(p2p->no_go_freq.range);
4864 	p2p->no_go_freq.range = tmp;
4865 	p2p->no_go_freq.num = list->num;
4866 	p2p_dbg(p2p, "Updated no GO chan list");
4867 
4868 	return 0;
4869 }
4870 
4871 
p2p_get_interface_addr(struct p2p_data * p2p,const u8 * dev_addr,u8 * iface_addr)4872 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4873 			   u8 *iface_addr)
4874 {
4875 	struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4876 	if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4877 		return -1;
4878 	os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4879 	return 0;
4880 }
4881 
4882 
p2p_get_dev_addr(struct p2p_data * p2p,const u8 * iface_addr,u8 * dev_addr)4883 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4884 			   u8 *dev_addr)
4885 {
4886 	struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4887 	if (dev == NULL)
4888 		return -1;
4889 	os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4890 	return 0;
4891 }
4892 
4893 
p2p_set_peer_filter(struct p2p_data * p2p,const u8 * addr)4894 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4895 {
4896 	os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4897 	if (is_zero_ether_addr(p2p->peer_filter))
4898 		p2p_dbg(p2p, "Disable peer filter");
4899 	else
4900 		p2p_dbg(p2p, "Enable peer filter for " MACSTR,
4901 			MAC2STR(p2p->peer_filter));
4902 }
4903 
4904 
p2p_set_cross_connect(struct p2p_data * p2p,int enabled)4905 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4906 {
4907 	p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
4908 	if (p2p->cross_connect == enabled)
4909 		return;
4910 	p2p->cross_connect = enabled;
4911 	/* TODO: may need to tear down any action group where we are GO(?) */
4912 }
4913 
4914 
p2p_get_oper_freq(struct p2p_data * p2p,const u8 * iface_addr)4915 int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4916 {
4917 	struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4918 	if (dev == NULL)
4919 		return -1;
4920 	if (dev->oper_freq <= 0)
4921 		return -1;
4922 	return dev->oper_freq;
4923 }
4924 
4925 
p2p_set_intra_bss_dist(struct p2p_data * p2p,int enabled)4926 void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4927 {
4928 	p2p_dbg(p2p, "Intra BSS distribution %s",
4929 		enabled ? "enabled" : "disabled");
4930 	p2p->cfg->p2p_intra_bss = enabled;
4931 }
4932 
4933 
p2p_update_channel_list(struct p2p_data * p2p,const struct p2p_channels * chan,const struct p2p_channels * cli_chan)4934 void p2p_update_channel_list(struct p2p_data *p2p,
4935 			     const struct p2p_channels *chan,
4936 			     const struct p2p_channels *cli_chan)
4937 {
4938 	p2p_dbg(p2p, "Update channel list");
4939 	os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
4940 	p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
4941 	os_memcpy(&p2p->cfg->cli_channels, cli_chan,
4942 		  sizeof(struct p2p_channels));
4943 	p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
4944 }
4945 
4946 
p2p_send_action(struct p2p_data * p2p,unsigned int freq,const u8 * dst,const u8 * src,const u8 * bssid,const u8 * buf,size_t len,unsigned int wait_time)4947 int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4948 		    const u8 *src, const u8 *bssid, const u8 *buf,
4949 		    size_t len, unsigned int wait_time)
4950 {
4951 	int res, scheduled;
4952 
4953 	res = p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4954 				    buf, len, wait_time, &scheduled);
4955 	if (res == 0 && scheduled && p2p->in_listen && freq > 0 &&
4956 	    p2p->drv_in_listen > 0 &&
4957 	    (unsigned int) p2p->drv_in_listen != freq) {
4958 		p2p_dbg(p2p,
4959 			"Stop listen on %d MHz to allow a frame to be sent immediately on %d MHz",
4960 			p2p->drv_in_listen, freq);
4961 		p2p_stop_listen_for_freq(p2p, freq);
4962 	}
4963 	return res;
4964 }
4965 
4966 
p2p_set_best_channels(struct p2p_data * p2p,int freq_24,int freq_5,int freq_overall)4967 void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4968 			   int freq_overall)
4969 {
4970 	p2p_dbg(p2p, "Best channel: 2.4 GHz: %d,  5 GHz: %d,  overall: %d",
4971 		freq_24, freq_5, freq_overall);
4972 	p2p->best_freq_24 = freq_24;
4973 	p2p->best_freq_5 = freq_5;
4974 	p2p->best_freq_overall = freq_overall;
4975 }
4976 
4977 
p2p_set_own_freq_preference(struct p2p_data * p2p,int freq)4978 void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
4979 {
4980 	p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
4981 	p2p->own_freq_preference = freq;
4982 }
4983 
4984 
p2p_get_go_neg_peer(struct p2p_data * p2p)4985 const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4986 {
4987 	if (p2p == NULL || p2p->go_neg_peer == NULL)
4988 		return NULL;
4989 	return p2p->go_neg_peer->info.p2p_device_addr;
4990 }
4991 
4992 
4993 const struct p2p_peer_info *
p2p_get_peer_found(struct p2p_data * p2p,const u8 * addr,int next)4994 p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4995 {
4996 	struct p2p_device *dev;
4997 
4998 	if (addr) {
4999 		dev = p2p_get_device(p2p, addr);
5000 		if (!dev)
5001 			return NULL;
5002 
5003 		if (!next) {
5004 			if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
5005 				return NULL;
5006 
5007 			return &dev->info;
5008 		} else {
5009 			do {
5010 				dev = dl_list_first(&dev->list,
5011 						    struct p2p_device,
5012 						    list);
5013 				if (!dev || &dev->list == &p2p->devices)
5014 					return NULL;
5015 			} while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
5016 		}
5017 	} else {
5018 		dev = dl_list_first(&p2p->devices, struct p2p_device, list);
5019 		if (!dev)
5020 			return NULL;
5021 		while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
5022 			dev = dl_list_first(&dev->list,
5023 					    struct p2p_device,
5024 					    list);
5025 			if (!dev || &dev->list == &p2p->devices)
5026 				return NULL;
5027 		}
5028 	}
5029 
5030 	return &dev->info;
5031 }
5032 
5033 
p2p_in_progress(struct p2p_data * p2p)5034 int p2p_in_progress(struct p2p_data *p2p)
5035 {
5036 	if (p2p == NULL)
5037 		return 0;
5038 	if (p2p->state == P2P_SEARCH)
5039 		return 2;
5040 	return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
5041 }
5042 
5043 
p2p_set_config_timeout(struct p2p_data * p2p,u8 go_timeout,u8 client_timeout)5044 void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
5045 			    u8 client_timeout)
5046 {
5047 	if (p2p) {
5048 		p2p->go_timeout = go_timeout;
5049 		p2p->client_timeout = client_timeout;
5050 	}
5051 }
5052 
5053 
5054 #ifdef CONFIG_WIFI_DISPLAY
5055 
p2p_update_wfd_ie_groups(struct p2p_data * p2p)5056 static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
5057 {
5058 	size_t g;
5059 	struct p2p_group *group;
5060 
5061 	for (g = 0; g < p2p->num_groups; g++) {
5062 		group = p2p->groups[g];
5063 		p2p_group_force_beacon_update_ies(group);
5064 	}
5065 }
5066 
5067 
p2p_set_wfd_ie_beacon(struct p2p_data * p2p,struct wpabuf * ie)5068 int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
5069 {
5070 	wpabuf_free(p2p->wfd_ie_beacon);
5071 	p2p->wfd_ie_beacon = ie;
5072 	p2p_update_wfd_ie_groups(p2p);
5073 	return 0;
5074 }
5075 
5076 
p2p_set_wfd_ie_probe_req(struct p2p_data * p2p,struct wpabuf * ie)5077 int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
5078 {
5079 	wpabuf_free(p2p->wfd_ie_probe_req);
5080 	p2p->wfd_ie_probe_req = ie;
5081 	return 0;
5082 }
5083 
5084 
p2p_set_wfd_ie_probe_resp(struct p2p_data * p2p,struct wpabuf * ie)5085 int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
5086 {
5087 	wpabuf_free(p2p->wfd_ie_probe_resp);
5088 	p2p->wfd_ie_probe_resp = ie;
5089 	p2p_update_wfd_ie_groups(p2p);
5090 	return 0;
5091 }
5092 
5093 
p2p_set_wfd_ie_assoc_req(struct p2p_data * p2p,struct wpabuf * ie)5094 int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
5095 {
5096 	wpabuf_free(p2p->wfd_ie_assoc_req);
5097 	p2p->wfd_ie_assoc_req = ie;
5098 	return 0;
5099 }
5100 
5101 
p2p_set_wfd_ie_invitation(struct p2p_data * p2p,struct wpabuf * ie)5102 int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
5103 {
5104 	wpabuf_free(p2p->wfd_ie_invitation);
5105 	p2p->wfd_ie_invitation = ie;
5106 	return 0;
5107 }
5108 
5109 
p2p_set_wfd_ie_prov_disc_req(struct p2p_data * p2p,struct wpabuf * ie)5110 int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
5111 {
5112 	wpabuf_free(p2p->wfd_ie_prov_disc_req);
5113 	p2p->wfd_ie_prov_disc_req = ie;
5114 	return 0;
5115 }
5116 
5117 
p2p_set_wfd_ie_prov_disc_resp(struct p2p_data * p2p,struct wpabuf * ie)5118 int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
5119 {
5120 	wpabuf_free(p2p->wfd_ie_prov_disc_resp);
5121 	p2p->wfd_ie_prov_disc_resp = ie;
5122 	return 0;
5123 }
5124 
5125 
p2p_set_wfd_ie_go_neg(struct p2p_data * p2p,struct wpabuf * ie)5126 int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
5127 {
5128 	wpabuf_free(p2p->wfd_ie_go_neg);
5129 	p2p->wfd_ie_go_neg = ie;
5130 	return 0;
5131 }
5132 
5133 
p2p_set_wfd_dev_info(struct p2p_data * p2p,const struct wpabuf * elem)5134 int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5135 {
5136 	wpabuf_free(p2p->wfd_dev_info);
5137 	if (elem) {
5138 		p2p->wfd_dev_info = wpabuf_dup(elem);
5139 		if (p2p->wfd_dev_info == NULL)
5140 			return -1;
5141 	} else
5142 		p2p->wfd_dev_info = NULL;
5143 
5144 	return 0;
5145 }
5146 
5147 
p2p_set_wfd_r2_dev_info(struct p2p_data * p2p,const struct wpabuf * elem)5148 int p2p_set_wfd_r2_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
5149 {
5150 	wpabuf_free(p2p->wfd_r2_dev_info);
5151 	if (elem) {
5152 		p2p->wfd_r2_dev_info = wpabuf_dup(elem);
5153 		if (p2p->wfd_r2_dev_info == NULL)
5154 			return -1;
5155 	} else
5156 		p2p->wfd_r2_dev_info = NULL;
5157 
5158 	return 0;
5159 }
5160 
5161 
p2p_set_wfd_assoc_bssid(struct p2p_data * p2p,const struct wpabuf * elem)5162 int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
5163 {
5164 	wpabuf_free(p2p->wfd_assoc_bssid);
5165 	if (elem) {
5166 		p2p->wfd_assoc_bssid = wpabuf_dup(elem);
5167 		if (p2p->wfd_assoc_bssid == NULL)
5168 			return -1;
5169 	} else
5170 		p2p->wfd_assoc_bssid = NULL;
5171 
5172 	return 0;
5173 }
5174 
5175 
p2p_set_wfd_coupled_sink_info(struct p2p_data * p2p,const struct wpabuf * elem)5176 int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
5177 				  const struct wpabuf *elem)
5178 {
5179 	wpabuf_free(p2p->wfd_coupled_sink_info);
5180 	if (elem) {
5181 		p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
5182 		if (p2p->wfd_coupled_sink_info == NULL)
5183 			return -1;
5184 	} else
5185 		p2p->wfd_coupled_sink_info = NULL;
5186 
5187 	return 0;
5188 }
5189 
5190 #endif /* CONFIG_WIFI_DISPLAY */
5191 
5192 
p2p_set_disc_int(struct p2p_data * p2p,int min_disc_int,int max_disc_int,int max_disc_tu)5193 int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
5194 		     int max_disc_tu)
5195 {
5196 	if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
5197 		return -1;
5198 
5199 	p2p->min_disc_int = min_disc_int;
5200 	p2p->max_disc_int = max_disc_int;
5201 	p2p->max_disc_tu = max_disc_tu;
5202 	p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
5203 		min_disc_int, max_disc_int, max_disc_tu);
5204 
5205 	return 0;
5206 }
5207 
5208 
p2p_dbg(struct p2p_data * p2p,const char * fmt,...)5209 void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
5210 {
5211 	va_list ap;
5212 	char buf[500];
5213 
5214 	if (!p2p->cfg->debug_print)
5215 		return;
5216 
5217 	va_start(ap, fmt);
5218 	vsnprintf(buf, sizeof(buf), fmt, ap);
5219 	buf[sizeof(buf) - 1] = '\0';
5220 	va_end(ap);
5221 	p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
5222 }
5223 
5224 
p2p_info(struct p2p_data * p2p,const char * fmt,...)5225 void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
5226 {
5227 	va_list ap;
5228 	char buf[500];
5229 
5230 	if (!p2p->cfg->debug_print)
5231 		return;
5232 
5233 	va_start(ap, fmt);
5234 	vsnprintf(buf, sizeof(buf), fmt, ap);
5235 	buf[sizeof(buf) - 1] = '\0';
5236 	va_end(ap);
5237 	p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
5238 }
5239 
5240 
p2p_err(struct p2p_data * p2p,const char * fmt,...)5241 void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
5242 {
5243 	va_list ap;
5244 	char buf[500];
5245 
5246 	if (!p2p->cfg->debug_print)
5247 		return;
5248 
5249 	va_start(ap, fmt);
5250 	vsnprintf(buf, sizeof(buf), fmt, ap);
5251 	buf[sizeof(buf) - 1] = '\0';
5252 	va_end(ap);
5253 	p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
5254 }
5255 
5256 
p2p_loop_on_known_peers(struct p2p_data * p2p,void (* peer_callback)(struct p2p_peer_info * peer,void * user_data),void * user_data)5257 void p2p_loop_on_known_peers(struct p2p_data *p2p,
5258 			     void (*peer_callback)(struct p2p_peer_info *peer,
5259 						   void *user_data),
5260 			     void *user_data)
5261 {
5262 	struct p2p_device *dev, *n;
5263 
5264 	dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
5265 		peer_callback(&dev->info, user_data);
5266 	}
5267 }
5268 
5269 
5270 #ifdef CONFIG_WPS_NFC
5271 
p2p_build_nfc_handover(struct p2p_data * p2p,int client_freq,const u8 * go_dev_addr,const u8 * ssid,size_t ssid_len)5272 static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
5273 					      int client_freq,
5274 					      const u8 *go_dev_addr,
5275 					      const u8 *ssid, size_t ssid_len)
5276 {
5277 	struct wpabuf *buf;
5278 	u8 op_class, channel;
5279 	enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
5280 
5281 	buf = wpabuf_alloc(1000);
5282 	if (buf == NULL)
5283 		return NULL;
5284 
5285 	op_class = p2p->cfg->reg_class;
5286 	channel = p2p->cfg->channel;
5287 
5288 	p2p_buf_add_capability(buf, p2p->dev_capab &
5289 			       ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
5290 	p2p_buf_add_device_info(buf, p2p, NULL);
5291 
5292 	if (p2p->num_groups > 0) {
5293 		int freq = p2p_group_get_freq(p2p->groups[0]);
5294 		role = P2P_GO_IN_A_GROUP;
5295 		if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) {
5296 			p2p_dbg(p2p,
5297 				"Unknown GO operating frequency %d MHz for NFC handover",
5298 				freq);
5299 			wpabuf_free(buf);
5300 			return NULL;
5301 		}
5302 	} else if (client_freq > 0) {
5303 		role = P2P_CLIENT_IN_A_GROUP;
5304 		if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) {
5305 			p2p_dbg(p2p,
5306 				"Unknown client operating frequency %d MHz for NFC handover",
5307 				client_freq);
5308 			wpabuf_free(buf);
5309 			return NULL;
5310 		}
5311 	}
5312 
5313 	p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
5314 				       channel, role);
5315 
5316 	if (p2p->num_groups > 0) {
5317 		/* Limit number of clients to avoid very long message */
5318 		p2p_buf_add_group_info(p2p->groups[0], buf, 5);
5319 		p2p_group_buf_add_id(p2p->groups[0], buf);
5320 	} else if (client_freq > 0 &&
5321 		   go_dev_addr && !is_zero_ether_addr(go_dev_addr) &&
5322 		   ssid && ssid_len > 0) {
5323 		/*
5324 		 * Add the optional P2P Group ID to indicate in which group this
5325 		 * device is a P2P Client.
5326 		 */
5327 		p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len);
5328 	}
5329 
5330 	return buf;
5331 }
5332 
5333 
p2p_build_nfc_handover_req(struct p2p_data * p2p,int client_freq,const u8 * go_dev_addr,const u8 * ssid,size_t ssid_len)5334 struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
5335 					   int client_freq,
5336 					   const u8 *go_dev_addr,
5337 					   const u8 *ssid, size_t ssid_len)
5338 {
5339 	return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5340 				      ssid_len);
5341 }
5342 
5343 
p2p_build_nfc_handover_sel(struct p2p_data * p2p,int client_freq,const u8 * go_dev_addr,const u8 * ssid,size_t ssid_len)5344 struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
5345 					   int client_freq,
5346 					   const u8 *go_dev_addr,
5347 					   const u8 *ssid, size_t ssid_len)
5348 {
5349 	return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
5350 				      ssid_len);
5351 }
5352 
5353 
p2p_process_nfc_connection_handover(struct p2p_data * p2p,struct p2p_nfc_params * params)5354 int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
5355 					struct p2p_nfc_params *params)
5356 {
5357 	struct p2p_message msg;
5358 	struct p2p_device *dev;
5359 	const u8 *p2p_dev_addr;
5360 	int freq;
5361 	enum p2p_role_indication role;
5362 
5363 	params->next_step = NO_ACTION;
5364 
5365 	if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
5366 				   params->p2p_attr, params->p2p_len, &msg)) {
5367 		p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
5368 		p2p_parse_free(&msg);
5369 		return -1;
5370 	}
5371 
5372 	if (msg.p2p_device_addr)
5373 		p2p_dev_addr = msg.p2p_device_addr;
5374 	else if (msg.device_id)
5375 		p2p_dev_addr = msg.device_id;
5376 	else {
5377 		p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
5378 		p2p_parse_free(&msg);
5379 		return -1;
5380 	}
5381 
5382 	if (msg.oob_dev_password) {
5383 		os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
5384 			  msg.oob_dev_password_len);
5385 		params->oob_dev_pw_len = msg.oob_dev_password_len;
5386 	}
5387 
5388 	dev = p2p_create_device(p2p, p2p_dev_addr);
5389 	if (dev == NULL) {
5390 		p2p_parse_free(&msg);
5391 		return -1;
5392 	}
5393 
5394 	params->peer = &dev->info;
5395 
5396 	os_get_reltime(&dev->last_seen);
5397 	dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
5398 	p2p_copy_wps_info(p2p, dev, 0, &msg);
5399 
5400 	if (!msg.oob_go_neg_channel) {
5401 		p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
5402 		p2p_parse_free(&msg);
5403 		return -1;
5404 	}
5405 
5406 	if (msg.oob_go_neg_channel[3] == 0 &&
5407 	    msg.oob_go_neg_channel[4] == 0)
5408 		freq = 0;
5409 	else
5410 		freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
5411 					   msg.oob_go_neg_channel[4]);
5412 	if (freq < 0) {
5413 		p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
5414 		p2p_parse_free(&msg);
5415 		return -1;
5416 	}
5417 	role = msg.oob_go_neg_channel[5];
5418 
5419 	if (role == P2P_GO_IN_A_GROUP) {
5420 		p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
5421 		params->go_freq = freq;
5422 	} else if (role == P2P_CLIENT_IN_A_GROUP) {
5423 		p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz",
5424 			freq);
5425 		params->go_freq = freq;
5426 	} else
5427 		p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
5428 	dev->oob_go_neg_freq = freq;
5429 
5430 	if (!params->sel && role != P2P_GO_IN_A_GROUP) {
5431 		freq = p2p_channel_to_freq(p2p->cfg->reg_class,
5432 					   p2p->cfg->channel);
5433 		if (freq < 0) {
5434 			p2p_dbg(p2p, "Own listen channel not known");
5435 			p2p_parse_free(&msg);
5436 			return -1;
5437 		}
5438 		p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
5439 		dev->oob_go_neg_freq = freq;
5440 	}
5441 
5442 	if (msg.group_id) {
5443 		os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
5444 		params->go_ssid_len = msg.group_id_len - ETH_ALEN;
5445 		os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
5446 			  params->go_ssid_len);
5447 	}
5448 
5449 	if (dev->flags & P2P_DEV_USER_REJECTED) {
5450 		p2p_dbg(p2p, "Do not report rejected device");
5451 		p2p_parse_free(&msg);
5452 		return 0;
5453 	}
5454 
5455 	if (!(dev->flags & P2P_DEV_REPORTED)) {
5456 		p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
5457 				    !(dev->flags & P2P_DEV_REPORTED_ONCE));
5458 		dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
5459 	}
5460 	p2p_parse_free(&msg);
5461 
5462 	if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
5463 		params->next_step = BOTH_GO;
5464 	else if (role == P2P_GO_IN_A_GROUP)
5465 		params->next_step = JOIN_GROUP;
5466 	else if (role == P2P_CLIENT_IN_A_GROUP) {
5467 		dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
5468 		params->next_step = PEER_CLIENT;
5469 	} else if (p2p->num_groups > 0)
5470 		params->next_step = AUTH_JOIN;
5471 	else if (params->sel)
5472 		params->next_step = INIT_GO_NEG;
5473 	else
5474 		params->next_step = RESP_GO_NEG;
5475 
5476 	return 0;
5477 }
5478 
5479 
p2p_set_authorized_oob_dev_pw_id(struct p2p_data * p2p,u16 dev_pw_id,int go_intent,const u8 * own_interface_addr)5480 void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
5481 				      int go_intent,
5482 				      const u8 *own_interface_addr)
5483 {
5484 
5485 	p2p->authorized_oob_dev_pw_id = dev_pw_id;
5486 	if (dev_pw_id == 0) {
5487 		p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
5488 		return;
5489 	}
5490 
5491 	p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
5492 		dev_pw_id);
5493 
5494 	p2p->go_intent = go_intent;
5495 	os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
5496 }
5497 
5498 #endif /* CONFIG_WPS_NFC */
5499 
5500 
p2p_set_passphrase_len(struct p2p_data * p2p,unsigned int len)5501 int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len)
5502 {
5503 	if (len < 8 || len > 63)
5504 		return -1;
5505 	p2p->cfg->passphrase_len = len;
5506 	return 0;
5507 }
5508 
5509 
p2p_set_vendor_elems(struct p2p_data * p2p,struct wpabuf ** vendor_elem)5510 void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem)
5511 {
5512 	p2p->vendor_elem = vendor_elem;
5513 }
5514 
5515 
p2p_go_neg_wait_timeout(void * eloop_ctx,void * timeout_ctx)5516 void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx)
5517 {
5518 	struct p2p_data *p2p = eloop_ctx;
5519 
5520 	p2p_dbg(p2p,
5521 		"Timeout on waiting peer to become ready for GO Negotiation");
5522 	p2p_go_neg_failed(p2p, -1);
5523 }
5524 
5525 
p2p_set_own_pref_freq_list(struct p2p_data * p2p,const unsigned int * pref_freq_list,unsigned int size)5526 void p2p_set_own_pref_freq_list(struct p2p_data *p2p,
5527 				const unsigned int *pref_freq_list,
5528 				unsigned int size)
5529 {
5530 	unsigned int i;
5531 
5532 	if (size > P2P_MAX_PREF_CHANNELS)
5533 		size = P2P_MAX_PREF_CHANNELS;
5534 	p2p->num_pref_freq = size;
5535 	for (i = 0; i < size; i++) {
5536 		p2p->pref_freq_list[i] = pref_freq_list[i];
5537 		p2p_dbg(p2p, "Own preferred frequency list[%u]=%u MHz",
5538 			i, p2p->pref_freq_list[i]);
5539 	}
5540 }
5541 
5542 
p2p_set_override_pref_op_chan(struct p2p_data * p2p,u8 op_class,u8 chan)5543 void p2p_set_override_pref_op_chan(struct p2p_data *p2p, u8 op_class,
5544 				   u8 chan)
5545 {
5546 	p2p->override_pref_op_class = op_class;
5547 	p2p->override_pref_channel = chan;
5548 }
5549 
5550 
p2p_build_probe_resp_template(struct p2p_data * p2p,unsigned int freq)5551 struct wpabuf * p2p_build_probe_resp_template(struct p2p_data *p2p,
5552 					      unsigned int freq)
5553 {
5554 	struct wpabuf *ies, *buf;
5555 	u8 addr[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF };
5556 	int ret;
5557 
5558 	ies = p2p_build_probe_resp_ies(p2p, NULL, 0);
5559 	if (!ies) {
5560 		wpa_printf(MSG_ERROR,
5561 			   "CTRL: Failed to build Probe Response IEs");
5562 		return NULL;
5563 	}
5564 
5565 	buf = wpabuf_alloc(200 + wpabuf_len(ies));
5566 	if (!buf) {
5567 		wpabuf_free(ies);
5568 		return NULL;
5569 	}
5570 
5571 	ret = p2p_build_probe_resp_buf(p2p, buf, ies, addr, freq);
5572 	wpabuf_free(ies);
5573 	if (ret) {
5574 		wpabuf_free(buf);
5575 		return NULL;
5576 	}
5577 
5578 	return buf;
5579 }
5580