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