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