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