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