blob: ab917dc92c7c00a77769b6a3a085e3f4f328d15f [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 */
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -070045#ifdef ANDROID_P2P
46#define P2P_PEER_EXPIRATION_AGE 30
47#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048#define P2P_PEER_EXPIRATION_AGE 300
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -070049#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070050
51#define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
52
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -070053#ifdef ANDROID_P2P
54int p2p_connection_in_progress(struct p2p_data *p2p)
55{
56 int ret = 0;
57
58 switch (p2p->state) {
59 case P2P_CONNECT:
60 case P2P_CONNECT_LISTEN:
61 case P2P_GO_NEG:
62 case P2P_WAIT_PEER_CONNECT:
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070063 case P2P_WAIT_PEER_IDLE:
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -070064 case P2P_PROVISIONING:
65 case P2P_INVITE:
66 case P2P_INVITE_LISTEN:
67 ret = 1;
68 break;
69
70 default:
Dmitry Shmidt98f9e762012-05-30 11:18:46 -070071 wpa_printf(MSG_DEBUG, "p2p_connection_in_progress state %d", p2p->state);
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -070072 ret = 0;
73 }
74
75 return ret;
76}
77#endif
78
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070079static void p2p_expire_peers(struct p2p_data *p2p)
80{
81 struct p2p_device *dev, *n;
82 struct os_time now;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080083 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070084
85 os_get_time(&now);
86 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
87 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
88 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080089
90 if (p2p->cfg->go_connected &&
91 p2p->cfg->go_connected(p2p->cfg->cb_ctx,
92 dev->info.p2p_device_addr)) {
93 /*
94 * We are connected as a client to a group in which the
95 * peer is the GO, so do not expire the peer entry.
96 */
97 os_get_time(&dev->last_seen);
98 continue;
99 }
100
101 for (i = 0; i < p2p->num_groups; i++) {
102 if (p2p_group_is_client_connected(
103 p2p->groups[i], dev->info.p2p_device_addr))
104 break;
105 }
106 if (i < p2p->num_groups) {
107 /*
108 * The peer is connected as a client in a group where
109 * we are the GO, so do not expire the peer entry.
110 */
111 os_get_time(&dev->last_seen);
112 continue;
113 }
114
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -0700115#ifdef ANDROID_P2P
116 /* If Connection is in progress, don't expire the peer
117 */
118 if (p2p_connection_in_progress(p2p))
119 continue;
120#endif
121
Dmitry Shmidt04949592012-07-19 12:16:46 -0700122 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700123 "entry " MACSTR, MAC2STR(dev->info.p2p_device_addr));
Irfan Sherifff44b9c42012-06-13 11:09:23 -0700124#ifdef ANDROID_P2P
125 /* SD_FAIR_POLICY: Update the current sd_dev_list pointer to next device */
126 if(&dev->list == p2p->sd_dev_list)
127 p2p->sd_dev_list = dev->list.next;
128#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700129 dl_list_del(&dev->list);
130 p2p_device_free(p2p, dev);
131 }
132}
133
134
135static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
136{
137 struct p2p_data *p2p = eloop_ctx;
138 p2p_expire_peers(p2p);
139 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
140 p2p_expiration_timeout, p2p, NULL);
141}
142
143
144static const char * p2p_state_txt(int state)
145{
146 switch (state) {
147 case P2P_IDLE:
148 return "IDLE";
149 case P2P_SEARCH:
150 return "SEARCH";
151 case P2P_CONNECT:
152 return "CONNECT";
153 case P2P_CONNECT_LISTEN:
154 return "CONNECT_LISTEN";
155 case P2P_GO_NEG:
156 return "GO_NEG";
157 case P2P_LISTEN_ONLY:
158 return "LISTEN_ONLY";
159 case P2P_WAIT_PEER_CONNECT:
160 return "WAIT_PEER_CONNECT";
161 case P2P_WAIT_PEER_IDLE:
162 return "WAIT_PEER_IDLE";
163 case P2P_SD_DURING_FIND:
164 return "SD_DURING_FIND";
165 case P2P_PROVISIONING:
166 return "PROVISIONING";
167 case P2P_PD_DURING_FIND:
168 return "PD_DURING_FIND";
169 case P2P_INVITE:
170 return "INVITE";
171 case P2P_INVITE_LISTEN:
172 return "INVITE_LISTEN";
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800173 case P2P_SEARCH_WHEN_READY:
174 return "SEARCH_WHEN_READY";
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700175 case P2P_CONTINUE_SEARCH_WHEN_READY:
176 return "CONTINUE_SEARCH_WHEN_READY";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700177 default:
178 return "?";
179 }
180}
181
182
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800183u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
184{
185 struct p2p_device *dev = NULL;
186
187 if (!addr || !p2p)
188 return 0;
189
190 dev = p2p_get_device(p2p, addr);
191 if (dev)
192 return dev->wps_prov_info;
193 else
194 return 0;
195}
196
197
Dmitry Shmidt04949592012-07-19 12:16:46 -0700198void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800199{
200 struct p2p_device *dev = NULL;
201
Dmitry Shmidt04949592012-07-19 12:16:46 -0700202 if (!addr || !p2p)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800203 return;
204
Dmitry Shmidt04949592012-07-19 12:16:46 -0700205 dev = p2p_get_device(p2p, addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800206 if (dev)
207 dev->wps_prov_info = 0;
208}
209
210
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700211void p2p_set_state(struct p2p_data *p2p, int new_state)
212{
213 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s",
214 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
215 p2p->state = new_state;
216}
217
218
219void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
220{
221 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
222 "P2P: Set timeout (state=%s): %u.%06u sec",
223 p2p_state_txt(p2p->state), sec, usec);
224 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
225 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
226}
227
228
229void p2p_clear_timeout(struct p2p_data *p2p)
230{
231 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)",
232 p2p_state_txt(p2p->state));
233 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
234}
235
236
237void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
238 int status)
239{
240 struct p2p_go_neg_results res;
241 p2p_clear_timeout(p2p);
242 p2p_set_state(p2p, P2P_IDLE);
Dmitry Shmidt8c652892013-03-01 10:14:01 -0800243 if (p2p->go_neg_peer) {
244 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800245 p2p->go_neg_peer->wps_method = WPS_NOT_READY;
Dmitry Shmidt8c652892013-03-01 10:14:01 -0800246 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700247 p2p->go_neg_peer = NULL;
248
249 os_memset(&res, 0, sizeof(res));
250 res.status = status;
251 if (peer) {
252 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr,
253 ETH_ALEN);
254 os_memcpy(res.peer_interface_addr, peer->intended_addr,
255 ETH_ALEN);
256 }
257 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
258}
259
260
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800261static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700262{
263 unsigned int r, tu;
264 int freq;
265 struct wpabuf *ies;
266
267 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
268 "P2P: Starting short listen state (state=%s)",
269 p2p_state_txt(p2p->state));
270
271 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
272 p2p->cfg->channel);
273 if (freq < 0) {
274 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
275 "P2P: Unknown regulatory class/channel");
276 return;
277 }
278
279 os_get_random((u8 *) &r, sizeof(r));
280 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
281 p2p->min_disc_int) * 100;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800282 if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu)
283 tu = p2p->max_disc_tu;
284 if (!dev_disc && tu < 100)
285 tu = 100; /* Need to wait in non-device discovery use cases */
286 if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen)
287 tu = p2p->cfg->max_listen * 1000 / 1024;
288
289 if (tu == 0) {
290 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip listen state "
291 "since duration was 0 TU");
292 p2p_set_timeout(p2p, 0, 0);
293 return;
294 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700295
296 p2p->pending_listen_freq = freq;
297 p2p->pending_listen_sec = 0;
298 p2p->pending_listen_usec = 1024 * tu;
299
300 ies = p2p_build_probe_resp_ies(p2p);
301 if (ies == NULL)
302 return;
303
304 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
305 ies) < 0) {
306 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
307 "P2P: Failed to start listen mode");
308 p2p->pending_listen_freq = 0;
309 }
310 wpabuf_free(ies);
311}
312
313
314int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
315{
316 int freq;
317 struct wpabuf *ies;
318
319 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
320 "P2P: Going to listen(only) state");
321
322 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
323 p2p->cfg->channel);
324 if (freq < 0) {
325 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
326 "P2P: Unknown regulatory class/channel");
327 return -1;
328 }
329
330 p2p->pending_listen_freq = freq;
331 p2p->pending_listen_sec = timeout / 1000;
332 p2p->pending_listen_usec = (timeout % 1000) * 1000;
333
334 if (p2p->p2p_scan_running) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700335 if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800336 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
337 "P2P: p2p_scan running - connect is already "
338 "pending - skip listen");
339 return 0;
340 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700341 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
342 "P2P: p2p_scan running - delay start of listen state");
343 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
344 return 0;
345 }
346
347 ies = p2p_build_probe_resp_ies(p2p);
348 if (ies == NULL)
349 return -1;
350
351 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
352 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
353 "P2P: Failed to start listen mode");
354 p2p->pending_listen_freq = 0;
355 wpabuf_free(ies);
356 return -1;
357 }
358 wpabuf_free(ies);
359
360 p2p_set_state(p2p, P2P_LISTEN_ONLY);
361
362 return 0;
363}
364
365
366static void p2p_device_clear_reported(struct p2p_data *p2p)
367{
368 struct p2p_device *dev;
369 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
370 dev->flags &= ~P2P_DEV_REPORTED;
371}
372
373
374/**
375 * p2p_get_device - Fetch a peer entry
376 * @p2p: P2P module context from p2p_init()
377 * @addr: P2P Device Address of the peer
378 * Returns: Pointer to the device entry or %NULL if not found
379 */
380struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
381{
382 struct p2p_device *dev;
383 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
384 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
385 return dev;
386 }
387 return NULL;
388}
389
390
391/**
392 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
393 * @p2p: P2P module context from p2p_init()
394 * @addr: P2P Interface Address of the peer
395 * Returns: Pointer to the device entry or %NULL if not found
396 */
397struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
398 const u8 *addr)
399{
400 struct p2p_device *dev;
401 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
402 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
403 return dev;
404 }
405 return NULL;
406}
407
408
409/**
410 * p2p_create_device - Create a peer entry
411 * @p2p: P2P module context from p2p_init()
412 * @addr: P2P Device Address of the peer
413 * Returns: Pointer to the device entry or %NULL on failure
414 *
415 * If there is already an entry for the peer, it will be returned instead of
416 * creating a new one.
417 */
418static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
419 const u8 *addr)
420{
421 struct p2p_device *dev, *oldest = NULL;
422 size_t count = 0;
423
424 dev = p2p_get_device(p2p, addr);
425 if (dev)
426 return dev;
427
428 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
429 count++;
430 if (oldest == NULL ||
431 os_time_before(&dev->last_seen, &oldest->last_seen))
432 oldest = dev;
433 }
434 if (count + 1 > p2p->cfg->max_peers && oldest) {
435 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
436 "P2P: Remove oldest peer entry to make room for a new "
437 "peer");
Irfan Sherifff44b9c42012-06-13 11:09:23 -0700438#ifdef ANDROID_P2P
439 /* SD_FAIR_POLICY: Update the current sd_dev_list pointer to next device */
440 if(&oldest->list == p2p->sd_dev_list)
441 p2p->sd_dev_list = oldest->list.next;
442#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700443 dl_list_del(&oldest->list);
444 p2p_device_free(p2p, oldest);
445 }
446
447 dev = os_zalloc(sizeof(*dev));
448 if (dev == NULL)
449 return NULL;
450 dl_list_add(&p2p->devices, &dev->list);
451 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
452
453 return dev;
454}
455
456
457static void p2p_copy_client_info(struct p2p_device *dev,
458 struct p2p_client_info *cli)
459{
460 os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
461 dev->info.device_name[cli->dev_name_len] = '\0';
462 dev->info.dev_capab = cli->dev_capab;
463 dev->info.config_methods = cli->config_methods;
464 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
465 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
466 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
467 dev->info.wps_sec_dev_type_list_len);
468}
469
470
471static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
472 const u8 *go_interface_addr, int freq,
473 const u8 *gi, size_t gi_len)
474{
475 struct p2p_group_info info;
476 size_t c;
477 struct p2p_device *dev;
478
479 if (gi == NULL)
480 return 0;
481
482 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
483 return -1;
484
485 /*
486 * Clear old data for this group; if the devices are still in the
487 * group, the information will be restored in the loop following this.
488 */
489 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800490 if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700491 ETH_ALEN) == 0) {
492 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
493 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
494 }
495 }
496
497 for (c = 0; c < info.num_clients; c++) {
498 struct p2p_client_info *cli = &info.client[c];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800499 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
500 ETH_ALEN) == 0)
501 continue; /* ignore our own entry */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502 dev = p2p_get_device(p2p, cli->p2p_device_addr);
503 if (dev) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700504 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
Dmitry Shmidt04949592012-07-19 12:16:46 -0700505 P2P_DEV_PROBE_REQ_ONLY)) {
506 /*
507 * Update information since we have not
508 * received this directly from the client.
509 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700510 p2p_copy_client_info(dev, cli);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700511 } else {
512 /*
513 * Need to update P2P Client Discoverability
514 * flag since it is valid only in P2P Group
515 * Info attribute.
516 */
517 dev->info.dev_capab &=
518 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
519 dev->info.dev_capab |=
520 cli->dev_capab &
521 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
522 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700523 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
524 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
525 }
526 } else {
527 dev = p2p_create_device(p2p, cli->p2p_device_addr);
528 if (dev == NULL)
529 continue;
530 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
531 p2p_copy_client_info(dev, cli);
532 dev->oper_freq = freq;
533 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
534 dev->info.p2p_device_addr,
535 &dev->info, 1);
536 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
537 }
538
539 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
540 ETH_ALEN);
541 os_get_time(&dev->last_seen);
542 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
543 os_memcpy(dev->member_in_go_iface, go_interface_addr,
544 ETH_ALEN);
545 }
546
547 return 0;
548}
549
550
551static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
552 const struct p2p_message *msg)
553{
554 os_memcpy(dev->info.device_name, msg->device_name,
555 sizeof(dev->info.device_name));
556
557 if (msg->manufacturer &&
558 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
559 os_memset(dev->info.manufacturer, 0,
560 sizeof(dev->info.manufacturer));
561 os_memcpy(dev->info.manufacturer, msg->manufacturer,
562 msg->manufacturer_len);
563 }
564
565 if (msg->model_name &&
566 msg->model_name_len < sizeof(dev->info.model_name)) {
567 os_memset(dev->info.model_name, 0,
568 sizeof(dev->info.model_name));
569 os_memcpy(dev->info.model_name, msg->model_name,
570 msg->model_name_len);
571 }
572
573 if (msg->model_number &&
574 msg->model_number_len < sizeof(dev->info.model_number)) {
575 os_memset(dev->info.model_number, 0,
576 sizeof(dev->info.model_number));
577 os_memcpy(dev->info.model_number, msg->model_number,
578 msg->model_number_len);
579 }
580
581 if (msg->serial_number &&
582 msg->serial_number_len < sizeof(dev->info.serial_number)) {
583 os_memset(dev->info.serial_number, 0,
584 sizeof(dev->info.serial_number));
585 os_memcpy(dev->info.serial_number, msg->serial_number,
586 msg->serial_number_len);
587 }
588
589 if (msg->pri_dev_type)
590 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
591 sizeof(dev->info.pri_dev_type));
592 else if (msg->wps_pri_dev_type)
593 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
594 sizeof(dev->info.pri_dev_type));
595
596 if (msg->wps_sec_dev_type_list) {
597 os_memcpy(dev->info.wps_sec_dev_type_list,
598 msg->wps_sec_dev_type_list,
599 msg->wps_sec_dev_type_list_len);
600 dev->info.wps_sec_dev_type_list_len =
601 msg->wps_sec_dev_type_list_len;
602 }
603
604 if (msg->capability) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700605 /*
606 * P2P Client Discoverability bit is reserved in all frames
607 * that use this function, so do not change its value here.
608 */
609 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
610 dev->info.dev_capab |= msg->capability[0] &
611 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700612 dev->info.group_capab = msg->capability[1];
613 }
614
615 if (msg->ext_listen_timing) {
616 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
617 dev->ext_listen_interval =
618 WPA_GET_LE16(msg->ext_listen_timing + 2);
619 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700620
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700621 if (!probe_req) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800622 u16 new_config_methods;
623 new_config_methods = msg->config_methods ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700624 msg->config_methods : msg->wps_config_methods;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800625 if (new_config_methods &&
626 dev->info.config_methods != new_config_methods) {
627 wpa_printf(MSG_DEBUG, "P2P: Update peer " MACSTR
628 " config_methods 0x%x -> 0x%x",
629 MAC2STR(dev->info.p2p_device_addr),
630 dev->info.config_methods,
631 new_config_methods);
632 dev->info.config_methods = new_config_methods;
633 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700634 }
635}
636
637
638/**
Dmitry Shmidt04949592012-07-19 12:16:46 -0700639 * p2p_add_device - Add peer entries based on scan results or P2P frames
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700640 * @p2p: P2P module context from p2p_init()
641 * @addr: Source address of Beacon or Probe Response frame (may be either
642 * P2P Device Address or P2P Interface Address)
643 * @level: Signal level (signal strength of the received frame from the peer)
644 * @freq: Frequency on which the Beacon or Probe Response frame was received
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800645 * @rx_time: Time when the result was received
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700646 * @ies: IEs from the Beacon or Probe Response frame
647 * @ies_len: Length of ies buffer in octets
Dmitry Shmidt04949592012-07-19 12:16:46 -0700648 * @scan_res: Whether this was based on scan results
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700649 * Returns: 0 on success, -1 on failure
650 *
651 * If the scan result is for a GO, the clients in the group will also be added
652 * to the peer table. This function can also be used with some other frames
653 * like Provision Discovery Request that contains P2P Capability and P2P Device
654 * Info attributes.
655 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800656int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800657 struct os_time *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800658 size_t ies_len, int scan_res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700659{
660 struct p2p_device *dev;
661 struct p2p_message msg;
662 const u8 *p2p_dev_addr;
663 int i;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800664 struct os_time time_now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700665
666 os_memset(&msg, 0, sizeof(msg));
667 if (p2p_parse_ies(ies, ies_len, &msg)) {
668 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
669 "P2P: Failed to parse P2P IE for a device entry");
670 p2p_parse_free(&msg);
671 return -1;
672 }
673
674 if (msg.p2p_device_addr)
675 p2p_dev_addr = msg.p2p_device_addr;
676 else if (msg.device_id)
677 p2p_dev_addr = msg.device_id;
678 else {
679 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
680 "P2P: Ignore scan data without P2P Device Info or "
681 "P2P Device Id");
682 p2p_parse_free(&msg);
683 return -1;
684 }
685
686 if (!is_zero_ether_addr(p2p->peer_filter) &&
687 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
688 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
689 "filter for " MACSTR " due to peer filter",
690 MAC2STR(p2p_dev_addr));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800691 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700692 return 0;
693 }
694
695 dev = p2p_create_device(p2p, p2p_dev_addr);
696 if (dev == NULL) {
697 p2p_parse_free(&msg);
698 return -1;
699 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800700
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800701 if (rx_time == NULL) {
702 os_get_time(&time_now);
703 rx_time = &time_now;
704 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800705
706 /*
707 * Update the device entry only if the new peer
708 * entry is newer than the one previously stored.
709 */
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800710 if (dev->last_seen.sec > 0 &&
711 os_time_before(rx_time, &dev->last_seen)) {
712 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not update peer "
713 "entry based on old frame (rx_time=%u.%06u "
714 "last_seen=%u.%06u)",
715 (unsigned int) rx_time->sec,
716 (unsigned int) rx_time->usec,
717 (unsigned int) dev->last_seen.sec,
718 (unsigned int) dev->last_seen.usec);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800719 p2p_parse_free(&msg);
720 return -1;
721 }
722
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800723 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_time));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800724
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700725 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
726
727 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
728 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
729 if (msg.ssid &&
730 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
731 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
732 != 0)) {
733 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
734 dev->oper_ssid_len = msg.ssid[1];
735 }
736
737 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
738 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
739 int ds_freq;
740 if (*msg.ds_params == 14)
741 ds_freq = 2484;
742 else
743 ds_freq = 2407 + *msg.ds_params * 5;
744 if (freq != ds_freq) {
745 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
746 "P2P: Update Listen frequency based on DS "
747 "Parameter Set IE: %d -> %d MHz",
748 freq, ds_freq);
749 freq = ds_freq;
750 }
751 }
752
Dmitry Shmidt04949592012-07-19 12:16:46 -0700753 if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700754 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
755 "P2P: Update Listen frequency based on scan "
756 "results (" MACSTR " %d -> %d MHz (DS param %d)",
757 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
758 freq, msg.ds_params ? *msg.ds_params : -1);
759 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700760 if (scan_res) {
761 dev->listen_freq = freq;
762 if (msg.group_info)
763 dev->oper_freq = freq;
764 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700765 dev->info.level = level;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700766
767 p2p_copy_wps_info(dev, 0, &msg);
768
769 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
770 wpabuf_free(dev->info.wps_vendor_ext[i]);
771 dev->info.wps_vendor_ext[i] = NULL;
772 }
773
774 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
775 if (msg.wps_vendor_ext[i] == NULL)
776 break;
777 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
778 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
779 if (dev->info.wps_vendor_ext[i] == NULL)
780 break;
781 }
782
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700783 if (msg.wfd_subelems) {
784 wpabuf_free(dev->info.wfd_subelems);
785 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
786 }
787
Dmitry Shmidt04949592012-07-19 12:16:46 -0700788 if (scan_res) {
789 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
790 msg.group_info, msg.group_info_len);
791 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700792
793 p2p_parse_free(&msg);
794
795 if (p2p_pending_sd_req(p2p, dev))
796 dev->flags |= P2P_DEV_SD_SCHEDULE;
797
798 if (dev->flags & P2P_DEV_REPORTED)
799 return 0;
800
801 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800802 "P2P: Peer found with Listen frequency %d MHz "
803 "(rx_time=%u.%06u)", freq, (unsigned int) rx_time->sec,
804 (unsigned int) rx_time->usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700805 if (dev->flags & P2P_DEV_USER_REJECTED) {
806 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
807 "P2P: Do not report rejected device");
808 return 0;
809 }
810
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800811 if (dev->info.config_methods == 0 &&
812 (freq == 2412 || freq == 2437 || freq == 2462)) {
813 /*
814 * If we have only seen a Beacon frame from a GO, we do not yet
815 * know what WPS config methods it supports. Since some
816 * applications use config_methods value from P2P-DEVICE-FOUND
817 * events, postpone reporting this peer until we've fully
818 * discovered its capabilities.
819 *
820 * At least for now, do this only if the peer was detected on
821 * one of the social channels since that peer can be easily be
822 * found again and there are no limitations of having to use
823 * passive scan on this channels, so this can be done through
824 * Probe Response frame that includes the config_methods
825 * information.
826 */
827 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
828 "P2P: Do not report peer " MACSTR " with unknown "
829 "config methods", MAC2STR(addr));
830 return 0;
831 }
832
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
834 !(dev->flags & P2P_DEV_REPORTED_ONCE));
835 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
836
837 return 0;
838}
839
840
841static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
842{
843 int i;
844
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700845 if (p2p->go_neg_peer == dev) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800846 /*
847 * If GO Negotiation is in progress, report that it has failed.
848 */
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700849 p2p_go_neg_failed(p2p, dev, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700850 p2p->go_neg_peer = NULL;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700851 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700852 if (p2p->invite_peer == dev)
853 p2p->invite_peer = NULL;
854 if (p2p->sd_peer == dev)
855 p2p->sd_peer = NULL;
856 if (p2p->pending_client_disc_go == dev)
857 p2p->pending_client_disc_go = NULL;
858
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700859 /* dev_lost() device, but only if it was previously dev_found() */
860 if (dev->flags & P2P_DEV_REPORTED_ONCE)
861 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
862 dev->info.p2p_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700863
864 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
865 wpabuf_free(dev->info.wps_vendor_ext[i]);
866 dev->info.wps_vendor_ext[i] = NULL;
867 }
868
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700869 wpabuf_free(dev->info.wfd_subelems);
870
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700871 os_free(dev);
872}
873
874
875static int p2p_get_next_prog_freq(struct p2p_data *p2p)
876{
877 struct p2p_channels *c;
878 struct p2p_reg_class *cla;
879 size_t cl, ch;
880 int found = 0;
881 u8 reg_class;
882 u8 channel;
883 int freq;
884
885 c = &p2p->cfg->channels;
886 for (cl = 0; cl < c->reg_classes; cl++) {
887 cla = &c->reg_class[cl];
888 if (cla->reg_class != p2p->last_prog_scan_class)
889 continue;
890 for (ch = 0; ch < cla->channels; ch++) {
891 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
892 found = 1;
893 break;
894 }
895 }
896 if (found)
897 break;
898 }
899
900 if (!found) {
901 /* Start from beginning */
902 reg_class = c->reg_class[0].reg_class;
903 channel = c->reg_class[0].channel[0];
904 } else {
905 /* Pick the next channel */
906 ch++;
907 if (ch == cla->channels) {
908 cl++;
909 if (cl == c->reg_classes)
910 cl = 0;
911 ch = 0;
912 }
913 reg_class = c->reg_class[cl].reg_class;
914 channel = c->reg_class[cl].channel[ch];
915 }
916
917 freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
918 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
919 "channel: reg_class %u channel %u -> %d MHz",
920 reg_class, channel, freq);
921 p2p->last_prog_scan_class = reg_class;
922 p2p->last_prog_scan_chan = channel;
923
924 if (freq == 2412 || freq == 2437 || freq == 2462)
925 return 0; /* No need to add social channels */
926 return freq;
927}
928
929
930static void p2p_search(struct p2p_data *p2p)
931{
932 int freq = 0;
933 enum p2p_scan_type type;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700934 u16 pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700935 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700936
937 if (p2p->drv_in_listen) {
938 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
939 "in Listen state - wait for it to end before "
940 "continuing");
941 return;
942 }
943 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
944
Dmitry Shmidt04949592012-07-19 12:16:46 -0700945 if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
946 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700947 type = P2P_SCAN_SOCIAL_PLUS_ONE;
948 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
949 "(+ freq %u)", freq);
950 } else {
951 type = P2P_SCAN_SOCIAL;
952 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
953 }
954
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700955 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
956 p2p->num_req_dev_types, p2p->req_dev_types,
957 p2p->find_dev_id, pw_id);
958 if (res < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700959 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
960 "P2P: Scan request failed");
961 p2p_continue_find(p2p);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700962 } else if (res == 1) {
963 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
964 "p2p_scan at this point - will try again after "
965 "previous scan completes");
966 p2p_set_state(p2p, P2P_CONTINUE_SEARCH_WHEN_READY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967 } else {
968 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
969 p2p->p2p_scan_running = 1;
970 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
971 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
972 p2p, NULL);
973 }
974}
975
976
977static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
978{
979 struct p2p_data *p2p = eloop_ctx;
980 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
981 p2p_stop_find(p2p);
982}
983
984
985static int p2p_run_after_scan(struct p2p_data *p2p)
986{
987 struct p2p_device *dev;
988 enum p2p_after_scan op;
989
990 if (p2p->after_scan_tx) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700991 /* TODO: schedule p2p_run_after_scan to be called from TX
992 * status callback(?) */
993 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
994 "Action frame at p2p_scan completion");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800995 p2p->cfg->send_action(p2p->cfg->cb_ctx,
996 p2p->after_scan_tx->freq,
997 p2p->after_scan_tx->dst,
998 p2p->after_scan_tx->src,
999 p2p->after_scan_tx->bssid,
1000 (u8 *) (p2p->after_scan_tx + 1),
1001 p2p->after_scan_tx->len,
1002 p2p->after_scan_tx->wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001003 os_free(p2p->after_scan_tx);
1004 p2p->after_scan_tx = NULL;
Irfan Sherifff44b9c42012-06-13 11:09:23 -07001005#ifdef ANDROID_P2P
1006 /* For SD frames, there is a scenario, where we can receive a SD request frame during p2p_scan.
1007 * At that moment, we will send the SD response from this context. After sending the SD response,
1008 * we need to continue p2p_find. But if we return 1 from here, p2p_find is going to be stopped.
1009 */
1010 return 0;
1011#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001012 return 1;
Irfan Sherifff44b9c42012-06-13 11:09:23 -07001013#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001014 }
1015
1016 op = p2p->start_after_scan;
1017 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1018 switch (op) {
1019 case P2P_AFTER_SCAN_NOTHING:
1020 break;
1021 case P2P_AFTER_SCAN_LISTEN:
1022 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
1023 "requested Listen state");
1024 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
1025 p2p->pending_listen_usec / 1000);
1026 return 1;
1027 case P2P_AFTER_SCAN_CONNECT:
1028 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
1029 "requested connect with " MACSTR,
1030 MAC2STR(p2p->after_scan_peer));
1031 dev = p2p_get_device(p2p, p2p->after_scan_peer);
1032 if (dev == NULL) {
1033 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
1034 "known anymore");
1035 break;
1036 }
1037 p2p_connect_send(p2p, dev);
1038 return 1;
1039 }
1040
1041 return 0;
1042}
1043
1044
1045static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1046{
1047 struct p2p_data *p2p = eloop_ctx;
1048 int running;
1049 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
1050 "(running=%d)", p2p->p2p_scan_running);
1051 running = p2p->p2p_scan_running;
1052 /* Make sure we recover from missed scan results callback */
1053 p2p->p2p_scan_running = 0;
1054
1055 if (running)
1056 p2p_run_after_scan(p2p);
1057}
1058
1059
1060static void p2p_free_req_dev_types(struct p2p_data *p2p)
1061{
1062 p2p->num_req_dev_types = 0;
1063 os_free(p2p->req_dev_types);
1064 p2p->req_dev_types = NULL;
1065}
1066
1067
1068int p2p_find(struct p2p_data *p2p, unsigned int timeout,
1069 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001070 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001071 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001072{
1073 int res;
1074
1075 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
1076 type);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001077 os_get_time(&p2p->find_start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001078 if (p2p->p2p_scan_running) {
1079 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
1080 "already running");
1081 }
1082
1083 p2p_free_req_dev_types(p2p);
1084 if (req_dev_types && num_req_dev_types) {
1085 p2p->req_dev_types = os_malloc(num_req_dev_types *
1086 WPS_DEV_TYPE_LEN);
1087 if (p2p->req_dev_types == NULL)
1088 return -1;
1089 os_memcpy(p2p->req_dev_types, req_dev_types,
1090 num_req_dev_types * WPS_DEV_TYPE_LEN);
1091 p2p->num_req_dev_types = num_req_dev_types;
1092 }
1093
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001094 if (dev_id) {
1095 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1096 p2p->find_dev_id = p2p->find_dev_id_buf;
1097 } else
1098 p2p->find_dev_id = NULL;
1099
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001100 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1101 p2p_clear_timeout(p2p);
1102 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1103 p2p->find_type = type;
1104 p2p_device_clear_reported(p2p);
1105 p2p_set_state(p2p, P2P_SEARCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001106 p2p->search_delay = search_delay;
1107 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001109 p2p->last_p2p_find_timeout = timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001110 if (timeout)
1111 eloop_register_timeout(timeout, 0, p2p_find_timeout,
1112 p2p, NULL);
1113 switch (type) {
1114 case P2P_FIND_START_WITH_FULL:
1115 case P2P_FIND_PROGRESSIVE:
1116 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1117 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001118 p2p->req_dev_types, dev_id,
1119 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001120 break;
1121 case P2P_FIND_ONLY_SOCIAL:
1122 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1123 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001124 p2p->req_dev_types, dev_id,
1125 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126 break;
1127 default:
1128 return -1;
1129 }
1130
1131 if (res == 0) {
1132 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
1133 p2p->p2p_scan_running = 1;
1134 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1135 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1136 p2p, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001137 } else if (res == 1) {
1138 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
1139 "p2p_scan at this point - will try again after "
1140 "previous scan completes");
1141 res = 0;
1142 p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
1143 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001144 } else {
1145 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
1146 "p2p_scan");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001147 p2p_set_state(p2p, P2P_IDLE);
1148 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001149 }
1150
1151 return res;
1152}
1153
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07001154#ifdef ANDROID_P2P
1155int p2p_search_pending(struct p2p_data *p2p)
1156{
1157 if(p2p == NULL)
1158 return 0;
1159
1160 if(p2p->state == P2P_SEARCH_WHEN_READY)
1161 return 1;
1162
1163 return 0;
1164}
1165#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001166
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001167int p2p_other_scan_completed(struct p2p_data *p2p)
1168{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001169 if (p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY) {
1170 p2p_set_state(p2p, P2P_SEARCH);
1171 p2p_search(p2p);
1172 return 1;
1173 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001174 if (p2p->state != P2P_SEARCH_WHEN_READY)
1175 return 0;
1176 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find "
1177 "now that previous scan was completed");
1178 if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001179 p2p->num_req_dev_types, p2p->req_dev_types,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001180 p2p->find_dev_id, p2p->search_delay) < 0) {
1181 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001182 return 0;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001183 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001184 return 1;
1185}
1186
1187
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001188void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1189{
1190 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
1191 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1192 p2p_clear_timeout(p2p);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001193 if (p2p->state == P2P_SEARCH ||
1194 p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY ||
1195 p2p->state == P2P_SEARCH_WHEN_READY)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001196 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001197 p2p_set_state(p2p, P2P_IDLE);
1198 p2p_free_req_dev_types(p2p);
1199 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08001200 if (p2p->go_neg_peer)
1201 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001202 p2p->go_neg_peer = NULL;
1203 p2p->sd_peer = NULL;
1204 p2p->invite_peer = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001205 p2p_stop_listen_for_freq(p2p, freq);
1206}
1207
1208
1209void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1210{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001211 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
1212 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
1213 "since we are on correct channel for response");
1214 return;
1215 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001216 if (p2p->in_listen) {
1217 p2p->in_listen = 0;
1218 p2p_clear_timeout(p2p);
1219 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001220 if (p2p->drv_in_listen) {
1221 /*
1222 * The driver may not deliver callback to p2p_listen_end()
1223 * when the operation gets canceled, so clear the internal
1224 * variable that is tracking driver state.
1225 */
1226 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear "
1227 "drv_in_listen (%d)", p2p->drv_in_listen);
1228 p2p->drv_in_listen = 0;
1229 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001230 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1231}
1232
1233
1234void p2p_stop_find(struct p2p_data *p2p)
1235{
1236 p2p_stop_find_for_freq(p2p, 0);
1237}
1238
1239
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001240static int p2p_prepare_channel_pref(struct p2p_data *p2p,
1241 unsigned int force_freq,
1242 unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001243{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001244 u8 op_class, op_channel;
1245 unsigned int freq = force_freq ? force_freq : pref_freq;
1246
1247 if (p2p_freq_to_channel(p2p->cfg->country, freq,
1248 &op_class, &op_channel) < 0) {
1249 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1250 "P2P: Unsupported frequency %u MHz", freq);
1251 return -1;
1252 }
1253
1254 if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel)) {
1255 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1256 "P2P: Frequency %u MHz (oper_class %u channel %u) not "
1257 "allowed for P2P", freq, op_class, op_channel);
1258 return -1;
1259 }
1260
1261 p2p->op_reg_class = op_class;
1262 p2p->op_channel = op_channel;
1263
1264 if (force_freq) {
1265 p2p->channels.reg_classes = 1;
1266 p2p->channels.reg_class[0].channels = 1;
1267 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1268 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001269 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001270 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1271 sizeof(struct p2p_channels));
1272 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001273
1274 return 0;
1275}
1276
1277
1278static void p2p_prepare_channel_best(struct p2p_data *p2p)
1279{
1280 u8 op_class, op_channel;
1281
1282 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1283 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
1284 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_overall,
1285 &op_class, &op_channel) == 0) {
1286 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Select best "
1287 "overall channel as operating channel preference");
1288 p2p->op_reg_class = op_class;
1289 p2p->op_channel = op_channel;
1290 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1291 p2p_supported_freq(p2p, p2p->best_freq_5) &&
1292 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_5,
1293 &op_class, &op_channel) == 0) {
1294 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Select best 5 GHz "
1295 "channel as operating channel preference");
1296 p2p->op_reg_class = op_class;
1297 p2p->op_channel = op_channel;
1298 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
1299 p2p_supported_freq(p2p, p2p->best_freq_24) &&
1300 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_24,
1301 &op_class, &op_channel) == 0) {
1302 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Select best 2.4 "
1303 "GHz channel as operating channel preference");
1304 p2p->op_reg_class = op_class;
1305 p2p->op_channel = op_channel;
1306 } else {
1307 p2p->op_reg_class = p2p->cfg->op_reg_class;
1308 p2p->op_channel = p2p->cfg->op_channel;
1309 }
1310
1311 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1312 sizeof(struct p2p_channels));
1313}
1314
1315
1316/**
1317 * p2p_prepare_channel - Select operating channel for GO Negotiation
1318 * @p2p: P2P module context from p2p_init()
1319 * @dev: Selected peer device
1320 * @force_freq: Forced frequency in MHz or 0 if not forced
1321 * @pref_freq: Preferred frequency in MHz or 0 if no preference
1322 * Returns: 0 on success, -1 on failure (channel not supported for P2P)
1323 *
1324 * This function is used to do initial operating channel selection for GO
1325 * Negotiation prior to having received peer information. The selected channel
1326 * may be further optimized in p2p_reselect_channel() once the peer information
1327 * is available.
1328 */
1329static int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
1330 unsigned int force_freq, unsigned int pref_freq)
1331{
1332 if (force_freq || pref_freq) {
1333 if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq) < 0)
1334 return -1;
1335 } else {
1336 p2p_prepare_channel_best(p2p);
1337 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001338 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1339 "P2P: Own preference for operation channel: "
1340 "Operating Class %u Channel %u%s",
1341 p2p->op_reg_class, p2p->op_channel,
1342 force_freq ? " (forced)" : "");
1343
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001344 if (force_freq)
1345 dev->flags |= P2P_DEV_FORCE_FREQ;
1346 else
1347 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1348
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001349 return 0;
1350}
1351
1352
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001353static void p2p_set_dev_persistent(struct p2p_device *dev,
1354 int persistent_group)
1355{
1356 switch (persistent_group) {
1357 case 0:
1358 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1359 P2P_DEV_PREFER_PERSISTENT_RECONN);
1360 break;
1361 case 1:
1362 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1363 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1364 break;
1365 case 2:
1366 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1367 P2P_DEV_PREFER_PERSISTENT_RECONN;
1368 break;
1369 }
1370}
1371
1372
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001373int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1374 enum p2p_wps_method wps_method,
1375 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001376 unsigned int force_freq, int persistent_group,
1377 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001378 int pd_before_go_neg, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001379{
1380 struct p2p_device *dev;
1381
1382 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1383 "P2P: Request to start group negotiation - peer=" MACSTR
1384 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001385 " wps_method=%d persistent_group=%d pd_before_go_neg=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001386 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001387 wps_method, persistent_group, pd_before_go_neg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001388
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001389 dev = p2p_get_device(p2p, peer_addr);
1390 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1391 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1392 "P2P: Cannot connect to unknown P2P Device " MACSTR,
1393 MAC2STR(peer_addr));
1394 return -1;
1395 }
1396
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001397 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq) < 0)
1398 return -1;
1399
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001400 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1401 if (!(dev->info.dev_capab &
1402 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1403 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1404 "P2P: Cannot connect to P2P Device " MACSTR
1405 " that is in a group and is not discoverable",
1406 MAC2STR(peer_addr));
1407 return -1;
1408 }
1409 if (dev->oper_freq <= 0) {
1410 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1411 "P2P: Cannot connect to P2P Device " MACSTR
1412 " with incomplete information",
1413 MAC2STR(peer_addr));
1414 return -1;
1415 }
1416
1417 /*
1418 * First, try to connect directly. If the peer does not
1419 * acknowledge frames, assume it is sleeping and use device
1420 * discoverability via the GO at that point.
1421 */
1422 }
1423
Dmitry Shmidt04949592012-07-19 12:16:46 -07001424 p2p->ssid_set = 0;
1425 if (force_ssid) {
1426 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1427 force_ssid, force_ssid_len);
1428 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1429 p2p->ssid_len = force_ssid_len;
1430 p2p->ssid_set = 1;
1431 }
1432
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1434 dev->flags &= ~P2P_DEV_USER_REJECTED;
1435 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1436 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001437 if (pd_before_go_neg)
1438 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001439 else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001440 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001441 /*
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001442 * Assign dialog token and tie breaker here to use the same
1443 * values in each retry within the same GO Negotiation exchange.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001444 */
1445 dev->dialog_token++;
1446 if (dev->dialog_token == 0)
1447 dev->dialog_token = 1;
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001448 dev->tie_breaker = p2p->next_tie_breaker;
1449 p2p->next_tie_breaker = !p2p->next_tie_breaker;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001450 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451 dev->connect_reqs = 0;
1452 dev->go_neg_req_sent = 0;
1453 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001454 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001455 p2p->go_intent = go_intent;
1456 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1457
1458 if (p2p->state != P2P_IDLE)
1459 p2p_stop_find(p2p);
1460
1461 if (p2p->after_scan_tx) {
1462 /*
1463 * We need to drop the pending frame to avoid issues with the
1464 * new GO Negotiation, e.g., when the pending frame was from a
1465 * previous attempt at starting a GO Negotiation.
1466 */
1467 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
1468 "previous pending Action frame TX that was waiting "
1469 "for p2p_scan completion");
1470 os_free(p2p->after_scan_tx);
1471 p2p->after_scan_tx = NULL;
1472 }
1473
1474 dev->wps_method = wps_method;
1475 dev->status = P2P_SC_SUCCESS;
1476
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001477 if (p2p->p2p_scan_running) {
1478 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1479 "P2P: p2p_scan running - delay connect send");
1480 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1481 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1482 return 0;
1483 }
1484 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1485
1486 return p2p_connect_send(p2p, dev);
1487}
1488
1489
1490int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1491 enum p2p_wps_method wps_method,
1492 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001493 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001494 const u8 *force_ssid, size_t force_ssid_len,
1495 unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001496{
1497 struct p2p_device *dev;
1498
1499 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1500 "P2P: Request to authorize group negotiation - peer=" MACSTR
1501 " GO Intent=%d Intended Interface Address=" MACSTR
1502 " wps_method=%d persistent_group=%d",
1503 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1504 wps_method, persistent_group);
1505
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001506 dev = p2p_get_device(p2p, peer_addr);
1507 if (dev == NULL) {
1508 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1509 "P2P: Cannot authorize unknown P2P Device " MACSTR,
1510 MAC2STR(peer_addr));
1511 return -1;
1512 }
1513
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001514 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq) < 0)
1515 return -1;
1516
Dmitry Shmidt04949592012-07-19 12:16:46 -07001517 p2p->ssid_set = 0;
1518 if (force_ssid) {
1519 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1520 force_ssid, force_ssid_len);
1521 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1522 p2p->ssid_len = force_ssid_len;
1523 p2p->ssid_set = 1;
1524 }
1525
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001526 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1527 dev->flags &= ~P2P_DEV_USER_REJECTED;
1528 dev->go_neg_req_sent = 0;
1529 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001530 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001531 p2p->go_intent = go_intent;
1532 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1533
1534 dev->wps_method = wps_method;
1535 dev->status = P2P_SC_SUCCESS;
1536
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537 return 0;
1538}
1539
1540
1541void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1542 struct p2p_device *dev, struct p2p_message *msg)
1543{
1544 os_get_time(&dev->last_seen);
1545
1546 p2p_copy_wps_info(dev, 0, msg);
1547
1548 if (msg->listen_channel) {
1549 int freq;
1550 freq = p2p_channel_to_freq((char *) msg->listen_channel,
1551 msg->listen_channel[3],
1552 msg->listen_channel[4]);
1553 if (freq < 0) {
1554 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1555 "P2P: Unknown peer Listen channel: "
1556 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1557 msg->listen_channel[0],
1558 msg->listen_channel[1],
1559 msg->listen_channel[2],
1560 msg->listen_channel[3],
1561 msg->listen_channel[4]);
1562 } else {
1563 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1564 "peer " MACSTR " Listen channel: %u -> %u MHz",
1565 MAC2STR(dev->info.p2p_device_addr),
1566 dev->listen_freq, freq);
1567 dev->listen_freq = freq;
1568 }
1569 }
1570
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001571 if (msg->wfd_subelems) {
1572 wpabuf_free(dev->info.wfd_subelems);
1573 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1574 }
1575
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001576 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1577 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1578 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1579 "P2P: Completed device entry based on data from "
1580 "GO Negotiation Request");
1581 } else {
1582 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1583 "P2P: Created device entry based on GO Neg Req: "
1584 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1585 "listen_freq=%d",
1586 MAC2STR(dev->info.p2p_device_addr),
1587 dev->info.dev_capab, dev->info.group_capab,
1588 dev->info.device_name, dev->listen_freq);
1589 }
1590
1591 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1592
1593 if (dev->flags & P2P_DEV_USER_REJECTED) {
1594 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1595 "P2P: Do not report rejected device");
1596 return;
1597 }
1598
1599 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1600 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1601 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1602}
1603
1604
1605void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1606{
1607 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1608 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1609 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1610 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1611 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1612}
1613
1614
1615int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1616{
1617 p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1618 p2p_random(params->passphrase, 8);
1619 return 0;
1620}
1621
1622
1623void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1624{
1625 struct p2p_go_neg_results res;
1626 int go = peer->go_state == LOCAL_GO;
1627 struct p2p_channels intersection;
1628 int freqs;
1629 size_t i, j;
1630
1631 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1632 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1633 "GO)", MAC2STR(peer->info.p2p_device_addr),
1634 go ? "local end" : "peer");
1635
1636 os_memset(&res, 0, sizeof(res));
1637 res.role_go = go;
1638 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1639 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1640 res.wps_method = peer->wps_method;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001641 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1642 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1643 res.persistent_group = 2;
1644 else
1645 res.persistent_group = 1;
1646 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001647
1648 if (go) {
1649 /* Setup AP mode for WPS provisioning */
1650 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1651 p2p->op_reg_class,
1652 p2p->op_channel);
1653 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1654 res.ssid_len = p2p->ssid_len;
1655 p2p_random(res.passphrase, 8);
1656 } else {
1657 res.freq = peer->oper_freq;
1658 if (p2p->ssid_len) {
1659 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1660 res.ssid_len = p2p->ssid_len;
1661 }
1662 }
1663
1664 p2p_channels_intersect(&p2p->channels, &peer->channels,
1665 &intersection);
1666 freqs = 0;
1667 for (i = 0; i < intersection.reg_classes; i++) {
1668 struct p2p_reg_class *c = &intersection.reg_class[i];
1669 if (freqs + 1 == P2P_MAX_CHANNELS)
1670 break;
1671 for (j = 0; j < c->channels; j++) {
1672 int freq;
1673 if (freqs + 1 == P2P_MAX_CHANNELS)
1674 break;
1675 freq = p2p_channel_to_freq(peer->country, c->reg_class,
1676 c->channel[j]);
1677 if (freq < 0)
1678 continue;
1679 res.freq_list[freqs++] = freq;
1680 }
1681 }
1682
1683 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1684
1685 p2p_clear_timeout(p2p);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001686 p2p->ssid_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001687 peer->go_neg_req_sent = 0;
1688 peer->wps_method = WPS_NOT_READY;
1689
1690 p2p_set_state(p2p, P2P_PROVISIONING);
1691 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1692}
1693
1694
1695static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1696 const u8 *data, size_t len, int rx_freq)
1697{
1698 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1699 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1700 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1701
1702 if (len < 1)
1703 return;
1704
1705 switch (data[0]) {
1706 case P2P_GO_NEG_REQ:
1707 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1708 break;
1709 case P2P_GO_NEG_RESP:
1710 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1711 break;
1712 case P2P_GO_NEG_CONF:
1713 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1714 break;
1715 case P2P_INVITATION_REQ:
1716 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1717 rx_freq);
1718 break;
1719 case P2P_INVITATION_RESP:
1720 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1721 break;
1722 case P2P_PROV_DISC_REQ:
1723 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1724 break;
1725 case P2P_PROV_DISC_RESP:
1726 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1727 break;
1728 case P2P_DEV_DISC_REQ:
1729 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1730 break;
1731 case P2P_DEV_DISC_RESP:
1732 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1733 break;
1734 default:
1735 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1736 "P2P: Unsupported P2P Public Action frame type %d",
1737 data[0]);
1738 break;
1739 }
1740}
1741
1742
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001743static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1744 const u8 *sa, const u8 *bssid, const u8 *data,
1745 size_t len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001746{
1747 if (len < 1)
1748 return;
1749
1750 switch (data[0]) {
1751 case WLAN_PA_VENDOR_SPECIFIC:
1752 data++;
1753 len--;
1754 if (len < 3)
1755 return;
1756 if (WPA_GET_BE24(data) != OUI_WFA)
1757 return;
1758
1759 data += 3;
1760 len -= 3;
1761 if (len < 1)
1762 return;
1763
1764 if (*data != P2P_OUI_TYPE)
1765 return;
1766
1767 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1768 break;
1769 case WLAN_PA_GAS_INITIAL_REQ:
1770 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1771 break;
1772 case WLAN_PA_GAS_INITIAL_RESP:
1773 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1774 break;
1775 case WLAN_PA_GAS_COMEBACK_REQ:
1776 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1777 break;
1778 case WLAN_PA_GAS_COMEBACK_RESP:
1779 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1780 break;
1781 }
1782}
1783
1784
1785void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1786 const u8 *bssid, u8 category,
1787 const u8 *data, size_t len, int freq)
1788{
1789 if (category == WLAN_ACTION_PUBLIC) {
1790 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1791 return;
1792 }
1793
1794 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1795 return;
1796
1797 if (len < 4)
1798 return;
1799
1800 if (WPA_GET_BE24(data) != OUI_WFA)
1801 return;
1802 data += 3;
1803 len -= 3;
1804
1805 if (*data != P2P_OUI_TYPE)
1806 return;
1807 data++;
1808 len--;
1809
1810 /* P2P action frame */
1811 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1812 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1813 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1814
1815 if (len < 1)
1816 return;
1817 switch (data[0]) {
1818 case P2P_NOA:
1819 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1820 "P2P: Received P2P Action - Notice of Absence");
1821 /* TODO */
1822 break;
1823 case P2P_PRESENCE_REQ:
1824 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1825 break;
1826 case P2P_PRESENCE_RESP:
1827 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1828 break;
1829 case P2P_GO_DISC_REQ:
1830 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1831 break;
1832 default:
1833 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1834 "P2P: Received P2P Action - unknown type %u", data[0]);
1835 break;
1836 }
1837}
1838
1839
1840static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1841{
1842 struct p2p_data *p2p = eloop_ctx;
1843 if (p2p->go_neg_peer == NULL)
1844 return;
1845 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1846 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1847 p2p_connect_send(p2p, p2p->go_neg_peer);
1848}
1849
1850
1851static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1852{
1853 struct p2p_data *p2p = eloop_ctx;
1854 if (p2p->invite_peer == NULL)
1855 return;
1856 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1857 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1858}
1859
1860
1861static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1862 const u8 *ie, size_t ie_len)
1863{
1864 struct p2p_message msg;
1865 struct p2p_device *dev;
1866
1867 os_memset(&msg, 0, sizeof(msg));
1868 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1869 {
1870 p2p_parse_free(&msg);
1871 return; /* not a P2P probe */
1872 }
1873
1874 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1875 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1876 != 0) {
1877 /* The Probe Request is not part of P2P Device Discovery. It is
1878 * not known whether the source address of the frame is the P2P
1879 * Device Address or P2P Interface Address. Do not add a new
1880 * peer entry based on this frames.
1881 */
1882 p2p_parse_free(&msg);
1883 return;
1884 }
1885
1886 dev = p2p_get_device(p2p, addr);
1887 if (dev) {
1888 if (dev->country[0] == 0 && msg.listen_channel)
1889 os_memcpy(dev->country, msg.listen_channel, 3);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001890 os_get_time(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001891 p2p_parse_free(&msg);
1892 return; /* already known */
1893 }
1894
1895 dev = p2p_create_device(p2p, addr);
1896 if (dev == NULL) {
1897 p2p_parse_free(&msg);
1898 return;
1899 }
1900
1901 os_get_time(&dev->last_seen);
1902 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1903
1904 if (msg.listen_channel) {
1905 os_memcpy(dev->country, msg.listen_channel, 3);
1906 dev->listen_freq = p2p_channel_to_freq(dev->country,
1907 msg.listen_channel[3],
1908 msg.listen_channel[4]);
1909 }
1910
1911 p2p_copy_wps_info(dev, 1, &msg);
1912
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001913 if (msg.wfd_subelems) {
1914 wpabuf_free(dev->info.wfd_subelems);
1915 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
1916 }
1917
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001918 p2p_parse_free(&msg);
1919
1920 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1921 "P2P: Created device entry based on Probe Req: " MACSTR
1922 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1923 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1924 dev->info.group_capab, dev->info.device_name,
1925 dev->listen_freq);
1926}
1927
1928
1929struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1930 const u8 *addr,
1931 struct p2p_message *msg)
1932{
1933 struct p2p_device *dev;
1934
1935 dev = p2p_get_device(p2p, addr);
1936 if (dev) {
1937 os_get_time(&dev->last_seen);
1938 return dev; /* already known */
1939 }
1940
1941 dev = p2p_create_device(p2p, addr);
1942 if (dev == NULL)
1943 return NULL;
1944
1945 p2p_add_dev_info(p2p, addr, dev, msg);
1946
1947 return dev;
1948}
1949
1950
1951static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1952{
1953 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1954 return 1;
1955 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1956 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1957 WPA_GET_BE16(&req_dev_type[6]) == 0)
1958 return 1; /* Category match with wildcard OUI/sub-category */
1959 return 0;
1960}
1961
1962
1963int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1964 size_t num_req_dev_type)
1965{
1966 size_t i;
1967 for (i = 0; i < num_req_dev_type; i++) {
1968 if (dev_type_match(dev_type, req_dev_type[i]))
1969 return 1;
1970 }
1971 return 0;
1972}
1973
1974
1975/**
1976 * p2p_match_dev_type - Match local device type with requested type
1977 * @p2p: P2P module context from p2p_init()
1978 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1979 * Returns: 1 on match, 0 on mismatch
1980 *
1981 * This function can be used to match the Requested Device Type attribute in
1982 * WPS IE with the local device types for deciding whether to reply to a Probe
1983 * Request frame.
1984 */
1985int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1986{
1987 struct wps_parse_attr attr;
1988 size_t i;
1989
1990 if (wps_parse_msg(wps, &attr))
1991 return 1; /* assume no Requested Device Type attributes */
1992
1993 if (attr.num_req_dev_type == 0)
1994 return 1; /* no Requested Device Type attributes -> match */
1995
1996 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1997 attr.num_req_dev_type))
1998 return 1; /* Own Primary Device Type matches */
1999
2000 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
2001 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
2002 attr.req_dev_type,
2003 attr.num_req_dev_type))
2004 return 1; /* Own Secondary Device Type matches */
2005
2006 /* No matching device type found */
2007 return 0;
2008}
2009
2010
2011struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
2012{
2013 struct wpabuf *buf;
2014 u8 *len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002015 int pw_id = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002016 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002017
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002018#ifdef CONFIG_WIFI_DISPLAY
2019 if (p2p->wfd_ie_probe_resp)
2020 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
2021#endif /* CONFIG_WIFI_DISPLAY */
2022
2023 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002024 if (buf == NULL)
2025 return NULL;
2026
Dmitry Shmidt04949592012-07-19 12:16:46 -07002027 if (p2p->go_neg_peer) {
2028 /* Advertise immediate availability of WPS credential */
2029 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
2030 }
2031
2032 p2p_build_wps_ie(p2p, buf, pw_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002033
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002034#ifdef CONFIG_WIFI_DISPLAY
2035 if (p2p->wfd_ie_probe_resp)
2036 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
2037#endif /* CONFIG_WIFI_DISPLAY */
2038
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 /* P2P IE */
2040 len = p2p_buf_add_ie_hdr(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002041 p2p_buf_add_capability(buf, p2p->dev_capab &
2042 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002043 if (p2p->ext_listen_interval)
2044 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
2045 p2p->ext_listen_interval);
2046 p2p_buf_add_device_info(buf, p2p, NULL);
2047 p2p_buf_update_ie_hdr(buf, len);
2048
2049 return buf;
2050}
2051
2052
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002053static int is_11b(u8 rate)
2054{
2055 return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
2056}
2057
2058
2059static int supp_rates_11b_only(struct ieee802_11_elems *elems)
2060{
2061 int num_11b = 0, num_others = 0;
2062 int i;
2063
2064 if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
2065 return 0;
2066
2067 for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
2068 if (is_11b(elems->supp_rates[i]))
2069 num_11b++;
2070 else
2071 num_others++;
2072 }
2073
2074 for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
2075 i++) {
2076 if (is_11b(elems->ext_supp_rates[i]))
2077 num_11b++;
2078 else
2079 num_others++;
2080 }
2081
2082 return num_11b > 0 && num_others == 0;
2083}
2084
2085
Dmitry Shmidt04949592012-07-19 12:16:46 -07002086static enum p2p_probe_req_status
2087p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2088 const u8 *bssid, const u8 *ie, size_t ie_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002089{
2090 struct ieee802_11_elems elems;
2091 struct wpabuf *buf;
2092 struct ieee80211_mgmt *resp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002093 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002094 struct wpabuf *ies;
2095
2096 if (!p2p->in_listen || !p2p->drv_in_listen) {
2097 /* not in Listen state - ignore Probe Request */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002098 return P2P_PREQ_NOT_LISTEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 }
2100
2101 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2102 ParseFailed) {
2103 /* Ignore invalid Probe Request frames */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002104 return P2P_PREQ_MALFORMED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105 }
2106
2107 if (elems.p2p == NULL) {
2108 /* not a P2P probe - ignore it */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002109 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002110 }
2111
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002112 if (dst && !is_broadcast_ether_addr(dst) &&
2113 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2114 /* Not sent to the broadcast address or our P2P Device Address
2115 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002116 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002117 }
2118
2119 if (bssid && !is_broadcast_ether_addr(bssid)) {
2120 /* Not sent to the Wildcard BSSID */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002121 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002122 }
2123
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002124 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2125 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2126 0) {
2127 /* not using P2P Wildcard SSID - ignore */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002128 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129 }
2130
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002131 if (supp_rates_11b_only(&elems)) {
2132 /* Indicates support for 11b rates only */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002133 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002134 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002135
2136 os_memset(&msg, 0, sizeof(msg));
2137 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2138 /* Could not parse P2P attributes */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002139 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002140 }
2141
2142 if (msg.device_id &&
Dmitry Shmidt04949592012-07-19 12:16:46 -07002143 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002144 /* Device ID did not match */
2145 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002146 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002147 }
2148
2149 /* Check Requested Device Type match */
2150 if (msg.wps_attributes &&
2151 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2152 /* No match with Requested Device Type */
2153 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002154 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002155 }
2156 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002157
Dmitry Shmidt04949592012-07-19 12:16:46 -07002158 if (!p2p->cfg->send_probe_resp) {
2159 /* Response generated elsewhere */
2160 return P2P_PREQ_NOT_PROCESSED;
2161 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162
2163 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2164 "P2P: Reply to P2P Probe Request in Listen state");
2165
2166 /*
2167 * We do not really have a specific BSS that this frame is advertising,
2168 * so build a frame that has some information in valid format. This is
2169 * really only used for discovery purposes, not to learn exact BSS
2170 * parameters.
2171 */
2172 ies = p2p_build_probe_resp_ies(p2p);
2173 if (ies == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002174 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002175
2176 buf = wpabuf_alloc(200 + wpabuf_len(ies));
2177 if (buf == NULL) {
2178 wpabuf_free(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002179 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180 }
2181
2182 resp = NULL;
2183 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
2184
2185 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2186 (WLAN_FC_STYPE_PROBE_RESP << 4));
2187 os_memcpy(resp->da, addr, ETH_ALEN);
2188 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2189 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2190 resp->u.probe_resp.beacon_int = host_to_le16(100);
2191 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2192 resp->u.probe_resp.capab_info =
2193 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2194 WLAN_CAPABILITY_PRIVACY |
2195 WLAN_CAPABILITY_SHORT_SLOT_TIME);
2196
2197 wpabuf_put_u8(buf, WLAN_EID_SSID);
2198 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2199 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2200
2201 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2202 wpabuf_put_u8(buf, 8);
2203 wpabuf_put_u8(buf, (60 / 5) | 0x80);
2204 wpabuf_put_u8(buf, 90 / 5);
2205 wpabuf_put_u8(buf, (120 / 5) | 0x80);
2206 wpabuf_put_u8(buf, 180 / 5);
2207 wpabuf_put_u8(buf, (240 / 5) | 0x80);
2208 wpabuf_put_u8(buf, 360 / 5);
2209 wpabuf_put_u8(buf, 480 / 5);
2210 wpabuf_put_u8(buf, 540 / 5);
2211
2212 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2213 wpabuf_put_u8(buf, 1);
2214 wpabuf_put_u8(buf, p2p->cfg->channel);
2215
2216 wpabuf_put_buf(buf, ies);
2217 wpabuf_free(ies);
2218
2219 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
2220
2221 wpabuf_free(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002222
2223 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002224}
2225
2226
Dmitry Shmidt04949592012-07-19 12:16:46 -07002227enum p2p_probe_req_status
2228p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2229 const u8 *bssid, const u8 *ie, size_t ie_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002230{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002231 enum p2p_probe_req_status res;
2232
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002233 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2234
Dmitry Shmidt04949592012-07-19 12:16:46 -07002235 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002236
2237 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2238 p2p->go_neg_peer &&
2239 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002240 == 0 &&
2241 !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002242 /* Received a Probe Request from GO Negotiation peer */
2243 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2244 "P2P: Found GO Negotiation peer - try to start GO "
2245 "negotiation from timeout");
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002246 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002247 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002248 return P2P_PREQ_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002249 }
2250
2251 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2252 p2p->invite_peer &&
2253 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2254 == 0) {
2255 /* Received a Probe Request from Invite peer */
2256 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2257 "P2P: Found Invite peer - try to start Invite from "
2258 "timeout");
2259 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002260 return P2P_PREQ_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002261 }
2262
Dmitry Shmidt04949592012-07-19 12:16:46 -07002263 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002264}
2265
2266
2267static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2268 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2269{
2270 struct wpabuf *tmp;
2271 u8 *lpos;
2272 size_t tmplen;
2273 int res;
2274 u8 group_capab;
2275
2276 if (p2p_ie == NULL)
2277 return 0; /* WLAN AP is not a P2P manager */
2278
2279 /*
2280 * (Re)Association Request - P2P IE
2281 * P2P Capability attribute (shall be present)
2282 * P2P Interface attribute (present if concurrent device and
2283 * P2P Management is enabled)
2284 */
2285 tmp = wpabuf_alloc(200);
2286 if (tmp == NULL)
2287 return -1;
2288
2289 lpos = p2p_buf_add_ie_hdr(tmp);
2290 group_capab = 0;
2291 if (p2p->num_groups > 0) {
2292 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2293 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2294 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2295 p2p->cross_connect)
2296 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2297 }
2298 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2299 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2300 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2301 p2p_buf_add_p2p_interface(tmp, p2p);
2302 p2p_buf_update_ie_hdr(tmp, lpos);
2303
2304 tmplen = wpabuf_len(tmp);
2305 if (tmplen > len)
2306 res = -1;
2307 else {
2308 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2309 res = tmplen;
2310 }
2311 wpabuf_free(tmp);
2312
2313 return res;
2314}
2315
2316
2317int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2318 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2319{
2320 struct wpabuf *tmp;
2321 u8 *lpos;
2322 struct p2p_device *peer;
2323 size_t tmplen;
2324 int res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002325 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002326
2327 if (!p2p_group)
2328 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2329
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002330#ifdef CONFIG_WIFI_DISPLAY
2331 if (p2p->wfd_ie_assoc_req)
2332 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2333#endif /* CONFIG_WIFI_DISPLAY */
2334
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002335 /*
2336 * (Re)Association Request - P2P IE
2337 * P2P Capability attribute (shall be present)
2338 * Extended Listen Timing (may be present)
2339 * P2P Device Info attribute (shall be present)
2340 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002341 tmp = wpabuf_alloc(200 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002342 if (tmp == NULL)
2343 return -1;
2344
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002345#ifdef CONFIG_WIFI_DISPLAY
2346 if (p2p->wfd_ie_assoc_req)
2347 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2348#endif /* CONFIG_WIFI_DISPLAY */
2349
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002350 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2351
2352 lpos = p2p_buf_add_ie_hdr(tmp);
2353 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2354 if (p2p->ext_listen_interval)
2355 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2356 p2p->ext_listen_interval);
2357 p2p_buf_add_device_info(tmp, p2p, peer);
2358 p2p_buf_update_ie_hdr(tmp, lpos);
2359
2360 tmplen = wpabuf_len(tmp);
2361 if (tmplen > len)
2362 res = -1;
2363 else {
2364 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2365 res = tmplen;
2366 }
2367 wpabuf_free(tmp);
2368
2369 return res;
2370}
2371
2372
2373int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2374{
2375 struct wpabuf *p2p_ie;
2376 int ret;
2377
2378 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2379 if (p2p_ie == NULL)
2380 return 0;
2381
2382 ret = p2p_attr_text(p2p_ie, buf, end);
2383 wpabuf_free(p2p_ie);
2384 return ret;
2385}
2386
2387
Dmitry Shmidt04949592012-07-19 12:16:46 -07002388int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2389{
2390 struct p2p_message msg;
2391
2392 os_memset(&msg, 0, sizeof(msg));
2393 if (p2p_parse_p2p_ie(p2p_ie, &msg))
2394 return -1;
2395
2396 if (msg.p2p_device_addr) {
2397 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2398 return 0;
2399 } else if (msg.device_id) {
2400 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2401 return 0;
2402 }
2403 return -1;
2404}
2405
2406
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002407int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2408{
2409 struct wpabuf *p2p_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002410 int ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002411
2412 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2413 P2P_IE_VENDOR_TYPE);
2414 if (p2p_ie == NULL)
2415 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002416 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002417 wpabuf_free(p2p_ie);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002418 return ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002419}
2420
2421
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002422static void p2p_clear_go_neg(struct p2p_data *p2p)
2423{
2424 p2p->go_neg_peer = NULL;
2425 p2p_clear_timeout(p2p);
2426 p2p_set_state(p2p, P2P_IDLE);
2427}
2428
2429
2430void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2431{
2432 if (p2p->go_neg_peer == NULL) {
2433 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2434 "P2P: No pending Group Formation - "
2435 "ignore WPS registration success notification");
2436 return; /* No pending Group Formation */
2437 }
2438
2439 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2440 0) {
2441 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2442 "P2P: Ignore WPS registration success notification "
2443 "for " MACSTR " (GO Negotiation peer " MACSTR ")",
2444 MAC2STR(mac_addr),
2445 MAC2STR(p2p->go_neg_peer->intended_addr));
2446 return; /* Ignore unexpected peer address */
2447 }
2448
2449 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2450 "P2P: Group Formation completed successfully with " MACSTR,
2451 MAC2STR(mac_addr));
2452
2453 p2p_clear_go_neg(p2p);
2454}
2455
2456
2457void p2p_group_formation_failed(struct p2p_data *p2p)
2458{
2459 if (p2p->go_neg_peer == NULL) {
2460 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2461 "P2P: No pending Group Formation - "
2462 "ignore group formation failure notification");
2463 return; /* No pending Group Formation */
2464 }
2465
2466 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2467 "P2P: Group Formation failed with " MACSTR,
2468 MAC2STR(p2p->go_neg_peer->intended_addr));
2469
2470 p2p_clear_go_neg(p2p);
2471}
2472
2473
2474struct p2p_data * p2p_init(const struct p2p_config *cfg)
2475{
2476 struct p2p_data *p2p;
2477
2478 if (cfg->max_peers < 1)
2479 return NULL;
2480
2481 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2482 if (p2p == NULL)
2483 return NULL;
2484 p2p->cfg = (struct p2p_config *) (p2p + 1);
2485 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2486 if (cfg->dev_name)
2487 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2488 if (cfg->manufacturer)
2489 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2490 if (cfg->model_name)
2491 p2p->cfg->model_name = os_strdup(cfg->model_name);
2492 if (cfg->model_number)
2493 p2p->cfg->model_number = os_strdup(cfg->model_number);
2494 if (cfg->serial_number)
2495 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002496 if (cfg->pref_chan) {
2497 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2498 sizeof(struct p2p_channel));
2499 if (p2p->cfg->pref_chan) {
2500 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2501 cfg->num_pref_chan *
2502 sizeof(struct p2p_channel));
2503 } else
2504 p2p->cfg->num_pref_chan = 0;
2505 }
2506
Irfan Sherifff44b9c42012-06-13 11:09:23 -07002507#ifdef ANDROID_P2P
2508 /* 100ms listen time is too less to receive the response frames in some scenarios
2509 * increasing min listen time to 200ms.
2510 */
2511 p2p->min_disc_int = 2;
2512 /* SD_FAIR_POLICY: Initializing the SD current serviced pointer to NULL */
2513 p2p->sd_dev_list = NULL;
2514#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515 p2p->min_disc_int = 1;
Irfan Sherifff44b9c42012-06-13 11:09:23 -07002516#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002517 p2p->max_disc_int = 3;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002518 p2p->max_disc_tu = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002519
2520 os_get_random(&p2p->next_tie_breaker, 1);
2521 p2p->next_tie_breaker &= 0x01;
2522 if (cfg->sd_request)
2523 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2524 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2525 if (cfg->concurrent_operations)
2526 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2527 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2528
2529 dl_list_init(&p2p->devices);
2530
2531 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
2532 p2p_expiration_timeout, p2p, NULL);
2533
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002534 p2p->go_timeout = 100;
2535 p2p->client_timeout = 20;
2536
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002537 return p2p;
2538}
2539
2540
2541void p2p_deinit(struct p2p_data *p2p)
2542{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002543#ifdef CONFIG_WIFI_DISPLAY
2544 wpabuf_free(p2p->wfd_ie_beacon);
2545 wpabuf_free(p2p->wfd_ie_probe_req);
2546 wpabuf_free(p2p->wfd_ie_probe_resp);
2547 wpabuf_free(p2p->wfd_ie_assoc_req);
2548 wpabuf_free(p2p->wfd_ie_invitation);
2549 wpabuf_free(p2p->wfd_ie_prov_disc_req);
2550 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
2551 wpabuf_free(p2p->wfd_ie_go_neg);
2552 wpabuf_free(p2p->wfd_dev_info);
2553 wpabuf_free(p2p->wfd_assoc_bssid);
2554 wpabuf_free(p2p->wfd_coupled_sink_info);
2555#endif /* CONFIG_WIFI_DISPLAY */
2556
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
2558 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2559 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002560 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002561 p2p_flush(p2p);
2562 p2p_free_req_dev_types(p2p);
2563 os_free(p2p->cfg->dev_name);
2564 os_free(p2p->cfg->manufacturer);
2565 os_free(p2p->cfg->model_name);
2566 os_free(p2p->cfg->model_number);
2567 os_free(p2p->cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002568 os_free(p2p->cfg->pref_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002569 os_free(p2p->groups);
2570 wpabuf_free(p2p->sd_resp);
2571 os_free(p2p->after_scan_tx);
2572 p2p_remove_wps_vendor_extensions(p2p);
2573 os_free(p2p);
2574}
2575
2576
2577void p2p_flush(struct p2p_data *p2p)
2578{
2579 struct p2p_device *dev, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002580 p2p_stop_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002581 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2582 list) {
2583 dl_list_del(&dev->list);
2584 p2p_device_free(p2p, dev);
2585 }
Irfan Sherifff44b9c42012-06-13 11:09:23 -07002586#ifdef ANDROID_P2P
2587 /* SD_FAIR_POLICY: Initializing the SD current serviced pointer to NULL */
2588 p2p->sd_dev_list = NULL;
2589#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002590 p2p_free_sd_queries(p2p);
2591 os_free(p2p->after_scan_tx);
2592 p2p->after_scan_tx = NULL;
2593}
2594
2595
2596int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2597{
2598 struct p2p_device *dev;
2599
2600 dev = p2p_get_device(p2p, addr);
2601 if (dev == NULL)
2602 return -1;
2603
2604 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
2605 MAC2STR(addr));
2606
2607 if (p2p->go_neg_peer == dev)
2608 p2p->go_neg_peer = NULL;
2609
2610 dev->wps_method = WPS_NOT_READY;
2611 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2612 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2613
2614 /* Check if after_scan_tx is for this peer. If so free it */
2615 if (p2p->after_scan_tx &&
2616 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2617 os_free(p2p->after_scan_tx);
2618 p2p->after_scan_tx = NULL;
2619 }
2620
2621 return 0;
2622}
2623
2624
2625int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2626{
2627 os_free(p2p->cfg->dev_name);
2628 if (dev_name) {
2629 p2p->cfg->dev_name = os_strdup(dev_name);
2630 if (p2p->cfg->dev_name == NULL)
2631 return -1;
2632 } else
2633 p2p->cfg->dev_name = NULL;
2634 return 0;
2635}
2636
2637
2638int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2639{
2640 os_free(p2p->cfg->manufacturer);
2641 p2p->cfg->manufacturer = NULL;
2642 if (manufacturer) {
2643 p2p->cfg->manufacturer = os_strdup(manufacturer);
2644 if (p2p->cfg->manufacturer == NULL)
2645 return -1;
2646 }
2647
2648 return 0;
2649}
2650
2651
2652int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2653{
2654 os_free(p2p->cfg->model_name);
2655 p2p->cfg->model_name = NULL;
2656 if (model_name) {
2657 p2p->cfg->model_name = os_strdup(model_name);
2658 if (p2p->cfg->model_name == NULL)
2659 return -1;
2660 }
2661
2662 return 0;
2663}
2664
2665
2666int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2667{
2668 os_free(p2p->cfg->model_number);
2669 p2p->cfg->model_number = NULL;
2670 if (model_number) {
2671 p2p->cfg->model_number = os_strdup(model_number);
2672 if (p2p->cfg->model_number == NULL)
2673 return -1;
2674 }
2675
2676 return 0;
2677}
2678
2679
2680int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2681{
2682 os_free(p2p->cfg->serial_number);
2683 p2p->cfg->serial_number = NULL;
2684 if (serial_number) {
2685 p2p->cfg->serial_number = os_strdup(serial_number);
2686 if (p2p->cfg->serial_number == NULL)
2687 return -1;
2688 }
2689
2690 return 0;
2691}
2692
2693
2694void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2695{
2696 p2p->cfg->config_methods = config_methods;
2697}
2698
2699
2700void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2701{
2702 os_memcpy(p2p->cfg->uuid, uuid, 16);
2703}
2704
2705
2706int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2707{
2708 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2709 return 0;
2710}
2711
2712
2713int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2714 size_t num_dev_types)
2715{
2716 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2717 num_dev_types = P2P_SEC_DEVICE_TYPES;
2718 p2p->cfg->num_sec_dev_types = num_dev_types;
2719 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2720 return 0;
2721}
2722
2723
2724void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2725{
2726 int i;
2727
2728 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2729 wpabuf_free(p2p->wps_vendor_ext[i]);
2730 p2p->wps_vendor_ext[i] = NULL;
2731 }
2732}
2733
2734
2735int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2736 const struct wpabuf *vendor_ext)
2737{
2738 int i;
2739
2740 if (vendor_ext == NULL)
2741 return -1;
2742
2743 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2744 if (p2p->wps_vendor_ext[i] == NULL)
2745 break;
2746 }
2747 if (i >= P2P_MAX_WPS_VENDOR_EXT)
2748 return -1;
2749
2750 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2751 if (p2p->wps_vendor_ext[i] == NULL)
2752 return -1;
2753
2754 return 0;
2755}
2756
2757
2758int p2p_set_country(struct p2p_data *p2p, const char *country)
2759{
2760 os_memcpy(p2p->cfg->country, country, 3);
2761 return 0;
2762}
2763
2764
2765void p2p_continue_find(struct p2p_data *p2p)
2766{
2767 struct p2p_device *dev;
Irfan Sherifff44b9c42012-06-13 11:09:23 -07002768#ifdef ANDROID_P2P
2769 int skip=1;
2770#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002771 p2p_set_state(p2p, P2P_SEARCH);
2772 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Irfan Sherifff44b9c42012-06-13 11:09:23 -07002773#ifdef ANDROID_P2P
2774 /* SD_FAIR_POLICY: We need to give chance to all devices in the device list
2775 * There may be a scenario, where a particular peer device have
2776 * not registered any query response. When we send a SD request to such device,
2777 * no response will be received. And if we continue to get probe responses from that device,
2778 * and if that device happens to be on top in our device list,
2779 * we will always continue to send SD requests always to that peer only.
2780 * We will not be able to send SD requests to other devices in that case.
2781 * This implementation keeps track of last serviced peer device.
2782 * And then takes the next one from the device list, in the next iteration.
2783 */
2784 if (p2p->sd_dev_list && p2p->sd_dev_list != &p2p->devices) {
2785 if(skip) {
2786 if ((&dev->list == p2p->sd_dev_list) ) {
2787 skip = 0;
2788 if (dev->list.next == &p2p->devices)
2789 p2p->sd_dev_list = NULL;
2790 }
2791 continue;
2792 }
2793 }
2794 p2p->sd_dev_list = &dev->list;
2795 wpa_printf(MSG_DEBUG, "P2P: ### Servicing %p dev->flags 0x%x SD schedule %s devaddr " MACSTR,
2796 p2p->sd_dev_list, dev->flags, dev->flags & P2P_DEV_SD_SCHEDULE ? "TRUE": "FALSE",
2797 MAC2STR(dev->info.p2p_device_addr));
2798#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002799 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2800 if (p2p_start_sd(p2p, dev) == 0)
2801 return;
2802 else
2803 break;
2804 } else if (dev->req_config_methods &&
2805 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2806 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002807 "pending Provision Discovery Request to "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002808 MACSTR " (config methods 0x%x)",
2809 MAC2STR(dev->info.p2p_device_addr),
2810 dev->req_config_methods);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002811 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002812 return;
2813 }
2814 }
2815
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002816 p2p_listen_in_find(p2p, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002817}
2818
2819
2820static void p2p_sd_cb(struct p2p_data *p2p, int success)
2821{
2822 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2823 "P2P: Service Discovery Query TX callback: success=%d",
2824 success);
2825 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2826
2827 if (!success) {
2828 if (p2p->sd_peer) {
2829 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2830 p2p->sd_peer = NULL;
2831 }
2832 p2p_continue_find(p2p);
2833 return;
2834 }
2835
2836 if (p2p->sd_peer == NULL) {
2837 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2838 "P2P: No SD peer entry known");
2839 p2p_continue_find(p2p);
2840 return;
2841 }
2842
2843 /* Wait for response from the peer */
2844 p2p_set_state(p2p, P2P_SD_DURING_FIND);
2845 p2p_set_timeout(p2p, 0, 200000);
2846}
2847
Jouni Malinen75ecf522011-06-27 15:19:46 -07002848
2849/**
2850 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2851 * @p2p: P2P module context from p2p_init()
2852 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002853static void p2p_retry_pd(struct p2p_data *p2p)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002854{
2855 struct p2p_device *dev;
2856
2857 if (p2p->state != P2P_IDLE)
2858 return;
2859
2860 /*
2861 * Retry the prov disc req attempt only for the peer that the user had
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002862 * requested.
Jouni Malinen75ecf522011-06-27 15:19:46 -07002863 */
2864
2865 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2866 if (os_memcmp(p2p->pending_pd_devaddr,
2867 dev->info.p2p_device_addr, ETH_ALEN) != 0)
2868 continue;
2869 if (!dev->req_config_methods)
2870 continue;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002871
2872 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002873 "pending Provision Discovery Request to "
Jouni Malinen75ecf522011-06-27 15:19:46 -07002874 MACSTR " (config methods 0x%x)",
2875 MAC2STR(dev->info.p2p_device_addr),
2876 dev->req_config_methods);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002877 p2p_send_prov_disc_req(p2p, dev,
2878 dev->flags & P2P_DEV_PD_FOR_JOIN, 0);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002879 return;
2880 }
2881}
2882
2883
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002884static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2885{
2886 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2887 "P2P: Provision Discovery Request TX callback: success=%d",
2888 success);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002889
2890 /*
2891 * Postpone resetting the pending action state till after we actually
2892 * time out. This allows us to take some action like notifying any
2893 * interested parties about no response to the request.
2894 *
2895 * When the timer (below) goes off we check in IDLE, SEARCH, or
2896 * LISTEN_ONLY state, which are the only allowed states to issue a PD
2897 * requests in, if this was still pending and then raise notification.
2898 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002899
2900 if (!success) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07002901 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2902
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002903 if (p2p->user_initiated_pd &&
2904 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
2905 {
2906 /* Retry request from timeout to avoid busy loops */
2907 p2p->pending_action_state = P2P_PENDING_PD;
2908 p2p_set_timeout(p2p, 0, 50000);
2909 } else if (p2p->state != P2P_IDLE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002910 p2p_continue_find(p2p);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002911 else if (p2p->user_initiated_pd) {
2912 p2p->pending_action_state = P2P_PENDING_PD;
Irfan Sheriff1a2ce112012-10-30 22:22:52 -07002913#ifdef ANDROID_P2P
2914 p2p_set_timeout(p2p, 0, 350000);
2915#else
Jouni Malinen75ecf522011-06-27 15:19:46 -07002916 p2p_set_timeout(p2p, 0, 300000);
Irfan Sheriff1a2ce112012-10-30 22:22:52 -07002917#endif
Jouni Malinen75ecf522011-06-27 15:19:46 -07002918 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002919 return;
2920 }
2921
Jouni Malinen75ecf522011-06-27 15:19:46 -07002922 /*
2923 * This postponing, of resetting pending_action_state, needs to be
2924 * done only for user initiated PD requests and not internal ones.
2925 */
2926 if (p2p->user_initiated_pd)
2927 p2p->pending_action_state = P2P_PENDING_PD;
2928 else
2929 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002931 /* Wait for response from the peer */
2932 if (p2p->state == P2P_SEARCH)
2933 p2p_set_state(p2p, P2P_PD_DURING_FIND);
Irfan Sheriff1a2ce112012-10-30 22:22:52 -07002934#ifdef ANDROID_P2P
2935 p2p_set_timeout(p2p, 0, 350000);
2936#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002937 p2p_set_timeout(p2p, 0, 200000);
Irfan Sheriff1a2ce112012-10-30 22:22:52 -07002938#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002939}
2940
2941
2942int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002943 struct os_time *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002944 size_t ies_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002945{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002946 if (os_time_before(rx_time, &p2p->find_start)) {
2947 /*
2948 * The driver may have cached (e.g., in cfg80211 BSS table) the
2949 * scan results for relatively long time. To avoid reporting
2950 * stale information, update P2P peers only based on results
2951 * that have based on frames received after the last p2p_find
2952 * operation was started.
2953 */
2954 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Ignore old scan "
2955 "result for " MACSTR " (rx_time=%u.%06u)",
2956 MAC2STR(bssid), (unsigned int) rx_time->sec,
2957 (unsigned int) rx_time->usec);
2958 return 0;
2959 }
2960
2961 p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002962
2963 return 0;
2964}
2965
2966
2967void p2p_scan_res_handled(struct p2p_data *p2p)
2968{
2969 if (!p2p->p2p_scan_running) {
2970 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
2971 "running, but scan results received");
2972 }
2973 p2p->p2p_scan_running = 0;
2974 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2975
2976 if (p2p_run_after_scan(p2p))
2977 return;
2978 if (p2p->state == P2P_SEARCH)
2979 p2p_continue_find(p2p);
2980}
2981
2982
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002983void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002984{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002985 u8 *len;
2986
2987#ifdef CONFIG_WIFI_DISPLAY
2988 if (p2p->wfd_ie_probe_req)
2989 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
2990#endif /* CONFIG_WIFI_DISPLAY */
2991
2992 len = p2p_buf_add_ie_hdr(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002993 p2p_buf_add_capability(ies, p2p->dev_capab &
2994 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002995 if (dev_id)
2996 p2p_buf_add_device_id(ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002997 if (p2p->cfg->reg_class && p2p->cfg->channel)
2998 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2999 p2p->cfg->reg_class,
3000 p2p->cfg->channel);
3001 if (p2p->ext_listen_interval)
3002 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
3003 p2p->ext_listen_interval);
3004 /* TODO: p2p_buf_add_operating_channel() if GO */
3005 p2p_buf_update_ie_hdr(ies, len);
3006}
3007
3008
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003009size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
3010{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003011 size_t len = 100;
3012
3013#ifdef CONFIG_WIFI_DISPLAY
3014 if (p2p && p2p->wfd_ie_probe_req)
3015 len += wpabuf_len(p2p->wfd_ie_probe_req);
3016#endif /* CONFIG_WIFI_DISPLAY */
3017
3018 return len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003019}
3020
3021
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003022int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
3023{
3024 return p2p_attr_text(p2p_ie, buf, end);
3025}
3026
3027
3028static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
3029{
3030 struct p2p_device *dev = p2p->go_neg_peer;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003031 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003032
3033 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3034 "P2P: GO Negotiation Request TX callback: success=%d",
3035 success);
3036
3037 if (dev == NULL) {
3038 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3039 "P2P: No pending GO Negotiation");
3040 return;
3041 }
3042
3043 if (success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003044 if (dev->flags & P2P_DEV_USER_REJECTED) {
3045 p2p_set_state(p2p, P2P_IDLE);
3046 return;
3047 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003048 } else if (dev->go_neg_req_sent) {
3049 /* Cancel the increment from p2p_connect_send() on failure */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07003050 dev->go_neg_req_sent--;
3051 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052
3053 if (!success &&
3054 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
3055 !is_zero_ether_addr(dev->member_in_go_dev)) {
3056 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3057 "P2P: Peer " MACSTR " did not acknowledge request - "
3058 "try to use device discoverability through its GO",
3059 MAC2STR(dev->info.p2p_device_addr));
3060 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3061 p2p_send_dev_disc_req(p2p, dev);
3062 return;
3063 }
3064
3065 /*
3066 * Use P2P find, if needed, to find the other device from its listen
3067 * channel.
3068 */
3069 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003070 timeout = success ? 500000 : 100000;
3071 if (!success && p2p->go_neg_peer &&
3072 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
3073 unsigned int r;
3074 /*
3075 * Peer is expected to wait our response and we will skip the
3076 * listen phase. Add some randomness to the wait time here to
3077 * make it less likely to hit cases where we could end up in
3078 * sync with peer not listening.
3079 */
3080 os_get_random((u8 *) &r, sizeof(r));
3081 timeout += r % 100000;
3082 }
3083 p2p_set_timeout(p2p, 0, timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003084}
3085
3086
3087static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
3088{
3089 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3090 "P2P: GO Negotiation Response TX callback: success=%d",
3091 success);
3092 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
3093 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3094 "P2P: Ignore TX callback event - GO Negotiation is "
3095 "not running anymore");
3096 return;
3097 }
3098 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003099 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003100}
3101
3102
3103static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
3104{
3105 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3106 "P2P: GO Negotiation Response (failure) TX callback: "
3107 "success=%d", success);
3108 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
3109 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
3110 p2p->go_neg_peer->status);
3111 }
3112}
3113
3114
3115static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
3116 enum p2p_send_action_result result)
3117{
3118 struct p2p_device *dev;
3119
3120 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3121 "P2P: GO Negotiation Confirm TX callback: result=%d",
3122 result);
3123 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3124 if (result == P2P_SEND_ACTION_FAILED) {
3125 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3126 return;
3127 }
3128 if (result == P2P_SEND_ACTION_NO_ACK) {
3129 /*
3130 * It looks like the TX status for GO Negotiation Confirm is
3131 * often showing failure even when the peer has actually
3132 * received the frame. Since the peer may change channels
3133 * immediately after having received the frame, we may not see
3134 * an Ack for retries, so just dropping a single frame may
3135 * trigger this. To allow the group formation to succeed if the
3136 * peer did indeed receive the frame, continue regardless of
3137 * the TX status.
3138 */
3139 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3140 "P2P: Assume GO Negotiation Confirm TX was actually "
3141 "received by the peer even though Ack was not "
3142 "reported");
3143 }
3144
3145 dev = p2p->go_neg_peer;
3146 if (dev == NULL)
3147 return;
3148
3149 p2p_go_complete(p2p, dev);
3150}
3151
3152
3153void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3154 const u8 *src, const u8 *bssid,
3155 enum p2p_send_action_result result)
3156{
3157 enum p2p_pending_action_state state;
3158 int success;
3159
3160 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3161 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
3162 " src=" MACSTR " bssid=" MACSTR " result=%d",
3163 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
3164 MAC2STR(bssid), result);
3165 success = result == P2P_SEND_ACTION_SUCCESS;
3166 state = p2p->pending_action_state;
3167 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3168 switch (state) {
3169 case P2P_NO_PENDING_ACTION:
3170 break;
3171 case P2P_PENDING_GO_NEG_REQUEST:
3172 p2p_go_neg_req_cb(p2p, success);
3173 break;
3174 case P2P_PENDING_GO_NEG_RESPONSE:
3175 p2p_go_neg_resp_cb(p2p, success);
3176 break;
3177 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
3178 p2p_go_neg_resp_failure_cb(p2p, success);
3179 break;
3180 case P2P_PENDING_GO_NEG_CONFIRM:
3181 p2p_go_neg_conf_cb(p2p, result);
3182 break;
3183 case P2P_PENDING_SD:
3184 p2p_sd_cb(p2p, success);
3185 break;
3186 case P2P_PENDING_PD:
3187 p2p_prov_disc_cb(p2p, success);
3188 break;
3189 case P2P_PENDING_INVITATION_REQUEST:
3190 p2p_invitation_req_cb(p2p, success);
3191 break;
3192 case P2P_PENDING_INVITATION_RESPONSE:
3193 p2p_invitation_resp_cb(p2p, success);
3194 break;
3195 case P2P_PENDING_DEV_DISC_REQUEST:
3196 p2p_dev_disc_req_cb(p2p, success);
3197 break;
3198 case P2P_PENDING_DEV_DISC_RESPONSE:
3199 p2p_dev_disc_resp_cb(p2p, success);
3200 break;
3201 case P2P_PENDING_GO_DISC_REQ:
3202 p2p_go_disc_req_cb(p2p, success);
3203 break;
3204 }
3205}
3206
3207
3208void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3209 unsigned int duration)
3210{
3211 if (freq == p2p->pending_client_disc_freq) {
3212 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3213 "P2P: Client discoverability remain-awake completed");
3214 p2p->pending_client_disc_freq = 0;
3215 return;
3216 }
3217
3218 if (freq != p2p->pending_listen_freq) {
3219 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3220 "P2P: Unexpected listen callback for freq=%u "
3221 "duration=%u (pending_listen_freq=%u)",
3222 freq, duration, p2p->pending_listen_freq);
3223 return;
3224 }
3225
3226 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3227 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
3228 "callback",
3229 p2p->pending_listen_sec, p2p->pending_listen_usec,
3230 p2p->pending_listen_freq);
3231 p2p->in_listen = 1;
3232 p2p->drv_in_listen = freq;
3233 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3234 /*
3235 * Add 20 msec extra wait to avoid race condition with driver
3236 * remain-on-channel end event, i.e., give driver more time to
3237 * complete the operation before our timeout expires.
3238 */
3239 p2p_set_timeout(p2p, p2p->pending_listen_sec,
3240 p2p->pending_listen_usec + 20000);
3241 }
3242
3243 p2p->pending_listen_freq = 0;
3244}
3245
3246
3247int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3248{
3249 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
3250 "state (freq=%u)", freq);
3251 p2p->drv_in_listen = 0;
3252 if (p2p->in_listen)
3253 return 0; /* Internal timeout will trigger the next step */
3254
3255 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3256 if (p2p->go_neg_peer->connect_reqs >= 120) {
3257 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3258 "P2P: Timeout on sending GO Negotiation "
3259 "Request without getting response");
3260 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3261 return 0;
3262 }
3263
3264 p2p_set_state(p2p, P2P_CONNECT);
3265 p2p_connect_send(p2p, p2p->go_neg_peer);
3266 return 1;
3267 } else if (p2p->state == P2P_SEARCH) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003268 if (p2p->p2p_scan_running) {
3269 /*
3270 * Search is already in progress. This can happen if
3271 * an Action frame RX is reported immediately after
3272 * the end of a remain-on-channel operation and the
3273 * response frame to that is sent using an offchannel
3274 * operation while in p2p_find. Avoid an attempt to
3275 * restart a scan here.
3276 */
3277 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan "
3278 "already in progress - do not try to start a "
3279 "new one");
3280 return 1;
3281 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003282 if (p2p->pending_listen_freq) {
3283 /*
3284 * Better wait a bit if the driver is unable to start
3285 * offchannel operation for some reason. p2p_search()
3286 * will be started from internal timeout.
3287 */
3288 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Listen "
3289 "operation did not seem to start - delay "
3290 "search phase to avoid busy loop");
3291 p2p_set_timeout(p2p, 0, 100000);
3292 return 1;
3293 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003294 if (p2p->search_delay) {
3295 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
3296 "search operation by %u ms",
3297 p2p->search_delay);
3298 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3299 (p2p->search_delay % 1000) * 1000);
3300 return 1;
3301 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003302 p2p_search(p2p);
3303 return 1;
3304 }
3305
3306 return 0;
3307}
3308
3309
3310static void p2p_timeout_connect(struct p2p_data *p2p)
3311{
3312 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003313 if (p2p->go_neg_peer &&
3314 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
3315 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Wait for GO "
3316 "Negotiation Confirm timed out - assume GO "
3317 "Negotiation failed");
3318 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3319 return;
3320 }
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003321 if (p2p->go_neg_peer &&
3322 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
3323 p2p->go_neg_peer->connect_reqs < 120) {
3324 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer expected to "
3325 "wait our response - skip listen");
3326 p2p_connect_send(p2p, p2p->go_neg_peer);
3327 return;
3328 }
3329
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003330 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003331 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003332}
3333
3334
3335static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3336{
3337 if (p2p->go_neg_peer) {
3338 if (p2p->drv_in_listen) {
3339 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
3340 "still in Listen state; wait for it to "
3341 "complete");
3342 return;
3343 }
3344
3345 if (p2p->go_neg_peer->connect_reqs >= 120) {
3346 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3347 "P2P: Timeout on sending GO Negotiation "
3348 "Request without getting response");
3349 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3350 return;
3351 }
3352
3353 p2p_set_state(p2p, P2P_CONNECT);
3354 p2p_connect_send(p2p, p2p->go_neg_peer);
3355 } else
3356 p2p_set_state(p2p, P2P_IDLE);
3357}
3358
3359
3360static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3361{
3362 /*
3363 * TODO: could remain constantly in Listen state for some time if there
3364 * are no other concurrent uses for the radio. For now, go to listen
3365 * state once per second to give other uses a chance to use the radio.
3366 */
3367 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003368 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003369}
3370
3371
3372static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3373{
3374 struct p2p_device *dev = p2p->go_neg_peer;
3375
3376 if (dev == NULL) {
3377 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3378 "P2P: Unknown GO Neg peer - stop GO Neg wait");
3379 return;
3380 }
3381
3382 dev->wait_count++;
3383 if (dev->wait_count >= 120) {
3384 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3385 "P2P: Timeout on waiting peer to become ready for GO "
3386 "Negotiation");
3387 p2p_go_neg_failed(p2p, dev, -1);
3388 return;
3389 }
3390
3391 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3392 "P2P: Go to Listen state while waiting for the peer to become "
3393 "ready for GO Negotiation");
3394 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003395 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003396}
3397
3398
3399static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3400{
3401 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3402 "P2P: Service Discovery Query timeout");
3403 if (p2p->sd_peer) {
3404 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3405 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
3406 p2p->sd_peer = NULL;
3407 }
3408 p2p_continue_find(p2p);
3409}
3410
3411
3412static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
3413{
3414 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3415 "P2P: Provision Discovery Request timeout");
3416 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3417 p2p_continue_find(p2p);
3418}
3419
3420
Jouni Malinen75ecf522011-06-27 15:19:46 -07003421static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
3422{
3423 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3424
3425 /*
3426 * For user initiated PD requests that we have not gotten any responses
3427 * for while in IDLE state, we retry them a couple of times before
3428 * giving up.
3429 */
3430 if (!p2p->user_initiated_pd)
3431 return;
3432
3433 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3434 "P2P: User initiated Provision Discovery Request timeout");
3435
3436 if (p2p->pd_retries) {
3437 p2p->pd_retries--;
3438 p2p_retry_pd(p2p);
3439 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003440 struct p2p_device *dev;
3441 int for_join = 0;
3442
3443 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3444 if (os_memcmp(p2p->pending_pd_devaddr,
3445 dev->info.p2p_device_addr, ETH_ALEN) != 0)
3446 continue;
3447 if (dev->req_config_methods &&
3448 (dev->flags & P2P_DEV_PD_FOR_JOIN))
3449 for_join = 1;
3450 }
3451
Jouni Malinen75ecf522011-06-27 15:19:46 -07003452 if (p2p->cfg->prov_disc_fail)
3453 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
3454 p2p->pending_pd_devaddr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003455 for_join ?
3456 P2P_PROV_DISC_TIMEOUT_JOIN :
Jouni Malinen75ecf522011-06-27 15:19:46 -07003457 P2P_PROV_DISC_TIMEOUT);
3458 p2p_reset_pending_pd(p2p);
3459 }
3460}
3461
3462
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003463static void p2p_timeout_invite(struct p2p_data *p2p)
3464{
3465 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3466 p2p_set_state(p2p, P2P_INVITE_LISTEN);
3467 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
3468 /*
3469 * Better remain on operating channel instead of listen channel
3470 * when running a group.
3471 */
3472 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
3473 "active GO role - wait on operating channel");
3474 p2p_set_timeout(p2p, 0, 100000);
3475 return;
3476 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003477 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003478}
3479
3480
3481static void p2p_timeout_invite_listen(struct p2p_data *p2p)
3482{
3483 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
3484 p2p_set_state(p2p, P2P_INVITE);
3485 p2p_invite_send(p2p, p2p->invite_peer,
3486 p2p->invite_go_dev_addr);
3487 } else {
3488 if (p2p->invite_peer) {
3489 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3490 "P2P: Invitation Request retry limit reached");
3491 if (p2p->cfg->invitation_result)
3492 p2p->cfg->invitation_result(
3493 p2p->cfg->cb_ctx, -1, NULL);
3494 }
3495 p2p_set_state(p2p, P2P_IDLE);
3496 }
3497}
3498
3499
3500static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
3501{
3502 struct p2p_data *p2p = eloop_ctx;
3503
3504 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
3505 p2p_state_txt(p2p->state));
3506
3507 p2p->in_listen = 0;
3508
3509 switch (p2p->state) {
3510 case P2P_IDLE:
Jouni Malinen75ecf522011-06-27 15:19:46 -07003511 /* Check if we timed out waiting for PD req */
3512 if (p2p->pending_action_state == P2P_PENDING_PD)
3513 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003514 break;
3515 case P2P_SEARCH:
Jouni Malinen75ecf522011-06-27 15:19:46 -07003516 /* Check if we timed out waiting for PD req */
3517 if (p2p->pending_action_state == P2P_PENDING_PD)
3518 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003519 if (p2p->search_delay && !p2p->in_search_delay) {
3520 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
3521 "search operation by %u ms",
3522 p2p->search_delay);
3523 p2p->in_search_delay = 1;
3524 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3525 (p2p->search_delay % 1000) * 1000);
3526 break;
3527 }
3528 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003529 p2p_search(p2p);
3530 break;
3531 case P2P_CONNECT:
3532 p2p_timeout_connect(p2p);
3533 break;
3534 case P2P_CONNECT_LISTEN:
3535 p2p_timeout_connect_listen(p2p);
3536 break;
3537 case P2P_GO_NEG:
3538 break;
3539 case P2P_LISTEN_ONLY:
Jouni Malinen75ecf522011-06-27 15:19:46 -07003540 /* Check if we timed out waiting for PD req */
3541 if (p2p->pending_action_state == P2P_PENDING_PD)
3542 p2p_timeout_prov_disc_req(p2p);
3543
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003544 if (p2p->ext_listen_only) {
3545 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3546 "P2P: Extended Listen Timing - Listen State "
3547 "completed");
3548 p2p->ext_listen_only = 0;
3549 p2p_set_state(p2p, P2P_IDLE);
3550 }
3551 break;
3552 case P2P_WAIT_PEER_CONNECT:
3553 p2p_timeout_wait_peer_connect(p2p);
3554 break;
3555 case P2P_WAIT_PEER_IDLE:
3556 p2p_timeout_wait_peer_idle(p2p);
3557 break;
3558 case P2P_SD_DURING_FIND:
3559 p2p_timeout_sd_during_find(p2p);
3560 break;
3561 case P2P_PROVISIONING:
3562 break;
3563 case P2P_PD_DURING_FIND:
3564 p2p_timeout_prov_disc_during_find(p2p);
3565 break;
3566 case P2P_INVITE:
3567 p2p_timeout_invite(p2p);
3568 break;
3569 case P2P_INVITE_LISTEN:
3570 p2p_timeout_invite_listen(p2p);
3571 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003572 case P2P_SEARCH_WHEN_READY:
3573 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003574 case P2P_CONTINUE_SEARCH_WHEN_READY:
3575 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003576 }
3577}
3578
3579
3580int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
3581{
3582 struct p2p_device *dev;
3583
3584 dev = p2p_get_device(p2p, peer_addr);
3585 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
3586 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
3587 if (dev == NULL) {
3588 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
3589 " unknown", MAC2STR(peer_addr));
3590 return -1;
3591 }
3592 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
3593 dev->flags |= P2P_DEV_USER_REJECTED;
3594 return 0;
3595}
3596
3597
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003598const char * p2p_wps_method_text(enum p2p_wps_method method)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003599{
3600 switch (method) {
3601 case WPS_NOT_READY:
3602 return "not-ready";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003603 case WPS_PIN_DISPLAY:
3604 return "Display";
3605 case WPS_PIN_KEYPAD:
3606 return "Keypad";
3607 case WPS_PBC:
3608 return "PBC";
3609 }
3610
3611 return "??";
3612}
3613
3614
3615static const char * p2p_go_state_text(enum p2p_go_state go_state)
3616{
3617 switch (go_state) {
3618 case UNKNOWN_GO:
3619 return "unknown";
3620 case LOCAL_GO:
3621 return "local";
3622 case REMOTE_GO:
3623 return "remote";
3624 }
3625
3626 return "??";
3627}
3628
3629
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003630const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
3631 const u8 *addr, int next)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003632{
3633 struct p2p_device *dev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003634
3635 if (addr)
3636 dev = p2p_get_device(p2p, addr);
3637 else
3638 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3639
3640 if (dev && next) {
3641 dev = dl_list_first(&dev->list, struct p2p_device, list);
3642 if (&dev->list == &p2p->devices)
3643 dev = NULL;
3644 }
3645
3646 if (dev == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003647 return NULL;
3648
3649 return &dev->info;
3650}
3651
3652
3653int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
3654 char *buf, size_t buflen)
3655{
3656 struct p2p_device *dev;
3657 int res;
3658 char *pos, *end;
3659 struct os_time now;
3660
3661 if (info == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003662 return -1;
3663
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003664 dev = (struct p2p_device *) (((u8 *) info) -
3665 offsetof(struct p2p_device, info));
3666
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003667 pos = buf;
3668 end = buf + buflen;
3669
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003670 os_get_time(&now);
3671 res = os_snprintf(pos, end - pos,
3672 "age=%d\n"
3673 "listen_freq=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003674 "wps_method=%s\n"
3675 "interface_addr=" MACSTR "\n"
3676 "member_in_go_dev=" MACSTR "\n"
3677 "member_in_go_iface=" MACSTR "\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003678 "go_neg_req_sent=%d\n"
3679 "go_state=%s\n"
3680 "dialog_token=%u\n"
3681 "intended_addr=" MACSTR "\n"
3682 "country=%c%c\n"
3683 "oper_freq=%d\n"
3684 "req_config_methods=0x%x\n"
3685 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
3686 "status=%d\n"
3687 "wait_count=%u\n"
3688 "invitation_reqs=%u\n",
3689 (int) (now.sec - dev->last_seen.sec),
3690 dev->listen_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003691 p2p_wps_method_text(dev->wps_method),
3692 MAC2STR(dev->interface_addr),
3693 MAC2STR(dev->member_in_go_dev),
3694 MAC2STR(dev->member_in_go_iface),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003695 dev->go_neg_req_sent,
3696 p2p_go_state_text(dev->go_state),
3697 dev->dialog_token,
3698 MAC2STR(dev->intended_addr),
3699 dev->country[0] ? dev->country[0] : '_',
3700 dev->country[1] ? dev->country[1] : '_',
3701 dev->oper_freq,
3702 dev->req_config_methods,
3703 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
3704 "[PROBE_REQ_ONLY]" : "",
3705 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
3706 dev->flags & P2P_DEV_NOT_YET_READY ?
3707 "[NOT_YET_READY]" : "",
3708 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
3709 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
3710 "",
3711 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
3712 "[PD_PEER_DISPLAY]" : "",
3713 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
3714 "[PD_PEER_KEYPAD]" : "",
3715 dev->flags & P2P_DEV_USER_REJECTED ?
3716 "[USER_REJECTED]" : "",
3717 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
3718 "[PEER_WAITING_RESPONSE]" : "",
3719 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
3720 "[PREFER_PERSISTENT_GROUP]" : "",
3721 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
3722 "[WAIT_GO_NEG_RESPONSE]" : "",
3723 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
3724 "[WAIT_GO_NEG_CONFIRM]" : "",
3725 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
3726 "[GROUP_CLIENT_ONLY]" : "",
3727 dev->flags & P2P_DEV_FORCE_FREQ ?
3728 "[FORCE_FREQ]" : "",
3729 dev->flags & P2P_DEV_PD_FOR_JOIN ?
3730 "[PD_FOR_JOIN]" : "",
3731 dev->status,
3732 dev->wait_count,
3733 dev->invitation_reqs);
3734 if (res < 0 || res >= end - pos)
3735 return pos - buf;
3736 pos += res;
3737
3738 if (dev->ext_listen_period) {
3739 res = os_snprintf(pos, end - pos,
3740 "ext_listen_period=%u\n"
3741 "ext_listen_interval=%u\n",
3742 dev->ext_listen_period,
3743 dev->ext_listen_interval);
3744 if (res < 0 || res >= end - pos)
3745 return pos - buf;
3746 pos += res;
3747 }
3748
3749 if (dev->oper_ssid_len) {
3750 res = os_snprintf(pos, end - pos,
3751 "oper_ssid=%s\n",
3752 wpa_ssid_txt(dev->oper_ssid,
3753 dev->oper_ssid_len));
3754 if (res < 0 || res >= end - pos)
3755 return pos - buf;
3756 pos += res;
3757 }
3758
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003759#ifdef CONFIG_WIFI_DISPLAY
3760 if (dev->info.wfd_subelems) {
3761 res = os_snprintf(pos, end - pos, "wfd_subelems=");
3762 if (res < 0 || res >= end - pos)
3763 return pos - buf;
3764 pos += res;
3765
3766 pos += wpa_snprintf_hex(pos, end - pos,
3767 wpabuf_head(dev->info.wfd_subelems),
3768 wpabuf_len(dev->info.wfd_subelems));
3769
3770 res = os_snprintf(pos, end - pos, "\n");
3771 if (res < 0 || res >= end - pos)
3772 return pos - buf;
3773 pos += res;
3774 }
3775#endif /* CONFIG_WIFI_DISPLAY */
3776
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003777 return pos - buf;
3778}
3779
3780
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003781int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
3782{
3783 return p2p_get_device(p2p, addr) != NULL;
3784}
3785
3786
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003787void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
3788{
3789 if (enabled) {
3790 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3791 "discoverability enabled");
3792 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3793 } else {
3794 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3795 "discoverability disabled");
3796 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3797 }
3798}
3799
3800
3801static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
3802 u32 duration2, u32 interval2)
3803{
3804 struct wpabuf *req;
3805 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
3806 u8 *len;
3807
3808 req = wpabuf_alloc(100);
3809 if (req == NULL)
3810 return NULL;
3811
3812 if (duration1 || interval1) {
3813 os_memset(&desc1, 0, sizeof(desc1));
3814 desc1.count_type = 1;
3815 desc1.duration = duration1;
3816 desc1.interval = interval1;
3817 ptr1 = &desc1;
3818
3819 if (duration2 || interval2) {
3820 os_memset(&desc2, 0, sizeof(desc2));
3821 desc2.count_type = 2;
3822 desc2.duration = duration2;
3823 desc2.interval = interval2;
3824 ptr2 = &desc2;
3825 }
3826 }
3827
3828 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
3829 len = p2p_buf_add_ie_hdr(req);
3830 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
3831 p2p_buf_update_ie_hdr(req, len);
3832
3833 return req;
3834}
3835
3836
3837int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
3838 const u8 *own_interface_addr, unsigned int freq,
3839 u32 duration1, u32 interval1, u32 duration2,
3840 u32 interval2)
3841{
3842 struct wpabuf *req;
3843
3844 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
3845 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
3846 "int1=%u dur2=%u int2=%u",
3847 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
3848 freq, duration1, interval1, duration2, interval2);
3849
3850 req = p2p_build_presence_req(duration1, interval1, duration2,
3851 interval2);
3852 if (req == NULL)
3853 return -1;
3854
3855 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3856 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3857 go_interface_addr,
3858 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
3859 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3860 "P2P: Failed to send Action frame");
3861 }
3862 wpabuf_free(req);
3863
3864 return 0;
3865}
3866
3867
3868static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3869 size_t noa_len, u8 dialog_token)
3870{
3871 struct wpabuf *resp;
3872 u8 *len;
3873
3874 resp = wpabuf_alloc(100 + noa_len);
3875 if (resp == NULL)
3876 return NULL;
3877
3878 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3879 len = p2p_buf_add_ie_hdr(resp);
3880 p2p_buf_add_status(resp, status);
3881 if (noa) {
3882 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3883 wpabuf_put_le16(resp, noa_len);
3884 wpabuf_put_data(resp, noa, noa_len);
3885 } else
3886 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3887 p2p_buf_update_ie_hdr(resp, len);
3888
3889 return resp;
3890}
3891
3892
3893static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3894 const u8 *sa, const u8 *data, size_t len,
3895 int rx_freq)
3896{
3897 struct p2p_message msg;
3898 u8 status;
3899 struct wpabuf *resp;
3900 size_t g;
3901 struct p2p_group *group = NULL;
3902 int parsed = 0;
3903 u8 noa[50];
3904 int noa_len;
3905
3906 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3907 "P2P: Received P2P Action - P2P Presence Request");
3908
3909 for (g = 0; g < p2p->num_groups; g++) {
3910 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3911 ETH_ALEN) == 0) {
3912 group = p2p->groups[g];
3913 break;
3914 }
3915 }
3916 if (group == NULL) {
3917 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3918 "P2P: Ignore P2P Presence Request for unknown group "
3919 MACSTR, MAC2STR(da));
3920 return;
3921 }
3922
3923 if (p2p_parse(data, len, &msg) < 0) {
3924 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3925 "P2P: Failed to parse P2P Presence Request");
3926 status = P2P_SC_FAIL_INVALID_PARAMS;
3927 goto fail;
3928 }
3929 parsed = 1;
3930
3931 if (msg.noa == NULL) {
3932 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3933 "P2P: No NoA attribute in P2P Presence Request");
3934 status = P2P_SC_FAIL_INVALID_PARAMS;
3935 goto fail;
3936 }
3937
3938 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3939
3940fail:
3941 if (p2p->cfg->get_noa)
3942 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3943 sizeof(noa));
3944 else
3945 noa_len = -1;
3946 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3947 noa_len > 0 ? noa_len : 0,
3948 msg.dialog_token);
3949 if (parsed)
3950 p2p_parse_free(&msg);
3951 if (resp == NULL)
3952 return;
3953
3954 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3955 if (p2p_send_action(p2p, rx_freq, sa, da, da,
3956 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
3957 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3958 "P2P: Failed to send Action frame");
3959 }
3960 wpabuf_free(resp);
3961}
3962
3963
3964static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3965 const u8 *sa, const u8 *data, size_t len)
3966{
3967 struct p2p_message msg;
3968
3969 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3970 "P2P: Received P2P Action - P2P Presence Response");
3971
3972 if (p2p_parse(data, len, &msg) < 0) {
3973 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3974 "P2P: Failed to parse P2P Presence Response");
3975 return;
3976 }
3977
3978 if (msg.status == NULL || msg.noa == NULL) {
3979 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3980 "P2P: No Status or NoA attribute in P2P Presence "
3981 "Response");
3982 p2p_parse_free(&msg);
3983 return;
3984 }
3985
3986 if (*msg.status) {
3987 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3988 "P2P: P2P Presence Request was rejected: status %u",
3989 *msg.status);
3990 p2p_parse_free(&msg);
3991 return;
3992 }
3993
3994 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3995 "P2P: P2P Presence Request was accepted");
3996 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
3997 msg.noa, msg.noa_len);
3998 /* TODO: process NoA */
3999 p2p_parse_free(&msg);
4000}
4001
4002
4003static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4004{
4005 struct p2p_data *p2p = eloop_ctx;
4006
4007 if (p2p->ext_listen_interval) {
4008 /* Schedule next extended listen timeout */
4009 eloop_register_timeout(p2p->ext_listen_interval_sec,
4010 p2p->ext_listen_interval_usec,
4011 p2p_ext_listen_timeout, p2p, NULL);
4012 }
4013
4014 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
4015 /*
4016 * This should not really happen, but it looks like the Listen
4017 * command may fail is something else (e.g., a scan) was
4018 * running at an inconvenient time. As a workaround, allow new
4019 * Extended Listen operation to be started.
4020 */
4021 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
4022 "Extended Listen operation had not been completed - "
4023 "try again");
4024 p2p->ext_listen_only = 0;
4025 p2p_set_state(p2p, P2P_IDLE);
4026 }
4027
4028 if (p2p->state != P2P_IDLE) {
4029 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
4030 "Listen timeout in active state (%s)",
4031 p2p_state_txt(p2p->state));
4032 return;
4033 }
4034
4035 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
4036 p2p->ext_listen_only = 1;
4037 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
4038 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
4039 "Listen state for Extended Listen Timing");
4040 p2p->ext_listen_only = 0;
4041 }
4042}
4043
4044
4045int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
4046 unsigned int interval)
4047{
4048 if (period > 65535 || interval > 65535 || period > interval ||
4049 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
4050 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
4051 "P2P: Invalid Extended Listen Timing request: "
4052 "period=%u interval=%u", period, interval);
4053 return -1;
4054 }
4055
4056 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
4057
4058 if (interval == 0) {
4059 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
4060 "P2P: Disabling Extended Listen Timing");
4061 p2p->ext_listen_period = 0;
4062 p2p->ext_listen_interval = 0;
4063 return 0;
4064 }
4065
4066 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
4067 "P2P: Enabling Extended Listen Timing: period %u msec, "
4068 "interval %u msec", period, interval);
4069 p2p->ext_listen_period = period;
4070 p2p->ext_listen_interval = interval;
4071 p2p->ext_listen_interval_sec = interval / 1000;
4072 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
4073
4074 eloop_register_timeout(p2p->ext_listen_interval_sec,
4075 p2p->ext_listen_interval_usec,
4076 p2p_ext_listen_timeout, p2p, NULL);
4077
4078 return 0;
4079}
4080
4081
4082void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4083 const u8 *ie, size_t ie_len)
4084{
4085 struct p2p_message msg;
4086
4087 if (bssid == NULL || ie == NULL)
4088 return;
4089
4090 os_memset(&msg, 0, sizeof(msg));
4091 if (p2p_parse_ies(ie, ie_len, &msg))
4092 return;
4093 if (msg.minor_reason_code == NULL)
4094 return;
4095
4096 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
4097 "P2P: Deauthentication notification BSSID " MACSTR
4098 " reason_code=%u minor_reason_code=%u",
4099 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4100
4101 p2p_parse_free(&msg);
4102}
4103
4104
4105void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4106 const u8 *ie, size_t ie_len)
4107{
4108 struct p2p_message msg;
4109
4110 if (bssid == NULL || ie == NULL)
4111 return;
4112
4113 os_memset(&msg, 0, sizeof(msg));
4114 if (p2p_parse_ies(ie, ie_len, &msg))
4115 return;
4116 if (msg.minor_reason_code == NULL)
4117 return;
4118
4119 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
4120 "P2P: Disassociation notification BSSID " MACSTR
4121 " reason_code=%u minor_reason_code=%u",
4122 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4123
4124 p2p_parse_free(&msg);
4125}
4126
4127
4128void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
4129{
4130 if (enabled) {
4131 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
4132 "Device operations enabled");
4133 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
4134 } else {
4135 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
4136 "Device operations disabled");
4137 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
4138 }
4139}
4140
4141
4142int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
4143{
4144 if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
4145 return -1;
4146
4147 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
4148 "reg_class %u channel %u", reg_class, channel);
4149 p2p->cfg->reg_class = reg_class;
4150 p2p->cfg->channel = channel;
4151
4152 return 0;
4153}
4154
4155
4156int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
4157{
4158 wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
4159 if (postfix == NULL) {
4160 p2p->cfg->ssid_postfix_len = 0;
4161 return 0;
4162 }
4163 if (len > sizeof(p2p->cfg->ssid_postfix))
4164 return -1;
4165 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4166 p2p->cfg->ssid_postfix_len = len;
4167 return 0;
4168}
4169
4170
Jouni Malinen75ecf522011-06-27 15:19:46 -07004171int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4172 int cfg_op_channel)
4173{
4174 if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
4175 < 0)
4176 return -1;
4177
4178 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
4179 "reg_class %u channel %u", op_reg_class, op_channel);
4180 p2p->cfg->op_reg_class = op_reg_class;
4181 p2p->cfg->op_channel = op_channel;
4182 p2p->cfg->cfg_op_channel = cfg_op_channel;
4183 return 0;
4184}
4185
4186
Dmitry Shmidt04949592012-07-19 12:16:46 -07004187int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4188 const struct p2p_channel *pref_chan)
4189{
4190 struct p2p_channel *n;
4191
4192 if (pref_chan) {
4193 n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
4194 if (n == NULL)
4195 return -1;
4196 os_memcpy(n, pref_chan,
4197 num_pref_chan * sizeof(struct p2p_channel));
4198 } else
4199 n = NULL;
4200
4201 os_free(p2p->cfg->pref_chan);
4202 p2p->cfg->pref_chan = n;
4203 p2p->cfg->num_pref_chan = num_pref_chan;
4204
4205 return 0;
4206}
4207
4208
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004209int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4210 u8 *iface_addr)
4211{
4212 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4213 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4214 return -1;
4215 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4216 return 0;
4217}
4218
4219
4220int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4221 u8 *dev_addr)
4222{
4223 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4224 if (dev == NULL)
4225 return -1;
4226 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4227 return 0;
4228}
4229
4230
4231void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4232{
4233 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4234 if (is_zero_ether_addr(p2p->peer_filter))
4235 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
4236 "filter");
4237 else
4238 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
4239 "filter for " MACSTR, MAC2STR(p2p->peer_filter));
4240}
4241
4242
4243void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4244{
4245 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
4246 enabled ? "enabled" : "disabled");
4247 if (p2p->cross_connect == enabled)
4248 return;
4249 p2p->cross_connect = enabled;
4250 /* TODO: may need to tear down any action group where we are GO(?) */
4251}
4252
4253
4254int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4255{
4256 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4257 if (dev == NULL)
4258 return -1;
4259 if (dev->oper_freq <= 0)
4260 return -1;
4261 return dev->oper_freq;
4262}
4263
4264
4265void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4266{
4267 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
4268 enabled ? "enabled" : "disabled");
4269 p2p->cfg->p2p_intra_bss = enabled;
4270}
4271
4272
4273void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
4274{
4275 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
4276 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
4277}
4278
4279
4280int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4281 const u8 *src, const u8 *bssid, const u8 *buf,
4282 size_t len, unsigned int wait_time)
4283{
4284 if (p2p->p2p_scan_running) {
4285 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
4286 "frame TX until p2p_scan completes");
4287 if (p2p->after_scan_tx) {
4288 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
4289 "previous pending Action frame TX");
4290 os_free(p2p->after_scan_tx);
4291 }
4292 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
4293 len);
4294 if (p2p->after_scan_tx == NULL)
4295 return -1;
4296 p2p->after_scan_tx->freq = freq;
4297 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
4298 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
4299 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
4300 p2p->after_scan_tx->len = len;
4301 p2p->after_scan_tx->wait_time = wait_time;
4302 os_memcpy(p2p->after_scan_tx + 1, buf, len);
4303 return 0;
4304 }
4305
4306 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4307 buf, len, wait_time);
4308}
4309
4310
4311void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4312 int freq_overall)
4313{
4314 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
4315 " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall);
4316 p2p->best_freq_24 = freq_24;
4317 p2p->best_freq_5 = freq_5;
4318 p2p->best_freq_overall = freq_overall;
4319}
4320
4321
4322const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4323{
4324 if (p2p == NULL || p2p->go_neg_peer == NULL)
4325 return NULL;
4326 return p2p->go_neg_peer->info.p2p_device_addr;
4327}
4328
4329
4330const struct p2p_peer_info *
4331p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4332{
4333 struct p2p_device *dev;
4334
4335 if (addr) {
4336 dev = p2p_get_device(p2p, addr);
4337 if (!dev)
4338 return NULL;
4339
4340 if (!next) {
4341 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
4342 return NULL;
4343
4344 return &dev->info;
4345 } else {
4346 do {
4347 dev = dl_list_first(&dev->list,
4348 struct p2p_device,
4349 list);
4350 if (&dev->list == &p2p->devices)
4351 return NULL;
4352 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
4353 }
4354 } else {
4355 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4356 if (!dev)
4357 return NULL;
4358 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
4359 dev = dl_list_first(&dev->list,
4360 struct p2p_device,
4361 list);
4362 if (&dev->list == &p2p->devices)
4363 return NULL;
4364 }
4365 }
4366
4367 return &dev->info;
4368}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004369
4370#ifdef ANDROID_P2P
4371int p2p_search_in_progress(struct p2p_data *p2p)
4372{
4373 if (p2p == NULL)
4374 return 0;
4375
4376 return p2p->state == P2P_SEARCH;
4377}
4378#endif
4379
4380int p2p_in_progress(struct p2p_data *p2p)
4381{
4382 if (p2p == NULL)
4383 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004384 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SEARCH_WHEN_READY ||
4385 p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY)
4386 return 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004387 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
4388}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004389
4390
4391void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
4392 u8 client_timeout)
4393{
4394 if (p2p) {
4395 p2p->go_timeout = go_timeout;
4396 p2p->client_timeout = client_timeout;
4397 }
4398}
4399
4400
4401void p2p_increase_search_delay(struct p2p_data *p2p, unsigned int delay)
4402{
4403 if (p2p && p2p->search_delay < delay)
4404 p2p->search_delay = delay;
4405}
4406
4407
4408#ifdef CONFIG_WIFI_DISPLAY
4409
4410static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
4411{
4412 size_t g;
4413 struct p2p_group *group;
4414
4415 for (g = 0; g < p2p->num_groups; g++) {
4416 group = p2p->groups[g];
4417 p2p_group_update_ies(group);
4418 }
4419}
4420
4421
4422int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
4423{
4424 wpabuf_free(p2p->wfd_ie_beacon);
4425 p2p->wfd_ie_beacon = ie;
4426 p2p_update_wfd_ie_groups(p2p);
4427 return 0;
4428}
4429
4430
4431int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
4432{
4433 wpabuf_free(p2p->wfd_ie_probe_req);
4434 p2p->wfd_ie_probe_req = ie;
4435 return 0;
4436}
4437
4438
4439int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
4440{
4441 wpabuf_free(p2p->wfd_ie_probe_resp);
4442 p2p->wfd_ie_probe_resp = ie;
4443 p2p_update_wfd_ie_groups(p2p);
4444 return 0;
4445}
4446
4447
4448int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
4449{
4450 wpabuf_free(p2p->wfd_ie_assoc_req);
4451 p2p->wfd_ie_assoc_req = ie;
4452 return 0;
4453}
4454
4455
4456int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
4457{
4458 wpabuf_free(p2p->wfd_ie_invitation);
4459 p2p->wfd_ie_invitation = ie;
4460 return 0;
4461}
4462
4463
4464int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
4465{
4466 wpabuf_free(p2p->wfd_ie_prov_disc_req);
4467 p2p->wfd_ie_prov_disc_req = ie;
4468 return 0;
4469}
4470
4471
4472int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
4473{
4474 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
4475 p2p->wfd_ie_prov_disc_resp = ie;
4476 return 0;
4477}
4478
4479
4480int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
4481{
4482 wpabuf_free(p2p->wfd_ie_go_neg);
4483 p2p->wfd_ie_go_neg = ie;
4484 return 0;
4485}
4486
4487
4488int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
4489{
4490 wpabuf_free(p2p->wfd_dev_info);
4491 if (elem) {
4492 p2p->wfd_dev_info = wpabuf_dup(elem);
4493 if (p2p->wfd_dev_info == NULL)
4494 return -1;
4495 } else
4496 p2p->wfd_dev_info = NULL;
4497
4498 return 0;
4499}
4500
4501
4502int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
4503{
4504 wpabuf_free(p2p->wfd_assoc_bssid);
4505 if (elem) {
4506 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
4507 if (p2p->wfd_assoc_bssid == NULL)
4508 return -1;
4509 } else
4510 p2p->wfd_assoc_bssid = NULL;
4511
4512 return 0;
4513}
4514
4515
4516int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
4517 const struct wpabuf *elem)
4518{
4519 wpabuf_free(p2p->wfd_coupled_sink_info);
4520 if (elem) {
4521 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
4522 if (p2p->wfd_coupled_sink_info == NULL)
4523 return -1;
4524 } else
4525 p2p->wfd_coupled_sink_info = NULL;
4526
4527 return 0;
4528}
4529
4530#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004531
4532
4533int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
4534 int max_disc_tu)
4535{
4536 if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
4537 return -1;
4538
4539 p2p->min_disc_int = min_disc_int;
4540 p2p->max_disc_int = max_disc_int;
4541 p2p->max_disc_tu = max_disc_tu;
4542 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set discoverable interval: "
4543 "min=%d max=%d max_tu=%d", min_disc_int, max_disc_int,
4544 max_disc_tu);
4545
4546 return 0;
4547}