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
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
426 int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
427 const u8 *ies, size_t ies_len)
429 struct p2p_device *dev;
430 struct p2p_message msg;
431 const u8 *p2p_dev_addr;
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);
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;
446 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
447 "P2P: Ignore scan data without P2P Device Info or "
449 p2p_parse_free(&msg);
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));
461 dev = p2p_create_device(p2p, p2p_dev_addr);
463 p2p_parse_free(&msg);
466 os_get_time(&dev->last_seen);
467 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
469 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
470 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
472 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
473 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
475 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
476 dev->oper_ssid_len = msg.ssid[1];
479 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
480 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
482 if (*msg.ds_params == 14)
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",
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);
502 dev->listen_freq = freq;
504 dev->oper_freq = freq;
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];
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);
524 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq, msg.group_info,
527 p2p_parse_free(&msg);
529 if (p2p_pending_sd_req(p2p, dev))
530 dev->flags |= P2P_DEV_SD_SCHEDULE;
532 if (dev->flags & P2P_DEV_REPORTED)
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");
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,
546 dev->flags |= P2P_DEV_REPORTED;
552 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
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)
560 if (p2p->pending_client_disc_go == dev)
561 p2p->pending_client_disc_go = NULL;
567 static int p2p_get_next_prog_freq(struct p2p_data *p2p)
569 struct p2p_channels *c;
570 struct p2p_reg_class *cla;
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)
582 for (ch = 0; ch < cla->channels; ch++) {
583 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
593 /* Start from beginning */
594 reg_class = c->reg_class[0].reg_class;
595 channel = c->reg_class[0].channel[0];
597 /* Pick the next channel */
599 if (ch == cla->channels) {
601 if (cl == c->reg_classes)
605 reg_class = c->reg_class[cl].reg_class;
606 channel = c->reg_class[cl].channel[ch];
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;
616 if (freq == 2412 || freq == 2437 || freq == 2462)
617 return 0; /* No need to add social channels */
622 static void p2p_search(struct p2p_data *p2p)
625 enum p2p_scan_type type;
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 "
633 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
635 if (p2p->go_neg_peer) {
637 * Only scan the known listen frequency of the peer
638 * during GO Negotiation start.
640 freq = p2p->go_neg_peer->listen_freq;
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) {
648 * Only scan the known listen frequency of the peer
649 * during Invite start.
651 freq = p2p->invite_peer->listen_freq;
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);
663 type = P2P_SCAN_SOCIAL;
664 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
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);
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,
681 static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
683 struct p2p_data *p2p = eloop_ctx;
684 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
689 static int p2p_run_after_scan(struct p2p_data *p2p)
691 struct p2p_device *dev;
692 enum p2p_after_scan op;
694 op = p2p->start_after_scan;
695 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
697 case P2P_AFTER_SCAN_NOTHING:
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);
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);
711 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
715 p2p_connect_send(p2p, dev);
723 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
725 struct p2p_data *p2p = eloop_ctx;
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;
734 p2p_run_after_scan(p2p);
738 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
739 enum p2p_discovery_type type)
743 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
745 if (p2p->p2p_scan_running) {
746 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
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);
757 eloop_register_timeout(timeout, 0, p2p_find_timeout,
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);
764 case P2P_FIND_ONLY_SOCIAL:
765 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 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,
778 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
786 void p2p_stop_find(struct p2p_data *p2p)
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;
795 p2p->invite_peer = NULL;
796 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
800 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
801 enum p2p_wps_method wps_method,
802 int go_intent, const u8 *own_interface_addr,
803 unsigned int force_freq, int persistent_group)
805 struct p2p_device *dev;
807 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
808 "P2P: Request to start group negotiation - peer=" MACSTR
809 " GO Intent=%d Intended Interface Address=" MACSTR
810 " wps_method=%d persistent_group=%d",
811 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
812 wps_method, persistent_group);
815 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
816 &p2p->op_reg_class, &p2p->op_channel) <
818 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
819 "P2P: Unsupported frequency %u MHz",
823 p2p->channels.reg_classes = 1;
824 p2p->channels.reg_class[0].channels = 1;
825 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
826 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
828 p2p->op_reg_class = p2p->cfg->op_reg_class;
829 p2p->op_channel = p2p->cfg->op_channel;
830 os_memcpy(&p2p->channels, &p2p->cfg->channels,
831 sizeof(struct p2p_channels));
833 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
834 "P2P: Own preference for operation channel: "
835 "Regulatory Class %u Channel %u%s",
836 p2p->op_reg_class, p2p->op_channel,
837 force_freq ? " (forced)" : "");
839 dev = p2p_get_device(p2p, peer_addr);
840 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
841 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
842 "P2P: Cannot connect to unknown P2P Device " MACSTR,
847 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
848 if (!(dev->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
849 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
850 "P2P: Cannot connect to P2P Device " MACSTR
851 " that is in a group and is not discoverable",
855 if (dev->oper_freq <= 0) {
856 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
857 "P2P: Cannot connect to P2P Device " MACSTR
858 " with incomplete information",
864 * First, try to connect directly. If the peer does not
865 * acknowledge frames, assume it is sleeping and use device
866 * discoverability via the GO at that point.
870 dev->flags &= ~P2P_DEV_NOT_YET_READY;
871 dev->flags &= ~P2P_DEV_USER_REJECTED;
872 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
873 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
874 dev->go_neg_req_sent = 0;
875 dev->go_state = UNKNOWN_GO;
876 if (persistent_group)
877 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
879 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_GROUP;
880 p2p->go_intent = go_intent;
881 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
883 if (p2p->state != P2P_IDLE)
886 dev->wps_method = wps_method;
887 dev->status = P2P_SC_SUCCESS;
890 dev->flags |= P2P_DEV_FORCE_FREQ;
892 dev->flags &= ~P2P_DEV_FORCE_FREQ;
894 if (p2p->p2p_scan_running) {
895 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
896 "P2P: p2p_scan running - delay connect send");
897 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
898 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
901 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
903 return p2p_connect_send(p2p, dev);
907 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
908 enum p2p_wps_method wps_method,
909 int go_intent, const u8 *own_interface_addr,
910 unsigned int force_freq, int persistent_group)
912 struct p2p_device *dev;
914 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
915 "P2P: Request to authorize group negotiation - peer=" MACSTR
916 " GO Intent=%d Intended Interface Address=" MACSTR
917 " wps_method=%d persistent_group=%d",
918 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
919 wps_method, persistent_group);
922 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
923 &p2p->op_reg_class, &p2p->op_channel) <
925 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
926 "P2P: Unsupported frequency %u MHz",
930 p2p->channels.reg_classes = 1;
931 p2p->channels.reg_class[0].channels = 1;
932 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
933 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
935 p2p->op_reg_class = p2p->cfg->op_reg_class;
936 p2p->op_channel = p2p->cfg->op_channel;
937 os_memcpy(&p2p->channels, &p2p->cfg->channels,
938 sizeof(struct p2p_channels));
940 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
941 "P2P: Own preference for operation channel: "
942 "Regulatory Class %u Channel %u%s",
943 p2p->op_reg_class, p2p->op_channel,
944 force_freq ? " (forced)" : "");
946 dev = p2p_get_device(p2p, peer_addr);
948 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
949 "P2P: Cannot authorize unknown P2P Device " MACSTR,
954 dev->flags &= ~P2P_DEV_NOT_YET_READY;
955 dev->flags &= ~P2P_DEV_USER_REJECTED;
956 dev->go_neg_req_sent = 0;
957 dev->go_state = UNKNOWN_GO;
958 if (persistent_group)
959 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
961 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_GROUP;
962 p2p->go_intent = go_intent;
963 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
965 dev->wps_method = wps_method;
966 dev->status = P2P_SC_SUCCESS;
969 dev->flags |= P2P_DEV_FORCE_FREQ;
971 dev->flags &= ~P2P_DEV_FORCE_FREQ;
977 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
978 struct p2p_device *dev, struct p2p_message *msg)
980 os_get_time(&dev->last_seen);
982 if (msg->pri_dev_type)
983 os_memcpy(dev->pri_dev_type, msg->pri_dev_type,
984 sizeof(dev->pri_dev_type));
985 os_memcpy(dev->device_name, msg->device_name,
986 sizeof(dev->device_name));
987 dev->config_methods = msg->config_methods ? msg->config_methods :
988 msg->wps_config_methods;
989 if (msg->capability) {
990 dev->dev_capab = msg->capability[0];
991 dev->group_capab = msg->capability[1];
993 if (msg->listen_channel) {
995 freq = p2p_channel_to_freq((char *) msg->listen_channel,
996 msg->listen_channel[3],
997 msg->listen_channel[4]);
999 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1000 "P2P: Unknown peer Listen channel: "
1001 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1002 msg->listen_channel[0],
1003 msg->listen_channel[1],
1004 msg->listen_channel[2],
1005 msg->listen_channel[3],
1006 msg->listen_channel[4]);
1008 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1009 "peer " MACSTR " Listen channel: %u -> %u MHz",
1010 MAC2STR(dev->p2p_device_addr),
1011 dev->listen_freq, freq);
1012 dev->listen_freq = freq;
1015 if (msg->ext_listen_timing) {
1016 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
1017 dev->ext_listen_interval =
1018 WPA_GET_LE16(msg->ext_listen_timing + 2);
1021 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1022 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1023 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1024 "P2P: Completed device entry based on data from "
1025 "GO Negotiation Request");
1027 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1028 "P2P: Created device entry based on GO Neg Req: "
1029 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1031 MAC2STR(dev->p2p_device_addr), dev->dev_capab,
1032 dev->group_capab, dev->device_name, dev->listen_freq);
1035 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1037 if (dev->flags & P2P_DEV_USER_REJECTED) {
1038 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1039 "P2P: Do not report rejected device");
1043 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, dev->p2p_device_addr,
1044 dev->pri_dev_type, dev->device_name,
1045 dev->config_methods, dev->dev_capab,
1050 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1052 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1053 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1054 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1055 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1056 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1060 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1062 p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len);
1063 p2p_random(params->passphrase, 8);
1068 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1070 struct p2p_go_neg_results res;
1071 int go = peer->go_state == LOCAL_GO;
1072 struct p2p_channels intersection;
1076 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1077 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1078 "GO)", MAC2STR(peer->p2p_device_addr),
1079 go ? "local end" : "peer");
1081 os_memset(&res, 0, sizeof(res));
1083 os_memcpy(res.peer_device_addr, peer->p2p_device_addr, ETH_ALEN);
1084 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1085 res.wps_method = peer->wps_method;
1086 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
1087 res.persistent_group = 1;
1090 /* Setup AP mode for WPS provisioning */
1091 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1094 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1095 res.ssid_len = p2p->ssid_len;
1096 p2p_random(res.passphrase, 8);
1098 res.freq = peer->oper_freq;
1099 if (p2p->ssid_len) {
1100 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1101 res.ssid_len = p2p->ssid_len;
1105 p2p_channels_intersect(&p2p->channels, &peer->channels,
1108 for (i = 0; i < intersection.reg_classes; i++) {
1109 struct p2p_reg_class *c = &intersection.reg_class[i];
1110 if (freqs + 1 == P2P_MAX_CHANNELS)
1112 for (j = 0; j < c->channels; j++) {
1114 if (freqs + 1 == P2P_MAX_CHANNELS)
1116 freq = p2p_channel_to_freq(peer->country, c->reg_class,
1120 res.freq_list[freqs++] = freq;
1124 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1126 p2p_clear_timeout(p2p);
1127 peer->go_neg_req_sent = 0;
1128 peer->wps_method = WPS_NOT_READY;
1130 p2p_set_state(p2p, P2P_PROVISIONING);
1131 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1135 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1136 const u8 *data, size_t len, int rx_freq)
1138 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1139 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1140 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1146 case P2P_GO_NEG_REQ:
1147 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1149 case P2P_GO_NEG_RESP:
1150 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1152 case P2P_GO_NEG_CONF:
1153 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1155 case P2P_INVITATION_REQ:
1156 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1159 case P2P_INVITATION_RESP:
1160 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1162 case P2P_PROV_DISC_REQ:
1163 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1165 case P2P_PROV_DISC_RESP:
1166 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1168 case P2P_DEV_DISC_REQ:
1169 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1171 case P2P_DEV_DISC_RESP:
1172 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1175 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1176 "P2P: Unsupported P2P Public Action frame type %d",
1183 void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1184 const u8 *bssid, const u8 *data, size_t len,
1191 case WLAN_PA_VENDOR_SPECIFIC:
1196 if (WPA_GET_BE24(data) != OUI_WFA)
1204 if (*data != P2P_OUI_TYPE)
1207 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1209 case WLAN_PA_GAS_INITIAL_REQ:
1210 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1212 case WLAN_PA_GAS_INITIAL_RESP:
1213 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1215 case WLAN_PA_GAS_COMEBACK_REQ:
1216 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1218 case WLAN_PA_GAS_COMEBACK_RESP:
1219 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1225 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1226 const u8 *bssid, u8 category,
1227 const u8 *data, size_t len, int freq)
1229 if (category == WLAN_ACTION_PUBLIC) {
1230 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1234 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1240 if (WPA_GET_BE24(data) != OUI_WFA)
1245 if (*data != P2P_OUI_TYPE)
1250 /* P2P action frame */
1251 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1252 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1253 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1259 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1260 "P2P: Received P2P Action - Notice of Absence");
1263 case P2P_PRESENCE_REQ:
1264 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1266 case P2P_PRESENCE_RESP:
1267 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1269 case P2P_GO_DISC_REQ:
1270 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1273 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1274 "P2P: Received P2P Action - unknown type %u", data[0]);
1280 static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1282 struct p2p_data *p2p = eloop_ctx;
1283 if (p2p->go_neg_peer == NULL)
1285 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1286 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1287 p2p_connect_send(p2p, p2p->go_neg_peer);
1291 static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1293 struct p2p_data *p2p = eloop_ctx;
1294 if (p2p->invite_peer == NULL)
1296 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1297 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1301 static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1302 const u8 *ie, size_t ie_len)
1304 struct p2p_message msg;
1305 struct p2p_device *dev;
1307 os_memset(&msg, 0, sizeof(msg));
1308 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1310 p2p_parse_free(&msg);
1311 return; /* not a P2P probe */
1314 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1315 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1317 /* The Probe Request is not part of P2P Device Discovery. It is
1318 * not known whether the source address of the frame is the P2P
1319 * Device Address or P2P Interface Address. Do not add a new
1320 * peer entry based on this frames.
1322 p2p_parse_free(&msg);
1326 dev = p2p_get_device(p2p, addr);
1328 if (dev->country[0] == 0 && msg.listen_channel)
1329 os_memcpy(dev->country, msg.listen_channel, 3);
1330 p2p_parse_free(&msg);
1331 return; /* already known */
1334 dev = p2p_create_device(p2p, addr);
1336 p2p_parse_free(&msg);
1340 os_get_time(&dev->last_seen);
1341 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1343 if (msg.capability) {
1344 dev->dev_capab = msg.capability[0];
1345 dev->group_capab = msg.capability[1];
1348 if (msg.listen_channel) {
1349 os_memcpy(dev->country, msg.listen_channel, 3);
1350 dev->listen_freq = p2p_channel_to_freq(dev->country,
1351 msg.listen_channel[3],
1352 msg.listen_channel[4]);
1355 os_memcpy(dev->device_name, msg.device_name, sizeof(dev->device_name));
1357 if (msg.wps_pri_dev_type)
1358 os_memcpy(dev->pri_dev_type, msg.wps_pri_dev_type,
1359 sizeof(dev->pri_dev_type));
1361 p2p_parse_free(&msg);
1363 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1364 "P2P: Created device entry based on Probe Req: " MACSTR
1365 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1366 MAC2STR(dev->p2p_device_addr), dev->dev_capab,
1367 dev->group_capab, dev->device_name, dev->listen_freq);
1371 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1373 struct p2p_message *msg)
1375 struct p2p_device *dev;
1377 dev = p2p_get_device(p2p, addr);
1379 os_get_time(&dev->last_seen);
1380 return dev; /* already known */
1383 dev = p2p_create_device(p2p, addr);
1387 p2p_add_dev_info(p2p, addr, dev, msg);
1393 static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1395 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1397 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1398 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1399 WPA_GET_BE16(&req_dev_type[6]) == 0)
1400 return 1; /* Category match with wildcard OUI/sub-category */
1405 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1406 size_t num_req_dev_type)
1409 for (i = 0; i < num_req_dev_type; i++) {
1410 if (dev_type_match(dev_type, req_dev_type[i]))
1418 * p2p_match_dev_type - Match local device type with requested type
1419 * @p2p: P2P module context from p2p_init()
1420 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1421 * Returns: 1 on match, 0 on mismatch
1423 * This function can be used to match the Requested Device Type attribute in
1424 * WPS IE with the local device types for deciding whether to reply to a Probe
1427 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1429 struct wps_parse_attr attr;
1432 if (wps_parse_msg(wps, &attr))
1433 return 1; /* assume no Requested Device Type attributes */
1435 if (attr.num_req_dev_type == 0)
1436 return 1; /* no Requested Device Type attributes -> match */
1438 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1439 attr.num_req_dev_type))
1440 return 1; /* Own Primary Device Type matches */
1442 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1443 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1445 attr.num_req_dev_type))
1446 return 1; /* Own Secondary Device Type matches */
1448 /* No matching device type found */
1453 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1458 buf = wpabuf_alloc(1000);
1462 /* TODO: add more info into WPS IE; maybe get from WPS module? */
1463 p2p_build_wps_ie(p2p, buf, DEV_PW_DEFAULT, 1);
1466 len = p2p_buf_add_ie_hdr(buf);
1467 p2p_buf_add_capability(buf, p2p->dev_capab, 0);
1468 if (p2p->ext_listen_interval)
1469 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1470 p2p->ext_listen_interval);
1471 p2p_buf_add_device_info(buf, p2p, NULL);
1472 p2p_buf_update_ie_hdr(buf, len);
1478 static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *ie,
1481 struct ieee802_11_elems elems;
1483 struct ieee80211_mgmt *resp;
1487 if (!p2p->in_listen || !p2p->drv_in_listen) {
1488 /* not in Listen state - ignore Probe Request */
1492 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
1494 /* Ignore invalid Probe Request frames */
1498 if (elems.p2p == NULL) {
1499 /* not a P2P probe - ignore it */
1503 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
1504 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
1506 /* not using P2P Wildcard SSID - ignore */
1510 /* Check Requested Device Type match */
1511 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1512 if (wps && !p2p_match_dev_type(p2p, wps)) {
1514 /* No match with Requested Device Type */
1519 if (!p2p->cfg->send_probe_resp)
1520 return; /* Response generated elsewhere */
1522 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1523 "P2P: Reply to P2P Probe Request in Listen state");
1526 * We do not really have a specific BSS that this frame is advertising,
1527 * so build a frame that has some information in valid format. This is
1528 * really only used for discovery purposes, not to learn exact BSS
1531 ies = p2p_build_probe_resp_ies(p2p);
1535 buf = wpabuf_alloc(200 + wpabuf_len(ies));
1542 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
1544 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
1545 (WLAN_FC_STYPE_PROBE_RESP << 4));
1546 os_memcpy(resp->da, addr, ETH_ALEN);
1547 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
1548 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
1549 resp->u.probe_resp.beacon_int = host_to_le16(100);
1550 /* hardware or low-level driver will setup seq_ctrl and timestamp */
1551 resp->u.probe_resp.capab_info =
1552 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
1553 WLAN_CAPABILITY_PRIVACY |
1554 WLAN_CAPABILITY_SHORT_SLOT_TIME);
1556 wpabuf_put_u8(buf, WLAN_EID_SSID);
1557 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
1558 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1560 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
1561 wpabuf_put_u8(buf, 8);
1562 wpabuf_put_u8(buf, (60 / 5) | 0x80);
1563 wpabuf_put_u8(buf, 90 / 5);
1564 wpabuf_put_u8(buf, (120 / 5) | 0x80);
1565 wpabuf_put_u8(buf, 180 / 5);
1566 wpabuf_put_u8(buf, (240 / 5) | 0x80);
1567 wpabuf_put_u8(buf, 360 / 5);
1568 wpabuf_put_u8(buf, 480 / 5);
1569 wpabuf_put_u8(buf, 540 / 5);
1571 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
1572 wpabuf_put_u8(buf, 1);
1573 wpabuf_put_u8(buf, p2p->cfg->channel);
1575 wpabuf_put_buf(buf, ies);
1578 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
1584 int p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *ie,
1587 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
1589 p2p_reply_probe(p2p, addr, ie, ie_len);
1591 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
1593 os_memcmp(addr, p2p->go_neg_peer->p2p_device_addr, ETH_ALEN) == 0)
1595 /* Received a Probe Request from GO Negotiation peer */
1596 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1597 "P2P: Found GO Negotiation peer - try to start GO "
1598 "negotiation from timeout");
1599 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
1603 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
1605 os_memcmp(addr, p2p->invite_peer->p2p_device_addr, ETH_ALEN) == 0)
1607 /* Received a Probe Request from Invite peer */
1608 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1609 "P2P: Found Invite peer - try to start Invite from "
1611 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
1619 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
1620 u8 *buf, size_t len, struct wpabuf *p2p_ie)
1629 return 0; /* WLAN AP is not a P2P manager */
1632 * (Re)Association Request - P2P IE
1633 * P2P Capability attribute (shall be present)
1634 * P2P Interface attribute (present if concurrent device and
1635 * P2P Management is enabled)
1637 tmp = wpabuf_alloc(200);
1641 lpos = p2p_buf_add_ie_hdr(tmp);
1643 if (p2p->num_groups > 0) {
1644 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
1645 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1646 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
1648 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
1650 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
1651 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1652 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
1653 p2p_buf_add_p2p_interface(tmp, p2p);
1654 p2p_buf_update_ie_hdr(tmp, lpos);
1656 tmplen = wpabuf_len(tmp);
1660 os_memcpy(buf, wpabuf_head(tmp), tmplen);
1669 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
1670 size_t len, int p2p_group, struct wpabuf *p2p_ie)
1674 struct p2p_device *peer;
1679 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
1682 * (Re)Association Request - P2P IE
1683 * P2P Capability attribute (shall be present)
1684 * Extended Listen Timing (may be present)
1685 * P2P Device Info attribute (shall be present)
1687 tmp = wpabuf_alloc(200);
1691 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
1693 lpos = p2p_buf_add_ie_hdr(tmp);
1694 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
1695 if (p2p->ext_listen_interval)
1696 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
1697 p2p->ext_listen_interval);
1698 p2p_buf_add_device_info(tmp, p2p, peer);
1699 p2p_buf_update_ie_hdr(tmp, lpos);
1701 tmplen = wpabuf_len(tmp);
1705 os_memcpy(buf, wpabuf_head(tmp), tmplen);
1714 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
1716 struct wpabuf *p2p_ie;
1719 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
1723 ret = p2p_attr_text(p2p_ie, buf, end);
1724 wpabuf_free(p2p_ie);
1729 static void p2p_clear_go_neg(struct p2p_data *p2p)
1731 p2p->go_neg_peer = NULL;
1732 p2p_clear_timeout(p2p);
1733 p2p_set_state(p2p, P2P_IDLE);
1737 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
1739 if (p2p->go_neg_peer == NULL) {
1740 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1741 "P2P: No pending Group Formation - "
1742 "ignore WPS registration success notification");
1743 return; /* No pending Group Formation */
1746 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
1748 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1749 "P2P: Ignore WPS registration success notification "
1750 "for " MACSTR " (GO Negotiation peer " MACSTR ")",
1752 MAC2STR(p2p->go_neg_peer->intended_addr));
1753 return; /* Ignore unexpected peer address */
1756 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1757 "P2P: Group Formation completed successfully with " MACSTR,
1760 p2p_clear_go_neg(p2p);
1764 void p2p_group_formation_failed(struct p2p_data *p2p)
1766 if (p2p->go_neg_peer == NULL) {
1767 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1768 "P2P: No pending Group Formation - "
1769 "ignore group formation failure notification");
1770 return; /* No pending Group Formation */
1773 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1774 "P2P: Group Formation failed with " MACSTR,
1775 MAC2STR(p2p->go_neg_peer->intended_addr));
1777 p2p_clear_go_neg(p2p);
1781 struct p2p_data * p2p_init(const struct p2p_config *cfg)
1783 struct p2p_data *p2p;
1785 if (cfg->max_peers < 1)
1788 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
1791 p2p->cfg = (struct p2p_config *) (p2p + 1);
1792 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
1794 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
1796 p2p->min_disc_int = 1;
1797 p2p->max_disc_int = 3;
1799 os_get_random(&p2p->next_tie_breaker, 1);
1800 p2p->next_tie_breaker &= 0x01;
1801 if (cfg->sd_request)
1802 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
1803 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
1804 if (cfg->concurrent_operations)
1805 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
1806 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
1808 dl_list_init(&p2p->devices);
1810 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
1811 p2p_expiration_timeout, p2p, NULL);
1817 void p2p_deinit(struct p2p_data *p2p)
1819 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
1820 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
1821 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1823 os_free(p2p->cfg->dev_name);
1824 os_free(p2p->groups);
1825 wpabuf_free(p2p->sd_resp);
1830 void p2p_flush(struct p2p_data *p2p)
1832 struct p2p_device *dev, *prev;
1833 p2p_clear_timeout(p2p);
1834 p2p_set_state(p2p, P2P_IDLE);
1835 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1836 p2p->go_neg_peer = NULL;
1837 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1838 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
1840 dl_list_del(&dev->list);
1841 p2p_device_free(p2p, dev);
1843 p2p_free_sd_queries(p2p);
1847 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
1849 os_free(p2p->cfg->dev_name);
1851 p2p->cfg->dev_name = os_strdup(dev_name);
1852 if (p2p->cfg->dev_name == NULL)
1855 p2p->cfg->dev_name = NULL;
1860 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
1862 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
1867 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
1868 size_t num_dev_types)
1870 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
1871 num_dev_types = P2P_SEC_DEVICE_TYPES;
1872 p2p->cfg->num_sec_dev_types = num_dev_types;
1873 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
1878 int p2p_set_country(struct p2p_data *p2p, const char *country)
1880 os_memcpy(p2p->cfg->country, country, 3);
1885 void p2p_continue_find(struct p2p_data *p2p)
1887 struct p2p_device *dev;
1888 p2p_set_state(p2p, P2P_SEARCH);
1889 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
1890 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
1891 if (p2p_start_sd(p2p, dev) == 0)
1895 } else if (dev->req_config_methods) {
1896 if (p2p_send_prov_disc_req(p2p, dev, 0) == 0)
1901 p2p_listen_in_find(p2p);
1905 static void p2p_sd_cb(struct p2p_data *p2p, int success)
1907 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1908 "P2P: Service Discovery Query TX callback: success=%d",
1910 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1914 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
1915 p2p->sd_peer = NULL;
1917 p2p_continue_find(p2p);
1921 if (p2p->sd_peer == NULL) {
1922 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1923 "P2P: No SD peer entry known");
1924 p2p_continue_find(p2p);
1928 /* Wait for response from the peer */
1929 p2p_set_state(p2p, P2P_SD_DURING_FIND);
1930 p2p_set_timeout(p2p, 0, 200000);
1934 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
1936 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1937 "P2P: Provision Discovery Request TX callback: success=%d",
1939 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
1942 if (p2p->state != P2P_IDLE)
1943 p2p_continue_find(p2p);
1947 /* Wait for response from the peer */
1948 if (p2p->state == P2P_SEARCH)
1949 p2p_set_state(p2p, P2P_PD_DURING_FIND);
1950 p2p_set_timeout(p2p, 0, 200000);
1954 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
1955 int level, const u8 *ies, size_t ies_len)
1957 p2p_add_device(p2p, bssid, freq, level, ies, ies_len);
1959 if (p2p->go_neg_peer && p2p->state == P2P_SEARCH &&
1960 os_memcmp(p2p->go_neg_peer->p2p_device_addr, bssid, ETH_ALEN) == 0)
1962 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1963 "P2P: Found GO Negotiation peer - try to start GO "
1965 p2p_connect_send(p2p, p2p->go_neg_peer);
1973 void p2p_scan_res_handled(struct p2p_data *p2p)
1975 if (!p2p->p2p_scan_running) {
1976 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
1977 "running, but scan results received");
1979 p2p->p2p_scan_running = 0;
1980 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1982 if (p2p_run_after_scan(p2p))
1984 if (p2p->state == P2P_SEARCH)
1985 p2p_continue_find(p2p);
1989 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies)
1991 u8 *len = p2p_buf_add_ie_hdr(ies);
1992 p2p_buf_add_capability(ies, p2p->dev_capab, 0);
1993 if (p2p->cfg->reg_class && p2p->cfg->channel)
1994 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
1995 p2p->cfg->reg_class,
1997 if (p2p->ext_listen_interval)
1998 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
1999 p2p->ext_listen_interval);
2000 /* TODO: p2p_buf_add_operating_channel() if GO */
2001 p2p_buf_update_ie_hdr(ies, len);
2005 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2007 return p2p_attr_text(p2p_ie, buf, end);
2011 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2013 struct p2p_device *dev = p2p->go_neg_peer;
2015 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2016 "P2P: GO Negotiation Request TX callback: success=%d",
2020 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2021 "P2P: No pending GO Negotiation");
2026 dev->go_neg_req_sent++;
2027 if (dev->flags & P2P_DEV_USER_REJECTED) {
2028 p2p_set_state(p2p, P2P_IDLE);
2034 (dev->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2035 !is_zero_ether_addr(dev->member_in_go_dev)) {
2036 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2037 "P2P: Peer " MACSTR " did not acknowledge request - "
2038 "try to use device discoverability through its GO",
2039 MAC2STR(dev->p2p_device_addr));
2040 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2041 p2p_send_dev_disc_req(p2p, dev);
2046 * Use P2P find, if needed, to find the other device from its listen
2049 p2p_set_state(p2p, P2P_CONNECT);
2050 p2p_set_timeout(p2p, 0, 100000);
2054 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2056 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2057 "P2P: GO Negotiation Response TX callback: success=%d",
2059 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2060 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2061 "P2P: Ignore TX callback event - GO Negotiation is "
2062 "not running anymore");
2065 p2p_set_state(p2p, P2P_CONNECT);
2066 p2p_set_timeout(p2p, 0, 100000);
2070 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2072 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2073 "P2P: GO Negotiation Response (failure) TX callback: "
2074 "success=%d", success);
2075 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2076 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2077 p2p->go_neg_peer->status);
2082 static void p2p_go_neg_conf_cb(struct p2p_data *p2p, int success)
2084 struct p2p_device *dev;
2086 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2087 "P2P: GO Negotiation Confirm TX callback: success=%d",
2089 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2092 * It looks like the TX status for GO Negotiation Confirm is
2093 * often showing failure even when the peer has actually
2094 * received the frame. Since the peer may change channels
2095 * immediately after having received the frame, we may not see
2096 * an Ack for retries, so just dropping a single frame may
2097 * trigger this. To allow the group formation to succeed if the
2098 * peer did indeed receive the frame, continue regardless of
2101 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2102 "P2P: Assume GO Negotiation Confirm TX was actually "
2103 "received by the peer even though Ack was not "
2107 dev = p2p->go_neg_peer;
2111 p2p_go_complete(p2p, dev);
2115 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
2116 const u8 *src, const u8 *bssid, int success)
2118 enum p2p_pending_action_state state;
2120 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2121 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
2122 " src=" MACSTR " bssid=" MACSTR " success=%d",
2123 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
2124 MAC2STR(bssid), success);
2125 state = p2p->pending_action_state;
2126 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2128 case P2P_NO_PENDING_ACTION:
2130 case P2P_PENDING_GO_NEG_REQUEST:
2131 p2p_go_neg_req_cb(p2p, success);
2133 case P2P_PENDING_GO_NEG_RESPONSE:
2134 p2p_go_neg_resp_cb(p2p, success);
2136 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
2137 p2p_go_neg_resp_failure_cb(p2p, success);
2139 case P2P_PENDING_GO_NEG_CONFIRM:
2140 p2p_go_neg_conf_cb(p2p, success);
2142 case P2P_PENDING_SD:
2143 p2p_sd_cb(p2p, success);
2145 case P2P_PENDING_PD:
2146 p2p_prov_disc_cb(p2p, success);
2148 case P2P_PENDING_INVITATION_REQUEST:
2149 p2p_invitation_req_cb(p2p, success);
2151 case P2P_PENDING_INVITATION_RESPONSE:
2152 p2p_invitation_resp_cb(p2p, success);
2154 case P2P_PENDING_DEV_DISC_REQUEST:
2155 p2p_dev_disc_req_cb(p2p, success);
2157 case P2P_PENDING_DEV_DISC_RESPONSE:
2158 p2p_dev_disc_resp_cb(p2p, success);
2160 case P2P_PENDING_GO_DISC_REQ:
2161 p2p_go_disc_req_cb(p2p, success);
2167 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
2168 unsigned int duration)
2170 if (freq == p2p->pending_client_disc_freq) {
2171 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2172 "P2P: Client discoverability remain-awake completed");
2173 p2p->pending_client_disc_freq = 0;
2177 if (freq != p2p->pending_listen_freq) {
2178 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2179 "P2P: Unexpected listen callback for freq=%u "
2180 "duration=%u (pending_listen_freq=%u)",
2181 freq, duration, p2p->pending_listen_freq);
2185 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2186 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
2188 p2p->pending_listen_sec, p2p->pending_listen_usec,
2189 p2p->pending_listen_freq);
2191 p2p->drv_in_listen = 1;
2192 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
2194 * Add 20 msec extra wait to avoid race condition with driver
2195 * remain-on-channel end event, i.e., give driver more time to
2196 * complete the operation before our timeout expires.
2198 p2p_set_timeout(p2p, p2p->pending_listen_sec,
2199 p2p->pending_listen_usec + 20000);
2202 p2p->pending_listen_freq = 0;
2206 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
2208 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
2209 "state (freq=%u)", freq);
2210 p2p->drv_in_listen = 0;
2212 return 0; /* Internal timeout will trigger the next step */
2214 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
2215 p2p_set_state(p2p, P2P_CONNECT);
2216 p2p_connect_send(p2p, p2p->go_neg_peer);
2218 } else if (p2p->state == P2P_SEARCH) {
2227 static void p2p_timeout_connect(struct p2p_data *p2p)
2229 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2230 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
2231 p2p_listen_in_find(p2p);
2235 static void p2p_timeout_connect_listen(struct p2p_data *p2p)
2237 if (p2p->go_neg_peer) {
2238 if (p2p->drv_in_listen) {
2239 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
2240 "still in Listen state; wait for it to "
2244 p2p_set_state(p2p, P2P_CONNECT);
2245 p2p_connect_send(p2p, p2p->go_neg_peer);
2247 p2p_set_state(p2p, P2P_IDLE);
2251 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
2254 * TODO: could remain constantly in Listen state for some time if there
2255 * are no other concurrent uses for the radio. For now, go to listen
2256 * state once per second to give other uses a chance to use the radio.
2258 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
2259 p2p_set_timeout(p2p, 1, 0);
2263 static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
2265 struct p2p_device *dev = p2p->go_neg_peer;
2268 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2269 "P2P: Unknown GO Neg peer - stop GO Neg wait");
2274 if (dev->wait_count >= 120) {
2275 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2276 "P2P: Timeout on waiting peer to become ready for GO "
2278 p2p_go_neg_failed(p2p, dev, -1);
2282 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2283 "P2P: Go to Listen state while waiting for the peer to become "
2284 "ready for GO Negotiation");
2285 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
2286 p2p_listen_in_find(p2p);
2290 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
2292 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2293 "P2P: Service Discovery Query timeout");
2295 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2296 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2297 p2p->sd_peer = NULL;
2299 p2p_continue_find(p2p);
2303 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
2305 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2306 "P2P: Provision Discovery Request timeout");
2307 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2308 p2p_continue_find(p2p);
2312 static void p2p_timeout_invite(struct p2p_data *p2p)
2314 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2315 p2p_set_state(p2p, P2P_INVITE_LISTEN);
2316 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
2318 * Better remain on operating channel instead of listen channel
2319 * when running a group.
2321 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
2322 "active GO role - wait on operating channel");
2323 p2p_set_timeout(p2p, 0, 100000);
2326 p2p_listen_in_find(p2p);
2330 static void p2p_timeout_invite_listen(struct p2p_data *p2p)
2332 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
2333 p2p_set_state(p2p, P2P_INVITE);
2334 p2p_invite_send(p2p, p2p->invite_peer,
2335 p2p->invite_go_dev_addr);
2337 if (p2p->invite_peer) {
2338 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2339 "P2P: Invitation Request retry limit reached");
2340 if (p2p->cfg->invitation_result)
2341 p2p->cfg->invitation_result(
2342 p2p->cfg->cb_ctx, -1, NULL);
2344 p2p_set_state(p2p, P2P_IDLE);
2349 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
2351 struct p2p_data *p2p = eloop_ctx;
2353 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
2354 p2p_state_txt(p2p->state));
2358 switch (p2p->state) {
2365 p2p_timeout_connect(p2p);
2367 case P2P_CONNECT_LISTEN:
2368 p2p_timeout_connect_listen(p2p);
2372 case P2P_LISTEN_ONLY:
2373 if (p2p->ext_listen_only) {
2374 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2375 "P2P: Extended Listen Timing - Listen State "
2377 p2p->ext_listen_only = 0;
2378 p2p_set_state(p2p, P2P_IDLE);
2381 case P2P_WAIT_PEER_CONNECT:
2382 p2p_timeout_wait_peer_connect(p2p);
2384 case P2P_WAIT_PEER_IDLE:
2385 p2p_timeout_wait_peer_idle(p2p);
2387 case P2P_SD_DURING_FIND:
2388 p2p_timeout_sd_during_find(p2p);
2390 case P2P_PROVISIONING:
2392 case P2P_PD_DURING_FIND:
2393 p2p_timeout_prov_disc_during_find(p2p);
2396 p2p_timeout_invite(p2p);
2398 case P2P_INVITE_LISTEN:
2399 p2p_timeout_invite_listen(p2p);
2405 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
2407 struct p2p_device *dev;
2409 dev = p2p_get_device(p2p, peer_addr);
2410 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
2411 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
2413 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
2414 " unknown", MAC2STR(peer_addr));
2417 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
2418 dev->flags |= P2P_DEV_USER_REJECTED;
2423 static const char * p2p_wps_method_text(enum p2p_wps_method method)
2430 case WPS_PIN_DISPLAY:
2432 case WPS_PIN_KEYPAD:
2442 static const char * p2p_go_state_text(enum p2p_go_state go_state)
2457 int p2p_get_peer_info(struct p2p_data *p2p, const u8 *addr, int next,
2458 char *buf, size_t buflen)
2460 struct p2p_device *dev;
2464 char devtype[WPS_DEV_TYPE_BUFSIZE];
2467 dev = p2p_get_device(p2p, addr);
2469 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
2472 dev = dl_list_first(&dev->list, struct p2p_device, list);
2473 if (&dev->list == &p2p->devices)
2483 res = os_snprintf(pos, end - pos, MACSTR "\n",
2484 MAC2STR(dev->p2p_device_addr));
2485 if (res < 0 || res >= end - pos)
2490 res = os_snprintf(pos, end - pos,
2495 "interface_addr=" MACSTR "\n"
2496 "member_in_go_dev=" MACSTR "\n"
2497 "member_in_go_iface=" MACSTR "\n"
2500 "config_methods=0x%x\n"
2502 "group_capab=0x%x\n"
2503 "go_neg_req_sent=%d\n"
2506 "intended_addr=" MACSTR "\n"
2509 "req_config_methods=0x%x\n"
2510 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
2513 "invitation_reqs=%u\n",
2514 (int) (now.sec - dev->last_seen.sec),
2517 p2p_wps_method_text(dev->wps_method),
2518 MAC2STR(dev->interface_addr),
2519 MAC2STR(dev->member_in_go_dev),
2520 MAC2STR(dev->member_in_go_iface),
2521 wps_dev_type_bin2str(dev->pri_dev_type,
2522 devtype, sizeof(devtype)),
2524 dev->config_methods,
2527 dev->go_neg_req_sent,
2528 p2p_go_state_text(dev->go_state),
2530 MAC2STR(dev->intended_addr),
2531 dev->country[0] ? dev->country[0] : '_',
2532 dev->country[1] ? dev->country[1] : '_',
2534 dev->req_config_methods,
2535 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
2536 "[PROBE_REQ_ONLY]" : "",
2537 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
2538 dev->flags & P2P_DEV_NOT_YET_READY ?
2539 "[NOT_YET_READY]" : "",
2540 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
2541 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
2543 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
2544 "[PD_PEER_DISPLAY]" : "",
2545 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
2546 "[PD_PEER_KEYPAD]" : "",
2547 dev->flags & P2P_DEV_USER_REJECTED ?
2548 "[USER_REJECTED]" : "",
2549 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
2550 "[PEER_WAITING_RESPONSE]" : "",
2551 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
2552 "[PREFER_PERSISTENT_GROUP]" : "",
2553 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
2554 "[WAIT_GO_NEG_RESPONSE]" : "",
2555 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
2556 "[WAIT_GO_NEG_CONFIRM]" : "",
2557 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
2558 "[GROUP_CLIENT_ONLY]" : "",
2559 dev->flags & P2P_DEV_FORCE_FREQ ?
2563 dev->invitation_reqs);
2564 if (res < 0 || res >= end - pos)
2568 if (dev->ext_listen_period) {
2569 res = os_snprintf(pos, end - pos,
2570 "ext_listen_period=%u\n"
2571 "ext_listen_interval=%u\n",
2572 dev->ext_listen_period,
2573 dev->ext_listen_interval);
2574 if (res < 0 || res >= end - pos)
2579 if (dev->oper_ssid_len) {
2580 res = os_snprintf(pos, end - pos,
2582 wpa_ssid_txt(dev->oper_ssid,
2583 dev->oper_ssid_len));
2584 if (res < 0 || res >= end - pos)
2593 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
2596 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
2597 "discoverability enabled");
2598 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2600 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
2601 "discoverability disabled");
2602 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2607 static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
2608 u32 duration2, u32 interval2)
2611 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
2614 req = wpabuf_alloc(100);
2618 if (duration1 || interval1) {
2619 os_memset(&desc1, 0, sizeof(desc1));
2620 desc1.count_type = 1;
2621 desc1.duration = duration1;
2622 desc1.interval = interval1;
2625 if (duration2 || interval2) {
2626 os_memset(&desc2, 0, sizeof(desc2));
2627 desc2.count_type = 2;
2628 desc2.duration = duration2;
2629 desc2.interval = interval2;
2634 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
2635 len = p2p_buf_add_ie_hdr(req);
2636 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
2637 p2p_buf_update_ie_hdr(req, len);
2643 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
2644 const u8 *own_interface_addr, unsigned int freq,
2645 u32 duration1, u32 interval1, u32 duration2,
2650 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
2651 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
2652 "int1=%u dur2=%u int2=%u",
2653 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
2654 freq, duration1, interval1, duration2, interval2);
2656 req = p2p_build_presence_req(duration1, interval1, duration2,
2661 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2662 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, go_interface_addr,
2665 wpabuf_head(req), wpabuf_len(req), 200) < 0)
2667 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2668 "P2P: Failed to send Action frame");
2676 static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
2677 size_t noa_len, u8 dialog_token)
2679 struct wpabuf *resp;
2682 resp = wpabuf_alloc(100 + noa_len);
2686 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
2687 len = p2p_buf_add_ie_hdr(resp);
2688 p2p_buf_add_status(resp, status);
2690 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
2691 wpabuf_put_le16(resp, noa_len);
2692 wpabuf_put_data(resp, noa, noa_len);
2694 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
2695 p2p_buf_update_ie_hdr(resp, len);
2701 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
2702 const u8 *sa, const u8 *data, size_t len,
2705 struct p2p_message msg;
2707 struct wpabuf *resp;
2709 struct p2p_group *group = NULL;
2714 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2715 "P2P: Received P2P Action - P2P Presence Request");
2717 for (g = 0; g < p2p->num_groups; g++) {
2718 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
2720 group = p2p->groups[g];
2724 if (group == NULL) {
2725 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2726 "P2P: Ignore P2P Presence Request for unknown group "
2727 MACSTR, MAC2STR(da));
2731 if (p2p_parse(data, len, &msg) < 0) {
2732 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2733 "P2P: Failed to parse P2P Presence Request");
2734 status = P2P_SC_FAIL_INVALID_PARAMS;
2739 if (msg.noa == NULL) {
2740 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2741 "P2P: No NoA attribute in P2P Presence Request");
2742 status = P2P_SC_FAIL_INVALID_PARAMS;
2746 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
2749 if (p2p->cfg->get_noa)
2750 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
2754 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
2755 noa_len > 0 ? noa_len : 0,
2758 p2p_parse_free(&msg);
2762 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2763 if (p2p->cfg->send_action(p2p->cfg->cb_ctx, rx_freq, sa, da, da,
2764 wpabuf_head(resp), wpabuf_len(resp), 200) <
2766 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2767 "P2P: Failed to send Action frame");
2773 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
2774 const u8 *sa, const u8 *data, size_t len)
2776 struct p2p_message msg;
2778 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2779 "P2P: Received P2P Action - P2P Presence Response");
2781 if (p2p_parse(data, len, &msg) < 0) {
2782 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2783 "P2P: Failed to parse P2P Presence Response");
2787 if (msg.status == NULL || msg.noa == NULL) {
2788 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2789 "P2P: No Status or NoA attribute in P2P Presence "
2791 p2p_parse_free(&msg);
2796 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2797 "P2P: P2P Presence Request was rejected: status %u",
2799 p2p_parse_free(&msg);
2803 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2804 "P2P: P2P Presence Request was accepted");
2805 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
2806 msg.noa, msg.noa_len);
2807 /* TODO: process NoA */
2808 p2p_parse_free(&msg);
2812 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
2814 struct p2p_data *p2p = eloop_ctx;
2816 if (p2p->ext_listen_interval) {
2817 /* Schedule next extended listen timeout */
2818 eloop_register_timeout(p2p->ext_listen_interval_sec,
2819 p2p->ext_listen_interval_usec,
2820 p2p_ext_listen_timeout, p2p, NULL);
2823 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
2825 * This should not really happen, but it looks like the Listen
2826 * command may fail is something else (e.g., a scan) was
2827 * running at an inconvenient time. As a workaround, allow new
2828 * Extended Listen operation to be started.
2830 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
2831 "Extended Listen operation had not been completed - "
2833 p2p->ext_listen_only = 0;
2834 p2p_set_state(p2p, P2P_IDLE);
2837 if (p2p->state != P2P_IDLE) {
2838 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
2839 "Listen timeout in active state (%s)",
2840 p2p_state_txt(p2p->state));
2844 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
2845 p2p->ext_listen_only = 1;
2846 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
2847 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
2848 "Listen state for Extended Listen Timing");
2849 p2p->ext_listen_only = 0;
2854 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
2855 unsigned int interval)
2857 if (period > 65535 || interval > 65535 || period > interval ||
2858 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
2859 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2860 "P2P: Invalid Extended Listen Timing request: "
2861 "period=%u interval=%u", period, interval);
2865 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2867 if (interval == 0) {
2868 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2869 "P2P: Disabling Extended Listen Timing");
2870 p2p->ext_listen_period = 0;
2871 p2p->ext_listen_interval = 0;
2875 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2876 "P2P: Enabling Extended Listen Timing: period %u msec, "
2877 "interval %u msec", period, interval);
2878 p2p->ext_listen_period = period;
2879 p2p->ext_listen_interval = interval;
2880 p2p->ext_listen_interval_sec = interval / 1000;
2881 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
2883 eloop_register_timeout(p2p->ext_listen_interval_sec,
2884 p2p->ext_listen_interval_usec,
2885 p2p_ext_listen_timeout, p2p, NULL);
2891 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
2892 const u8 *ie, size_t ie_len)
2894 struct p2p_message msg;
2896 if (bssid == NULL || ie == NULL)
2899 os_memset(&msg, 0, sizeof(msg));
2900 if (p2p_parse_ies(ie, ie_len, &msg))
2902 if (msg.minor_reason_code == NULL)
2905 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
2906 "P2P: Deauthentication notification BSSID " MACSTR
2907 " reason_code=%u minor_reason_code=%u",
2908 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
2910 p2p_parse_free(&msg);
2914 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
2915 const u8 *ie, size_t ie_len)
2917 struct p2p_message msg;
2919 if (bssid == NULL || ie == NULL)
2922 os_memset(&msg, 0, sizeof(msg));
2923 if (p2p_parse_ies(ie, ie_len, &msg))
2925 if (msg.minor_reason_code == NULL)
2928 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
2929 "P2P: Disassociation notification BSSID " MACSTR
2930 " reason_code=%u minor_reason_code=%u",
2931 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
2933 p2p_parse_free(&msg);
2937 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
2940 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
2941 "Device operations enabled");
2942 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
2944 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
2945 "Device operations disabled");
2946 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
2951 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
2953 if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
2956 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
2957 "reg_class %u channel %u", reg_class, channel);
2958 p2p->cfg->reg_class = reg_class;
2959 p2p->cfg->channel = channel;
2965 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
2967 wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
2968 if (postfix == NULL) {
2969 p2p->cfg->ssid_postfix_len = 0;
2972 if (len > sizeof(p2p->cfg->ssid_postfix))
2974 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
2975 p2p->cfg->ssid_postfix_len = len;
2980 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
2983 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
2984 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
2986 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
2991 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
2994 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
2997 os_memcpy(dev_addr, dev->p2p_device_addr, ETH_ALEN);
3002 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
3004 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
3005 if (is_zero_ether_addr(p2p->peer_filter))
3006 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
3009 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
3010 "filter for " MACSTR, MAC2STR(p2p->peer_filter));
3014 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
3016 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
3017 enabled ? "enabled" : "disabled");
3018 if (p2p->cross_connect == enabled)
3020 p2p->cross_connect = enabled;
3021 /* TODO: may need to tear down any action group where we are GO(?) */
3025 int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
3027 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3030 if (dev->oper_freq <= 0)
3032 return dev->oper_freq;