]> git.ipfire.org Git - thirdparty/hostap.git/blob - src/p2p/p2p.c
P2P: Allow Device ID to be specified for p2p_find command
[thirdparty/hostap.git] / src / p2p / p2p.c
1 /*
2 * Wi-Fi Direct - P2P module
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15 #include "includes.h"
16
17 #include "common.h"
18 #include "eloop.h"
19 #include "common/ieee802_11_defs.h"
20 #include "common/ieee802_11_common.h"
21 #include "wps/wps_i.h"
22 #include "p2p_i.h"
23 #include "p2p.h"
24
25
26 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
27 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
28 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
29 const u8 *sa, const u8 *data, size_t len,
30 int rx_freq);
31 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
32 const u8 *sa, const u8 *data,
33 size_t len);
34 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
35 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
36
37
38 /*
39 * p2p_scan recovery timeout
40 *
41 * Many drivers are using 30 second timeout on scan results. Allow a bit larger
42 * timeout for this to avoid hitting P2P timeout unnecessarily.
43 */
44 #define P2P_SCAN_TIMEOUT 35
45
46 /**
47 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
48 * entries will be removed
49 */
50 #define P2P_PEER_EXPIRATION_AGE 300
51
52 #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
53
54 static void p2p_expire_peers(struct p2p_data *p2p)
55 {
56 struct p2p_device *dev, *n;
57 struct os_time now;
58
59 os_get_time(&now);
60 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
61 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
62 continue;
63 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer "
64 "entry " MACSTR, MAC2STR(dev->info.p2p_device_addr));
65 dl_list_del(&dev->list);
66 p2p_device_free(p2p, dev);
67 }
68 }
69
70
71 static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
72 {
73 struct p2p_data *p2p = eloop_ctx;
74 p2p_expire_peers(p2p);
75 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
76 p2p_expiration_timeout, p2p, NULL);
77 }
78
79
80 static const char * p2p_state_txt(int state)
81 {
82 switch (state) {
83 case P2P_IDLE:
84 return "IDLE";
85 case P2P_SEARCH:
86 return "SEARCH";
87 case P2P_CONNECT:
88 return "CONNECT";
89 case P2P_CONNECT_LISTEN:
90 return "CONNECT_LISTEN";
91 case P2P_GO_NEG:
92 return "GO_NEG";
93 case P2P_LISTEN_ONLY:
94 return "LISTEN_ONLY";
95 case P2P_WAIT_PEER_CONNECT:
96 return "WAIT_PEER_CONNECT";
97 case P2P_WAIT_PEER_IDLE:
98 return "WAIT_PEER_IDLE";
99 case P2P_SD_DURING_FIND:
100 return "SD_DURING_FIND";
101 case P2P_PROVISIONING:
102 return "PROVISIONING";
103 case P2P_PD_DURING_FIND:
104 return "PD_DURING_FIND";
105 case P2P_INVITE:
106 return "INVITE";
107 case P2P_INVITE_LISTEN:
108 return "INVITE_LISTEN";
109 case P2P_SEARCH_WHEN_READY:
110 return "SEARCH_WHEN_READY";
111 default:
112 return "?";
113 }
114 }
115
116
117 u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
118 {
119 struct p2p_device *dev = NULL;
120
121 if (!addr || !p2p)
122 return 0;
123
124 dev = p2p_get_device(p2p, addr);
125 if (dev)
126 return dev->wps_prov_info;
127 else
128 return 0;
129 }
130
131
132 void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *iface_addr)
133 {
134 struct p2p_device *dev = NULL;
135
136 if (!iface_addr || !p2p)
137 return;
138
139 dev = p2p_get_device_interface(p2p, iface_addr);
140 if (dev)
141 dev->wps_prov_info = 0;
142 }
143
144
145 void p2p_set_state(struct p2p_data *p2p, int new_state)
146 {
147 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s",
148 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
149 p2p->state = new_state;
150 }
151
152
153 void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
154 {
155 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
156 "P2P: Set timeout (state=%s): %u.%06u sec",
157 p2p_state_txt(p2p->state), sec, usec);
158 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
159 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
160 }
161
162
163 void p2p_clear_timeout(struct p2p_data *p2p)
164 {
165 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)",
166 p2p_state_txt(p2p->state));
167 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
168 }
169
170
171 void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
172 int status)
173 {
174 struct p2p_go_neg_results res;
175 p2p_clear_timeout(p2p);
176 p2p_set_state(p2p, P2P_IDLE);
177 if (p2p->go_neg_peer)
178 p2p->go_neg_peer->wps_method = WPS_NOT_READY;
179 p2p->go_neg_peer = NULL;
180
181 os_memset(&res, 0, sizeof(res));
182 res.status = status;
183 if (peer) {
184 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr,
185 ETH_ALEN);
186 os_memcpy(res.peer_interface_addr, peer->intended_addr,
187 ETH_ALEN);
188 }
189 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
190 }
191
192
193 static void p2p_listen_in_find(struct p2p_data *p2p)
194 {
195 unsigned int r, tu;
196 int freq;
197 struct wpabuf *ies;
198
199 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
200 "P2P: Starting short listen state (state=%s)",
201 p2p_state_txt(p2p->state));
202
203 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
204 p2p->cfg->channel);
205 if (freq < 0) {
206 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
207 "P2P: Unknown regulatory class/channel");
208 return;
209 }
210
211 os_get_random((u8 *) &r, sizeof(r));
212 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
213 p2p->min_disc_int) * 100;
214
215 p2p->pending_listen_freq = freq;
216 p2p->pending_listen_sec = 0;
217 p2p->pending_listen_usec = 1024 * tu;
218
219 ies = p2p_build_probe_resp_ies(p2p);
220 if (ies == NULL)
221 return;
222
223 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
224 ies) < 0) {
225 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
226 "P2P: Failed to start listen mode");
227 p2p->pending_listen_freq = 0;
228 }
229 wpabuf_free(ies);
230 }
231
232
233 int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
234 {
235 int freq;
236 struct wpabuf *ies;
237
238 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
239 "P2P: Going to listen(only) state");
240
241 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
242 p2p->cfg->channel);
243 if (freq < 0) {
244 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
245 "P2P: Unknown regulatory class/channel");
246 return -1;
247 }
248
249 p2p->pending_listen_freq = freq;
250 p2p->pending_listen_sec = timeout / 1000;
251 p2p->pending_listen_usec = (timeout % 1000) * 1000;
252
253 if (p2p->p2p_scan_running) {
254 if (p2p->start_after_scan == P2P_AFTER_SCAN_NOTHING) {
255 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
256 "P2P: p2p_scan running - connect is already "
257 "pending - skip listen");
258 return 0;
259 }
260 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
261 "P2P: p2p_scan running - delay start of listen state");
262 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
263 return 0;
264 }
265
266 ies = p2p_build_probe_resp_ies(p2p);
267 if (ies == NULL)
268 return -1;
269
270 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
271 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
272 "P2P: Failed to start listen mode");
273 p2p->pending_listen_freq = 0;
274 wpabuf_free(ies);
275 return -1;
276 }
277 wpabuf_free(ies);
278
279 p2p_set_state(p2p, P2P_LISTEN_ONLY);
280
281 return 0;
282 }
283
284
285 static void p2p_device_clear_reported(struct p2p_data *p2p)
286 {
287 struct p2p_device *dev;
288 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
289 dev->flags &= ~P2P_DEV_REPORTED;
290 }
291
292
293 /**
294 * p2p_get_device - Fetch a peer entry
295 * @p2p: P2P module context from p2p_init()
296 * @addr: P2P Device Address of the peer
297 * Returns: Pointer to the device entry or %NULL if not found
298 */
299 struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
300 {
301 struct p2p_device *dev;
302 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
303 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
304 return dev;
305 }
306 return NULL;
307 }
308
309
310 /**
311 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
312 * @p2p: P2P module context from p2p_init()
313 * @addr: P2P Interface Address of the peer
314 * Returns: Pointer to the device entry or %NULL if not found
315 */
316 struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
317 const u8 *addr)
318 {
319 struct p2p_device *dev;
320 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
321 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
322 return dev;
323 }
324 return NULL;
325 }
326
327
328 /**
329 * p2p_create_device - Create a peer entry
330 * @p2p: P2P module context from p2p_init()
331 * @addr: P2P Device Address of the peer
332 * Returns: Pointer to the device entry or %NULL on failure
333 *
334 * If there is already an entry for the peer, it will be returned instead of
335 * creating a new one.
336 */
337 static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
338 const u8 *addr)
339 {
340 struct p2p_device *dev, *oldest = NULL;
341 size_t count = 0;
342
343 dev = p2p_get_device(p2p, addr);
344 if (dev)
345 return dev;
346
347 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
348 count++;
349 if (oldest == NULL ||
350 os_time_before(&dev->last_seen, &oldest->last_seen))
351 oldest = dev;
352 }
353 if (count + 1 > p2p->cfg->max_peers && oldest) {
354 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
355 "P2P: Remove oldest peer entry to make room for a new "
356 "peer");
357 dl_list_del(&oldest->list);
358 p2p_device_free(p2p, oldest);
359 }
360
361 dev = os_zalloc(sizeof(*dev));
362 if (dev == NULL)
363 return NULL;
364 dl_list_add(&p2p->devices, &dev->list);
365 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
366
367 return dev;
368 }
369
370
371 static void p2p_copy_client_info(struct p2p_device *dev,
372 struct p2p_client_info *cli)
373 {
374 os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
375 dev->info.device_name[cli->dev_name_len] = '\0';
376 dev->info.dev_capab = cli->dev_capab;
377 dev->info.config_methods = cli->config_methods;
378 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
379 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
380 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
381 dev->info.wps_sec_dev_type_list_len);
382 }
383
384
385 static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
386 const u8 *go_interface_addr, int freq,
387 const u8 *gi, size_t gi_len)
388 {
389 struct p2p_group_info info;
390 size_t c;
391 struct p2p_device *dev;
392
393 if (gi == NULL)
394 return 0;
395
396 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
397 return -1;
398
399 /*
400 * Clear old data for this group; if the devices are still in the
401 * group, the information will be restored in the loop following this.
402 */
403 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
404 if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
405 ETH_ALEN) == 0) {
406 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
407 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
408 }
409 }
410
411 for (c = 0; c < info.num_clients; c++) {
412 struct p2p_client_info *cli = &info.client[c];
413 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
414 ETH_ALEN) == 0)
415 continue; /* ignore our own entry */
416 dev = p2p_get_device(p2p, cli->p2p_device_addr);
417 if (dev) {
418 /*
419 * Update information only if we have not received this
420 * directly from the client.
421 */
422 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
423 P2P_DEV_PROBE_REQ_ONLY))
424 p2p_copy_client_info(dev, cli);
425 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
426 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
427 }
428 } else {
429 dev = p2p_create_device(p2p, cli->p2p_device_addr);
430 if (dev == NULL)
431 continue;
432 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
433 p2p_copy_client_info(dev, cli);
434 dev->oper_freq = freq;
435 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
436 dev->info.p2p_device_addr,
437 &dev->info, 1);
438 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
439 }
440
441 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
442 ETH_ALEN);
443 os_get_time(&dev->last_seen);
444 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
445 os_memcpy(dev->member_in_go_iface, go_interface_addr,
446 ETH_ALEN);
447 }
448
449 return 0;
450 }
451
452
453 static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
454 const struct p2p_message *msg)
455 {
456 os_memcpy(dev->info.device_name, msg->device_name,
457 sizeof(dev->info.device_name));
458
459 if (msg->manufacturer &&
460 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
461 os_memset(dev->info.manufacturer, 0,
462 sizeof(dev->info.manufacturer));
463 os_memcpy(dev->info.manufacturer, msg->manufacturer,
464 msg->manufacturer_len);
465 }
466
467 if (msg->model_name &&
468 msg->model_name_len < sizeof(dev->info.model_name)) {
469 os_memset(dev->info.model_name, 0,
470 sizeof(dev->info.model_name));
471 os_memcpy(dev->info.model_name, msg->model_name,
472 msg->model_name_len);
473 }
474
475 if (msg->model_number &&
476 msg->model_number_len < sizeof(dev->info.model_number)) {
477 os_memset(dev->info.model_number, 0,
478 sizeof(dev->info.model_number));
479 os_memcpy(dev->info.model_number, msg->model_number,
480 msg->model_number_len);
481 }
482
483 if (msg->serial_number &&
484 msg->serial_number_len < sizeof(dev->info.serial_number)) {
485 os_memset(dev->info.serial_number, 0,
486 sizeof(dev->info.serial_number));
487 os_memcpy(dev->info.serial_number, msg->serial_number,
488 msg->serial_number_len);
489 }
490
491 if (msg->pri_dev_type)
492 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
493 sizeof(dev->info.pri_dev_type));
494 else if (msg->wps_pri_dev_type)
495 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
496 sizeof(dev->info.pri_dev_type));
497
498 if (msg->wps_sec_dev_type_list) {
499 os_memcpy(dev->info.wps_sec_dev_type_list,
500 msg->wps_sec_dev_type_list,
501 msg->wps_sec_dev_type_list_len);
502 dev->info.wps_sec_dev_type_list_len =
503 msg->wps_sec_dev_type_list_len;
504 }
505
506 if (msg->capability) {
507 dev->info.dev_capab = msg->capability[0];
508 dev->info.group_capab = msg->capability[1];
509 }
510
511 if (msg->ext_listen_timing) {
512 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
513 dev->ext_listen_interval =
514 WPA_GET_LE16(msg->ext_listen_timing + 2);
515 }
516
517 if (!probe_req) {
518 dev->info.config_methods = msg->config_methods ?
519 msg->config_methods : msg->wps_config_methods;
520 }
521 }
522
523
524 /**
525 * p2p_add_device - Add peer entries based on scan results
526 * @p2p: P2P module context from p2p_init()
527 * @addr: Source address of Beacon or Probe Response frame (may be either
528 * P2P Device Address or P2P Interface Address)
529 * @level: Signal level (signal strength of the received frame from the peer)
530 * @freq: Frequency on which the Beacon or Probe Response frame was received
531 * @ies: IEs from the Beacon or Probe Response frame
532 * @ies_len: Length of ies buffer in octets
533 * Returns: 0 on success, -1 on failure
534 *
535 * If the scan result is for a GO, the clients in the group will also be added
536 * to the peer table. This function can also be used with some other frames
537 * like Provision Discovery Request that contains P2P Capability and P2P Device
538 * Info attributes.
539 */
540 int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
541 const u8 *ies, size_t ies_len)
542 {
543 struct p2p_device *dev;
544 struct p2p_message msg;
545 const u8 *p2p_dev_addr;
546 int i;
547
548 os_memset(&msg, 0, sizeof(msg));
549 if (p2p_parse_ies(ies, ies_len, &msg)) {
550 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
551 "P2P: Failed to parse P2P IE for a device entry");
552 p2p_parse_free(&msg);
553 return -1;
554 }
555
556 if (msg.p2p_device_addr)
557 p2p_dev_addr = msg.p2p_device_addr;
558 else if (msg.device_id)
559 p2p_dev_addr = msg.device_id;
560 else {
561 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
562 "P2P: Ignore scan data without P2P Device Info or "
563 "P2P Device Id");
564 p2p_parse_free(&msg);
565 return -1;
566 }
567
568 if (!is_zero_ether_addr(p2p->peer_filter) &&
569 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
570 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
571 "filter for " MACSTR " due to peer filter",
572 MAC2STR(p2p_dev_addr));
573 return 0;
574 }
575
576 dev = p2p_create_device(p2p, p2p_dev_addr);
577 if (dev == NULL) {
578 p2p_parse_free(&msg);
579 return -1;
580 }
581 os_get_time(&dev->last_seen);
582 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
583
584 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
585 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
586 if (msg.ssid &&
587 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
588 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
589 != 0)) {
590 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
591 dev->oper_ssid_len = msg.ssid[1];
592 }
593
594 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
595 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
596 int ds_freq;
597 if (*msg.ds_params == 14)
598 ds_freq = 2484;
599 else
600 ds_freq = 2407 + *msg.ds_params * 5;
601 if (freq != ds_freq) {
602 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
603 "P2P: Update Listen frequency based on DS "
604 "Parameter Set IE: %d -> %d MHz",
605 freq, ds_freq);
606 freq = ds_freq;
607 }
608 }
609
610 if (dev->listen_freq && dev->listen_freq != freq) {
611 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
612 "P2P: Update Listen frequency based on scan "
613 "results (" MACSTR " %d -> %d MHz (DS param %d)",
614 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
615 freq, msg.ds_params ? *msg.ds_params : -1);
616 }
617 dev->listen_freq = freq;
618 if (msg.group_info)
619 dev->oper_freq = freq;
620 dev->info.level = level;
621
622 p2p_copy_wps_info(dev, 0, &msg);
623
624 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
625 wpabuf_free(dev->info.wps_vendor_ext[i]);
626 dev->info.wps_vendor_ext[i] = NULL;
627 }
628
629 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
630 if (msg.wps_vendor_ext[i] == NULL)
631 break;
632 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
633 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
634 if (dev->info.wps_vendor_ext[i] == NULL)
635 break;
636 }
637
638 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq, msg.group_info,
639 msg.group_info_len);
640
641 p2p_parse_free(&msg);
642
643 if (p2p_pending_sd_req(p2p, dev))
644 dev->flags |= P2P_DEV_SD_SCHEDULE;
645
646 if (dev->flags & P2P_DEV_REPORTED)
647 return 0;
648
649 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
650 "P2P: Peer found with Listen frequency %d MHz", freq);
651 if (dev->flags & P2P_DEV_USER_REJECTED) {
652 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
653 "P2P: Do not report rejected device");
654 return 0;
655 }
656
657 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
658 !(dev->flags & P2P_DEV_REPORTED_ONCE));
659 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
660
661 return 0;
662 }
663
664
665 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
666 {
667 int i;
668
669 if (p2p->go_neg_peer == dev) {
670 /*
671 * If GO Negotiation is in progress, report that it has failed.
672 */
673 p2p_go_neg_failed(p2p, dev, -1);
674 p2p->go_neg_peer = NULL;
675 }
676 if (p2p->invite_peer == dev)
677 p2p->invite_peer = NULL;
678 if (p2p->sd_peer == dev)
679 p2p->sd_peer = NULL;
680 if (p2p->pending_client_disc_go == dev)
681 p2p->pending_client_disc_go = NULL;
682
683 /* dev_lost() device, but only if it was previously dev_found() */
684 if (dev->flags & P2P_DEV_REPORTED_ONCE)
685 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
686 dev->info.p2p_device_addr);
687
688 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
689 wpabuf_free(dev->info.wps_vendor_ext[i]);
690 dev->info.wps_vendor_ext[i] = NULL;
691 }
692
693 os_free(dev);
694 }
695
696
697 static int p2p_get_next_prog_freq(struct p2p_data *p2p)
698 {
699 struct p2p_channels *c;
700 struct p2p_reg_class *cla;
701 size_t cl, ch;
702 int found = 0;
703 u8 reg_class;
704 u8 channel;
705 int freq;
706
707 c = &p2p->cfg->channels;
708 for (cl = 0; cl < c->reg_classes; cl++) {
709 cla = &c->reg_class[cl];
710 if (cla->reg_class != p2p->last_prog_scan_class)
711 continue;
712 for (ch = 0; ch < cla->channels; ch++) {
713 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
714 found = 1;
715 break;
716 }
717 }
718 if (found)
719 break;
720 }
721
722 if (!found) {
723 /* Start from beginning */
724 reg_class = c->reg_class[0].reg_class;
725 channel = c->reg_class[0].channel[0];
726 } else {
727 /* Pick the next channel */
728 ch++;
729 if (ch == cla->channels) {
730 cl++;
731 if (cl == c->reg_classes)
732 cl = 0;
733 ch = 0;
734 }
735 reg_class = c->reg_class[cl].reg_class;
736 channel = c->reg_class[cl].channel[ch];
737 }
738
739 freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
740 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
741 "channel: reg_class %u channel %u -> %d MHz",
742 reg_class, channel, freq);
743 p2p->last_prog_scan_class = reg_class;
744 p2p->last_prog_scan_chan = channel;
745
746 if (freq == 2412 || freq == 2437 || freq == 2462)
747 return 0; /* No need to add social channels */
748 return freq;
749 }
750
751
752 static void p2p_search(struct p2p_data *p2p)
753 {
754 int freq = 0;
755 enum p2p_scan_type type;
756
757 if (p2p->drv_in_listen) {
758 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
759 "in Listen state - wait for it to end before "
760 "continuing");
761 return;
762 }
763 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
764
765 if (p2p->go_neg_peer) {
766 /*
767 * Only scan the known listen frequency of the peer
768 * during GO Negotiation start.
769 */
770 freq = p2p->go_neg_peer->listen_freq;
771 if (freq <= 0)
772 freq = p2p->go_neg_peer->oper_freq;
773 type = P2P_SCAN_SPECIFIC;
774 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
775 "for freq %u (GO Neg)", freq);
776 } else if (p2p->invite_peer) {
777 /*
778 * Only scan the known listen frequency of the peer
779 * during Invite start.
780 */
781 freq = p2p->invite_peer->listen_freq;
782 if (freq <= 0)
783 freq = p2p->invite_peer->oper_freq;
784 type = P2P_SCAN_SPECIFIC;
785 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
786 "for freq %u (Invite)", freq);
787 } else if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
788 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
789 type = P2P_SCAN_SOCIAL_PLUS_ONE;
790 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
791 "(+ freq %u)", freq);
792 } else {
793 type = P2P_SCAN_SOCIAL;
794 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
795 }
796
797 if (p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
798 p2p->num_req_dev_types, p2p->req_dev_types,
799 p2p->find_dev_id) < 0) {
800 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
801 "P2P: Scan request failed");
802 p2p_continue_find(p2p);
803 } else {
804 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
805 p2p->p2p_scan_running = 1;
806 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
807 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
808 p2p, NULL);
809 }
810 }
811
812
813 static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
814 {
815 struct p2p_data *p2p = eloop_ctx;
816 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
817 p2p_stop_find(p2p);
818 }
819
820
821 static int p2p_run_after_scan(struct p2p_data *p2p)
822 {
823 struct p2p_device *dev;
824 enum p2p_after_scan op;
825
826 if (p2p->after_scan_tx) {
827 /* TODO: schedule p2p_run_after_scan to be called from TX
828 * status callback(?) */
829 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
830 "Action frame at p2p_scan completion");
831 p2p->cfg->send_action(p2p->cfg->cb_ctx,
832 p2p->after_scan_tx->freq,
833 p2p->after_scan_tx->dst,
834 p2p->after_scan_tx->src,
835 p2p->after_scan_tx->bssid,
836 (u8 *) (p2p->after_scan_tx + 1),
837 p2p->after_scan_tx->len,
838 p2p->after_scan_tx->wait_time);
839 os_free(p2p->after_scan_tx);
840 p2p->after_scan_tx = NULL;
841 return 1;
842 }
843
844 op = p2p->start_after_scan;
845 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
846 switch (op) {
847 case P2P_AFTER_SCAN_NOTHING:
848 break;
849 case P2P_AFTER_SCAN_LISTEN:
850 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
851 "requested Listen state");
852 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
853 p2p->pending_listen_usec / 1000);
854 return 1;
855 case P2P_AFTER_SCAN_CONNECT:
856 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
857 "requested connect with " MACSTR,
858 MAC2STR(p2p->after_scan_peer));
859 dev = p2p_get_device(p2p, p2p->after_scan_peer);
860 if (dev == NULL) {
861 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
862 "known anymore");
863 break;
864 }
865 p2p_connect_send(p2p, dev);
866 return 1;
867 }
868
869 return 0;
870 }
871
872
873 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
874 {
875 struct p2p_data *p2p = eloop_ctx;
876 int running;
877 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
878 "(running=%d)", p2p->p2p_scan_running);
879 running = p2p->p2p_scan_running;
880 /* Make sure we recover from missed scan results callback */
881 p2p->p2p_scan_running = 0;
882
883 if (running)
884 p2p_run_after_scan(p2p);
885 }
886
887
888 static void p2p_free_req_dev_types(struct p2p_data *p2p)
889 {
890 p2p->num_req_dev_types = 0;
891 os_free(p2p->req_dev_types);
892 p2p->req_dev_types = NULL;
893 }
894
895
896 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
897 enum p2p_discovery_type type,
898 unsigned int num_req_dev_types, const u8 *req_dev_types,
899 const u8 *dev_id)
900 {
901 int res;
902
903 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
904 type);
905 if (p2p->p2p_scan_running) {
906 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
907 "already running");
908 }
909
910 p2p_free_req_dev_types(p2p);
911 if (req_dev_types && num_req_dev_types) {
912 p2p->req_dev_types = os_malloc(num_req_dev_types *
913 WPS_DEV_TYPE_LEN);
914 if (p2p->req_dev_types == NULL)
915 return -1;
916 os_memcpy(p2p->req_dev_types, req_dev_types,
917 num_req_dev_types * WPS_DEV_TYPE_LEN);
918 p2p->num_req_dev_types = num_req_dev_types;
919 }
920
921 if (dev_id) {
922 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
923 p2p->find_dev_id = p2p->find_dev_id_buf;
924 } else
925 p2p->find_dev_id = NULL;
926
927 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
928 p2p_clear_timeout(p2p);
929 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
930 p2p->find_type = type;
931 p2p_device_clear_reported(p2p);
932 p2p_set_state(p2p, P2P_SEARCH);
933 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
934 p2p->last_p2p_find_timeout = timeout;
935 if (timeout)
936 eloop_register_timeout(timeout, 0, p2p_find_timeout,
937 p2p, NULL);
938 switch (type) {
939 case P2P_FIND_START_WITH_FULL:
940 case P2P_FIND_PROGRESSIVE:
941 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
942 p2p->num_req_dev_types,
943 p2p->req_dev_types, dev_id);
944 break;
945 case P2P_FIND_ONLY_SOCIAL:
946 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
947 p2p->num_req_dev_types,
948 p2p->req_dev_types, dev_id);
949 break;
950 default:
951 return -1;
952 }
953
954 if (res == 0) {
955 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
956 p2p->p2p_scan_running = 1;
957 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
958 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
959 p2p, NULL);
960 } else if (res == 1) {
961 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
962 "p2p_scan at this point - will try again after "
963 "previous scan completes");
964 res = 0;
965 p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
966 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
967 } else {
968 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
969 "p2p_scan");
970 p2p_set_state(p2p, P2P_IDLE);
971 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
972 }
973
974 return res;
975 }
976
977
978 int p2p_other_scan_completed(struct p2p_data *p2p)
979 {
980 if (p2p->state != P2P_SEARCH_WHEN_READY)
981 return 0;
982 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find "
983 "now that previous scan was completed");
984 if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
985 p2p->num_req_dev_types, p2p->req_dev_types,
986 p2p->find_dev_id) < 0)
987 return 0;
988 return 1;
989 }
990
991
992 void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
993 {
994 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
995 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
996 p2p_clear_timeout(p2p);
997 p2p_set_state(p2p, P2P_IDLE);
998 p2p_free_req_dev_types(p2p);
999 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1000 p2p->go_neg_peer = NULL;
1001 p2p->sd_peer = NULL;
1002 p2p->invite_peer = NULL;
1003 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
1004 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
1005 "since we are on correct channel for response");
1006 return;
1007 }
1008 if (p2p->drv_in_listen) {
1009 /*
1010 * The driver may not deliver callback to p2p_listen_end()
1011 * when the operation gets canceled, so clear the internal
1012 * variable that is tracking driver state.
1013 */
1014 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear "
1015 "drv_in_listen (%d)", p2p->drv_in_listen);
1016 p2p->drv_in_listen = 0;
1017 }
1018 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1019 }
1020
1021
1022 void p2p_stop_find(struct p2p_data *p2p)
1023 {
1024 p2p_stop_find_for_freq(p2p, 0);
1025 }
1026
1027
1028 static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
1029 {
1030 if (force_freq) {
1031 u8 op_reg_class, op_channel;
1032 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
1033 &op_reg_class, &op_channel) < 0) {
1034 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1035 "P2P: Unsupported frequency %u MHz",
1036 force_freq);
1037 return -1;
1038 }
1039 if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
1040 op_channel)) {
1041 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1042 "P2P: Frequency %u MHz (oper_class %u "
1043 "channel %u) not allowed for P2P",
1044 force_freq, op_reg_class, op_channel);
1045 return -1;
1046 }
1047 p2p->op_reg_class = op_reg_class;
1048 p2p->op_channel = op_channel;
1049 p2p->channels.reg_classes = 1;
1050 p2p->channels.reg_class[0].channels = 1;
1051 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1052 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
1053 } else {
1054 u8 op_reg_class, op_channel;
1055
1056 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1057 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
1058 p2p_freq_to_channel(p2p->cfg->country,
1059 p2p->best_freq_overall,
1060 &op_reg_class, &op_channel) == 0) {
1061 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1062 "P2P: Select best overall channel as "
1063 "operating channel preference");
1064 p2p->op_reg_class = op_reg_class;
1065 p2p->op_channel = op_channel;
1066 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1067 p2p_supported_freq(p2p, p2p->best_freq_5) &&
1068 p2p_freq_to_channel(p2p->cfg->country,
1069 p2p->best_freq_5,
1070 &op_reg_class, &op_channel) ==
1071 0) {
1072 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1073 "P2P: Select best 5 GHz channel as "
1074 "operating channel preference");
1075 p2p->op_reg_class = op_reg_class;
1076 p2p->op_channel = op_channel;
1077 } else if (!p2p->cfg->cfg_op_channel &&
1078 p2p->best_freq_24 > 0 &&
1079 p2p_supported_freq(p2p, p2p->best_freq_24) &&
1080 p2p_freq_to_channel(p2p->cfg->country,
1081 p2p->best_freq_24,
1082 &op_reg_class, &op_channel) ==
1083 0) {
1084 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1085 "P2P: Select best 2.4 GHz channel as "
1086 "operating channel preference");
1087 p2p->op_reg_class = op_reg_class;
1088 p2p->op_channel = op_channel;
1089 } else {
1090 p2p->op_reg_class = p2p->cfg->op_reg_class;
1091 p2p->op_channel = p2p->cfg->op_channel;
1092 }
1093
1094 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1095 sizeof(struct p2p_channels));
1096 }
1097 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1098 "P2P: Own preference for operation channel: "
1099 "Operating Class %u Channel %u%s",
1100 p2p->op_reg_class, p2p->op_channel,
1101 force_freq ? " (forced)" : "");
1102
1103 return 0;
1104 }
1105
1106
1107 static void p2p_set_dev_persistent(struct p2p_device *dev,
1108 int persistent_group)
1109 {
1110 switch (persistent_group) {
1111 case 0:
1112 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1113 P2P_DEV_PREFER_PERSISTENT_RECONN);
1114 break;
1115 case 1:
1116 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1117 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1118 break;
1119 case 2:
1120 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1121 P2P_DEV_PREFER_PERSISTENT_RECONN;
1122 break;
1123 }
1124 }
1125
1126
1127 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1128 enum p2p_wps_method wps_method,
1129 int go_intent, const u8 *own_interface_addr,
1130 unsigned int force_freq, int persistent_group)
1131 {
1132 struct p2p_device *dev;
1133
1134 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1135 "P2P: Request to start group negotiation - peer=" MACSTR
1136 " GO Intent=%d Intended Interface Address=" MACSTR
1137 " wps_method=%d persistent_group=%d",
1138 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1139 wps_method, persistent_group);
1140
1141 if (p2p_prepare_channel(p2p, force_freq) < 0)
1142 return -1;
1143
1144 p2p->ssid_set = 0;
1145 dev = p2p_get_device(p2p, peer_addr);
1146 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1147 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1148 "P2P: Cannot connect to unknown P2P Device " MACSTR,
1149 MAC2STR(peer_addr));
1150 return -1;
1151 }
1152
1153 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1154 if (!(dev->info.dev_capab &
1155 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1156 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1157 "P2P: Cannot connect to P2P Device " MACSTR
1158 " that is in a group and is not discoverable",
1159 MAC2STR(peer_addr));
1160 return -1;
1161 }
1162 if (dev->oper_freq <= 0) {
1163 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1164 "P2P: Cannot connect to P2P Device " MACSTR
1165 " with incomplete information",
1166 MAC2STR(peer_addr));
1167 return -1;
1168 }
1169
1170 /*
1171 * First, try to connect directly. If the peer does not
1172 * acknowledge frames, assume it is sleeping and use device
1173 * discoverability via the GO at that point.
1174 */
1175 }
1176
1177 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1178 dev->flags &= ~P2P_DEV_USER_REJECTED;
1179 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1180 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1181 dev->connect_reqs = 0;
1182 dev->go_neg_req_sent = 0;
1183 dev->go_state = UNKNOWN_GO;
1184 p2p_set_dev_persistent(dev, persistent_group);
1185 p2p->go_intent = go_intent;
1186 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1187
1188 if (p2p->state != P2P_IDLE)
1189 p2p_stop_find(p2p);
1190
1191 if (p2p->after_scan_tx) {
1192 /*
1193 * We need to drop the pending frame to avoid issues with the
1194 * new GO Negotiation, e.g., when the pending frame was from a
1195 * previous attempt at starting a GO Negotiation.
1196 */
1197 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
1198 "previous pending Action frame TX that was waiting "
1199 "for p2p_scan completion");
1200 os_free(p2p->after_scan_tx);
1201 p2p->after_scan_tx = NULL;
1202 }
1203
1204 dev->wps_method = wps_method;
1205 dev->status = P2P_SC_SUCCESS;
1206
1207 if (force_freq)
1208 dev->flags |= P2P_DEV_FORCE_FREQ;
1209 else
1210 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1211
1212 if (p2p->p2p_scan_running) {
1213 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1214 "P2P: p2p_scan running - delay connect send");
1215 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1216 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1217 return 0;
1218 }
1219 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1220
1221 return p2p_connect_send(p2p, dev);
1222 }
1223
1224
1225 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1226 enum p2p_wps_method wps_method,
1227 int go_intent, const u8 *own_interface_addr,
1228 unsigned int force_freq, int persistent_group)
1229 {
1230 struct p2p_device *dev;
1231
1232 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1233 "P2P: Request to authorize group negotiation - peer=" MACSTR
1234 " GO Intent=%d Intended Interface Address=" MACSTR
1235 " wps_method=%d persistent_group=%d",
1236 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1237 wps_method, persistent_group);
1238
1239 if (p2p_prepare_channel(p2p, force_freq) < 0)
1240 return -1;
1241
1242 dev = p2p_get_device(p2p, peer_addr);
1243 if (dev == NULL) {
1244 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1245 "P2P: Cannot authorize unknown P2P Device " MACSTR,
1246 MAC2STR(peer_addr));
1247 return -1;
1248 }
1249
1250 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1251 dev->flags &= ~P2P_DEV_USER_REJECTED;
1252 dev->go_neg_req_sent = 0;
1253 dev->go_state = UNKNOWN_GO;
1254 p2p_set_dev_persistent(dev, persistent_group);
1255 p2p->go_intent = go_intent;
1256 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1257
1258 dev->wps_method = wps_method;
1259 dev->status = P2P_SC_SUCCESS;
1260
1261 if (force_freq)
1262 dev->flags |= P2P_DEV_FORCE_FREQ;
1263 else
1264 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1265
1266 return 0;
1267 }
1268
1269
1270 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1271 struct p2p_device *dev, struct p2p_message *msg)
1272 {
1273 os_get_time(&dev->last_seen);
1274
1275 p2p_copy_wps_info(dev, 0, msg);
1276
1277 if (msg->listen_channel) {
1278 int freq;
1279 freq = p2p_channel_to_freq((char *) msg->listen_channel,
1280 msg->listen_channel[3],
1281 msg->listen_channel[4]);
1282 if (freq < 0) {
1283 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1284 "P2P: Unknown peer Listen channel: "
1285 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1286 msg->listen_channel[0],
1287 msg->listen_channel[1],
1288 msg->listen_channel[2],
1289 msg->listen_channel[3],
1290 msg->listen_channel[4]);
1291 } else {
1292 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1293 "peer " MACSTR " Listen channel: %u -> %u MHz",
1294 MAC2STR(dev->info.p2p_device_addr),
1295 dev->listen_freq, freq);
1296 dev->listen_freq = freq;
1297 }
1298 }
1299
1300 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1301 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1302 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1303 "P2P: Completed device entry based on data from "
1304 "GO Negotiation Request");
1305 } else {
1306 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1307 "P2P: Created device entry based on GO Neg Req: "
1308 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1309 "listen_freq=%d",
1310 MAC2STR(dev->info.p2p_device_addr),
1311 dev->info.dev_capab, dev->info.group_capab,
1312 dev->info.device_name, dev->listen_freq);
1313 }
1314
1315 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1316
1317 if (dev->flags & P2P_DEV_USER_REJECTED) {
1318 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1319 "P2P: Do not report rejected device");
1320 return;
1321 }
1322
1323 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1324 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1325 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1326 }
1327
1328
1329 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1330 {
1331 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1332 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1333 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1334 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1335 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1336 }
1337
1338
1339 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1340 {
1341 p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1342 p2p_random(params->passphrase, 8);
1343 return 0;
1344 }
1345
1346
1347 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1348 {
1349 struct p2p_go_neg_results res;
1350 int go = peer->go_state == LOCAL_GO;
1351 struct p2p_channels intersection;
1352 int freqs;
1353 size_t i, j;
1354
1355 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1356 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1357 "GO)", MAC2STR(peer->info.p2p_device_addr),
1358 go ? "local end" : "peer");
1359
1360 os_memset(&res, 0, sizeof(res));
1361 res.role_go = go;
1362 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1363 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1364 res.wps_method = peer->wps_method;
1365 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1366 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1367 res.persistent_group = 2;
1368 else
1369 res.persistent_group = 1;
1370 }
1371
1372 if (go) {
1373 /* Setup AP mode for WPS provisioning */
1374 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1375 p2p->op_reg_class,
1376 p2p->op_channel);
1377 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1378 res.ssid_len = p2p->ssid_len;
1379 p2p_random(res.passphrase, 8);
1380 } else {
1381 res.freq = peer->oper_freq;
1382 if (p2p->ssid_len) {
1383 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1384 res.ssid_len = p2p->ssid_len;
1385 }
1386 }
1387
1388 p2p_channels_intersect(&p2p->channels, &peer->channels,
1389 &intersection);
1390 freqs = 0;
1391 for (i = 0; i < intersection.reg_classes; i++) {
1392 struct p2p_reg_class *c = &intersection.reg_class[i];
1393 if (freqs + 1 == P2P_MAX_CHANNELS)
1394 break;
1395 for (j = 0; j < c->channels; j++) {
1396 int freq;
1397 if (freqs + 1 == P2P_MAX_CHANNELS)
1398 break;
1399 freq = p2p_channel_to_freq(peer->country, c->reg_class,
1400 c->channel[j]);
1401 if (freq < 0)
1402 continue;
1403 res.freq_list[freqs++] = freq;
1404 }
1405 }
1406
1407 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1408
1409 p2p_clear_timeout(p2p);
1410 p2p->ssid_set = 0;
1411 peer->go_neg_req_sent = 0;
1412 peer->wps_method = WPS_NOT_READY;
1413
1414 p2p_set_state(p2p, P2P_PROVISIONING);
1415 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1416 }
1417
1418
1419 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1420 const u8 *data, size_t len, int rx_freq)
1421 {
1422 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1423 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1424 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1425
1426 if (len < 1)
1427 return;
1428
1429 switch (data[0]) {
1430 case P2P_GO_NEG_REQ:
1431 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1432 break;
1433 case P2P_GO_NEG_RESP:
1434 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1435 break;
1436 case P2P_GO_NEG_CONF:
1437 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1438 break;
1439 case P2P_INVITATION_REQ:
1440 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1441 rx_freq);
1442 break;
1443 case P2P_INVITATION_RESP:
1444 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1445 break;
1446 case P2P_PROV_DISC_REQ:
1447 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1448 break;
1449 case P2P_PROV_DISC_RESP:
1450 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1451 break;
1452 case P2P_DEV_DISC_REQ:
1453 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1454 break;
1455 case P2P_DEV_DISC_RESP:
1456 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1457 break;
1458 default:
1459 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1460 "P2P: Unsupported P2P Public Action frame type %d",
1461 data[0]);
1462 break;
1463 }
1464 }
1465
1466
1467 static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1468 const u8 *sa, const u8 *bssid, const u8 *data,
1469 size_t len, int freq)
1470 {
1471 if (len < 1)
1472 return;
1473
1474 switch (data[0]) {
1475 case WLAN_PA_VENDOR_SPECIFIC:
1476 data++;
1477 len--;
1478 if (len < 3)
1479 return;
1480 if (WPA_GET_BE24(data) != OUI_WFA)
1481 return;
1482
1483 data += 3;
1484 len -= 3;
1485 if (len < 1)
1486 return;
1487
1488 if (*data != P2P_OUI_TYPE)
1489 return;
1490
1491 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1492 break;
1493 case WLAN_PA_GAS_INITIAL_REQ:
1494 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1495 break;
1496 case WLAN_PA_GAS_INITIAL_RESP:
1497 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1498 break;
1499 case WLAN_PA_GAS_COMEBACK_REQ:
1500 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1501 break;
1502 case WLAN_PA_GAS_COMEBACK_RESP:
1503 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1504 break;
1505 }
1506 }
1507
1508
1509 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1510 const u8 *bssid, u8 category,
1511 const u8 *data, size_t len, int freq)
1512 {
1513 if (category == WLAN_ACTION_PUBLIC) {
1514 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1515 return;
1516 }
1517
1518 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1519 return;
1520
1521 if (len < 4)
1522 return;
1523
1524 if (WPA_GET_BE24(data) != OUI_WFA)
1525 return;
1526 data += 3;
1527 len -= 3;
1528
1529 if (*data != P2P_OUI_TYPE)
1530 return;
1531 data++;
1532 len--;
1533
1534 /* P2P action frame */
1535 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1536 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1537 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1538
1539 if (len < 1)
1540 return;
1541 switch (data[0]) {
1542 case P2P_NOA:
1543 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1544 "P2P: Received P2P Action - Notice of Absence");
1545 /* TODO */
1546 break;
1547 case P2P_PRESENCE_REQ:
1548 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1549 break;
1550 case P2P_PRESENCE_RESP:
1551 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1552 break;
1553 case P2P_GO_DISC_REQ:
1554 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1555 break;
1556 default:
1557 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1558 "P2P: Received P2P Action - unknown type %u", data[0]);
1559 break;
1560 }
1561 }
1562
1563
1564 static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1565 {
1566 struct p2p_data *p2p = eloop_ctx;
1567 if (p2p->go_neg_peer == NULL)
1568 return;
1569 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1570 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1571 p2p_connect_send(p2p, p2p->go_neg_peer);
1572 }
1573
1574
1575 static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1576 {
1577 struct p2p_data *p2p = eloop_ctx;
1578 if (p2p->invite_peer == NULL)
1579 return;
1580 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1581 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1582 }
1583
1584
1585 static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1586 const u8 *ie, size_t ie_len)
1587 {
1588 struct p2p_message msg;
1589 struct p2p_device *dev;
1590
1591 os_memset(&msg, 0, sizeof(msg));
1592 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1593 {
1594 p2p_parse_free(&msg);
1595 return; /* not a P2P probe */
1596 }
1597
1598 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1599 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1600 != 0) {
1601 /* The Probe Request is not part of P2P Device Discovery. It is
1602 * not known whether the source address of the frame is the P2P
1603 * Device Address or P2P Interface Address. Do not add a new
1604 * peer entry based on this frames.
1605 */
1606 p2p_parse_free(&msg);
1607 return;
1608 }
1609
1610 dev = p2p_get_device(p2p, addr);
1611 if (dev) {
1612 if (dev->country[0] == 0 && msg.listen_channel)
1613 os_memcpy(dev->country, msg.listen_channel, 3);
1614 os_get_time(&dev->last_seen);
1615 p2p_parse_free(&msg);
1616 return; /* already known */
1617 }
1618
1619 dev = p2p_create_device(p2p, addr);
1620 if (dev == NULL) {
1621 p2p_parse_free(&msg);
1622 return;
1623 }
1624
1625 os_get_time(&dev->last_seen);
1626 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1627
1628 if (msg.listen_channel) {
1629 os_memcpy(dev->country, msg.listen_channel, 3);
1630 dev->listen_freq = p2p_channel_to_freq(dev->country,
1631 msg.listen_channel[3],
1632 msg.listen_channel[4]);
1633 }
1634
1635 p2p_copy_wps_info(dev, 1, &msg);
1636
1637 p2p_parse_free(&msg);
1638
1639 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1640 "P2P: Created device entry based on Probe Req: " MACSTR
1641 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1642 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1643 dev->info.group_capab, dev->info.device_name,
1644 dev->listen_freq);
1645 }
1646
1647
1648 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1649 const u8 *addr,
1650 struct p2p_message *msg)
1651 {
1652 struct p2p_device *dev;
1653
1654 dev = p2p_get_device(p2p, addr);
1655 if (dev) {
1656 os_get_time(&dev->last_seen);
1657 return dev; /* already known */
1658 }
1659
1660 dev = p2p_create_device(p2p, addr);
1661 if (dev == NULL)
1662 return NULL;
1663
1664 p2p_add_dev_info(p2p, addr, dev, msg);
1665
1666 return dev;
1667 }
1668
1669
1670 static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1671 {
1672 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1673 return 1;
1674 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1675 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1676 WPA_GET_BE16(&req_dev_type[6]) == 0)
1677 return 1; /* Category match with wildcard OUI/sub-category */
1678 return 0;
1679 }
1680
1681
1682 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1683 size_t num_req_dev_type)
1684 {
1685 size_t i;
1686 for (i = 0; i < num_req_dev_type; i++) {
1687 if (dev_type_match(dev_type, req_dev_type[i]))
1688 return 1;
1689 }
1690 return 0;
1691 }
1692
1693
1694 /**
1695 * p2p_match_dev_type - Match local device type with requested type
1696 * @p2p: P2P module context from p2p_init()
1697 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1698 * Returns: 1 on match, 0 on mismatch
1699 *
1700 * This function can be used to match the Requested Device Type attribute in
1701 * WPS IE with the local device types for deciding whether to reply to a Probe
1702 * Request frame.
1703 */
1704 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1705 {
1706 struct wps_parse_attr attr;
1707 size_t i;
1708
1709 if (wps_parse_msg(wps, &attr))
1710 return 1; /* assume no Requested Device Type attributes */
1711
1712 if (attr.num_req_dev_type == 0)
1713 return 1; /* no Requested Device Type attributes -> match */
1714
1715 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1716 attr.num_req_dev_type))
1717 return 1; /* Own Primary Device Type matches */
1718
1719 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1720 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1721 attr.req_dev_type,
1722 attr.num_req_dev_type))
1723 return 1; /* Own Secondary Device Type matches */
1724
1725 /* No matching device type found */
1726 return 0;
1727 }
1728
1729
1730 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1731 {
1732 struct wpabuf *buf;
1733 u8 *len;
1734
1735 buf = wpabuf_alloc(1000);
1736 if (buf == NULL)
1737 return NULL;
1738
1739 p2p_build_wps_ie(p2p, buf, DEV_PW_DEFAULT, 1);
1740
1741 /* P2P IE */
1742 len = p2p_buf_add_ie_hdr(buf);
1743 p2p_buf_add_capability(buf, p2p->dev_capab, 0);
1744 if (p2p->ext_listen_interval)
1745 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1746 p2p->ext_listen_interval);
1747 p2p_buf_add_device_info(buf, p2p, NULL);
1748 p2p_buf_update_ie_hdr(buf, len);
1749
1750 return buf;
1751 }
1752
1753
1754 static int is_11b(u8 rate)
1755 {
1756 return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
1757 }
1758
1759
1760 static int supp_rates_11b_only(struct ieee802_11_elems *elems)
1761 {
1762 int num_11b = 0, num_others = 0;
1763 int i;
1764
1765 if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
1766 return 0;
1767
1768 for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
1769 if (is_11b(elems->supp_rates[i]))
1770 num_11b++;
1771 else
1772 num_others++;
1773 }
1774
1775 for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
1776 i++) {
1777 if (is_11b(elems->ext_supp_rates[i]))
1778 num_11b++;
1779 else
1780 num_others++;
1781 }
1782
1783 return num_11b > 0 && num_others == 0;
1784 }
1785
1786
1787 static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr,
1788 const u8 *dst, const u8 *bssid, const u8 *ie,
1789 size_t ie_len)
1790 {
1791 struct ieee802_11_elems elems;
1792 struct wpabuf *buf;
1793 struct ieee80211_mgmt *resp;
1794 struct p2p_message msg;
1795 struct wpabuf *ies;
1796
1797 if (!p2p->in_listen || !p2p->drv_in_listen) {
1798 /* not in Listen state - ignore Probe Request */
1799 return;
1800 }
1801
1802 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
1803 ParseFailed) {
1804 /* Ignore invalid Probe Request frames */
1805 return;
1806 }
1807
1808 if (elems.p2p == NULL) {
1809 /* not a P2P probe - ignore it */
1810 return;
1811 }
1812
1813 if (dst && !is_broadcast_ether_addr(dst) &&
1814 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
1815 /* Not sent to the broadcast address or our P2P Device Address
1816 */
1817 return;
1818 }
1819
1820 if (bssid && !is_broadcast_ether_addr(bssid)) {
1821 /* Not sent to the Wildcard BSSID */
1822 return;
1823 }
1824
1825 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
1826 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
1827 0) {
1828 /* not using P2P Wildcard SSID - ignore */
1829 return;
1830 }
1831
1832 if (supp_rates_11b_only(&elems)) {
1833 /* Indicates support for 11b rates only */
1834 return;
1835 }
1836
1837 os_memset(&msg, 0, sizeof(msg));
1838 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
1839 /* Could not parse P2P attributes */
1840 return;
1841 }
1842
1843 if (msg.device_id &&
1844 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN != 0)) {
1845 /* Device ID did not match */
1846 p2p_parse_free(&msg);
1847 return;
1848 }
1849
1850 /* Check Requested Device Type match */
1851 if (msg.wps_attributes &&
1852 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
1853 /* No match with Requested Device Type */
1854 p2p_parse_free(&msg);
1855 return;
1856 }
1857 p2p_parse_free(&msg);
1858
1859 if (!p2p->cfg->send_probe_resp)
1860 return; /* Response generated elsewhere */
1861
1862 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1863 "P2P: Reply to P2P Probe Request in Listen state");
1864
1865 /*
1866 * We do not really have a specific BSS that this frame is advertising,
1867 * so build a frame that has some information in valid format. This is
1868 * really only used for discovery purposes, not to learn exact BSS
1869 * parameters.
1870 */
1871 ies = p2p_build_probe_resp_ies(p2p);
1872 if (ies == NULL)
1873 return;
1874
1875 buf = wpabuf_alloc(200 + wpabuf_len(ies));
1876 if (buf == NULL) {
1877 wpabuf_free(ies);
1878 return;
1879 }
1880
1881 resp = NULL;
1882 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
1883
1884 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
1885 (WLAN_FC_STYPE_PROBE_RESP << 4));
1886 os_memcpy(resp->da, addr, ETH_ALEN);
1887 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
1888 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
1889 resp->u.probe_resp.beacon_int = host_to_le16(100);
1890 /* hardware or low-level driver will setup seq_ctrl and timestamp */
1891 resp->u.probe_resp.capab_info =
1892 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
1893 WLAN_CAPABILITY_PRIVACY |
1894 WLAN_CAPABILITY_SHORT_SLOT_TIME);
1895
1896 wpabuf_put_u8(buf, WLAN_EID_SSID);
1897 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
1898 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1899
1900 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
1901 wpabuf_put_u8(buf, 8);
1902 wpabuf_put_u8(buf, (60 / 5) | 0x80);
1903 wpabuf_put_u8(buf, 90 / 5);
1904 wpabuf_put_u8(buf, (120 / 5) | 0x80);
1905 wpabuf_put_u8(buf, 180 / 5);
1906 wpabuf_put_u8(buf, (240 / 5) | 0x80);
1907 wpabuf_put_u8(buf, 360 / 5);
1908 wpabuf_put_u8(buf, 480 / 5);
1909 wpabuf_put_u8(buf, 540 / 5);
1910
1911 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
1912 wpabuf_put_u8(buf, 1);
1913 wpabuf_put_u8(buf, p2p->cfg->channel);
1914
1915 wpabuf_put_buf(buf, ies);
1916 wpabuf_free(ies);
1917
1918 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
1919
1920 wpabuf_free(buf);
1921 }
1922
1923
1924 int p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
1925 const u8 *bssid, const u8 *ie, size_t ie_len)
1926 {
1927 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
1928
1929 p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
1930
1931 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
1932 p2p->go_neg_peer &&
1933 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
1934 == 0) {
1935 /* Received a Probe Request from GO Negotiation peer */
1936 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1937 "P2P: Found GO Negotiation peer - try to start GO "
1938 "negotiation from timeout");
1939 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
1940 return 1;
1941 }
1942
1943 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
1944 p2p->invite_peer &&
1945 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
1946 == 0) {
1947 /* Received a Probe Request from Invite peer */
1948 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1949 "P2P: Found Invite peer - try to start Invite from "
1950 "timeout");
1951 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
1952 return 1;
1953 }
1954
1955 return 0;
1956 }
1957
1958
1959 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
1960 u8 *buf, size_t len, struct wpabuf *p2p_ie)
1961 {
1962 struct wpabuf *tmp;
1963 u8 *lpos;
1964 size_t tmplen;
1965 int res;
1966 u8 group_capab;
1967
1968 if (p2p_ie == NULL)
1969 return 0; /* WLAN AP is not a P2P manager */
1970
1971 /*
1972 * (Re)Association Request - P2P IE
1973 * P2P Capability attribute (shall be present)
1974 * P2P Interface attribute (present if concurrent device and
1975 * P2P Management is enabled)
1976 */
1977 tmp = wpabuf_alloc(200);
1978 if (tmp == NULL)
1979 return -1;
1980
1981 lpos = p2p_buf_add_ie_hdr(tmp);
1982 group_capab = 0;
1983 if (p2p->num_groups > 0) {
1984 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
1985 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1986 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
1987 p2p->cross_connect)
1988 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
1989 }
1990 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
1991 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1992 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
1993 p2p_buf_add_p2p_interface(tmp, p2p);
1994 p2p_buf_update_ie_hdr(tmp, lpos);
1995
1996 tmplen = wpabuf_len(tmp);
1997 if (tmplen > len)
1998 res = -1;
1999 else {
2000 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2001 res = tmplen;
2002 }
2003 wpabuf_free(tmp);
2004
2005 return res;
2006 }
2007
2008
2009 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2010 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2011 {
2012 struct wpabuf *tmp;
2013 u8 *lpos;
2014 struct p2p_device *peer;
2015 size_t tmplen;
2016 int res;
2017
2018 if (!p2p_group)
2019 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2020
2021 /*
2022 * (Re)Association Request - P2P IE
2023 * P2P Capability attribute (shall be present)
2024 * Extended Listen Timing (may be present)
2025 * P2P Device Info attribute (shall be present)
2026 */
2027 tmp = wpabuf_alloc(200);
2028 if (tmp == NULL)
2029 return -1;
2030
2031 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2032
2033 lpos = p2p_buf_add_ie_hdr(tmp);
2034 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2035 if (p2p->ext_listen_interval)
2036 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2037 p2p->ext_listen_interval);
2038 p2p_buf_add_device_info(tmp, p2p, peer);
2039 p2p_buf_update_ie_hdr(tmp, lpos);
2040
2041 tmplen = wpabuf_len(tmp);
2042 if (tmplen > len)
2043 res = -1;
2044 else {
2045 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2046 res = tmplen;
2047 }
2048 wpabuf_free(tmp);
2049
2050 return res;
2051 }
2052
2053
2054 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2055 {
2056 struct wpabuf *p2p_ie;
2057 int ret;
2058
2059 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2060 if (p2p_ie == NULL)
2061 return 0;
2062
2063 ret = p2p_attr_text(p2p_ie, buf, end);
2064 wpabuf_free(p2p_ie);
2065 return ret;
2066 }
2067
2068
2069 static void p2p_clear_go_neg(struct p2p_data *p2p)
2070 {
2071 p2p->go_neg_peer = NULL;
2072 p2p_clear_timeout(p2p);
2073 p2p_set_state(p2p, P2P_IDLE);
2074 }
2075
2076
2077 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2078 {
2079 if (p2p->go_neg_peer == NULL) {
2080 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2081 "P2P: No pending Group Formation - "
2082 "ignore WPS registration success notification");
2083 return; /* No pending Group Formation */
2084 }
2085
2086 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2087 0) {
2088 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2089 "P2P: Ignore WPS registration success notification "
2090 "for " MACSTR " (GO Negotiation peer " MACSTR ")",
2091 MAC2STR(mac_addr),
2092 MAC2STR(p2p->go_neg_peer->intended_addr));
2093 return; /* Ignore unexpected peer address */
2094 }
2095
2096 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2097 "P2P: Group Formation completed successfully with " MACSTR,
2098 MAC2STR(mac_addr));
2099
2100 p2p_clear_go_neg(p2p);
2101 }
2102
2103
2104 void p2p_group_formation_failed(struct p2p_data *p2p)
2105 {
2106 if (p2p->go_neg_peer == NULL) {
2107 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2108 "P2P: No pending Group Formation - "
2109 "ignore group formation failure notification");
2110 return; /* No pending Group Formation */
2111 }
2112
2113 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2114 "P2P: Group Formation failed with " MACSTR,
2115 MAC2STR(p2p->go_neg_peer->intended_addr));
2116
2117 p2p_clear_go_neg(p2p);
2118 }
2119
2120
2121 struct p2p_data * p2p_init(const struct p2p_config *cfg)
2122 {
2123 struct p2p_data *p2p;
2124
2125 if (cfg->max_peers < 1)
2126 return NULL;
2127
2128 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2129 if (p2p == NULL)
2130 return NULL;
2131 p2p->cfg = (struct p2p_config *) (p2p + 1);
2132 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2133 if (cfg->dev_name)
2134 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2135 if (cfg->manufacturer)
2136 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2137 if (cfg->model_name)
2138 p2p->cfg->model_name = os_strdup(cfg->model_name);
2139 if (cfg->model_number)
2140 p2p->cfg->model_number = os_strdup(cfg->model_number);
2141 if (cfg->serial_number)
2142 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
2143
2144 p2p->min_disc_int = 1;
2145 p2p->max_disc_int = 3;
2146
2147 os_get_random(&p2p->next_tie_breaker, 1);
2148 p2p->next_tie_breaker &= 0x01;
2149 if (cfg->sd_request)
2150 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2151 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2152 if (cfg->concurrent_operations)
2153 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2154 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2155
2156 dl_list_init(&p2p->devices);
2157
2158 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
2159 p2p_expiration_timeout, p2p, NULL);
2160
2161 return p2p;
2162 }
2163
2164
2165 void p2p_deinit(struct p2p_data *p2p)
2166 {
2167 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
2168 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2169 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2170 p2p_flush(p2p);
2171 p2p_free_req_dev_types(p2p);
2172 os_free(p2p->cfg->dev_name);
2173 os_free(p2p->cfg->manufacturer);
2174 os_free(p2p->cfg->model_name);
2175 os_free(p2p->cfg->model_number);
2176 os_free(p2p->cfg->serial_number);
2177 os_free(p2p->groups);
2178 wpabuf_free(p2p->sd_resp);
2179 os_free(p2p->after_scan_tx);
2180 p2p_remove_wps_vendor_extensions(p2p);
2181 os_free(p2p);
2182 }
2183
2184
2185 void p2p_flush(struct p2p_data *p2p)
2186 {
2187 struct p2p_device *dev, *prev;
2188 p2p_clear_timeout(p2p);
2189 p2p_set_state(p2p, P2P_IDLE);
2190 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
2191 p2p->go_neg_peer = NULL;
2192 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
2193 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2194 list) {
2195 dl_list_del(&dev->list);
2196 p2p_device_free(p2p, dev);
2197 }
2198 p2p_free_sd_queries(p2p);
2199 os_free(p2p->after_scan_tx);
2200 p2p->after_scan_tx = NULL;
2201 }
2202
2203
2204 int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2205 {
2206 struct p2p_device *dev;
2207
2208 dev = p2p_get_device(p2p, addr);
2209 if (dev == NULL)
2210 return -1;
2211
2212 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
2213 MAC2STR(addr));
2214
2215 if (p2p->go_neg_peer == dev)
2216 p2p->go_neg_peer = NULL;
2217
2218 dev->wps_method = WPS_NOT_READY;
2219 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2220 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2221
2222 /* Check if after_scan_tx is for this peer. If so free it */
2223 if (p2p->after_scan_tx &&
2224 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2225 os_free(p2p->after_scan_tx);
2226 p2p->after_scan_tx = NULL;
2227 }
2228
2229 return 0;
2230 }
2231
2232
2233 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2234 {
2235 os_free(p2p->cfg->dev_name);
2236 if (dev_name) {
2237 p2p->cfg->dev_name = os_strdup(dev_name);
2238 if (p2p->cfg->dev_name == NULL)
2239 return -1;
2240 } else
2241 p2p->cfg->dev_name = NULL;
2242 return 0;
2243 }
2244
2245
2246 int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2247 {
2248 os_free(p2p->cfg->manufacturer);
2249 p2p->cfg->manufacturer = NULL;
2250 if (manufacturer) {
2251 p2p->cfg->manufacturer = os_strdup(manufacturer);
2252 if (p2p->cfg->manufacturer == NULL)
2253 return -1;
2254 }
2255
2256 return 0;
2257 }
2258
2259
2260 int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2261 {
2262 os_free(p2p->cfg->model_name);
2263 p2p->cfg->model_name = NULL;
2264 if (model_name) {
2265 p2p->cfg->model_name = os_strdup(model_name);
2266 if (p2p->cfg->model_name == NULL)
2267 return -1;
2268 }
2269
2270 return 0;
2271 }
2272
2273
2274 int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2275 {
2276 os_free(p2p->cfg->model_number);
2277 p2p->cfg->model_number = NULL;
2278 if (model_number) {
2279 p2p->cfg->model_number = os_strdup(model_number);
2280 if (p2p->cfg->model_number == NULL)
2281 return -1;
2282 }
2283
2284 return 0;
2285 }
2286
2287
2288 int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2289 {
2290 os_free(p2p->cfg->serial_number);
2291 p2p->cfg->serial_number = NULL;
2292 if (serial_number) {
2293 p2p->cfg->serial_number = os_strdup(serial_number);
2294 if (p2p->cfg->serial_number == NULL)
2295 return -1;
2296 }
2297
2298 return 0;
2299 }
2300
2301
2302 void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2303 {
2304 p2p->cfg->config_methods = config_methods;
2305 }
2306
2307
2308 void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2309 {
2310 os_memcpy(p2p->cfg->uuid, uuid, 16);
2311 }
2312
2313
2314 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2315 {
2316 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2317 return 0;
2318 }
2319
2320
2321 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2322 size_t num_dev_types)
2323 {
2324 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2325 num_dev_types = P2P_SEC_DEVICE_TYPES;
2326 p2p->cfg->num_sec_dev_types = num_dev_types;
2327 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2328 return 0;
2329 }
2330
2331
2332 void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2333 {
2334 int i;
2335
2336 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2337 wpabuf_free(p2p->wps_vendor_ext[i]);
2338 p2p->wps_vendor_ext[i] = NULL;
2339 }
2340 }
2341
2342
2343 int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2344 const struct wpabuf *vendor_ext)
2345 {
2346 int i;
2347
2348 if (vendor_ext == NULL)
2349 return -1;
2350
2351 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2352 if (p2p->wps_vendor_ext[i] == NULL)
2353 break;
2354 }
2355 if (i >= P2P_MAX_WPS_VENDOR_EXT)
2356 return -1;
2357
2358 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2359 if (p2p->wps_vendor_ext[i] == NULL)
2360 return -1;
2361
2362 return 0;
2363 }
2364
2365
2366 int p2p_set_country(struct p2p_data *p2p, const char *country)
2367 {
2368 os_memcpy(p2p->cfg->country, country, 3);
2369 return 0;
2370 }
2371
2372
2373 void p2p_continue_find(struct p2p_data *p2p)
2374 {
2375 struct p2p_device *dev;
2376 p2p_set_state(p2p, P2P_SEARCH);
2377 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2378 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2379 if (p2p_start_sd(p2p, dev) == 0)
2380 return;
2381 else
2382 break;
2383 } else if (dev->req_config_methods &&
2384 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2385 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2386 "pending Provision Discovery Request to "
2387 MACSTR " (config methods 0x%x)",
2388 MAC2STR(dev->info.p2p_device_addr),
2389 dev->req_config_methods);
2390 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
2391 return;
2392 }
2393 }
2394
2395 p2p_listen_in_find(p2p);
2396 }
2397
2398
2399 static void p2p_sd_cb(struct p2p_data *p2p, int success)
2400 {
2401 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2402 "P2P: Service Discovery Query TX callback: success=%d",
2403 success);
2404 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2405
2406 if (!success) {
2407 if (p2p->sd_peer) {
2408 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2409 p2p->sd_peer = NULL;
2410 }
2411 p2p_continue_find(p2p);
2412 return;
2413 }
2414
2415 if (p2p->sd_peer == NULL) {
2416 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2417 "P2P: No SD peer entry known");
2418 p2p_continue_find(p2p);
2419 return;
2420 }
2421
2422 /* Wait for response from the peer */
2423 p2p_set_state(p2p, P2P_SD_DURING_FIND);
2424 p2p_set_timeout(p2p, 0, 200000);
2425 }
2426
2427
2428 /**
2429 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2430 * @p2p: P2P module context from p2p_init()
2431 */
2432 static void p2p_retry_pd(struct p2p_data *p2p)
2433 {
2434 struct p2p_device *dev;
2435
2436 if (p2p->state != P2P_IDLE)
2437 return;
2438
2439 /*
2440 * Retry the prov disc req attempt only for the peer that the user had
2441 * requested for and provided a join has not been initiated on it
2442 * in the meantime.
2443 */
2444
2445 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2446 if (os_memcmp(p2p->pending_pd_devaddr,
2447 dev->info.p2p_device_addr, ETH_ALEN) != 0)
2448 continue;
2449 if (!dev->req_config_methods)
2450 continue;
2451 if (dev->flags & P2P_DEV_PD_FOR_JOIN)
2452 continue;
2453
2454 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2455 "pending Provision Discovery Request to "
2456 MACSTR " (config methods 0x%x)",
2457 MAC2STR(dev->info.p2p_device_addr),
2458 dev->req_config_methods);
2459 p2p_send_prov_disc_req(p2p, dev, 0, 0);
2460 return;
2461 }
2462 }
2463
2464
2465 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2466 {
2467 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2468 "P2P: Provision Discovery Request TX callback: success=%d",
2469 success);
2470
2471 /*
2472 * Postpone resetting the pending action state till after we actually
2473 * time out. This allows us to take some action like notifying any
2474 * interested parties about no response to the request.
2475 *
2476 * When the timer (below) goes off we check in IDLE, SEARCH, or
2477 * LISTEN_ONLY state, which are the only allowed states to issue a PD
2478 * requests in, if this was still pending and then raise notification.
2479 */
2480
2481 if (!success) {
2482 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2483
2484 if (p2p->state != P2P_IDLE)
2485 p2p_continue_find(p2p);
2486 else if (p2p->user_initiated_pd) {
2487 p2p->pending_action_state = P2P_PENDING_PD;
2488 p2p_set_timeout(p2p, 0, 300000);
2489 }
2490 return;
2491 }
2492
2493 /*
2494 * This postponing, of resetting pending_action_state, needs to be
2495 * done only for user initiated PD requests and not internal ones.
2496 */
2497 if (p2p->user_initiated_pd)
2498 p2p->pending_action_state = P2P_PENDING_PD;
2499 else
2500 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2501
2502 /* Wait for response from the peer */
2503 if (p2p->state == P2P_SEARCH)
2504 p2p_set_state(p2p, P2P_PD_DURING_FIND);
2505 p2p_set_timeout(p2p, 0, 200000);
2506 }
2507
2508
2509 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
2510 int level, const u8 *ies, size_t ies_len)
2511 {
2512 p2p_add_device(p2p, bssid, freq, level, ies, ies_len);
2513
2514 if (p2p->go_neg_peer && p2p->state == P2P_SEARCH &&
2515 os_memcmp(p2p->go_neg_peer->info.p2p_device_addr, bssid, ETH_ALEN)
2516 == 0) {
2517 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2518 "P2P: Found GO Negotiation peer - try to start GO "
2519 "negotiation");
2520 p2p_connect_send(p2p, p2p->go_neg_peer);
2521 return 1;
2522 }
2523
2524 return 0;
2525 }
2526
2527
2528 void p2p_scan_res_handled(struct p2p_data *p2p)
2529 {
2530 if (!p2p->p2p_scan_running) {
2531 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
2532 "running, but scan results received");
2533 }
2534 p2p->p2p_scan_running = 0;
2535 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2536
2537 if (p2p_run_after_scan(p2p))
2538 return;
2539 if (p2p->state == P2P_SEARCH)
2540 p2p_continue_find(p2p);
2541 }
2542
2543
2544 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
2545 {
2546 u8 *len = p2p_buf_add_ie_hdr(ies);
2547 p2p_buf_add_capability(ies, p2p->dev_capab, 0);
2548 if (dev_id)
2549 p2p_buf_add_device_id(ies, dev_id);
2550 if (p2p->cfg->reg_class && p2p->cfg->channel)
2551 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2552 p2p->cfg->reg_class,
2553 p2p->cfg->channel);
2554 if (p2p->ext_listen_interval)
2555 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2556 p2p->ext_listen_interval);
2557 /* TODO: p2p_buf_add_operating_channel() if GO */
2558 p2p_buf_update_ie_hdr(ies, len);
2559 }
2560
2561
2562 size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
2563 {
2564 return 100;
2565 }
2566
2567
2568 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2569 {
2570 return p2p_attr_text(p2p_ie, buf, end);
2571 }
2572
2573
2574 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2575 {
2576 struct p2p_device *dev = p2p->go_neg_peer;
2577
2578 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2579 "P2P: GO Negotiation Request TX callback: success=%d",
2580 success);
2581
2582 if (dev == NULL) {
2583 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2584 "P2P: No pending GO Negotiation");
2585 return;
2586 }
2587
2588 if (success) {
2589 dev->go_neg_req_sent++;
2590 if (dev->flags & P2P_DEV_USER_REJECTED) {
2591 p2p_set_state(p2p, P2P_IDLE);
2592 return;
2593 }
2594 }
2595
2596 if (!success &&
2597 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2598 !is_zero_ether_addr(dev->member_in_go_dev)) {
2599 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2600 "P2P: Peer " MACSTR " did not acknowledge request - "
2601 "try to use device discoverability through its GO",
2602 MAC2STR(dev->info.p2p_device_addr));
2603 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2604 p2p_send_dev_disc_req(p2p, dev);
2605 return;
2606 }
2607
2608 /*
2609 * Use P2P find, if needed, to find the other device from its listen
2610 * channel.
2611 */
2612 p2p_set_state(p2p, P2P_CONNECT);
2613 p2p_set_timeout(p2p, 0, 100000);
2614 }
2615
2616
2617 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2618 {
2619 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2620 "P2P: GO Negotiation Response TX callback: success=%d",
2621 success);
2622 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2623 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2624 "P2P: Ignore TX callback event - GO Negotiation is "
2625 "not running anymore");
2626 return;
2627 }
2628 p2p_set_state(p2p, P2P_CONNECT);
2629 p2p_set_timeout(p2p, 0, 100000);
2630 }
2631
2632
2633 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2634 {
2635 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2636 "P2P: GO Negotiation Response (failure) TX callback: "
2637 "success=%d", success);
2638 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2639 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2640 p2p->go_neg_peer->status);
2641 }
2642 }
2643
2644
2645 static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
2646 enum p2p_send_action_result result)
2647 {
2648 struct p2p_device *dev;
2649
2650 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2651 "P2P: GO Negotiation Confirm TX callback: result=%d",
2652 result);
2653 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2654 if (result == P2P_SEND_ACTION_FAILED) {
2655 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2656 return;
2657 }
2658 if (result == P2P_SEND_ACTION_NO_ACK) {
2659 /*
2660 * It looks like the TX status for GO Negotiation Confirm is
2661 * often showing failure even when the peer has actually
2662 * received the frame. Since the peer may change channels
2663 * immediately after having received the frame, we may not see
2664 * an Ack for retries, so just dropping a single frame may
2665 * trigger this. To allow the group formation to succeed if the
2666 * peer did indeed receive the frame, continue regardless of
2667 * the TX status.
2668 */
2669 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2670 "P2P: Assume GO Negotiation Confirm TX was actually "
2671 "received by the peer even though Ack was not "
2672 "reported");
2673 }
2674
2675 dev = p2p->go_neg_peer;
2676 if (dev == NULL)
2677 return;
2678
2679 p2p_go_complete(p2p, dev);
2680 }
2681
2682
2683 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
2684 const u8 *src, const u8 *bssid,
2685 enum p2p_send_action_result result)
2686 {
2687 enum p2p_pending_action_state state;
2688 int success;
2689
2690 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2691 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
2692 " src=" MACSTR " bssid=" MACSTR " result=%d",
2693 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
2694 MAC2STR(bssid), result);
2695 success = result == P2P_SEND_ACTION_SUCCESS;
2696 state = p2p->pending_action_state;
2697 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2698 switch (state) {
2699 case P2P_NO_PENDING_ACTION:
2700 break;
2701 case P2P_PENDING_GO_NEG_REQUEST:
2702 p2p_go_neg_req_cb(p2p, success);
2703 break;
2704 case P2P_PENDING_GO_NEG_RESPONSE:
2705 p2p_go_neg_resp_cb(p2p, success);
2706 break;
2707 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
2708 p2p_go_neg_resp_failure_cb(p2p, success);
2709 break;
2710 case P2P_PENDING_GO_NEG_CONFIRM:
2711 p2p_go_neg_conf_cb(p2p, result);
2712 break;
2713 case P2P_PENDING_SD:
2714 p2p_sd_cb(p2p, success);
2715 break;
2716 case P2P_PENDING_PD:
2717 p2p_prov_disc_cb(p2p, success);
2718 break;
2719 case P2P_PENDING_INVITATION_REQUEST:
2720 p2p_invitation_req_cb(p2p, success);
2721 break;
2722 case P2P_PENDING_INVITATION_RESPONSE:
2723 p2p_invitation_resp_cb(p2p, success);
2724 break;
2725 case P2P_PENDING_DEV_DISC_REQUEST:
2726 p2p_dev_disc_req_cb(p2p, success);
2727 break;
2728 case P2P_PENDING_DEV_DISC_RESPONSE:
2729 p2p_dev_disc_resp_cb(p2p, success);
2730 break;
2731 case P2P_PENDING_GO_DISC_REQ:
2732 p2p_go_disc_req_cb(p2p, success);
2733 break;
2734 }
2735 }
2736
2737
2738 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
2739 unsigned int duration)
2740 {
2741 if (freq == p2p->pending_client_disc_freq) {
2742 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2743 "P2P: Client discoverability remain-awake completed");
2744 p2p->pending_client_disc_freq = 0;
2745 return;
2746 }
2747
2748 if (freq != p2p->pending_listen_freq) {
2749 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2750 "P2P: Unexpected listen callback for freq=%u "
2751 "duration=%u (pending_listen_freq=%u)",
2752 freq, duration, p2p->pending_listen_freq);
2753 return;
2754 }
2755
2756 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2757 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
2758 "callback",
2759 p2p->pending_listen_sec, p2p->pending_listen_usec,
2760 p2p->pending_listen_freq);
2761 p2p->in_listen = 1;
2762 p2p->drv_in_listen = freq;
2763 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
2764 /*
2765 * Add 20 msec extra wait to avoid race condition with driver
2766 * remain-on-channel end event, i.e., give driver more time to
2767 * complete the operation before our timeout expires.
2768 */
2769 p2p_set_timeout(p2p, p2p->pending_listen_sec,
2770 p2p->pending_listen_usec + 20000);
2771 }
2772
2773 p2p->pending_listen_freq = 0;
2774 }
2775
2776
2777 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
2778 {
2779 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
2780 "state (freq=%u)", freq);
2781 p2p->drv_in_listen = 0;
2782 if (p2p->in_listen)
2783 return 0; /* Internal timeout will trigger the next step */
2784
2785 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
2786 if (p2p->go_neg_peer->connect_reqs >= 120) {
2787 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2788 "P2P: Timeout on sending GO Negotiation "
2789 "Request without getting response");
2790 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2791 return 0;
2792 }
2793
2794 p2p_set_state(p2p, P2P_CONNECT);
2795 p2p_connect_send(p2p, p2p->go_neg_peer);
2796 return 1;
2797 } else if (p2p->state == P2P_SEARCH) {
2798 p2p_search(p2p);
2799 return 1;
2800 }
2801
2802 return 0;
2803 }
2804
2805
2806 static void p2p_timeout_connect(struct p2p_data *p2p)
2807 {
2808 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2809 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
2810 p2p_listen_in_find(p2p);
2811 }
2812
2813
2814 static void p2p_timeout_connect_listen(struct p2p_data *p2p)
2815 {
2816 if (p2p->go_neg_peer) {
2817 if (p2p->drv_in_listen) {
2818 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
2819 "still in Listen state; wait for it to "
2820 "complete");
2821 return;
2822 }
2823
2824 if (p2p->go_neg_peer->connect_reqs >= 120) {
2825 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2826 "P2P: Timeout on sending GO Negotiation "
2827 "Request without getting response");
2828 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2829 return;
2830 }
2831
2832 p2p_set_state(p2p, P2P_CONNECT);
2833 p2p_connect_send(p2p, p2p->go_neg_peer);
2834 } else
2835 p2p_set_state(p2p, P2P_IDLE);
2836 }
2837
2838
2839 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
2840 {
2841 /*
2842 * TODO: could remain constantly in Listen state for some time if there
2843 * are no other concurrent uses for the radio. For now, go to listen
2844 * state once per second to give other uses a chance to use the radio.
2845 */
2846 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
2847 p2p_set_timeout(p2p, 0, 500000);
2848 }
2849
2850
2851 static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
2852 {
2853 struct p2p_device *dev = p2p->go_neg_peer;
2854
2855 if (dev == NULL) {
2856 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2857 "P2P: Unknown GO Neg peer - stop GO Neg wait");
2858 return;
2859 }
2860
2861 dev->wait_count++;
2862 if (dev->wait_count >= 120) {
2863 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2864 "P2P: Timeout on waiting peer to become ready for GO "
2865 "Negotiation");
2866 p2p_go_neg_failed(p2p, dev, -1);
2867 return;
2868 }
2869
2870 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2871 "P2P: Go to Listen state while waiting for the peer to become "
2872 "ready for GO Negotiation");
2873 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
2874 p2p_listen_in_find(p2p);
2875 }
2876
2877
2878 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
2879 {
2880 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2881 "P2P: Service Discovery Query timeout");
2882 if (p2p->sd_peer) {
2883 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2884 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2885 p2p->sd_peer = NULL;
2886 }
2887 p2p_continue_find(p2p);
2888 }
2889
2890
2891 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
2892 {
2893 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2894 "P2P: Provision Discovery Request timeout");
2895 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2896 p2p_continue_find(p2p);
2897 }
2898
2899
2900 static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
2901 {
2902 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2903
2904 /*
2905 * For user initiated PD requests that we have not gotten any responses
2906 * for while in IDLE state, we retry them a couple of times before
2907 * giving up.
2908 */
2909 if (!p2p->user_initiated_pd)
2910 return;
2911
2912 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2913 "P2P: User initiated Provision Discovery Request timeout");
2914
2915 if (p2p->pd_retries) {
2916 p2p->pd_retries--;
2917 p2p_retry_pd(p2p);
2918 } else {
2919 if (p2p->cfg->prov_disc_fail)
2920 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
2921 p2p->pending_pd_devaddr,
2922 P2P_PROV_DISC_TIMEOUT);
2923 p2p_reset_pending_pd(p2p);
2924 }
2925 }
2926
2927
2928 static void p2p_timeout_invite(struct p2p_data *p2p)
2929 {
2930 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2931 p2p_set_state(p2p, P2P_INVITE_LISTEN);
2932 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
2933 /*
2934 * Better remain on operating channel instead of listen channel
2935 * when running a group.
2936 */
2937 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
2938 "active GO role - wait on operating channel");
2939 p2p_set_timeout(p2p, 0, 100000);
2940 return;
2941 }
2942 p2p_listen_in_find(p2p);
2943 }
2944
2945
2946 static void p2p_timeout_invite_listen(struct p2p_data *p2p)
2947 {
2948 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
2949 p2p_set_state(p2p, P2P_INVITE);
2950 p2p_invite_send(p2p, p2p->invite_peer,
2951 p2p->invite_go_dev_addr);
2952 } else {
2953 if (p2p->invite_peer) {
2954 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2955 "P2P: Invitation Request retry limit reached");
2956 if (p2p->cfg->invitation_result)
2957 p2p->cfg->invitation_result(
2958 p2p->cfg->cb_ctx, -1, NULL);
2959 }
2960 p2p_set_state(p2p, P2P_IDLE);
2961 }
2962 }
2963
2964
2965 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
2966 {
2967 struct p2p_data *p2p = eloop_ctx;
2968
2969 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
2970 p2p_state_txt(p2p->state));
2971
2972 p2p->in_listen = 0;
2973
2974 switch (p2p->state) {
2975 case P2P_IDLE:
2976 /* Check if we timed out waiting for PD req */
2977 if (p2p->pending_action_state == P2P_PENDING_PD)
2978 p2p_timeout_prov_disc_req(p2p);
2979 break;
2980 case P2P_SEARCH:
2981 /* Check if we timed out waiting for PD req */
2982 if (p2p->pending_action_state == P2P_PENDING_PD)
2983 p2p_timeout_prov_disc_req(p2p);
2984 p2p_search(p2p);
2985 break;
2986 case P2P_CONNECT:
2987 p2p_timeout_connect(p2p);
2988 break;
2989 case P2P_CONNECT_LISTEN:
2990 p2p_timeout_connect_listen(p2p);
2991 break;
2992 case P2P_GO_NEG:
2993 break;
2994 case P2P_LISTEN_ONLY:
2995 /* Check if we timed out waiting for PD req */
2996 if (p2p->pending_action_state == P2P_PENDING_PD)
2997 p2p_timeout_prov_disc_req(p2p);
2998
2999 if (p2p->ext_listen_only) {
3000 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3001 "P2P: Extended Listen Timing - Listen State "
3002 "completed");
3003 p2p->ext_listen_only = 0;
3004 p2p_set_state(p2p, P2P_IDLE);
3005 }
3006 break;
3007 case P2P_WAIT_PEER_CONNECT:
3008 p2p_timeout_wait_peer_connect(p2p);
3009 break;
3010 case P2P_WAIT_PEER_IDLE:
3011 p2p_timeout_wait_peer_idle(p2p);
3012 break;
3013 case P2P_SD_DURING_FIND:
3014 p2p_timeout_sd_during_find(p2p);
3015 break;
3016 case P2P_PROVISIONING:
3017 break;
3018 case P2P_PD_DURING_FIND:
3019 p2p_timeout_prov_disc_during_find(p2p);
3020 break;
3021 case P2P_INVITE:
3022 p2p_timeout_invite(p2p);
3023 break;
3024 case P2P_INVITE_LISTEN:
3025 p2p_timeout_invite_listen(p2p);
3026 break;
3027 case P2P_SEARCH_WHEN_READY:
3028 break;
3029 }
3030 }
3031
3032
3033 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
3034 {
3035 struct p2p_device *dev;
3036
3037 dev = p2p_get_device(p2p, peer_addr);
3038 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
3039 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
3040 if (dev == NULL) {
3041 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
3042 " unknown", MAC2STR(peer_addr));
3043 return -1;
3044 }
3045 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
3046 dev->flags |= P2P_DEV_USER_REJECTED;
3047 return 0;
3048 }
3049
3050
3051 const char * p2p_wps_method_text(enum p2p_wps_method method)
3052 {
3053 switch (method) {
3054 case WPS_NOT_READY:
3055 return "not-ready";
3056 case WPS_PIN_DISPLAY:
3057 return "Display";
3058 case WPS_PIN_KEYPAD:
3059 return "Keypad";
3060 case WPS_PBC:
3061 return "PBC";
3062 }
3063
3064 return "??";
3065 }
3066
3067
3068 static const char * p2p_go_state_text(enum p2p_go_state go_state)
3069 {
3070 switch (go_state) {
3071 case UNKNOWN_GO:
3072 return "unknown";
3073 case LOCAL_GO:
3074 return "local";
3075 case REMOTE_GO:
3076 return "remote";
3077 }
3078
3079 return "??";
3080 }
3081
3082
3083 const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
3084 const u8 *addr, int next)
3085 {
3086 struct p2p_device *dev;
3087
3088 if (addr)
3089 dev = p2p_get_device(p2p, addr);
3090 else
3091 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3092
3093 if (dev && next) {
3094 dev = dl_list_first(&dev->list, struct p2p_device, list);
3095 if (&dev->list == &p2p->devices)
3096 dev = NULL;
3097 }
3098
3099 if (dev == NULL)
3100 return NULL;
3101
3102 return &dev->info;
3103 }
3104
3105
3106 int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
3107 char *buf, size_t buflen)
3108 {
3109 struct p2p_device *dev;
3110 int res;
3111 char *pos, *end;
3112 struct os_time now;
3113
3114 if (info == NULL)
3115 return -1;
3116
3117 dev = (struct p2p_device *) (((u8 *) info) -
3118 offsetof(struct p2p_device, info));
3119
3120 pos = buf;
3121 end = buf + buflen;
3122
3123 os_get_time(&now);
3124 res = os_snprintf(pos, end - pos,
3125 "age=%d\n"
3126 "listen_freq=%d\n"
3127 "wps_method=%s\n"
3128 "interface_addr=" MACSTR "\n"
3129 "member_in_go_dev=" MACSTR "\n"
3130 "member_in_go_iface=" MACSTR "\n"
3131 "go_neg_req_sent=%d\n"
3132 "go_state=%s\n"
3133 "dialog_token=%u\n"
3134 "intended_addr=" MACSTR "\n"
3135 "country=%c%c\n"
3136 "oper_freq=%d\n"
3137 "req_config_methods=0x%x\n"
3138 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
3139 "status=%d\n"
3140 "wait_count=%u\n"
3141 "invitation_reqs=%u\n",
3142 (int) (now.sec - dev->last_seen.sec),
3143 dev->listen_freq,
3144 p2p_wps_method_text(dev->wps_method),
3145 MAC2STR(dev->interface_addr),
3146 MAC2STR(dev->member_in_go_dev),
3147 MAC2STR(dev->member_in_go_iface),
3148 dev->go_neg_req_sent,
3149 p2p_go_state_text(dev->go_state),
3150 dev->dialog_token,
3151 MAC2STR(dev->intended_addr),
3152 dev->country[0] ? dev->country[0] : '_',
3153 dev->country[1] ? dev->country[1] : '_',
3154 dev->oper_freq,
3155 dev->req_config_methods,
3156 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
3157 "[PROBE_REQ_ONLY]" : "",
3158 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
3159 dev->flags & P2P_DEV_NOT_YET_READY ?
3160 "[NOT_YET_READY]" : "",
3161 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
3162 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
3163 "",
3164 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
3165 "[PD_PEER_DISPLAY]" : "",
3166 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
3167 "[PD_PEER_KEYPAD]" : "",
3168 dev->flags & P2P_DEV_USER_REJECTED ?
3169 "[USER_REJECTED]" : "",
3170 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
3171 "[PEER_WAITING_RESPONSE]" : "",
3172 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
3173 "[PREFER_PERSISTENT_GROUP]" : "",
3174 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
3175 "[WAIT_GO_NEG_RESPONSE]" : "",
3176 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
3177 "[WAIT_GO_NEG_CONFIRM]" : "",
3178 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
3179 "[GROUP_CLIENT_ONLY]" : "",
3180 dev->flags & P2P_DEV_FORCE_FREQ ?
3181 "[FORCE_FREQ]" : "",
3182 dev->flags & P2P_DEV_PD_FOR_JOIN ?
3183 "[PD_FOR_JOIN]" : "",
3184 dev->status,
3185 dev->wait_count,
3186 dev->invitation_reqs);
3187 if (res < 0 || res >= end - pos)
3188 return pos - buf;
3189 pos += res;
3190
3191 if (dev->ext_listen_period) {
3192 res = os_snprintf(pos, end - pos,
3193 "ext_listen_period=%u\n"
3194 "ext_listen_interval=%u\n",
3195 dev->ext_listen_period,
3196 dev->ext_listen_interval);
3197 if (res < 0 || res >= end - pos)
3198 return pos - buf;
3199 pos += res;
3200 }
3201
3202 if (dev->oper_ssid_len) {
3203 res = os_snprintf(pos, end - pos,
3204 "oper_ssid=%s\n",
3205 wpa_ssid_txt(dev->oper_ssid,
3206 dev->oper_ssid_len));
3207 if (res < 0 || res >= end - pos)
3208 return pos - buf;
3209 pos += res;
3210 }
3211
3212 return pos - buf;
3213 }
3214
3215
3216 int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
3217 {
3218 return p2p_get_device(p2p, addr) != NULL;
3219 }
3220
3221
3222 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
3223 {
3224 if (enabled) {
3225 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3226 "discoverability enabled");
3227 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3228 } else {
3229 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3230 "discoverability disabled");
3231 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3232 }
3233 }
3234
3235
3236 static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
3237 u32 duration2, u32 interval2)
3238 {
3239 struct wpabuf *req;
3240 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
3241 u8 *len;
3242
3243 req = wpabuf_alloc(100);
3244 if (req == NULL)
3245 return NULL;
3246
3247 if (duration1 || interval1) {
3248 os_memset(&desc1, 0, sizeof(desc1));
3249 desc1.count_type = 1;
3250 desc1.duration = duration1;
3251 desc1.interval = interval1;
3252 ptr1 = &desc1;
3253
3254 if (duration2 || interval2) {
3255 os_memset(&desc2, 0, sizeof(desc2));
3256 desc2.count_type = 2;
3257 desc2.duration = duration2;
3258 desc2.interval = interval2;
3259 ptr2 = &desc2;
3260 }
3261 }
3262
3263 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
3264 len = p2p_buf_add_ie_hdr(req);
3265 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
3266 p2p_buf_update_ie_hdr(req, len);
3267
3268 return req;
3269 }
3270
3271
3272 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
3273 const u8 *own_interface_addr, unsigned int freq,
3274 u32 duration1, u32 interval1, u32 duration2,
3275 u32 interval2)
3276 {
3277 struct wpabuf *req;
3278
3279 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
3280 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
3281 "int1=%u dur2=%u int2=%u",
3282 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
3283 freq, duration1, interval1, duration2, interval2);
3284
3285 req = p2p_build_presence_req(duration1, interval1, duration2,
3286 interval2);
3287 if (req == NULL)
3288 return -1;
3289
3290 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3291 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3292 go_interface_addr,
3293 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
3294 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3295 "P2P: Failed to send Action frame");
3296 }
3297 wpabuf_free(req);
3298
3299 return 0;
3300 }
3301
3302
3303 static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3304 size_t noa_len, u8 dialog_token)
3305 {
3306 struct wpabuf *resp;
3307 u8 *len;
3308
3309 resp = wpabuf_alloc(100 + noa_len);
3310 if (resp == NULL)
3311 return NULL;
3312
3313 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3314 len = p2p_buf_add_ie_hdr(resp);
3315 p2p_buf_add_status(resp, status);
3316 if (noa) {
3317 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3318 wpabuf_put_le16(resp, noa_len);
3319 wpabuf_put_data(resp, noa, noa_len);
3320 } else
3321 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3322 p2p_buf_update_ie_hdr(resp, len);
3323
3324 return resp;
3325 }
3326
3327
3328 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3329 const u8 *sa, const u8 *data, size_t len,
3330 int rx_freq)
3331 {
3332 struct p2p_message msg;
3333 u8 status;
3334 struct wpabuf *resp;
3335 size_t g;
3336 struct p2p_group *group = NULL;
3337 int parsed = 0;
3338 u8 noa[50];
3339 int noa_len;
3340
3341 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3342 "P2P: Received P2P Action - P2P Presence Request");
3343
3344 for (g = 0; g < p2p->num_groups; g++) {
3345 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3346 ETH_ALEN) == 0) {
3347 group = p2p->groups[g];
3348 break;
3349 }
3350 }
3351 if (group == NULL) {
3352 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3353 "P2P: Ignore P2P Presence Request for unknown group "
3354 MACSTR, MAC2STR(da));
3355 return;
3356 }
3357
3358 if (p2p_parse(data, len, &msg) < 0) {
3359 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3360 "P2P: Failed to parse P2P Presence Request");
3361 status = P2P_SC_FAIL_INVALID_PARAMS;
3362 goto fail;
3363 }
3364 parsed = 1;
3365
3366 if (msg.noa == NULL) {
3367 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3368 "P2P: No NoA attribute in P2P Presence Request");
3369 status = P2P_SC_FAIL_INVALID_PARAMS;
3370 goto fail;
3371 }
3372
3373 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3374
3375 fail:
3376 if (p2p->cfg->get_noa)
3377 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3378 sizeof(noa));
3379 else
3380 noa_len = -1;
3381 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3382 noa_len > 0 ? noa_len : 0,
3383 msg.dialog_token);
3384 if (parsed)
3385 p2p_parse_free(&msg);
3386 if (resp == NULL)
3387 return;
3388
3389 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3390 if (p2p_send_action(p2p, rx_freq, sa, da, da,
3391 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
3392 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3393 "P2P: Failed to send Action frame");
3394 }
3395 wpabuf_free(resp);
3396 }
3397
3398
3399 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3400 const u8 *sa, const u8 *data, size_t len)
3401 {
3402 struct p2p_message msg;
3403
3404 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3405 "P2P: Received P2P Action - P2P Presence Response");
3406
3407 if (p2p_parse(data, len, &msg) < 0) {
3408 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3409 "P2P: Failed to parse P2P Presence Response");
3410 return;
3411 }
3412
3413 if (msg.status == NULL || msg.noa == NULL) {
3414 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3415 "P2P: No Status or NoA attribute in P2P Presence "
3416 "Response");
3417 p2p_parse_free(&msg);
3418 return;
3419 }
3420
3421 if (*msg.status) {
3422 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3423 "P2P: P2P Presence Request was rejected: status %u",
3424 *msg.status);
3425 p2p_parse_free(&msg);
3426 return;
3427 }
3428
3429 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3430 "P2P: P2P Presence Request was accepted");
3431 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
3432 msg.noa, msg.noa_len);
3433 /* TODO: process NoA */
3434 p2p_parse_free(&msg);
3435 }
3436
3437
3438 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3439 {
3440 struct p2p_data *p2p = eloop_ctx;
3441
3442 if (p2p->ext_listen_interval) {
3443 /* Schedule next extended listen timeout */
3444 eloop_register_timeout(p2p->ext_listen_interval_sec,
3445 p2p->ext_listen_interval_usec,
3446 p2p_ext_listen_timeout, p2p, NULL);
3447 }
3448
3449 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
3450 /*
3451 * This should not really happen, but it looks like the Listen
3452 * command may fail is something else (e.g., a scan) was
3453 * running at an inconvenient time. As a workaround, allow new
3454 * Extended Listen operation to be started.
3455 */
3456 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
3457 "Extended Listen operation had not been completed - "
3458 "try again");
3459 p2p->ext_listen_only = 0;
3460 p2p_set_state(p2p, P2P_IDLE);
3461 }
3462
3463 if (p2p->state != P2P_IDLE) {
3464 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
3465 "Listen timeout in active state (%s)",
3466 p2p_state_txt(p2p->state));
3467 return;
3468 }
3469
3470 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
3471 p2p->ext_listen_only = 1;
3472 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
3473 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
3474 "Listen state for Extended Listen Timing");
3475 p2p->ext_listen_only = 0;
3476 }
3477 }
3478
3479
3480 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
3481 unsigned int interval)
3482 {
3483 if (period > 65535 || interval > 65535 || period > interval ||
3484 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
3485 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3486 "P2P: Invalid Extended Listen Timing request: "
3487 "period=%u interval=%u", period, interval);
3488 return -1;
3489 }
3490
3491 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
3492
3493 if (interval == 0) {
3494 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3495 "P2P: Disabling Extended Listen Timing");
3496 p2p->ext_listen_period = 0;
3497 p2p->ext_listen_interval = 0;
3498 return 0;
3499 }
3500
3501 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3502 "P2P: Enabling Extended Listen Timing: period %u msec, "
3503 "interval %u msec", period, interval);
3504 p2p->ext_listen_period = period;
3505 p2p->ext_listen_interval = interval;
3506 p2p->ext_listen_interval_sec = interval / 1000;
3507 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
3508
3509 eloop_register_timeout(p2p->ext_listen_interval_sec,
3510 p2p->ext_listen_interval_usec,
3511 p2p_ext_listen_timeout, p2p, NULL);
3512
3513 return 0;
3514 }
3515
3516
3517 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3518 const u8 *ie, size_t ie_len)
3519 {
3520 struct p2p_message msg;
3521
3522 if (bssid == NULL || ie == NULL)
3523 return;
3524
3525 os_memset(&msg, 0, sizeof(msg));
3526 if (p2p_parse_ies(ie, ie_len, &msg))
3527 return;
3528 if (msg.minor_reason_code == NULL)
3529 return;
3530
3531 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3532 "P2P: Deauthentication notification BSSID " MACSTR
3533 " reason_code=%u minor_reason_code=%u",
3534 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3535
3536 p2p_parse_free(&msg);
3537 }
3538
3539
3540 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3541 const u8 *ie, size_t ie_len)
3542 {
3543 struct p2p_message msg;
3544
3545 if (bssid == NULL || ie == NULL)
3546 return;
3547
3548 os_memset(&msg, 0, sizeof(msg));
3549 if (p2p_parse_ies(ie, ie_len, &msg))
3550 return;
3551 if (msg.minor_reason_code == NULL)
3552 return;
3553
3554 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3555 "P2P: Disassociation notification BSSID " MACSTR
3556 " reason_code=%u minor_reason_code=%u",
3557 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3558
3559 p2p_parse_free(&msg);
3560 }
3561
3562
3563 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
3564 {
3565 if (enabled) {
3566 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3567 "Device operations enabled");
3568 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
3569 } else {
3570 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3571 "Device operations disabled");
3572 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
3573 }
3574 }
3575
3576
3577 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
3578 {
3579 if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
3580 return -1;
3581
3582 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
3583 "reg_class %u channel %u", reg_class, channel);
3584 p2p->cfg->reg_class = reg_class;
3585 p2p->cfg->channel = channel;
3586
3587 return 0;
3588 }
3589
3590
3591 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
3592 {
3593 wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
3594 if (postfix == NULL) {
3595 p2p->cfg->ssid_postfix_len = 0;
3596 return 0;
3597 }
3598 if (len > sizeof(p2p->cfg->ssid_postfix))
3599 return -1;
3600 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
3601 p2p->cfg->ssid_postfix_len = len;
3602 return 0;
3603 }
3604
3605
3606 int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
3607 int cfg_op_channel)
3608 {
3609 if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
3610 < 0)
3611 return -1;
3612
3613 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
3614 "reg_class %u channel %u", op_reg_class, op_channel);
3615 p2p->cfg->op_reg_class = op_reg_class;
3616 p2p->cfg->op_channel = op_channel;
3617 p2p->cfg->cfg_op_channel = cfg_op_channel;
3618 return 0;
3619 }
3620
3621
3622 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
3623 u8 *iface_addr)
3624 {
3625 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
3626 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
3627 return -1;
3628 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
3629 return 0;
3630 }
3631
3632
3633 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
3634 u8 *dev_addr)
3635 {
3636 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3637 if (dev == NULL)
3638 return -1;
3639 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
3640 return 0;
3641 }
3642
3643
3644 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
3645 {
3646 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
3647 if (is_zero_ether_addr(p2p->peer_filter))
3648 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
3649 "filter");
3650 else
3651 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
3652 "filter for " MACSTR, MAC2STR(p2p->peer_filter));
3653 }
3654
3655
3656 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
3657 {
3658 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
3659 enabled ? "enabled" : "disabled");
3660 if (p2p->cross_connect == enabled)
3661 return;
3662 p2p->cross_connect = enabled;
3663 /* TODO: may need to tear down any action group where we are GO(?) */
3664 }
3665
3666
3667 int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
3668 {
3669 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3670 if (dev == NULL)
3671 return -1;
3672 if (dev->oper_freq <= 0)
3673 return -1;
3674 return dev->oper_freq;
3675 }
3676
3677
3678 void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
3679 {
3680 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
3681 enabled ? "enabled" : "disabled");
3682 p2p->cfg->p2p_intra_bss = enabled;
3683 }
3684
3685
3686 void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
3687 {
3688 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
3689 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
3690 }
3691
3692
3693 int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3694 const u8 *src, const u8 *bssid, const u8 *buf,
3695 size_t len, unsigned int wait_time)
3696 {
3697 if (p2p->p2p_scan_running) {
3698 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
3699 "frame TX until p2p_scan completes");
3700 if (p2p->after_scan_tx) {
3701 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
3702 "previous pending Action frame TX");
3703 os_free(p2p->after_scan_tx);
3704 }
3705 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
3706 len);
3707 if (p2p->after_scan_tx == NULL)
3708 return -1;
3709 p2p->after_scan_tx->freq = freq;
3710 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
3711 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
3712 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
3713 p2p->after_scan_tx->len = len;
3714 p2p->after_scan_tx->wait_time = wait_time;
3715 os_memcpy(p2p->after_scan_tx + 1, buf, len);
3716 return 0;
3717 }
3718
3719 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
3720 buf, len, wait_time);
3721 }
3722
3723
3724 void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
3725 int freq_overall)
3726 {
3727 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
3728 " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall);
3729 p2p->best_freq_24 = freq_24;
3730 p2p->best_freq_5 = freq_5;
3731 p2p->best_freq_overall = freq_overall;
3732 }
3733
3734
3735 const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
3736 {
3737 if (p2p == NULL || p2p->go_neg_peer == NULL)
3738 return NULL;
3739 return p2p->go_neg_peer->info.p2p_device_addr;
3740 }
3741
3742
3743 const struct p2p_peer_info *
3744 p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
3745 {
3746 struct p2p_device *dev;
3747
3748 if (addr) {
3749 dev = p2p_get_device(p2p, addr);
3750 if (!dev)
3751 return NULL;
3752
3753 if (!next) {
3754 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
3755 return NULL;
3756
3757 return &dev->info;
3758 } else {
3759 do {
3760 dev = dl_list_first(&dev->list,
3761 struct p2p_device,
3762 list);
3763 if (&dev->list == &p2p->devices)
3764 return NULL;
3765 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
3766 }
3767 } else {
3768 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3769 if (!dev)
3770 return NULL;
3771 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
3772 dev = dl_list_first(&dev->list,
3773 struct p2p_device,
3774 list);
3775 if (&dev->list == &p2p->devices)
3776 return NULL;
3777 }
3778 }
3779
3780 return &dev->info;
3781 }
3782
3783
3784 int p2p_in_progress(struct p2p_data *p2p)
3785 {
3786 if (p2p == NULL)
3787 return 0;
3788 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
3789 }