blob: 6de50f2362d4b35147399900fc899ed383728f53 [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 Shmidt1f69aa52012-01-24 16:10:04 -0800243 if (p2p->go_neg_peer)
244 p2p->go_neg_peer->wps_method = WPS_NOT_READY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700245 p2p->go_neg_peer = NULL;
246
247 os_memset(&res, 0, sizeof(res));
248 res.status = status;
249 if (peer) {
250 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr,
251 ETH_ALEN);
252 os_memcpy(res.peer_interface_addr, peer->intended_addr,
253 ETH_ALEN);
254 }
255 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
256}
257
258
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800259static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700260{
261 unsigned int r, tu;
262 int freq;
263 struct wpabuf *ies;
264
265 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
266 "P2P: Starting short listen state (state=%s)",
267 p2p_state_txt(p2p->state));
268
269 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
270 p2p->cfg->channel);
271 if (freq < 0) {
272 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
273 "P2P: Unknown regulatory class/channel");
274 return;
275 }
276
277 os_get_random((u8 *) &r, sizeof(r));
278 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
279 p2p->min_disc_int) * 100;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800280 if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu)
281 tu = p2p->max_disc_tu;
282 if (!dev_disc && tu < 100)
283 tu = 100; /* Need to wait in non-device discovery use cases */
284 if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen)
285 tu = p2p->cfg->max_listen * 1000 / 1024;
286
287 if (tu == 0) {
288 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip listen state "
289 "since duration was 0 TU");
290 p2p_set_timeout(p2p, 0, 0);
291 return;
292 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293
294 p2p->pending_listen_freq = freq;
295 p2p->pending_listen_sec = 0;
296 p2p->pending_listen_usec = 1024 * tu;
297
298 ies = p2p_build_probe_resp_ies(p2p);
299 if (ies == NULL)
300 return;
301
302 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
303 ies) < 0) {
304 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
305 "P2P: Failed to start listen mode");
306 p2p->pending_listen_freq = 0;
307 }
308 wpabuf_free(ies);
309}
310
311
312int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
313{
314 int freq;
315 struct wpabuf *ies;
316
317 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
318 "P2P: Going to listen(only) state");
319
320 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
321 p2p->cfg->channel);
322 if (freq < 0) {
323 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
324 "P2P: Unknown regulatory class/channel");
325 return -1;
326 }
327
328 p2p->pending_listen_freq = freq;
329 p2p->pending_listen_sec = timeout / 1000;
330 p2p->pending_listen_usec = (timeout % 1000) * 1000;
331
332 if (p2p->p2p_scan_running) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700333 if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800334 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
335 "P2P: p2p_scan running - connect is already "
336 "pending - skip listen");
337 return 0;
338 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700339 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
340 "P2P: p2p_scan running - delay start of listen state");
341 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
342 return 0;
343 }
344
345 ies = p2p_build_probe_resp_ies(p2p);
346 if (ies == NULL)
347 return -1;
348
349 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
350 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
351 "P2P: Failed to start listen mode");
352 p2p->pending_listen_freq = 0;
353 wpabuf_free(ies);
354 return -1;
355 }
356 wpabuf_free(ies);
357
358 p2p_set_state(p2p, P2P_LISTEN_ONLY);
359
360 return 0;
361}
362
363
364static void p2p_device_clear_reported(struct p2p_data *p2p)
365{
366 struct p2p_device *dev;
367 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
368 dev->flags &= ~P2P_DEV_REPORTED;
369}
370
371
372/**
373 * p2p_get_device - Fetch a peer entry
374 * @p2p: P2P module context from p2p_init()
375 * @addr: P2P Device Address of the peer
376 * Returns: Pointer to the device entry or %NULL if not found
377 */
378struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
379{
380 struct p2p_device *dev;
381 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
382 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
383 return dev;
384 }
385 return NULL;
386}
387
388
389/**
390 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
391 * @p2p: P2P module context from p2p_init()
392 * @addr: P2P Interface Address of the peer
393 * Returns: Pointer to the device entry or %NULL if not found
394 */
395struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
396 const u8 *addr)
397{
398 struct p2p_device *dev;
399 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
400 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
401 return dev;
402 }
403 return NULL;
404}
405
406
407/**
408 * p2p_create_device - Create a peer entry
409 * @p2p: P2P module context from p2p_init()
410 * @addr: P2P Device Address of the peer
411 * Returns: Pointer to the device entry or %NULL on failure
412 *
413 * If there is already an entry for the peer, it will be returned instead of
414 * creating a new one.
415 */
416static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
417 const u8 *addr)
418{
419 struct p2p_device *dev, *oldest = NULL;
420 size_t count = 0;
421
422 dev = p2p_get_device(p2p, addr);
423 if (dev)
424 return dev;
425
426 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
427 count++;
428 if (oldest == NULL ||
429 os_time_before(&dev->last_seen, &oldest->last_seen))
430 oldest = dev;
431 }
432 if (count + 1 > p2p->cfg->max_peers && oldest) {
433 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
434 "P2P: Remove oldest peer entry to make room for a new "
435 "peer");
Irfan Sherifff44b9c42012-06-13 11:09:23 -0700436#ifdef ANDROID_P2P
437 /* SD_FAIR_POLICY: Update the current sd_dev_list pointer to next device */
438 if(&oldest->list == p2p->sd_dev_list)
439 p2p->sd_dev_list = oldest->list.next;
440#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700441 dl_list_del(&oldest->list);
442 p2p_device_free(p2p, oldest);
443 }
444
445 dev = os_zalloc(sizeof(*dev));
446 if (dev == NULL)
447 return NULL;
448 dl_list_add(&p2p->devices, &dev->list);
449 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
450
451 return dev;
452}
453
454
455static void p2p_copy_client_info(struct p2p_device *dev,
456 struct p2p_client_info *cli)
457{
458 os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
459 dev->info.device_name[cli->dev_name_len] = '\0';
460 dev->info.dev_capab = cli->dev_capab;
461 dev->info.config_methods = cli->config_methods;
462 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
463 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
464 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
465 dev->info.wps_sec_dev_type_list_len);
466}
467
468
469static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
470 const u8 *go_interface_addr, int freq,
471 const u8 *gi, size_t gi_len)
472{
473 struct p2p_group_info info;
474 size_t c;
475 struct p2p_device *dev;
476
477 if (gi == NULL)
478 return 0;
479
480 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
481 return -1;
482
483 /*
484 * Clear old data for this group; if the devices are still in the
485 * group, the information will be restored in the loop following this.
486 */
487 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800488 if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700489 ETH_ALEN) == 0) {
490 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
491 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
492 }
493 }
494
495 for (c = 0; c < info.num_clients; c++) {
496 struct p2p_client_info *cli = &info.client[c];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800497 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
498 ETH_ALEN) == 0)
499 continue; /* ignore our own entry */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700500 dev = p2p_get_device(p2p, cli->p2p_device_addr);
501 if (dev) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700502 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
Dmitry Shmidt04949592012-07-19 12:16:46 -0700503 P2P_DEV_PROBE_REQ_ONLY)) {
504 /*
505 * Update information since we have not
506 * received this directly from the client.
507 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700508 p2p_copy_client_info(dev, cli);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700509 } else {
510 /*
511 * Need to update P2P Client Discoverability
512 * flag since it is valid only in P2P Group
513 * Info attribute.
514 */
515 dev->info.dev_capab &=
516 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
517 dev->info.dev_capab |=
518 cli->dev_capab &
519 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
520 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700521 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
522 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
523 }
524 } else {
525 dev = p2p_create_device(p2p, cli->p2p_device_addr);
526 if (dev == NULL)
527 continue;
528 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
529 p2p_copy_client_info(dev, cli);
530 dev->oper_freq = freq;
531 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
532 dev->info.p2p_device_addr,
533 &dev->info, 1);
534 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
535 }
536
537 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
538 ETH_ALEN);
539 os_get_time(&dev->last_seen);
540 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
541 os_memcpy(dev->member_in_go_iface, go_interface_addr,
542 ETH_ALEN);
543 }
544
545 return 0;
546}
547
548
549static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
550 const struct p2p_message *msg)
551{
552 os_memcpy(dev->info.device_name, msg->device_name,
553 sizeof(dev->info.device_name));
554
555 if (msg->manufacturer &&
556 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
557 os_memset(dev->info.manufacturer, 0,
558 sizeof(dev->info.manufacturer));
559 os_memcpy(dev->info.manufacturer, msg->manufacturer,
560 msg->manufacturer_len);
561 }
562
563 if (msg->model_name &&
564 msg->model_name_len < sizeof(dev->info.model_name)) {
565 os_memset(dev->info.model_name, 0,
566 sizeof(dev->info.model_name));
567 os_memcpy(dev->info.model_name, msg->model_name,
568 msg->model_name_len);
569 }
570
571 if (msg->model_number &&
572 msg->model_number_len < sizeof(dev->info.model_number)) {
573 os_memset(dev->info.model_number, 0,
574 sizeof(dev->info.model_number));
575 os_memcpy(dev->info.model_number, msg->model_number,
576 msg->model_number_len);
577 }
578
579 if (msg->serial_number &&
580 msg->serial_number_len < sizeof(dev->info.serial_number)) {
581 os_memset(dev->info.serial_number, 0,
582 sizeof(dev->info.serial_number));
583 os_memcpy(dev->info.serial_number, msg->serial_number,
584 msg->serial_number_len);
585 }
586
587 if (msg->pri_dev_type)
588 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
589 sizeof(dev->info.pri_dev_type));
590 else if (msg->wps_pri_dev_type)
591 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
592 sizeof(dev->info.pri_dev_type));
593
594 if (msg->wps_sec_dev_type_list) {
595 os_memcpy(dev->info.wps_sec_dev_type_list,
596 msg->wps_sec_dev_type_list,
597 msg->wps_sec_dev_type_list_len);
598 dev->info.wps_sec_dev_type_list_len =
599 msg->wps_sec_dev_type_list_len;
600 }
601
602 if (msg->capability) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700603 /*
604 * P2P Client Discoverability bit is reserved in all frames
605 * that use this function, so do not change its value here.
606 */
607 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
608 dev->info.dev_capab |= msg->capability[0] &
609 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700610 dev->info.group_capab = msg->capability[1];
611 }
612
613 if (msg->ext_listen_timing) {
614 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
615 dev->ext_listen_interval =
616 WPA_GET_LE16(msg->ext_listen_timing + 2);
617 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700618
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700619 if (!probe_req) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800620 u16 new_config_methods;
621 new_config_methods = msg->config_methods ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700622 msg->config_methods : msg->wps_config_methods;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800623 if (new_config_methods &&
624 dev->info.config_methods != new_config_methods) {
625 wpa_printf(MSG_DEBUG, "P2P: Update peer " MACSTR
626 " config_methods 0x%x -> 0x%x",
627 MAC2STR(dev->info.p2p_device_addr),
628 dev->info.config_methods,
629 new_config_methods);
630 dev->info.config_methods = new_config_methods;
631 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700632 }
633}
634
635
636/**
Dmitry Shmidt04949592012-07-19 12:16:46 -0700637 * p2p_add_device - Add peer entries based on scan results or P2P frames
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700638 * @p2p: P2P module context from p2p_init()
639 * @addr: Source address of Beacon or Probe Response frame (may be either
640 * P2P Device Address or P2P Interface Address)
641 * @level: Signal level (signal strength of the received frame from the peer)
642 * @freq: Frequency on which the Beacon or Probe Response frame was received
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800643 * @rx_time: Time when the result was received
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700644 * @ies: IEs from the Beacon or Probe Response frame
645 * @ies_len: Length of ies buffer in octets
Dmitry Shmidt04949592012-07-19 12:16:46 -0700646 * @scan_res: Whether this was based on scan results
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700647 * Returns: 0 on success, -1 on failure
648 *
649 * If the scan result is for a GO, the clients in the group will also be added
650 * to the peer table. This function can also be used with some other frames
651 * like Provision Discovery Request that contains P2P Capability and P2P Device
652 * Info attributes.
653 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800654int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800655 struct os_time *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800656 size_t ies_len, int scan_res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700657{
658 struct p2p_device *dev;
659 struct p2p_message msg;
660 const u8 *p2p_dev_addr;
661 int i;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800662 struct os_time time_now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700663
664 os_memset(&msg, 0, sizeof(msg));
665 if (p2p_parse_ies(ies, ies_len, &msg)) {
666 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
667 "P2P: Failed to parse P2P IE for a device entry");
668 p2p_parse_free(&msg);
669 return -1;
670 }
671
672 if (msg.p2p_device_addr)
673 p2p_dev_addr = msg.p2p_device_addr;
674 else if (msg.device_id)
675 p2p_dev_addr = msg.device_id;
676 else {
677 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
678 "P2P: Ignore scan data without P2P Device Info or "
679 "P2P Device Id");
680 p2p_parse_free(&msg);
681 return -1;
682 }
683
684 if (!is_zero_ether_addr(p2p->peer_filter) &&
685 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
686 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
687 "filter for " MACSTR " due to peer filter",
688 MAC2STR(p2p_dev_addr));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800689 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 return 0;
691 }
692
693 dev = p2p_create_device(p2p, p2p_dev_addr);
694 if (dev == NULL) {
695 p2p_parse_free(&msg);
696 return -1;
697 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800698
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800699 if (rx_time == NULL) {
700 os_get_time(&time_now);
701 rx_time = &time_now;
702 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800703
704 /*
705 * Update the device entry only if the new peer
706 * entry is newer than the one previously stored.
707 */
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800708 if (dev->last_seen.sec > 0 &&
709 os_time_before(rx_time, &dev->last_seen)) {
710 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not update peer "
711 "entry based on old frame (rx_time=%u.%06u "
712 "last_seen=%u.%06u)",
713 (unsigned int) rx_time->sec,
714 (unsigned int) rx_time->usec,
715 (unsigned int) dev->last_seen.sec,
716 (unsigned int) dev->last_seen.usec);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800717 p2p_parse_free(&msg);
718 return -1;
719 }
720
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800721 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_time));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800722
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700723 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
724
725 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
726 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
727 if (msg.ssid &&
728 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
729 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
730 != 0)) {
731 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
732 dev->oper_ssid_len = msg.ssid[1];
733 }
734
735 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
736 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
737 int ds_freq;
738 if (*msg.ds_params == 14)
739 ds_freq = 2484;
740 else
741 ds_freq = 2407 + *msg.ds_params * 5;
742 if (freq != ds_freq) {
743 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
744 "P2P: Update Listen frequency based on DS "
745 "Parameter Set IE: %d -> %d MHz",
746 freq, ds_freq);
747 freq = ds_freq;
748 }
749 }
750
Dmitry Shmidt04949592012-07-19 12:16:46 -0700751 if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700752 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
753 "P2P: Update Listen frequency based on scan "
754 "results (" MACSTR " %d -> %d MHz (DS param %d)",
755 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
756 freq, msg.ds_params ? *msg.ds_params : -1);
757 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700758 if (scan_res) {
759 dev->listen_freq = freq;
760 if (msg.group_info)
761 dev->oper_freq = freq;
762 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700763 dev->info.level = level;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700764
765 p2p_copy_wps_info(dev, 0, &msg);
766
767 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
768 wpabuf_free(dev->info.wps_vendor_ext[i]);
769 dev->info.wps_vendor_ext[i] = NULL;
770 }
771
772 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
773 if (msg.wps_vendor_ext[i] == NULL)
774 break;
775 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
776 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
777 if (dev->info.wps_vendor_ext[i] == NULL)
778 break;
779 }
780
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700781 if (msg.wfd_subelems) {
782 wpabuf_free(dev->info.wfd_subelems);
783 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
784 }
785
Dmitry Shmidt04949592012-07-19 12:16:46 -0700786 if (scan_res) {
787 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
788 msg.group_info, msg.group_info_len);
789 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790
791 p2p_parse_free(&msg);
792
793 if (p2p_pending_sd_req(p2p, dev))
794 dev->flags |= P2P_DEV_SD_SCHEDULE;
795
796 if (dev->flags & P2P_DEV_REPORTED)
797 return 0;
798
799 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800800 "P2P: Peer found with Listen frequency %d MHz "
801 "(rx_time=%u.%06u)", freq, (unsigned int) rx_time->sec,
802 (unsigned int) rx_time->usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700803 if (dev->flags & P2P_DEV_USER_REJECTED) {
804 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
805 "P2P: Do not report rejected device");
806 return 0;
807 }
808
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800809 if (dev->info.config_methods == 0 &&
810 (freq == 2412 || freq == 2437 || freq == 2462)) {
811 /*
812 * If we have only seen a Beacon frame from a GO, we do not yet
813 * know what WPS config methods it supports. Since some
814 * applications use config_methods value from P2P-DEVICE-FOUND
815 * events, postpone reporting this peer until we've fully
816 * discovered its capabilities.
817 *
818 * At least for now, do this only if the peer was detected on
819 * one of the social channels since that peer can be easily be
820 * found again and there are no limitations of having to use
821 * passive scan on this channels, so this can be done through
822 * Probe Response frame that includes the config_methods
823 * information.
824 */
825 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
826 "P2P: Do not report peer " MACSTR " with unknown "
827 "config methods", MAC2STR(addr));
828 return 0;
829 }
830
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700831 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
832 !(dev->flags & P2P_DEV_REPORTED_ONCE));
833 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
834
835 return 0;
836}
837
838
839static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
840{
841 int i;
842
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700843 if (p2p->go_neg_peer == dev) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800844 /*
845 * If GO Negotiation is in progress, report that it has failed.
846 */
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700847 p2p_go_neg_failed(p2p, dev, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700848 p2p->go_neg_peer = NULL;
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700849 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700850 if (p2p->invite_peer == dev)
851 p2p->invite_peer = NULL;
852 if (p2p->sd_peer == dev)
853 p2p->sd_peer = NULL;
854 if (p2p->pending_client_disc_go == dev)
855 p2p->pending_client_disc_go = NULL;
856
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700857 /* dev_lost() device, but only if it was previously dev_found() */
858 if (dev->flags & P2P_DEV_REPORTED_ONCE)
859 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
860 dev->info.p2p_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700861
862 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
863 wpabuf_free(dev->info.wps_vendor_ext[i]);
864 dev->info.wps_vendor_ext[i] = NULL;
865 }
866
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700867 wpabuf_free(dev->info.wfd_subelems);
868
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700869 os_free(dev);
870}
871
872
873static int p2p_get_next_prog_freq(struct p2p_data *p2p)
874{
875 struct p2p_channels *c;
876 struct p2p_reg_class *cla;
877 size_t cl, ch;
878 int found = 0;
879 u8 reg_class;
880 u8 channel;
881 int freq;
882
883 c = &p2p->cfg->channels;
884 for (cl = 0; cl < c->reg_classes; cl++) {
885 cla = &c->reg_class[cl];
886 if (cla->reg_class != p2p->last_prog_scan_class)
887 continue;
888 for (ch = 0; ch < cla->channels; ch++) {
889 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
890 found = 1;
891 break;
892 }
893 }
894 if (found)
895 break;
896 }
897
898 if (!found) {
899 /* Start from beginning */
900 reg_class = c->reg_class[0].reg_class;
901 channel = c->reg_class[0].channel[0];
902 } else {
903 /* Pick the next channel */
904 ch++;
905 if (ch == cla->channels) {
906 cl++;
907 if (cl == c->reg_classes)
908 cl = 0;
909 ch = 0;
910 }
911 reg_class = c->reg_class[cl].reg_class;
912 channel = c->reg_class[cl].channel[ch];
913 }
914
915 freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
916 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
917 "channel: reg_class %u channel %u -> %d MHz",
918 reg_class, channel, freq);
919 p2p->last_prog_scan_class = reg_class;
920 p2p->last_prog_scan_chan = channel;
921
922 if (freq == 2412 || freq == 2437 || freq == 2462)
923 return 0; /* No need to add social channels */
924 return freq;
925}
926
927
928static void p2p_search(struct p2p_data *p2p)
929{
930 int freq = 0;
931 enum p2p_scan_type type;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700932 u16 pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700933 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700934
935 if (p2p->drv_in_listen) {
936 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
937 "in Listen state - wait for it to end before "
938 "continuing");
939 return;
940 }
941 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
942
Dmitry Shmidt04949592012-07-19 12:16:46 -0700943 if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
944 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700945 type = P2P_SCAN_SOCIAL_PLUS_ONE;
946 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
947 "(+ freq %u)", freq);
948 } else {
949 type = P2P_SCAN_SOCIAL;
950 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
951 }
952
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700953 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
954 p2p->num_req_dev_types, p2p->req_dev_types,
955 p2p->find_dev_id, pw_id);
956 if (res < 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700957 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
958 "P2P: Scan request failed");
959 p2p_continue_find(p2p);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700960 } else if (res == 1) {
961 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
962 "p2p_scan at this point - will try again after "
963 "previous scan completes");
964 p2p_set_state(p2p, P2P_CONTINUE_SEARCH_WHEN_READY);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700965 } else {
966 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
967 p2p->p2p_scan_running = 1;
968 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
969 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
970 p2p, NULL);
971 }
972}
973
974
975static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
976{
977 struct p2p_data *p2p = eloop_ctx;
978 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
979 p2p_stop_find(p2p);
980}
981
982
983static int p2p_run_after_scan(struct p2p_data *p2p)
984{
985 struct p2p_device *dev;
986 enum p2p_after_scan op;
987
988 if (p2p->after_scan_tx) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700989 /* TODO: schedule p2p_run_after_scan to be called from TX
990 * status callback(?) */
991 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
992 "Action frame at p2p_scan completion");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800993 p2p->cfg->send_action(p2p->cfg->cb_ctx,
994 p2p->after_scan_tx->freq,
995 p2p->after_scan_tx->dst,
996 p2p->after_scan_tx->src,
997 p2p->after_scan_tx->bssid,
998 (u8 *) (p2p->after_scan_tx + 1),
999 p2p->after_scan_tx->len,
1000 p2p->after_scan_tx->wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001001 os_free(p2p->after_scan_tx);
1002 p2p->after_scan_tx = NULL;
Irfan Sherifff44b9c42012-06-13 11:09:23 -07001003#ifdef ANDROID_P2P
1004 /* For SD frames, there is a scenario, where we can receive a SD request frame during p2p_scan.
1005 * At that moment, we will send the SD response from this context. After sending the SD response,
1006 * we need to continue p2p_find. But if we return 1 from here, p2p_find is going to be stopped.
1007 */
1008 return 0;
1009#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001010 return 1;
Irfan Sherifff44b9c42012-06-13 11:09:23 -07001011#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001012 }
1013
1014 op = p2p->start_after_scan;
1015 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1016 switch (op) {
1017 case P2P_AFTER_SCAN_NOTHING:
1018 break;
1019 case P2P_AFTER_SCAN_LISTEN:
1020 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
1021 "requested Listen state");
1022 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
1023 p2p->pending_listen_usec / 1000);
1024 return 1;
1025 case P2P_AFTER_SCAN_CONNECT:
1026 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
1027 "requested connect with " MACSTR,
1028 MAC2STR(p2p->after_scan_peer));
1029 dev = p2p_get_device(p2p, p2p->after_scan_peer);
1030 if (dev == NULL) {
1031 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
1032 "known anymore");
1033 break;
1034 }
1035 p2p_connect_send(p2p, dev);
1036 return 1;
1037 }
1038
1039 return 0;
1040}
1041
1042
1043static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1044{
1045 struct p2p_data *p2p = eloop_ctx;
1046 int running;
1047 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
1048 "(running=%d)", p2p->p2p_scan_running);
1049 running = p2p->p2p_scan_running;
1050 /* Make sure we recover from missed scan results callback */
1051 p2p->p2p_scan_running = 0;
1052
1053 if (running)
1054 p2p_run_after_scan(p2p);
1055}
1056
1057
1058static void p2p_free_req_dev_types(struct p2p_data *p2p)
1059{
1060 p2p->num_req_dev_types = 0;
1061 os_free(p2p->req_dev_types);
1062 p2p->req_dev_types = NULL;
1063}
1064
1065
1066int p2p_find(struct p2p_data *p2p, unsigned int timeout,
1067 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001068 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001069 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001070{
1071 int res;
1072
1073 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
1074 type);
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001075 os_get_time(&p2p->find_start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001076 if (p2p->p2p_scan_running) {
1077 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
1078 "already running");
1079 }
1080
1081 p2p_free_req_dev_types(p2p);
1082 if (req_dev_types && num_req_dev_types) {
1083 p2p->req_dev_types = os_malloc(num_req_dev_types *
1084 WPS_DEV_TYPE_LEN);
1085 if (p2p->req_dev_types == NULL)
1086 return -1;
1087 os_memcpy(p2p->req_dev_types, req_dev_types,
1088 num_req_dev_types * WPS_DEV_TYPE_LEN);
1089 p2p->num_req_dev_types = num_req_dev_types;
1090 }
1091
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001092 if (dev_id) {
1093 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1094 p2p->find_dev_id = p2p->find_dev_id_buf;
1095 } else
1096 p2p->find_dev_id = NULL;
1097
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001098 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1099 p2p_clear_timeout(p2p);
1100 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1101 p2p->find_type = type;
1102 p2p_device_clear_reported(p2p);
1103 p2p_set_state(p2p, P2P_SEARCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001104 p2p->search_delay = search_delay;
1105 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001106 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001107 p2p->last_p2p_find_timeout = timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001108 if (timeout)
1109 eloop_register_timeout(timeout, 0, p2p_find_timeout,
1110 p2p, NULL);
1111 switch (type) {
1112 case P2P_FIND_START_WITH_FULL:
1113 case P2P_FIND_PROGRESSIVE:
1114 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1115 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001116 p2p->req_dev_types, dev_id,
1117 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001118 break;
1119 case P2P_FIND_ONLY_SOCIAL:
1120 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1121 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001122 p2p->req_dev_types, dev_id,
1123 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 break;
1125 default:
1126 return -1;
1127 }
1128
1129 if (res == 0) {
1130 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
1131 p2p->p2p_scan_running = 1;
1132 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1133 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1134 p2p, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001135 } else if (res == 1) {
1136 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
1137 "p2p_scan at this point - will try again after "
1138 "previous scan completes");
1139 res = 0;
1140 p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
1141 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142 } else {
1143 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
1144 "p2p_scan");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001145 p2p_set_state(p2p, P2P_IDLE);
1146 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001147 }
1148
1149 return res;
1150}
1151
Dmitry Shmidt2fb777c2012-05-02 12:29:53 -07001152#ifdef ANDROID_P2P
1153int p2p_search_pending(struct p2p_data *p2p)
1154{
1155 if(p2p == NULL)
1156 return 0;
1157
1158 if(p2p->state == P2P_SEARCH_WHEN_READY)
1159 return 1;
1160
1161 return 0;
1162}
1163#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001165int p2p_other_scan_completed(struct p2p_data *p2p)
1166{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001167 if (p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY) {
1168 p2p_set_state(p2p, P2P_SEARCH);
1169 p2p_search(p2p);
1170 return 1;
1171 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001172 if (p2p->state != P2P_SEARCH_WHEN_READY)
1173 return 0;
1174 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find "
1175 "now that previous scan was completed");
1176 if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001177 p2p->num_req_dev_types, p2p->req_dev_types,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001178 p2p->find_dev_id, p2p->search_delay) < 0) {
1179 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001180 return 0;
Dmitry Shmidtf8623282013-02-20 14:34:59 -08001181 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001182 return 1;
1183}
1184
1185
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001186void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1187{
1188 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
1189 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1190 p2p_clear_timeout(p2p);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001191 if (p2p->state == P2P_SEARCH ||
1192 p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY ||
1193 p2p->state == P2P_SEARCH_WHEN_READY)
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001194 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001195 p2p_set_state(p2p, P2P_IDLE);
1196 p2p_free_req_dev_types(p2p);
1197 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1198 p2p->go_neg_peer = NULL;
1199 p2p->sd_peer = NULL;
1200 p2p->invite_peer = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001201 p2p_stop_listen_for_freq(p2p, freq);
1202}
1203
1204
1205void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1206{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001207 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
1208 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
1209 "since we are on correct channel for response");
1210 return;
1211 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001212 if (p2p->in_listen) {
1213 p2p->in_listen = 0;
1214 p2p_clear_timeout(p2p);
1215 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001216 if (p2p->drv_in_listen) {
1217 /*
1218 * The driver may not deliver callback to p2p_listen_end()
1219 * when the operation gets canceled, so clear the internal
1220 * variable that is tracking driver state.
1221 */
1222 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear "
1223 "drv_in_listen (%d)", p2p->drv_in_listen);
1224 p2p->drv_in_listen = 0;
1225 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001226 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1227}
1228
1229
1230void p2p_stop_find(struct p2p_data *p2p)
1231{
1232 p2p_stop_find_for_freq(p2p, 0);
1233}
1234
1235
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001236static int p2p_prepare_channel_pref(struct p2p_data *p2p,
1237 unsigned int force_freq,
1238 unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001239{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001240 u8 op_class, op_channel;
1241 unsigned int freq = force_freq ? force_freq : pref_freq;
1242
1243 if (p2p_freq_to_channel(p2p->cfg->country, freq,
1244 &op_class, &op_channel) < 0) {
1245 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1246 "P2P: Unsupported frequency %u MHz", freq);
1247 return -1;
1248 }
1249
1250 if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel)) {
1251 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1252 "P2P: Frequency %u MHz (oper_class %u channel %u) not "
1253 "allowed for P2P", freq, op_class, op_channel);
1254 return -1;
1255 }
1256
1257 p2p->op_reg_class = op_class;
1258 p2p->op_channel = op_channel;
1259
1260 if (force_freq) {
1261 p2p->channels.reg_classes = 1;
1262 p2p->channels.reg_class[0].channels = 1;
1263 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1264 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001265 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001266 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1267 sizeof(struct p2p_channels));
1268 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001269
1270 return 0;
1271}
1272
1273
1274static void p2p_prepare_channel_best(struct p2p_data *p2p)
1275{
1276 u8 op_class, op_channel;
1277
1278 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1279 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
1280 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_overall,
1281 &op_class, &op_channel) == 0) {
1282 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Select best "
1283 "overall channel as operating channel preference");
1284 p2p->op_reg_class = op_class;
1285 p2p->op_channel = op_channel;
1286 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1287 p2p_supported_freq(p2p, p2p->best_freq_5) &&
1288 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_5,
1289 &op_class, &op_channel) == 0) {
1290 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Select best 5 GHz "
1291 "channel as operating channel preference");
1292 p2p->op_reg_class = op_class;
1293 p2p->op_channel = op_channel;
1294 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
1295 p2p_supported_freq(p2p, p2p->best_freq_24) &&
1296 p2p_freq_to_channel(p2p->cfg->country, p2p->best_freq_24,
1297 &op_class, &op_channel) == 0) {
1298 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Select best 2.4 "
1299 "GHz channel as operating channel preference");
1300 p2p->op_reg_class = op_class;
1301 p2p->op_channel = op_channel;
1302 } else {
1303 p2p->op_reg_class = p2p->cfg->op_reg_class;
1304 p2p->op_channel = p2p->cfg->op_channel;
1305 }
1306
1307 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1308 sizeof(struct p2p_channels));
1309}
1310
1311
1312/**
1313 * p2p_prepare_channel - Select operating channel for GO Negotiation
1314 * @p2p: P2P module context from p2p_init()
1315 * @dev: Selected peer device
1316 * @force_freq: Forced frequency in MHz or 0 if not forced
1317 * @pref_freq: Preferred frequency in MHz or 0 if no preference
1318 * Returns: 0 on success, -1 on failure (channel not supported for P2P)
1319 *
1320 * This function is used to do initial operating channel selection for GO
1321 * Negotiation prior to having received peer information. The selected channel
1322 * may be further optimized in p2p_reselect_channel() once the peer information
1323 * is available.
1324 */
1325static int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
1326 unsigned int force_freq, unsigned int pref_freq)
1327{
1328 if (force_freq || pref_freq) {
1329 if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq) < 0)
1330 return -1;
1331 } else {
1332 p2p_prepare_channel_best(p2p);
1333 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001334 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1335 "P2P: Own preference for operation channel: "
1336 "Operating Class %u Channel %u%s",
1337 p2p->op_reg_class, p2p->op_channel,
1338 force_freq ? " (forced)" : "");
1339
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001340 if (force_freq)
1341 dev->flags |= P2P_DEV_FORCE_FREQ;
1342 else
1343 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1344
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001345 return 0;
1346}
1347
1348
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001349static void p2p_set_dev_persistent(struct p2p_device *dev,
1350 int persistent_group)
1351{
1352 switch (persistent_group) {
1353 case 0:
1354 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1355 P2P_DEV_PREFER_PERSISTENT_RECONN);
1356 break;
1357 case 1:
1358 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1359 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1360 break;
1361 case 2:
1362 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1363 P2P_DEV_PREFER_PERSISTENT_RECONN;
1364 break;
1365 }
1366}
1367
1368
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001369int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1370 enum p2p_wps_method wps_method,
1371 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001372 unsigned int force_freq, int persistent_group,
1373 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001374 int pd_before_go_neg, unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001375{
1376 struct p2p_device *dev;
1377
1378 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1379 "P2P: Request to start group negotiation - peer=" MACSTR
1380 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001381 " wps_method=%d persistent_group=%d pd_before_go_neg=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001382 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001383 wps_method, persistent_group, pd_before_go_neg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001384
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001385 dev = p2p_get_device(p2p, peer_addr);
1386 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1387 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1388 "P2P: Cannot connect to unknown P2P Device " MACSTR,
1389 MAC2STR(peer_addr));
1390 return -1;
1391 }
1392
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001393 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq) < 0)
1394 return -1;
1395
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001396 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1397 if (!(dev->info.dev_capab &
1398 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1399 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1400 "P2P: Cannot connect to P2P Device " MACSTR
1401 " that is in a group and is not discoverable",
1402 MAC2STR(peer_addr));
1403 return -1;
1404 }
1405 if (dev->oper_freq <= 0) {
1406 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1407 "P2P: Cannot connect to P2P Device " MACSTR
1408 " with incomplete information",
1409 MAC2STR(peer_addr));
1410 return -1;
1411 }
1412
1413 /*
1414 * First, try to connect directly. If the peer does not
1415 * acknowledge frames, assume it is sleeping and use device
1416 * discoverability via the GO at that point.
1417 */
1418 }
1419
Dmitry Shmidt04949592012-07-19 12:16:46 -07001420 p2p->ssid_set = 0;
1421 if (force_ssid) {
1422 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1423 force_ssid, force_ssid_len);
1424 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1425 p2p->ssid_len = force_ssid_len;
1426 p2p->ssid_set = 1;
1427 }
1428
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001429 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1430 dev->flags &= ~P2P_DEV_USER_REJECTED;
1431 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1432 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001433 if (pd_before_go_neg)
1434 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001435 else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001436 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001437 /*
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001438 * Assign dialog token and tie breaker here to use the same
1439 * values in each retry within the same GO Negotiation exchange.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001440 */
1441 dev->dialog_token++;
1442 if (dev->dialog_token == 0)
1443 dev->dialog_token = 1;
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001444 dev->tie_breaker = p2p->next_tie_breaker;
1445 p2p->next_tie_breaker = !p2p->next_tie_breaker;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001446 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001447 dev->connect_reqs = 0;
1448 dev->go_neg_req_sent = 0;
1449 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001450 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001451 p2p->go_intent = go_intent;
1452 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1453
1454 if (p2p->state != P2P_IDLE)
1455 p2p_stop_find(p2p);
1456
1457 if (p2p->after_scan_tx) {
1458 /*
1459 * We need to drop the pending frame to avoid issues with the
1460 * new GO Negotiation, e.g., when the pending frame was from a
1461 * previous attempt at starting a GO Negotiation.
1462 */
1463 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
1464 "previous pending Action frame TX that was waiting "
1465 "for p2p_scan completion");
1466 os_free(p2p->after_scan_tx);
1467 p2p->after_scan_tx = NULL;
1468 }
1469
1470 dev->wps_method = wps_method;
1471 dev->status = P2P_SC_SUCCESS;
1472
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001473 if (p2p->p2p_scan_running) {
1474 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1475 "P2P: p2p_scan running - delay connect send");
1476 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1477 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1478 return 0;
1479 }
1480 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1481
1482 return p2p_connect_send(p2p, dev);
1483}
1484
1485
1486int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1487 enum p2p_wps_method wps_method,
1488 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001489 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001490 const u8 *force_ssid, size_t force_ssid_len,
1491 unsigned int pref_freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001492{
1493 struct p2p_device *dev;
1494
1495 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1496 "P2P: Request to authorize group negotiation - peer=" MACSTR
1497 " GO Intent=%d Intended Interface Address=" MACSTR
1498 " wps_method=%d persistent_group=%d",
1499 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1500 wps_method, persistent_group);
1501
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001502 dev = p2p_get_device(p2p, peer_addr);
1503 if (dev == NULL) {
1504 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1505 "P2P: Cannot authorize unknown P2P Device " MACSTR,
1506 MAC2STR(peer_addr));
1507 return -1;
1508 }
1509
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001510 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq) < 0)
1511 return -1;
1512
Dmitry Shmidt04949592012-07-19 12:16:46 -07001513 p2p->ssid_set = 0;
1514 if (force_ssid) {
1515 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1516 force_ssid, force_ssid_len);
1517 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1518 p2p->ssid_len = force_ssid_len;
1519 p2p->ssid_set = 1;
1520 }
1521
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001522 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1523 dev->flags &= ~P2P_DEV_USER_REJECTED;
1524 dev->go_neg_req_sent = 0;
1525 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001526 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001527 p2p->go_intent = go_intent;
1528 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1529
1530 dev->wps_method = wps_method;
1531 dev->status = P2P_SC_SUCCESS;
1532
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533 return 0;
1534}
1535
1536
1537void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1538 struct p2p_device *dev, struct p2p_message *msg)
1539{
1540 os_get_time(&dev->last_seen);
1541
1542 p2p_copy_wps_info(dev, 0, msg);
1543
1544 if (msg->listen_channel) {
1545 int freq;
1546 freq = p2p_channel_to_freq((char *) msg->listen_channel,
1547 msg->listen_channel[3],
1548 msg->listen_channel[4]);
1549 if (freq < 0) {
1550 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1551 "P2P: Unknown peer Listen channel: "
1552 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1553 msg->listen_channel[0],
1554 msg->listen_channel[1],
1555 msg->listen_channel[2],
1556 msg->listen_channel[3],
1557 msg->listen_channel[4]);
1558 } else {
1559 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1560 "peer " MACSTR " Listen channel: %u -> %u MHz",
1561 MAC2STR(dev->info.p2p_device_addr),
1562 dev->listen_freq, freq);
1563 dev->listen_freq = freq;
1564 }
1565 }
1566
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001567 if (msg->wfd_subelems) {
1568 wpabuf_free(dev->info.wfd_subelems);
1569 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1570 }
1571
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001572 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1573 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1574 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1575 "P2P: Completed device entry based on data from "
1576 "GO Negotiation Request");
1577 } else {
1578 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1579 "P2P: Created device entry based on GO Neg Req: "
1580 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1581 "listen_freq=%d",
1582 MAC2STR(dev->info.p2p_device_addr),
1583 dev->info.dev_capab, dev->info.group_capab,
1584 dev->info.device_name, dev->listen_freq);
1585 }
1586
1587 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1588
1589 if (dev->flags & P2P_DEV_USER_REJECTED) {
1590 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1591 "P2P: Do not report rejected device");
1592 return;
1593 }
1594
1595 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1596 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1597 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1598}
1599
1600
1601void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1602{
1603 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1604 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1605 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1606 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1607 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1608}
1609
1610
1611int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1612{
1613 p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1614 p2p_random(params->passphrase, 8);
1615 return 0;
1616}
1617
1618
1619void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1620{
1621 struct p2p_go_neg_results res;
1622 int go = peer->go_state == LOCAL_GO;
1623 struct p2p_channels intersection;
1624 int freqs;
1625 size_t i, j;
1626
1627 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1628 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1629 "GO)", MAC2STR(peer->info.p2p_device_addr),
1630 go ? "local end" : "peer");
1631
1632 os_memset(&res, 0, sizeof(res));
1633 res.role_go = go;
1634 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1635 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1636 res.wps_method = peer->wps_method;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001637 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1638 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1639 res.persistent_group = 2;
1640 else
1641 res.persistent_group = 1;
1642 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001643
1644 if (go) {
1645 /* Setup AP mode for WPS provisioning */
1646 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1647 p2p->op_reg_class,
1648 p2p->op_channel);
1649 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1650 res.ssid_len = p2p->ssid_len;
1651 p2p_random(res.passphrase, 8);
1652 } else {
1653 res.freq = peer->oper_freq;
1654 if (p2p->ssid_len) {
1655 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1656 res.ssid_len = p2p->ssid_len;
1657 }
1658 }
1659
1660 p2p_channels_intersect(&p2p->channels, &peer->channels,
1661 &intersection);
1662 freqs = 0;
1663 for (i = 0; i < intersection.reg_classes; i++) {
1664 struct p2p_reg_class *c = &intersection.reg_class[i];
1665 if (freqs + 1 == P2P_MAX_CHANNELS)
1666 break;
1667 for (j = 0; j < c->channels; j++) {
1668 int freq;
1669 if (freqs + 1 == P2P_MAX_CHANNELS)
1670 break;
1671 freq = p2p_channel_to_freq(peer->country, c->reg_class,
1672 c->channel[j]);
1673 if (freq < 0)
1674 continue;
1675 res.freq_list[freqs++] = freq;
1676 }
1677 }
1678
1679 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1680
1681 p2p_clear_timeout(p2p);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001682 p2p->ssid_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001683 peer->go_neg_req_sent = 0;
1684 peer->wps_method = WPS_NOT_READY;
1685
1686 p2p_set_state(p2p, P2P_PROVISIONING);
1687 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1688}
1689
1690
1691static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1692 const u8 *data, size_t len, int rx_freq)
1693{
1694 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1695 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1696 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1697
1698 if (len < 1)
1699 return;
1700
1701 switch (data[0]) {
1702 case P2P_GO_NEG_REQ:
1703 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1704 break;
1705 case P2P_GO_NEG_RESP:
1706 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1707 break;
1708 case P2P_GO_NEG_CONF:
1709 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1710 break;
1711 case P2P_INVITATION_REQ:
1712 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1713 rx_freq);
1714 break;
1715 case P2P_INVITATION_RESP:
1716 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1717 break;
1718 case P2P_PROV_DISC_REQ:
1719 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1720 break;
1721 case P2P_PROV_DISC_RESP:
1722 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1723 break;
1724 case P2P_DEV_DISC_REQ:
1725 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1726 break;
1727 case P2P_DEV_DISC_RESP:
1728 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1729 break;
1730 default:
1731 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1732 "P2P: Unsupported P2P Public Action frame type %d",
1733 data[0]);
1734 break;
1735 }
1736}
1737
1738
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001739static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1740 const u8 *sa, const u8 *bssid, const u8 *data,
1741 size_t len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001742{
1743 if (len < 1)
1744 return;
1745
1746 switch (data[0]) {
1747 case WLAN_PA_VENDOR_SPECIFIC:
1748 data++;
1749 len--;
1750 if (len < 3)
1751 return;
1752 if (WPA_GET_BE24(data) != OUI_WFA)
1753 return;
1754
1755 data += 3;
1756 len -= 3;
1757 if (len < 1)
1758 return;
1759
1760 if (*data != P2P_OUI_TYPE)
1761 return;
1762
1763 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1764 break;
1765 case WLAN_PA_GAS_INITIAL_REQ:
1766 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1767 break;
1768 case WLAN_PA_GAS_INITIAL_RESP:
1769 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1770 break;
1771 case WLAN_PA_GAS_COMEBACK_REQ:
1772 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1773 break;
1774 case WLAN_PA_GAS_COMEBACK_RESP:
1775 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1776 break;
1777 }
1778}
1779
1780
1781void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1782 const u8 *bssid, u8 category,
1783 const u8 *data, size_t len, int freq)
1784{
1785 if (category == WLAN_ACTION_PUBLIC) {
1786 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1787 return;
1788 }
1789
1790 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1791 return;
1792
1793 if (len < 4)
1794 return;
1795
1796 if (WPA_GET_BE24(data) != OUI_WFA)
1797 return;
1798 data += 3;
1799 len -= 3;
1800
1801 if (*data != P2P_OUI_TYPE)
1802 return;
1803 data++;
1804 len--;
1805
1806 /* P2P action frame */
1807 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1808 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1809 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1810
1811 if (len < 1)
1812 return;
1813 switch (data[0]) {
1814 case P2P_NOA:
1815 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1816 "P2P: Received P2P Action - Notice of Absence");
1817 /* TODO */
1818 break;
1819 case P2P_PRESENCE_REQ:
1820 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1821 break;
1822 case P2P_PRESENCE_RESP:
1823 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1824 break;
1825 case P2P_GO_DISC_REQ:
1826 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1827 break;
1828 default:
1829 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1830 "P2P: Received P2P Action - unknown type %u", data[0]);
1831 break;
1832 }
1833}
1834
1835
1836static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1837{
1838 struct p2p_data *p2p = eloop_ctx;
1839 if (p2p->go_neg_peer == NULL)
1840 return;
1841 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1842 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1843 p2p_connect_send(p2p, p2p->go_neg_peer);
1844}
1845
1846
1847static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1848{
1849 struct p2p_data *p2p = eloop_ctx;
1850 if (p2p->invite_peer == NULL)
1851 return;
1852 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1853 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1854}
1855
1856
1857static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1858 const u8 *ie, size_t ie_len)
1859{
1860 struct p2p_message msg;
1861 struct p2p_device *dev;
1862
1863 os_memset(&msg, 0, sizeof(msg));
1864 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1865 {
1866 p2p_parse_free(&msg);
1867 return; /* not a P2P probe */
1868 }
1869
1870 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1871 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1872 != 0) {
1873 /* The Probe Request is not part of P2P Device Discovery. It is
1874 * not known whether the source address of the frame is the P2P
1875 * Device Address or P2P Interface Address. Do not add a new
1876 * peer entry based on this frames.
1877 */
1878 p2p_parse_free(&msg);
1879 return;
1880 }
1881
1882 dev = p2p_get_device(p2p, addr);
1883 if (dev) {
1884 if (dev->country[0] == 0 && msg.listen_channel)
1885 os_memcpy(dev->country, msg.listen_channel, 3);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001886 os_get_time(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001887 p2p_parse_free(&msg);
1888 return; /* already known */
1889 }
1890
1891 dev = p2p_create_device(p2p, addr);
1892 if (dev == NULL) {
1893 p2p_parse_free(&msg);
1894 return;
1895 }
1896
1897 os_get_time(&dev->last_seen);
1898 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1899
1900 if (msg.listen_channel) {
1901 os_memcpy(dev->country, msg.listen_channel, 3);
1902 dev->listen_freq = p2p_channel_to_freq(dev->country,
1903 msg.listen_channel[3],
1904 msg.listen_channel[4]);
1905 }
1906
1907 p2p_copy_wps_info(dev, 1, &msg);
1908
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001909 if (msg.wfd_subelems) {
1910 wpabuf_free(dev->info.wfd_subelems);
1911 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
1912 }
1913
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001914 p2p_parse_free(&msg);
1915
1916 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1917 "P2P: Created device entry based on Probe Req: " MACSTR
1918 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1919 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1920 dev->info.group_capab, dev->info.device_name,
1921 dev->listen_freq);
1922}
1923
1924
1925struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1926 const u8 *addr,
1927 struct p2p_message *msg)
1928{
1929 struct p2p_device *dev;
1930
1931 dev = p2p_get_device(p2p, addr);
1932 if (dev) {
1933 os_get_time(&dev->last_seen);
1934 return dev; /* already known */
1935 }
1936
1937 dev = p2p_create_device(p2p, addr);
1938 if (dev == NULL)
1939 return NULL;
1940
1941 p2p_add_dev_info(p2p, addr, dev, msg);
1942
1943 return dev;
1944}
1945
1946
1947static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1948{
1949 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1950 return 1;
1951 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1952 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1953 WPA_GET_BE16(&req_dev_type[6]) == 0)
1954 return 1; /* Category match with wildcard OUI/sub-category */
1955 return 0;
1956}
1957
1958
1959int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1960 size_t num_req_dev_type)
1961{
1962 size_t i;
1963 for (i = 0; i < num_req_dev_type; i++) {
1964 if (dev_type_match(dev_type, req_dev_type[i]))
1965 return 1;
1966 }
1967 return 0;
1968}
1969
1970
1971/**
1972 * p2p_match_dev_type - Match local device type with requested type
1973 * @p2p: P2P module context from p2p_init()
1974 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1975 * Returns: 1 on match, 0 on mismatch
1976 *
1977 * This function can be used to match the Requested Device Type attribute in
1978 * WPS IE with the local device types for deciding whether to reply to a Probe
1979 * Request frame.
1980 */
1981int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1982{
1983 struct wps_parse_attr attr;
1984 size_t i;
1985
1986 if (wps_parse_msg(wps, &attr))
1987 return 1; /* assume no Requested Device Type attributes */
1988
1989 if (attr.num_req_dev_type == 0)
1990 return 1; /* no Requested Device Type attributes -> match */
1991
1992 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1993 attr.num_req_dev_type))
1994 return 1; /* Own Primary Device Type matches */
1995
1996 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1997 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1998 attr.req_dev_type,
1999 attr.num_req_dev_type))
2000 return 1; /* Own Secondary Device Type matches */
2001
2002 /* No matching device type found */
2003 return 0;
2004}
2005
2006
2007struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
2008{
2009 struct wpabuf *buf;
2010 u8 *len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002011 int pw_id = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002012 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002014#ifdef CONFIG_WIFI_DISPLAY
2015 if (p2p->wfd_ie_probe_resp)
2016 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
2017#endif /* CONFIG_WIFI_DISPLAY */
2018
2019 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002020 if (buf == NULL)
2021 return NULL;
2022
Dmitry Shmidt04949592012-07-19 12:16:46 -07002023 if (p2p->go_neg_peer) {
2024 /* Advertise immediate availability of WPS credential */
2025 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
2026 }
2027
2028 p2p_build_wps_ie(p2p, buf, pw_id, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002029
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002030#ifdef CONFIG_WIFI_DISPLAY
2031 if (p2p->wfd_ie_probe_resp)
2032 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
2033#endif /* CONFIG_WIFI_DISPLAY */
2034
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002035 /* P2P IE */
2036 len = p2p_buf_add_ie_hdr(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002037 p2p_buf_add_capability(buf, p2p->dev_capab &
2038 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002039 if (p2p->ext_listen_interval)
2040 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
2041 p2p->ext_listen_interval);
2042 p2p_buf_add_device_info(buf, p2p, NULL);
2043 p2p_buf_update_ie_hdr(buf, len);
2044
2045 return buf;
2046}
2047
2048
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002049static int is_11b(u8 rate)
2050{
2051 return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
2052}
2053
2054
2055static int supp_rates_11b_only(struct ieee802_11_elems *elems)
2056{
2057 int num_11b = 0, num_others = 0;
2058 int i;
2059
2060 if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
2061 return 0;
2062
2063 for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
2064 if (is_11b(elems->supp_rates[i]))
2065 num_11b++;
2066 else
2067 num_others++;
2068 }
2069
2070 for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
2071 i++) {
2072 if (is_11b(elems->ext_supp_rates[i]))
2073 num_11b++;
2074 else
2075 num_others++;
2076 }
2077
2078 return num_11b > 0 && num_others == 0;
2079}
2080
2081
Dmitry Shmidt04949592012-07-19 12:16:46 -07002082static enum p2p_probe_req_status
2083p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2084 const u8 *bssid, const u8 *ie, size_t ie_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085{
2086 struct ieee802_11_elems elems;
2087 struct wpabuf *buf;
2088 struct ieee80211_mgmt *resp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002089 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002090 struct wpabuf *ies;
2091
2092 if (!p2p->in_listen || !p2p->drv_in_listen) {
2093 /* not in Listen state - ignore Probe Request */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002094 return P2P_PREQ_NOT_LISTEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002095 }
2096
2097 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2098 ParseFailed) {
2099 /* Ignore invalid Probe Request frames */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002100 return P2P_PREQ_MALFORMED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002101 }
2102
2103 if (elems.p2p == NULL) {
2104 /* not a P2P probe - ignore it */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002105 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002106 }
2107
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002108 if (dst && !is_broadcast_ether_addr(dst) &&
2109 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2110 /* Not sent to the broadcast address or our P2P Device Address
2111 */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002112 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002113 }
2114
2115 if (bssid && !is_broadcast_ether_addr(bssid)) {
2116 /* Not sent to the Wildcard BSSID */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002117 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002118 }
2119
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002120 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2121 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2122 0) {
2123 /* not using P2P Wildcard SSID - ignore */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002124 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002125 }
2126
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002127 if (supp_rates_11b_only(&elems)) {
2128 /* Indicates support for 11b rates only */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002129 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002130 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002131
2132 os_memset(&msg, 0, sizeof(msg));
2133 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2134 /* Could not parse P2P attributes */
Dmitry Shmidt04949592012-07-19 12:16:46 -07002135 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002136 }
2137
2138 if (msg.device_id &&
Dmitry Shmidt04949592012-07-19 12:16:46 -07002139 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002140 /* Device ID did not match */
2141 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002142 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002143 }
2144
2145 /* Check Requested Device Type match */
2146 if (msg.wps_attributes &&
2147 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2148 /* No match with Requested Device Type */
2149 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002150 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002151 }
2152 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002153
Dmitry Shmidt04949592012-07-19 12:16:46 -07002154 if (!p2p->cfg->send_probe_resp) {
2155 /* Response generated elsewhere */
2156 return P2P_PREQ_NOT_PROCESSED;
2157 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002158
2159 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2160 "P2P: Reply to P2P Probe Request in Listen state");
2161
2162 /*
2163 * We do not really have a specific BSS that this frame is advertising,
2164 * so build a frame that has some information in valid format. This is
2165 * really only used for discovery purposes, not to learn exact BSS
2166 * parameters.
2167 */
2168 ies = p2p_build_probe_resp_ies(p2p);
2169 if (ies == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002170 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002171
2172 buf = wpabuf_alloc(200 + wpabuf_len(ies));
2173 if (buf == NULL) {
2174 wpabuf_free(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002175 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002176 }
2177
2178 resp = NULL;
2179 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
2180
2181 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2182 (WLAN_FC_STYPE_PROBE_RESP << 4));
2183 os_memcpy(resp->da, addr, ETH_ALEN);
2184 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2185 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2186 resp->u.probe_resp.beacon_int = host_to_le16(100);
2187 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2188 resp->u.probe_resp.capab_info =
2189 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2190 WLAN_CAPABILITY_PRIVACY |
2191 WLAN_CAPABILITY_SHORT_SLOT_TIME);
2192
2193 wpabuf_put_u8(buf, WLAN_EID_SSID);
2194 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2195 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2196
2197 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2198 wpabuf_put_u8(buf, 8);
2199 wpabuf_put_u8(buf, (60 / 5) | 0x80);
2200 wpabuf_put_u8(buf, 90 / 5);
2201 wpabuf_put_u8(buf, (120 / 5) | 0x80);
2202 wpabuf_put_u8(buf, 180 / 5);
2203 wpabuf_put_u8(buf, (240 / 5) | 0x80);
2204 wpabuf_put_u8(buf, 360 / 5);
2205 wpabuf_put_u8(buf, 480 / 5);
2206 wpabuf_put_u8(buf, 540 / 5);
2207
2208 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2209 wpabuf_put_u8(buf, 1);
2210 wpabuf_put_u8(buf, p2p->cfg->channel);
2211
2212 wpabuf_put_buf(buf, ies);
2213 wpabuf_free(ies);
2214
2215 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
2216
2217 wpabuf_free(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002218
2219 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002220}
2221
2222
Dmitry Shmidt04949592012-07-19 12:16:46 -07002223enum p2p_probe_req_status
2224p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2225 const u8 *bssid, const u8 *ie, size_t ie_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002226{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002227 enum p2p_probe_req_status res;
2228
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002229 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2230
Dmitry Shmidt04949592012-07-19 12:16:46 -07002231 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002232
2233 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2234 p2p->go_neg_peer &&
2235 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002236 == 0 &&
2237 !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238 /* Received a Probe Request from GO Negotiation peer */
2239 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2240 "P2P: Found GO Negotiation peer - try to start GO "
2241 "negotiation from timeout");
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002242 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002243 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002244 return P2P_PREQ_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002245 }
2246
2247 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2248 p2p->invite_peer &&
2249 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2250 == 0) {
2251 /* Received a Probe Request from Invite peer */
2252 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2253 "P2P: Found Invite peer - try to start Invite from "
2254 "timeout");
2255 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002256 return P2P_PREQ_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257 }
2258
Dmitry Shmidt04949592012-07-19 12:16:46 -07002259 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002260}
2261
2262
2263static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2264 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2265{
2266 struct wpabuf *tmp;
2267 u8 *lpos;
2268 size_t tmplen;
2269 int res;
2270 u8 group_capab;
2271
2272 if (p2p_ie == NULL)
2273 return 0; /* WLAN AP is not a P2P manager */
2274
2275 /*
2276 * (Re)Association Request - P2P IE
2277 * P2P Capability attribute (shall be present)
2278 * P2P Interface attribute (present if concurrent device and
2279 * P2P Management is enabled)
2280 */
2281 tmp = wpabuf_alloc(200);
2282 if (tmp == NULL)
2283 return -1;
2284
2285 lpos = p2p_buf_add_ie_hdr(tmp);
2286 group_capab = 0;
2287 if (p2p->num_groups > 0) {
2288 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2289 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2290 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2291 p2p->cross_connect)
2292 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2293 }
2294 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2295 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2296 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2297 p2p_buf_add_p2p_interface(tmp, p2p);
2298 p2p_buf_update_ie_hdr(tmp, lpos);
2299
2300 tmplen = wpabuf_len(tmp);
2301 if (tmplen > len)
2302 res = -1;
2303 else {
2304 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2305 res = tmplen;
2306 }
2307 wpabuf_free(tmp);
2308
2309 return res;
2310}
2311
2312
2313int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2314 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2315{
2316 struct wpabuf *tmp;
2317 u8 *lpos;
2318 struct p2p_device *peer;
2319 size_t tmplen;
2320 int res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002321 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002322
2323 if (!p2p_group)
2324 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2325
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002326#ifdef CONFIG_WIFI_DISPLAY
2327 if (p2p->wfd_ie_assoc_req)
2328 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2329#endif /* CONFIG_WIFI_DISPLAY */
2330
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002331 /*
2332 * (Re)Association Request - P2P IE
2333 * P2P Capability attribute (shall be present)
2334 * Extended Listen Timing (may be present)
2335 * P2P Device Info attribute (shall be present)
2336 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002337 tmp = wpabuf_alloc(200 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002338 if (tmp == NULL)
2339 return -1;
2340
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002341#ifdef CONFIG_WIFI_DISPLAY
2342 if (p2p->wfd_ie_assoc_req)
2343 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2344#endif /* CONFIG_WIFI_DISPLAY */
2345
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002346 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2347
2348 lpos = p2p_buf_add_ie_hdr(tmp);
2349 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2350 if (p2p->ext_listen_interval)
2351 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2352 p2p->ext_listen_interval);
2353 p2p_buf_add_device_info(tmp, p2p, peer);
2354 p2p_buf_update_ie_hdr(tmp, lpos);
2355
2356 tmplen = wpabuf_len(tmp);
2357 if (tmplen > len)
2358 res = -1;
2359 else {
2360 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2361 res = tmplen;
2362 }
2363 wpabuf_free(tmp);
2364
2365 return res;
2366}
2367
2368
2369int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2370{
2371 struct wpabuf *p2p_ie;
2372 int ret;
2373
2374 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2375 if (p2p_ie == NULL)
2376 return 0;
2377
2378 ret = p2p_attr_text(p2p_ie, buf, end);
2379 wpabuf_free(p2p_ie);
2380 return ret;
2381}
2382
2383
Dmitry Shmidt04949592012-07-19 12:16:46 -07002384int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2385{
2386 struct p2p_message msg;
2387
2388 os_memset(&msg, 0, sizeof(msg));
2389 if (p2p_parse_p2p_ie(p2p_ie, &msg))
2390 return -1;
2391
2392 if (msg.p2p_device_addr) {
2393 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2394 return 0;
2395 } else if (msg.device_id) {
2396 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2397 return 0;
2398 }
2399 return -1;
2400}
2401
2402
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002403int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2404{
2405 struct wpabuf *p2p_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002406 int ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002407
2408 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2409 P2P_IE_VENDOR_TYPE);
2410 if (p2p_ie == NULL)
2411 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002412 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002413 wpabuf_free(p2p_ie);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002414 return ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002415}
2416
2417
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002418static void p2p_clear_go_neg(struct p2p_data *p2p)
2419{
2420 p2p->go_neg_peer = NULL;
2421 p2p_clear_timeout(p2p);
2422 p2p_set_state(p2p, P2P_IDLE);
2423}
2424
2425
2426void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2427{
2428 if (p2p->go_neg_peer == NULL) {
2429 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2430 "P2P: No pending Group Formation - "
2431 "ignore WPS registration success notification");
2432 return; /* No pending Group Formation */
2433 }
2434
2435 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2436 0) {
2437 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2438 "P2P: Ignore WPS registration success notification "
2439 "for " MACSTR " (GO Negotiation peer " MACSTR ")",
2440 MAC2STR(mac_addr),
2441 MAC2STR(p2p->go_neg_peer->intended_addr));
2442 return; /* Ignore unexpected peer address */
2443 }
2444
2445 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2446 "P2P: Group Formation completed successfully with " MACSTR,
2447 MAC2STR(mac_addr));
2448
2449 p2p_clear_go_neg(p2p);
2450}
2451
2452
2453void p2p_group_formation_failed(struct p2p_data *p2p)
2454{
2455 if (p2p->go_neg_peer == NULL) {
2456 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2457 "P2P: No pending Group Formation - "
2458 "ignore group formation failure notification");
2459 return; /* No pending Group Formation */
2460 }
2461
2462 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2463 "P2P: Group Formation failed with " MACSTR,
2464 MAC2STR(p2p->go_neg_peer->intended_addr));
2465
2466 p2p_clear_go_neg(p2p);
2467}
2468
2469
2470struct p2p_data * p2p_init(const struct p2p_config *cfg)
2471{
2472 struct p2p_data *p2p;
2473
2474 if (cfg->max_peers < 1)
2475 return NULL;
2476
2477 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2478 if (p2p == NULL)
2479 return NULL;
2480 p2p->cfg = (struct p2p_config *) (p2p + 1);
2481 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2482 if (cfg->dev_name)
2483 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2484 if (cfg->manufacturer)
2485 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2486 if (cfg->model_name)
2487 p2p->cfg->model_name = os_strdup(cfg->model_name);
2488 if (cfg->model_number)
2489 p2p->cfg->model_number = os_strdup(cfg->model_number);
2490 if (cfg->serial_number)
2491 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002492 if (cfg->pref_chan) {
2493 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2494 sizeof(struct p2p_channel));
2495 if (p2p->cfg->pref_chan) {
2496 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2497 cfg->num_pref_chan *
2498 sizeof(struct p2p_channel));
2499 } else
2500 p2p->cfg->num_pref_chan = 0;
2501 }
2502
Irfan Sherifff44b9c42012-06-13 11:09:23 -07002503#ifdef ANDROID_P2P
2504 /* 100ms listen time is too less to receive the response frames in some scenarios
2505 * increasing min listen time to 200ms.
2506 */
2507 p2p->min_disc_int = 2;
2508 /* SD_FAIR_POLICY: Initializing the SD current serviced pointer to NULL */
2509 p2p->sd_dev_list = NULL;
2510#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002511 p2p->min_disc_int = 1;
Irfan Sherifff44b9c42012-06-13 11:09:23 -07002512#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002513 p2p->max_disc_int = 3;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002514 p2p->max_disc_tu = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002515
2516 os_get_random(&p2p->next_tie_breaker, 1);
2517 p2p->next_tie_breaker &= 0x01;
2518 if (cfg->sd_request)
2519 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2520 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2521 if (cfg->concurrent_operations)
2522 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2523 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2524
2525 dl_list_init(&p2p->devices);
2526
2527 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
2528 p2p_expiration_timeout, p2p, NULL);
2529
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002530 p2p->go_timeout = 100;
2531 p2p->client_timeout = 20;
2532
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002533 return p2p;
2534}
2535
2536
2537void p2p_deinit(struct p2p_data *p2p)
2538{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002539#ifdef CONFIG_WIFI_DISPLAY
2540 wpabuf_free(p2p->wfd_ie_beacon);
2541 wpabuf_free(p2p->wfd_ie_probe_req);
2542 wpabuf_free(p2p->wfd_ie_probe_resp);
2543 wpabuf_free(p2p->wfd_ie_assoc_req);
2544 wpabuf_free(p2p->wfd_ie_invitation);
2545 wpabuf_free(p2p->wfd_ie_prov_disc_req);
2546 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
2547 wpabuf_free(p2p->wfd_ie_go_neg);
2548 wpabuf_free(p2p->wfd_dev_info);
2549 wpabuf_free(p2p->wfd_assoc_bssid);
2550 wpabuf_free(p2p->wfd_coupled_sink_info);
2551#endif /* CONFIG_WIFI_DISPLAY */
2552
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002553 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
2554 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2555 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002556 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002557 p2p_flush(p2p);
2558 p2p_free_req_dev_types(p2p);
2559 os_free(p2p->cfg->dev_name);
2560 os_free(p2p->cfg->manufacturer);
2561 os_free(p2p->cfg->model_name);
2562 os_free(p2p->cfg->model_number);
2563 os_free(p2p->cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002564 os_free(p2p->cfg->pref_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002565 os_free(p2p->groups);
2566 wpabuf_free(p2p->sd_resp);
2567 os_free(p2p->after_scan_tx);
2568 p2p_remove_wps_vendor_extensions(p2p);
2569 os_free(p2p);
2570}
2571
2572
2573void p2p_flush(struct p2p_data *p2p)
2574{
2575 struct p2p_device *dev, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002576 p2p_stop_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002577 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2578 list) {
2579 dl_list_del(&dev->list);
2580 p2p_device_free(p2p, dev);
2581 }
Irfan Sherifff44b9c42012-06-13 11:09:23 -07002582#ifdef ANDROID_P2P
2583 /* SD_FAIR_POLICY: Initializing the SD current serviced pointer to NULL */
2584 p2p->sd_dev_list = NULL;
2585#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002586 p2p_free_sd_queries(p2p);
2587 os_free(p2p->after_scan_tx);
2588 p2p->after_scan_tx = NULL;
2589}
2590
2591
2592int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2593{
2594 struct p2p_device *dev;
2595
2596 dev = p2p_get_device(p2p, addr);
2597 if (dev == NULL)
2598 return -1;
2599
2600 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
2601 MAC2STR(addr));
2602
2603 if (p2p->go_neg_peer == dev)
2604 p2p->go_neg_peer = NULL;
2605
2606 dev->wps_method = WPS_NOT_READY;
2607 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2608 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2609
2610 /* Check if after_scan_tx is for this peer. If so free it */
2611 if (p2p->after_scan_tx &&
2612 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2613 os_free(p2p->after_scan_tx);
2614 p2p->after_scan_tx = NULL;
2615 }
2616
2617 return 0;
2618}
2619
2620
2621int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2622{
2623 os_free(p2p->cfg->dev_name);
2624 if (dev_name) {
2625 p2p->cfg->dev_name = os_strdup(dev_name);
2626 if (p2p->cfg->dev_name == NULL)
2627 return -1;
2628 } else
2629 p2p->cfg->dev_name = NULL;
2630 return 0;
2631}
2632
2633
2634int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2635{
2636 os_free(p2p->cfg->manufacturer);
2637 p2p->cfg->manufacturer = NULL;
2638 if (manufacturer) {
2639 p2p->cfg->manufacturer = os_strdup(manufacturer);
2640 if (p2p->cfg->manufacturer == NULL)
2641 return -1;
2642 }
2643
2644 return 0;
2645}
2646
2647
2648int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2649{
2650 os_free(p2p->cfg->model_name);
2651 p2p->cfg->model_name = NULL;
2652 if (model_name) {
2653 p2p->cfg->model_name = os_strdup(model_name);
2654 if (p2p->cfg->model_name == NULL)
2655 return -1;
2656 }
2657
2658 return 0;
2659}
2660
2661
2662int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2663{
2664 os_free(p2p->cfg->model_number);
2665 p2p->cfg->model_number = NULL;
2666 if (model_number) {
2667 p2p->cfg->model_number = os_strdup(model_number);
2668 if (p2p->cfg->model_number == NULL)
2669 return -1;
2670 }
2671
2672 return 0;
2673}
2674
2675
2676int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2677{
2678 os_free(p2p->cfg->serial_number);
2679 p2p->cfg->serial_number = NULL;
2680 if (serial_number) {
2681 p2p->cfg->serial_number = os_strdup(serial_number);
2682 if (p2p->cfg->serial_number == NULL)
2683 return -1;
2684 }
2685
2686 return 0;
2687}
2688
2689
2690void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2691{
2692 p2p->cfg->config_methods = config_methods;
2693}
2694
2695
2696void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2697{
2698 os_memcpy(p2p->cfg->uuid, uuid, 16);
2699}
2700
2701
2702int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2703{
2704 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2705 return 0;
2706}
2707
2708
2709int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2710 size_t num_dev_types)
2711{
2712 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2713 num_dev_types = P2P_SEC_DEVICE_TYPES;
2714 p2p->cfg->num_sec_dev_types = num_dev_types;
2715 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2716 return 0;
2717}
2718
2719
2720void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2721{
2722 int i;
2723
2724 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2725 wpabuf_free(p2p->wps_vendor_ext[i]);
2726 p2p->wps_vendor_ext[i] = NULL;
2727 }
2728}
2729
2730
2731int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2732 const struct wpabuf *vendor_ext)
2733{
2734 int i;
2735
2736 if (vendor_ext == NULL)
2737 return -1;
2738
2739 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2740 if (p2p->wps_vendor_ext[i] == NULL)
2741 break;
2742 }
2743 if (i >= P2P_MAX_WPS_VENDOR_EXT)
2744 return -1;
2745
2746 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2747 if (p2p->wps_vendor_ext[i] == NULL)
2748 return -1;
2749
2750 return 0;
2751}
2752
2753
2754int p2p_set_country(struct p2p_data *p2p, const char *country)
2755{
2756 os_memcpy(p2p->cfg->country, country, 3);
2757 return 0;
2758}
2759
2760
2761void p2p_continue_find(struct p2p_data *p2p)
2762{
2763 struct p2p_device *dev;
Irfan Sherifff44b9c42012-06-13 11:09:23 -07002764#ifdef ANDROID_P2P
2765 int skip=1;
2766#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002767 p2p_set_state(p2p, P2P_SEARCH);
2768 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Irfan Sherifff44b9c42012-06-13 11:09:23 -07002769#ifdef ANDROID_P2P
2770 /* SD_FAIR_POLICY: We need to give chance to all devices in the device list
2771 * There may be a scenario, where a particular peer device have
2772 * not registered any query response. When we send a SD request to such device,
2773 * no response will be received. And if we continue to get probe responses from that device,
2774 * and if that device happens to be on top in our device list,
2775 * we will always continue to send SD requests always to that peer only.
2776 * We will not be able to send SD requests to other devices in that case.
2777 * This implementation keeps track of last serviced peer device.
2778 * And then takes the next one from the device list, in the next iteration.
2779 */
2780 if (p2p->sd_dev_list && p2p->sd_dev_list != &p2p->devices) {
2781 if(skip) {
2782 if ((&dev->list == p2p->sd_dev_list) ) {
2783 skip = 0;
2784 if (dev->list.next == &p2p->devices)
2785 p2p->sd_dev_list = NULL;
2786 }
2787 continue;
2788 }
2789 }
2790 p2p->sd_dev_list = &dev->list;
2791 wpa_printf(MSG_DEBUG, "P2P: ### Servicing %p dev->flags 0x%x SD schedule %s devaddr " MACSTR,
2792 p2p->sd_dev_list, dev->flags, dev->flags & P2P_DEV_SD_SCHEDULE ? "TRUE": "FALSE",
2793 MAC2STR(dev->info.p2p_device_addr));
2794#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002795 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2796 if (p2p_start_sd(p2p, dev) == 0)
2797 return;
2798 else
2799 break;
2800 } else if (dev->req_config_methods &&
2801 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2802 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002803 "pending Provision Discovery Request to "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002804 MACSTR " (config methods 0x%x)",
2805 MAC2STR(dev->info.p2p_device_addr),
2806 dev->req_config_methods);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002807 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002808 return;
2809 }
2810 }
2811
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002812 p2p_listen_in_find(p2p, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002813}
2814
2815
2816static void p2p_sd_cb(struct p2p_data *p2p, int success)
2817{
2818 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2819 "P2P: Service Discovery Query TX callback: success=%d",
2820 success);
2821 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2822
2823 if (!success) {
2824 if (p2p->sd_peer) {
2825 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2826 p2p->sd_peer = NULL;
2827 }
2828 p2p_continue_find(p2p);
2829 return;
2830 }
2831
2832 if (p2p->sd_peer == NULL) {
2833 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2834 "P2P: No SD peer entry known");
2835 p2p_continue_find(p2p);
2836 return;
2837 }
2838
2839 /* Wait for response from the peer */
2840 p2p_set_state(p2p, P2P_SD_DURING_FIND);
2841 p2p_set_timeout(p2p, 0, 200000);
2842}
2843
Jouni Malinen75ecf522011-06-27 15:19:46 -07002844
2845/**
2846 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2847 * @p2p: P2P module context from p2p_init()
2848 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002849static void p2p_retry_pd(struct p2p_data *p2p)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002850{
2851 struct p2p_device *dev;
2852
2853 if (p2p->state != P2P_IDLE)
2854 return;
2855
2856 /*
2857 * Retry the prov disc req attempt only for the peer that the user had
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002858 * requested.
Jouni Malinen75ecf522011-06-27 15:19:46 -07002859 */
2860
2861 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2862 if (os_memcmp(p2p->pending_pd_devaddr,
2863 dev->info.p2p_device_addr, ETH_ALEN) != 0)
2864 continue;
2865 if (!dev->req_config_methods)
2866 continue;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002867
2868 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002869 "pending Provision Discovery Request to "
Jouni Malinen75ecf522011-06-27 15:19:46 -07002870 MACSTR " (config methods 0x%x)",
2871 MAC2STR(dev->info.p2p_device_addr),
2872 dev->req_config_methods);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002873 p2p_send_prov_disc_req(p2p, dev,
2874 dev->flags & P2P_DEV_PD_FOR_JOIN, 0);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002875 return;
2876 }
2877}
2878
2879
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002880static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2881{
2882 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2883 "P2P: Provision Discovery Request TX callback: success=%d",
2884 success);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002885
2886 /*
2887 * Postpone resetting the pending action state till after we actually
2888 * time out. This allows us to take some action like notifying any
2889 * interested parties about no response to the request.
2890 *
2891 * When the timer (below) goes off we check in IDLE, SEARCH, or
2892 * LISTEN_ONLY state, which are the only allowed states to issue a PD
2893 * requests in, if this was still pending and then raise notification.
2894 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002895
2896 if (!success) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07002897 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2898
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002899 if (p2p->user_initiated_pd &&
2900 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
2901 {
2902 /* Retry request from timeout to avoid busy loops */
2903 p2p->pending_action_state = P2P_PENDING_PD;
2904 p2p_set_timeout(p2p, 0, 50000);
2905 } else if (p2p->state != P2P_IDLE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002906 p2p_continue_find(p2p);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002907 else if (p2p->user_initiated_pd) {
2908 p2p->pending_action_state = P2P_PENDING_PD;
Irfan Sheriff1a2ce112012-10-30 22:22:52 -07002909#ifdef ANDROID_P2P
2910 p2p_set_timeout(p2p, 0, 350000);
2911#else
Jouni Malinen75ecf522011-06-27 15:19:46 -07002912 p2p_set_timeout(p2p, 0, 300000);
Irfan Sheriff1a2ce112012-10-30 22:22:52 -07002913#endif
Jouni Malinen75ecf522011-06-27 15:19:46 -07002914 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002915 return;
2916 }
2917
Jouni Malinen75ecf522011-06-27 15:19:46 -07002918 /*
2919 * This postponing, of resetting pending_action_state, needs to be
2920 * done only for user initiated PD requests and not internal ones.
2921 */
2922 if (p2p->user_initiated_pd)
2923 p2p->pending_action_state = P2P_PENDING_PD;
2924 else
2925 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2926
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002927 /* Wait for response from the peer */
2928 if (p2p->state == P2P_SEARCH)
2929 p2p_set_state(p2p, P2P_PD_DURING_FIND);
Irfan Sheriff1a2ce112012-10-30 22:22:52 -07002930#ifdef ANDROID_P2P
2931 p2p_set_timeout(p2p, 0, 350000);
2932#else
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002933 p2p_set_timeout(p2p, 0, 200000);
Irfan Sheriff1a2ce112012-10-30 22:22:52 -07002934#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002935}
2936
2937
2938int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002939 struct os_time *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002940 size_t ies_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002941{
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002942 if (os_time_before(rx_time, &p2p->find_start)) {
2943 /*
2944 * The driver may have cached (e.g., in cfg80211 BSS table) the
2945 * scan results for relatively long time. To avoid reporting
2946 * stale information, update P2P peers only based on results
2947 * that have based on frames received after the last p2p_find
2948 * operation was started.
2949 */
2950 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Ignore old scan "
2951 "result for " MACSTR " (rx_time=%u.%06u)",
2952 MAC2STR(bssid), (unsigned int) rx_time->sec,
2953 (unsigned int) rx_time->usec);
2954 return 0;
2955 }
2956
2957 p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002958
2959 return 0;
2960}
2961
2962
2963void p2p_scan_res_handled(struct p2p_data *p2p)
2964{
2965 if (!p2p->p2p_scan_running) {
2966 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
2967 "running, but scan results received");
2968 }
2969 p2p->p2p_scan_running = 0;
2970 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2971
2972 if (p2p_run_after_scan(p2p))
2973 return;
2974 if (p2p->state == P2P_SEARCH)
2975 p2p_continue_find(p2p);
2976}
2977
2978
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002979void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002980{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002981 u8 *len;
2982
2983#ifdef CONFIG_WIFI_DISPLAY
2984 if (p2p->wfd_ie_probe_req)
2985 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
2986#endif /* CONFIG_WIFI_DISPLAY */
2987
2988 len = p2p_buf_add_ie_hdr(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002989 p2p_buf_add_capability(ies, p2p->dev_capab &
2990 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002991 if (dev_id)
2992 p2p_buf_add_device_id(ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002993 if (p2p->cfg->reg_class && p2p->cfg->channel)
2994 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2995 p2p->cfg->reg_class,
2996 p2p->cfg->channel);
2997 if (p2p->ext_listen_interval)
2998 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2999 p2p->ext_listen_interval);
3000 /* TODO: p2p_buf_add_operating_channel() if GO */
3001 p2p_buf_update_ie_hdr(ies, len);
3002}
3003
3004
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003005size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
3006{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003007 size_t len = 100;
3008
3009#ifdef CONFIG_WIFI_DISPLAY
3010 if (p2p && p2p->wfd_ie_probe_req)
3011 len += wpabuf_len(p2p->wfd_ie_probe_req);
3012#endif /* CONFIG_WIFI_DISPLAY */
3013
3014 return len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003015}
3016
3017
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003018int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
3019{
3020 return p2p_attr_text(p2p_ie, buf, end);
3021}
3022
3023
3024static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
3025{
3026 struct p2p_device *dev = p2p->go_neg_peer;
3027
3028 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3029 "P2P: GO Negotiation Request TX callback: success=%d",
3030 success);
3031
3032 if (dev == NULL) {
3033 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3034 "P2P: No pending GO Negotiation");
3035 return;
3036 }
3037
3038 if (success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003039 if (dev->flags & P2P_DEV_USER_REJECTED) {
3040 p2p_set_state(p2p, P2P_IDLE);
3041 return;
3042 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003043 } else if (dev->go_neg_req_sent) {
3044 /* Cancel the increment from p2p_connect_send() on failure */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07003045 dev->go_neg_req_sent--;
3046 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003047
3048 if (!success &&
3049 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
3050 !is_zero_ether_addr(dev->member_in_go_dev)) {
3051 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3052 "P2P: Peer " MACSTR " did not acknowledge request - "
3053 "try to use device discoverability through its GO",
3054 MAC2STR(dev->info.p2p_device_addr));
3055 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3056 p2p_send_dev_disc_req(p2p, dev);
3057 return;
3058 }
3059
3060 /*
3061 * Use P2P find, if needed, to find the other device from its listen
3062 * channel.
3063 */
3064 p2p_set_state(p2p, P2P_CONNECT);
Irfan Sheriff1a2ce112012-10-30 22:22:52 -07003065#ifdef ANDROID_P2P
3066 p2p_set_timeout(p2p, 0, 350000);
3067#else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003068 p2p_set_timeout(p2p, 0, success ? 200000 : 100000);
Irfan Sheriff1a2ce112012-10-30 22:22:52 -07003069#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003070}
3071
3072
3073static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
3074{
3075 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3076 "P2P: GO Negotiation Response TX callback: success=%d",
3077 success);
3078 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
3079 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3080 "P2P: Ignore TX callback event - GO Negotiation is "
3081 "not running anymore");
3082 return;
3083 }
3084 p2p_set_state(p2p, P2P_CONNECT);
Irfan Sheriff1a2ce112012-10-30 22:22:52 -07003085#ifdef ANDROID_P2P
3086 p2p_set_timeout(p2p, 0, 350000);
3087#else
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003088 p2p_set_timeout(p2p, 0, 250000);
Irfan Sheriff1a2ce112012-10-30 22:22:52 -07003089#endif
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003090}
3091
3092
3093static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
3094{
3095 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3096 "P2P: GO Negotiation Response (failure) TX callback: "
3097 "success=%d", success);
3098 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
3099 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
3100 p2p->go_neg_peer->status);
3101 }
3102}
3103
3104
3105static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
3106 enum p2p_send_action_result result)
3107{
3108 struct p2p_device *dev;
3109
3110 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3111 "P2P: GO Negotiation Confirm TX callback: result=%d",
3112 result);
3113 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3114 if (result == P2P_SEND_ACTION_FAILED) {
3115 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3116 return;
3117 }
3118 if (result == P2P_SEND_ACTION_NO_ACK) {
3119 /*
3120 * It looks like the TX status for GO Negotiation Confirm is
3121 * often showing failure even when the peer has actually
3122 * received the frame. Since the peer may change channels
3123 * immediately after having received the frame, we may not see
3124 * an Ack for retries, so just dropping a single frame may
3125 * trigger this. To allow the group formation to succeed if the
3126 * peer did indeed receive the frame, continue regardless of
3127 * the TX status.
3128 */
3129 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3130 "P2P: Assume GO Negotiation Confirm TX was actually "
3131 "received by the peer even though Ack was not "
3132 "reported");
3133 }
3134
3135 dev = p2p->go_neg_peer;
3136 if (dev == NULL)
3137 return;
3138
3139 p2p_go_complete(p2p, dev);
3140}
3141
3142
3143void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3144 const u8 *src, const u8 *bssid,
3145 enum p2p_send_action_result result)
3146{
3147 enum p2p_pending_action_state state;
3148 int success;
3149
3150 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3151 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
3152 " src=" MACSTR " bssid=" MACSTR " result=%d",
3153 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
3154 MAC2STR(bssid), result);
3155 success = result == P2P_SEND_ACTION_SUCCESS;
3156 state = p2p->pending_action_state;
3157 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3158 switch (state) {
3159 case P2P_NO_PENDING_ACTION:
3160 break;
3161 case P2P_PENDING_GO_NEG_REQUEST:
3162 p2p_go_neg_req_cb(p2p, success);
3163 break;
3164 case P2P_PENDING_GO_NEG_RESPONSE:
3165 p2p_go_neg_resp_cb(p2p, success);
3166 break;
3167 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
3168 p2p_go_neg_resp_failure_cb(p2p, success);
3169 break;
3170 case P2P_PENDING_GO_NEG_CONFIRM:
3171 p2p_go_neg_conf_cb(p2p, result);
3172 break;
3173 case P2P_PENDING_SD:
3174 p2p_sd_cb(p2p, success);
3175 break;
3176 case P2P_PENDING_PD:
3177 p2p_prov_disc_cb(p2p, success);
3178 break;
3179 case P2P_PENDING_INVITATION_REQUEST:
3180 p2p_invitation_req_cb(p2p, success);
3181 break;
3182 case P2P_PENDING_INVITATION_RESPONSE:
3183 p2p_invitation_resp_cb(p2p, success);
3184 break;
3185 case P2P_PENDING_DEV_DISC_REQUEST:
3186 p2p_dev_disc_req_cb(p2p, success);
3187 break;
3188 case P2P_PENDING_DEV_DISC_RESPONSE:
3189 p2p_dev_disc_resp_cb(p2p, success);
3190 break;
3191 case P2P_PENDING_GO_DISC_REQ:
3192 p2p_go_disc_req_cb(p2p, success);
3193 break;
3194 }
3195}
3196
3197
3198void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3199 unsigned int duration)
3200{
3201 if (freq == p2p->pending_client_disc_freq) {
3202 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3203 "P2P: Client discoverability remain-awake completed");
3204 p2p->pending_client_disc_freq = 0;
3205 return;
3206 }
3207
3208 if (freq != p2p->pending_listen_freq) {
3209 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3210 "P2P: Unexpected listen callback for freq=%u "
3211 "duration=%u (pending_listen_freq=%u)",
3212 freq, duration, p2p->pending_listen_freq);
3213 return;
3214 }
3215
3216 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3217 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
3218 "callback",
3219 p2p->pending_listen_sec, p2p->pending_listen_usec,
3220 p2p->pending_listen_freq);
3221 p2p->in_listen = 1;
3222 p2p->drv_in_listen = freq;
3223 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3224 /*
3225 * Add 20 msec extra wait to avoid race condition with driver
3226 * remain-on-channel end event, i.e., give driver more time to
3227 * complete the operation before our timeout expires.
3228 */
3229 p2p_set_timeout(p2p, p2p->pending_listen_sec,
3230 p2p->pending_listen_usec + 20000);
3231 }
3232
3233 p2p->pending_listen_freq = 0;
3234}
3235
3236
3237int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3238{
3239 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
3240 "state (freq=%u)", freq);
3241 p2p->drv_in_listen = 0;
3242 if (p2p->in_listen)
3243 return 0; /* Internal timeout will trigger the next step */
3244
3245 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3246 if (p2p->go_neg_peer->connect_reqs >= 120) {
3247 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3248 "P2P: Timeout on sending GO Negotiation "
3249 "Request without getting response");
3250 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3251 return 0;
3252 }
3253
3254 p2p_set_state(p2p, P2P_CONNECT);
3255 p2p_connect_send(p2p, p2p->go_neg_peer);
3256 return 1;
3257 } else if (p2p->state == P2P_SEARCH) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003258 if (p2p->p2p_scan_running) {
3259 /*
3260 * Search is already in progress. This can happen if
3261 * an Action frame RX is reported immediately after
3262 * the end of a remain-on-channel operation and the
3263 * response frame to that is sent using an offchannel
3264 * operation while in p2p_find. Avoid an attempt to
3265 * restart a scan here.
3266 */
3267 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan "
3268 "already in progress - do not try to start a "
3269 "new one");
3270 return 1;
3271 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003272 if (p2p->pending_listen_freq) {
3273 /*
3274 * Better wait a bit if the driver is unable to start
3275 * offchannel operation for some reason. p2p_search()
3276 * will be started from internal timeout.
3277 */
3278 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Listen "
3279 "operation did not seem to start - delay "
3280 "search phase to avoid busy loop");
3281 p2p_set_timeout(p2p, 0, 100000);
3282 return 1;
3283 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003284 if (p2p->search_delay) {
3285 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
3286 "search operation by %u ms",
3287 p2p->search_delay);
3288 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3289 (p2p->search_delay % 1000) * 1000);
3290 return 1;
3291 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003292 p2p_search(p2p);
3293 return 1;
3294 }
3295
3296 return 0;
3297}
3298
3299
3300static void p2p_timeout_connect(struct p2p_data *p2p)
3301{
3302 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003303 if (p2p->go_neg_peer &&
3304 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
3305 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Wait for GO "
3306 "Negotiation Confirm timed out - assume GO "
3307 "Negotiation failed");
3308 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3309 return;
3310 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003312 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003313}
3314
3315
3316static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3317{
3318 if (p2p->go_neg_peer) {
3319 if (p2p->drv_in_listen) {
3320 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
3321 "still in Listen state; wait for it to "
3322 "complete");
3323 return;
3324 }
3325
3326 if (p2p->go_neg_peer->connect_reqs >= 120) {
3327 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3328 "P2P: Timeout on sending GO Negotiation "
3329 "Request without getting response");
3330 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3331 return;
3332 }
3333
3334 p2p_set_state(p2p, P2P_CONNECT);
3335 p2p_connect_send(p2p, p2p->go_neg_peer);
3336 } else
3337 p2p_set_state(p2p, P2P_IDLE);
3338}
3339
3340
3341static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3342{
3343 /*
3344 * TODO: could remain constantly in Listen state for some time if there
3345 * are no other concurrent uses for the radio. For now, go to listen
3346 * state once per second to give other uses a chance to use the radio.
3347 */
3348 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003349 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003350}
3351
3352
3353static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3354{
3355 struct p2p_device *dev = p2p->go_neg_peer;
3356
3357 if (dev == NULL) {
3358 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3359 "P2P: Unknown GO Neg peer - stop GO Neg wait");
3360 return;
3361 }
3362
3363 dev->wait_count++;
3364 if (dev->wait_count >= 120) {
3365 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3366 "P2P: Timeout on waiting peer to become ready for GO "
3367 "Negotiation");
3368 p2p_go_neg_failed(p2p, dev, -1);
3369 return;
3370 }
3371
3372 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3373 "P2P: Go to Listen state while waiting for the peer to become "
3374 "ready for GO Negotiation");
3375 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003376 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003377}
3378
3379
3380static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3381{
3382 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3383 "P2P: Service Discovery Query timeout");
3384 if (p2p->sd_peer) {
3385 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3386 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
3387 p2p->sd_peer = NULL;
3388 }
3389 p2p_continue_find(p2p);
3390}
3391
3392
3393static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
3394{
3395 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3396 "P2P: Provision Discovery Request timeout");
3397 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3398 p2p_continue_find(p2p);
3399}
3400
3401
Jouni Malinen75ecf522011-06-27 15:19:46 -07003402static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
3403{
3404 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3405
3406 /*
3407 * For user initiated PD requests that we have not gotten any responses
3408 * for while in IDLE state, we retry them a couple of times before
3409 * giving up.
3410 */
3411 if (!p2p->user_initiated_pd)
3412 return;
3413
3414 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3415 "P2P: User initiated Provision Discovery Request timeout");
3416
3417 if (p2p->pd_retries) {
3418 p2p->pd_retries--;
3419 p2p_retry_pd(p2p);
3420 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003421 struct p2p_device *dev;
3422 int for_join = 0;
3423
3424 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3425 if (os_memcmp(p2p->pending_pd_devaddr,
3426 dev->info.p2p_device_addr, ETH_ALEN) != 0)
3427 continue;
3428 if (dev->req_config_methods &&
3429 (dev->flags & P2P_DEV_PD_FOR_JOIN))
3430 for_join = 1;
3431 }
3432
Jouni Malinen75ecf522011-06-27 15:19:46 -07003433 if (p2p->cfg->prov_disc_fail)
3434 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
3435 p2p->pending_pd_devaddr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003436 for_join ?
3437 P2P_PROV_DISC_TIMEOUT_JOIN :
Jouni Malinen75ecf522011-06-27 15:19:46 -07003438 P2P_PROV_DISC_TIMEOUT);
3439 p2p_reset_pending_pd(p2p);
3440 }
3441}
3442
3443
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003444static void p2p_timeout_invite(struct p2p_data *p2p)
3445{
3446 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3447 p2p_set_state(p2p, P2P_INVITE_LISTEN);
3448 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
3449 /*
3450 * Better remain on operating channel instead of listen channel
3451 * when running a group.
3452 */
3453 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
3454 "active GO role - wait on operating channel");
3455 p2p_set_timeout(p2p, 0, 100000);
3456 return;
3457 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003458 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003459}
3460
3461
3462static void p2p_timeout_invite_listen(struct p2p_data *p2p)
3463{
3464 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
3465 p2p_set_state(p2p, P2P_INVITE);
3466 p2p_invite_send(p2p, p2p->invite_peer,
3467 p2p->invite_go_dev_addr);
3468 } else {
3469 if (p2p->invite_peer) {
3470 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3471 "P2P: Invitation Request retry limit reached");
3472 if (p2p->cfg->invitation_result)
3473 p2p->cfg->invitation_result(
3474 p2p->cfg->cb_ctx, -1, NULL);
3475 }
3476 p2p_set_state(p2p, P2P_IDLE);
3477 }
3478}
3479
3480
3481static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
3482{
3483 struct p2p_data *p2p = eloop_ctx;
3484
3485 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
3486 p2p_state_txt(p2p->state));
3487
3488 p2p->in_listen = 0;
3489
3490 switch (p2p->state) {
3491 case P2P_IDLE:
Jouni Malinen75ecf522011-06-27 15:19:46 -07003492 /* Check if we timed out waiting for PD req */
3493 if (p2p->pending_action_state == P2P_PENDING_PD)
3494 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003495 break;
3496 case P2P_SEARCH:
Jouni Malinen75ecf522011-06-27 15:19:46 -07003497 /* Check if we timed out waiting for PD req */
3498 if (p2p->pending_action_state == P2P_PENDING_PD)
3499 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003500 if (p2p->search_delay && !p2p->in_search_delay) {
3501 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
3502 "search operation by %u ms",
3503 p2p->search_delay);
3504 p2p->in_search_delay = 1;
3505 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3506 (p2p->search_delay % 1000) * 1000);
3507 break;
3508 }
3509 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003510 p2p_search(p2p);
3511 break;
3512 case P2P_CONNECT:
3513 p2p_timeout_connect(p2p);
3514 break;
3515 case P2P_CONNECT_LISTEN:
3516 p2p_timeout_connect_listen(p2p);
3517 break;
3518 case P2P_GO_NEG:
3519 break;
3520 case P2P_LISTEN_ONLY:
Jouni Malinen75ecf522011-06-27 15:19:46 -07003521 /* Check if we timed out waiting for PD req */
3522 if (p2p->pending_action_state == P2P_PENDING_PD)
3523 p2p_timeout_prov_disc_req(p2p);
3524
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003525 if (p2p->ext_listen_only) {
3526 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3527 "P2P: Extended Listen Timing - Listen State "
3528 "completed");
3529 p2p->ext_listen_only = 0;
3530 p2p_set_state(p2p, P2P_IDLE);
3531 }
3532 break;
3533 case P2P_WAIT_PEER_CONNECT:
3534 p2p_timeout_wait_peer_connect(p2p);
3535 break;
3536 case P2P_WAIT_PEER_IDLE:
3537 p2p_timeout_wait_peer_idle(p2p);
3538 break;
3539 case P2P_SD_DURING_FIND:
3540 p2p_timeout_sd_during_find(p2p);
3541 break;
3542 case P2P_PROVISIONING:
3543 break;
3544 case P2P_PD_DURING_FIND:
3545 p2p_timeout_prov_disc_during_find(p2p);
3546 break;
3547 case P2P_INVITE:
3548 p2p_timeout_invite(p2p);
3549 break;
3550 case P2P_INVITE_LISTEN:
3551 p2p_timeout_invite_listen(p2p);
3552 break;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003553 case P2P_SEARCH_WHEN_READY:
3554 break;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003555 case P2P_CONTINUE_SEARCH_WHEN_READY:
3556 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003557 }
3558}
3559
3560
3561int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
3562{
3563 struct p2p_device *dev;
3564
3565 dev = p2p_get_device(p2p, peer_addr);
3566 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
3567 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
3568 if (dev == NULL) {
3569 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
3570 " unknown", MAC2STR(peer_addr));
3571 return -1;
3572 }
3573 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
3574 dev->flags |= P2P_DEV_USER_REJECTED;
3575 return 0;
3576}
3577
3578
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003579const char * p2p_wps_method_text(enum p2p_wps_method method)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003580{
3581 switch (method) {
3582 case WPS_NOT_READY:
3583 return "not-ready";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003584 case WPS_PIN_DISPLAY:
3585 return "Display";
3586 case WPS_PIN_KEYPAD:
3587 return "Keypad";
3588 case WPS_PBC:
3589 return "PBC";
3590 }
3591
3592 return "??";
3593}
3594
3595
3596static const char * p2p_go_state_text(enum p2p_go_state go_state)
3597{
3598 switch (go_state) {
3599 case UNKNOWN_GO:
3600 return "unknown";
3601 case LOCAL_GO:
3602 return "local";
3603 case REMOTE_GO:
3604 return "remote";
3605 }
3606
3607 return "??";
3608}
3609
3610
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003611const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
3612 const u8 *addr, int next)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003613{
3614 struct p2p_device *dev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003615
3616 if (addr)
3617 dev = p2p_get_device(p2p, addr);
3618 else
3619 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3620
3621 if (dev && next) {
3622 dev = dl_list_first(&dev->list, struct p2p_device, list);
3623 if (&dev->list == &p2p->devices)
3624 dev = NULL;
3625 }
3626
3627 if (dev == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003628 return NULL;
3629
3630 return &dev->info;
3631}
3632
3633
3634int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
3635 char *buf, size_t buflen)
3636{
3637 struct p2p_device *dev;
3638 int res;
3639 char *pos, *end;
3640 struct os_time now;
3641
3642 if (info == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003643 return -1;
3644
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003645 dev = (struct p2p_device *) (((u8 *) info) -
3646 offsetof(struct p2p_device, info));
3647
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003648 pos = buf;
3649 end = buf + buflen;
3650
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003651 os_get_time(&now);
3652 res = os_snprintf(pos, end - pos,
3653 "age=%d\n"
3654 "listen_freq=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003655 "wps_method=%s\n"
3656 "interface_addr=" MACSTR "\n"
3657 "member_in_go_dev=" MACSTR "\n"
3658 "member_in_go_iface=" MACSTR "\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003659 "go_neg_req_sent=%d\n"
3660 "go_state=%s\n"
3661 "dialog_token=%u\n"
3662 "intended_addr=" MACSTR "\n"
3663 "country=%c%c\n"
3664 "oper_freq=%d\n"
3665 "req_config_methods=0x%x\n"
3666 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
3667 "status=%d\n"
3668 "wait_count=%u\n"
3669 "invitation_reqs=%u\n",
3670 (int) (now.sec - dev->last_seen.sec),
3671 dev->listen_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003672 p2p_wps_method_text(dev->wps_method),
3673 MAC2STR(dev->interface_addr),
3674 MAC2STR(dev->member_in_go_dev),
3675 MAC2STR(dev->member_in_go_iface),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003676 dev->go_neg_req_sent,
3677 p2p_go_state_text(dev->go_state),
3678 dev->dialog_token,
3679 MAC2STR(dev->intended_addr),
3680 dev->country[0] ? dev->country[0] : '_',
3681 dev->country[1] ? dev->country[1] : '_',
3682 dev->oper_freq,
3683 dev->req_config_methods,
3684 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
3685 "[PROBE_REQ_ONLY]" : "",
3686 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
3687 dev->flags & P2P_DEV_NOT_YET_READY ?
3688 "[NOT_YET_READY]" : "",
3689 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
3690 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
3691 "",
3692 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
3693 "[PD_PEER_DISPLAY]" : "",
3694 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
3695 "[PD_PEER_KEYPAD]" : "",
3696 dev->flags & P2P_DEV_USER_REJECTED ?
3697 "[USER_REJECTED]" : "",
3698 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
3699 "[PEER_WAITING_RESPONSE]" : "",
3700 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
3701 "[PREFER_PERSISTENT_GROUP]" : "",
3702 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
3703 "[WAIT_GO_NEG_RESPONSE]" : "",
3704 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
3705 "[WAIT_GO_NEG_CONFIRM]" : "",
3706 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
3707 "[GROUP_CLIENT_ONLY]" : "",
3708 dev->flags & P2P_DEV_FORCE_FREQ ?
3709 "[FORCE_FREQ]" : "",
3710 dev->flags & P2P_DEV_PD_FOR_JOIN ?
3711 "[PD_FOR_JOIN]" : "",
3712 dev->status,
3713 dev->wait_count,
3714 dev->invitation_reqs);
3715 if (res < 0 || res >= end - pos)
3716 return pos - buf;
3717 pos += res;
3718
3719 if (dev->ext_listen_period) {
3720 res = os_snprintf(pos, end - pos,
3721 "ext_listen_period=%u\n"
3722 "ext_listen_interval=%u\n",
3723 dev->ext_listen_period,
3724 dev->ext_listen_interval);
3725 if (res < 0 || res >= end - pos)
3726 return pos - buf;
3727 pos += res;
3728 }
3729
3730 if (dev->oper_ssid_len) {
3731 res = os_snprintf(pos, end - pos,
3732 "oper_ssid=%s\n",
3733 wpa_ssid_txt(dev->oper_ssid,
3734 dev->oper_ssid_len));
3735 if (res < 0 || res >= end - pos)
3736 return pos - buf;
3737 pos += res;
3738 }
3739
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003740#ifdef CONFIG_WIFI_DISPLAY
3741 if (dev->info.wfd_subelems) {
3742 res = os_snprintf(pos, end - pos, "wfd_subelems=");
3743 if (res < 0 || res >= end - pos)
3744 return pos - buf;
3745 pos += res;
3746
3747 pos += wpa_snprintf_hex(pos, end - pos,
3748 wpabuf_head(dev->info.wfd_subelems),
3749 wpabuf_len(dev->info.wfd_subelems));
3750
3751 res = os_snprintf(pos, end - pos, "\n");
3752 if (res < 0 || res >= end - pos)
3753 return pos - buf;
3754 pos += res;
3755 }
3756#endif /* CONFIG_WIFI_DISPLAY */
3757
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003758 return pos - buf;
3759}
3760
3761
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003762int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
3763{
3764 return p2p_get_device(p2p, addr) != NULL;
3765}
3766
3767
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003768void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
3769{
3770 if (enabled) {
3771 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3772 "discoverability enabled");
3773 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3774 } else {
3775 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3776 "discoverability disabled");
3777 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3778 }
3779}
3780
3781
3782static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
3783 u32 duration2, u32 interval2)
3784{
3785 struct wpabuf *req;
3786 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
3787 u8 *len;
3788
3789 req = wpabuf_alloc(100);
3790 if (req == NULL)
3791 return NULL;
3792
3793 if (duration1 || interval1) {
3794 os_memset(&desc1, 0, sizeof(desc1));
3795 desc1.count_type = 1;
3796 desc1.duration = duration1;
3797 desc1.interval = interval1;
3798 ptr1 = &desc1;
3799
3800 if (duration2 || interval2) {
3801 os_memset(&desc2, 0, sizeof(desc2));
3802 desc2.count_type = 2;
3803 desc2.duration = duration2;
3804 desc2.interval = interval2;
3805 ptr2 = &desc2;
3806 }
3807 }
3808
3809 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
3810 len = p2p_buf_add_ie_hdr(req);
3811 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
3812 p2p_buf_update_ie_hdr(req, len);
3813
3814 return req;
3815}
3816
3817
3818int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
3819 const u8 *own_interface_addr, unsigned int freq,
3820 u32 duration1, u32 interval1, u32 duration2,
3821 u32 interval2)
3822{
3823 struct wpabuf *req;
3824
3825 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
3826 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
3827 "int1=%u dur2=%u int2=%u",
3828 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
3829 freq, duration1, interval1, duration2, interval2);
3830
3831 req = p2p_build_presence_req(duration1, interval1, duration2,
3832 interval2);
3833 if (req == NULL)
3834 return -1;
3835
3836 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3837 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3838 go_interface_addr,
3839 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
3840 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3841 "P2P: Failed to send Action frame");
3842 }
3843 wpabuf_free(req);
3844
3845 return 0;
3846}
3847
3848
3849static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3850 size_t noa_len, u8 dialog_token)
3851{
3852 struct wpabuf *resp;
3853 u8 *len;
3854
3855 resp = wpabuf_alloc(100 + noa_len);
3856 if (resp == NULL)
3857 return NULL;
3858
3859 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3860 len = p2p_buf_add_ie_hdr(resp);
3861 p2p_buf_add_status(resp, status);
3862 if (noa) {
3863 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3864 wpabuf_put_le16(resp, noa_len);
3865 wpabuf_put_data(resp, noa, noa_len);
3866 } else
3867 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3868 p2p_buf_update_ie_hdr(resp, len);
3869
3870 return resp;
3871}
3872
3873
3874static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3875 const u8 *sa, const u8 *data, size_t len,
3876 int rx_freq)
3877{
3878 struct p2p_message msg;
3879 u8 status;
3880 struct wpabuf *resp;
3881 size_t g;
3882 struct p2p_group *group = NULL;
3883 int parsed = 0;
3884 u8 noa[50];
3885 int noa_len;
3886
3887 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3888 "P2P: Received P2P Action - P2P Presence Request");
3889
3890 for (g = 0; g < p2p->num_groups; g++) {
3891 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3892 ETH_ALEN) == 0) {
3893 group = p2p->groups[g];
3894 break;
3895 }
3896 }
3897 if (group == NULL) {
3898 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3899 "P2P: Ignore P2P Presence Request for unknown group "
3900 MACSTR, MAC2STR(da));
3901 return;
3902 }
3903
3904 if (p2p_parse(data, len, &msg) < 0) {
3905 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3906 "P2P: Failed to parse P2P Presence Request");
3907 status = P2P_SC_FAIL_INVALID_PARAMS;
3908 goto fail;
3909 }
3910 parsed = 1;
3911
3912 if (msg.noa == NULL) {
3913 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3914 "P2P: No NoA attribute in P2P Presence Request");
3915 status = P2P_SC_FAIL_INVALID_PARAMS;
3916 goto fail;
3917 }
3918
3919 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3920
3921fail:
3922 if (p2p->cfg->get_noa)
3923 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3924 sizeof(noa));
3925 else
3926 noa_len = -1;
3927 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3928 noa_len > 0 ? noa_len : 0,
3929 msg.dialog_token);
3930 if (parsed)
3931 p2p_parse_free(&msg);
3932 if (resp == NULL)
3933 return;
3934
3935 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3936 if (p2p_send_action(p2p, rx_freq, sa, da, da,
3937 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
3938 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3939 "P2P: Failed to send Action frame");
3940 }
3941 wpabuf_free(resp);
3942}
3943
3944
3945static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3946 const u8 *sa, const u8 *data, size_t len)
3947{
3948 struct p2p_message msg;
3949
3950 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3951 "P2P: Received P2P Action - P2P Presence Response");
3952
3953 if (p2p_parse(data, len, &msg) < 0) {
3954 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3955 "P2P: Failed to parse P2P Presence Response");
3956 return;
3957 }
3958
3959 if (msg.status == NULL || msg.noa == NULL) {
3960 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3961 "P2P: No Status or NoA attribute in P2P Presence "
3962 "Response");
3963 p2p_parse_free(&msg);
3964 return;
3965 }
3966
3967 if (*msg.status) {
3968 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3969 "P2P: P2P Presence Request was rejected: status %u",
3970 *msg.status);
3971 p2p_parse_free(&msg);
3972 return;
3973 }
3974
3975 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3976 "P2P: P2P Presence Request was accepted");
3977 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
3978 msg.noa, msg.noa_len);
3979 /* TODO: process NoA */
3980 p2p_parse_free(&msg);
3981}
3982
3983
3984static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3985{
3986 struct p2p_data *p2p = eloop_ctx;
3987
3988 if (p2p->ext_listen_interval) {
3989 /* Schedule next extended listen timeout */
3990 eloop_register_timeout(p2p->ext_listen_interval_sec,
3991 p2p->ext_listen_interval_usec,
3992 p2p_ext_listen_timeout, p2p, NULL);
3993 }
3994
3995 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
3996 /*
3997 * This should not really happen, but it looks like the Listen
3998 * command may fail is something else (e.g., a scan) was
3999 * running at an inconvenient time. As a workaround, allow new
4000 * Extended Listen operation to be started.
4001 */
4002 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
4003 "Extended Listen operation had not been completed - "
4004 "try again");
4005 p2p->ext_listen_only = 0;
4006 p2p_set_state(p2p, P2P_IDLE);
4007 }
4008
4009 if (p2p->state != P2P_IDLE) {
4010 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
4011 "Listen timeout in active state (%s)",
4012 p2p_state_txt(p2p->state));
4013 return;
4014 }
4015
4016 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
4017 p2p->ext_listen_only = 1;
4018 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
4019 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
4020 "Listen state for Extended Listen Timing");
4021 p2p->ext_listen_only = 0;
4022 }
4023}
4024
4025
4026int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
4027 unsigned int interval)
4028{
4029 if (period > 65535 || interval > 65535 || period > interval ||
4030 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
4031 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
4032 "P2P: Invalid Extended Listen Timing request: "
4033 "period=%u interval=%u", period, interval);
4034 return -1;
4035 }
4036
4037 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
4038
4039 if (interval == 0) {
4040 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
4041 "P2P: Disabling Extended Listen Timing");
4042 p2p->ext_listen_period = 0;
4043 p2p->ext_listen_interval = 0;
4044 return 0;
4045 }
4046
4047 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
4048 "P2P: Enabling Extended Listen Timing: period %u msec, "
4049 "interval %u msec", period, interval);
4050 p2p->ext_listen_period = period;
4051 p2p->ext_listen_interval = interval;
4052 p2p->ext_listen_interval_sec = interval / 1000;
4053 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
4054
4055 eloop_register_timeout(p2p->ext_listen_interval_sec,
4056 p2p->ext_listen_interval_usec,
4057 p2p_ext_listen_timeout, p2p, NULL);
4058
4059 return 0;
4060}
4061
4062
4063void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4064 const u8 *ie, size_t ie_len)
4065{
4066 struct p2p_message msg;
4067
4068 if (bssid == NULL || ie == NULL)
4069 return;
4070
4071 os_memset(&msg, 0, sizeof(msg));
4072 if (p2p_parse_ies(ie, ie_len, &msg))
4073 return;
4074 if (msg.minor_reason_code == NULL)
4075 return;
4076
4077 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
4078 "P2P: Deauthentication notification BSSID " MACSTR
4079 " reason_code=%u minor_reason_code=%u",
4080 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4081
4082 p2p_parse_free(&msg);
4083}
4084
4085
4086void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4087 const u8 *ie, size_t ie_len)
4088{
4089 struct p2p_message msg;
4090
4091 if (bssid == NULL || ie == NULL)
4092 return;
4093
4094 os_memset(&msg, 0, sizeof(msg));
4095 if (p2p_parse_ies(ie, ie_len, &msg))
4096 return;
4097 if (msg.minor_reason_code == NULL)
4098 return;
4099
4100 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
4101 "P2P: Disassociation notification BSSID " MACSTR
4102 " reason_code=%u minor_reason_code=%u",
4103 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4104
4105 p2p_parse_free(&msg);
4106}
4107
4108
4109void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
4110{
4111 if (enabled) {
4112 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
4113 "Device operations enabled");
4114 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
4115 } else {
4116 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
4117 "Device operations disabled");
4118 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
4119 }
4120}
4121
4122
4123int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
4124{
4125 if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
4126 return -1;
4127
4128 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
4129 "reg_class %u channel %u", reg_class, channel);
4130 p2p->cfg->reg_class = reg_class;
4131 p2p->cfg->channel = channel;
4132
4133 return 0;
4134}
4135
4136
4137int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
4138{
4139 wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
4140 if (postfix == NULL) {
4141 p2p->cfg->ssid_postfix_len = 0;
4142 return 0;
4143 }
4144 if (len > sizeof(p2p->cfg->ssid_postfix))
4145 return -1;
4146 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4147 p2p->cfg->ssid_postfix_len = len;
4148 return 0;
4149}
4150
4151
Jouni Malinen75ecf522011-06-27 15:19:46 -07004152int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4153 int cfg_op_channel)
4154{
4155 if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
4156 < 0)
4157 return -1;
4158
4159 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
4160 "reg_class %u channel %u", op_reg_class, op_channel);
4161 p2p->cfg->op_reg_class = op_reg_class;
4162 p2p->cfg->op_channel = op_channel;
4163 p2p->cfg->cfg_op_channel = cfg_op_channel;
4164 return 0;
4165}
4166
4167
Dmitry Shmidt04949592012-07-19 12:16:46 -07004168int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4169 const struct p2p_channel *pref_chan)
4170{
4171 struct p2p_channel *n;
4172
4173 if (pref_chan) {
4174 n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
4175 if (n == NULL)
4176 return -1;
4177 os_memcpy(n, pref_chan,
4178 num_pref_chan * sizeof(struct p2p_channel));
4179 } else
4180 n = NULL;
4181
4182 os_free(p2p->cfg->pref_chan);
4183 p2p->cfg->pref_chan = n;
4184 p2p->cfg->num_pref_chan = num_pref_chan;
4185
4186 return 0;
4187}
4188
4189
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004190int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4191 u8 *iface_addr)
4192{
4193 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4194 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4195 return -1;
4196 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4197 return 0;
4198}
4199
4200
4201int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4202 u8 *dev_addr)
4203{
4204 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4205 if (dev == NULL)
4206 return -1;
4207 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4208 return 0;
4209}
4210
4211
4212void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4213{
4214 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4215 if (is_zero_ether_addr(p2p->peer_filter))
4216 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
4217 "filter");
4218 else
4219 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
4220 "filter for " MACSTR, MAC2STR(p2p->peer_filter));
4221}
4222
4223
4224void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4225{
4226 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
4227 enabled ? "enabled" : "disabled");
4228 if (p2p->cross_connect == enabled)
4229 return;
4230 p2p->cross_connect = enabled;
4231 /* TODO: may need to tear down any action group where we are GO(?) */
4232}
4233
4234
4235int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4236{
4237 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4238 if (dev == NULL)
4239 return -1;
4240 if (dev->oper_freq <= 0)
4241 return -1;
4242 return dev->oper_freq;
4243}
4244
4245
4246void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4247{
4248 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
4249 enabled ? "enabled" : "disabled");
4250 p2p->cfg->p2p_intra_bss = enabled;
4251}
4252
4253
4254void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
4255{
4256 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
4257 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
4258}
4259
4260
4261int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4262 const u8 *src, const u8 *bssid, const u8 *buf,
4263 size_t len, unsigned int wait_time)
4264{
4265 if (p2p->p2p_scan_running) {
4266 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
4267 "frame TX until p2p_scan completes");
4268 if (p2p->after_scan_tx) {
4269 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
4270 "previous pending Action frame TX");
4271 os_free(p2p->after_scan_tx);
4272 }
4273 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
4274 len);
4275 if (p2p->after_scan_tx == NULL)
4276 return -1;
4277 p2p->after_scan_tx->freq = freq;
4278 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
4279 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
4280 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
4281 p2p->after_scan_tx->len = len;
4282 p2p->after_scan_tx->wait_time = wait_time;
4283 os_memcpy(p2p->after_scan_tx + 1, buf, len);
4284 return 0;
4285 }
4286
4287 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4288 buf, len, wait_time);
4289}
4290
4291
4292void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4293 int freq_overall)
4294{
4295 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
4296 " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall);
4297 p2p->best_freq_24 = freq_24;
4298 p2p->best_freq_5 = freq_5;
4299 p2p->best_freq_overall = freq_overall;
4300}
4301
4302
4303const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4304{
4305 if (p2p == NULL || p2p->go_neg_peer == NULL)
4306 return NULL;
4307 return p2p->go_neg_peer->info.p2p_device_addr;
4308}
4309
4310
4311const struct p2p_peer_info *
4312p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4313{
4314 struct p2p_device *dev;
4315
4316 if (addr) {
4317 dev = p2p_get_device(p2p, addr);
4318 if (!dev)
4319 return NULL;
4320
4321 if (!next) {
4322 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
4323 return NULL;
4324
4325 return &dev->info;
4326 } else {
4327 do {
4328 dev = dl_list_first(&dev->list,
4329 struct p2p_device,
4330 list);
4331 if (&dev->list == &p2p->devices)
4332 return NULL;
4333 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
4334 }
4335 } else {
4336 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4337 if (!dev)
4338 return NULL;
4339 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
4340 dev = dl_list_first(&dev->list,
4341 struct p2p_device,
4342 list);
4343 if (&dev->list == &p2p->devices)
4344 return NULL;
4345 }
4346 }
4347
4348 return &dev->info;
4349}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004350
4351#ifdef ANDROID_P2P
4352int p2p_search_in_progress(struct p2p_data *p2p)
4353{
4354 if (p2p == NULL)
4355 return 0;
4356
4357 return p2p->state == P2P_SEARCH;
4358}
4359#endif
4360
4361int p2p_in_progress(struct p2p_data *p2p)
4362{
4363 if (p2p == NULL)
4364 return 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004365 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SEARCH_WHEN_READY ||
4366 p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY)
4367 return 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004368 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
4369}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004370
4371
4372void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
4373 u8 client_timeout)
4374{
4375 if (p2p) {
4376 p2p->go_timeout = go_timeout;
4377 p2p->client_timeout = client_timeout;
4378 }
4379}
4380
4381
4382void p2p_increase_search_delay(struct p2p_data *p2p, unsigned int delay)
4383{
4384 if (p2p && p2p->search_delay < delay)
4385 p2p->search_delay = delay;
4386}
4387
4388
4389#ifdef CONFIG_WIFI_DISPLAY
4390
4391static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
4392{
4393 size_t g;
4394 struct p2p_group *group;
4395
4396 for (g = 0; g < p2p->num_groups; g++) {
4397 group = p2p->groups[g];
4398 p2p_group_update_ies(group);
4399 }
4400}
4401
4402
4403int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
4404{
4405 wpabuf_free(p2p->wfd_ie_beacon);
4406 p2p->wfd_ie_beacon = ie;
4407 p2p_update_wfd_ie_groups(p2p);
4408 return 0;
4409}
4410
4411
4412int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
4413{
4414 wpabuf_free(p2p->wfd_ie_probe_req);
4415 p2p->wfd_ie_probe_req = ie;
4416 return 0;
4417}
4418
4419
4420int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
4421{
4422 wpabuf_free(p2p->wfd_ie_probe_resp);
4423 p2p->wfd_ie_probe_resp = ie;
4424 p2p_update_wfd_ie_groups(p2p);
4425 return 0;
4426}
4427
4428
4429int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
4430{
4431 wpabuf_free(p2p->wfd_ie_assoc_req);
4432 p2p->wfd_ie_assoc_req = ie;
4433 return 0;
4434}
4435
4436
4437int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
4438{
4439 wpabuf_free(p2p->wfd_ie_invitation);
4440 p2p->wfd_ie_invitation = ie;
4441 return 0;
4442}
4443
4444
4445int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
4446{
4447 wpabuf_free(p2p->wfd_ie_prov_disc_req);
4448 p2p->wfd_ie_prov_disc_req = ie;
4449 return 0;
4450}
4451
4452
4453int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
4454{
4455 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
4456 p2p->wfd_ie_prov_disc_resp = ie;
4457 return 0;
4458}
4459
4460
4461int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
4462{
4463 wpabuf_free(p2p->wfd_ie_go_neg);
4464 p2p->wfd_ie_go_neg = ie;
4465 return 0;
4466}
4467
4468
4469int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
4470{
4471 wpabuf_free(p2p->wfd_dev_info);
4472 if (elem) {
4473 p2p->wfd_dev_info = wpabuf_dup(elem);
4474 if (p2p->wfd_dev_info == NULL)
4475 return -1;
4476 } else
4477 p2p->wfd_dev_info = NULL;
4478
4479 return 0;
4480}
4481
4482
4483int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
4484{
4485 wpabuf_free(p2p->wfd_assoc_bssid);
4486 if (elem) {
4487 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
4488 if (p2p->wfd_assoc_bssid == NULL)
4489 return -1;
4490 } else
4491 p2p->wfd_assoc_bssid = NULL;
4492
4493 return 0;
4494}
4495
4496
4497int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
4498 const struct wpabuf *elem)
4499{
4500 wpabuf_free(p2p->wfd_coupled_sink_info);
4501 if (elem) {
4502 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
4503 if (p2p->wfd_coupled_sink_info == NULL)
4504 return -1;
4505 } else
4506 p2p->wfd_coupled_sink_info = NULL;
4507
4508 return 0;
4509}
4510
4511#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004512
4513
4514int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
4515 int max_disc_tu)
4516{
4517 if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
4518 return -1;
4519
4520 p2p->min_disc_int = min_disc_int;
4521 p2p->max_disc_int = max_disc_int;
4522 p2p->max_disc_tu = max_disc_tu;
4523 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set discoverable interval: "
4524 "min=%d max=%d max_tu=%d", min_disc_int, max_disc_int,
4525 max_disc_tu);
4526
4527 return 0;
4528}