blob: f3e31a80ef60321edda7a5a1c0a5c237ea0c607a [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 Shmidt2e67f062014-07-16 09:55:28 -070015#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 Shmidt2093d062014-01-17 10:58:50 -080045#ifndef P2P_PEER_EXPIRATION_AGE
46#define P2P_PEER_EXPIRATION_AGE 60
Dmitry Shmidt18463232014-01-24 12:29:41 -080047#endif /* P2P_PEER_EXPIRATION_AGE */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070048
49#define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
50
51static void p2p_expire_peers(struct p2p_data *p2p)
52{
53 struct p2p_device *dev, *n;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080054 struct os_reltime now;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080055 size_t i;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070056
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080057 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070058 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
59 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
60 continue;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080061
Dmitry Shmidt9e3f8ee2014-01-17 10:52:01 -080062 if (dev == p2p->go_neg_peer) {
63 /*
64 * GO Negotiation is in progress with the peer, so
65 * don't expire the peer entry until GO Negotiation
66 * fails or times out.
67 */
68 continue;
69 }
70
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080071 if (p2p->cfg->go_connected &&
72 p2p->cfg->go_connected(p2p->cfg->cb_ctx,
73 dev->info.p2p_device_addr)) {
74 /*
75 * We are connected as a client to a group in which the
76 * peer is the GO, so do not expire the peer entry.
77 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080078 os_get_reltime(&dev->last_seen);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080079 continue;
80 }
81
82 for (i = 0; i < p2p->num_groups; i++) {
83 if (p2p_group_is_client_connected(
84 p2p->groups[i], dev->info.p2p_device_addr))
85 break;
86 }
87 if (i < p2p->num_groups) {
88 /*
89 * The peer is connected as a client in a group where
90 * we are the GO, so do not expire the peer entry.
91 */
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -080092 os_get_reltime(&dev->last_seen);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -080093 continue;
94 }
95
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -070096 p2p_dbg(p2p, "Expiring old peer entry " MACSTR,
97 MAC2STR(dev->info.p2p_device_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -070098 dl_list_del(&dev->list);
99 p2p_device_free(p2p, dev);
100 }
101}
102
103
104static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
105{
106 struct p2p_data *p2p = eloop_ctx;
107 p2p_expire_peers(p2p);
108 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
109 p2p_expiration_timeout, p2p, NULL);
110}
111
112
113static const char * p2p_state_txt(int state)
114{
115 switch (state) {
116 case P2P_IDLE:
117 return "IDLE";
118 case P2P_SEARCH:
119 return "SEARCH";
120 case P2P_CONNECT:
121 return "CONNECT";
122 case P2P_CONNECT_LISTEN:
123 return "CONNECT_LISTEN";
124 case P2P_GO_NEG:
125 return "GO_NEG";
126 case P2P_LISTEN_ONLY:
127 return "LISTEN_ONLY";
128 case P2P_WAIT_PEER_CONNECT:
129 return "WAIT_PEER_CONNECT";
130 case P2P_WAIT_PEER_IDLE:
131 return "WAIT_PEER_IDLE";
132 case P2P_SD_DURING_FIND:
133 return "SD_DURING_FIND";
134 case P2P_PROVISIONING:
135 return "PROVISIONING";
136 case P2P_PD_DURING_FIND:
137 return "PD_DURING_FIND";
138 case P2P_INVITE:
139 return "INVITE";
140 case P2P_INVITE_LISTEN:
141 return "INVITE_LISTEN";
142 default:
143 return "?";
144 }
145}
146
147
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -0700148const char * p2p_get_state_txt(struct p2p_data *p2p)
149{
150 return p2p_state_txt(p2p->state);
151}
152
153
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800154u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
155{
156 struct p2p_device *dev = NULL;
157
158 if (!addr || !p2p)
159 return 0;
160
161 dev = p2p_get_device(p2p, addr);
162 if (dev)
163 return dev->wps_prov_info;
164 else
165 return 0;
166}
167
168
Dmitry Shmidt04949592012-07-19 12:16:46 -0700169void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800170{
171 struct p2p_device *dev = NULL;
172
Dmitry Shmidt04949592012-07-19 12:16:46 -0700173 if (!addr || !p2p)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800174 return;
175
Dmitry Shmidt04949592012-07-19 12:16:46 -0700176 dev = p2p_get_device(p2p, addr);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800177 if (dev)
178 dev->wps_prov_info = 0;
179}
180
181
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700182void p2p_set_state(struct p2p_data *p2p, int new_state)
183{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700184 p2p_dbg(p2p, "State %s -> %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700185 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
186 p2p->state = new_state;
Dmitry Shmidt43cb5782014-06-16 16:23:22 -0700187
188 if (new_state == P2P_IDLE && p2p->pending_channel) {
189 p2p_dbg(p2p, "Apply change in listen channel");
190 p2p->cfg->reg_class = p2p->pending_reg_class;
191 p2p->cfg->channel = p2p->pending_channel;
192 p2p->pending_reg_class = 0;
193 p2p->pending_channel = 0;
194 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700195}
196
197
198void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
199{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700200 p2p_dbg(p2p, "Set timeout (state=%s): %u.%06u sec",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700201 p2p_state_txt(p2p->state), sec, usec);
202 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
203 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
204}
205
206
207void p2p_clear_timeout(struct p2p_data *p2p)
208{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700209 p2p_dbg(p2p, "Clear timeout (state=%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700210 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
211}
212
213
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800214void p2p_go_neg_failed(struct p2p_data *p2p, int status)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700215{
216 struct p2p_go_neg_results res;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800217 struct p2p_device *peer = p2p->go_neg_peer;
218
219 if (!peer)
220 return;
221
222 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
223 if (p2p->state != P2P_SEARCH) {
224 /*
225 * Clear timeouts related to GO Negotiation if no new p2p_find
226 * has been started.
227 */
228 p2p_clear_timeout(p2p);
229 p2p_set_state(p2p, P2P_IDLE);
Dmitry Shmidt8c652892013-03-01 10:14:01 -0800230 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800231
232 peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
233 peer->wps_method = WPS_NOT_READY;
234 peer->oob_pw_id = 0;
235 wpabuf_free(peer->go_neg_conf);
236 peer->go_neg_conf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700237 p2p->go_neg_peer = NULL;
238
239 os_memset(&res, 0, sizeof(res));
240 res.status = status;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800241 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
242 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700243 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
244}
245
246
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800247static void p2p_listen_in_find(struct p2p_data *p2p, int dev_disc)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700248{
249 unsigned int r, tu;
250 int freq;
251 struct wpabuf *ies;
252
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700253 p2p_dbg(p2p, "Starting short listen state (state=%s)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700254 p2p_state_txt(p2p->state));
255
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700256 if (p2p->pending_listen_freq) {
257 /* We have a pending p2p_listen request */
258 p2p_dbg(p2p, "p2p_listen command pending already");
259 return;
260 }
261
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700262 freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700263 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700264 p2p_dbg(p2p, "Unknown regulatory class/channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700265 return;
266 }
267
Dmitry Shmidt661b4f72014-09-29 14:58:27 -0700268 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
269 r = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700270 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
271 p2p->min_disc_int) * 100;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800272 if (p2p->max_disc_tu >= 0 && tu > (unsigned int) p2p->max_disc_tu)
273 tu = p2p->max_disc_tu;
274 if (!dev_disc && tu < 100)
275 tu = 100; /* Need to wait in non-device discovery use cases */
276 if (p2p->cfg->max_listen && 1024 * tu / 1000 > p2p->cfg->max_listen)
277 tu = p2p->cfg->max_listen * 1000 / 1024;
278
279 if (tu == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700280 p2p_dbg(p2p, "Skip listen state since duration was 0 TU");
Dmitry Shmidtd5e49232012-12-03 15:08:10 -0800281 p2p_set_timeout(p2p, 0, 0);
282 return;
283 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700284
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700285 ies = p2p_build_probe_resp_ies(p2p);
286 if (ies == NULL)
287 return;
288
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700289 p2p->pending_listen_freq = freq;
290 p2p->pending_listen_sec = 0;
291 p2p->pending_listen_usec = 1024 * tu;
292
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700293 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
294 ies) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700295 p2p_dbg(p2p, "Failed to start listen mode");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700296 p2p->pending_listen_freq = 0;
297 }
298 wpabuf_free(ies);
299}
300
301
302int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
303{
304 int freq;
305 struct wpabuf *ies;
306
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700307 p2p_dbg(p2p, "Going to listen(only) state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700308
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700309 if (p2p->pending_listen_freq) {
310 /* We have a pending p2p_listen request */
311 p2p_dbg(p2p, "p2p_listen command pending already");
312 return -1;
313 }
314
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700315 freq = p2p_channel_to_freq(p2p->cfg->reg_class, p2p->cfg->channel);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700316 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700317 p2p_dbg(p2p, "Unknown regulatory class/channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700318 return -1;
319 }
320
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700321 p2p->pending_listen_sec = timeout / 1000;
322 p2p->pending_listen_usec = (timeout % 1000) * 1000;
323
324 if (p2p->p2p_scan_running) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700325 if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700326 p2p_dbg(p2p, "p2p_scan running - connect is already pending - skip listen");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800327 return 0;
328 }
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700329 p2p_dbg(p2p, "p2p_scan running - delay start of listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700330 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
331 return 0;
332 }
333
334 ies = p2p_build_probe_resp_ies(p2p);
335 if (ies == NULL)
336 return -1;
337
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -0700338 p2p->pending_listen_freq = freq;
339
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700340 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700341 p2p_dbg(p2p, "Failed to start listen mode");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700342 p2p->pending_listen_freq = 0;
343 wpabuf_free(ies);
344 return -1;
345 }
346 wpabuf_free(ies);
347
348 p2p_set_state(p2p, P2P_LISTEN_ONLY);
349
350 return 0;
351}
352
353
354static void p2p_device_clear_reported(struct p2p_data *p2p)
355{
356 struct p2p_device *dev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800357 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700358 dev->flags &= ~P2P_DEV_REPORTED;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800359 dev->sd_reqs = 0;
360 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700361}
362
363
364/**
365 * p2p_get_device - Fetch a peer entry
366 * @p2p: P2P module context from p2p_init()
367 * @addr: P2P Device Address of the peer
368 * Returns: Pointer to the device entry or %NULL if not found
369 */
370struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
371{
372 struct p2p_device *dev;
373 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
374 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
375 return dev;
376 }
377 return NULL;
378}
379
380
381/**
382 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
383 * @p2p: P2P module context from p2p_init()
384 * @addr: P2P Interface Address of the peer
385 * Returns: Pointer to the device entry or %NULL if not found
386 */
387struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
388 const u8 *addr)
389{
390 struct p2p_device *dev;
391 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
392 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
393 return dev;
394 }
395 return NULL;
396}
397
398
399/**
400 * p2p_create_device - Create a peer entry
401 * @p2p: P2P module context from p2p_init()
402 * @addr: P2P Device Address of the peer
403 * Returns: Pointer to the device entry or %NULL on failure
404 *
405 * If there is already an entry for the peer, it will be returned instead of
406 * creating a new one.
407 */
408static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
409 const u8 *addr)
410{
411 struct p2p_device *dev, *oldest = NULL;
412 size_t count = 0;
413
414 dev = p2p_get_device(p2p, addr);
415 if (dev)
416 return dev;
417
418 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
419 count++;
420 if (oldest == NULL ||
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800421 os_reltime_before(&dev->last_seen, &oldest->last_seen))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700422 oldest = dev;
423 }
424 if (count + 1 > p2p->cfg->max_peers && oldest) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700425 p2p_dbg(p2p, "Remove oldest peer entry to make room for a new peer");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700426 dl_list_del(&oldest->list);
427 p2p_device_free(p2p, oldest);
428 }
429
430 dev = os_zalloc(sizeof(*dev));
431 if (dev == NULL)
432 return NULL;
433 dl_list_add(&p2p->devices, &dev->list);
434 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
435
436 return dev;
437}
438
439
440static void p2p_copy_client_info(struct p2p_device *dev,
441 struct p2p_client_info *cli)
442{
443 os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
444 dev->info.device_name[cli->dev_name_len] = '\0';
445 dev->info.dev_capab = cli->dev_capab;
446 dev->info.config_methods = cli->config_methods;
447 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
448 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
449 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
450 dev->info.wps_sec_dev_type_list_len);
451}
452
453
454static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
455 const u8 *go_interface_addr, int freq,
456 const u8 *gi, size_t gi_len)
457{
458 struct p2p_group_info info;
459 size_t c;
460 struct p2p_device *dev;
461
462 if (gi == NULL)
463 return 0;
464
465 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
466 return -1;
467
468 /*
469 * Clear old data for this group; if the devices are still in the
470 * group, the information will be restored in the loop following this.
471 */
472 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800473 if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700474 ETH_ALEN) == 0) {
475 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
476 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
477 }
478 }
479
480 for (c = 0; c < info.num_clients; c++) {
481 struct p2p_client_info *cli = &info.client[c];
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800482 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
483 ETH_ALEN) == 0)
484 continue; /* ignore our own entry */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700485 dev = p2p_get_device(p2p, cli->p2p_device_addr);
486 if (dev) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700487 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
Dmitry Shmidt04949592012-07-19 12:16:46 -0700488 P2P_DEV_PROBE_REQ_ONLY)) {
489 /*
490 * Update information since we have not
491 * received this directly from the client.
492 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700493 p2p_copy_client_info(dev, cli);
Dmitry Shmidt04949592012-07-19 12:16:46 -0700494 } else {
495 /*
496 * Need to update P2P Client Discoverability
497 * flag since it is valid only in P2P Group
498 * Info attribute.
499 */
500 dev->info.dev_capab &=
501 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
502 dev->info.dev_capab |=
503 cli->dev_capab &
504 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
505 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700506 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
507 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
508 }
509 } else {
510 dev = p2p_create_device(p2p, cli->p2p_device_addr);
511 if (dev == NULL)
512 continue;
513 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
514 p2p_copy_client_info(dev, cli);
515 dev->oper_freq = freq;
516 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
517 dev->info.p2p_device_addr,
518 &dev->info, 1);
519 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
520 }
521
522 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
523 ETH_ALEN);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800524 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700525 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
526 os_memcpy(dev->member_in_go_iface, go_interface_addr,
527 ETH_ALEN);
528 }
529
530 return 0;
531}
532
533
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700534static void p2p_copy_wps_info(struct p2p_data *p2p, struct p2p_device *dev,
535 int probe_req, const struct p2p_message *msg)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700536{
537 os_memcpy(dev->info.device_name, msg->device_name,
538 sizeof(dev->info.device_name));
539
540 if (msg->manufacturer &&
541 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
542 os_memset(dev->info.manufacturer, 0,
543 sizeof(dev->info.manufacturer));
544 os_memcpy(dev->info.manufacturer, msg->manufacturer,
545 msg->manufacturer_len);
546 }
547
548 if (msg->model_name &&
549 msg->model_name_len < sizeof(dev->info.model_name)) {
550 os_memset(dev->info.model_name, 0,
551 sizeof(dev->info.model_name));
552 os_memcpy(dev->info.model_name, msg->model_name,
553 msg->model_name_len);
554 }
555
556 if (msg->model_number &&
557 msg->model_number_len < sizeof(dev->info.model_number)) {
558 os_memset(dev->info.model_number, 0,
559 sizeof(dev->info.model_number));
560 os_memcpy(dev->info.model_number, msg->model_number,
561 msg->model_number_len);
562 }
563
564 if (msg->serial_number &&
565 msg->serial_number_len < sizeof(dev->info.serial_number)) {
566 os_memset(dev->info.serial_number, 0,
567 sizeof(dev->info.serial_number));
568 os_memcpy(dev->info.serial_number, msg->serial_number,
569 msg->serial_number_len);
570 }
571
572 if (msg->pri_dev_type)
573 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
574 sizeof(dev->info.pri_dev_type));
575 else if (msg->wps_pri_dev_type)
576 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
577 sizeof(dev->info.pri_dev_type));
578
579 if (msg->wps_sec_dev_type_list) {
580 os_memcpy(dev->info.wps_sec_dev_type_list,
581 msg->wps_sec_dev_type_list,
582 msg->wps_sec_dev_type_list_len);
583 dev->info.wps_sec_dev_type_list_len =
584 msg->wps_sec_dev_type_list_len;
585 }
586
587 if (msg->capability) {
Dmitry Shmidt04949592012-07-19 12:16:46 -0700588 /*
589 * P2P Client Discoverability bit is reserved in all frames
590 * that use this function, so do not change its value here.
591 */
592 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
593 dev->info.dev_capab |= msg->capability[0] &
594 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700595 dev->info.group_capab = msg->capability[1];
596 }
597
598 if (msg->ext_listen_timing) {
599 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
600 dev->ext_listen_interval =
601 WPA_GET_LE16(msg->ext_listen_timing + 2);
602 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700603
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700604 if (!probe_req) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800605 u16 new_config_methods;
606 new_config_methods = msg->config_methods ?
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700607 msg->config_methods : msg->wps_config_methods;
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800608 if (new_config_methods &&
609 dev->info.config_methods != new_config_methods) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700610 p2p_dbg(p2p, "Update peer " MACSTR
611 " config_methods 0x%x -> 0x%x",
612 MAC2STR(dev->info.p2p_device_addr),
613 dev->info.config_methods,
614 new_config_methods);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800615 dev->info.config_methods = new_config_methods;
616 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700617 }
618}
619
620
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700621static void p2p_update_peer_vendor_elems(struct p2p_device *dev, const u8 *ies,
622 size_t ies_len)
623{
624 const u8 *pos, *end;
625 u8 id, len;
626
627 wpabuf_free(dev->info.vendor_elems);
628 dev->info.vendor_elems = NULL;
629
630 end = ies + ies_len;
631
632 for (pos = ies; pos + 1 < end; pos += len) {
633 id = *pos++;
634 len = *pos++;
635
636 if (pos + len > end)
637 break;
638
639 if (id != WLAN_EID_VENDOR_SPECIFIC || len < 3)
640 continue;
641
642 if (len >= 4) {
643 u32 type = WPA_GET_BE32(pos);
644
645 if (type == WPA_IE_VENDOR_TYPE ||
646 type == WMM_IE_VENDOR_TYPE ||
647 type == WPS_IE_VENDOR_TYPE ||
648 type == P2P_IE_VENDOR_TYPE ||
649 type == WFD_IE_VENDOR_TYPE)
650 continue;
651 }
652
653 /* Unknown vendor element - make raw IE data available */
654 if (wpabuf_resize(&dev->info.vendor_elems, 2 + len) < 0)
655 break;
656 wpabuf_put_data(dev->info.vendor_elems, pos - 2, 2 + len);
657 }
658}
659
660
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800661static int p2p_compare_wfd_info(struct p2p_device *dev,
662 const struct p2p_message *msg)
663{
664 if (dev->info.wfd_subelems && msg->wfd_subelems) {
665 if (dev->info.wfd_subelems->used != msg->wfd_subelems->used)
666 return 1;
667
668 return os_memcmp(dev->info.wfd_subelems->buf,
669 msg->wfd_subelems->buf,
670 dev->info.wfd_subelems->used);
671 }
672 if (dev->info.wfd_subelems || msg->wfd_subelems)
673 return 1;
674
675 return 0;
676}
677
678
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700679/**
Dmitry Shmidt04949592012-07-19 12:16:46 -0700680 * p2p_add_device - Add peer entries based on scan results or P2P frames
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700681 * @p2p: P2P module context from p2p_init()
682 * @addr: Source address of Beacon or Probe Response frame (may be either
683 * P2P Device Address or P2P Interface Address)
684 * @level: Signal level (signal strength of the received frame from the peer)
685 * @freq: Frequency on which the Beacon or Probe Response frame was received
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800686 * @rx_time: Time when the result was received
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700687 * @ies: IEs from the Beacon or Probe Response frame
688 * @ies_len: Length of ies buffer in octets
Dmitry Shmidt04949592012-07-19 12:16:46 -0700689 * @scan_res: Whether this was based on scan results
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700690 * Returns: 0 on success, -1 on failure
691 *
692 * If the scan result is for a GO, the clients in the group will also be added
693 * to the peer table. This function can also be used with some other frames
694 * like Provision Discovery Request that contains P2P Capability and P2P Device
695 * Info attributes.
696 */
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800697int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800698 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800699 size_t ies_len, int scan_res)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700700{
701 struct p2p_device *dev;
702 struct p2p_message msg;
703 const u8 *p2p_dev_addr;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800704 int wfd_changed;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700705 int i;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800706 struct os_reltime time_now;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700707
708 os_memset(&msg, 0, sizeof(msg));
709 if (p2p_parse_ies(ies, ies_len, &msg)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700710 p2p_dbg(p2p, "Failed to parse P2P IE for a device entry");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700711 p2p_parse_free(&msg);
712 return -1;
713 }
714
715 if (msg.p2p_device_addr)
716 p2p_dev_addr = msg.p2p_device_addr;
717 else if (msg.device_id)
718 p2p_dev_addr = msg.device_id;
719 else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700720 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700721 p2p_parse_free(&msg);
722 return -1;
723 }
724
725 if (!is_zero_ether_addr(p2p->peer_filter) &&
726 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700727 p2p_dbg(p2p, "Do not add peer filter for " MACSTR
728 " due to peer filter", MAC2STR(p2p_dev_addr));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800729 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700730 return 0;
731 }
732
733 dev = p2p_create_device(p2p, p2p_dev_addr);
734 if (dev == NULL) {
735 p2p_parse_free(&msg);
736 return -1;
737 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800738
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800739 if (rx_time == NULL) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800740 os_get_reltime(&time_now);
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800741 rx_time = &time_now;
742 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800743
744 /*
745 * Update the device entry only if the new peer
746 * entry is newer than the one previously stored.
747 */
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800748 if (dev->last_seen.sec > 0 &&
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800749 os_reltime_before(rx_time, &dev->last_seen)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700750 p2p_dbg(p2p, "Do not update peer entry based on old frame (rx_time=%u.%06u last_seen=%u.%06u)",
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800751 (unsigned int) rx_time->sec,
752 (unsigned int) rx_time->usec,
753 (unsigned int) dev->last_seen.sec,
754 (unsigned int) dev->last_seen.usec);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800755 p2p_parse_free(&msg);
756 return -1;
757 }
758
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -0800759 os_memcpy(&dev->last_seen, rx_time, sizeof(struct os_reltime));
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -0800760
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700761 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
762
763 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
764 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
765 if (msg.ssid &&
766 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
767 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
768 != 0)) {
769 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
770 dev->oper_ssid_len = msg.ssid[1];
771 }
772
773 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
774 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
775 int ds_freq;
776 if (*msg.ds_params == 14)
777 ds_freq = 2484;
778 else
779 ds_freq = 2407 + *msg.ds_params * 5;
780 if (freq != ds_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700781 p2p_dbg(p2p, "Update Listen frequency based on DS Parameter Set IE: %d -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700782 freq, ds_freq);
783 freq = ds_freq;
784 }
785 }
786
Dmitry Shmidt04949592012-07-19 12:16:46 -0700787 if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700788 p2p_dbg(p2p, "Update Listen frequency based on scan results ("
789 MACSTR " %d -> %d MHz (DS param %d)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700790 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
791 freq, msg.ds_params ? *msg.ds_params : -1);
792 }
Dmitry Shmidt04949592012-07-19 12:16:46 -0700793 if (scan_res) {
794 dev->listen_freq = freq;
795 if (msg.group_info)
796 dev->oper_freq = freq;
797 }
Jouni Malinen75ecf522011-06-27 15:19:46 -0700798 dev->info.level = level;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700799
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700800 p2p_copy_wps_info(p2p, dev, 0, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700801
802 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
803 wpabuf_free(dev->info.wps_vendor_ext[i]);
804 dev->info.wps_vendor_ext[i] = NULL;
805 }
806
807 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
808 if (msg.wps_vendor_ext[i] == NULL)
809 break;
810 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
811 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
812 if (dev->info.wps_vendor_ext[i] == NULL)
813 break;
814 }
815
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800816 wfd_changed = p2p_compare_wfd_info(dev, &msg);
817
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700818 if (msg.wfd_subelems) {
819 wpabuf_free(dev->info.wfd_subelems);
820 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
821 }
822
Dmitry Shmidt04949592012-07-19 12:16:46 -0700823 if (scan_res) {
824 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
825 msg.group_info, msg.group_info_len);
826 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700827
828 p2p_parse_free(&msg);
829
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700830 p2p_update_peer_vendor_elems(dev, ies, ies_len);
831
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800832 if (dev->flags & P2P_DEV_REPORTED && !wfd_changed)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700833 return 0;
834
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700835 p2p_dbg(p2p, "Peer found with Listen frequency %d MHz (rx_time=%u.%06u)",
836 freq, (unsigned int) rx_time->sec,
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800837 (unsigned int) rx_time->usec);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700838 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700839 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700840 return 0;
841 }
842
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800843 if (dev->info.config_methods == 0 &&
844 (freq == 2412 || freq == 2437 || freq == 2462)) {
845 /*
846 * If we have only seen a Beacon frame from a GO, we do not yet
847 * know what WPS config methods it supports. Since some
848 * applications use config_methods value from P2P-DEVICE-FOUND
849 * events, postpone reporting this peer until we've fully
850 * discovered its capabilities.
851 *
852 * At least for now, do this only if the peer was detected on
853 * one of the social channels since that peer can be easily be
854 * found again and there are no limitations of having to use
855 * passive scan on this channels, so this can be done through
856 * Probe Response frame that includes the config_methods
857 * information.
858 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700859 p2p_dbg(p2p, "Do not report peer " MACSTR
860 " with unknown config methods", MAC2STR(addr));
Dmitry Shmidtf8623282013-02-20 14:34:59 -0800861 return 0;
862 }
863
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700864 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
865 !(dev->flags & P2P_DEV_REPORTED_ONCE));
866 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
867
868 return 0;
869}
870
871
872static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
873{
874 int i;
875
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700876 if (p2p->go_neg_peer == dev) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -0800877 /*
878 * If GO Negotiation is in progress, report that it has failed.
879 */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800880 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt497c1d52011-07-21 15:19:46 -0700881 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700882 if (p2p->invite_peer == dev)
883 p2p->invite_peer = NULL;
884 if (p2p->sd_peer == dev)
885 p2p->sd_peer = NULL;
886 if (p2p->pending_client_disc_go == dev)
887 p2p->pending_client_disc_go = NULL;
888
Dmitry Shmidtc55524a2011-07-07 11:18:38 -0700889 /* dev_lost() device, but only if it was previously dev_found() */
890 if (dev->flags & P2P_DEV_REPORTED_ONCE)
891 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
892 dev->info.p2p_device_addr);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700893
894 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
895 wpabuf_free(dev->info.wps_vendor_ext[i]);
896 dev->info.wps_vendor_ext[i] = NULL;
897 }
898
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700899 wpabuf_free(dev->info.wfd_subelems);
Dmitry Shmidt2e67f062014-07-16 09:55:28 -0700900 wpabuf_free(dev->info.vendor_elems);
Dmitry Shmidt413dde72014-04-11 10:23:22 -0700901 wpabuf_free(dev->go_neg_conf);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700902
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700903 os_free(dev);
904}
905
906
907static int p2p_get_next_prog_freq(struct p2p_data *p2p)
908{
909 struct p2p_channels *c;
910 struct p2p_reg_class *cla;
911 size_t cl, ch;
912 int found = 0;
913 u8 reg_class;
914 u8 channel;
915 int freq;
916
917 c = &p2p->cfg->channels;
918 for (cl = 0; cl < c->reg_classes; cl++) {
919 cla = &c->reg_class[cl];
920 if (cla->reg_class != p2p->last_prog_scan_class)
921 continue;
922 for (ch = 0; ch < cla->channels; ch++) {
923 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
924 found = 1;
925 break;
926 }
927 }
928 if (found)
929 break;
930 }
931
932 if (!found) {
933 /* Start from beginning */
934 reg_class = c->reg_class[0].reg_class;
935 channel = c->reg_class[0].channel[0];
936 } else {
937 /* Pick the next channel */
938 ch++;
939 if (ch == cla->channels) {
940 cl++;
941 if (cl == c->reg_classes)
942 cl = 0;
943 ch = 0;
944 }
945 reg_class = c->reg_class[cl].reg_class;
946 channel = c->reg_class[cl].channel[ch];
947 }
948
Dmitry Shmidt4b060592013-04-29 16:42:49 -0700949 freq = p2p_channel_to_freq(reg_class, channel);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700950 p2p_dbg(p2p, "Next progressive search channel: reg_class %u channel %u -> %d MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700951 reg_class, channel, freq);
952 p2p->last_prog_scan_class = reg_class;
953 p2p->last_prog_scan_chan = channel;
954
955 if (freq == 2412 || freq == 2437 || freq == 2462)
956 return 0; /* No need to add social channels */
957 return freq;
958}
959
960
961static void p2p_search(struct p2p_data *p2p)
962{
963 int freq = 0;
964 enum p2p_scan_type type;
Dmitry Shmidt04949592012-07-19 12:16:46 -0700965 u16 pw_id = DEV_PW_DEFAULT;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700966 int res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700967
968 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700969 p2p_dbg(p2p, "Driver is still in Listen state - wait for it to end before continuing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700970 return;
971 }
972 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
973
Dmitry Shmidt04949592012-07-19 12:16:46 -0700974 if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
975 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700976 type = P2P_SCAN_SOCIAL_PLUS_ONE;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700977 p2p_dbg(p2p, "Starting search (+ freq %u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700978 } else {
979 type = P2P_SCAN_SOCIAL;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700980 p2p_dbg(p2p, "Starting search");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700981 }
982
Dmitry Shmidt61d9df32012-08-29 16:22:06 -0700983 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
984 p2p->num_req_dev_types, p2p->req_dev_types,
985 p2p->find_dev_id, pw_id);
986 if (res < 0) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -0800987 p2p_dbg(p2p, "Scan request schedule failed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700988 p2p_continue_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700989 }
990}
991
992
993static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
994{
995 struct p2p_data *p2p = eloop_ctx;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -0700996 p2p_dbg(p2p, "Find timeout -> stop");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -0700997 p2p_stop_find(p2p);
998}
999
1000
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001001void p2p_notify_scan_trigger_status(struct p2p_data *p2p, int status)
1002{
1003 if (status != 0) {
1004 p2p_dbg(p2p, "Scan request failed");
1005 /* Do continue find even for the first p2p_find_scan */
1006 p2p_continue_find(p2p);
1007 } else {
1008 p2p_dbg(p2p, "Running p2p_scan");
1009 p2p->p2p_scan_running = 1;
1010 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1011 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1012 p2p, NULL);
1013 }
1014}
1015
1016
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001017static int p2p_run_after_scan(struct p2p_data *p2p)
1018{
1019 struct p2p_device *dev;
1020 enum p2p_after_scan op;
1021
1022 if (p2p->after_scan_tx) {
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07001023 p2p->after_scan_tx_in_progress = 1;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001024 p2p_dbg(p2p, "Send pending Action frame at p2p_scan completion");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001025 p2p->cfg->send_action(p2p->cfg->cb_ctx,
1026 p2p->after_scan_tx->freq,
1027 p2p->after_scan_tx->dst,
1028 p2p->after_scan_tx->src,
1029 p2p->after_scan_tx->bssid,
1030 (u8 *) (p2p->after_scan_tx + 1),
1031 p2p->after_scan_tx->len,
1032 p2p->after_scan_tx->wait_time);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001033 os_free(p2p->after_scan_tx);
1034 p2p->after_scan_tx = NULL;
1035 return 1;
1036 }
1037
1038 op = p2p->start_after_scan;
1039 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1040 switch (op) {
1041 case P2P_AFTER_SCAN_NOTHING:
1042 break;
1043 case P2P_AFTER_SCAN_LISTEN:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001044 p2p_dbg(p2p, "Start previously requested Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001045 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
1046 p2p->pending_listen_usec / 1000);
1047 return 1;
1048 case P2P_AFTER_SCAN_CONNECT:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001049 p2p_dbg(p2p, "Start previously requested connect with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001050 MAC2STR(p2p->after_scan_peer));
1051 dev = p2p_get_device(p2p, p2p->after_scan_peer);
1052 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001053 p2p_dbg(p2p, "Peer not known anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001054 break;
1055 }
1056 p2p_connect_send(p2p, dev);
1057 return 1;
1058 }
1059
1060 return 0;
1061}
1062
1063
1064static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
1065{
1066 struct p2p_data *p2p = eloop_ctx;
1067 int running;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001068 p2p_dbg(p2p, "p2p_scan timeout (running=%d)", p2p->p2p_scan_running);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001069 running = p2p->p2p_scan_running;
1070 /* Make sure we recover from missed scan results callback */
1071 p2p->p2p_scan_running = 0;
1072
1073 if (running)
1074 p2p_run_after_scan(p2p);
1075}
1076
1077
1078static void p2p_free_req_dev_types(struct p2p_data *p2p)
1079{
1080 p2p->num_req_dev_types = 0;
1081 os_free(p2p->req_dev_types);
1082 p2p->req_dev_types = NULL;
1083}
1084
1085
1086int p2p_find(struct p2p_data *p2p, unsigned int timeout,
1087 enum p2p_discovery_type type,
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001088 unsigned int num_req_dev_types, const u8 *req_dev_types,
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001089 const u8 *dev_id, unsigned int search_delay)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001090{
1091 int res;
1092
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001093 p2p_dbg(p2p, "Starting find (type=%d)", type);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001094 os_get_reltime(&p2p->find_start);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001095 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001096 p2p_dbg(p2p, "p2p_scan is already running");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001097 }
1098
1099 p2p_free_req_dev_types(p2p);
1100 if (req_dev_types && num_req_dev_types) {
1101 p2p->req_dev_types = os_malloc(num_req_dev_types *
1102 WPS_DEV_TYPE_LEN);
1103 if (p2p->req_dev_types == NULL)
1104 return -1;
1105 os_memcpy(p2p->req_dev_types, req_dev_types,
1106 num_req_dev_types * WPS_DEV_TYPE_LEN);
1107 p2p->num_req_dev_types = num_req_dev_types;
1108 }
1109
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08001110 if (dev_id) {
1111 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1112 p2p->find_dev_id = p2p->find_dev_id_buf;
1113 } else
1114 p2p->find_dev_id = NULL;
1115
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001116 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1117 p2p_clear_timeout(p2p);
1118 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1119 p2p->find_type = type;
1120 p2p_device_clear_reported(p2p);
1121 p2p_set_state(p2p, P2P_SEARCH);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001122 p2p->search_delay = search_delay;
1123 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001124 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001125 p2p->last_p2p_find_timeout = timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001126 if (timeout)
1127 eloop_register_timeout(timeout, 0, p2p_find_timeout,
1128 p2p, NULL);
1129 switch (type) {
1130 case P2P_FIND_START_WITH_FULL:
1131 case P2P_FIND_PROGRESSIVE:
1132 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1133 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001134 p2p->req_dev_types, dev_id,
1135 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001136 break;
1137 case P2P_FIND_ONLY_SOCIAL:
1138 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1139 p2p->num_req_dev_types,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001140 p2p->req_dev_types, dev_id,
1141 DEV_PW_DEFAULT);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001142 break;
1143 default:
1144 return -1;
1145 }
1146
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001147 if (res != 0 && p2p->p2p_scan_running) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001148 p2p_dbg(p2p, "Failed to start p2p_scan - another p2p_scan was already running");
1149 /* wait for the previous p2p_scan to complete */
Dmitry Shmidt96be6222014-02-13 10:16:51 -08001150 res = 0; /* do not report failure */
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001151 } else if (res != 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001152 p2p_dbg(p2p, "Failed to start p2p_scan");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001153 p2p_set_state(p2p, P2P_IDLE);
1154 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001155 }
1156
1157 return res;
1158}
1159
1160
1161void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1162{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001163 p2p_dbg(p2p, "Stopping find");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001164 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1165 p2p_clear_timeout(p2p);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001166 if (p2p->state == P2P_SEARCH)
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001167 p2p->cfg->find_stopped(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001168 p2p_set_state(p2p, P2P_IDLE);
1169 p2p_free_req_dev_types(p2p);
1170 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08001171 if (p2p->go_neg_peer)
1172 p2p->go_neg_peer->flags &= ~P2P_DEV_PEER_WAITING_RESPONSE;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001173 p2p->go_neg_peer = NULL;
1174 p2p->sd_peer = NULL;
1175 p2p->invite_peer = NULL;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001176 p2p_stop_listen_for_freq(p2p, freq);
1177}
1178
1179
1180void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1181{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001182 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001183 p2p_dbg(p2p, "Skip stop_listen since we are on correct channel for response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001184 return;
1185 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07001186 if (p2p->in_listen) {
1187 p2p->in_listen = 0;
1188 p2p_clear_timeout(p2p);
1189 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001190 if (p2p->drv_in_listen) {
1191 /*
1192 * The driver may not deliver callback to p2p_listen_end()
1193 * when the operation gets canceled, so clear the internal
1194 * variable that is tracking driver state.
1195 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001196 p2p_dbg(p2p, "Clear drv_in_listen (%d)", p2p->drv_in_listen);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001197 p2p->drv_in_listen = 0;
1198 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001199 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1200}
1201
1202
Dmitry Shmidt391c59f2013-09-03 12:16:28 -07001203void p2p_stop_listen(struct p2p_data *p2p)
1204{
1205 if (p2p->state != P2P_LISTEN_ONLY) {
1206 p2p_dbg(p2p, "Skip stop_listen since not in listen_only state.");
1207 return;
1208 }
1209
1210 p2p_stop_listen_for_freq(p2p, 0);
1211 p2p_set_state(p2p, P2P_IDLE);
1212}
1213
1214
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001215void p2p_stop_find(struct p2p_data *p2p)
1216{
Dmitry Shmidt3c57b3f2014-05-22 15:13:07 -07001217 p2p->pending_listen_freq = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001218 p2p_stop_find_for_freq(p2p, 0);
1219}
1220
1221
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001222static int p2p_prepare_channel_pref(struct p2p_data *p2p,
1223 unsigned int force_freq,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001224 unsigned int pref_freq, int go)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001225{
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001226 u8 op_class, op_channel;
1227 unsigned int freq = force_freq ? force_freq : pref_freq;
1228
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001229 p2p_dbg(p2p, "Prepare channel pref - force_freq=%u pref_freq=%u go=%d",
1230 force_freq, pref_freq, go);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001231 if (p2p_freq_to_channel(freq, &op_class, &op_channel) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001232 p2p_dbg(p2p, "Unsupported frequency %u MHz", freq);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001233 return -1;
1234 }
1235
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001236 if (!p2p_channels_includes(&p2p->cfg->channels, op_class, op_channel) &&
1237 (go || !p2p_channels_includes(&p2p->cfg->cli_channels, op_class,
1238 op_channel))) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001239 p2p_dbg(p2p, "Frequency %u MHz (oper_class %u channel %u) not allowed for P2P",
1240 freq, op_class, op_channel);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001241 return -1;
1242 }
1243
1244 p2p->op_reg_class = op_class;
1245 p2p->op_channel = op_channel;
1246
1247 if (force_freq) {
1248 p2p->channels.reg_classes = 1;
1249 p2p->channels.reg_class[0].channels = 1;
1250 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1251 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001252 } else {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001253 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1254 sizeof(struct p2p_channels));
1255 }
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001256
1257 return 0;
1258}
1259
1260
1261static void p2p_prepare_channel_best(struct p2p_data *p2p)
1262{
1263 u8 op_class, op_channel;
Dmitry Shmidta0d265f2013-11-19 13:13:41 -08001264 const int op_classes_5ghz[] = { 124, 115, 0 };
1265 const int op_classes_ht40[] = { 126, 127, 116, 117, 0 };
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001266 const int op_classes_vht[] = { 128, 0 };
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001267
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001268 p2p_dbg(p2p, "Prepare channel best");
1269
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001270 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1271 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001272 p2p_freq_to_channel(p2p->best_freq_overall, &op_class, &op_channel)
1273 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001274 p2p_dbg(p2p, "Select best overall channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001275 p2p->op_reg_class = op_class;
1276 p2p->op_channel = op_channel;
1277 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1278 p2p_supported_freq(p2p, p2p->best_freq_5) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001279 p2p_freq_to_channel(p2p->best_freq_5, &op_class, &op_channel)
1280 == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001281 p2p_dbg(p2p, "Select best 5 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001282 p2p->op_reg_class = op_class;
1283 p2p->op_channel = op_channel;
1284 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_24 > 0 &&
1285 p2p_supported_freq(p2p, p2p->best_freq_24) &&
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001286 p2p_freq_to_channel(p2p->best_freq_24, &op_class,
1287 &op_channel) == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001288 p2p_dbg(p2p, "Select best 2.4 GHz channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001289 p2p->op_reg_class = op_class;
1290 p2p->op_channel = op_channel;
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001291 } else if (p2p->cfg->num_pref_chan > 0 &&
1292 p2p_channels_includes(&p2p->cfg->channels,
1293 p2p->cfg->pref_chan[0].op_class,
1294 p2p->cfg->pref_chan[0].chan)) {
1295 p2p_dbg(p2p, "Select first pref_chan entry as operating channel preference");
1296 p2p->op_reg_class = p2p->cfg->pref_chan[0].op_class;
1297 p2p->op_channel = p2p->cfg->pref_chan[0].chan;
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08001298 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_vht,
1299 &p2p->op_reg_class, &p2p->op_channel) ==
1300 0) {
1301 p2p_dbg(p2p, "Select possible VHT channel (op_class %u channel %u) as operating channel preference",
1302 p2p->op_reg_class, p2p->op_channel);
1303 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_ht40,
1304 &p2p->op_reg_class, &p2p->op_channel) ==
1305 0) {
1306 p2p_dbg(p2p, "Select possible HT40 channel (op_class %u channel %u) as operating channel preference",
1307 p2p->op_reg_class, p2p->op_channel);
1308 } else if (p2p_channel_select(&p2p->cfg->channels, op_classes_5ghz,
1309 &p2p->op_reg_class, &p2p->op_channel) ==
1310 0) {
1311 p2p_dbg(p2p, "Select possible 5 GHz channel (op_class %u channel %u) as operating channel preference",
1312 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001313 } else if (p2p_channels_includes(&p2p->cfg->channels,
1314 p2p->cfg->op_reg_class,
1315 p2p->cfg->op_channel)) {
Dmitry Shmidtb7b4d0e2013-08-26 12:09:05 -07001316 p2p_dbg(p2p, "Select pre-configured channel as operating channel preference");
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001317 p2p->op_reg_class = p2p->cfg->op_reg_class;
1318 p2p->op_channel = p2p->cfg->op_channel;
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001319 } else if (p2p_channel_random_social(&p2p->cfg->channels,
1320 &p2p->op_reg_class,
1321 &p2p->op_channel) == 0) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001322 p2p_dbg(p2p, "Select random available social channel (op_class %u channel %u) as operating channel preference",
1323 p2p->op_reg_class, p2p->op_channel);
Dmitry Shmidtd11f0192014-03-24 12:09:47 -07001324 } else {
1325 /* Select any random available channel from the first available
1326 * operating class */
1327 p2p_channel_select(&p2p->cfg->channels, NULL,
1328 &p2p->op_reg_class,
1329 &p2p->op_channel);
1330 p2p_dbg(p2p, "Select random available channel %d from operating class %d as operating channel preference",
1331 p2p->op_channel, p2p->op_reg_class);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001332 }
1333
1334 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1335 sizeof(struct p2p_channels));
1336}
1337
1338
1339/**
1340 * p2p_prepare_channel - Select operating channel for GO Negotiation
1341 * @p2p: P2P module context from p2p_init()
1342 * @dev: Selected peer device
1343 * @force_freq: Forced frequency in MHz or 0 if not forced
1344 * @pref_freq: Preferred frequency in MHz or 0 if no preference
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001345 * @go: Whether the local end will be forced to be GO
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001346 * Returns: 0 on success, -1 on failure (channel not supported for P2P)
1347 *
1348 * This function is used to do initial operating channel selection for GO
1349 * Negotiation prior to having received peer information. The selected channel
1350 * may be further optimized in p2p_reselect_channel() once the peer information
1351 * is available.
1352 */
Dmitry Shmidt7a5e50a2013-03-05 12:37:16 -08001353int p2p_prepare_channel(struct p2p_data *p2p, struct p2p_device *dev,
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001354 unsigned int force_freq, unsigned int pref_freq, int go)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001355{
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001356 p2p_dbg(p2p, "Prepare channel - force_freq=%u pref_freq=%u go=%d",
1357 force_freq, pref_freq, go);
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001358 if (force_freq || pref_freq) {
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001359 if (p2p_prepare_channel_pref(p2p, force_freq, pref_freq, go) <
1360 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001361 return -1;
1362 } else {
1363 p2p_prepare_channel_best(p2p);
1364 }
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001365 p2p_channels_dump(p2p, "prepared channels", &p2p->channels);
1366 if (go)
1367 p2p_channels_remove_freqs(&p2p->channels, &p2p->no_go_freq);
1368 else if (!force_freq)
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001369 p2p_channels_union_inplace(&p2p->channels,
1370 &p2p->cfg->cli_channels);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001371 p2p_channels_dump(p2p, "after go/cli filter/add", &p2p->channels);
1372
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001373 p2p_dbg(p2p, "Own preference for operation channel: Operating Class %u Channel %u%s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001374 p2p->op_reg_class, p2p->op_channel,
1375 force_freq ? " (forced)" : "");
1376
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001377 if (force_freq)
1378 dev->flags |= P2P_DEV_FORCE_FREQ;
1379 else
1380 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1381
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001382 return 0;
1383}
1384
1385
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001386static void p2p_set_dev_persistent(struct p2p_device *dev,
1387 int persistent_group)
1388{
1389 switch (persistent_group) {
1390 case 0:
1391 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1392 P2P_DEV_PREFER_PERSISTENT_RECONN);
1393 break;
1394 case 1:
1395 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1396 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1397 break;
1398 case 2:
1399 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1400 P2P_DEV_PREFER_PERSISTENT_RECONN;
1401 break;
1402 }
1403}
1404
1405
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001406int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1407 enum p2p_wps_method wps_method,
1408 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001409 unsigned int force_freq, int persistent_group,
1410 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001411 int pd_before_go_neg, unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001412{
1413 struct p2p_device *dev;
1414
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001415 p2p_dbg(p2p, "Request to start group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001416 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001417 " wps_method=%d persistent_group=%d pd_before_go_neg=%d "
1418 "oob_pw_id=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001419 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001420 wps_method, persistent_group, pd_before_go_neg, oob_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001421
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001422 dev = p2p_get_device(p2p, peer_addr);
1423 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001424 p2p_dbg(p2p, "Cannot connect to unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001425 MAC2STR(peer_addr));
1426 return -1;
1427 }
1428
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001429 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq,
1430 go_intent == 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001431 return -1;
1432
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001433 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1434 if (!(dev->info.dev_capab &
1435 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001436 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001437 " that is in a group and is not discoverable",
1438 MAC2STR(peer_addr));
1439 return -1;
1440 }
1441 if (dev->oper_freq <= 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001442 p2p_dbg(p2p, "Cannot connect to P2P Device " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001443 " with incomplete information",
1444 MAC2STR(peer_addr));
1445 return -1;
1446 }
1447
1448 /*
1449 * First, try to connect directly. If the peer does not
1450 * acknowledge frames, assume it is sleeping and use device
1451 * discoverability via the GO at that point.
1452 */
1453 }
1454
Dmitry Shmidt04949592012-07-19 12:16:46 -07001455 p2p->ssid_set = 0;
1456 if (force_ssid) {
1457 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1458 force_ssid, force_ssid_len);
1459 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1460 p2p->ssid_len = force_ssid_len;
1461 p2p->ssid_set = 1;
1462 }
1463
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001464 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1465 dev->flags &= ~P2P_DEV_USER_REJECTED;
1466 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1467 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
Dmitry Shmidt04949592012-07-19 12:16:46 -07001468 if (pd_before_go_neg)
1469 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001470 else {
Dmitry Shmidt04949592012-07-19 12:16:46 -07001471 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001472 /*
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001473 * Assign dialog token and tie breaker here to use the same
1474 * values in each retry within the same GO Negotiation exchange.
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001475 */
1476 dev->dialog_token++;
1477 if (dev->dialog_token == 0)
1478 dev->dialog_token = 1;
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08001479 dev->tie_breaker = p2p->next_tie_breaker;
1480 p2p->next_tie_breaker = !p2p->next_tie_breaker;
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001481 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001482 dev->connect_reqs = 0;
1483 dev->go_neg_req_sent = 0;
1484 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001485 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001486 p2p->go_intent = go_intent;
1487 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1488
1489 if (p2p->state != P2P_IDLE)
1490 p2p_stop_find(p2p);
1491
1492 if (p2p->after_scan_tx) {
1493 /*
1494 * We need to drop the pending frame to avoid issues with the
1495 * new GO Negotiation, e.g., when the pending frame was from a
1496 * previous attempt at starting a GO Negotiation.
1497 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001498 p2p_dbg(p2p, "Dropped previous pending Action frame TX that was waiting for p2p_scan completion");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001499 os_free(p2p->after_scan_tx);
1500 p2p->after_scan_tx = NULL;
1501 }
1502
1503 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001504 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001505 dev->status = P2P_SC_SUCCESS;
1506
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001507 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001508 p2p_dbg(p2p, "p2p_scan running - delay connect send");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001509 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1510 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1511 return 0;
1512 }
1513 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1514
1515 return p2p_connect_send(p2p, dev);
1516}
1517
1518
1519int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1520 enum p2p_wps_method wps_method,
1521 int go_intent, const u8 *own_interface_addr,
Dmitry Shmidt04949592012-07-19 12:16:46 -07001522 unsigned int force_freq, int persistent_group,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08001523 const u8 *force_ssid, size_t force_ssid_len,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001524 unsigned int pref_freq, u16 oob_pw_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001525{
1526 struct p2p_device *dev;
1527
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001528 p2p_dbg(p2p, "Request to authorize group negotiation - peer=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001529 " GO Intent=%d Intended Interface Address=" MACSTR
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001530 " wps_method=%d persistent_group=%d oob_pw_id=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001531 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001532 wps_method, persistent_group, oob_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001533
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001534 dev = p2p_get_device(p2p, peer_addr);
1535 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001536 p2p_dbg(p2p, "Cannot authorize unknown P2P Device " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001537 MAC2STR(peer_addr));
1538 return -1;
1539 }
1540
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001541 if (p2p_prepare_channel(p2p, dev, force_freq, pref_freq, go_intent ==
1542 15) < 0)
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08001543 return -1;
1544
Dmitry Shmidt04949592012-07-19 12:16:46 -07001545 p2p->ssid_set = 0;
1546 if (force_ssid) {
1547 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1548 force_ssid, force_ssid_len);
1549 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1550 p2p->ssid_len = force_ssid_len;
1551 p2p->ssid_set = 1;
1552 }
1553
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001554 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1555 dev->flags &= ~P2P_DEV_USER_REJECTED;
1556 dev->go_neg_req_sent = 0;
1557 dev->go_state = UNKNOWN_GO;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001558 p2p_set_dev_persistent(dev, persistent_group);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001559 p2p->go_intent = go_intent;
1560 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1561
1562 dev->wps_method = wps_method;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001563 dev->oob_pw_id = oob_pw_id;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001564 dev->status = P2P_SC_SUCCESS;
1565
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001566 return 0;
1567}
1568
1569
1570void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1571 struct p2p_device *dev, struct p2p_message *msg)
1572{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001573 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001574
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001575 p2p_copy_wps_info(p2p, dev, 0, msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001576
1577 if (msg->listen_channel) {
1578 int freq;
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001579 freq = p2p_channel_to_freq(msg->listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001580 msg->listen_channel[4]);
1581 if (freq < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001582 p2p_dbg(p2p, "Unknown peer Listen channel: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001583 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1584 msg->listen_channel[0],
1585 msg->listen_channel[1],
1586 msg->listen_channel[2],
1587 msg->listen_channel[3],
1588 msg->listen_channel[4]);
1589 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001590 p2p_dbg(p2p, "Update peer " MACSTR
1591 " Listen channel: %u -> %u MHz",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001592 MAC2STR(dev->info.p2p_device_addr),
1593 dev->listen_freq, freq);
1594 dev->listen_freq = freq;
1595 }
1596 }
1597
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001598 if (msg->wfd_subelems) {
1599 wpabuf_free(dev->info.wfd_subelems);
1600 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1601 }
1602
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001603 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1604 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001605 p2p_dbg(p2p, "Completed device entry based on data from GO Negotiation Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001606 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001607 p2p_dbg(p2p, "Created device entry based on GO Neg Req: "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001608 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1609 "listen_freq=%d",
1610 MAC2STR(dev->info.p2p_device_addr),
1611 dev->info.dev_capab, dev->info.group_capab,
1612 dev->info.device_name, dev->listen_freq);
1613 }
1614
1615 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1616
1617 if (dev->flags & P2P_DEV_USER_REJECTED) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001618 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001619 return;
1620 }
1621
1622 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1623 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1624 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1625}
1626
1627
1628void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1629{
1630 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1631 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1632 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1633 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1634 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1635}
1636
1637
1638int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1639{
1640 p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001641 p2p_random(params->passphrase, p2p->cfg->passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001642 return 0;
1643}
1644
1645
1646void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1647{
1648 struct p2p_go_neg_results res;
1649 int go = peer->go_state == LOCAL_GO;
1650 struct p2p_channels intersection;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001651
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001652 p2p_dbg(p2p, "GO Negotiation with " MACSTR " completed (%s will be GO)",
1653 MAC2STR(peer->info.p2p_device_addr), go ? "local end" : "peer");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001654
1655 os_memset(&res, 0, sizeof(res));
1656 res.role_go = go;
1657 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1658 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1659 res.wps_method = peer->wps_method;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001660 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1661 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1662 res.persistent_group = 2;
1663 else
1664 res.persistent_group = 1;
1665 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001666
1667 if (go) {
1668 /* Setup AP mode for WPS provisioning */
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001669 res.freq = p2p_channel_to_freq(p2p->op_reg_class,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001670 p2p->op_channel);
1671 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1672 res.ssid_len = p2p->ssid_len;
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07001673 p2p_random(res.passphrase, p2p->cfg->passphrase_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001674 } else {
1675 res.freq = peer->oper_freq;
1676 if (p2p->ssid_len) {
1677 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1678 res.ssid_len = p2p->ssid_len;
1679 }
1680 }
1681
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001682 p2p_channels_dump(p2p, "own channels", &p2p->channels);
1683 p2p_channels_dump(p2p, "peer channels", &peer->channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001684 p2p_channels_intersect(&p2p->channels, &peer->channels,
1685 &intersection);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07001686 if (go) {
1687 p2p_channels_remove_freqs(&intersection, &p2p->no_go_freq);
1688 p2p_channels_dump(p2p, "intersection after no-GO removal",
1689 &intersection);
1690 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08001691
1692 p2p_channels_to_freqs(&intersection, res.freq_list,
1693 P2P_MAX_CHANNELS);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001694
1695 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1696
1697 p2p_clear_timeout(p2p);
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001698 p2p->ssid_set = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001699 peer->go_neg_req_sent = 0;
1700 peer->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001701 peer->oob_pw_id = 0;
Dmitry Shmidt413dde72014-04-11 10:23:22 -07001702 wpabuf_free(peer->go_neg_conf);
1703 peer->go_neg_conf = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001704
1705 p2p_set_state(p2p, P2P_PROVISIONING);
1706 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1707}
1708
1709
1710static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1711 const u8 *data, size_t len, int rx_freq)
1712{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001713 p2p_dbg(p2p, "RX P2P Public Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001714 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1715
1716 if (len < 1)
1717 return;
1718
1719 switch (data[0]) {
1720 case P2P_GO_NEG_REQ:
1721 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1722 break;
1723 case P2P_GO_NEG_RESP:
1724 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1725 break;
1726 case P2P_GO_NEG_CONF:
1727 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1728 break;
1729 case P2P_INVITATION_REQ:
1730 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1731 rx_freq);
1732 break;
1733 case P2P_INVITATION_RESP:
1734 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1735 break;
1736 case P2P_PROV_DISC_REQ:
1737 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1738 break;
1739 case P2P_PROV_DISC_RESP:
1740 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1741 break;
1742 case P2P_DEV_DISC_REQ:
1743 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1744 break;
1745 case P2P_DEV_DISC_RESP:
1746 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1747 break;
1748 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001749 p2p_dbg(p2p, "Unsupported P2P Public Action frame type %d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001750 data[0]);
1751 break;
1752 }
1753}
1754
1755
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08001756static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1757 const u8 *sa, const u8 *bssid, const u8 *data,
1758 size_t len, int freq)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001759{
1760 if (len < 1)
1761 return;
1762
1763 switch (data[0]) {
1764 case WLAN_PA_VENDOR_SPECIFIC:
1765 data++;
1766 len--;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001767 if (len < 4)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001768 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001769 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001770 return;
1771
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001772 data += 4;
1773 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001774
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001775 p2p_rx_p2p_action(p2p, sa, data, len, freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001776 break;
1777 case WLAN_PA_GAS_INITIAL_REQ:
1778 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1779 break;
1780 case WLAN_PA_GAS_INITIAL_RESP:
1781 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1782 break;
1783 case WLAN_PA_GAS_COMEBACK_REQ:
1784 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1785 break;
1786 case WLAN_PA_GAS_COMEBACK_RESP:
1787 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1788 break;
1789 }
1790}
1791
1792
1793void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1794 const u8 *bssid, u8 category,
1795 const u8 *data, size_t len, int freq)
1796{
1797 if (category == WLAN_ACTION_PUBLIC) {
1798 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1799 return;
1800 }
1801
1802 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1803 return;
1804
1805 if (len < 4)
1806 return;
1807
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001808 if (WPA_GET_BE32(data) != P2P_IE_VENDOR_TYPE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001809 return;
Dmitry Shmidta38abf92014-03-06 13:38:44 -08001810 data += 4;
1811 len -= 4;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001812
1813 /* P2P action frame */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001814 p2p_dbg(p2p, "RX P2P Action from " MACSTR, MAC2STR(sa));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001815 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1816
1817 if (len < 1)
1818 return;
1819 switch (data[0]) {
1820 case P2P_NOA:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001821 p2p_dbg(p2p, "Received P2P Action - Notice of Absence");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001822 /* TODO */
1823 break;
1824 case P2P_PRESENCE_REQ:
1825 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1826 break;
1827 case P2P_PRESENCE_RESP:
1828 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1829 break;
1830 case P2P_GO_DISC_REQ:
1831 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1832 break;
1833 default:
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001834 p2p_dbg(p2p, "Received P2P Action - unknown type %u", data[0]);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001835 break;
1836 }
1837}
1838
1839
1840static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1841{
1842 struct p2p_data *p2p = eloop_ctx;
1843 if (p2p->go_neg_peer == NULL)
1844 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001845 if (p2p->pending_listen_freq) {
1846 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_go_neg_start");
1847 p2p->pending_listen_freq = 0;
1848 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001849 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1850 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001851 /*
1852 * Set new timeout to make sure a previously set one does not expire
1853 * too quickly while waiting for the GO Negotiation to complete.
1854 */
1855 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001856 p2p_connect_send(p2p, p2p->go_neg_peer);
1857}
1858
1859
1860static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1861{
1862 struct p2p_data *p2p = eloop_ctx;
1863 if (p2p->invite_peer == NULL)
1864 return;
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07001865 if (p2p->pending_listen_freq) {
1866 p2p_dbg(p2p, "Clear pending_listen_freq for p2p_invite_start");
1867 p2p->pending_listen_freq = 0;
1868 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001869 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08001870 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr,
1871 p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001872}
1873
1874
1875static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1876 const u8 *ie, size_t ie_len)
1877{
1878 struct p2p_message msg;
1879 struct p2p_device *dev;
1880
1881 os_memset(&msg, 0, sizeof(msg));
1882 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1883 {
1884 p2p_parse_free(&msg);
1885 return; /* not a P2P probe */
1886 }
1887
1888 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1889 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1890 != 0) {
1891 /* The Probe Request is not part of P2P Device Discovery. It is
1892 * not known whether the source address of the frame is the P2P
1893 * Device Address or P2P Interface Address. Do not add a new
1894 * peer entry based on this frames.
1895 */
1896 p2p_parse_free(&msg);
1897 return;
1898 }
1899
1900 dev = p2p_get_device(p2p, addr);
1901 if (dev) {
1902 if (dev->country[0] == 0 && msg.listen_channel)
1903 os_memcpy(dev->country, msg.listen_channel, 3);
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001904 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001905 p2p_parse_free(&msg);
1906 return; /* already known */
1907 }
1908
1909 dev = p2p_create_device(p2p, addr);
1910 if (dev == NULL) {
1911 p2p_parse_free(&msg);
1912 return;
1913 }
1914
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001915 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001916 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1917
1918 if (msg.listen_channel) {
1919 os_memcpy(dev->country, msg.listen_channel, 3);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07001920 dev->listen_freq = p2p_channel_to_freq(msg.listen_channel[3],
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001921 msg.listen_channel[4]);
1922 }
1923
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001924 p2p_copy_wps_info(p2p, dev, 1, &msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001925
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07001926 if (msg.wfd_subelems) {
1927 wpabuf_free(dev->info.wfd_subelems);
1928 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
1929 }
1930
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001931 p2p_parse_free(&msg);
1932
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07001933 p2p_dbg(p2p, "Created device entry based on Probe Req: " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001934 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1935 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1936 dev->info.group_capab, dev->info.device_name,
1937 dev->listen_freq);
1938}
1939
1940
1941struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1942 const u8 *addr,
1943 struct p2p_message *msg)
1944{
1945 struct p2p_device *dev;
1946
1947 dev = p2p_get_device(p2p, addr);
1948 if (dev) {
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08001949 os_get_reltime(&dev->last_seen);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07001950 return dev; /* already known */
1951 }
1952
1953 dev = p2p_create_device(p2p, addr);
1954 if (dev == NULL)
1955 return NULL;
1956
1957 p2p_add_dev_info(p2p, addr, dev, msg);
1958
1959 return dev;
1960}
1961
1962
1963static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1964{
1965 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1966 return 1;
1967 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1968 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1969 WPA_GET_BE16(&req_dev_type[6]) == 0)
1970 return 1; /* Category match with wildcard OUI/sub-category */
1971 return 0;
1972}
1973
1974
1975int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1976 size_t num_req_dev_type)
1977{
1978 size_t i;
1979 for (i = 0; i < num_req_dev_type; i++) {
1980 if (dev_type_match(dev_type, req_dev_type[i]))
1981 return 1;
1982 }
1983 return 0;
1984}
1985
1986
1987/**
1988 * p2p_match_dev_type - Match local device type with requested type
1989 * @p2p: P2P module context from p2p_init()
1990 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1991 * Returns: 1 on match, 0 on mismatch
1992 *
1993 * This function can be used to match the Requested Device Type attribute in
1994 * WPS IE with the local device types for deciding whether to reply to a Probe
1995 * Request frame.
1996 */
1997int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1998{
1999 struct wps_parse_attr attr;
2000 size_t i;
2001
2002 if (wps_parse_msg(wps, &attr))
2003 return 1; /* assume no Requested Device Type attributes */
2004
2005 if (attr.num_req_dev_type == 0)
2006 return 1; /* no Requested Device Type attributes -> match */
2007
2008 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
2009 attr.num_req_dev_type))
2010 return 1; /* Own Primary Device Type matches */
2011
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002012 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002013 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
2014 attr.req_dev_type,
2015 attr.num_req_dev_type))
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002016 return 1; /* Own Secondary Device Type matches */
2017 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002018
2019 /* No matching device type found */
2020 return 0;
2021}
2022
2023
2024struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
2025{
2026 struct wpabuf *buf;
2027 u8 *len;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002028 int pw_id = -1;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002029 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002030
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002031#ifdef CONFIG_WIFI_DISPLAY
2032 if (p2p->wfd_ie_probe_resp)
2033 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
2034#endif /* CONFIG_WIFI_DISPLAY */
2035
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002036 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2037 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2038
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002039 buf = wpabuf_alloc(1000 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002040 if (buf == NULL)
2041 return NULL;
2042
Dmitry Shmidt04949592012-07-19 12:16:46 -07002043 if (p2p->go_neg_peer) {
2044 /* Advertise immediate availability of WPS credential */
2045 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
2046 }
2047
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002048 if (p2p_build_wps_ie(p2p, buf, pw_id, 1) < 0) {
2049 p2p_dbg(p2p, "Failed to build WPS IE for Probe Response");
2050 wpabuf_free(buf);
2051 return NULL;
2052 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002053
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002054#ifdef CONFIG_WIFI_DISPLAY
2055 if (p2p->wfd_ie_probe_resp)
2056 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
2057#endif /* CONFIG_WIFI_DISPLAY */
2058
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002059 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P])
2060 wpabuf_put_buf(buf,
2061 p2p->vendor_elem[VENDOR_ELEM_PROBE_RESP_P2P]);
2062
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002063 /* P2P IE */
2064 len = p2p_buf_add_ie_hdr(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002065 p2p_buf_add_capability(buf, p2p->dev_capab &
2066 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002067 if (p2p->ext_listen_interval)
2068 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
2069 p2p->ext_listen_interval);
2070 p2p_buf_add_device_info(buf, p2p, NULL);
2071 p2p_buf_update_ie_hdr(buf, len);
2072
2073 return buf;
2074}
2075
2076
Dmitry Shmidt04949592012-07-19 12:16:46 -07002077static enum p2p_probe_req_status
2078p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2079 const u8 *bssid, const u8 *ie, size_t ie_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002080{
2081 struct ieee802_11_elems elems;
2082 struct wpabuf *buf;
2083 struct ieee80211_mgmt *resp;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002084 struct p2p_message msg;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002085 struct wpabuf *ies;
2086
2087 if (!p2p->in_listen || !p2p->drv_in_listen) {
2088 /* not in Listen state - ignore Probe Request */
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08002089 p2p_dbg(p2p, "Not in Listen state (in_listen=%d drv_in_listen=%d) - ignore Probe Request",
2090 p2p->in_listen, p2p->drv_in_listen);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002091 return P2P_PREQ_NOT_LISTEN;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002092 }
2093
2094 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2095 ParseFailed) {
2096 /* Ignore invalid Probe Request frames */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002097 p2p_dbg(p2p, "Could not parse Probe Request frame - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002098 return P2P_PREQ_MALFORMED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002099 }
2100
2101 if (elems.p2p == NULL) {
2102 /* not a P2P probe - ignore it */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002103 p2p_dbg(p2p, "Not a P2P probe - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002104 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002105 }
2106
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002107 if (dst && !is_broadcast_ether_addr(dst) &&
2108 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2109 /* Not sent to the broadcast address or our P2P Device Address
2110 */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002111 p2p_dbg(p2p, "Probe Req DA " MACSTR " not ours - ignore it",
2112 MAC2STR(dst));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002113 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002114 }
2115
2116 if (bssid && !is_broadcast_ether_addr(bssid)) {
2117 /* Not sent to the Wildcard BSSID */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002118 p2p_dbg(p2p, "Probe Req BSSID " MACSTR " not wildcard - ignore it",
2119 MAC2STR(bssid));
Dmitry Shmidt04949592012-07-19 12:16:46 -07002120 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002121 }
2122
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002123 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2124 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2125 0) {
2126 /* not using P2P Wildcard SSID - ignore */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002127 p2p_dbg(p2p, "Probe Req not using P2P Wildcard SSID - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002128 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002129 }
2130
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002131 if (supp_rates_11b_only(&elems)) {
2132 /* Indicates support for 11b rates only */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002133 p2p_dbg(p2p, "Probe Req with 11b rates only supported - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002134 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002135 }
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002136
2137 os_memset(&msg, 0, sizeof(msg));
2138 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2139 /* Could not parse P2P attributes */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002140 p2p_dbg(p2p, "Could not parse P2P attributes in Probe Req - ignore it");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002141 return P2P_PREQ_NOT_P2P;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002142 }
2143
2144 if (msg.device_id &&
Dmitry Shmidt04949592012-07-19 12:16:46 -07002145 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002146 /* Device ID did not match */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002147 p2p_dbg(p2p, "Probe Req requested Device ID " MACSTR " did not match - ignore it",
2148 MAC2STR(msg.device_id));
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002149 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
2153 /* Check Requested Device Type match */
2154 if (msg.wps_attributes &&
2155 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2156 /* No match with Requested Device Type */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002157 p2p_dbg(p2p, "Probe Req requestred Device Type did not match - ignore it");
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002158 p2p_parse_free(&msg);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002159 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002160 }
2161 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002162
Dmitry Shmidt04949592012-07-19 12:16:46 -07002163 if (!p2p->cfg->send_probe_resp) {
2164 /* Response generated elsewhere */
Dmitry Shmidtec58b162014-02-19 12:44:18 -08002165 p2p_dbg(p2p, "Probe Resp generated elsewhere - do not generate additional response");
Dmitry Shmidt04949592012-07-19 12:16:46 -07002166 return P2P_PREQ_NOT_PROCESSED;
2167 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002168
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002169 p2p_dbg(p2p, "Reply to P2P Probe Request in Listen state");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002170
2171 /*
2172 * We do not really have a specific BSS that this frame is advertising,
2173 * so build a frame that has some information in valid format. This is
2174 * really only used for discovery purposes, not to learn exact BSS
2175 * parameters.
2176 */
2177 ies = p2p_build_probe_resp_ies(p2p);
2178 if (ies == NULL)
Dmitry Shmidt04949592012-07-19 12:16:46 -07002179 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002180
2181 buf = wpabuf_alloc(200 + wpabuf_len(ies));
2182 if (buf == NULL) {
2183 wpabuf_free(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002184 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002185 }
2186
2187 resp = NULL;
2188 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
2189
2190 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2191 (WLAN_FC_STYPE_PROBE_RESP << 4));
2192 os_memcpy(resp->da, addr, ETH_ALEN);
2193 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2194 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2195 resp->u.probe_resp.beacon_int = host_to_le16(100);
2196 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2197 resp->u.probe_resp.capab_info =
2198 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2199 WLAN_CAPABILITY_PRIVACY |
2200 WLAN_CAPABILITY_SHORT_SLOT_TIME);
2201
2202 wpabuf_put_u8(buf, WLAN_EID_SSID);
2203 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2204 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2205
2206 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2207 wpabuf_put_u8(buf, 8);
2208 wpabuf_put_u8(buf, (60 / 5) | 0x80);
2209 wpabuf_put_u8(buf, 90 / 5);
2210 wpabuf_put_u8(buf, (120 / 5) | 0x80);
2211 wpabuf_put_u8(buf, 180 / 5);
2212 wpabuf_put_u8(buf, (240 / 5) | 0x80);
2213 wpabuf_put_u8(buf, 360 / 5);
2214 wpabuf_put_u8(buf, 480 / 5);
2215 wpabuf_put_u8(buf, 540 / 5);
2216
2217 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2218 wpabuf_put_u8(buf, 1);
2219 wpabuf_put_u8(buf, p2p->cfg->channel);
2220
2221 wpabuf_put_buf(buf, ies);
2222 wpabuf_free(ies);
2223
2224 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
2225
2226 wpabuf_free(buf);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002227
2228 return P2P_PREQ_NOT_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002229}
2230
2231
Dmitry Shmidt04949592012-07-19 12:16:46 -07002232enum p2p_probe_req_status
2233p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2234 const u8 *bssid, const u8 *ie, size_t ie_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002235{
Dmitry Shmidt04949592012-07-19 12:16:46 -07002236 enum p2p_probe_req_status res;
2237
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002238 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2239
Dmitry Shmidt04949592012-07-19 12:16:46 -07002240 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002241
2242 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2243 p2p->go_neg_peer &&
2244 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002245 == 0 &&
2246 !(p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002247 /* Received a Probe Request from GO Negotiation peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002248 p2p_dbg(p2p, "Found GO Negotiation peer - try to start GO negotiation from timeout");
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002249 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002250 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002251 return P2P_PREQ_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002252 }
2253
2254 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2255 p2p->invite_peer &&
Dmitry Shmidt3c479372014-02-04 10:50:36 -08002256 (p2p->invite_peer->flags & P2P_DEV_WAIT_INV_REQ_ACK) &&
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002257 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2258 == 0) {
2259 /* Received a Probe Request from Invite peer */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002260 p2p_dbg(p2p, "Found Invite peer - try to start Invite from timeout");
Dmitry Shmidt7f93d6f2014-02-21 11:22:49 -08002261 eloop_cancel_timeout(p2p_invite_start, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002262 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002263 return P2P_PREQ_PROCESSED;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002264 }
2265
Dmitry Shmidt04949592012-07-19 12:16:46 -07002266 return res;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002267}
2268
2269
2270static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2271 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2272{
2273 struct wpabuf *tmp;
2274 u8 *lpos;
2275 size_t tmplen;
2276 int res;
2277 u8 group_capab;
2278
2279 if (p2p_ie == NULL)
2280 return 0; /* WLAN AP is not a P2P manager */
2281
2282 /*
2283 * (Re)Association Request - P2P IE
2284 * P2P Capability attribute (shall be present)
2285 * P2P Interface attribute (present if concurrent device and
2286 * P2P Management is enabled)
2287 */
2288 tmp = wpabuf_alloc(200);
2289 if (tmp == NULL)
2290 return -1;
2291
2292 lpos = p2p_buf_add_ie_hdr(tmp);
2293 group_capab = 0;
2294 if (p2p->num_groups > 0) {
2295 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2296 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2297 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2298 p2p->cross_connect)
2299 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2300 }
2301 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2302 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2303 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2304 p2p_buf_add_p2p_interface(tmp, p2p);
2305 p2p_buf_update_ie_hdr(tmp, lpos);
2306
2307 tmplen = wpabuf_len(tmp);
2308 if (tmplen > len)
2309 res = -1;
2310 else {
2311 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2312 res = tmplen;
2313 }
2314 wpabuf_free(tmp);
2315
2316 return res;
2317}
2318
2319
2320int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2321 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2322{
2323 struct wpabuf *tmp;
2324 u8 *lpos;
2325 struct p2p_device *peer;
2326 size_t tmplen;
2327 int res;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002328 size_t extra = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002329
2330 if (!p2p_group)
2331 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2332
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002333#ifdef CONFIG_WIFI_DISPLAY
2334 if (p2p->wfd_ie_assoc_req)
2335 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2336#endif /* CONFIG_WIFI_DISPLAY */
2337
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002338 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2339 extra += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2340
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002341 /*
2342 * (Re)Association Request - P2P IE
2343 * P2P Capability attribute (shall be present)
2344 * Extended Listen Timing (may be present)
2345 * P2P Device Info attribute (shall be present)
2346 */
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002347 tmp = wpabuf_alloc(200 + extra);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002348 if (tmp == NULL)
2349 return -1;
2350
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002351#ifdef CONFIG_WIFI_DISPLAY
2352 if (p2p->wfd_ie_assoc_req)
2353 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2354#endif /* CONFIG_WIFI_DISPLAY */
2355
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07002356 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ])
2357 wpabuf_put_buf(tmp,
2358 p2p->vendor_elem[VENDOR_ELEM_P2P_ASSOC_REQ]);
2359
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002360 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2361
2362 lpos = p2p_buf_add_ie_hdr(tmp);
2363 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2364 if (p2p->ext_listen_interval)
2365 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2366 p2p->ext_listen_interval);
2367 p2p_buf_add_device_info(tmp, p2p, peer);
2368 p2p_buf_update_ie_hdr(tmp, lpos);
2369
2370 tmplen = wpabuf_len(tmp);
2371 if (tmplen > len)
2372 res = -1;
2373 else {
2374 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2375 res = tmplen;
2376 }
2377 wpabuf_free(tmp);
2378
2379 return res;
2380}
2381
2382
2383int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2384{
2385 struct wpabuf *p2p_ie;
2386 int ret;
2387
2388 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2389 if (p2p_ie == NULL)
2390 return 0;
2391
2392 ret = p2p_attr_text(p2p_ie, buf, end);
2393 wpabuf_free(p2p_ie);
2394 return ret;
2395}
2396
2397
Dmitry Shmidt04949592012-07-19 12:16:46 -07002398int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2399{
2400 struct p2p_message msg;
2401
2402 os_memset(&msg, 0, sizeof(msg));
2403 if (p2p_parse_p2p_ie(p2p_ie, &msg))
2404 return -1;
2405
2406 if (msg.p2p_device_addr) {
2407 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2408 return 0;
2409 } else if (msg.device_id) {
2410 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2411 return 0;
2412 }
2413 return -1;
2414}
2415
2416
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002417int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2418{
2419 struct wpabuf *p2p_ie;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002420 int ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002421
2422 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2423 P2P_IE_VENDOR_TYPE);
2424 if (p2p_ie == NULL)
2425 return -1;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002426 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002427 wpabuf_free(p2p_ie);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002428 return ret;
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002429}
2430
2431
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002432static void p2p_clear_go_neg(struct p2p_data *p2p)
2433{
2434 p2p->go_neg_peer = NULL;
2435 p2p_clear_timeout(p2p);
2436 p2p_set_state(p2p, P2P_IDLE);
2437}
2438
2439
2440void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2441{
2442 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002443 p2p_dbg(p2p, "No pending Group Formation - ignore WPS registration success notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002444 return; /* No pending Group Formation */
2445 }
2446
2447 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2448 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002449 p2p_dbg(p2p, "Ignore WPS registration success notification for "
2450 MACSTR " (GO Negotiation peer " MACSTR ")",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002451 MAC2STR(mac_addr),
2452 MAC2STR(p2p->go_neg_peer->intended_addr));
2453 return; /* Ignore unexpected peer address */
2454 }
2455
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002456 p2p_dbg(p2p, "Group Formation completed successfully with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002457 MAC2STR(mac_addr));
2458
2459 p2p_clear_go_neg(p2p);
2460}
2461
2462
2463void p2p_group_formation_failed(struct p2p_data *p2p)
2464{
2465 if (p2p->go_neg_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002466 p2p_dbg(p2p, "No pending Group Formation - ignore group formation failure notification");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002467 return; /* No pending Group Formation */
2468 }
2469
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002470 p2p_dbg(p2p, "Group Formation failed with " MACSTR,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002471 MAC2STR(p2p->go_neg_peer->intended_addr));
2472
2473 p2p_clear_go_neg(p2p);
2474}
2475
2476
2477struct p2p_data * p2p_init(const struct p2p_config *cfg)
2478{
2479 struct p2p_data *p2p;
2480
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07002481 if (cfg->max_peers < 1 ||
2482 cfg->passphrase_len < 8 || cfg->passphrase_len > 63)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002483 return NULL;
2484
2485 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2486 if (p2p == NULL)
2487 return NULL;
2488 p2p->cfg = (struct p2p_config *) (p2p + 1);
2489 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2490 if (cfg->dev_name)
2491 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2492 if (cfg->manufacturer)
2493 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2494 if (cfg->model_name)
2495 p2p->cfg->model_name = os_strdup(cfg->model_name);
2496 if (cfg->model_number)
2497 p2p->cfg->model_number = os_strdup(cfg->model_number);
2498 if (cfg->serial_number)
2499 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002500 if (cfg->pref_chan) {
2501 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2502 sizeof(struct p2p_channel));
2503 if (p2p->cfg->pref_chan) {
2504 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2505 cfg->num_pref_chan *
2506 sizeof(struct p2p_channel));
2507 } else
2508 p2p->cfg->num_pref_chan = 0;
2509 }
2510
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002511 p2p->min_disc_int = 1;
2512 p2p->max_disc_int = 3;
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002513 p2p->max_disc_tu = -1;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002514
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002515 if (os_get_random(&p2p->next_tie_breaker, 1) < 0)
2516 p2p->next_tie_breaker = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002517 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;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08002532 p2p->num_p2p_sd_queries = 0;
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002533
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002534 p2p_dbg(p2p, "initialized");
2535 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
2536 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
2537
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002538 return p2p;
2539}
2540
2541
2542void p2p_deinit(struct p2p_data *p2p)
2543{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002544#ifdef CONFIG_WIFI_DISPLAY
2545 wpabuf_free(p2p->wfd_ie_beacon);
2546 wpabuf_free(p2p->wfd_ie_probe_req);
2547 wpabuf_free(p2p->wfd_ie_probe_resp);
2548 wpabuf_free(p2p->wfd_ie_assoc_req);
2549 wpabuf_free(p2p->wfd_ie_invitation);
2550 wpabuf_free(p2p->wfd_ie_prov_disc_req);
2551 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
2552 wpabuf_free(p2p->wfd_ie_go_neg);
2553 wpabuf_free(p2p->wfd_dev_info);
2554 wpabuf_free(p2p->wfd_assoc_bssid);
2555 wpabuf_free(p2p->wfd_coupled_sink_info);
2556#endif /* CONFIG_WIFI_DISPLAY */
2557
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002558 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
2559 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2560 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
Dmitry Shmidt9cdf1b92013-02-27 12:58:50 -08002561 eloop_cancel_timeout(p2p_go_neg_start, p2p, NULL);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002562 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002563 p2p_flush(p2p);
2564 p2p_free_req_dev_types(p2p);
2565 os_free(p2p->cfg->dev_name);
2566 os_free(p2p->cfg->manufacturer);
2567 os_free(p2p->cfg->model_name);
2568 os_free(p2p->cfg->model_number);
2569 os_free(p2p->cfg->serial_number);
Dmitry Shmidt04949592012-07-19 12:16:46 -07002570 os_free(p2p->cfg->pref_chan);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002571 os_free(p2p->groups);
2572 wpabuf_free(p2p->sd_resp);
2573 os_free(p2p->after_scan_tx);
2574 p2p_remove_wps_vendor_extensions(p2p);
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07002575 os_free(p2p->no_go_freq.range);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002576 os_free(p2p);
2577}
2578
2579
2580void p2p_flush(struct p2p_data *p2p)
2581{
2582 struct p2p_device *dev, *prev;
Dmitry Shmidt04949592012-07-19 12:16:46 -07002583 p2p_stop_find(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002584 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2585 list) {
2586 dl_list_del(&dev->list);
2587 p2p_device_free(p2p, dev);
2588 }
2589 p2p_free_sd_queries(p2p);
2590 os_free(p2p->after_scan_tx);
2591 p2p->after_scan_tx = NULL;
2592}
2593
2594
2595int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2596{
2597 struct p2p_device *dev;
2598
2599 dev = p2p_get_device(p2p, addr);
2600 if (dev == NULL)
2601 return -1;
2602
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002603 p2p_dbg(p2p, "Unauthorizing " MACSTR, MAC2STR(addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002604
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002605 if (p2p->go_neg_peer == dev) {
2606 eloop_cancel_timeout(p2p_go_neg_wait_timeout, p2p, NULL);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002607 p2p->go_neg_peer = NULL;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002608 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002609
2610 dev->wps_method = WPS_NOT_READY;
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08002611 dev->oob_pw_id = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002612 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2613 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2614
2615 /* Check if after_scan_tx is for this peer. If so free it */
2616 if (p2p->after_scan_tx &&
2617 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2618 os_free(p2p->after_scan_tx);
2619 p2p->after_scan_tx = NULL;
2620 }
2621
2622 return 0;
2623}
2624
2625
2626int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2627{
2628 os_free(p2p->cfg->dev_name);
2629 if (dev_name) {
2630 p2p->cfg->dev_name = os_strdup(dev_name);
2631 if (p2p->cfg->dev_name == NULL)
2632 return -1;
2633 } else
2634 p2p->cfg->dev_name = NULL;
2635 return 0;
2636}
2637
2638
2639int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2640{
2641 os_free(p2p->cfg->manufacturer);
2642 p2p->cfg->manufacturer = NULL;
2643 if (manufacturer) {
2644 p2p->cfg->manufacturer = os_strdup(manufacturer);
2645 if (p2p->cfg->manufacturer == NULL)
2646 return -1;
2647 }
2648
2649 return 0;
2650}
2651
2652
2653int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2654{
2655 os_free(p2p->cfg->model_name);
2656 p2p->cfg->model_name = NULL;
2657 if (model_name) {
2658 p2p->cfg->model_name = os_strdup(model_name);
2659 if (p2p->cfg->model_name == NULL)
2660 return -1;
2661 }
2662
2663 return 0;
2664}
2665
2666
2667int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2668{
2669 os_free(p2p->cfg->model_number);
2670 p2p->cfg->model_number = NULL;
2671 if (model_number) {
2672 p2p->cfg->model_number = os_strdup(model_number);
2673 if (p2p->cfg->model_number == NULL)
2674 return -1;
2675 }
2676
2677 return 0;
2678}
2679
2680
2681int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2682{
2683 os_free(p2p->cfg->serial_number);
2684 p2p->cfg->serial_number = NULL;
2685 if (serial_number) {
2686 p2p->cfg->serial_number = os_strdup(serial_number);
2687 if (p2p->cfg->serial_number == NULL)
2688 return -1;
2689 }
2690
2691 return 0;
2692}
2693
2694
2695void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2696{
2697 p2p->cfg->config_methods = config_methods;
2698}
2699
2700
2701void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2702{
2703 os_memcpy(p2p->cfg->uuid, uuid, 16);
2704}
2705
2706
2707int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2708{
2709 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2710 return 0;
2711}
2712
2713
2714int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2715 size_t num_dev_types)
2716{
2717 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2718 num_dev_types = P2P_SEC_DEVICE_TYPES;
2719 p2p->cfg->num_sec_dev_types = num_dev_types;
2720 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2721 return 0;
2722}
2723
2724
2725void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2726{
2727 int i;
2728
2729 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2730 wpabuf_free(p2p->wps_vendor_ext[i]);
2731 p2p->wps_vendor_ext[i] = NULL;
2732 }
2733}
2734
2735
2736int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2737 const struct wpabuf *vendor_ext)
2738{
2739 int i;
2740
2741 if (vendor_ext == NULL)
2742 return -1;
2743
2744 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2745 if (p2p->wps_vendor_ext[i] == NULL)
2746 break;
2747 }
2748 if (i >= P2P_MAX_WPS_VENDOR_EXT)
2749 return -1;
2750
2751 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2752 if (p2p->wps_vendor_ext[i] == NULL)
2753 return -1;
2754
2755 return 0;
2756}
2757
2758
2759int p2p_set_country(struct p2p_data *p2p, const char *country)
2760{
2761 os_memcpy(p2p->cfg->country, country, 3);
2762 return 0;
2763}
2764
2765
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002766static int p2p_pre_find_operation(struct p2p_data *p2p, struct p2p_device *dev)
2767{
2768 if (dev->sd_pending_bcast_queries == 0) {
2769 /* Initialize with total number of registered broadcast
2770 * SD queries. */
2771 dev->sd_pending_bcast_queries = p2p->num_p2p_sd_queries;
2772 }
2773
2774 if (p2p_start_sd(p2p, dev) == 0)
2775 return 1;
2776
2777 if (dev->req_config_methods &&
2778 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2779 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
2780 MACSTR " (config methods 0x%x)",
2781 MAC2STR(dev->info.p2p_device_addr),
2782 dev->req_config_methods);
2783 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
2784 return 1;
2785 }
2786
2787 return 0;
2788}
2789
2790
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002791void p2p_continue_find(struct p2p_data *p2p)
2792{
2793 struct p2p_device *dev;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002794 int found;
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08002795
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002796 p2p_set_state(p2p, P2P_SEARCH);
2797
2798 /* Continue from the device following the last iteration */
2799 found = 0;
2800 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2801 if (dev == p2p->last_p2p_find_oper) {
2802 found = 1;
2803 continue;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002804 }
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002805 if (!found)
2806 continue;
2807 if (p2p_pre_find_operation(p2p, dev) > 0) {
2808 p2p->last_p2p_find_oper = dev;
2809 return;
2810 }
2811 }
2812
2813 /*
2814 * Wrap around to the beginning of the list and continue until the last
2815 * iteration device.
2816 */
2817 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2818 if (p2p_pre_find_operation(p2p, dev) > 0) {
2819 p2p->last_p2p_find_oper = dev;
2820 return;
2821 }
2822 if (dev == p2p->last_p2p_find_oper)
2823 break;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002824 }
2825
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002826 p2p_listen_in_find(p2p, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002827}
2828
2829
2830static void p2p_sd_cb(struct p2p_data *p2p, int success)
2831{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002832 p2p_dbg(p2p, "Service Discovery Query TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002833 success);
2834 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2835
2836 if (!success) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08002837 if (p2p->sd_peer)
2838 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08002839 p2p->sd_peer = NULL;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002840 p2p_continue_find(p2p);
2841 return;
2842 }
2843
2844 if (p2p->sd_peer == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002845 p2p_dbg(p2p, "No SD peer entry known");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002846 p2p_continue_find(p2p);
2847 return;
2848 }
2849
Dmitry Shmidt807291d2015-01-27 13:40:23 -08002850 if (p2p->sd_query && p2p->sd_query->for_all_peers) {
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07002851 /* Update the pending broadcast SD query count for this device
2852 */
2853 p2p->sd_peer->sd_pending_bcast_queries--;
2854
2855 /*
2856 * If there are no pending broadcast queries for this device,
2857 * mark it as done (-1).
2858 */
2859 if (p2p->sd_peer->sd_pending_bcast_queries == 0)
2860 p2p->sd_peer->sd_pending_bcast_queries = -1;
2861 }
2862
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002863 /* Wait for response from the peer */
2864 p2p_set_state(p2p, P2P_SD_DURING_FIND);
2865 p2p_set_timeout(p2p, 0, 200000);
2866}
2867
Jouni Malinen75ecf522011-06-27 15:19:46 -07002868
2869/**
2870 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2871 * @p2p: P2P module context from p2p_init()
2872 */
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08002873static void p2p_retry_pd(struct p2p_data *p2p)
Jouni Malinen75ecf522011-06-27 15:19:46 -07002874{
2875 struct p2p_device *dev;
2876
2877 if (p2p->state != P2P_IDLE)
2878 return;
2879
2880 /*
2881 * Retry the prov disc req attempt only for the peer that the user had
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002882 * requested.
Jouni Malinen75ecf522011-06-27 15:19:46 -07002883 */
2884
2885 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2886 if (os_memcmp(p2p->pending_pd_devaddr,
2887 dev->info.p2p_device_addr, ETH_ALEN) != 0)
2888 continue;
2889 if (!dev->req_config_methods)
2890 continue;
Jouni Malinen75ecf522011-06-27 15:19:46 -07002891
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002892 p2p_dbg(p2p, "Send pending Provision Discovery Request to "
Jouni Malinen75ecf522011-06-27 15:19:46 -07002893 MACSTR " (config methods 0x%x)",
2894 MAC2STR(dev->info.p2p_device_addr),
2895 dev->req_config_methods);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08002896 p2p_send_prov_disc_req(p2p, dev,
Dmitry Shmidt051af732013-10-22 13:52:46 -07002897 dev->flags & P2P_DEV_PD_FOR_JOIN,
2898 p2p->pd_force_freq);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002899 return;
2900 }
2901}
2902
2903
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002904static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2905{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002906 p2p_dbg(p2p, "Provision Discovery Request TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002907 success);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002908
2909 /*
2910 * Postpone resetting the pending action state till after we actually
2911 * time out. This allows us to take some action like notifying any
2912 * interested parties about no response to the request.
2913 *
2914 * When the timer (below) goes off we check in IDLE, SEARCH, or
2915 * LISTEN_ONLY state, which are the only allowed states to issue a PD
2916 * requests in, if this was still pending and then raise notification.
2917 */
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002918
2919 if (!success) {
Jouni Malinen75ecf522011-06-27 15:19:46 -07002920 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2921
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002922 if (p2p->user_initiated_pd &&
2923 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
2924 {
2925 /* Retry request from timeout to avoid busy loops */
2926 p2p->pending_action_state = P2P_PENDING_PD;
2927 p2p_set_timeout(p2p, 0, 50000);
2928 } else if (p2p->state != P2P_IDLE)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002929 p2p_continue_find(p2p);
Jouni Malinen75ecf522011-06-27 15:19:46 -07002930 else if (p2p->user_initiated_pd) {
2931 p2p->pending_action_state = P2P_PENDING_PD;
2932 p2p_set_timeout(p2p, 0, 300000);
2933 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002934 return;
2935 }
2936
Jouni Malinen75ecf522011-06-27 15:19:46 -07002937 /*
2938 * This postponing, of resetting pending_action_state, needs to be
2939 * done only for user initiated PD requests and not internal ones.
2940 */
2941 if (p2p->user_initiated_pd)
2942 p2p->pending_action_state = P2P_PENDING_PD;
2943 else
2944 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2945
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002946 /* Wait for response from the peer */
2947 if (p2p->state == P2P_SEARCH)
2948 p2p_set_state(p2p, P2P_PD_DURING_FIND);
2949 p2p_set_timeout(p2p, 0, 200000);
2950}
2951
2952
2953int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002954 struct os_reltime *rx_time, int level, const u8 *ies,
Dmitry Shmidta54fa5f2013-01-15 13:53:35 -08002955 size_t ies_len)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002956{
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08002957 if (os_reltime_before(rx_time, &p2p->find_start)) {
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002958 /*
2959 * The driver may have cached (e.g., in cfg80211 BSS table) the
2960 * scan results for relatively long time. To avoid reporting
2961 * stale information, update P2P peers only based on results
2962 * that have based on frames received after the last p2p_find
2963 * operation was started.
2964 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002965 p2p_dbg(p2p, "Ignore old scan result for " MACSTR
2966 " (rx_time=%u.%06u)",
Dmitry Shmidtf8623282013-02-20 14:34:59 -08002967 MAC2STR(bssid), (unsigned int) rx_time->sec,
2968 (unsigned int) rx_time->usec);
2969 return 0;
2970 }
2971
2972 p2p_add_device(p2p, bssid, freq, rx_time, level, ies, ies_len, 1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002973
2974 return 0;
2975}
2976
2977
2978void p2p_scan_res_handled(struct p2p_data *p2p)
2979{
2980 if (!p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07002981 p2p_dbg(p2p, "p2p_scan was not running, but scan results received");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002982 }
2983 p2p->p2p_scan_running = 0;
2984 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2985
2986 if (p2p_run_after_scan(p2p))
2987 return;
2988 if (p2p->state == P2P_SEARCH)
2989 p2p_continue_find(p2p);
2990}
2991
2992
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08002993void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07002994{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07002995 u8 *len;
2996
2997#ifdef CONFIG_WIFI_DISPLAY
2998 if (p2p->wfd_ie_probe_req)
2999 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
3000#endif /* CONFIG_WIFI_DISPLAY */
3001
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003002 if (p2p->vendor_elem && p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3003 wpabuf_put_buf(ies,
3004 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3005
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003006 len = p2p_buf_add_ie_hdr(ies);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003007 p2p_buf_add_capability(ies, p2p->dev_capab &
3008 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003009 if (dev_id)
3010 p2p_buf_add_device_id(ies, dev_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003011 if (p2p->cfg->reg_class && p2p->cfg->channel)
3012 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
3013 p2p->cfg->reg_class,
3014 p2p->cfg->channel);
3015 if (p2p->ext_listen_interval)
3016 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
3017 p2p->ext_listen_interval);
3018 /* TODO: p2p_buf_add_operating_channel() if GO */
3019 p2p_buf_update_ie_hdr(ies, len);
3020}
3021
3022
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003023size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
3024{
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003025 size_t len = 100;
3026
3027#ifdef CONFIG_WIFI_DISPLAY
3028 if (p2p && p2p->wfd_ie_probe_req)
3029 len += wpabuf_len(p2p->wfd_ie_probe_req);
3030#endif /* CONFIG_WIFI_DISPLAY */
3031
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07003032 if (p2p && p2p->vendor_elem &&
3033 p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P])
3034 len += wpabuf_len(p2p->vendor_elem[VENDOR_ELEM_PROBE_REQ_P2P]);
3035
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003036 return len;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003037}
3038
3039
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003040int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
3041{
3042 return p2p_attr_text(p2p_ie, buf, end);
3043}
3044
3045
3046static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
3047{
3048 struct p2p_device *dev = p2p->go_neg_peer;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003049 int timeout;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003050
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003051 p2p_dbg(p2p, "GO Negotiation Request TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003052
3053 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003054 p2p_dbg(p2p, "No pending GO Negotiation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003055 return;
3056 }
3057
3058 if (success) {
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003059 if (dev->flags & P2P_DEV_USER_REJECTED) {
3060 p2p_set_state(p2p, P2P_IDLE);
3061 return;
3062 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003063 } else if (dev->go_neg_req_sent) {
3064 /* Cancel the increment from p2p_connect_send() on failure */
Dmitry Shmidt98f9e762012-05-30 11:18:46 -07003065 dev->go_neg_req_sent--;
3066 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003067
3068 if (!success &&
3069 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
3070 !is_zero_ether_addr(dev->member_in_go_dev)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003071 p2p_dbg(p2p, "Peer " MACSTR " did not acknowledge request - try to use device discoverability through its GO",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003072 MAC2STR(dev->info.p2p_device_addr));
3073 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3074 p2p_send_dev_disc_req(p2p, dev);
3075 return;
3076 }
3077
3078 /*
3079 * Use P2P find, if needed, to find the other device from its listen
3080 * channel.
3081 */
3082 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003083 timeout = success ? 500000 : 100000;
3084 if (!success && p2p->go_neg_peer &&
3085 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE)) {
3086 unsigned int r;
3087 /*
3088 * Peer is expected to wait our response and we will skip the
3089 * listen phase. Add some randomness to the wait time here to
3090 * make it less likely to hit cases where we could end up in
3091 * sync with peer not listening.
3092 */
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07003093 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
3094 r = 0;
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003095 timeout += r % 100000;
3096 }
3097 p2p_set_timeout(p2p, 0, timeout);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003098}
3099
3100
3101static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
3102{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003103 p2p_dbg(p2p, "GO Negotiation Response TX callback: success=%d",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003104 success);
3105 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003106 p2p_dbg(p2p, "Ignore TX callback event - GO Negotiation is not running anymore");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003107 return;
3108 }
3109 p2p_set_state(p2p, P2P_CONNECT);
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003110 p2p_set_timeout(p2p, 0, 500000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003111}
3112
3113
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003114static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success,
3115 const u8 *addr)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003116{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003117 p2p_dbg(p2p, "GO Negotiation Response (failure) TX callback: success=%d", success);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003118 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003119 p2p_go_neg_failed(p2p, p2p->go_neg_peer->status);
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003120 } else if (success) {
3121 struct p2p_device *dev;
3122 dev = p2p_get_device(p2p, addr);
3123 if (dev &&
Jithu Jance1d57ba22014-08-19 14:19:38 -07003124 dev->status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003125 dev->flags |= P2P_DEV_PEER_WAITING_RESPONSE;
Jithu Jance1d57ba22014-08-19 14:19:38 -07003126 if ((p2p->state == P2P_SEARCH) ||
3127 (p2p->state == P2P_LISTEN_ONLY)) {
3128 /* Clear our search state or Listen state since
3129 * now peer is awaiting response from our side.
3130 */
3131 p2p_dbg(p2p, "Clear the P2P discovery state");
3132 p2p_stop_find(p2p);
3133 }
3134 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003135 }
3136}
3137
3138
3139static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
3140 enum p2p_send_action_result result)
3141{
3142 struct p2p_device *dev;
3143
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003144 p2p_dbg(p2p, "GO Negotiation Confirm TX callback: result=%d", result);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003145 if (result == P2P_SEND_ACTION_FAILED) {
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003146 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003147 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003148 return;
3149 }
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003150
3151 dev = p2p->go_neg_peer;
3152
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003153 if (result == P2P_SEND_ACTION_NO_ACK) {
3154 /*
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003155 * Retry GO Negotiation Confirmation
3156 * P2P_GO_NEG_CNF_MAX_RETRY_COUNT times if we did not receive
3157 * ACK for confirmation.
3158 */
3159 if (dev && dev->go_neg_conf &&
3160 dev->go_neg_conf_sent <= P2P_GO_NEG_CNF_MAX_RETRY_COUNT) {
3161 p2p_dbg(p2p, "GO Negotiation Confirm retry %d",
3162 dev->go_neg_conf_sent);
3163 p2p->pending_action_state = P2P_PENDING_GO_NEG_CONFIRM;
3164 if (p2p_send_action(p2p, dev->go_neg_conf_freq,
3165 dev->info.p2p_device_addr,
3166 p2p->cfg->dev_addr,
3167 dev->info.p2p_device_addr,
3168 wpabuf_head(dev->go_neg_conf),
3169 wpabuf_len(dev->go_neg_conf), 0) >=
3170 0) {
3171 dev->go_neg_conf_sent++;
3172 return;
3173 }
3174 p2p_dbg(p2p, "Failed to re-send Action frame");
3175
3176 /*
3177 * Continue with the assumption that the first attempt
3178 * went through and just the ACK frame was lost.
3179 */
3180 }
3181
3182 /*
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003183 * It looks like the TX status for GO Negotiation Confirm is
3184 * often showing failure even when the peer has actually
3185 * received the frame. Since the peer may change channels
3186 * immediately after having received the frame, we may not see
3187 * an Ack for retries, so just dropping a single frame may
3188 * trigger this. To allow the group formation to succeed if the
3189 * peer did indeed receive the frame, continue regardless of
3190 * the TX status.
3191 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003192 p2p_dbg(p2p, "Assume GO Negotiation Confirm TX was actually received by the peer even though Ack was not reported");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003193 }
3194
Dmitry Shmidt413dde72014-04-11 10:23:22 -07003195 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3196
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003197 if (dev == NULL)
3198 return;
3199
3200 p2p_go_complete(p2p, dev);
3201}
3202
3203
3204void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3205 const u8 *src, const u8 *bssid,
3206 enum p2p_send_action_result result)
3207{
3208 enum p2p_pending_action_state state;
3209 int success;
3210
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003211 p2p_dbg(p2p, "Action frame TX callback (state=%d freq=%u dst=" MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003212 " src=" MACSTR " bssid=" MACSTR " result=%d",
3213 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
3214 MAC2STR(bssid), result);
3215 success = result == P2P_SEND_ACTION_SUCCESS;
3216 state = p2p->pending_action_state;
3217 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3218 switch (state) {
3219 case P2P_NO_PENDING_ACTION:
Dmitry Shmidt4582d2a2014-02-28 11:14:23 -08003220 if (p2p->send_action_in_progress) {
3221 p2p->send_action_in_progress = 0;
3222 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3223 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003224 if (p2p->after_scan_tx_in_progress) {
3225 p2p->after_scan_tx_in_progress = 0;
3226 if (p2p->start_after_scan != P2P_AFTER_SCAN_NOTHING &&
3227 p2p_run_after_scan(p2p))
3228 break;
3229 if (p2p->state == P2P_SEARCH) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003230 p2p_dbg(p2p, "Continue find after after_scan_tx completion");
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003231 p2p_continue_find(p2p);
3232 }
3233 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003234 break;
3235 case P2P_PENDING_GO_NEG_REQUEST:
3236 p2p_go_neg_req_cb(p2p, success);
3237 break;
3238 case P2P_PENDING_GO_NEG_RESPONSE:
3239 p2p_go_neg_resp_cb(p2p, success);
3240 break;
3241 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
Dmitry Shmidt4b060592013-04-29 16:42:49 -07003242 p2p_go_neg_resp_failure_cb(p2p, success, dst);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003243 break;
3244 case P2P_PENDING_GO_NEG_CONFIRM:
3245 p2p_go_neg_conf_cb(p2p, result);
3246 break;
3247 case P2P_PENDING_SD:
3248 p2p_sd_cb(p2p, success);
3249 break;
3250 case P2P_PENDING_PD:
3251 p2p_prov_disc_cb(p2p, success);
3252 break;
3253 case P2P_PENDING_INVITATION_REQUEST:
3254 p2p_invitation_req_cb(p2p, success);
3255 break;
3256 case P2P_PENDING_INVITATION_RESPONSE:
3257 p2p_invitation_resp_cb(p2p, success);
3258 break;
3259 case P2P_PENDING_DEV_DISC_REQUEST:
3260 p2p_dev_disc_req_cb(p2p, success);
3261 break;
3262 case P2P_PENDING_DEV_DISC_RESPONSE:
3263 p2p_dev_disc_resp_cb(p2p, success);
3264 break;
3265 case P2P_PENDING_GO_DISC_REQ:
3266 p2p_go_disc_req_cb(p2p, success);
3267 break;
3268 }
Dmitry Shmidt8da800a2013-04-24 12:57:01 -07003269
3270 p2p->after_scan_tx_in_progress = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003271}
3272
3273
3274void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3275 unsigned int duration)
3276{
3277 if (freq == p2p->pending_client_disc_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003278 p2p_dbg(p2p, "Client discoverability remain-awake completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003279 p2p->pending_client_disc_freq = 0;
3280 return;
3281 }
3282
3283 if (freq != p2p->pending_listen_freq) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003284 p2p_dbg(p2p, "Unexpected listen callback for freq=%u duration=%u (pending_listen_freq=%u)",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003285 freq, duration, p2p->pending_listen_freq);
3286 return;
3287 }
3288
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003289 p2p_dbg(p2p, "Starting Listen timeout(%u,%u) on freq=%u based on callback",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003290 p2p->pending_listen_sec, p2p->pending_listen_usec,
3291 p2p->pending_listen_freq);
3292 p2p->in_listen = 1;
3293 p2p->drv_in_listen = freq;
3294 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3295 /*
3296 * Add 20 msec extra wait to avoid race condition with driver
3297 * remain-on-channel end event, i.e., give driver more time to
3298 * complete the operation before our timeout expires.
3299 */
3300 p2p_set_timeout(p2p, p2p->pending_listen_sec,
3301 p2p->pending_listen_usec + 20000);
3302 }
3303
3304 p2p->pending_listen_freq = 0;
3305}
3306
3307
3308int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3309{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003310 p2p_dbg(p2p, "Driver ended Listen state (freq=%u)", freq);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003311 p2p->drv_in_listen = 0;
3312 if (p2p->in_listen)
3313 return 0; /* Internal timeout will trigger the next step */
3314
3315 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3316 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003317 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003318 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003319 return 0;
3320 }
3321
3322 p2p_set_state(p2p, P2P_CONNECT);
3323 p2p_connect_send(p2p, p2p->go_neg_peer);
3324 return 1;
3325 } else if (p2p->state == P2P_SEARCH) {
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003326 if (p2p->p2p_scan_running) {
3327 /*
3328 * Search is already in progress. This can happen if
3329 * an Action frame RX is reported immediately after
3330 * the end of a remain-on-channel operation and the
3331 * response frame to that is sent using an offchannel
3332 * operation while in p2p_find. Avoid an attempt to
3333 * restart a scan here.
3334 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003335 p2p_dbg(p2p, "p2p_scan already in progress - do not try to start a new one");
Dmitry Shmidtc5ec7f52012-03-06 16:33:24 -08003336 return 1;
3337 }
Dmitry Shmidt04949592012-07-19 12:16:46 -07003338 if (p2p->pending_listen_freq) {
3339 /*
3340 * Better wait a bit if the driver is unable to start
3341 * offchannel operation for some reason. p2p_search()
3342 * will be started from internal timeout.
3343 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003344 p2p_dbg(p2p, "Listen operation did not seem to start - delay search phase to avoid busy loop");
Dmitry Shmidt04949592012-07-19 12:16:46 -07003345 p2p_set_timeout(p2p, 0, 100000);
3346 return 1;
3347 }
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003348 if (p2p->search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003349 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003350 p2p->search_delay);
3351 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3352 (p2p->search_delay % 1000) * 1000);
3353 return 1;
3354 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003355 p2p_search(p2p);
3356 return 1;
3357 }
3358
3359 return 0;
3360}
3361
3362
3363static void p2p_timeout_connect(struct p2p_data *p2p)
3364{
3365 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003366 if (p2p->go_neg_peer &&
3367 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003368 p2p_dbg(p2p, "Wait for GO Negotiation Confirm timed out - assume GO Negotiation failed");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003369 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt04949592012-07-19 12:16:46 -07003370 return;
3371 }
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003372 if (p2p->go_neg_peer &&
3373 (p2p->go_neg_peer->flags & P2P_DEV_PEER_WAITING_RESPONSE) &&
3374 p2p->go_neg_peer->connect_reqs < 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003375 p2p_dbg(p2p, "Peer expected to wait our response - skip listen");
Dmitry Shmidt8c652892013-03-01 10:14:01 -08003376 p2p_connect_send(p2p, p2p->go_neg_peer);
3377 return;
3378 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003379 if (p2p->go_neg_peer && p2p->go_neg_peer->oob_go_neg_freq > 0) {
3380 p2p_dbg(p2p, "Skip connect-listen since GO Neg channel known (OOB)");
3381 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3382 p2p_set_timeout(p2p, 0, 30000);
3383 return;
3384 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003385 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003386 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003387}
3388
3389
3390static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3391{
3392 if (p2p->go_neg_peer) {
3393 if (p2p->drv_in_listen) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003394 p2p_dbg(p2p, "Driver is still in Listen state; wait for it to complete");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003395 return;
3396 }
3397
3398 if (p2p->go_neg_peer->connect_reqs >= 120) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003399 p2p_dbg(p2p, "Timeout on sending GO Negotiation Request without getting response");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003400 p2p_go_neg_failed(p2p, -1);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003401 return;
3402 }
3403
3404 p2p_set_state(p2p, P2P_CONNECT);
3405 p2p_connect_send(p2p, p2p->go_neg_peer);
3406 } else
3407 p2p_set_state(p2p, P2P_IDLE);
3408}
3409
3410
3411static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3412{
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003413 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
Dmitry Shmidt18463232014-01-24 12:29:41 -08003414
3415 if (p2p->cfg->is_concurrent_session_active &&
3416 p2p->cfg->is_concurrent_session_active(p2p->cfg->cb_ctx))
3417 p2p_set_timeout(p2p, 0, 500000);
3418 else
3419 p2p_set_timeout(p2p, 0, 200000);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003420}
3421
3422
3423static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3424{
3425 struct p2p_device *dev = p2p->go_neg_peer;
3426
3427 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003428 p2p_dbg(p2p, "Unknown GO Neg peer - stop GO Neg wait");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003429 return;
3430 }
3431
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003432 p2p_dbg(p2p, "Go to Listen state while waiting for the peer to become ready for GO Negotiation");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003433 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003434 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003435}
3436
3437
3438static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3439{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003440 p2p_dbg(p2p, "Service Discovery Query timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003441 if (p2p->sd_peer) {
3442 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003443 p2p->sd_peer = NULL;
3444 }
3445 p2p_continue_find(p2p);
3446}
3447
3448
3449static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
3450{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003451 p2p_dbg(p2p, "Provision Discovery Request timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003452 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3453 p2p_continue_find(p2p);
3454}
3455
3456
Jouni Malinen75ecf522011-06-27 15:19:46 -07003457static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
3458{
3459 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3460
3461 /*
3462 * For user initiated PD requests that we have not gotten any responses
3463 * for while in IDLE state, we retry them a couple of times before
3464 * giving up.
3465 */
3466 if (!p2p->user_initiated_pd)
3467 return;
3468
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003469 p2p_dbg(p2p, "User initiated Provision Discovery Request timeout");
Jouni Malinen75ecf522011-06-27 15:19:46 -07003470
3471 if (p2p->pd_retries) {
3472 p2p->pd_retries--;
3473 p2p_retry_pd(p2p);
3474 } else {
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003475 struct p2p_device *dev;
3476 int for_join = 0;
3477
3478 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
3479 if (os_memcmp(p2p->pending_pd_devaddr,
3480 dev->info.p2p_device_addr, ETH_ALEN) != 0)
3481 continue;
3482 if (dev->req_config_methods &&
3483 (dev->flags & P2P_DEV_PD_FOR_JOIN))
3484 for_join = 1;
3485 }
3486
Jouni Malinen75ecf522011-06-27 15:19:46 -07003487 if (p2p->cfg->prov_disc_fail)
3488 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
3489 p2p->pending_pd_devaddr,
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003490 for_join ?
3491 P2P_PROV_DISC_TIMEOUT_JOIN :
Jouni Malinen75ecf522011-06-27 15:19:46 -07003492 P2P_PROV_DISC_TIMEOUT);
3493 p2p_reset_pending_pd(p2p);
3494 }
3495}
3496
3497
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003498static void p2p_timeout_invite(struct p2p_data *p2p)
3499{
3500 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3501 p2p_set_state(p2p, P2P_INVITE_LISTEN);
3502 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
3503 /*
3504 * Better remain on operating channel instead of listen channel
3505 * when running a group.
3506 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003507 p2p_dbg(p2p, "Inviting in active GO role - wait on operating channel");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003508 p2p_set_timeout(p2p, 0, 100000);
3509 return;
3510 }
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08003511 p2p_listen_in_find(p2p, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003512}
3513
3514
3515static void p2p_timeout_invite_listen(struct p2p_data *p2p)
3516{
3517 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
3518 p2p_set_state(p2p, P2P_INVITE);
3519 p2p_invite_send(p2p, p2p->invite_peer,
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003520 p2p->invite_go_dev_addr, p2p->invite_dev_pw_id);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003521 } else {
3522 if (p2p->invite_peer) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003523 p2p_dbg(p2p, "Invitation Request retry limit reached");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003524 if (p2p->cfg->invitation_result)
3525 p2p->cfg->invitation_result(
Dmitry Shmidt700a1372013-03-15 14:14:44 -07003526 p2p->cfg->cb_ctx, -1, NULL, NULL,
Dmitry Shmidte0e48dc2013-11-18 12:00:06 -08003527 p2p->invite_peer->info.p2p_device_addr,
Dmitry Shmidt15907092014-03-25 10:42:57 -07003528 0, 0);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003529 }
3530 p2p_set_state(p2p, P2P_IDLE);
3531 }
3532}
3533
3534
3535static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
3536{
3537 struct p2p_data *p2p = eloop_ctx;
3538
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003539 p2p_dbg(p2p, "Timeout (state=%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003540
3541 p2p->in_listen = 0;
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003542 if (p2p->drv_in_listen) {
3543 p2p_dbg(p2p, "Driver is still in listen state - stop it");
3544 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
3545 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003546
3547 switch (p2p->state) {
3548 case P2P_IDLE:
Jouni Malinen75ecf522011-06-27 15:19:46 -07003549 /* Check if we timed out waiting for PD req */
3550 if (p2p->pending_action_state == P2P_PENDING_PD)
3551 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003552 break;
3553 case P2P_SEARCH:
Jouni Malinen75ecf522011-06-27 15:19:46 -07003554 /* Check if we timed out waiting for PD req */
3555 if (p2p->pending_action_state == P2P_PENDING_PD)
3556 p2p_timeout_prov_disc_req(p2p);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003557 if (p2p->search_delay && !p2p->in_search_delay) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003558 p2p_dbg(p2p, "Delay search operation by %u ms",
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003559 p2p->search_delay);
3560 p2p->in_search_delay = 1;
3561 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3562 (p2p->search_delay % 1000) * 1000);
3563 break;
3564 }
3565 p2p->in_search_delay = 0;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003566 p2p_search(p2p);
3567 break;
3568 case P2P_CONNECT:
3569 p2p_timeout_connect(p2p);
3570 break;
3571 case P2P_CONNECT_LISTEN:
3572 p2p_timeout_connect_listen(p2p);
3573 break;
3574 case P2P_GO_NEG:
3575 break;
3576 case P2P_LISTEN_ONLY:
Jouni Malinen75ecf522011-06-27 15:19:46 -07003577 /* Check if we timed out waiting for PD req */
3578 if (p2p->pending_action_state == P2P_PENDING_PD)
3579 p2p_timeout_prov_disc_req(p2p);
3580
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003581 if (p2p->ext_listen_only) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003582 p2p_dbg(p2p, "Extended Listen Timing - Listen State completed");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003583 p2p->ext_listen_only = 0;
3584 p2p_set_state(p2p, P2P_IDLE);
3585 }
3586 break;
3587 case P2P_WAIT_PEER_CONNECT:
3588 p2p_timeout_wait_peer_connect(p2p);
3589 break;
3590 case P2P_WAIT_PEER_IDLE:
3591 p2p_timeout_wait_peer_idle(p2p);
3592 break;
3593 case P2P_SD_DURING_FIND:
3594 p2p_timeout_sd_during_find(p2p);
3595 break;
3596 case P2P_PROVISIONING:
3597 break;
3598 case P2P_PD_DURING_FIND:
3599 p2p_timeout_prov_disc_during_find(p2p);
3600 break;
3601 case P2P_INVITE:
3602 p2p_timeout_invite(p2p);
3603 break;
3604 case P2P_INVITE_LISTEN:
3605 p2p_timeout_invite_listen(p2p);
3606 break;
3607 }
3608}
3609
3610
3611int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
3612{
3613 struct p2p_device *dev;
3614
3615 dev = p2p_get_device(p2p, peer_addr);
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003616 p2p_dbg(p2p, "Local request to reject connection attempts by peer "
3617 MACSTR, MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003618 if (dev == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003619 p2p_dbg(p2p, "Peer " MACSTR " unknown", MAC2STR(peer_addr));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003620 return -1;
3621 }
3622 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
3623 dev->flags |= P2P_DEV_USER_REJECTED;
3624 return 0;
3625}
3626
3627
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003628const char * p2p_wps_method_text(enum p2p_wps_method method)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003629{
3630 switch (method) {
3631 case WPS_NOT_READY:
3632 return "not-ready";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003633 case WPS_PIN_DISPLAY:
3634 return "Display";
3635 case WPS_PIN_KEYPAD:
3636 return "Keypad";
3637 case WPS_PBC:
3638 return "PBC";
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08003639 case WPS_NFC:
3640 return "NFC";
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003641 }
3642
3643 return "??";
3644}
3645
3646
3647static const char * p2p_go_state_text(enum p2p_go_state go_state)
3648{
3649 switch (go_state) {
3650 case UNKNOWN_GO:
3651 return "unknown";
3652 case LOCAL_GO:
3653 return "local";
3654 case REMOTE_GO:
3655 return "remote";
3656 }
3657
3658 return "??";
3659}
3660
3661
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003662const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
3663 const u8 *addr, int next)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003664{
3665 struct p2p_device *dev;
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003666
3667 if (addr)
3668 dev = p2p_get_device(p2p, addr);
3669 else
3670 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3671
3672 if (dev && next) {
3673 dev = dl_list_first(&dev->list, struct p2p_device, list);
3674 if (&dev->list == &p2p->devices)
3675 dev = NULL;
3676 }
3677
3678 if (dev == NULL)
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003679 return NULL;
3680
3681 return &dev->info;
3682}
3683
3684
3685int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
3686 char *buf, size_t buflen)
3687{
3688 struct p2p_device *dev;
3689 int res;
3690 char *pos, *end;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003691 struct os_reltime now;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003692
3693 if (info == NULL)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003694 return -1;
3695
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003696 dev = (struct p2p_device *) (((u8 *) info) -
3697 offsetof(struct p2p_device, info));
3698
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003699 pos = buf;
3700 end = buf + buflen;
3701
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08003702 os_get_reltime(&now);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003703 res = os_snprintf(pos, end - pos,
3704 "age=%d\n"
3705 "listen_freq=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003706 "wps_method=%s\n"
3707 "interface_addr=" MACSTR "\n"
3708 "member_in_go_dev=" MACSTR "\n"
3709 "member_in_go_iface=" MACSTR "\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003710 "go_neg_req_sent=%d\n"
3711 "go_state=%s\n"
3712 "dialog_token=%u\n"
3713 "intended_addr=" MACSTR "\n"
3714 "country=%c%c\n"
3715 "oper_freq=%d\n"
3716 "req_config_methods=0x%x\n"
Dmitry Shmidt13ca8d82014-02-20 10:18:40 -08003717 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003718 "status=%d\n"
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003719 "invitation_reqs=%u\n",
3720 (int) (now.sec - dev->last_seen.sec),
3721 dev->listen_freq,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003722 p2p_wps_method_text(dev->wps_method),
3723 MAC2STR(dev->interface_addr),
3724 MAC2STR(dev->member_in_go_dev),
3725 MAC2STR(dev->member_in_go_iface),
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003726 dev->go_neg_req_sent,
3727 p2p_go_state_text(dev->go_state),
3728 dev->dialog_token,
3729 MAC2STR(dev->intended_addr),
3730 dev->country[0] ? dev->country[0] : '_',
3731 dev->country[1] ? dev->country[1] : '_',
3732 dev->oper_freq,
3733 dev->req_config_methods,
3734 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
3735 "[PROBE_REQ_ONLY]" : "",
3736 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
3737 dev->flags & P2P_DEV_NOT_YET_READY ?
3738 "[NOT_YET_READY]" : "",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003739 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
3740 "[PD_PEER_DISPLAY]" : "",
3741 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
3742 "[PD_PEER_KEYPAD]" : "",
3743 dev->flags & P2P_DEV_USER_REJECTED ?
3744 "[USER_REJECTED]" : "",
3745 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
3746 "[PEER_WAITING_RESPONSE]" : "",
3747 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
3748 "[PREFER_PERSISTENT_GROUP]" : "",
3749 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
3750 "[WAIT_GO_NEG_RESPONSE]" : "",
3751 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
3752 "[WAIT_GO_NEG_CONFIRM]" : "",
3753 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
3754 "[GROUP_CLIENT_ONLY]" : "",
3755 dev->flags & P2P_DEV_FORCE_FREQ ?
3756 "[FORCE_FREQ]" : "",
3757 dev->flags & P2P_DEV_PD_FOR_JOIN ?
3758 "[PD_FOR_JOIN]" : "",
3759 dev->status,
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003760 dev->invitation_reqs);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003761 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003762 return pos - buf;
3763 pos += res;
3764
3765 if (dev->ext_listen_period) {
3766 res = os_snprintf(pos, end - pos,
3767 "ext_listen_period=%u\n"
3768 "ext_listen_interval=%u\n",
3769 dev->ext_listen_period,
3770 dev->ext_listen_interval);
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003771 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003772 return pos - buf;
3773 pos += res;
3774 }
3775
3776 if (dev->oper_ssid_len) {
3777 res = os_snprintf(pos, end - pos,
3778 "oper_ssid=%s\n",
3779 wpa_ssid_txt(dev->oper_ssid,
3780 dev->oper_ssid_len));
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003781 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003782 return pos - buf;
3783 pos += res;
3784 }
3785
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003786#ifdef CONFIG_WIFI_DISPLAY
3787 if (dev->info.wfd_subelems) {
3788 res = os_snprintf(pos, end - pos, "wfd_subelems=");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003789 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003790 return pos - buf;
3791 pos += res;
3792
3793 pos += wpa_snprintf_hex(pos, end - pos,
3794 wpabuf_head(dev->info.wfd_subelems),
3795 wpabuf_len(dev->info.wfd_subelems));
3796
3797 res = os_snprintf(pos, end - pos, "\n");
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08003798 if (os_snprintf_error(end - pos, res))
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07003799 return pos - buf;
3800 pos += res;
3801 }
3802#endif /* CONFIG_WIFI_DISPLAY */
3803
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003804 return pos - buf;
3805}
3806
3807
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08003808int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
3809{
3810 return p2p_get_device(p2p, addr) != NULL;
3811}
3812
3813
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003814void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
3815{
3816 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003817 p2p_dbg(p2p, "Client discoverability enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003818 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3819 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003820 p2p_dbg(p2p, "Client discoverability disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003821 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3822 }
3823}
3824
3825
3826static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
3827 u32 duration2, u32 interval2)
3828{
3829 struct wpabuf *req;
3830 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
3831 u8 *len;
3832
3833 req = wpabuf_alloc(100);
3834 if (req == NULL)
3835 return NULL;
3836
3837 if (duration1 || interval1) {
3838 os_memset(&desc1, 0, sizeof(desc1));
3839 desc1.count_type = 1;
3840 desc1.duration = duration1;
3841 desc1.interval = interval1;
3842 ptr1 = &desc1;
3843
3844 if (duration2 || interval2) {
3845 os_memset(&desc2, 0, sizeof(desc2));
3846 desc2.count_type = 2;
3847 desc2.duration = duration2;
3848 desc2.interval = interval2;
3849 ptr2 = &desc2;
3850 }
3851 }
3852
3853 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
3854 len = p2p_buf_add_ie_hdr(req);
3855 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
3856 p2p_buf_update_ie_hdr(req, len);
3857
3858 return req;
3859}
3860
3861
3862int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
3863 const u8 *own_interface_addr, unsigned int freq,
3864 u32 duration1, u32 interval1, u32 duration2,
3865 u32 interval2)
3866{
3867 struct wpabuf *req;
3868
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003869 p2p_dbg(p2p, "Send Presence Request to GO " MACSTR
3870 " (own interface " MACSTR ") freq=%u dur1=%u int1=%u "
3871 "dur2=%u int2=%u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003872 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
3873 freq, duration1, interval1, duration2, interval2);
3874
3875 req = p2p_build_presence_req(duration1, interval1, duration2,
3876 interval2);
3877 if (req == NULL)
3878 return -1;
3879
3880 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3881 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3882 go_interface_addr,
3883 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003884 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003885 }
3886 wpabuf_free(req);
3887
3888 return 0;
3889}
3890
3891
3892static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3893 size_t noa_len, u8 dialog_token)
3894{
3895 struct wpabuf *resp;
3896 u8 *len;
3897
3898 resp = wpabuf_alloc(100 + noa_len);
3899 if (resp == NULL)
3900 return NULL;
3901
3902 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3903 len = p2p_buf_add_ie_hdr(resp);
3904 p2p_buf_add_status(resp, status);
3905 if (noa) {
3906 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3907 wpabuf_put_le16(resp, noa_len);
3908 wpabuf_put_data(resp, noa, noa_len);
3909 } else
3910 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3911 p2p_buf_update_ie_hdr(resp, len);
3912
3913 return resp;
3914}
3915
3916
3917static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3918 const u8 *sa, const u8 *data, size_t len,
3919 int rx_freq)
3920{
3921 struct p2p_message msg;
3922 u8 status;
3923 struct wpabuf *resp;
3924 size_t g;
3925 struct p2p_group *group = NULL;
3926 int parsed = 0;
3927 u8 noa[50];
3928 int noa_len;
3929
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003930 p2p_dbg(p2p, "Received P2P Action - P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003931
3932 for (g = 0; g < p2p->num_groups; g++) {
3933 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3934 ETH_ALEN) == 0) {
3935 group = p2p->groups[g];
3936 break;
3937 }
3938 }
3939 if (group == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003940 p2p_dbg(p2p, "Ignore P2P Presence Request for unknown group "
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003941 MACSTR, MAC2STR(da));
3942 return;
3943 }
3944
3945 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003946 p2p_dbg(p2p, "Failed to parse P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003947 status = P2P_SC_FAIL_INVALID_PARAMS;
3948 goto fail;
3949 }
3950 parsed = 1;
3951
3952 if (msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003953 p2p_dbg(p2p, "No NoA attribute in P2P Presence Request");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003954 status = P2P_SC_FAIL_INVALID_PARAMS;
3955 goto fail;
3956 }
3957
3958 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3959
3960fail:
3961 if (p2p->cfg->get_noa)
3962 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3963 sizeof(noa));
3964 else
3965 noa_len = -1;
3966 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3967 noa_len > 0 ? noa_len : 0,
3968 msg.dialog_token);
3969 if (parsed)
3970 p2p_parse_free(&msg);
3971 if (resp == NULL)
3972 return;
3973
3974 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3975 if (p2p_send_action(p2p, rx_freq, sa, da, da,
3976 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003977 p2p_dbg(p2p, "Failed to send Action frame");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003978 }
3979 wpabuf_free(resp);
3980}
3981
3982
3983static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3984 const u8 *sa, const u8 *data, size_t len)
3985{
3986 struct p2p_message msg;
3987
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003988 p2p_dbg(p2p, "Received P2P Action - P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003989
3990 if (p2p_parse(data, len, &msg) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003991 p2p_dbg(p2p, "Failed to parse P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003992 return;
3993 }
3994
3995 if (msg.status == NULL || msg.noa == NULL) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07003996 p2p_dbg(p2p, "No Status or NoA attribute in P2P Presence Response");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07003997 p2p_parse_free(&msg);
3998 return;
3999 }
4000
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004001 if (p2p->cfg->presence_resp) {
4002 p2p->cfg->presence_resp(p2p->cfg->cb_ctx, sa, *msg.status,
4003 msg.noa, msg.noa_len);
4004 }
4005
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004006 if (*msg.status) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004007 p2p_dbg(p2p, "P2P Presence Request was rejected: status %u",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004008 *msg.status);
4009 p2p_parse_free(&msg);
4010 return;
4011 }
4012
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004013 p2p_dbg(p2p, "P2P Presence Request was accepted");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004014 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
4015 msg.noa, msg.noa_len);
4016 /* TODO: process NoA */
4017 p2p_parse_free(&msg);
4018}
4019
4020
4021static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4022{
4023 struct p2p_data *p2p = eloop_ctx;
4024
4025 if (p2p->ext_listen_interval) {
4026 /* Schedule next extended listen timeout */
4027 eloop_register_timeout(p2p->ext_listen_interval_sec,
4028 p2p->ext_listen_interval_usec,
4029 p2p_ext_listen_timeout, p2p, NULL);
4030 }
4031
Dmitry Shmidt6dc03bd2014-05-16 10:40:13 -07004032 if ((p2p->cfg->is_p2p_in_progress &&
4033 p2p->cfg->is_p2p_in_progress(p2p->cfg->cb_ctx)) ||
4034 (p2p->pending_action_state == P2P_PENDING_PD &&
4035 p2p->pd_retries > 0)) {
Dmitry Shmidt5a1480c2014-05-12 09:46:02 -07004036 p2p_dbg(p2p, "Operation in progress - skip Extended Listen timeout (%s)",
4037 p2p_state_txt(p2p->state));
4038 return;
4039 }
4040
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004041 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
4042 /*
4043 * This should not really happen, but it looks like the Listen
4044 * command may fail is something else (e.g., a scan) was
4045 * running at an inconvenient time. As a workaround, allow new
4046 * Extended Listen operation to be started.
4047 */
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004048 p2p_dbg(p2p, "Previous Extended Listen operation had not been completed - try again");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004049 p2p->ext_listen_only = 0;
4050 p2p_set_state(p2p, P2P_IDLE);
4051 }
4052
4053 if (p2p->state != P2P_IDLE) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004054 p2p_dbg(p2p, "Skip Extended Listen timeout in active state (%s)", p2p_state_txt(p2p->state));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004055 return;
4056 }
4057
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004058 p2p_dbg(p2p, "Extended Listen timeout");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004059 p2p->ext_listen_only = 1;
4060 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004061 p2p_dbg(p2p, "Failed to start Listen state for Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004062 p2p->ext_listen_only = 0;
4063 }
4064}
4065
4066
4067int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
4068 unsigned int interval)
4069{
4070 if (period > 65535 || interval > 65535 || period > interval ||
4071 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004072 p2p_dbg(p2p, "Invalid Extended Listen Timing request: period=%u interval=%u",
4073 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004074 return -1;
4075 }
4076
4077 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
4078
4079 if (interval == 0) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004080 p2p_dbg(p2p, "Disabling Extended Listen Timing");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004081 p2p->ext_listen_period = 0;
4082 p2p->ext_listen_interval = 0;
4083 return 0;
4084 }
4085
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004086 p2p_dbg(p2p, "Enabling Extended Listen Timing: period %u msec, interval %u msec",
4087 period, interval);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004088 p2p->ext_listen_period = period;
4089 p2p->ext_listen_interval = interval;
4090 p2p->ext_listen_interval_sec = interval / 1000;
4091 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
4092
4093 eloop_register_timeout(p2p->ext_listen_interval_sec,
4094 p2p->ext_listen_interval_usec,
4095 p2p_ext_listen_timeout, p2p, NULL);
4096
4097 return 0;
4098}
4099
4100
4101void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4102 const u8 *ie, size_t ie_len)
4103{
4104 struct p2p_message msg;
4105
4106 if (bssid == NULL || ie == NULL)
4107 return;
4108
4109 os_memset(&msg, 0, sizeof(msg));
4110 if (p2p_parse_ies(ie, ie_len, &msg))
4111 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004112 if (msg.minor_reason_code == NULL) {
4113 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004114 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004115 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004116
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004117 p2p_dbg(p2p, "Deauthentication notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004118 " reason_code=%u minor_reason_code=%u",
4119 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4120
4121 p2p_parse_free(&msg);
4122}
4123
4124
4125void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
4126 const u8 *ie, size_t ie_len)
4127{
4128 struct p2p_message msg;
4129
4130 if (bssid == NULL || ie == NULL)
4131 return;
4132
4133 os_memset(&msg, 0, sizeof(msg));
4134 if (p2p_parse_ies(ie, ie_len, &msg))
4135 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004136 if (msg.minor_reason_code == NULL) {
4137 p2p_parse_free(&msg);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004138 return;
Dmitry Shmidt97672262014-02-03 13:02:54 -08004139 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004140
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004141 p2p_dbg(p2p, "Disassociation notification BSSID " MACSTR
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004142 " reason_code=%u minor_reason_code=%u",
4143 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
4144
4145 p2p_parse_free(&msg);
4146}
4147
4148
4149void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
4150{
4151 if (enabled) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004152 p2p_dbg(p2p, "Managed P2P Device operations enabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004153 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
4154 } else {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004155 p2p_dbg(p2p, "Managed P2P Device operations disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004156 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
4157 }
4158}
4159
4160
Dmitry Shmidt661b4f72014-09-29 14:58:27 -07004161int p2p_config_get_random_social(struct p2p_config *p2p, u8 *op_class,
4162 u8 *op_channel)
4163{
4164 return p2p_channel_random_social(&p2p->channels, op_class, op_channel);
4165}
4166
4167
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004168int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel,
4169 u8 forced)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004170{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004171 if (p2p_channel_to_freq(reg_class, channel) < 0)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004172 return -1;
4173
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004174 p2p_dbg(p2p, "Set Listen channel: reg_class %u channel %u",
4175 reg_class, channel);
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004176
4177 /*
4178 * Listen channel was set in configuration or set by control interface;
4179 * cannot override it.
4180 */
4181 if (p2p->cfg->channel_forced && forced == 0)
4182 return -1;
4183
4184 if (p2p->state == P2P_IDLE) {
4185 p2p->cfg->reg_class = reg_class;
4186 p2p->cfg->channel = channel;
4187 p2p->cfg->channel_forced = forced;
4188 } else {
4189 p2p_dbg(p2p, "Defer setting listen channel");
4190 p2p->pending_reg_class = reg_class;
4191 p2p->pending_channel = channel;
4192 p2p->pending_channel_forced = forced;
4193 }
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004194
4195 return 0;
4196}
4197
4198
Dmitry Shmidt43cb5782014-06-16 16:23:22 -07004199u8 p2p_get_listen_channel(struct p2p_data *p2p)
4200{
4201 return p2p->cfg->channel;
4202}
4203
4204
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004205int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
4206{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004207 p2p_dbg(p2p, "New SSID postfix: %s", wpa_ssid_txt(postfix, len));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004208 if (postfix == NULL) {
4209 p2p->cfg->ssid_postfix_len = 0;
4210 return 0;
4211 }
4212 if (len > sizeof(p2p->cfg->ssid_postfix))
4213 return -1;
4214 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4215 p2p->cfg->ssid_postfix_len = len;
4216 return 0;
4217}
4218
4219
Jouni Malinen75ecf522011-06-27 15:19:46 -07004220int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4221 int cfg_op_channel)
4222{
Dmitry Shmidt4b060592013-04-29 16:42:49 -07004223 if (p2p_channel_to_freq(op_reg_class, op_channel) < 0)
Jouni Malinen75ecf522011-06-27 15:19:46 -07004224 return -1;
4225
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004226 p2p_dbg(p2p, "Set Operating channel: reg_class %u channel %u",
4227 op_reg_class, op_channel);
Jouni Malinen75ecf522011-06-27 15:19:46 -07004228 p2p->cfg->op_reg_class = op_reg_class;
4229 p2p->cfg->op_channel = op_channel;
4230 p2p->cfg->cfg_op_channel = cfg_op_channel;
4231 return 0;
4232}
4233
4234
Dmitry Shmidt04949592012-07-19 12:16:46 -07004235int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4236 const struct p2p_channel *pref_chan)
4237{
4238 struct p2p_channel *n;
4239
4240 if (pref_chan) {
4241 n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
4242 if (n == NULL)
4243 return -1;
4244 os_memcpy(n, pref_chan,
4245 num_pref_chan * sizeof(struct p2p_channel));
4246 } else
4247 n = NULL;
4248
4249 os_free(p2p->cfg->pref_chan);
4250 p2p->cfg->pref_chan = n;
4251 p2p->cfg->num_pref_chan = num_pref_chan;
4252
4253 return 0;
4254}
4255
4256
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004257int p2p_set_no_go_freq(struct p2p_data *p2p,
4258 const struct wpa_freq_range_list *list)
4259{
4260 struct wpa_freq_range *tmp;
4261
4262 if (list == NULL || list->num == 0) {
4263 os_free(p2p->no_go_freq.range);
4264 p2p->no_go_freq.range = NULL;
4265 p2p->no_go_freq.num = 0;
4266 return 0;
4267 }
4268
4269 tmp = os_calloc(list->num, sizeof(struct wpa_freq_range));
4270 if (tmp == NULL)
4271 return -1;
4272 os_memcpy(tmp, list->range, list->num * sizeof(struct wpa_freq_range));
4273 os_free(p2p->no_go_freq.range);
4274 p2p->no_go_freq.range = tmp;
4275 p2p->no_go_freq.num = list->num;
4276 p2p_dbg(p2p, "Updated no GO chan list");
4277
4278 return 0;
4279}
4280
4281
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004282int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4283 u8 *iface_addr)
4284{
4285 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4286 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4287 return -1;
4288 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4289 return 0;
4290}
4291
4292
4293int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4294 u8 *dev_addr)
4295{
4296 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4297 if (dev == NULL)
4298 return -1;
4299 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4300 return 0;
4301}
4302
4303
4304void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4305{
4306 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4307 if (is_zero_ether_addr(p2p->peer_filter))
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004308 p2p_dbg(p2p, "Disable peer filter");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004309 else
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004310 p2p_dbg(p2p, "Enable peer filter for " MACSTR,
4311 MAC2STR(p2p->peer_filter));
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004312}
4313
4314
4315void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4316{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004317 p2p_dbg(p2p, "Cross connection %s", enabled ? "enabled" : "disabled");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004318 if (p2p->cross_connect == enabled)
4319 return;
4320 p2p->cross_connect = enabled;
4321 /* TODO: may need to tear down any action group where we are GO(?) */
4322}
4323
4324
4325int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4326{
4327 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4328 if (dev == NULL)
4329 return -1;
4330 if (dev->oper_freq <= 0)
4331 return -1;
4332 return dev->oper_freq;
4333}
4334
4335
4336void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4337{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004338 p2p_dbg(p2p, "Intra BSS distribution %s",
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004339 enabled ? "enabled" : "disabled");
4340 p2p->cfg->p2p_intra_bss = enabled;
4341}
4342
4343
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004344void p2p_update_channel_list(struct p2p_data *p2p,
4345 const struct p2p_channels *chan,
4346 const struct p2p_channels *cli_chan)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004347{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004348 p2p_dbg(p2p, "Update channel list");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004349 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
Dmitry Shmidt68d0e3e2013-10-28 17:59:21 -07004350 p2p_channels_dump(p2p, "channels", &p2p->cfg->channels);
4351 os_memcpy(&p2p->cfg->cli_channels, cli_chan,
4352 sizeof(struct p2p_channels));
4353 p2p_channels_dump(p2p, "cli_channels", &p2p->cfg->cli_channels);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004354}
4355
4356
4357int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4358 const u8 *src, const u8 *bssid, const u8 *buf,
4359 size_t len, unsigned int wait_time)
4360{
4361 if (p2p->p2p_scan_running) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004362 p2p_dbg(p2p, "Delay Action frame TX until p2p_scan completes");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004363 if (p2p->after_scan_tx) {
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004364 p2p_dbg(p2p, "Dropped previous pending Action frame TX");
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004365 os_free(p2p->after_scan_tx);
4366 }
4367 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
4368 len);
4369 if (p2p->after_scan_tx == NULL)
4370 return -1;
4371 p2p->after_scan_tx->freq = freq;
4372 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
4373 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
4374 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
4375 p2p->after_scan_tx->len = len;
4376 p2p->after_scan_tx->wait_time = wait_time;
4377 os_memcpy(p2p->after_scan_tx + 1, buf, len);
4378 return 0;
4379 }
4380
4381 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4382 buf, len, wait_time);
4383}
4384
4385
4386void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4387 int freq_overall)
4388{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004389 p2p_dbg(p2p, "Best channel: 2.4 GHz: %d, 5 GHz: %d, overall: %d",
4390 freq_24, freq_5, freq_overall);
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004391 p2p->best_freq_24 = freq_24;
4392 p2p->best_freq_5 = freq_5;
4393 p2p->best_freq_overall = freq_overall;
4394}
4395
4396
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004397void p2p_set_own_freq_preference(struct p2p_data *p2p, int freq)
4398{
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004399 p2p_dbg(p2p, "Own frequency preference: %d MHz", freq);
Dmitry Shmidt700a1372013-03-15 14:14:44 -07004400 p2p->own_freq_preference = freq;
4401}
4402
4403
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004404const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4405{
4406 if (p2p == NULL || p2p->go_neg_peer == NULL)
4407 return NULL;
4408 return p2p->go_neg_peer->info.p2p_device_addr;
4409}
4410
4411
4412const struct p2p_peer_info *
4413p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4414{
4415 struct p2p_device *dev;
4416
4417 if (addr) {
4418 dev = p2p_get_device(p2p, addr);
4419 if (!dev)
4420 return NULL;
4421
4422 if (!next) {
4423 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
4424 return NULL;
4425
4426 return &dev->info;
4427 } else {
4428 do {
4429 dev = dl_list_first(&dev->list,
4430 struct p2p_device,
4431 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004432 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004433 return NULL;
4434 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
4435 }
4436 } else {
4437 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4438 if (!dev)
4439 return NULL;
4440 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
4441 dev = dl_list_first(&dev->list,
4442 struct p2p_device,
4443 list);
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004444 if (!dev || &dev->list == &p2p->devices)
Dmitry Shmidt8d520ff2011-05-09 14:06:53 -07004445 return NULL;
4446 }
4447 }
4448
4449 return &dev->info;
4450}
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004451
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004452
4453int p2p_in_progress(struct p2p_data *p2p)
4454{
4455 if (p2p == NULL)
4456 return 0;
Dmitry Shmidtfb79edc2014-01-10 10:45:54 -08004457 if (p2p->state == P2P_SEARCH)
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004458 return 2;
Dmitry Shmidt1f69aa52012-01-24 16:10:04 -08004459 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
4460}
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004461
4462
4463void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
4464 u8 client_timeout)
4465{
4466 if (p2p) {
4467 p2p->go_timeout = go_timeout;
4468 p2p->client_timeout = client_timeout;
4469 }
4470}
4471
4472
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004473#ifdef CONFIG_WIFI_DISPLAY
4474
4475static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
4476{
4477 size_t g;
4478 struct p2p_group *group;
4479
4480 for (g = 0; g < p2p->num_groups; g++) {
4481 group = p2p->groups[g];
Dmitry Shmidtb96dad42013-11-05 10:07:29 -08004482 p2p_group_force_beacon_update_ies(group);
Dmitry Shmidt61d9df32012-08-29 16:22:06 -07004483 }
4484}
4485
4486
4487int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
4488{
4489 wpabuf_free(p2p->wfd_ie_beacon);
4490 p2p->wfd_ie_beacon = ie;
4491 p2p_update_wfd_ie_groups(p2p);
4492 return 0;
4493}
4494
4495
4496int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
4497{
4498 wpabuf_free(p2p->wfd_ie_probe_req);
4499 p2p->wfd_ie_probe_req = ie;
4500 return 0;
4501}
4502
4503
4504int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
4505{
4506 wpabuf_free(p2p->wfd_ie_probe_resp);
4507 p2p->wfd_ie_probe_resp = ie;
4508 p2p_update_wfd_ie_groups(p2p);
4509 return 0;
4510}
4511
4512
4513int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
4514{
4515 wpabuf_free(p2p->wfd_ie_assoc_req);
4516 p2p->wfd_ie_assoc_req = ie;
4517 return 0;
4518}
4519
4520
4521int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
4522{
4523 wpabuf_free(p2p->wfd_ie_invitation);
4524 p2p->wfd_ie_invitation = ie;
4525 return 0;
4526}
4527
4528
4529int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
4530{
4531 wpabuf_free(p2p->wfd_ie_prov_disc_req);
4532 p2p->wfd_ie_prov_disc_req = ie;
4533 return 0;
4534}
4535
4536
4537int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
4538{
4539 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
4540 p2p->wfd_ie_prov_disc_resp = ie;
4541 return 0;
4542}
4543
4544
4545int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
4546{
4547 wpabuf_free(p2p->wfd_ie_go_neg);
4548 p2p->wfd_ie_go_neg = ie;
4549 return 0;
4550}
4551
4552
4553int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
4554{
4555 wpabuf_free(p2p->wfd_dev_info);
4556 if (elem) {
4557 p2p->wfd_dev_info = wpabuf_dup(elem);
4558 if (p2p->wfd_dev_info == NULL)
4559 return -1;
4560 } else
4561 p2p->wfd_dev_info = NULL;
4562
4563 return 0;
4564}
4565
4566
4567int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
4568{
4569 wpabuf_free(p2p->wfd_assoc_bssid);
4570 if (elem) {
4571 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
4572 if (p2p->wfd_assoc_bssid == NULL)
4573 return -1;
4574 } else
4575 p2p->wfd_assoc_bssid = NULL;
4576
4577 return 0;
4578}
4579
4580
4581int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
4582 const struct wpabuf *elem)
4583{
4584 wpabuf_free(p2p->wfd_coupled_sink_info);
4585 if (elem) {
4586 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
4587 if (p2p->wfd_coupled_sink_info == NULL)
4588 return -1;
4589 } else
4590 p2p->wfd_coupled_sink_info = NULL;
4591
4592 return 0;
4593}
4594
4595#endif /* CONFIG_WIFI_DISPLAY */
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004596
4597
4598int p2p_set_disc_int(struct p2p_data *p2p, int min_disc_int, int max_disc_int,
4599 int max_disc_tu)
4600{
4601 if (min_disc_int > max_disc_int || min_disc_int < 0 || max_disc_int < 0)
4602 return -1;
4603
4604 p2p->min_disc_int = min_disc_int;
4605 p2p->max_disc_int = max_disc_int;
4606 p2p->max_disc_tu = max_disc_tu;
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004607 p2p_dbg(p2p, "Set discoverable interval: min=%d max=%d max_tu=%d",
4608 min_disc_int, max_disc_int, max_disc_tu);
Dmitry Shmidtd5e49232012-12-03 15:08:10 -08004609
4610 return 0;
4611}
Dmitry Shmidtb6e9aaf2013-05-20 14:49:44 -07004612
4613
4614void p2p_dbg(struct p2p_data *p2p, const char *fmt, ...)
4615{
4616 va_list ap;
4617 char buf[500];
4618
4619 if (!p2p->cfg->debug_print)
4620 return;
4621
4622 va_start(ap, fmt);
4623 vsnprintf(buf, sizeof(buf), fmt, ap);
4624 buf[sizeof(buf) - 1] = '\0';
4625 va_end(ap);
4626 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_DEBUG, buf);
4627}
4628
4629
4630void p2p_info(struct p2p_data *p2p, const char *fmt, ...)
4631{
4632 va_list ap;
4633 char buf[500];
4634
4635 if (!p2p->cfg->debug_print)
4636 return;
4637
4638 va_start(ap, fmt);
4639 vsnprintf(buf, sizeof(buf), fmt, ap);
4640 buf[sizeof(buf) - 1] = '\0';
4641 va_end(ap);
4642 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_INFO, buf);
4643}
4644
4645
4646void p2p_err(struct p2p_data *p2p, const char *fmt, ...)
4647{
4648 va_list ap;
4649 char buf[500];
4650
4651 if (!p2p->cfg->debug_print)
4652 return;
4653
4654 va_start(ap, fmt);
4655 vsnprintf(buf, sizeof(buf), fmt, ap);
4656 buf[sizeof(buf) - 1] = '\0';
4657 va_end(ap);
4658 p2p->cfg->debug_print(p2p->cfg->cb_ctx, MSG_ERROR, buf);
4659}
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004660
4661
Dmitry Shmidtd30ac602014-06-30 09:54:22 -07004662void p2p_loop_on_known_peers(struct p2p_data *p2p,
4663 void (*peer_callback)(struct p2p_peer_info *peer,
4664 void *user_data),
4665 void *user_data)
4666{
4667 struct p2p_device *dev, *n;
4668
4669 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
4670 peer_callback(&dev->info, user_data);
4671 }
4672}
4673
4674
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004675#ifdef CONFIG_WPS_NFC
4676
4677static struct wpabuf * p2p_build_nfc_handover(struct p2p_data *p2p,
4678 int client_freq,
4679 const u8 *go_dev_addr,
4680 const u8 *ssid, size_t ssid_len)
4681{
4682 struct wpabuf *buf;
4683 u8 op_class, channel;
4684 enum p2p_role_indication role = P2P_DEVICE_NOT_IN_GROUP;
4685
4686 buf = wpabuf_alloc(1000);
4687 if (buf == NULL)
4688 return NULL;
4689
4690 op_class = p2p->cfg->reg_class;
4691 channel = p2p->cfg->channel;
4692
4693 p2p_buf_add_capability(buf, p2p->dev_capab &
4694 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
4695 p2p_buf_add_device_info(buf, p2p, NULL);
4696
4697 if (p2p->num_groups > 0) {
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004698 int freq = p2p_group_get_freq(p2p->groups[0]);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004699 role = P2P_GO_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004700 if (p2p_freq_to_channel(freq, &op_class, &channel) < 0) {
4701 p2p_dbg(p2p,
4702 "Unknown GO operating frequency %d MHz for NFC handover",
4703 freq);
4704 wpabuf_free(buf);
4705 return NULL;
4706 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004707 } else if (client_freq > 0) {
4708 role = P2P_CLIENT_IN_A_GROUP;
Dmitry Shmidt7832adb2014-04-29 10:53:02 -07004709 if (p2p_freq_to_channel(client_freq, &op_class, &channel) < 0) {
4710 p2p_dbg(p2p,
4711 "Unknown client operating frequency %d MHz for NFC handover",
4712 client_freq);
4713 wpabuf_free(buf);
4714 return NULL;
4715 }
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004716 }
4717
4718 p2p_buf_add_oob_go_neg_channel(buf, p2p->cfg->country, op_class,
4719 channel, role);
4720
4721 if (p2p->num_groups > 0) {
4722 /* Limit number of clients to avoid very long message */
4723 p2p_buf_add_group_info(p2p->groups[0], buf, 5);
4724 p2p_group_buf_add_id(p2p->groups[0], buf);
4725 } else if (client_freq > 0 &&
4726 go_dev_addr && !is_zero_ether_addr(go_dev_addr) &&
4727 ssid && ssid_len > 0) {
4728 /*
4729 * Add the optional P2P Group ID to indicate in which group this
4730 * device is a P2P Client.
4731 */
4732 p2p_buf_add_group_id(buf, go_dev_addr, ssid, ssid_len);
4733 }
4734
4735 return buf;
4736}
4737
4738
4739struct wpabuf * p2p_build_nfc_handover_req(struct p2p_data *p2p,
4740 int client_freq,
4741 const u8 *go_dev_addr,
4742 const u8 *ssid, size_t ssid_len)
4743{
4744 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
4745 ssid_len);
4746}
4747
4748
4749struct wpabuf * p2p_build_nfc_handover_sel(struct p2p_data *p2p,
4750 int client_freq,
4751 const u8 *go_dev_addr,
4752 const u8 *ssid, size_t ssid_len)
4753{
4754 return p2p_build_nfc_handover(p2p, client_freq, go_dev_addr, ssid,
4755 ssid_len);
4756}
4757
4758
4759int p2p_process_nfc_connection_handover(struct p2p_data *p2p,
4760 struct p2p_nfc_params *params)
4761{
4762 struct p2p_message msg;
4763 struct p2p_device *dev;
4764 const u8 *p2p_dev_addr;
4765 int freq;
4766 enum p2p_role_indication role;
4767
4768 params->next_step = NO_ACTION;
4769
4770 if (p2p_parse_ies_separate(params->wsc_attr, params->wsc_len,
4771 params->p2p_attr, params->p2p_len, &msg)) {
4772 p2p_dbg(p2p, "Failed to parse WSC/P2P attributes from NFC");
4773 p2p_parse_free(&msg);
4774 return -1;
4775 }
4776
4777 if (msg.p2p_device_addr)
4778 p2p_dev_addr = msg.p2p_device_addr;
4779 else if (msg.device_id)
4780 p2p_dev_addr = msg.device_id;
4781 else {
4782 p2p_dbg(p2p, "Ignore scan data without P2P Device Info or P2P Device Id");
4783 p2p_parse_free(&msg);
4784 return -1;
4785 }
4786
4787 if (msg.oob_dev_password) {
4788 os_memcpy(params->oob_dev_pw, msg.oob_dev_password,
4789 msg.oob_dev_password_len);
4790 params->oob_dev_pw_len = msg.oob_dev_password_len;
4791 }
4792
4793 dev = p2p_create_device(p2p, p2p_dev_addr);
4794 if (dev == NULL) {
4795 p2p_parse_free(&msg);
4796 return -1;
4797 }
4798
4799 params->peer = &dev->info;
4800
4801 os_get_reltime(&dev->last_seen);
4802 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
4803 p2p_copy_wps_info(p2p, dev, 0, &msg);
4804
4805 if (!msg.oob_go_neg_channel) {
4806 p2p_dbg(p2p, "OOB GO Negotiation Channel attribute not included");
4807 return -1;
4808 }
4809
4810 if (msg.oob_go_neg_channel[3] == 0 &&
4811 msg.oob_go_neg_channel[4] == 0)
4812 freq = 0;
4813 else
4814 freq = p2p_channel_to_freq(msg.oob_go_neg_channel[3],
4815 msg.oob_go_neg_channel[4]);
4816 if (freq < 0) {
4817 p2p_dbg(p2p, "Unknown peer OOB GO Neg channel");
4818 return -1;
4819 }
4820 role = msg.oob_go_neg_channel[5];
4821
4822 if (role == P2P_GO_IN_A_GROUP) {
4823 p2p_dbg(p2p, "Peer OOB GO operating channel: %u MHz", freq);
4824 params->go_freq = freq;
4825 } else if (role == P2P_CLIENT_IN_A_GROUP) {
4826 p2p_dbg(p2p, "Peer (client) OOB GO operating channel: %u MHz",
4827 freq);
4828 params->go_freq = freq;
4829 } else
4830 p2p_dbg(p2p, "Peer OOB GO Neg channel: %u MHz", freq);
4831 dev->oob_go_neg_freq = freq;
4832
4833 if (!params->sel && role != P2P_GO_IN_A_GROUP) {
4834 freq = p2p_channel_to_freq(p2p->cfg->reg_class,
4835 p2p->cfg->channel);
4836 if (freq < 0) {
4837 p2p_dbg(p2p, "Own listen channel not known");
4838 return -1;
4839 }
4840 p2p_dbg(p2p, "Use own Listen channel as OOB GO Neg channel: %u MHz", freq);
4841 dev->oob_go_neg_freq = freq;
4842 }
4843
4844 if (msg.group_id) {
4845 os_memcpy(params->go_dev_addr, msg.group_id, ETH_ALEN);
4846 params->go_ssid_len = msg.group_id_len - ETH_ALEN;
4847 os_memcpy(params->go_ssid, msg.group_id + ETH_ALEN,
4848 params->go_ssid_len);
4849 }
4850
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004851 if (dev->flags & P2P_DEV_USER_REJECTED) {
4852 p2p_dbg(p2p, "Do not report rejected device");
Dmitry Shmidt71757432014-06-02 13:50:35 -07004853 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004854 return 0;
4855 }
4856
4857 if (!(dev->flags & P2P_DEV_REPORTED)) {
4858 p2p->cfg->dev_found(p2p->cfg->cb_ctx, p2p_dev_addr, &dev->info,
4859 !(dev->flags & P2P_DEV_REPORTED_ONCE));
4860 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
4861 }
Dmitry Shmidt71757432014-06-02 13:50:35 -07004862 p2p_parse_free(&msg);
Dmitry Shmidtcf32e602014-01-28 10:57:39 -08004863
4864 if (role == P2P_GO_IN_A_GROUP && p2p->num_groups > 0)
4865 params->next_step = BOTH_GO;
4866 else if (role == P2P_GO_IN_A_GROUP)
4867 params->next_step = JOIN_GROUP;
4868 else if (role == P2P_CLIENT_IN_A_GROUP) {
4869 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
4870 params->next_step = PEER_CLIENT;
4871 } else if (p2p->num_groups > 0)
4872 params->next_step = AUTH_JOIN;
4873 else if (params->sel)
4874 params->next_step = INIT_GO_NEG;
4875 else
4876 params->next_step = RESP_GO_NEG;
4877
4878 return 0;
4879}
4880
4881
4882void p2p_set_authorized_oob_dev_pw_id(struct p2p_data *p2p, u16 dev_pw_id,
4883 int go_intent,
4884 const u8 *own_interface_addr)
4885{
4886
4887 p2p->authorized_oob_dev_pw_id = dev_pw_id;
4888 if (dev_pw_id == 0) {
4889 p2p_dbg(p2p, "NFC OOB Password unauthorized for static handover");
4890 return;
4891 }
4892
4893 p2p_dbg(p2p, "NFC OOB Password (id=%u) authorized for static handover",
4894 dev_pw_id);
4895
4896 p2p->go_intent = go_intent;
4897 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
4898}
4899
4900#endif /* CONFIG_WPS_NFC */
Dmitry Shmidt2271d3f2014-06-23 12:16:31 -07004901
4902
4903int p2p_set_passphrase_len(struct p2p_data *p2p, unsigned int len)
4904{
4905 if (len < 8 || len > 63)
4906 return -1;
4907 p2p->cfg->passphrase_len = len;
4908 return 0;
4909}
Dmitry Shmidt2e67f062014-07-16 09:55:28 -07004910
4911
4912void p2p_set_vendor_elems(struct p2p_data *p2p, struct wpabuf **vendor_elem)
4913{
4914 p2p->vendor_elem = vendor_elem;
4915}
Dmitry Shmidt6c0da2b2015-01-05 13:08:17 -08004916
4917
4918void p2p_go_neg_wait_timeout(void *eloop_ctx, void *timeout_ctx)
4919{
4920 struct p2p_data *p2p = eloop_ctx;
4921
4922 p2p_dbg(p2p,
4923 "Timeout on waiting peer to become ready for GO Negotiation");
4924 p2p_go_neg_failed(p2p, -1);
4925}