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