2 * Wi-Fi Direct - P2P module
3 * Copyright (c) 2009-2010, Atheros Communications
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.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #include "common/ieee802_11_defs.h"
20 #include "common/ieee802_11_common.h"
21 #include "wps/wps_i.h"
26 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
27 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
28 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
29 const u8 *sa, const u8 *data, size_t len,
31 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
32 const u8 *sa, const u8 *data,
34 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
35 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
39 * p2p_scan recovery timeout
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.
44 #define P2P_SCAN_TIMEOUT 35
47 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
48 * entries will be removed
50 #define P2P_PEER_EXPIRATION_AGE 300
52 #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
54 static void p2p_expire_peers(struct p2p_data *p2p)
56 struct p2p_device *dev, *n;
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)
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);
71 static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
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);
80 static const char * p2p_state_txt(int state)
89 case P2P_CONNECT_LISTEN:
90 return "CONNECT_LISTEN";
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";
107 case P2P_INVITE_LISTEN:
108 return "INVITE_LISTEN";
115 void p2p_set_state(struct p2p_data *p2p, int new_state)
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;
123 void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
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);
133 void p2p_clear_timeout(struct p2p_data *p2p)
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);
141 void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
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;
149 os_memset(&res, 0, sizeof(res));
152 os_memcpy(res.peer_device_addr, peer->p2p_device_addr,
154 os_memcpy(res.peer_interface_addr, peer->intended_addr,
157 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
161 static void p2p_listen_in_find(struct p2p_data *p2p)
167 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
168 "P2P: Starting short listen state (state=%s)",
169 p2p_state_txt(p2p->state));
171 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
174 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
175 "P2P: Unknown regulatory class/channel");
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;
183 p2p->pending_listen_freq = freq;
184 p2p->pending_listen_sec = 0;
185 p2p->pending_listen_usec = 1024 * tu;
187 ies = p2p_build_probe_resp_ies(p2p);
191 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
193 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
194 "P2P: Failed to start listen mode");
195 p2p->pending_listen_freq = 0;
201 int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
206 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
207 "P2P: Going to listen(only) state");
209 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
212 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
213 "P2P: Unknown regulatory class/channel");
217 p2p->pending_listen_freq = freq;
218 p2p->pending_listen_sec = timeout / 1000;
219 p2p->pending_listen_usec = (timeout % 1000) * 1000;
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;
228 ies = p2p_build_probe_resp_ies(p2p);
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;
241 p2p_set_state(p2p, P2P_LISTEN_ONLY);
247 static void p2p_device_clear_reported(struct p2p_data *p2p)
249 struct p2p_device *dev;
250 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
251 dev->flags &= ~P2P_DEV_REPORTED;
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
261 struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
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)
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
278 struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
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)
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
296 * If there is already an entry for the peer, it will be returned instead of
297 * creating a new one.
299 static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
302 struct p2p_device *dev, *oldest = NULL;
305 dev = p2p_get_device(p2p, addr);
309 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
311 if (oldest == NULL ||
312 os_time_before(&dev->last_seen, &oldest->last_seen))
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 "
319 dl_list_del(&oldest->list);
320 p2p_device_free(p2p, oldest);
323 dev = os_zalloc(sizeof(*dev));
326 dl_list_add(&p2p->devices, &dev->list);
327 os_memcpy(dev->p2p_device_addr, addr, ETH_ALEN);
333 static void p2p_copy_client_info(struct p2p_device *dev,
334 struct p2p_client_info *cli)
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);
344 static 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)
348 struct p2p_group_info info;
350 struct p2p_device *dev;
355 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
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.
362 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
363 if (os_memcpy(dev->member_in_go_iface, go_interface_addr,
365 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
366 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
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);
375 * Update information only if we have not received this
376 * directly from the client.
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;
385 dev = p2p_create_device(p2p, cli->p2p_device_addr);
388 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
389 p2p_copy_client_info(dev, cli);
390 dev->oper_freq = freq;
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,
398 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
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,
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
421 * If the scan result is for a GO, the clients in the group will also be added
424 static int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq,
425 int level, const u8 *ies, size_t ies_len)
427 struct p2p_device *dev;
428 struct p2p_message msg;
429 const u8 *p2p_dev_addr;
431 os_memset(&msg, 0, sizeof(msg));
432 if (p2p_parse_ies(ies, ies_len, &msg)) {
433 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
434 "P2P: Failed to parse P2P IE for a device entry");
435 p2p_parse_free(&msg);
439 if (msg.p2p_device_addr)
440 p2p_dev_addr = msg.p2p_device_addr;
441 else if (msg.device_id)
442 p2p_dev_addr = msg.device_id;
444 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
445 "P2P: Ignore scan data without P2P Device Info or "
447 p2p_parse_free(&msg);
451 if (!is_zero_ether_addr(p2p->peer_filter) &&
452 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
453 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
454 "filter for " MACSTR " due to peer filter",
455 MAC2STR(p2p_dev_addr));
459 dev = p2p_create_device(p2p, p2p_dev_addr);
461 p2p_parse_free(&msg);
464 os_get_time(&dev->last_seen);
465 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
467 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
468 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
470 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
471 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
473 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
474 dev->oper_ssid_len = msg.ssid[1];
477 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
478 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
480 if (*msg.ds_params == 14)
483 ds_freq = 2407 + *msg.ds_params * 5;
484 if (freq != ds_freq) {
485 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
486 "P2P: Update Listen frequency based on DS "
487 "Parameter Set IE: %d -> %d MHz",
493 if (dev->listen_freq && dev->listen_freq != freq) {
494 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
495 "P2P: Update Listen frequency based on scan "
496 "results (" MACSTR " %d -> %d MHz (DS param %d)",
497 MAC2STR(dev->p2p_device_addr), dev->listen_freq, freq,
498 msg.ds_params ? *msg.ds_params : -1);
500 dev->listen_freq = freq;
503 if (msg.pri_dev_type)
504 os_memcpy(dev->pri_dev_type, msg.pri_dev_type,
505 sizeof(dev->pri_dev_type));
506 os_memcpy(dev->device_name, msg.device_name, sizeof(dev->device_name));
507 dev->config_methods = msg.config_methods ? msg.config_methods :
508 msg.wps_config_methods;
509 if (msg.capability) {
510 dev->dev_capab = msg.capability[0];
511 dev->group_capab = msg.capability[1];
514 if (msg.ext_listen_timing) {
515 dev->ext_listen_period = WPA_GET_LE16(msg.ext_listen_timing);
516 dev->ext_listen_interval =
517 WPA_GET_LE16(msg.ext_listen_timing + 2);
520 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq, msg.group_info,
523 p2p_parse_free(&msg);
525 if (p2p_pending_sd_req(p2p, dev))
526 dev->flags |= P2P_DEV_SD_SCHEDULE;
528 if (dev->flags & P2P_DEV_REPORTED)
531 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
532 "P2P: Peer found with Listen frequency %d MHz", freq);
533 if (dev->flags & P2P_DEV_USER_REJECTED) {
534 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
535 "P2P: Do not report rejected device");
538 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, dev->p2p_device_addr,
539 dev->pri_dev_type, dev->device_name,
540 dev->config_methods, dev->dev_capab,
542 dev->flags |= P2P_DEV_REPORTED;
548 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
550 if (p2p->go_neg_peer == dev)
551 p2p->go_neg_peer = NULL;
552 if (p2p->invite_peer == dev)
553 p2p->invite_peer = NULL;
554 if (p2p->sd_peer == dev)
556 if (p2p->pending_client_disc_go == dev)
557 p2p->pending_client_disc_go = NULL;
563 static int p2p_get_next_prog_freq(struct p2p_data *p2p)
565 struct p2p_channels *c;
566 struct p2p_reg_class *cla;
573 c = &p2p->cfg->channels;
574 for (cl = 0; cl < c->reg_classes; cl++) {
575 cla = &c->reg_class[cl];
576 if (cla->reg_class != p2p->last_prog_scan_class)
578 for (ch = 0; ch < cla->channels; ch++) {
579 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
589 /* Start from beginning */
590 reg_class = c->reg_class[0].reg_class;
591 channel = c->reg_class[0].channel[0];
593 /* Pick the next channel */
595 if (ch == cla->channels) {
597 if (cl == c->reg_classes)
601 reg_class = c->reg_class[cl].reg_class;
602 channel = c->reg_class[cl].channel[ch];
605 freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
606 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
607 "channel: reg_class %u channel %u -> %d MHz",
608 reg_class, channel, freq);
609 p2p->last_prog_scan_class = reg_class;
610 p2p->last_prog_scan_chan = channel;
612 if (freq == 2412 || freq == 2437 || freq == 2462)
613 return 0; /* No need to add social channels */
618 static void p2p_search(struct p2p_data *p2p)
621 enum p2p_scan_type type;
623 if (p2p->drv_in_listen) {
624 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
625 "in Listen state - wait for it to end before "
629 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
631 if (p2p->go_neg_peer) {
633 * Only scan the known listen frequency of the peer
634 * during GO Negotiation start.
636 freq = p2p->go_neg_peer->listen_freq;
638 freq = p2p->go_neg_peer->oper_freq;
639 type = P2P_SCAN_SPECIFIC;
640 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
641 "for freq %u (GO Neg)", freq);
642 } else if (p2p->invite_peer) {
644 * Only scan the known listen frequency of the peer
645 * during Invite start.
647 freq = p2p->invite_peer->listen_freq;
649 freq = p2p->invite_peer->oper_freq;
650 type = P2P_SCAN_SPECIFIC;
651 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
652 "for freq %u (Invite)", freq);
653 } else if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
654 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
655 type = P2P_SCAN_SOCIAL_PLUS_ONE;
656 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
657 "(+ freq %u)", freq);
659 type = P2P_SCAN_SOCIAL;
660 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
663 if (p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq) < 0) {
664 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
665 "P2P: Scan request failed");
666 p2p_continue_find(p2p);
668 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
669 p2p->p2p_scan_running = 1;
670 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
671 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
677 static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
679 struct p2p_data *p2p = eloop_ctx;
680 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
685 static int p2p_run_after_scan(struct p2p_data *p2p)
687 struct p2p_device *dev;
688 enum p2p_after_scan op;
690 op = p2p->start_after_scan;
691 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
693 case P2P_AFTER_SCAN_NOTHING:
695 case P2P_AFTER_SCAN_LISTEN:
696 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
697 "requested Listen state");
698 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
699 p2p->pending_listen_usec / 1000);
701 case P2P_AFTER_SCAN_CONNECT:
702 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
703 "requested connect with " MACSTR,
704 MAC2STR(p2p->after_scan_peer));
705 dev = p2p_get_device(p2p, p2p->after_scan_peer);
707 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
711 p2p_connect_send(p2p, dev);
719 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
721 struct p2p_data *p2p = eloop_ctx;
723 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
724 "(running=%d)", p2p->p2p_scan_running);
725 running = p2p->p2p_scan_running;
726 /* Make sure we recover from missed scan results callback */
727 p2p->p2p_scan_running = 0;
730 p2p_run_after_scan(p2p);
734 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
735 enum p2p_discovery_type type)
739 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
741 if (p2p->p2p_scan_running) {
742 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
745 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
746 p2p_clear_timeout(p2p);
747 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
748 p2p->find_type = type;
749 p2p_device_clear_reported(p2p);
750 p2p_set_state(p2p, P2P_SEARCH);
751 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
753 eloop_register_timeout(timeout, 0, p2p_find_timeout,
756 case P2P_FIND_START_WITH_FULL:
757 case P2P_FIND_PROGRESSIVE:
758 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0);
760 case P2P_FIND_ONLY_SOCIAL:
761 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0);
768 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
769 p2p->p2p_scan_running = 1;
770 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
771 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
774 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
782 void p2p_stop_find(struct p2p_data *p2p)
784 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
785 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
786 p2p_clear_timeout(p2p);
787 p2p_set_state(p2p, P2P_IDLE);
788 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
789 p2p->go_neg_peer = NULL;
791 p2p->invite_peer = NULL;
792 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
796 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
797 enum p2p_wps_method wps_method,
798 int go_intent, const u8 *own_interface_addr,
799 unsigned int force_freq, int persistent_group)
801 struct p2p_device *dev;
803 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
804 "P2P: Request to start group negotiation - peer=" MACSTR
805 " GO Intent=%d Intended Interface Address=" MACSTR
806 " wps_method=%d persistent_group=%d",
807 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
808 wps_method, persistent_group);
811 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
812 &p2p->op_reg_class, &p2p->op_channel) <
814 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
815 "P2P: Unsupported frequency %u MHz",
819 p2p->channels.reg_classes = 1;
820 p2p->channels.reg_class[0].channels = 1;
821 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
822 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
824 p2p->op_reg_class = p2p->cfg->op_reg_class;
825 p2p->op_channel = p2p->cfg->op_channel;
826 os_memcpy(&p2p->channels, &p2p->cfg->channels,
827 sizeof(struct p2p_channels));
829 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
830 "P2P: Own preference for operation channel: "
831 "Regulatory Class %u Channel %u%s",
832 p2p->op_reg_class, p2p->op_channel,
833 force_freq ? " (forced)" : "");
835 dev = p2p_get_device(p2p, peer_addr);
836 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
837 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
838 "P2P: Cannot connect to unknown P2P Device " MACSTR,
843 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
844 if (!(dev->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
845 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
846 "P2P: Cannot connect to P2P Device " MACSTR
847 " that is in a group and is not discoverable",
851 if (dev->oper_freq <= 0) {
852 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
853 "P2P: Cannot connect to P2P Device " MACSTR
854 " with incomplete information",
860 * First, try to connect directly. If the peer does not
861 * acknowledge frames, assume it is sleeping and use device
862 * discoverability via the GO at that point.
866 dev->flags &= ~P2P_DEV_NOT_YET_READY;
867 dev->flags &= ~P2P_DEV_USER_REJECTED;
868 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
869 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
870 dev->go_neg_req_sent = 0;
871 dev->go_state = UNKNOWN_GO;
872 if (persistent_group)
873 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
875 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_GROUP;
876 p2p->go_intent = go_intent;
877 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
879 if (p2p->state != P2P_IDLE)
882 dev->wps_method = wps_method;
883 dev->status = P2P_SC_SUCCESS;
886 dev->flags |= P2P_DEV_FORCE_FREQ;
888 dev->flags &= ~P2P_DEV_FORCE_FREQ;
890 if (p2p->p2p_scan_running) {
891 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
892 "P2P: p2p_scan running - delay connect send");
893 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
894 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
897 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
899 return p2p_connect_send(p2p, dev);
903 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
904 enum p2p_wps_method wps_method,
905 int go_intent, const u8 *own_interface_addr,
906 unsigned int force_freq, int persistent_group)
908 struct p2p_device *dev;
910 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
911 "P2P: Request to authorize group negotiation - peer=" MACSTR
912 " GO Intent=%d Intended Interface Address=" MACSTR
913 " wps_method=%d persistent_group=%d",
914 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
915 wps_method, persistent_group);
918 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
919 &p2p->op_reg_class, &p2p->op_channel) <
921 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
922 "P2P: Unsupported frequency %u MHz",
926 p2p->channels.reg_classes = 1;
927 p2p->channels.reg_class[0].channels = 1;
928 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
929 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
931 p2p->op_reg_class = p2p->cfg->op_reg_class;
932 p2p->op_channel = p2p->cfg->op_channel;
933 os_memcpy(&p2p->channels, &p2p->cfg->channels,
934 sizeof(struct p2p_channels));
936 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
937 "P2P: Own preference for operation channel: "
938 "Regulatory Class %u Channel %u%s",
939 p2p->op_reg_class, p2p->op_channel,
940 force_freq ? " (forced)" : "");
942 dev = p2p_get_device(p2p, peer_addr);
944 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
945 "P2P: Cannot authorize unknown P2P Device " MACSTR,
950 dev->flags &= ~P2P_DEV_NOT_YET_READY;
951 dev->flags &= ~P2P_DEV_USER_REJECTED;
952 dev->go_neg_req_sent = 0;
953 dev->go_state = UNKNOWN_GO;
954 if (persistent_group)
955 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
957 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_GROUP;
958 p2p->go_intent = go_intent;
959 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
961 dev->wps_method = wps_method;
962 dev->status = P2P_SC_SUCCESS;
965 dev->flags |= P2P_DEV_FORCE_FREQ;
967 dev->flags &= ~P2P_DEV_FORCE_FREQ;
973 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
974 struct p2p_device *dev, struct p2p_message *msg)
976 os_get_time(&dev->last_seen);
978 if (msg->pri_dev_type)
979 os_memcpy(dev->pri_dev_type, msg->pri_dev_type,
980 sizeof(dev->pri_dev_type));
981 os_memcpy(dev->device_name, msg->device_name,
982 sizeof(dev->device_name));
983 dev->config_methods = msg->config_methods ? msg->config_methods :
984 msg->wps_config_methods;
985 if (msg->capability) {
986 dev->dev_capab = msg->capability[0];
987 dev->group_capab = msg->capability[1];
989 if (msg->listen_channel) {
991 freq = p2p_channel_to_freq((char *) msg->listen_channel,
992 msg->listen_channel[3],
993 msg->listen_channel[4]);
995 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
996 "P2P: Unknown peer Listen channel: "
997 "country=%c%c(0x%02x) reg_class=%u channel=%u",
998 msg->listen_channel[0],
999 msg->listen_channel[1],
1000 msg->listen_channel[2],
1001 msg->listen_channel[3],
1002 msg->listen_channel[4]);
1004 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1005 "peer " MACSTR " Listen channel: %u -> %u MHz",
1006 MAC2STR(dev->p2p_device_addr),
1007 dev->listen_freq, freq);
1008 dev->listen_freq = freq;
1011 if (msg->ext_listen_timing) {
1012 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
1013 dev->ext_listen_interval =
1014 WPA_GET_LE16(msg->ext_listen_timing + 2);
1017 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1018 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1019 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1020 "P2P: Completed device entry based on data from "
1021 "GO Negotiation Request");
1023 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1024 "P2P: Created device entry based on GO Neg Req: "
1025 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1027 MAC2STR(dev->p2p_device_addr), dev->dev_capab,
1028 dev->group_capab, dev->device_name, dev->listen_freq);
1031 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1033 if (dev->flags & P2P_DEV_USER_REJECTED) {
1034 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1035 "P2P: Do not report rejected device");
1039 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, dev->p2p_device_addr,
1040 dev->pri_dev_type, dev->device_name,
1041 dev->config_methods, dev->dev_capab,
1046 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1048 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1049 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1050 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1051 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1052 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1056 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1058 p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len);
1059 p2p_random(params->passphrase, 8);
1064 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1066 struct p2p_go_neg_results res;
1067 int go = peer->go_state == LOCAL_GO;
1068 struct p2p_channels intersection;
1072 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1073 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1074 "GO)", MAC2STR(peer->p2p_device_addr),
1075 go ? "local end" : "peer");
1077 os_memset(&res, 0, sizeof(res));
1079 os_memcpy(res.peer_device_addr, peer->p2p_device_addr, ETH_ALEN);
1080 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1081 res.wps_method = peer->wps_method;
1082 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
1083 res.persistent_group = 1;
1086 /* Setup AP mode for WPS provisioning */
1087 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1090 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1091 res.ssid_len = p2p->ssid_len;
1092 p2p_random(res.passphrase, 8);
1094 res.freq = peer->oper_freq;
1096 p2p_channels_intersect(&p2p->channels, &peer->channels,
1099 for (i = 0; i < intersection.reg_classes; i++) {
1100 struct p2p_reg_class *c = &intersection.reg_class[i];
1101 if (freqs + 1 == P2P_MAX_CHANNELS)
1103 for (j = 0; j < c->channels; j++) {
1105 if (freqs + 1 == P2P_MAX_CHANNELS)
1107 freq = p2p_channel_to_freq(peer->country, c->reg_class,
1111 res.freq_list[freqs++] = freq;
1115 p2p_clear_timeout(p2p);
1116 peer->go_neg_req_sent = 0;
1117 peer->wps_method = WPS_NOT_READY;
1119 p2p_set_state(p2p, P2P_PROVISIONING);
1120 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1124 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1125 const u8 *data, size_t len, int rx_freq)
1127 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1128 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1129 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1135 case P2P_GO_NEG_REQ:
1136 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1138 case P2P_GO_NEG_RESP:
1139 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1141 case P2P_GO_NEG_CONF:
1142 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1144 case P2P_INVITATION_REQ:
1145 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1148 case P2P_INVITATION_RESP:
1149 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1151 case P2P_PROV_DISC_REQ:
1152 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1154 case P2P_PROV_DISC_RESP:
1155 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1157 case P2P_DEV_DISC_REQ:
1158 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1160 case P2P_DEV_DISC_RESP:
1161 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1164 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1165 "P2P: Unsupported P2P Public Action frame type %d",
1172 void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1173 const u8 *bssid, const u8 *data, size_t len,
1180 case WLAN_PA_VENDOR_SPECIFIC:
1185 if (WPA_GET_BE24(data) != OUI_WFA)
1193 if (*data != P2P_OUI_TYPE)
1196 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1198 case WLAN_PA_GAS_INITIAL_REQ:
1199 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1201 case WLAN_PA_GAS_INITIAL_RESP:
1202 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1204 case WLAN_PA_GAS_COMEBACK_REQ:
1205 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1207 case WLAN_PA_GAS_COMEBACK_RESP:
1208 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1214 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1215 const u8 *bssid, u8 category,
1216 const u8 *data, size_t len, int freq)
1218 if (category == WLAN_ACTION_PUBLIC) {
1219 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1223 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1229 if (WPA_GET_BE24(data) != OUI_WFA)
1234 if (*data != P2P_OUI_TYPE)
1239 /* P2P action frame */
1240 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1241 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1242 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1248 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1249 "P2P: Received P2P Action - Notice of Absence");
1252 case P2P_PRESENCE_REQ:
1253 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1255 case P2P_PRESENCE_RESP:
1256 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1258 case P2P_GO_DISC_REQ:
1259 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1262 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1263 "P2P: Received P2P Action - unknown type %u", data[0]);
1269 static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1271 struct p2p_data *p2p = eloop_ctx;
1272 if (p2p->go_neg_peer == NULL)
1274 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1275 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1276 p2p_connect_send(p2p, p2p->go_neg_peer);
1280 static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1282 struct p2p_data *p2p = eloop_ctx;
1283 if (p2p->invite_peer == NULL)
1285 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1286 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1290 static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1291 const u8 *ie, size_t ie_len)
1293 struct p2p_message msg;
1294 struct p2p_device *dev;
1296 os_memset(&msg, 0, sizeof(msg));
1297 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1299 p2p_parse_free(&msg);
1300 return; /* not a P2P probe */
1303 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1304 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1306 /* The Probe Request is not part of P2P Device Discovery. It is
1307 * not known whether the source address of the frame is the P2P
1308 * Device Address or P2P Interface Address. Do not add a new
1309 * peer entry based on this frames.
1311 p2p_parse_free(&msg);
1315 dev = p2p_get_device(p2p, addr);
1317 if (dev->country[0] == 0 && msg.listen_channel)
1318 os_memcpy(dev->country, msg.listen_channel, 3);
1319 p2p_parse_free(&msg);
1320 return; /* already known */
1323 dev = p2p_create_device(p2p, addr);
1325 p2p_parse_free(&msg);
1329 os_get_time(&dev->last_seen);
1330 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1332 if (msg.capability) {
1333 dev->dev_capab = msg.capability[0];
1334 dev->group_capab = msg.capability[1];
1337 if (msg.listen_channel) {
1338 os_memcpy(dev->country, msg.listen_channel, 3);
1339 dev->listen_freq = p2p_channel_to_freq(dev->country,
1340 msg.listen_channel[3],
1341 msg.listen_channel[4]);
1344 os_memcpy(dev->device_name, msg.device_name, sizeof(dev->device_name));
1346 if (msg.wps_pri_dev_type)
1347 os_memcpy(dev->pri_dev_type, msg.wps_pri_dev_type,
1348 sizeof(dev->pri_dev_type));
1350 p2p_parse_free(&msg);
1352 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1353 "P2P: Created device entry based on Probe Req: " MACSTR
1354 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1355 MAC2STR(dev->p2p_device_addr), dev->dev_capab,
1356 dev->group_capab, dev->device_name, dev->listen_freq);
1360 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1362 struct p2p_message *msg)
1364 struct p2p_device *dev;
1366 dev = p2p_get_device(p2p, addr);
1368 os_get_time(&dev->last_seen);
1369 return dev; /* already known */
1372 dev = p2p_create_device(p2p, addr);
1376 p2p_add_dev_info(p2p, addr, dev, msg);
1382 static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1384 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1386 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1387 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1388 WPA_GET_BE16(&req_dev_type[6]) == 0)
1389 return 1; /* Category match with wildcard OUI/sub-category */
1394 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1395 size_t num_req_dev_type)
1398 for (i = 0; i < num_req_dev_type; i++) {
1399 if (dev_type_match(dev_type, req_dev_type[i]))
1407 * p2p_match_dev_type - Match local device type with requested type
1408 * @p2p: P2P module context from p2p_init()
1409 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1410 * Returns: 1 on match, 0 on mismatch
1412 * This function can be used to match the Requested Device Type attribute in
1413 * WPS IE with the local device types for deciding whether to reply to a Probe
1416 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1418 struct wps_parse_attr attr;
1421 if (wps_parse_msg(wps, &attr))
1422 return 1; /* assume no Requested Device Type attributes */
1424 if (attr.num_req_dev_type == 0)
1425 return 1; /* no Requested Device Type attributes -> match */
1427 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1428 attr.num_req_dev_type))
1429 return 1; /* Own Primary Device Type matches */
1431 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1432 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1434 attr.num_req_dev_type))
1435 return 1; /* Own Secondary Device Type matches */
1437 /* No matching device type found */
1442 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1447 buf = wpabuf_alloc(1000);
1451 /* TODO: add more info into WPS IE; maybe get from WPS module? */
1452 p2p_build_wps_ie(p2p, buf, DEV_PW_DEFAULT, 1);
1455 len = p2p_buf_add_ie_hdr(buf);
1456 p2p_buf_add_capability(buf, p2p->dev_capab, 0);
1457 if (p2p->ext_listen_interval)
1458 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1459 p2p->ext_listen_interval);
1460 p2p_buf_add_device_info(buf, p2p, NULL);
1461 p2p_buf_update_ie_hdr(buf, len);
1467 static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *ie,
1470 struct ieee802_11_elems elems;
1472 struct ieee80211_mgmt *resp;
1476 if (!p2p->in_listen || !p2p->drv_in_listen) {
1477 /* not in Listen state - ignore Probe Request */
1481 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
1483 /* Ignore invalid Probe Request frames */
1487 if (elems.p2p == NULL) {
1488 /* not a P2P probe - ignore it */
1492 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
1493 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
1495 /* not using P2P Wildcard SSID - ignore */
1499 /* Check Requested Device Type match */
1500 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1501 if (wps && !p2p_match_dev_type(p2p, wps)) {
1503 /* No match with Requested Device Type */
1508 if (!p2p->cfg->send_probe_resp)
1509 return; /* Response generated elsewhere */
1511 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1512 "P2P: Reply to P2P Probe Request in Listen state");
1515 * We do not really have a specific BSS that this frame is advertising,
1516 * so build a frame that has some information in valid format. This is
1517 * really only used for discovery purposes, not to learn exact BSS
1520 ies = p2p_build_probe_resp_ies(p2p);
1524 buf = wpabuf_alloc(200 + wpabuf_len(ies));
1531 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
1533 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
1534 (WLAN_FC_STYPE_PROBE_RESP << 4));
1535 os_memcpy(resp->da, addr, ETH_ALEN);
1536 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
1537 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
1538 resp->u.probe_resp.beacon_int = host_to_le16(100);
1539 /* hardware or low-level driver will setup seq_ctrl and timestamp */
1540 resp->u.probe_resp.capab_info =
1541 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
1542 WLAN_CAPABILITY_PRIVACY |
1543 WLAN_CAPABILITY_SHORT_SLOT_TIME);
1545 wpabuf_put_u8(buf, WLAN_EID_SSID);
1546 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
1547 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1549 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
1550 wpabuf_put_u8(buf, 8);
1551 wpabuf_put_u8(buf, (60 / 5) | 0x80);
1552 wpabuf_put_u8(buf, 90 / 5);
1553 wpabuf_put_u8(buf, (120 / 5) | 0x80);
1554 wpabuf_put_u8(buf, 180 / 5);
1555 wpabuf_put_u8(buf, (240 / 5) | 0x80);
1556 wpabuf_put_u8(buf, 360 / 5);
1557 wpabuf_put_u8(buf, 480 / 5);
1558 wpabuf_put_u8(buf, 540 / 5);
1560 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
1561 wpabuf_put_u8(buf, 1);
1562 wpabuf_put_u8(buf, p2p->cfg->channel);
1564 wpabuf_put_buf(buf, ies);
1567 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
1573 int p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *ie,
1576 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
1578 p2p_reply_probe(p2p, addr, ie, ie_len);
1580 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
1582 os_memcmp(addr, p2p->go_neg_peer->p2p_device_addr, ETH_ALEN) == 0)
1584 /* Received a Probe Request from GO Negotiation peer */
1585 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1586 "P2P: Found GO Negotiation peer - try to start GO "
1587 "negotiation from timeout");
1588 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
1592 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
1594 os_memcmp(addr, p2p->invite_peer->p2p_device_addr, ETH_ALEN) == 0)
1596 /* Received a Probe Request from Invite peer */
1597 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1598 "P2P: Found Invite peer - try to start Invite from "
1600 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
1608 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
1609 u8 *buf, size_t len, struct wpabuf *p2p_ie)
1618 return 0; /* WLAN AP is not a P2P manager */
1621 * (Re)Association Request - P2P IE
1622 * P2P Capability attribute (shall be present)
1623 * P2P Interface attribute (present if concurrent device and
1624 * P2P Management is enabled)
1626 tmp = wpabuf_alloc(200);
1630 lpos = p2p_buf_add_ie_hdr(tmp);
1632 if (p2p->num_groups > 0) {
1633 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
1634 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1635 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
1637 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
1639 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
1640 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1641 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
1642 p2p_buf_add_p2p_interface(tmp, p2p);
1643 p2p_buf_update_ie_hdr(tmp, lpos);
1645 tmplen = wpabuf_len(tmp);
1649 os_memcpy(buf, wpabuf_head(tmp), tmplen);
1658 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
1659 size_t len, int p2p_group, struct wpabuf *p2p_ie)
1663 struct p2p_device *peer;
1668 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
1671 * (Re)Association Request - P2P IE
1672 * P2P Capability attribute (shall be present)
1673 * Extended Listen Timing (may be present)
1674 * P2P Device Info attribute (shall be present)
1676 tmp = wpabuf_alloc(200);
1680 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
1682 lpos = p2p_buf_add_ie_hdr(tmp);
1683 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
1684 if (p2p->ext_listen_interval)
1685 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
1686 p2p->ext_listen_interval);
1687 p2p_buf_add_device_info(tmp, p2p, peer);
1688 p2p_buf_update_ie_hdr(tmp, lpos);
1690 tmplen = wpabuf_len(tmp);
1694 os_memcpy(buf, wpabuf_head(tmp), tmplen);
1703 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
1705 struct wpabuf *p2p_ie;
1708 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
1712 ret = p2p_attr_text(p2p_ie, buf, end);
1713 wpabuf_free(p2p_ie);
1718 static void p2p_clear_go_neg(struct p2p_data *p2p)
1720 p2p->go_neg_peer = NULL;
1721 p2p_clear_timeout(p2p);
1722 p2p_set_state(p2p, P2P_IDLE);
1726 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
1728 if (p2p->go_neg_peer == NULL) {
1729 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1730 "P2P: No pending Group Formation - "
1731 "ignore WPS registration success notification");
1732 return; /* No pending Group Formation */
1735 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
1737 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1738 "P2P: Ignore WPS registration success notification "
1739 "for " MACSTR " (GO Negotiation peer " MACSTR ")",
1741 MAC2STR(p2p->go_neg_peer->intended_addr));
1742 return; /* Ignore unexpected peer address */
1745 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1746 "P2P: Group Formation completed successfully with " MACSTR,
1749 p2p_clear_go_neg(p2p);
1753 void p2p_group_formation_failed(struct p2p_data *p2p)
1755 if (p2p->go_neg_peer == NULL) {
1756 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1757 "P2P: No pending Group Formation - "
1758 "ignore group formation failure notification");
1759 return; /* No pending Group Formation */
1762 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1763 "P2P: Group Formation failed with " MACSTR,
1764 MAC2STR(p2p->go_neg_peer->intended_addr));
1766 p2p_clear_go_neg(p2p);
1770 struct p2p_data * p2p_init(const struct p2p_config *cfg)
1772 struct p2p_data *p2p;
1774 if (cfg->max_peers < 1)
1777 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
1780 p2p->cfg = (struct p2p_config *) (p2p + 1);
1781 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
1783 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
1785 p2p->min_disc_int = 1;
1786 p2p->max_disc_int = 3;
1788 os_get_random(&p2p->next_tie_breaker, 1);
1789 p2p->next_tie_breaker &= 0x01;
1790 if (cfg->sd_request)
1791 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
1792 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
1793 if (cfg->concurrent_operations)
1794 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
1795 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
1797 dl_list_init(&p2p->devices);
1799 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
1800 p2p_expiration_timeout, p2p, NULL);
1806 void p2p_deinit(struct p2p_data *p2p)
1808 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
1809 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
1810 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1812 os_free(p2p->cfg->dev_name);
1813 os_free(p2p->groups);
1814 wpabuf_free(p2p->sd_resp);
1819 void p2p_flush(struct p2p_data *p2p)
1821 struct p2p_device *dev, *prev;
1822 p2p_clear_timeout(p2p);
1823 p2p_set_state(p2p, P2P_IDLE);
1824 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1825 p2p->go_neg_peer = NULL;
1826 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1827 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
1829 dl_list_del(&dev->list);
1830 p2p_device_free(p2p, dev);
1832 p2p_free_sd_queries(p2p);
1836 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
1838 os_free(p2p->cfg->dev_name);
1840 p2p->cfg->dev_name = os_strdup(dev_name);
1841 if (p2p->cfg->dev_name == NULL)
1844 p2p->cfg->dev_name = NULL;
1849 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
1851 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
1856 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
1857 size_t num_dev_types)
1859 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
1860 num_dev_types = P2P_SEC_DEVICE_TYPES;
1861 p2p->cfg->num_sec_dev_types = num_dev_types;
1862 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
1867 int p2p_set_country(struct p2p_data *p2p, const char *country)
1869 os_memcpy(p2p->cfg->country, country, 3);
1874 void p2p_continue_find(struct p2p_data *p2p)
1876 struct p2p_device *dev;
1877 p2p_set_state(p2p, P2P_SEARCH);
1878 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
1879 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
1880 if (p2p_start_sd(p2p, dev) == 0)
1884 } else if (dev->req_config_methods) {
1885 if (p2p_send_prov_disc_req(p2p, dev, 0) == 0)
1890 p2p_listen_in_find(p2p);
1894 static void p2p_sd_cb(struct p2p_data *p2p, int success)
1896 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1897 "P2P: Service Discovery Query TX callback: success=%d",
1899 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1903 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
1904 p2p->sd_peer = NULL;
1906 p2p_continue_find(p2p);
1910 if (p2p->sd_peer == NULL) {
1911 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1912 "P2P: No SD peer entry known");
1913 p2p_continue_find(p2p);
1917 /* Wait for response from the peer */
1918 p2p_set_state(p2p, P2P_SD_DURING_FIND);
1919 p2p_set_timeout(p2p, 0, 200000);
1923 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
1925 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1926 "P2P: Provision Discovery Request TX callback: success=%d",
1928 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1931 if (p2p->state != P2P_IDLE)
1932 p2p_continue_find(p2p);
1936 /* Wait for response from the peer */
1937 if (p2p->state == P2P_SEARCH)
1938 p2p_set_state(p2p, P2P_PD_DURING_FIND);
1939 p2p_set_timeout(p2p, 0, 200000);
1943 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
1944 int level, const u8 *ies, size_t ies_len)
1946 p2p_add_device(p2p, bssid, freq, level, ies, ies_len);
1948 if (p2p->go_neg_peer && p2p->state == P2P_SEARCH &&
1949 os_memcmp(p2p->go_neg_peer->p2p_device_addr, bssid, ETH_ALEN) == 0)
1951 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1952 "P2P: Found GO Negotiation peer - try to start GO "
1954 p2p_connect_send(p2p, p2p->go_neg_peer);
1962 void p2p_scan_res_handled(struct p2p_data *p2p)
1964 if (!p2p->p2p_scan_running) {
1965 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
1966 "running, but scan results received");
1968 p2p->p2p_scan_running = 0;
1969 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1971 if (p2p_run_after_scan(p2p))
1973 if (p2p->state == P2P_SEARCH)
1974 p2p_continue_find(p2p);
1978 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies)
1980 u8 *len = p2p_buf_add_ie_hdr(ies);
1981 p2p_buf_add_capability(ies, p2p->dev_capab, 0);
1982 if (p2p->cfg->reg_class && p2p->cfg->channel)
1983 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
1984 p2p->cfg->reg_class,
1986 if (p2p->ext_listen_interval)
1987 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
1988 p2p->ext_listen_interval);
1989 /* TODO: p2p_buf_add_operating_channel() if GO */
1990 p2p_buf_update_ie_hdr(ies, len);
1994 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
1996 return p2p_attr_text(p2p_ie, buf, end);
2000 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2002 struct p2p_device *dev = p2p->go_neg_peer;
2004 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2005 "P2P: GO Negotiation Request TX callback: success=%d",
2009 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2010 "P2P: No pending GO Negotiation");
2015 dev->go_neg_req_sent++;
2016 if (dev->flags & P2P_DEV_USER_REJECTED) {
2017 p2p_set_state(p2p, P2P_IDLE);
2023 (dev->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2024 !is_zero_ether_addr(dev->member_in_go_dev)) {
2025 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2026 "P2P: Peer " MACSTR " did not acknowledge request - "
2027 "try to use device discoverability through its GO",
2028 MAC2STR(dev->p2p_device_addr));
2029 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2030 p2p_send_dev_disc_req(p2p, dev);
2035 * Use P2P find, if needed, to find the other device from its listen
2038 p2p_set_state(p2p, P2P_CONNECT);
2039 p2p_set_timeout(p2p, 0, 100000);
2043 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2045 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2046 "P2P: GO Negotiation Response TX callback: success=%d",
2048 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2049 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2050 "P2P: Ignore TX callback event - GO Negotiation is "
2051 "not running anymore");
2054 p2p_set_state(p2p, P2P_CONNECT);
2055 p2p_set_timeout(p2p, 0, 100000);
2059 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2061 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2062 "P2P: GO Negotiation Response (failure) TX callback: "
2063 "success=%d", success);
2067 static void p2p_go_neg_conf_cb(struct p2p_data *p2p, int success)
2069 struct p2p_device *dev;
2071 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2072 "P2P: GO Negotiation Confirm TX callback: success=%d",
2074 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2077 * It looks like the TX status for GO Negotiation Confirm is
2078 * often showing failure even when the peer has actually
2079 * received the frame. Since the peer may change channels
2080 * immediately after having received the frame, we may not see
2081 * an Ack for retries, so just dropping a single frame may
2082 * trigger this. To allow the group formation to succeed if the
2083 * peer did indeed receive the frame, continue regardless of
2086 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2087 "P2P: Assume GO Negotiation Confirm TX was actually "
2088 "received by the peer even though Ack was not "
2092 dev = p2p->go_neg_peer;
2096 p2p_go_complete(p2p, dev);
2100 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
2101 const u8 *src, const u8 *bssid, int success)
2103 enum p2p_pending_action_state state;
2105 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2106 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
2107 " src=" MACSTR " bssid=" MACSTR " success=%d",
2108 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
2109 MAC2STR(bssid), success);
2110 state = p2p->pending_action_state;
2111 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2113 case P2P_NO_PENDING_ACTION:
2115 case P2P_PENDING_GO_NEG_REQUEST:
2116 p2p_go_neg_req_cb(p2p, success);
2118 case P2P_PENDING_GO_NEG_RESPONSE:
2119 p2p_go_neg_resp_cb(p2p, success);
2121 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
2122 p2p_go_neg_resp_failure_cb(p2p, success);
2124 case P2P_PENDING_GO_NEG_CONFIRM:
2125 p2p_go_neg_conf_cb(p2p, success);
2127 case P2P_PENDING_SD:
2128 p2p_sd_cb(p2p, success);
2130 case P2P_PENDING_PD:
2131 p2p_prov_disc_cb(p2p, success);
2133 case P2P_PENDING_INVITATION_REQUEST:
2134 p2p_invitation_req_cb(p2p, success);
2136 case P2P_PENDING_INVITATION_RESPONSE:
2137 p2p_invitation_resp_cb(p2p, success);
2139 case P2P_PENDING_DEV_DISC_REQUEST:
2140 p2p_dev_disc_req_cb(p2p, success);
2142 case P2P_PENDING_DEV_DISC_RESPONSE:
2143 p2p_dev_disc_resp_cb(p2p, success);
2145 case P2P_PENDING_GO_DISC_REQ:
2146 p2p_go_disc_req_cb(p2p, success);
2152 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
2153 unsigned int duration)
2155 if (freq == p2p->pending_client_disc_freq) {
2156 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2157 "P2P: Client discoverability remain-awake completed");
2158 p2p->pending_client_disc_freq = 0;
2162 if (freq != p2p->pending_listen_freq) {
2163 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2164 "P2P: Unexpected listen callback for freq=%u "
2165 "duration=%u (pending_listen_freq=%u)",
2166 freq, duration, p2p->pending_listen_freq);
2170 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2171 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
2173 p2p->pending_listen_sec, p2p->pending_listen_usec,
2174 p2p->pending_listen_freq);
2176 p2p->drv_in_listen = 1;
2177 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
2179 * Add 20 msec extra wait to avoid race condition with driver
2180 * remain-on-channel end event, i.e., give driver more time to
2181 * complete the operation before our timeout expires.
2183 p2p_set_timeout(p2p, p2p->pending_listen_sec,
2184 p2p->pending_listen_usec + 20000);
2187 p2p->pending_listen_freq = 0;
2191 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
2193 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
2194 "state (freq=%u)", freq);
2195 p2p->drv_in_listen = 0;
2197 return 0; /* Internal timeout will trigger the next step */
2199 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
2200 p2p_set_state(p2p, P2P_CONNECT);
2201 p2p_connect_send(p2p, p2p->go_neg_peer);
2203 } else if (p2p->state == P2P_SEARCH) {
2212 static void p2p_timeout_connect(struct p2p_data *p2p)
2214 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2215 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
2216 p2p_listen_in_find(p2p);
2220 static void p2p_timeout_connect_listen(struct p2p_data *p2p)
2222 if (p2p->go_neg_peer) {
2223 if (p2p->drv_in_listen) {
2224 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
2225 "still in Listen state; wait for it to "
2229 p2p_set_state(p2p, P2P_CONNECT);
2230 p2p_connect_send(p2p, p2p->go_neg_peer);
2232 p2p_set_state(p2p, P2P_IDLE);
2236 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
2239 * TODO: could remain constantly in Listen state for some time if there
2240 * are no other concurrent uses for the radio. For now, go to listen
2241 * state once per second to give other uses a chance to use the radio.
2243 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
2244 p2p_set_timeout(p2p, 1, 0);
2248 static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
2250 struct p2p_device *dev = p2p->go_neg_peer;
2253 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2254 "P2P: Unknown GO Neg peer - stop GO Neg wait");
2259 if (dev->wait_count >= 120) {
2260 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2261 "P2P: Timeout on waiting peer to become ready for GO "
2263 p2p_go_neg_failed(p2p, dev, -1);
2267 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2268 "P2P: Go to Listen state while waiting for the peer to become "
2269 "ready for GO Negotiation");
2270 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
2271 p2p_listen_in_find(p2p);
2275 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
2277 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2278 "P2P: Service Discovery Query timeout");
2280 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2281 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2282 p2p->sd_peer = NULL;
2284 p2p_continue_find(p2p);
2288 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
2290 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2291 "P2P: Provision Discovery Request timeout");
2292 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2293 p2p_continue_find(p2p);
2297 static void p2p_timeout_invite(struct p2p_data *p2p)
2299 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2300 p2p_set_state(p2p, P2P_INVITE_LISTEN);
2301 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
2303 * Better remain on operating channel instead of listen channel
2304 * when running a group.
2306 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
2307 "active GO role - wait on operating channel");
2308 p2p_set_timeout(p2p, 0, 100000);
2311 p2p_listen_in_find(p2p);
2315 static void p2p_timeout_invite_listen(struct p2p_data *p2p)
2317 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
2318 p2p_set_state(p2p, P2P_INVITE);
2319 p2p_invite_send(p2p, p2p->invite_peer,
2320 p2p->invite_go_dev_addr);
2322 if (p2p->invite_peer) {
2323 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2324 "P2P: Invitation Request retry limit reached");
2325 if (p2p->cfg->invitation_result)
2326 p2p->cfg->invitation_result(
2327 p2p->cfg->cb_ctx, -1, NULL);
2329 p2p_set_state(p2p, P2P_IDLE);
2334 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
2336 struct p2p_data *p2p = eloop_ctx;
2338 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
2339 p2p_state_txt(p2p->state));
2343 switch (p2p->state) {
2350 p2p_timeout_connect(p2p);
2352 case P2P_CONNECT_LISTEN:
2353 p2p_timeout_connect_listen(p2p);
2357 case P2P_LISTEN_ONLY:
2358 if (p2p->ext_listen_only) {
2359 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2360 "P2P: Extended Listen Timing - Listen State "
2362 p2p->ext_listen_only = 0;
2363 p2p_set_state(p2p, P2P_IDLE);
2366 case P2P_WAIT_PEER_CONNECT:
2367 p2p_timeout_wait_peer_connect(p2p);
2369 case P2P_WAIT_PEER_IDLE:
2370 p2p_timeout_wait_peer_idle(p2p);
2372 case P2P_SD_DURING_FIND:
2373 p2p_timeout_sd_during_find(p2p);
2375 case P2P_PROVISIONING:
2377 case P2P_PD_DURING_FIND:
2378 p2p_timeout_prov_disc_during_find(p2p);
2381 p2p_timeout_invite(p2p);
2383 case P2P_INVITE_LISTEN:
2384 p2p_timeout_invite_listen(p2p);
2390 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
2392 struct p2p_device *dev;
2394 dev = p2p_get_device(p2p, peer_addr);
2395 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
2396 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
2398 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
2399 " unknown", MAC2STR(peer_addr));
2402 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
2403 dev->flags |= P2P_DEV_USER_REJECTED;
2408 static const char * p2p_wps_method_text(enum p2p_wps_method method)
2415 case WPS_PIN_DISPLAY:
2417 case WPS_PIN_KEYPAD:
2427 static const char * p2p_go_state_text(enum p2p_go_state go_state)
2442 int p2p_get_peer_info(struct p2p_data *p2p, const u8 *addr, int next,
2443 char *buf, size_t buflen)
2445 struct p2p_device *dev;
2449 char devtype[WPS_DEV_TYPE_BUFSIZE];
2452 dev = p2p_get_device(p2p, addr);
2454 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
2457 dev = dl_list_first(&dev->list, struct p2p_device, list);
2458 if (&dev->list == &p2p->devices)
2468 res = os_snprintf(pos, end - pos, MACSTR "\n",
2469 MAC2STR(dev->p2p_device_addr));
2470 if (res < 0 || res >= end - pos)
2475 res = os_snprintf(pos, end - pos,
2480 "interface_addr=" MACSTR "\n"
2481 "member_in_go_dev=" MACSTR "\n"
2482 "member_in_go_iface=" MACSTR "\n"
2485 "config_methods=0x%x\n"
2487 "group_capab=0x%x\n"
2488 "go_neg_req_sent=%d\n"
2491 "intended_addr=" MACSTR "\n"
2494 "req_config_methods=0x%x\n"
2495 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
2498 "invitation_reqs=%u\n",
2499 (int) (now.sec - dev->last_seen.sec),
2502 p2p_wps_method_text(dev->wps_method),
2503 MAC2STR(dev->interface_addr),
2504 MAC2STR(dev->member_in_go_dev),
2505 MAC2STR(dev->member_in_go_iface),
2506 wps_dev_type_bin2str(dev->pri_dev_type,
2507 devtype, sizeof(devtype)),
2509 dev->config_methods,
2512 dev->go_neg_req_sent,
2513 p2p_go_state_text(dev->go_state),
2515 MAC2STR(dev->intended_addr),
2516 dev->country[0] ? dev->country[0] : '_',
2517 dev->country[1] ? dev->country[1] : '_',
2519 dev->req_config_methods,
2520 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
2521 "[PROBE_REQ_ONLY]" : "",
2522 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
2523 dev->flags & P2P_DEV_NOT_YET_READY ?
2524 "[NOT_YET_READY]" : "",
2525 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
2526 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
2528 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
2529 "[PD_PEER_DISPLAY]" : "",
2530 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
2531 "[PD_PEER_KEYPAD]" : "",
2532 dev->flags & P2P_DEV_USER_REJECTED ?
2533 "[USER_REJECTED]" : "",
2534 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
2535 "[PEER_WAITING_RESPONSE]" : "",
2536 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
2537 "[PREFER_PERSISTENT_GROUP]" : "",
2538 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
2539 "[WAIT_GO_NEG_RESPONSE]" : "",
2540 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
2541 "[WAIT_GO_NEG_CONFIRM]" : "",
2542 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
2543 "[GROUP_CLIENT_ONLY]" : "",
2544 dev->flags & P2P_DEV_FORCE_FREQ ?
2548 dev->invitation_reqs);
2549 if (res < 0 || res >= end - pos)
2553 if (dev->ext_listen_period) {
2554 res = os_snprintf(pos, end - pos,
2555 "ext_listen_period=%u\n"
2556 "ext_listen_interval=%u\n",
2557 dev->ext_listen_period,
2558 dev->ext_listen_interval);
2559 if (res < 0 || res >= end - pos)
2564 if (dev->oper_ssid_len) {
2565 res = os_snprintf(pos, end - pos,
2567 wpa_ssid_txt(dev->oper_ssid,
2568 dev->oper_ssid_len));
2569 if (res < 0 || res >= end - pos)
2578 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
2581 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
2582 "discoverability enabled");
2583 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2585 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
2586 "discoverability disabled");
2587 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2592 static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
2593 u32 duration2, u32 interval2)
2596 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
2599 req = wpabuf_alloc(100);
2603 if (duration1 || interval1) {
2604 os_memset(&desc1, 0, sizeof(desc1));
2605 desc1.count_type = 1;
2606 desc1.duration = duration1;
2607 desc1.interval = interval1;
2610 if (duration2 || interval2) {
2611 os_memset(&desc2, 0, sizeof(desc2));
2612 desc2.count_type = 2;
2613 desc2.duration = duration2;
2614 desc2.interval = interval2;
2619 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
2620 len = p2p_buf_add_ie_hdr(req);
2621 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
2622 p2p_buf_update_ie_hdr(req, len);
2628 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
2629 const u8 *own_interface_addr, unsigned int freq,
2630 u32 duration1, u32 interval1, u32 duration2,
2635 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
2636 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
2637 "int1=%u dur2=%u int2=%u",
2638 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
2639 freq, duration1, interval1, duration2, interval2);
2641 req = p2p_build_presence_req(duration1, interval1, duration2,
2646 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2647 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, go_interface_addr,
2650 wpabuf_head(req), wpabuf_len(req), 200) < 0)
2652 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2653 "P2P: Failed to send Action frame");
2661 static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
2662 size_t noa_len, u8 dialog_token)
2664 struct wpabuf *resp;
2667 resp = wpabuf_alloc(100 + noa_len);
2671 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
2672 len = p2p_buf_add_ie_hdr(resp);
2673 p2p_buf_add_status(resp, status);
2675 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
2676 wpabuf_put_le16(resp, noa_len);
2677 wpabuf_put_data(resp, noa, noa_len);
2679 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
2680 p2p_buf_update_ie_hdr(resp, len);
2686 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
2687 const u8 *sa, const u8 *data, size_t len,
2690 struct p2p_message msg;
2692 struct wpabuf *resp;
2694 struct p2p_group *group = NULL;
2699 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2700 "P2P: Received P2P Action - P2P Presence Request");
2702 for (g = 0; g < p2p->num_groups; g++) {
2703 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
2705 group = p2p->groups[g];
2709 if (group == NULL) {
2710 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2711 "P2P: Ignore P2P Presence Request for unknown group "
2712 MACSTR, MAC2STR(da));
2716 if (p2p_parse(data, len, &msg) < 0) {
2717 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2718 "P2P: Failed to parse P2P Presence Request");
2719 status = P2P_SC_FAIL_INVALID_PARAMS;
2724 if (msg.noa == NULL) {
2725 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2726 "P2P: No NoA attribute in P2P Presence Request");
2727 status = P2P_SC_FAIL_INVALID_PARAMS;
2731 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
2734 if (p2p->cfg->get_noa)
2735 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
2739 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
2740 noa_len > 0 ? noa_len : 0,
2743 p2p_parse_free(&msg);
2747 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2748 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, rx_freq, sa, da, da,
2749 wpabuf_head(resp), wpabuf_len(resp), 200) <
2751 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2752 "P2P: Failed to send Action frame");
2758 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
2759 const u8 *sa, const u8 *data, size_t len)
2761 struct p2p_message msg;
2763 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2764 "P2P: Received P2P Action - P2P Presence Response");
2766 if (p2p_parse(data, len, &msg) < 0) {
2767 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2768 "P2P: Failed to parse P2P Presence Response");
2772 if (msg.status == NULL || msg.noa == NULL) {
2773 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2774 "P2P: No Status or NoA attribute in P2P Presence "
2776 p2p_parse_free(&msg);
2781 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2782 "P2P: P2P Presence Request was rejected: status %u",
2784 p2p_parse_free(&msg);
2788 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2789 "P2P: P2P Presence Request was accepted");
2790 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
2791 msg.noa, msg.noa_len);
2792 /* TODO: process NoA */
2793 p2p_parse_free(&msg);
2797 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
2799 struct p2p_data *p2p = eloop_ctx;
2801 if (p2p->ext_listen_interval) {
2802 /* Schedule next extended listen timeout */
2803 eloop_register_timeout(p2p->ext_listen_interval_sec,
2804 p2p->ext_listen_interval_usec,
2805 p2p_ext_listen_timeout, p2p, NULL);
2808 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
2810 * This should not really happen, but it looks like the Listen
2811 * command may fail is something else (e.g., a scan) was
2812 * running at an inconvenient time. As a workaround, allow new
2813 * Extended Listen operation to be started.
2815 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
2816 "Extended Listen operation had not been completed - "
2818 p2p->ext_listen_only = 0;
2819 p2p_set_state(p2p, P2P_IDLE);
2822 if (p2p->state != P2P_IDLE) {
2823 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
2824 "Listen timeout in active state (%s)",
2825 p2p_state_txt(p2p->state));
2829 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
2830 p2p->ext_listen_only = 1;
2831 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
2832 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
2833 "Listen state for Extended Listen Timing");
2834 p2p->ext_listen_only = 0;
2839 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
2840 unsigned int interval)
2842 if (period > 65535 || interval > 65535 || period > interval ||
2843 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
2844 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2845 "P2P: Invalid Extended Listen Timing request: "
2846 "period=%u interval=%u", period, interval);
2850 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2852 if (interval == 0) {
2853 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2854 "P2P: Disabling Extended Listen Timing");
2855 p2p->ext_listen_period = 0;
2856 p2p->ext_listen_interval = 0;
2860 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2861 "P2P: Enabling Extended Listen Timing: period %u msec, "
2862 "interval %u msec", period, interval);
2863 p2p->ext_listen_period = period;
2864 p2p->ext_listen_interval = interval;
2865 p2p->ext_listen_interval_sec = interval / 1000;
2866 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
2868 eloop_register_timeout(p2p->ext_listen_interval_sec,
2869 p2p->ext_listen_interval_usec,
2870 p2p_ext_listen_timeout, p2p, NULL);
2876 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
2877 const u8 *ie, size_t ie_len)
2879 struct p2p_message msg;
2881 if (bssid == NULL || ie == NULL)
2884 os_memset(&msg, 0, sizeof(msg));
2885 if (p2p_parse_ies(ie, ie_len, &msg))
2887 if (msg.minor_reason_code == NULL)
2890 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
2891 "P2P: Deauthentication notification BSSID " MACSTR
2892 " reason_code=%u minor_reason_code=%u",
2893 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
2895 p2p_parse_free(&msg);
2899 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
2900 const u8 *ie, size_t ie_len)
2902 struct p2p_message msg;
2904 if (bssid == NULL || ie == NULL)
2907 os_memset(&msg, 0, sizeof(msg));
2908 if (p2p_parse_ies(ie, ie_len, &msg))
2910 if (msg.minor_reason_code == NULL)
2913 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
2914 "P2P: Disassociation notification BSSID " MACSTR
2915 " reason_code=%u minor_reason_code=%u",
2916 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
2918 p2p_parse_free(&msg);
2922 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
2925 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
2926 "Device operations enabled");
2927 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
2929 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
2930 "Device operations disabled");
2931 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
2936 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
2938 if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
2941 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
2942 "reg_class %u channel %u", reg_class, channel);
2943 p2p->cfg->reg_class = reg_class;
2944 p2p->cfg->channel = channel;
2950 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
2952 wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
2953 if (postfix == NULL) {
2954 p2p->cfg->ssid_postfix_len = 0;
2957 if (len > sizeof(p2p->cfg->ssid_postfix))
2959 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
2960 p2p->cfg->ssid_postfix_len = len;
2965 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
2968 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
2969 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
2971 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
2976 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
2979 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
2982 os_memcpy(dev_addr, dev->p2p_device_addr, ETH_ALEN);
2987 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
2989 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
2990 if (is_zero_ether_addr(p2p->peer_filter))
2991 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
2994 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
2995 "filter for " MACSTR, MAC2STR(p2p->peer_filter));
2999 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
3001 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
3002 enabled ? "enabled" : "disabled");
3003 if (p2p->cross_connect == enabled)
3005 p2p->cross_connect = enabled;
3006 /* TODO: may need to tear down any action group where we are GO(?) */