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