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