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