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