]> git.ipfire.org Git - thirdparty/hostap.git/blame - src/p2p/p2p.c
P2P: Do not stop Listen state if it is on correct channel
[thirdparty/hostap.git] / src / p2p / p2p.c
CommitLineData
b22128ef
JM
1/*
2 * Wi-Fi Direct - P2P module
3 * Copyright (c) 2009-2010, Atheros Communications
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * Alternatively, this software may be distributed under the terms of BSD
10 * license.
11 *
12 * See README and COPYING for more details.
13 */
14
15#include "includes.h"
16
17#include "common.h"
18#include "eloop.h"
19#include "common/ieee802_11_defs.h"
20#include "common/ieee802_11_common.h"
21#include "wps/wps_i.h"
22#include "p2p_i.h"
23#include "p2p.h"
24
25
26static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
27static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
28static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
29 const u8 *sa, const u8 *data, size_t len,
30 int rx_freq);
31static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
32 const u8 *sa, const u8 *data,
33 size_t len);
34static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
40c03fd4 35static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
b22128ef
JM
36
37
40c03fd4
JM
38/*
39 * p2p_scan recovery timeout
40 *
41 * Many drivers are using 30 second timeout on scan results. Allow a bit larger
42 * timeout for this to avoid hitting P2P timeout unnecessarily.
43 */
44#define P2P_SCAN_TIMEOUT 35
45
b22128ef
JM
46/**
47 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
48 * entries will be removed
49 */
50#define P2P_PEER_EXPIRATION_AGE 300
51
52#define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
53
54static void p2p_expire_peers(struct p2p_data *p2p)
55{
56 struct p2p_device *dev, *n;
57 struct os_time now;
58
59 os_get_time(&now);
60 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
61 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
62 continue;
63 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer "
64 "entry " MACSTR, MAC2STR(dev->p2p_device_addr));
65 dl_list_del(&dev->list);
66 p2p_device_free(p2p, dev);
67 }
68}
69
70
71static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
72{
73 struct p2p_data *p2p = eloop_ctx;
74 p2p_expire_peers(p2p);
75 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
76 p2p_expiration_timeout, p2p, NULL);
77}
78
79
80static const char * p2p_state_txt(int state)
81{
82 switch (state) {
83 case P2P_IDLE:
84 return "IDLE";
85 case P2P_SEARCH:
86 return "SEARCH";
87 case P2P_CONNECT:
88 return "CONNECT";
89 case P2P_CONNECT_LISTEN:
90 return "CONNECT_LISTEN";
91 case P2P_GO_NEG:
92 return "GO_NEG";
93 case P2P_LISTEN_ONLY:
94 return "LISTEN_ONLY";
95 case P2P_WAIT_PEER_CONNECT:
96 return "WAIT_PEER_CONNECT";
97 case P2P_WAIT_PEER_IDLE:
98 return "WAIT_PEER_IDLE";
99 case P2P_SD_DURING_FIND:
100 return "SD_DURING_FIND";
101 case P2P_PROVISIONING:
102 return "PROVISIONING";
103 case P2P_PD_DURING_FIND:
104 return "PD_DURING_FIND";
105 case P2P_INVITE:
106 return "INVITE";
107 case P2P_INVITE_LISTEN:
108 return "INVITE_LISTEN";
109 default:
110 return "?";
111 }
112}
113
114
115void p2p_set_state(struct p2p_data *p2p, int new_state)
116{
117 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s",
118 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
119 p2p->state = new_state;
120}
121
122
123void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
124{
125 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
126 "P2P: Set timeout (state=%s): %u.%06u sec",
127 p2p_state_txt(p2p->state), sec, usec);
128 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
129 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
130}
131
132
133void p2p_clear_timeout(struct p2p_data *p2p)
134{
135 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)",
136 p2p_state_txt(p2p->state));
137 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
138}
139
140
141void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
142 int status)
143{
144 struct p2p_go_neg_results res;
145 p2p_clear_timeout(p2p);
146 p2p_set_state(p2p, P2P_IDLE);
147 p2p->go_neg_peer = NULL;
148
149 os_memset(&res, 0, sizeof(res));
150 res.status = status;
151 if (peer) {
152 os_memcpy(res.peer_device_addr, peer->p2p_device_addr,
153 ETH_ALEN);
154 os_memcpy(res.peer_interface_addr, peer->intended_addr,
155 ETH_ALEN);
156 }
157 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
158}
159
160
161static void p2p_listen_in_find(struct p2p_data *p2p)
162{
163 unsigned int r, tu;
164 int freq;
165 struct wpabuf *ies;
166
167 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
168 "P2P: Starting short listen state (state=%s)",
169 p2p_state_txt(p2p->state));
170
171 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
172 p2p->cfg->channel);
173 if (freq < 0) {
174 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
175 "P2P: Unknown regulatory class/channel");
176 return;
177 }
178
179 os_get_random((u8 *) &r, sizeof(r));
180 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
181 p2p->min_disc_int) * 100;
182
183 p2p->pending_listen_freq = freq;
184 p2p->pending_listen_sec = 0;
185 p2p->pending_listen_usec = 1024 * tu;
186
187 ies = p2p_build_probe_resp_ies(p2p);
188 if (ies == NULL)
189 return;
190
191 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
192 ies) < 0) {
193 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
194 "P2P: Failed to start listen mode");
195 p2p->pending_listen_freq = 0;
196 }
197 wpabuf_free(ies);
198}
199
200
201int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
202{
203 int freq;
204 struct wpabuf *ies;
205
206 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
207 "P2P: Going to listen(only) state");
208
209 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
210 p2p->cfg->channel);
211 if (freq < 0) {
212 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
213 "P2P: Unknown regulatory class/channel");
214 return -1;
215 }
216
217 p2p->pending_listen_freq = freq;
218 p2p->pending_listen_sec = timeout / 1000;
219 p2p->pending_listen_usec = (timeout % 1000) * 1000;
220
221 if (p2p->p2p_scan_running) {
222 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
223 "P2P: p2p_scan running - delay start of listen state");
224 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
225 return 0;
226 }
227
228 ies = p2p_build_probe_resp_ies(p2p);
229 if (ies == NULL)
230 return -1;
231
232 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
233 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
234 "P2P: Failed to start listen mode");
235 p2p->pending_listen_freq = 0;
236 wpabuf_free(ies);
237 return -1;
238 }
239 wpabuf_free(ies);
240
241 p2p_set_state(p2p, P2P_LISTEN_ONLY);
242
243 return 0;
244}
245
246
247static void p2p_device_clear_reported(struct p2p_data *p2p)
248{
249 struct p2p_device *dev;
250 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
251 dev->flags &= ~P2P_DEV_REPORTED;
252}
253
254
255/**
256 * p2p_get_device - Fetch a peer entry
257 * @p2p: P2P module context from p2p_init()
258 * @addr: P2P Device Address of the peer
259 * Returns: Pointer to the device entry or %NULL if not found
260 */
261struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
262{
263 struct p2p_device *dev;
264 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
265 if (os_memcmp(dev->p2p_device_addr, addr, ETH_ALEN) == 0)
266 return dev;
267 }
268 return NULL;
269}
270
271
272/**
273 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
274 * @p2p: P2P module context from p2p_init()
275 * @addr: P2P Interface Address of the peer
276 * Returns: Pointer to the device entry or %NULL if not found
277 */
278struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
279 const u8 *addr)
280{
281 struct p2p_device *dev;
282 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
283 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
284 return dev;
285 }
286 return NULL;
287}
288
289
290/**
291 * p2p_create_device - Create a peer entry
292 * @p2p: P2P module context from p2p_init()
293 * @addr: P2P Device Address of the peer
294 * Returns: Pointer to the device entry or %NULL on failure
295 *
296 * If there is already an entry for the peer, it will be returned instead of
297 * creating a new one.
298 */
299static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
300 const u8 *addr)
301{
302 struct p2p_device *dev, *oldest = NULL;
303 size_t count = 0;
304
305 dev = p2p_get_device(p2p, addr);
306 if (dev)
307 return dev;
308
309 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
310 count++;
311 if (oldest == NULL ||
312 os_time_before(&dev->last_seen, &oldest->last_seen))
313 oldest = dev;
314 }
315 if (count + 1 > p2p->cfg->max_peers && oldest) {
316 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
317 "P2P: Remove oldest peer entry to make room for a new "
318 "peer");
319 dl_list_del(&oldest->list);
320 p2p_device_free(p2p, oldest);
321 }
322
323 dev = os_zalloc(sizeof(*dev));
324 if (dev == NULL)
325 return NULL;
326 dl_list_add(&p2p->devices, &dev->list);
327 os_memcpy(dev->p2p_device_addr, addr, ETH_ALEN);
328
329 return dev;
330}
331
332
333static void p2p_copy_client_info(struct p2p_device *dev,
334 struct p2p_client_info *cli)
335{
336 os_memcpy(dev->device_name, cli->dev_name, cli->dev_name_len);
337 dev->device_name[cli->dev_name_len] = '\0';
338 dev->dev_capab = cli->dev_capab;
339 dev->config_methods = cli->config_methods;
340 os_memcpy(dev->pri_dev_type, cli->pri_dev_type, 8);
341}
342
343
344static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
345 const u8 *go_interface_addr, int freq,
346 const u8 *gi, size_t gi_len)
347{
348 struct p2p_group_info info;
349 size_t c;
350 struct p2p_device *dev;
351
352 if (gi == NULL)
353 return 0;
354
355 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
356 return -1;
357
358 /*
359 * Clear old data for this group; if the devices are still in the
360 * group, the information will be restored in the loop following this.
361 */
362 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
363 if (os_memcpy(dev->member_in_go_iface, go_interface_addr,
364 ETH_ALEN) == 0) {
365 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
366 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
367 }
368 }
369
370 for (c = 0; c < info.num_clients; c++) {
371 struct p2p_client_info *cli = &info.client[c];
372 dev = p2p_get_device(p2p, cli->p2p_device_addr);
373 if (dev) {
374 /*
375 * Update information only if we have not received this
376 * directly from the client.
377 */
378 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
379 P2P_DEV_PROBE_REQ_ONLY))
380 p2p_copy_client_info(dev, cli);
381 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
382 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
383 }
384 } else {
385 dev = p2p_create_device(p2p, cli->p2p_device_addr);
386 if (dev == NULL)
387 continue;
388 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
389 p2p_copy_client_info(dev, cli);
390 dev->oper_freq = freq;
391 p2p->cfg->dev_found(
392 p2p->cfg->cb_ctx, dev->p2p_device_addr,
393 dev->p2p_device_addr, dev->pri_dev_type,
394 dev->device_name, dev->config_methods,
395 dev->dev_capab, 0);
396 }
397
398 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
399 ETH_ALEN);
400 os_get_time(&dev->last_seen);
401 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
402 os_memcpy(dev->member_in_go_iface, go_interface_addr,
403 ETH_ALEN);
404 }
405
406 return 0;
407}
408
409
410/**
411 * p2p_add_device - Add peer entries based on scan results
412 * @p2p: P2P module context from p2p_init()
413 * @addr: Source address of Beacon or Probe Response frame (may be either
414 * P2P Device Address or P2P Interface Address)
415 * @level: Signal level (signal strength of the received frame from the peer)
416 * @freq: Frequency on which the Beacon or Probe Response frame was received
417 * @ies: IEs from the Beacon or Probe Response frame
418 * @ies_len: Length of ies buffer in octets
419 * Returns: 0 on success, -1 on failure
420 *
421 * If the scan result is for a GO, the clients in the group will also be added
17bef1e9
AC
422 * to the peer table. This function can also be used with some other frames
423 * like Provision Discovery Request that contains P2P Capability and P2P Device
424 * Info attributes.
b22128ef 425 */
17bef1e9
AC
426int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
427 const u8 *ies, size_t ies_len)
b22128ef
JM
428{
429 struct p2p_device *dev;
430 struct p2p_message msg;
431 const u8 *p2p_dev_addr;
432
433 os_memset(&msg, 0, sizeof(msg));
434 if (p2p_parse_ies(ies, ies_len, &msg)) {
435 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
436 "P2P: Failed to parse P2P IE for a device entry");
437 p2p_parse_free(&msg);
438 return -1;
439 }
440
441 if (msg.p2p_device_addr)
442 p2p_dev_addr = msg.p2p_device_addr;
443 else if (msg.device_id)
444 p2p_dev_addr = msg.device_id;
445 else {
446 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
447 "P2P: Ignore scan data without P2P Device Info or "
448 "P2P Device Id");
449 p2p_parse_free(&msg);
450 return -1;
451 }
452
80c9582a
JM
453 if (!is_zero_ether_addr(p2p->peer_filter) &&
454 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
455 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
456 "filter for " MACSTR " due to peer filter",
457 MAC2STR(p2p_dev_addr));
458 return 0;
459 }
460
b22128ef
JM
461 dev = p2p_create_device(p2p, p2p_dev_addr);
462 if (dev == NULL) {
463 p2p_parse_free(&msg);
464 return -1;
465 }
466 os_get_time(&dev->last_seen);
467 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
468
469 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
470 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
471 if (msg.ssid &&
472 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
473 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
474 != 0)) {
475 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
476 dev->oper_ssid_len = msg.ssid[1];
477 }
478
479 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
480 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
481 int ds_freq;
482 if (*msg.ds_params == 14)
483 ds_freq = 2484;
484 else
485 ds_freq = 2407 + *msg.ds_params * 5;
486 if (freq != ds_freq) {
487 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
488 "P2P: Update Listen frequency based on DS "
489 "Parameter Set IE: %d -> %d MHz",
490 freq, ds_freq);
491 freq = ds_freq;
492 }
493 }
494
495 if (dev->listen_freq && dev->listen_freq != freq) {
496 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
497 "P2P: Update Listen frequency based on scan "
498 "results (" MACSTR " %d -> %d MHz (DS param %d)",
499 MAC2STR(dev->p2p_device_addr), dev->listen_freq, freq,
500 msg.ds_params ? *msg.ds_params : -1);
501 }
502 dev->listen_freq = freq;
f8d0131a
JM
503 if (msg.group_info)
504 dev->oper_freq = freq;
b22128ef
JM
505 dev->level = level;
506
507 if (msg.pri_dev_type)
508 os_memcpy(dev->pri_dev_type, msg.pri_dev_type,
509 sizeof(dev->pri_dev_type));
510 os_memcpy(dev->device_name, msg.device_name, sizeof(dev->device_name));
511 dev->config_methods = msg.config_methods ? msg.config_methods :
512 msg.wps_config_methods;
513 if (msg.capability) {
514 dev->dev_capab = msg.capability[0];
515 dev->group_capab = msg.capability[1];
516 }
517
518 if (msg.ext_listen_timing) {
519 dev->ext_listen_period = WPA_GET_LE16(msg.ext_listen_timing);
520 dev->ext_listen_interval =
521 WPA_GET_LE16(msg.ext_listen_timing + 2);
522 }
523
524 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq, msg.group_info,
525 msg.group_info_len);
526
527 p2p_parse_free(&msg);
528
529 if (p2p_pending_sd_req(p2p, dev))
530 dev->flags |= P2P_DEV_SD_SCHEDULE;
531
532 if (dev->flags & P2P_DEV_REPORTED)
533 return 0;
534
535 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
536 "P2P: Peer found with Listen frequency %d MHz", freq);
537 if (dev->flags & P2P_DEV_USER_REJECTED) {
538 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
539 "P2P: Do not report rejected device");
540 return 0;
541 }
542 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, dev->p2p_device_addr,
543 dev->pri_dev_type, dev->device_name,
544 dev->config_methods, dev->dev_capab,
545 dev->group_capab);
546 dev->flags |= P2P_DEV_REPORTED;
547
548 return 0;
549}
550
551
552static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
553{
554 if (p2p->go_neg_peer == dev)
555 p2p->go_neg_peer = NULL;
556 if (p2p->invite_peer == dev)
557 p2p->invite_peer = NULL;
558 if (p2p->sd_peer == dev)
559 p2p->sd_peer = NULL;
560 if (p2p->pending_client_disc_go == dev)
561 p2p->pending_client_disc_go = NULL;
562
563 os_free(dev);
564}
565
566
567static int p2p_get_next_prog_freq(struct p2p_data *p2p)
568{
569 struct p2p_channels *c;
570 struct p2p_reg_class *cla;
571 size_t cl, ch;
572 int found = 0;
573 u8 reg_class;
574 u8 channel;
575 int freq;
576
577 c = &p2p->cfg->channels;
578 for (cl = 0; cl < c->reg_classes; cl++) {
579 cla = &c->reg_class[cl];
580 if (cla->reg_class != p2p->last_prog_scan_class)
581 continue;
582 for (ch = 0; ch < cla->channels; ch++) {
583 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
584 found = 1;
585 break;
586 }
587 }
588 if (found)
589 break;
590 }
591
592 if (!found) {
593 /* Start from beginning */
594 reg_class = c->reg_class[0].reg_class;
595 channel = c->reg_class[0].channel[0];
596 } else {
597 /* Pick the next channel */
598 ch++;
599 if (ch == cla->channels) {
600 cl++;
601 if (cl == c->reg_classes)
602 cl = 0;
603 ch = 0;
604 }
605 reg_class = c->reg_class[cl].reg_class;
606 channel = c->reg_class[cl].channel[ch];
607 }
608
609 freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
610 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
611 "channel: reg_class %u channel %u -> %d MHz",
612 reg_class, channel, freq);
613 p2p->last_prog_scan_class = reg_class;
614 p2p->last_prog_scan_chan = channel;
615
616 if (freq == 2412 || freq == 2437 || freq == 2462)
617 return 0; /* No need to add social channels */
618 return freq;
619}
620
621
622static void p2p_search(struct p2p_data *p2p)
623{
624 int freq = 0;
625 enum p2p_scan_type type;
626
627 if (p2p->drv_in_listen) {
628 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
629 "in Listen state - wait for it to end before "
630 "continuing");
631 return;
632 }
633 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
634
635 if (p2p->go_neg_peer) {
636 /*
637 * Only scan the known listen frequency of the peer
638 * during GO Negotiation start.
639 */
640 freq = p2p->go_neg_peer->listen_freq;
641 if (freq <= 0)
642 freq = p2p->go_neg_peer->oper_freq;
643 type = P2P_SCAN_SPECIFIC;
644 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
645 "for freq %u (GO Neg)", freq);
646 } else if (p2p->invite_peer) {
647 /*
648 * Only scan the known listen frequency of the peer
649 * during Invite start.
650 */
651 freq = p2p->invite_peer->listen_freq;
652 if (freq <= 0)
653 freq = p2p->invite_peer->oper_freq;
654 type = P2P_SCAN_SPECIFIC;
655 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
656 "for freq %u (Invite)", freq);
657 } else if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
658 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
659 type = P2P_SCAN_SOCIAL_PLUS_ONE;
660 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
661 "(+ freq %u)", freq);
662 } else {
663 type = P2P_SCAN_SOCIAL;
664 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
665 }
666
667 if (p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq) < 0) {
668 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
669 "P2P: Scan request failed");
670 p2p_continue_find(p2p);
40c03fd4
JM
671 } else {
672 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
673 p2p->p2p_scan_running = 1;
674 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
675 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
676 p2p, NULL);
b22128ef
JM
677 }
678}
679
680
681static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
682{
683 struct p2p_data *p2p = eloop_ctx;
684 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
685 p2p_stop_find(p2p);
686}
687
688
689static int p2p_run_after_scan(struct p2p_data *p2p)
690{
691 struct p2p_device *dev;
692 enum p2p_after_scan op;
693
694 op = p2p->start_after_scan;
695 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
696 switch (op) {
697 case P2P_AFTER_SCAN_NOTHING:
698 break;
699 case P2P_AFTER_SCAN_LISTEN:
700 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
701 "requested Listen state");
702 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
703 p2p->pending_listen_usec / 1000);
704 return 1;
705 case P2P_AFTER_SCAN_CONNECT:
706 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
707 "requested connect with " MACSTR,
708 MAC2STR(p2p->after_scan_peer));
709 dev = p2p_get_device(p2p, p2p->after_scan_peer);
710 if (dev == NULL) {
711 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
712 "known anymore");
713 break;
714 }
715 p2p_connect_send(p2p, dev);
716 return 1;
717 }
718
719 return 0;
720}
721
722
b22128ef
JM
723static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
724{
725 struct p2p_data *p2p = eloop_ctx;
726 int running;
727 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
728 "(running=%d)", p2p->p2p_scan_running);
729 running = p2p->p2p_scan_running;
730 /* Make sure we recover from missed scan results callback */
731 p2p->p2p_scan_running = 0;
732
733 if (running)
734 p2p_run_after_scan(p2p);
735}
736
737
738int p2p_find(struct p2p_data *p2p, unsigned int timeout,
739 enum p2p_discovery_type type)
740{
741 int res;
742
743 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
744 type);
745 if (p2p->p2p_scan_running) {
746 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
747 "already running");
748 }
749 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
750 p2p_clear_timeout(p2p);
751 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
752 p2p->find_type = type;
753 p2p_device_clear_reported(p2p);
754 p2p_set_state(p2p, P2P_SEARCH);
755 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
756 if (timeout)
757 eloop_register_timeout(timeout, 0, p2p_find_timeout,
758 p2p, NULL);
759 switch (type) {
760 case P2P_FIND_START_WITH_FULL:
761 case P2P_FIND_PROGRESSIVE:
762 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0);
763 break;
764 case P2P_FIND_ONLY_SOCIAL:
765 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0);
766 break;
767 default:
768 return -1;
769 }
770
771 if (res == 0) {
772 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
773 p2p->p2p_scan_running = 1;
774 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
775 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
776 p2p, NULL);
777 } else {
778 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
779 "p2p_scan");
780 }
781
782 return res;
783}
784
785
0b8889d8 786void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
b22128ef
JM
787{
788 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
789 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
790 p2p_clear_timeout(p2p);
791 p2p_set_state(p2p, P2P_IDLE);
792 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
793 p2p->go_neg_peer = NULL;
794 p2p->sd_peer = NULL;
795 p2p->invite_peer = NULL;
0b8889d8
JM
796 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
797 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
798 "since we are on correct channel for response");
799 return;
800 }
b22128ef
JM
801 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
802}
803
804
0b8889d8
JM
805void p2p_stop_find(struct p2p_data *p2p)
806{
807 p2p_stop_find_for_freq(p2p, 0);
808}
809
810
7861cb08 811static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
b22128ef 812{
b22128ef 813 if (force_freq) {
1e19f734 814 u8 op_reg_class, op_channel;
b22128ef 815 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
1e19f734 816 &op_reg_class, &op_channel) < 0) {
b22128ef
JM
817 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
818 "P2P: Unsupported frequency %u MHz",
819 force_freq);
820 return -1;
821 }
1e19f734
JM
822 if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
823 op_channel)) {
824 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
825 "P2P: Frequency %u MHz (oper_class %u "
826 "channel %u) not allowed for P2P",
827 force_freq, op_reg_class, op_channel);
828 return -1;
829 }
830 p2p->op_reg_class = op_reg_class;
831 p2p->op_channel = op_channel;
b22128ef
JM
832 p2p->channels.reg_classes = 1;
833 p2p->channels.reg_class[0].channels = 1;
834 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
835 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
836 } else {
837 p2p->op_reg_class = p2p->cfg->op_reg_class;
838 p2p->op_channel = p2p->cfg->op_channel;
839 os_memcpy(&p2p->channels, &p2p->cfg->channels,
840 sizeof(struct p2p_channels));
841 }
842 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
843 "P2P: Own preference for operation channel: "
844 "Regulatory Class %u Channel %u%s",
845 p2p->op_reg_class, p2p->op_channel,
846 force_freq ? " (forced)" : "");
847
7861cb08
JM
848 return 0;
849}
850
851
852int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
853 enum p2p_wps_method wps_method,
854 int go_intent, const u8 *own_interface_addr,
855 unsigned int force_freq, int persistent_group)
856{
857 struct p2p_device *dev;
858
859 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
860 "P2P: Request to start group negotiation - peer=" MACSTR
861 " GO Intent=%d Intended Interface Address=" MACSTR
862 " wps_method=%d persistent_group=%d",
863 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
864 wps_method, persistent_group);
865
866 if (p2p_prepare_channel(p2p, force_freq) < 0)
867 return -1;
868
b22128ef
JM
869 dev = p2p_get_device(p2p, peer_addr);
870 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
871 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
872 "P2P: Cannot connect to unknown P2P Device " MACSTR,
873 MAC2STR(peer_addr));
874 return -1;
875 }
876
877 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
878 if (!(dev->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
879 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
880 "P2P: Cannot connect to P2P Device " MACSTR
881 " that is in a group and is not discoverable",
882 MAC2STR(peer_addr));
883 return -1;
884 }
885 if (dev->oper_freq <= 0) {
886 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
887 "P2P: Cannot connect to P2P Device " MACSTR
888 " with incomplete information",
889 MAC2STR(peer_addr));
890 return -1;
891 }
892
893 /*
894 * First, try to connect directly. If the peer does not
895 * acknowledge frames, assume it is sleeping and use device
896 * discoverability via the GO at that point.
897 */
898 }
899
900 dev->flags &= ~P2P_DEV_NOT_YET_READY;
901 dev->flags &= ~P2P_DEV_USER_REJECTED;
902 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
903 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
904 dev->go_neg_req_sent = 0;
905 dev->go_state = UNKNOWN_GO;
906 if (persistent_group)
907 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
908 else
909 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_GROUP;
910 p2p->go_intent = go_intent;
911 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
912
913 if (p2p->state != P2P_IDLE)
914 p2p_stop_find(p2p);
915
916 dev->wps_method = wps_method;
917 dev->status = P2P_SC_SUCCESS;
d5b20a73
JM
918
919 if (force_freq)
920 dev->flags |= P2P_DEV_FORCE_FREQ;
921 else
922 dev->flags &= ~P2P_DEV_FORCE_FREQ;
923
b22128ef
JM
924 if (p2p->p2p_scan_running) {
925 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
926 "P2P: p2p_scan running - delay connect send");
927 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
928 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
929 return 0;
930 }
931 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
932
933 return p2p_connect_send(p2p, dev);
934}
935
936
937int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
938 enum p2p_wps_method wps_method,
939 int go_intent, const u8 *own_interface_addr,
940 unsigned int force_freq, int persistent_group)
941{
942 struct p2p_device *dev;
943
944 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
945 "P2P: Request to authorize group negotiation - peer=" MACSTR
946 " GO Intent=%d Intended Interface Address=" MACSTR
947 " wps_method=%d persistent_group=%d",
948 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
949 wps_method, persistent_group);
950
7861cb08
JM
951 if (p2p_prepare_channel(p2p, force_freq) < 0)
952 return -1;
b22128ef
JM
953
954 dev = p2p_get_device(p2p, peer_addr);
955 if (dev == NULL) {
956 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
957 "P2P: Cannot authorize unknown P2P Device " MACSTR,
958 MAC2STR(peer_addr));
959 return -1;
960 }
961
962 dev->flags &= ~P2P_DEV_NOT_YET_READY;
963 dev->flags &= ~P2P_DEV_USER_REJECTED;
964 dev->go_neg_req_sent = 0;
965 dev->go_state = UNKNOWN_GO;
966 if (persistent_group)
967 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
968 else
969 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_GROUP;
970 p2p->go_intent = go_intent;
971 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
972
973 dev->wps_method = wps_method;
974 dev->status = P2P_SC_SUCCESS;
975
d5b20a73
JM
976 if (force_freq)
977 dev->flags |= P2P_DEV_FORCE_FREQ;
978 else
979 dev->flags &= ~P2P_DEV_FORCE_FREQ;
980
b22128ef
JM
981 return 0;
982}
983
984
985void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
986 struct p2p_device *dev, struct p2p_message *msg)
987{
988 os_get_time(&dev->last_seen);
989
990 if (msg->pri_dev_type)
991 os_memcpy(dev->pri_dev_type, msg->pri_dev_type,
992 sizeof(dev->pri_dev_type));
993 os_memcpy(dev->device_name, msg->device_name,
994 sizeof(dev->device_name));
995 dev->config_methods = msg->config_methods ? msg->config_methods :
996 msg->wps_config_methods;
997 if (msg->capability) {
998 dev->dev_capab = msg->capability[0];
999 dev->group_capab = msg->capability[1];
1000 }
1001 if (msg->listen_channel) {
1002 int freq;
1003 freq = p2p_channel_to_freq((char *) msg->listen_channel,
1004 msg->listen_channel[3],
1005 msg->listen_channel[4]);
1006 if (freq < 0) {
1007 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1008 "P2P: Unknown peer Listen channel: "
1009 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1010 msg->listen_channel[0],
1011 msg->listen_channel[1],
1012 msg->listen_channel[2],
1013 msg->listen_channel[3],
1014 msg->listen_channel[4]);
1015 } else {
1016 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1017 "peer " MACSTR " Listen channel: %u -> %u MHz",
1018 MAC2STR(dev->p2p_device_addr),
1019 dev->listen_freq, freq);
1020 dev->listen_freq = freq;
1021 }
1022 }
1023 if (msg->ext_listen_timing) {
1024 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
1025 dev->ext_listen_interval =
1026 WPA_GET_LE16(msg->ext_listen_timing + 2);
1027 }
1028
1029 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1030 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1031 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1032 "P2P: Completed device entry based on data from "
1033 "GO Negotiation Request");
1034 } else {
1035 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1036 "P2P: Created device entry based on GO Neg Req: "
1037 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1038 "listen_freq=%d",
1039 MAC2STR(dev->p2p_device_addr), dev->dev_capab,
1040 dev->group_capab, dev->device_name, dev->listen_freq);
1041 }
1042
1043 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1044
1045 if (dev->flags & P2P_DEV_USER_REJECTED) {
1046 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1047 "P2P: Do not report rejected device");
1048 return;
1049 }
1050
1051 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, dev->p2p_device_addr,
1052 dev->pri_dev_type, dev->device_name,
1053 dev->config_methods, dev->dev_capab,
1054 dev->group_capab);
1055}
1056
1057
1058void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1059{
1060 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1061 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1062 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1063 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1064 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1065}
1066
1067
1068int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1069{
1070 p2p_build_ssid(p2p, params->ssid, &params->ssid_len);
1071 p2p_random(params->passphrase, 8);
1072 return 0;
1073}
1074
1075
1076void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1077{
1078 struct p2p_go_neg_results res;
1079 int go = peer->go_state == LOCAL_GO;
1080 struct p2p_channels intersection;
1081 int freqs;
1082 size_t i, j;
1083
1084 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1085 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1086 "GO)", MAC2STR(peer->p2p_device_addr),
1087 go ? "local end" : "peer");
1088
1089 os_memset(&res, 0, sizeof(res));
1090 res.role_go = go;
1091 os_memcpy(res.peer_device_addr, peer->p2p_device_addr, ETH_ALEN);
1092 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1093 res.wps_method = peer->wps_method;
1094 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
1095 res.persistent_group = 1;
1096
1097 if (go) {
1098 /* Setup AP mode for WPS provisioning */
1099 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1100 p2p->op_reg_class,
1101 p2p->op_channel);
1102 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1103 res.ssid_len = p2p->ssid_len;
1104 p2p_random(res.passphrase, 8);
e9a7ae41 1105 } else {
b22128ef 1106 res.freq = peer->oper_freq;
e9a7ae41
JM
1107 if (p2p->ssid_len) {
1108 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1109 res.ssid_len = p2p->ssid_len;
1110 }
1111 }
b22128ef
JM
1112
1113 p2p_channels_intersect(&p2p->channels, &peer->channels,
1114 &intersection);
1115 freqs = 0;
1116 for (i = 0; i < intersection.reg_classes; i++) {
1117 struct p2p_reg_class *c = &intersection.reg_class[i];
1118 if (freqs + 1 == P2P_MAX_CHANNELS)
1119 break;
1120 for (j = 0; j < c->channels; j++) {
1121 int freq;
1122 if (freqs + 1 == P2P_MAX_CHANNELS)
1123 break;
1124 freq = p2p_channel_to_freq(peer->country, c->reg_class,
1125 c->channel[j]);
1126 if (freq < 0)
1127 continue;
1128 res.freq_list[freqs++] = freq;
1129 }
1130 }
1131
ae3e3421
JM
1132 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1133
b22128ef
JM
1134 p2p_clear_timeout(p2p);
1135 peer->go_neg_req_sent = 0;
1136 peer->wps_method = WPS_NOT_READY;
1137
1138 p2p_set_state(p2p, P2P_PROVISIONING);
1139 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1140}
1141
1142
1143static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1144 const u8 *data, size_t len, int rx_freq)
1145{
1146 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1147 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1148 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1149
1150 if (len < 1)
1151 return;
1152
1153 switch (data[0]) {
1154 case P2P_GO_NEG_REQ:
1155 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1156 break;
1157 case P2P_GO_NEG_RESP:
1158 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1159 break;
1160 case P2P_GO_NEG_CONF:
1161 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1162 break;
1163 case P2P_INVITATION_REQ:
1164 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1165 rx_freq);
1166 break;
1167 case P2P_INVITATION_RESP:
1168 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1169 break;
1170 case P2P_PROV_DISC_REQ:
1171 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1172 break;
1173 case P2P_PROV_DISC_RESP:
1174 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1175 break;
1176 case P2P_DEV_DISC_REQ:
1177 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1178 break;
1179 case P2P_DEV_DISC_RESP:
1180 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1181 break;
1182 default:
1183 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1184 "P2P: Unsupported P2P Public Action frame type %d",
1185 data[0]);
1186 break;
1187 }
1188}
1189
1190
1191void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1192 const u8 *bssid, const u8 *data, size_t len,
1193 int freq)
1194{
1195 if (len < 1)
1196 return;
1197
1198 switch (data[0]) {
1199 case WLAN_PA_VENDOR_SPECIFIC:
1200 data++;
1201 len--;
1202 if (len < 3)
1203 return;
1204 if (WPA_GET_BE24(data) != OUI_WFA)
1205 return;
1206
1207 data += 3;
1208 len -= 3;
1209 if (len < 1)
1210 return;
1211
1212 if (*data != P2P_OUI_TYPE)
1213 return;
1214
1215 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1216 break;
1217 case WLAN_PA_GAS_INITIAL_REQ:
1218 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1219 break;
1220 case WLAN_PA_GAS_INITIAL_RESP:
18708aad
JM
1221 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1222 break;
1223 case WLAN_PA_GAS_COMEBACK_REQ:
1224 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1225 break;
1226 case WLAN_PA_GAS_COMEBACK_RESP:
1227 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
b22128ef
JM
1228 break;
1229 }
1230}
1231
1232
1233void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1234 const u8 *bssid, u8 category,
1235 const u8 *data, size_t len, int freq)
1236{
1237 if (category == WLAN_ACTION_PUBLIC) {
1238 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1239 return;
1240 }
1241
1242 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1243 return;
1244
1245 if (len < 4)
1246 return;
1247
1248 if (WPA_GET_BE24(data) != OUI_WFA)
1249 return;
1250 data += 3;
1251 len -= 3;
1252
1253 if (*data != P2P_OUI_TYPE)
1254 return;
1255 data++;
1256 len--;
1257
1258 /* P2P action frame */
1259 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1260 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1261 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1262
1263 if (len < 1)
1264 return;
1265 switch (data[0]) {
1266 case P2P_NOA:
1267 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1268 "P2P: Received P2P Action - Notice of Absence");
1269 /* TODO */
1270 break;
1271 case P2P_PRESENCE_REQ:
1272 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1273 break;
1274 case P2P_PRESENCE_RESP:
1275 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1276 break;
1277 case P2P_GO_DISC_REQ:
1278 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1279 break;
1280 default:
1281 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1282 "P2P: Received P2P Action - unknown type %u", data[0]);
1283 break;
1284 }
1285}
1286
1287
1288static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1289{
1290 struct p2p_data *p2p = eloop_ctx;
1291 if (p2p->go_neg_peer == NULL)
1292 return;
1293 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1294 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1295 p2p_connect_send(p2p, p2p->go_neg_peer);
1296}
1297
1298
1299static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1300{
1301 struct p2p_data *p2p = eloop_ctx;
1302 if (p2p->invite_peer == NULL)
1303 return;
1304 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1305 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1306}
1307
1308
1309static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1310 const u8 *ie, size_t ie_len)
1311{
1312 struct p2p_message msg;
1313 struct p2p_device *dev;
1314
1315 os_memset(&msg, 0, sizeof(msg));
1316 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1317 {
1318 p2p_parse_free(&msg);
1319 return; /* not a P2P probe */
1320 }
1321
1322 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1323 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1324 != 0) {
1325 /* The Probe Request is not part of P2P Device Discovery. It is
1326 * not known whether the source address of the frame is the P2P
1327 * Device Address or P2P Interface Address. Do not add a new
1328 * peer entry based on this frames.
1329 */
1330 p2p_parse_free(&msg);
1331 return;
1332 }
1333
1334 dev = p2p_get_device(p2p, addr);
1335 if (dev) {
1336 if (dev->country[0] == 0 && msg.listen_channel)
1337 os_memcpy(dev->country, msg.listen_channel, 3);
1338 p2p_parse_free(&msg);
1339 return; /* already known */
1340 }
1341
1342 dev = p2p_create_device(p2p, addr);
1343 if (dev == NULL) {
1344 p2p_parse_free(&msg);
1345 return;
1346 }
1347
1348 os_get_time(&dev->last_seen);
1349 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1350
1351 if (msg.capability) {
1352 dev->dev_capab = msg.capability[0];
1353 dev->group_capab = msg.capability[1];
1354 }
1355
1356 if (msg.listen_channel) {
1357 os_memcpy(dev->country, msg.listen_channel, 3);
1358 dev->listen_freq = p2p_channel_to_freq(dev->country,
1359 msg.listen_channel[3],
1360 msg.listen_channel[4]);
1361 }
1362
1363 os_memcpy(dev->device_name, msg.device_name, sizeof(dev->device_name));
1364
1365 if (msg.wps_pri_dev_type)
1366 os_memcpy(dev->pri_dev_type, msg.wps_pri_dev_type,
1367 sizeof(dev->pri_dev_type));
1368
1369 p2p_parse_free(&msg);
1370
1371 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1372 "P2P: Created device entry based on Probe Req: " MACSTR
1373 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1374 MAC2STR(dev->p2p_device_addr), dev->dev_capab,
1375 dev->group_capab, dev->device_name, dev->listen_freq);
1376}
1377
1378
1379struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1380 const u8 *addr,
1381 struct p2p_message *msg)
1382{
1383 struct p2p_device *dev;
1384
1385 dev = p2p_get_device(p2p, addr);
1386 if (dev) {
1387 os_get_time(&dev->last_seen);
1388 return dev; /* already known */
1389 }
1390
1391 dev = p2p_create_device(p2p, addr);
1392 if (dev == NULL)
1393 return NULL;
1394
1395 p2p_add_dev_info(p2p, addr, dev, msg);
1396
1397 return dev;
1398}
1399
1400
1401static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1402{
1403 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1404 return 1;
1405 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1406 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1407 WPA_GET_BE16(&req_dev_type[6]) == 0)
1408 return 1; /* Category match with wildcard OUI/sub-category */
1409 return 0;
1410}
1411
1412
1413int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1414 size_t num_req_dev_type)
1415{
1416 size_t i;
1417 for (i = 0; i < num_req_dev_type; i++) {
1418 if (dev_type_match(dev_type, req_dev_type[i]))
1419 return 1;
1420 }
1421 return 0;
1422}
1423
1424
1425/**
1426 * p2p_match_dev_type - Match local device type with requested type
1427 * @p2p: P2P module context from p2p_init()
1428 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1429 * Returns: 1 on match, 0 on mismatch
1430 *
1431 * This function can be used to match the Requested Device Type attribute in
1432 * WPS IE with the local device types for deciding whether to reply to a Probe
1433 * Request frame.
1434 */
1435int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1436{
1437 struct wps_parse_attr attr;
1438 size_t i;
1439
1440 if (wps_parse_msg(wps, &attr))
1441 return 1; /* assume no Requested Device Type attributes */
1442
1443 if (attr.num_req_dev_type == 0)
1444 return 1; /* no Requested Device Type attributes -> match */
1445
1446 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1447 attr.num_req_dev_type))
1448 return 1; /* Own Primary Device Type matches */
1449
1450 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1451 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1452 attr.req_dev_type,
1453 attr.num_req_dev_type))
1454 return 1; /* Own Secondary Device Type matches */
1455
1456 /* No matching device type found */
1457 return 0;
1458}
1459
1460
1461struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1462{
1463 struct wpabuf *buf;
1464 u8 *len;
1465
1466 buf = wpabuf_alloc(1000);
1467 if (buf == NULL)
1468 return NULL;
1469
1470 /* TODO: add more info into WPS IE; maybe get from WPS module? */
1471 p2p_build_wps_ie(p2p, buf, DEV_PW_DEFAULT, 1);
1472
1473 /* P2P IE */
1474 len = p2p_buf_add_ie_hdr(buf);
1475 p2p_buf_add_capability(buf, p2p->dev_capab, 0);
1476 if (p2p->ext_listen_interval)
1477 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1478 p2p->ext_listen_interval);
1479 p2p_buf_add_device_info(buf, p2p, NULL);
1480 p2p_buf_update_ie_hdr(buf, len);
1481
1482 return buf;
1483}
1484
1485
1486static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *ie,
1487 size_t ie_len)
1488{
1489 struct ieee802_11_elems elems;
1490 struct wpabuf *buf;
1491 struct ieee80211_mgmt *resp;
1492 struct wpabuf *wps;
1493 struct wpabuf *ies;
1494
1495 if (!p2p->in_listen || !p2p->drv_in_listen) {
1496 /* not in Listen state - ignore Probe Request */
1497 return;
1498 }
1499
1500 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
1501 ParseFailed) {
1502 /* Ignore invalid Probe Request frames */
1503 return;
1504 }
1505
1506 if (elems.p2p == NULL) {
1507 /* not a P2P probe - ignore it */
1508 return;
1509 }
1510
1511 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
1512 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
1513 0) {
1514 /* not using P2P Wildcard SSID - ignore */
1515 return;
1516 }
1517
1518 /* Check Requested Device Type match */
1519 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1520 if (wps && !p2p_match_dev_type(p2p, wps)) {
1521 wpabuf_free(wps);
1522 /* No match with Requested Device Type */
1523 return;
1524 }
1525 wpabuf_free(wps);
1526
1527 if (!p2p->cfg->send_probe_resp)
1528 return; /* Response generated elsewhere */
1529
1530 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1531 "P2P: Reply to P2P Probe Request in Listen state");
1532
1533 /*
1534 * We do not really have a specific BSS that this frame is advertising,
1535 * so build a frame that has some information in valid format. This is
1536 * really only used for discovery purposes, not to learn exact BSS
1537 * parameters.
1538 */
1539 ies = p2p_build_probe_resp_ies(p2p);
1540 if (ies == NULL)
1541 return;
1542
1543 buf = wpabuf_alloc(200 + wpabuf_len(ies));
1544 if (buf == NULL) {
1545 wpabuf_free(ies);
1546 return;
1547 }
1548
1549 resp = NULL;
1550 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
1551
1552 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
1553 (WLAN_FC_STYPE_PROBE_RESP << 4));
1554 os_memcpy(resp->da, addr, ETH_ALEN);
1555 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
1556 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
1557 resp->u.probe_resp.beacon_int = host_to_le16(100);
1558 /* hardware or low-level driver will setup seq_ctrl and timestamp */
1559 resp->u.probe_resp.capab_info =
1560 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
1561 WLAN_CAPABILITY_PRIVACY |
1562 WLAN_CAPABILITY_SHORT_SLOT_TIME);
1563
1564 wpabuf_put_u8(buf, WLAN_EID_SSID);
1565 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
1566 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1567
1568 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
1569 wpabuf_put_u8(buf, 8);
1570 wpabuf_put_u8(buf, (60 / 5) | 0x80);
1571 wpabuf_put_u8(buf, 90 / 5);
1572 wpabuf_put_u8(buf, (120 / 5) | 0x80);
1573 wpabuf_put_u8(buf, 180 / 5);
1574 wpabuf_put_u8(buf, (240 / 5) | 0x80);
1575 wpabuf_put_u8(buf, 360 / 5);
1576 wpabuf_put_u8(buf, 480 / 5);
1577 wpabuf_put_u8(buf, 540 / 5);
1578
1579 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
1580 wpabuf_put_u8(buf, 1);
1581 wpabuf_put_u8(buf, p2p->cfg->channel);
1582
1583 wpabuf_put_buf(buf, ies);
1584 wpabuf_free(ies);
1585
1586 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
1587
1588 wpabuf_free(buf);
1589}
1590
1591
1592int p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *ie,
1593 size_t ie_len)
1594{
1595 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
1596
1597 p2p_reply_probe(p2p, addr, ie, ie_len);
1598
1599 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
1600 p2p->go_neg_peer &&
1601 os_memcmp(addr, p2p->go_neg_peer->p2p_device_addr, ETH_ALEN) == 0)
1602 {
1603 /* Received a Probe Request from GO Negotiation peer */
1604 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1605 "P2P: Found GO Negotiation peer - try to start GO "
1606 "negotiation from timeout");
1607 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
1608 return 1;
1609 }
1610
1611 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
1612 p2p->invite_peer &&
1613 os_memcmp(addr, p2p->invite_peer->p2p_device_addr, ETH_ALEN) == 0)
1614 {
1615 /* Received a Probe Request from Invite peer */
1616 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1617 "P2P: Found Invite peer - try to start Invite from "
1618 "timeout");
1619 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
1620 return 1;
1621 }
1622
1623 return 0;
1624}
1625
1626
1627static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
4c08c0bd 1628 u8 *buf, size_t len, struct wpabuf *p2p_ie)
b22128ef
JM
1629{
1630 struct wpabuf *tmp;
1631 u8 *lpos;
1632 size_t tmplen;
1633 int res;
72044390 1634 u8 group_capab;
b22128ef 1635
4c08c0bd
JM
1636 if (p2p_ie == NULL)
1637 return 0; /* WLAN AP is not a P2P manager */
b22128ef
JM
1638
1639 /*
1640 * (Re)Association Request - P2P IE
1641 * P2P Capability attribute (shall be present)
4c08c0bd
JM
1642 * P2P Interface attribute (present if concurrent device and
1643 * P2P Management is enabled)
b22128ef
JM
1644 */
1645 tmp = wpabuf_alloc(200);
1646 if (tmp == NULL)
1647 return -1;
1648
1649 lpos = p2p_buf_add_ie_hdr(tmp);
72044390
JM
1650 group_capab = 0;
1651 if (p2p->num_groups > 0) {
1652 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
1653 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1654 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
1655 p2p->cross_connect)
1656 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
1657 }
1658 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
4c08c0bd
JM
1659 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1660 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
b22128ef
JM
1661 p2p_buf_add_p2p_interface(tmp, p2p);
1662 p2p_buf_update_ie_hdr(tmp, lpos);
1663
1664 tmplen = wpabuf_len(tmp);
1665 if (tmplen > len)
1666 res = -1;
1667 else {
1668 os_memcpy(buf, wpabuf_head(tmp), tmplen);
1669 res = tmplen;
1670 }
1671 wpabuf_free(tmp);
1672
1673 return res;
1674}
1675
1676
1677int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
4c08c0bd 1678 size_t len, int p2p_group, struct wpabuf *p2p_ie)
b22128ef
JM
1679{
1680 struct wpabuf *tmp;
1681 u8 *lpos;
1682 struct p2p_device *peer;
1683 size_t tmplen;
1684 int res;
1685
1686 if (!p2p_group)
4c08c0bd 1687 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
b22128ef
JM
1688
1689 /*
1690 * (Re)Association Request - P2P IE
1691 * P2P Capability attribute (shall be present)
1692 * Extended Listen Timing (may be present)
1693 * P2P Device Info attribute (shall be present)
1694 */
1695 tmp = wpabuf_alloc(200);
1696 if (tmp == NULL)
1697 return -1;
1698
1699 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
1700
1701 lpos = p2p_buf_add_ie_hdr(tmp);
1702 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
5be5305b
JM
1703 if (p2p->ext_listen_interval)
1704 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
1705 p2p->ext_listen_interval);
b22128ef
JM
1706 p2p_buf_add_device_info(tmp, p2p, peer);
1707 p2p_buf_update_ie_hdr(tmp, lpos);
1708
1709 tmplen = wpabuf_len(tmp);
1710 if (tmplen > len)
1711 res = -1;
1712 else {
1713 os_memcpy(buf, wpabuf_head(tmp), tmplen);
1714 res = tmplen;
1715 }
1716 wpabuf_free(tmp);
1717
1718 return res;
1719}
1720
1721
1722int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
1723{
1724 struct wpabuf *p2p_ie;
1725 int ret;
1726
1727 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
1728 if (p2p_ie == NULL)
1729 return 0;
1730
1731 ret = p2p_attr_text(p2p_ie, buf, end);
1732 wpabuf_free(p2p_ie);
1733 return ret;
1734}
1735
1736
1737static void p2p_clear_go_neg(struct p2p_data *p2p)
1738{
1739 p2p->go_neg_peer = NULL;
1740 p2p_clear_timeout(p2p);
1741 p2p_set_state(p2p, P2P_IDLE);
1742}
1743
1744
1745void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
1746{
1747 if (p2p->go_neg_peer == NULL) {
1748 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1749 "P2P: No pending Group Formation - "
1750 "ignore WPS registration success notification");
1751 return; /* No pending Group Formation */
1752 }
1753
1754 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
1755 0) {
1756 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1757 "P2P: Ignore WPS registration success notification "
1758 "for " MACSTR " (GO Negotiation peer " MACSTR ")",
1759 MAC2STR(mac_addr),
1760 MAC2STR(p2p->go_neg_peer->intended_addr));
1761 return; /* Ignore unexpected peer address */
1762 }
1763
1764 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1765 "P2P: Group Formation completed successfully with " MACSTR,
1766 MAC2STR(mac_addr));
1767
1768 p2p_clear_go_neg(p2p);
1769}
1770
1771
1772void p2p_group_formation_failed(struct p2p_data *p2p)
1773{
1774 if (p2p->go_neg_peer == NULL) {
1775 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1776 "P2P: No pending Group Formation - "
1777 "ignore group formation failure notification");
1778 return; /* No pending Group Formation */
1779 }
1780
1781 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1782 "P2P: Group Formation failed with " MACSTR,
1783 MAC2STR(p2p->go_neg_peer->intended_addr));
1784
1785 p2p_clear_go_neg(p2p);
1786}
1787
1788
1789struct p2p_data * p2p_init(const struct p2p_config *cfg)
1790{
1791 struct p2p_data *p2p;
1792
1793 if (cfg->max_peers < 1)
1794 return NULL;
1795
1796 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
1797 if (p2p == NULL)
1798 return NULL;
1799 p2p->cfg = (struct p2p_config *) (p2p + 1);
1800 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
1801 if (cfg->dev_name)
1802 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
1803
1804 p2p->min_disc_int = 1;
1805 p2p->max_disc_int = 3;
1806
1807 os_get_random(&p2p->next_tie_breaker, 1);
1808 p2p->next_tie_breaker &= 0x01;
1809 if (cfg->sd_request)
1810 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
1811 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
1812 if (cfg->concurrent_operations)
1813 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
1814 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
1815
1816 dl_list_init(&p2p->devices);
1817
1818 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
1819 p2p_expiration_timeout, p2p, NULL);
1820
1821 return p2p;
1822}
1823
1824
1825void p2p_deinit(struct p2p_data *p2p)
1826{
1827 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
1828 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
1829 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1830 p2p_flush(p2p);
1831 os_free(p2p->cfg->dev_name);
1832 os_free(p2p->groups);
18708aad 1833 wpabuf_free(p2p->sd_resp);
b22128ef
JM
1834 os_free(p2p);
1835}
1836
1837
1838void p2p_flush(struct p2p_data *p2p)
1839{
1840 struct p2p_device *dev, *prev;
1841 p2p_clear_timeout(p2p);
1842 p2p_set_state(p2p, P2P_IDLE);
1843 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1844 p2p->go_neg_peer = NULL;
1845 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1846 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
1847 list) {
1848 dl_list_del(&dev->list);
1849 p2p_device_free(p2p, dev);
1850 }
1851 p2p_free_sd_queries(p2p);
1852}
1853
1854
1855int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
1856{
1857 os_free(p2p->cfg->dev_name);
1858 if (dev_name) {
1859 p2p->cfg->dev_name = os_strdup(dev_name);
1860 if (p2p->cfg->dev_name == NULL)
1861 return -1;
1862 } else
1863 p2p->cfg->dev_name = NULL;
1864 return 0;
1865}
1866
1867
1868int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
1869{
1870 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
1871 return 0;
1872}
1873
1874
1875int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
1876 size_t num_dev_types)
1877{
1878 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
1879 num_dev_types = P2P_SEC_DEVICE_TYPES;
1880 p2p->cfg->num_sec_dev_types = num_dev_types;
1881 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
1882 return 0;
1883}
1884
1885
1886int p2p_set_country(struct p2p_data *p2p, const char *country)
1887{
1888 os_memcpy(p2p->cfg->country, country, 3);
1889 return 0;
1890}
1891
1892
1893void p2p_continue_find(struct p2p_data *p2p)
1894{
1895 struct p2p_device *dev;
1896 p2p_set_state(p2p, P2P_SEARCH);
1897 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
1898 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
1899 if (p2p_start_sd(p2p, dev) == 0)
1900 return;
1901 else
1902 break;
1903 } else if (dev->req_config_methods) {
1904 if (p2p_send_prov_disc_req(p2p, dev, 0) == 0)
1905 return;
1906 }
1907 }
1908
1909 p2p_listen_in_find(p2p);
1910}
1911
1912
1913static void p2p_sd_cb(struct p2p_data *p2p, int success)
1914{
1915 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1916 "P2P: Service Discovery Query TX callback: success=%d",
1917 success);
1918 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1919
1920 if (!success) {
1921 if (p2p->sd_peer) {
1922 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
1923 p2p->sd_peer = NULL;
1924 }
1925 p2p_continue_find(p2p);
1926 return;
1927 }
1928
1929 if (p2p->sd_peer == NULL) {
1930 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1931 "P2P: No SD peer entry known");
1932 p2p_continue_find(p2p);
1933 return;
1934 }
1935
1936 /* Wait for response from the peer */
1937 p2p_set_state(p2p, P2P_SD_DURING_FIND);
1938 p2p_set_timeout(p2p, 0, 200000);
1939}
1940
1941
1942static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
1943{
1944 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1945 "P2P: Provision Discovery Request TX callback: success=%d",
1946 success);
1947 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1948
1949 if (!success) {
1950 if (p2p->state != P2P_IDLE)
1951 p2p_continue_find(p2p);
1952 return;
1953 }
1954
1955 /* Wait for response from the peer */
1956 if (p2p->state == P2P_SEARCH)
1957 p2p_set_state(p2p, P2P_PD_DURING_FIND);
1958 p2p_set_timeout(p2p, 0, 200000);
1959}
1960
1961
1962int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
1963 int level, const u8 *ies, size_t ies_len)
1964{
1965 p2p_add_device(p2p, bssid, freq, level, ies, ies_len);
1966
1967 if (p2p->go_neg_peer && p2p->state == P2P_SEARCH &&
1968 os_memcmp(p2p->go_neg_peer->p2p_device_addr, bssid, ETH_ALEN) == 0)
1969 {
1970 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1971 "P2P: Found GO Negotiation peer - try to start GO "
1972 "negotiation");
1973 p2p_connect_send(p2p, p2p->go_neg_peer);
1974 return 1;
1975 }
1976
1977 return 0;
1978}
1979
1980
1981void p2p_scan_res_handled(struct p2p_data *p2p)
1982{
1983 if (!p2p->p2p_scan_running) {
1984 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
1985 "running, but scan results received");
1986 }
1987 p2p->p2p_scan_running = 0;
1988 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1989
1990 if (p2p_run_after_scan(p2p))
1991 return;
1992 if (p2p->state == P2P_SEARCH)
1993 p2p_continue_find(p2p);
1994}
1995
1996
1997void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies)
1998{
1999 u8 *len = p2p_buf_add_ie_hdr(ies);
2000 p2p_buf_add_capability(ies, p2p->dev_capab, 0);
2001 if (p2p->cfg->reg_class && p2p->cfg->channel)
2002 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2003 p2p->cfg->reg_class,
2004 p2p->cfg->channel);
2005 if (p2p->ext_listen_interval)
2006 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2007 p2p->ext_listen_interval);
2008 /* TODO: p2p_buf_add_operating_channel() if GO */
2009 p2p_buf_update_ie_hdr(ies, len);
2010}
2011
2012
2013int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2014{
2015 return p2p_attr_text(p2p_ie, buf, end);
2016}
2017
2018
2019static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2020{
2021 struct p2p_device *dev = p2p->go_neg_peer;
2022
2023 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2024 "P2P: GO Negotiation Request TX callback: success=%d",
2025 success);
2026
2027 if (dev == NULL) {
2028 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2029 "P2P: No pending GO Negotiation");
2030 return;
2031 }
2032
2033 if (success) {
2034 dev->go_neg_req_sent++;
2035 if (dev->flags & P2P_DEV_USER_REJECTED) {
2036 p2p_set_state(p2p, P2P_IDLE);
2037 return;
2038 }
2039 }
2040
2041 if (!success &&
2042 (dev->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2043 !is_zero_ether_addr(dev->member_in_go_dev)) {
2044 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2045 "P2P: Peer " MACSTR " did not acknowledge request - "
2046 "try to use device discoverability through its GO",
2047 MAC2STR(dev->p2p_device_addr));
2048 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2049 p2p_send_dev_disc_req(p2p, dev);
2050 return;
2051 }
2052
2053 /*
2054 * Use P2P find, if needed, to find the other device from its listen
2055 * channel.
2056 */
2057 p2p_set_state(p2p, P2P_CONNECT);
2058 p2p_set_timeout(p2p, 0, 100000);
2059}
2060
2061
2062static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2063{
2064 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2065 "P2P: GO Negotiation Response TX callback: success=%d",
2066 success);
2067 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2068 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2069 "P2P: Ignore TX callback event - GO Negotiation is "
2070 "not running anymore");
2071 return;
2072 }
2073 p2p_set_state(p2p, P2P_CONNECT);
2074 p2p_set_timeout(p2p, 0, 100000);
2075}
2076
2077
2078static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2079{
2080 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2081 "P2P: GO Negotiation Response (failure) TX callback: "
2082 "success=%d", success);
fbe70272
JM
2083 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2084 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2085 p2p->go_neg_peer->status);
2086 }
b22128ef
JM
2087}
2088
2089
93b7ddd0
JM
2090static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
2091 enum p2p_send_action_result result)
b22128ef
JM
2092{
2093 struct p2p_device *dev;
2094
2095 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
93b7ddd0
JM
2096 "P2P: GO Negotiation Confirm TX callback: result=%d",
2097 result);
b22128ef 2098 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
93b7ddd0
JM
2099 if (result == P2P_SEND_ACTION_FAILED) {
2100 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2101 return;
2102 }
2103 if (result == P2P_SEND_ACTION_NO_ACK) {
b22128ef
JM
2104 /*
2105 * It looks like the TX status for GO Negotiation Confirm is
2106 * often showing failure even when the peer has actually
2107 * received the frame. Since the peer may change channels
2108 * immediately after having received the frame, we may not see
2109 * an Ack for retries, so just dropping a single frame may
2110 * trigger this. To allow the group formation to succeed if the
2111 * peer did indeed receive the frame, continue regardless of
2112 * the TX status.
2113 */
2114 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2115 "P2P: Assume GO Negotiation Confirm TX was actually "
2116 "received by the peer even though Ack was not "
2117 "reported");
2118 }
2119
2120 dev = p2p->go_neg_peer;
2121 if (dev == NULL)
2122 return;
2123
2124 p2p_go_complete(p2p, dev);
2125}
2126
2127
2128void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
93b7ddd0
JM
2129 const u8 *src, const u8 *bssid,
2130 enum p2p_send_action_result result)
b22128ef
JM
2131{
2132 enum p2p_pending_action_state state;
93b7ddd0 2133 int success;
b22128ef
JM
2134
2135 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2136 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
93b7ddd0 2137 " src=" MACSTR " bssid=" MACSTR " result=%d",
b22128ef 2138 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
93b7ddd0
JM
2139 MAC2STR(bssid), result);
2140 success = result == P2P_SEND_ACTION_SUCCESS;
b22128ef
JM
2141 state = p2p->pending_action_state;
2142 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2143 switch (state) {
2144 case P2P_NO_PENDING_ACTION:
2145 break;
2146 case P2P_PENDING_GO_NEG_REQUEST:
2147 p2p_go_neg_req_cb(p2p, success);
2148 break;
2149 case P2P_PENDING_GO_NEG_RESPONSE:
2150 p2p_go_neg_resp_cb(p2p, success);
2151 break;
2152 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
2153 p2p_go_neg_resp_failure_cb(p2p, success);
2154 break;
2155 case P2P_PENDING_GO_NEG_CONFIRM:
93b7ddd0 2156 p2p_go_neg_conf_cb(p2p, result);
b22128ef
JM
2157 break;
2158 case P2P_PENDING_SD:
2159 p2p_sd_cb(p2p, success);
2160 break;
2161 case P2P_PENDING_PD:
2162 p2p_prov_disc_cb(p2p, success);
2163 break;
2164 case P2P_PENDING_INVITATION_REQUEST:
2165 p2p_invitation_req_cb(p2p, success);
2166 break;
2167 case P2P_PENDING_INVITATION_RESPONSE:
2168 p2p_invitation_resp_cb(p2p, success);
2169 break;
2170 case P2P_PENDING_DEV_DISC_REQUEST:
2171 p2p_dev_disc_req_cb(p2p, success);
2172 break;
2173 case P2P_PENDING_DEV_DISC_RESPONSE:
2174 p2p_dev_disc_resp_cb(p2p, success);
2175 break;
2176 case P2P_PENDING_GO_DISC_REQ:
2177 p2p_go_disc_req_cb(p2p, success);
2178 break;
2179 }
2180}
2181
2182
2183void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
2184 unsigned int duration)
2185{
2186 if (freq == p2p->pending_client_disc_freq) {
2187 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2188 "P2P: Client discoverability remain-awake completed");
2189 p2p->pending_client_disc_freq = 0;
2190 return;
2191 }
2192
2193 if (freq != p2p->pending_listen_freq) {
2194 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2195 "P2P: Unexpected listen callback for freq=%u "
2196 "duration=%u (pending_listen_freq=%u)",
2197 freq, duration, p2p->pending_listen_freq);
2198 return;
2199 }
2200
2201 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2202 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
2203 "callback",
2204 p2p->pending_listen_sec, p2p->pending_listen_usec,
2205 p2p->pending_listen_freq);
2206 p2p->in_listen = 1;
0b8889d8 2207 p2p->drv_in_listen = freq;
b22128ef
JM
2208 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
2209 /*
2210 * Add 20 msec extra wait to avoid race condition with driver
2211 * remain-on-channel end event, i.e., give driver more time to
2212 * complete the operation before our timeout expires.
2213 */
2214 p2p_set_timeout(p2p, p2p->pending_listen_sec,
2215 p2p->pending_listen_usec + 20000);
2216 }
2217
2218 p2p->pending_listen_freq = 0;
2219}
2220
2221
2222int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
2223{
2224 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
2225 "state (freq=%u)", freq);
2226 p2p->drv_in_listen = 0;
2227 if (p2p->in_listen)
2228 return 0; /* Internal timeout will trigger the next step */
2229
2230 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
2231 p2p_set_state(p2p, P2P_CONNECT);
2232 p2p_connect_send(p2p, p2p->go_neg_peer);
2233 return 1;
2234 } else if (p2p->state == P2P_SEARCH) {
2235 p2p_search(p2p);
2236 return 1;
2237 }
2238
2239 return 0;
2240}
2241
2242
2243static void p2p_timeout_connect(struct p2p_data *p2p)
2244{
2245 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2246 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
2247 p2p_listen_in_find(p2p);
2248}
2249
2250
2251static void p2p_timeout_connect_listen(struct p2p_data *p2p)
2252{
2253 if (p2p->go_neg_peer) {
2254 if (p2p->drv_in_listen) {
2255 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
2256 "still in Listen state; wait for it to "
2257 "complete");
2258 return;
2259 }
2260 p2p_set_state(p2p, P2P_CONNECT);
2261 p2p_connect_send(p2p, p2p->go_neg_peer);
2262 } else
2263 p2p_set_state(p2p, P2P_IDLE);
2264}
2265
2266
2267static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
2268{
2269 /*
2270 * TODO: could remain constantly in Listen state for some time if there
2271 * are no other concurrent uses for the radio. For now, go to listen
2272 * state once per second to give other uses a chance to use the radio.
2273 */
2274 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
2275 p2p_set_timeout(p2p, 1, 0);
2276}
2277
2278
2279static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
2280{
2281 struct p2p_device *dev = p2p->go_neg_peer;
2282
2283 if (dev == NULL) {
2284 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2285 "P2P: Unknown GO Neg peer - stop GO Neg wait");
2286 return;
2287 }
2288
2289 dev->wait_count++;
2290 if (dev->wait_count >= 120) {
2291 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2292 "P2P: Timeout on waiting peer to become ready for GO "
2293 "Negotiation");
2294 p2p_go_neg_failed(p2p, dev, -1);
2295 return;
2296 }
2297
2298 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2299 "P2P: Go to Listen state while waiting for the peer to become "
2300 "ready for GO Negotiation");
2301 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
2302 p2p_listen_in_find(p2p);
2303}
2304
2305
2306static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
2307{
2308 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2309 "P2P: Service Discovery Query timeout");
2310 if (p2p->sd_peer) {
2311 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2312 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2313 p2p->sd_peer = NULL;
2314 }
2315 p2p_continue_find(p2p);
2316}
2317
2318
2319static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
2320{
2321 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2322 "P2P: Provision Discovery Request timeout");
2323 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2324 p2p_continue_find(p2p);
2325}
2326
2327
2328static void p2p_timeout_invite(struct p2p_data *p2p)
2329{
2330 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2331 p2p_set_state(p2p, P2P_INVITE_LISTEN);
04d8dad5
JM
2332 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
2333 /*
2334 * Better remain on operating channel instead of listen channel
2335 * when running a group.
2336 */
2337 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
2338 "active GO role - wait on operating channel");
2339 p2p_set_timeout(p2p, 0, 100000);
2340 return;
2341 }
b22128ef
JM
2342 p2p_listen_in_find(p2p);
2343}
2344
2345
2346static void p2p_timeout_invite_listen(struct p2p_data *p2p)
2347{
2348 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
2349 p2p_set_state(p2p, P2P_INVITE);
2350 p2p_invite_send(p2p, p2p->invite_peer,
2351 p2p->invite_go_dev_addr);
2352 } else {
2353 if (p2p->invite_peer) {
2354 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2355 "P2P: Invitation Request retry limit reached");
2356 if (p2p->cfg->invitation_result)
2357 p2p->cfg->invitation_result(
2358 p2p->cfg->cb_ctx, -1, NULL);
2359 }
2360 p2p_set_state(p2p, P2P_IDLE);
2361 }
2362}
2363
2364
2365static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
2366{
2367 struct p2p_data *p2p = eloop_ctx;
2368
2369 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
2370 p2p_state_txt(p2p->state));
2371
2372 p2p->in_listen = 0;
2373
2374 switch (p2p->state) {
2375 case P2P_IDLE:
2376 break;
2377 case P2P_SEARCH:
2378 p2p_search(p2p);
2379 break;
2380 case P2P_CONNECT:
2381 p2p_timeout_connect(p2p);
2382 break;
2383 case P2P_CONNECT_LISTEN:
2384 p2p_timeout_connect_listen(p2p);
2385 break;
2386 case P2P_GO_NEG:
2387 break;
2388 case P2P_LISTEN_ONLY:
2389 if (p2p->ext_listen_only) {
2390 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2391 "P2P: Extended Listen Timing - Listen State "
2392 "completed");
2393 p2p->ext_listen_only = 0;
2394 p2p_set_state(p2p, P2P_IDLE);
2395 }
2396 break;
2397 case P2P_WAIT_PEER_CONNECT:
2398 p2p_timeout_wait_peer_connect(p2p);
2399 break;
2400 case P2P_WAIT_PEER_IDLE:
2401 p2p_timeout_wait_peer_idle(p2p);
2402 break;
2403 case P2P_SD_DURING_FIND:
2404 p2p_timeout_sd_during_find(p2p);
2405 break;
2406 case P2P_PROVISIONING:
2407 break;
2408 case P2P_PD_DURING_FIND:
2409 p2p_timeout_prov_disc_during_find(p2p);
2410 break;
2411 case P2P_INVITE:
2412 p2p_timeout_invite(p2p);
2413 break;
2414 case P2P_INVITE_LISTEN:
2415 p2p_timeout_invite_listen(p2p);
2416 break;
2417 }
2418}
2419
2420
2421int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
2422{
2423 struct p2p_device *dev;
2424
2425 dev = p2p_get_device(p2p, peer_addr);
2426 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
2427 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
2428 if (dev == NULL) {
2429 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
2430 " unknown", MAC2STR(peer_addr));
2431 return -1;
2432 }
2433 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
2434 dev->flags |= P2P_DEV_USER_REJECTED;
2435 return 0;
2436}
2437
2438
2439static const char * p2p_wps_method_text(enum p2p_wps_method method)
2440{
2441 switch (method) {
2442 case WPS_NOT_READY:
2443 return "not-ready";
2444 case WPS_PIN_LABEL:
2445 return "Label";
2446 case WPS_PIN_DISPLAY:
2447 return "Display";
2448 case WPS_PIN_KEYPAD:
2449 return "Keypad";
2450 case WPS_PBC:
2451 return "PBC";
2452 }
2453
2454 return "??";
2455}
2456
2457
2458static const char * p2p_go_state_text(enum p2p_go_state go_state)
2459{
2460 switch (go_state) {
2461 case UNKNOWN_GO:
2462 return "unknown";
2463 case LOCAL_GO:
2464 return "local";
2465 case REMOTE_GO:
2466 return "remote";
2467 }
2468
2469 return "??";
2470}
2471
2472
2473int p2p_get_peer_info(struct p2p_data *p2p, const u8 *addr, int next,
2474 char *buf, size_t buflen)
2475{
2476 struct p2p_device *dev;
2477 int res;
2478 char *pos, *end;
2479 struct os_time now;
2480 char devtype[WPS_DEV_TYPE_BUFSIZE];
2481
2482 if (addr)
2483 dev = p2p_get_device(p2p, addr);
2484 else
2485 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
2486
2487 if (dev && next) {
2488 dev = dl_list_first(&dev->list, struct p2p_device, list);
2489 if (&dev->list == &p2p->devices)
2490 dev = NULL;
2491 }
2492
2493 if (dev == NULL)
2494 return -1;
2495
2496 pos = buf;
2497 end = buf + buflen;
2498
2499 res = os_snprintf(pos, end - pos, MACSTR "\n",
2500 MAC2STR(dev->p2p_device_addr));
2501 if (res < 0 || res >= end - pos)
2502 return pos - buf;
2503 pos += res;
2504
2505 os_get_time(&now);
2506 res = os_snprintf(pos, end - pos,
2507 "age=%d\n"
2508 "listen_freq=%d\n"
2509 "level=%d\n"
2510 "wps_method=%s\n"
2511 "interface_addr=" MACSTR "\n"
2512 "member_in_go_dev=" MACSTR "\n"
2513 "member_in_go_iface=" MACSTR "\n"
2514 "pri_dev_type=%s\n"
2515 "device_name=%s\n"
2516 "config_methods=0x%x\n"
2517 "dev_capab=0x%x\n"
2518 "group_capab=0x%x\n"
2519 "go_neg_req_sent=%d\n"
2520 "go_state=%s\n"
2521 "dialog_token=%u\n"
2522 "intended_addr=" MACSTR "\n"
2523 "country=%c%c\n"
2524 "oper_freq=%d\n"
2525 "req_config_methods=0x%x\n"
d5b20a73 2526 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
b22128ef
JM
2527 "status=%d\n"
2528 "wait_count=%u\n"
2529 "invitation_reqs=%u\n",
2530 (int) (now.sec - dev->last_seen.sec),
2531 dev->listen_freq,
2532 dev->level,
2533 p2p_wps_method_text(dev->wps_method),
2534 MAC2STR(dev->interface_addr),
2535 MAC2STR(dev->member_in_go_dev),
2536 MAC2STR(dev->member_in_go_iface),
2537 wps_dev_type_bin2str(dev->pri_dev_type,
2538 devtype, sizeof(devtype)),
2539 dev->device_name,
2540 dev->config_methods,
2541 dev->dev_capab,
2542 dev->group_capab,
2543 dev->go_neg_req_sent,
2544 p2p_go_state_text(dev->go_state),
2545 dev->dialog_token,
2546 MAC2STR(dev->intended_addr),
2547 dev->country[0] ? dev->country[0] : '_',
2548 dev->country[1] ? dev->country[1] : '_',
2549 dev->oper_freq,
2550 dev->req_config_methods,
2551 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
2552 "[PROBE_REQ_ONLY]" : "",
2553 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
2554 dev->flags & P2P_DEV_NOT_YET_READY ?
2555 "[NOT_YET_READY]" : "",
2556 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
2557 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
2558 "",
2559 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
2560 "[PD_PEER_DISPLAY]" : "",
2561 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
2562 "[PD_PEER_KEYPAD]" : "",
2563 dev->flags & P2P_DEV_USER_REJECTED ?
2564 "[USER_REJECTED]" : "",
2565 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
2566 "[PEER_WAITING_RESPONSE]" : "",
2567 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
2568 "[PREFER_PERSISTENT_GROUP]" : "",
2569 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
2570 "[WAIT_GO_NEG_RESPONSE]" : "",
2571 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
2572 "[WAIT_GO_NEG_CONFIRM]" : "",
2573 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
2574 "[GROUP_CLIENT_ONLY]" : "",
d5b20a73
JM
2575 dev->flags & P2P_DEV_FORCE_FREQ ?
2576 "[FORCE_FREQ" : "",
b22128ef
JM
2577 dev->status,
2578 dev->wait_count,
2579 dev->invitation_reqs);
2580 if (res < 0 || res >= end - pos)
2581 return pos - buf;
2582 pos += res;
2583
2584 if (dev->ext_listen_period) {
2585 res = os_snprintf(pos, end - pos,
2586 "ext_listen_period=%u\n"
2587 "ext_listen_interval=%u\n",
2588 dev->ext_listen_period,
2589 dev->ext_listen_interval);
2590 if (res < 0 || res >= end - pos)
2591 return pos - buf;
2592 pos += res;
2593 }
2594
2595 if (dev->oper_ssid_len) {
2596 res = os_snprintf(pos, end - pos,
2597 "oper_ssid=%s\n",
2598 wpa_ssid_txt(dev->oper_ssid,
2599 dev->oper_ssid_len));
2600 if (res < 0 || res >= end - pos)
2601 return pos - buf;
2602 pos += res;
2603 }
2604
2605 return pos - buf;
2606}
2607
2608
2609void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
2610{
2611 if (enabled) {
2612 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
2613 "discoverability enabled");
2614 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2615 } else {
2616 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
2617 "discoverability disabled");
2618 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2619 }
2620}
2621
2622
2623static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
2624 u32 duration2, u32 interval2)
2625{
2626 struct wpabuf *req;
2627 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
2628 u8 *len;
2629
2630 req = wpabuf_alloc(100);
2631 if (req == NULL)
2632 return NULL;
2633
2634 if (duration1 || interval1) {
2635 os_memset(&desc1, 0, sizeof(desc1));
2636 desc1.count_type = 1;
2637 desc1.duration = duration1;
2638 desc1.interval = interval1;
2639 ptr1 = &desc1;
2640
2641 if (duration2 || interval2) {
2642 os_memset(&desc2, 0, sizeof(desc2));
2643 desc2.count_type = 2;
2644 desc2.duration = duration2;
2645 desc2.interval = interval2;
2646 ptr2 = &desc2;
2647 }
2648 }
2649
2650 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
2651 len = p2p_buf_add_ie_hdr(req);
2652 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
2653 p2p_buf_update_ie_hdr(req, len);
2654
2655 return req;
2656}
2657
2658
2659int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
2660 const u8 *own_interface_addr, unsigned int freq,
2661 u32 duration1, u32 interval1, u32 duration2,
2662 u32 interval2)
2663{
2664 struct wpabuf *req;
2665
2666 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
2667 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
2668 "int1=%u dur2=%u int2=%u",
2669 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
2670 freq, duration1, interval1, duration2, interval2);
2671
2672 req = p2p_build_presence_req(duration1, interval1, duration2,
2673 interval2);
2674 if (req == NULL)
2675 return -1;
2676
2677 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2678 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, go_interface_addr,
2679 own_interface_addr,
2680 go_interface_addr,
2681 wpabuf_head(req), wpabuf_len(req), 200) < 0)
2682 {
2683 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2684 "P2P: Failed to send Action frame");
2685 }
2686 wpabuf_free(req);
2687
2688 return 0;
2689}
2690
2691
2692static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
2693 size_t noa_len, u8 dialog_token)
2694{
2695 struct wpabuf *resp;
2696 u8 *len;
2697
2698 resp = wpabuf_alloc(100 + noa_len);
2699 if (resp == NULL)
2700 return NULL;
2701
2702 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
2703 len = p2p_buf_add_ie_hdr(resp);
2704 p2p_buf_add_status(resp, status);
2705 if (noa) {
2706 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
2707 wpabuf_put_le16(resp, noa_len);
2708 wpabuf_put_data(resp, noa, noa_len);
2709 } else
2710 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
2711 p2p_buf_update_ie_hdr(resp, len);
2712
2713 return resp;
2714}
2715
2716
2717static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
2718 const u8 *sa, const u8 *data, size_t len,
2719 int rx_freq)
2720{
2721 struct p2p_message msg;
2722 u8 status;
2723 struct wpabuf *resp;
2724 size_t g;
2725 struct p2p_group *group = NULL;
2726 int parsed = 0;
2727 u8 noa[50];
2728 int noa_len;
2729
2730 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2731 "P2P: Received P2P Action - P2P Presence Request");
2732
2733 for (g = 0; g < p2p->num_groups; g++) {
2734 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
2735 ETH_ALEN) == 0) {
2736 group = p2p->groups[g];
2737 break;
2738 }
2739 }
2740 if (group == NULL) {
2741 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2742 "P2P: Ignore P2P Presence Request for unknown group "
2743 MACSTR, MAC2STR(da));
2744 return;
2745 }
2746
2747 if (p2p_parse(data, len, &msg) < 0) {
2748 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2749 "P2P: Failed to parse P2P Presence Request");
2750 status = P2P_SC_FAIL_INVALID_PARAMS;
2751 goto fail;
2752 }
2753 parsed = 1;
2754
2755 if (msg.noa == NULL) {
2756 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2757 "P2P: No NoA attribute in P2P Presence Request");
2758 status = P2P_SC_FAIL_INVALID_PARAMS;
2759 goto fail;
2760 }
2761
2762 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
2763
2764fail:
2765 if (p2p->cfg->get_noa)
2766 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
2767 sizeof(noa));
2768 else
2769 noa_len = -1;
2770 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
2771 noa_len > 0 ? noa_len : 0,
2772 msg.dialog_token);
2773 if (parsed)
2774 p2p_parse_free(&msg);
2775 if (resp == NULL)
2776 return;
2777
2778 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2779 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, rx_freq, sa, da, da,
2780 wpabuf_head(resp), wpabuf_len(resp), 200) <
2781 0) {
2782 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2783 "P2P: Failed to send Action frame");
2784 }
2785 wpabuf_free(resp);
2786}
2787
2788
2789static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
2790 const u8 *sa, const u8 *data, size_t len)
2791{
2792 struct p2p_message msg;
2793
2794 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2795 "P2P: Received P2P Action - P2P Presence Response");
2796
2797 if (p2p_parse(data, len, &msg) < 0) {
2798 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2799 "P2P: Failed to parse P2P Presence Response");
2800 return;
2801 }
2802
2803 if (msg.status == NULL || msg.noa == NULL) {
2804 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2805 "P2P: No Status or NoA attribute in P2P Presence "
2806 "Response");
2807 p2p_parse_free(&msg);
2808 return;
2809 }
2810
2811 if (*msg.status) {
2812 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2813 "P2P: P2P Presence Request was rejected: status %u",
2814 *msg.status);
2815 p2p_parse_free(&msg);
2816 return;
2817 }
2818
2819 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2820 "P2P: P2P Presence Request was accepted");
2821 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
2822 msg.noa, msg.noa_len);
2823 /* TODO: process NoA */
2824 p2p_parse_free(&msg);
2825}
2826
2827
2828static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
2829{
2830 struct p2p_data *p2p = eloop_ctx;
2831
2832 if (p2p->ext_listen_interval) {
2833 /* Schedule next extended listen timeout */
2834 eloop_register_timeout(p2p->ext_listen_interval_sec,
2835 p2p->ext_listen_interval_usec,
2836 p2p_ext_listen_timeout, p2p, NULL);
2837 }
2838
f7a69057
JM
2839 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
2840 /*
2841 * This should not really happen, but it looks like the Listen
2842 * command may fail is something else (e.g., a scan) was
2843 * running at an inconvenient time. As a workaround, allow new
2844 * Extended Listen operation to be started.
2845 */
2846 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
2847 "Extended Listen operation had not been completed - "
2848 "try again");
2849 p2p->ext_listen_only = 0;
2850 p2p_set_state(p2p, P2P_IDLE);
2851 }
2852
b22128ef
JM
2853 if (p2p->state != P2P_IDLE) {
2854 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
2855 "Listen timeout in active state (%s)",
2856 p2p_state_txt(p2p->state));
2857 return;
2858 }
2859
2860 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
2861 p2p->ext_listen_only = 1;
2862 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
2863 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
2864 "Listen state for Extended Listen Timing");
2865 p2p->ext_listen_only = 0;
2866 }
2867}
2868
2869
2870int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
2871 unsigned int interval)
2872{
2873 if (period > 65535 || interval > 65535 || period > interval ||
2874 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
2875 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2876 "P2P: Invalid Extended Listen Timing request: "
2877 "period=%u interval=%u", period, interval);
2878 return -1;
2879 }
2880
2881 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2882
2883 if (interval == 0) {
2884 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2885 "P2P: Disabling Extended Listen Timing");
2886 p2p->ext_listen_period = 0;
2887 p2p->ext_listen_interval = 0;
2888 return 0;
2889 }
2890
2891 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2892 "P2P: Enabling Extended Listen Timing: period %u msec, "
2893 "interval %u msec", period, interval);
2894 p2p->ext_listen_period = period;
2895 p2p->ext_listen_interval = interval;
2896 p2p->ext_listen_interval_sec = interval / 1000;
2897 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
2898
2899 eloop_register_timeout(p2p->ext_listen_interval_sec,
2900 p2p->ext_listen_interval_usec,
2901 p2p_ext_listen_timeout, p2p, NULL);
2902
2903 return 0;
2904}
2905
2906
2907void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
2908 const u8 *ie, size_t ie_len)
2909{
2910 struct p2p_message msg;
2911
2912 if (bssid == NULL || ie == NULL)
2913 return;
2914
2915 os_memset(&msg, 0, sizeof(msg));
2916 if (p2p_parse_ies(ie, ie_len, &msg))
2917 return;
2918 if (msg.minor_reason_code == NULL)
2919 return;
2920
2921 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
2922 "P2P: Deauthentication notification BSSID " MACSTR
2923 " reason_code=%u minor_reason_code=%u",
2924 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
2925
2926 p2p_parse_free(&msg);
2927}
2928
2929
2930void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
2931 const u8 *ie, size_t ie_len)
2932{
2933 struct p2p_message msg;
2934
2935 if (bssid == NULL || ie == NULL)
2936 return;
2937
2938 os_memset(&msg, 0, sizeof(msg));
2939 if (p2p_parse_ies(ie, ie_len, &msg))
2940 return;
2941 if (msg.minor_reason_code == NULL)
2942 return;
2943
2944 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
2945 "P2P: Disassociation notification BSSID " MACSTR
2946 " reason_code=%u minor_reason_code=%u",
2947 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
2948
2949 p2p_parse_free(&msg);
2950}
2951
2952
2953void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
2954{
2955 if (enabled) {
2956 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
2957 "Device operations enabled");
2958 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
2959 } else {
2960 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
2961 "Device operations disabled");
2962 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
2963 }
2964}
2965
2966
2967int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
2968{
2969 if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
2970 return -1;
2971
2972 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
2973 "reg_class %u channel %u", reg_class, channel);
2974 p2p->cfg->reg_class = reg_class;
2975 p2p->cfg->channel = channel;
2976
2977 return 0;
2978}
2979
2980
2981int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
2982{
2983 wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
2984 if (postfix == NULL) {
2985 p2p->cfg->ssid_postfix_len = 0;
2986 return 0;
2987 }
2988 if (len > sizeof(p2p->cfg->ssid_postfix))
2989 return -1;
2990 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
2991 p2p->cfg->ssid_postfix_len = len;
2992 return 0;
2993}
2994
2995
2996int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
2997 u8 *iface_addr)
2998{
2999 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
3000 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
3001 return -1;
3002 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
3003 return 0;
3004}
80c9582a
JM
3005
3006
4147a2cc
JM
3007int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
3008 u8 *dev_addr)
3009{
3010 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3011 if (dev == NULL)
3012 return -1;
3013 os_memcpy(dev_addr, dev->p2p_device_addr, ETH_ALEN);
3014 return 0;
3015}
3016
3017
80c9582a
JM
3018void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
3019{
3020 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
3021 if (is_zero_ether_addr(p2p->peer_filter))
3022 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
3023 "filter");
3024 else
3025 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
3026 "filter for " MACSTR, MAC2STR(p2p->peer_filter));
3027}
72044390
JM
3028
3029
3030void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
3031{
3032 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
3033 enabled ? "enabled" : "disabled");
3034 if (p2p->cross_connect == enabled)
3035 return;
3036 p2p->cross_connect = enabled;
3037 /* TODO: may need to tear down any action group where we are GO(?) */
3038}
f8d0131a
JM
3039
3040
3041int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
3042{
3043 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3044 if (dev == NULL)
3045 return -1;
3046 if (dev->oper_freq <= 0)
3047 return -1;
3048 return dev->oper_freq;
3049}
0f66abd2
SS
3050
3051
3052void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
3053{
3054 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
3055 enabled ? "enabled" : "disabled");
3056 p2p->cfg->p2p_intra_bss = enabled;
3057}
b5c9da8d
JM
3058
3059
3060void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
3061{
3062 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
3063 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
3064}