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 if (p2p->after_scan_tx) {
696 /* TODO: schedule p2p_run_after_scan to be called from TX
697 * status callback(?) */
698 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
699 "Action frame at p2p_scan completion");
700 ret = p2p->cfg->send_action(p2p->cfg->cb_ctx,
701 p2p->after_scan_tx->freq,
702 p2p->after_scan_tx->dst,
703 p2p->after_scan_tx->src,
704 p2p->after_scan_tx->bssid,
705 (u8 *) (p2p->after_scan_tx + 1),
706 p2p->after_scan_tx->len,
707 p2p->after_scan_tx->wait_time);
708 os_free(p2p->after_scan_tx);
709 p2p->after_scan_tx = NULL;
713 op = p2p->start_after_scan;
714 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
716 case P2P_AFTER_SCAN_NOTHING:
718 case P2P_AFTER_SCAN_LISTEN:
719 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
720 "requested Listen state");
721 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
722 p2p->pending_listen_usec / 1000);
724 case P2P_AFTER_SCAN_CONNECT:
725 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
726 "requested connect with " MACSTR,
727 MAC2STR(p2p->after_scan_peer));
728 dev = p2p_get_device(p2p, p2p->after_scan_peer);
730 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
734 p2p_connect_send(p2p, dev);
742 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
744 struct p2p_data *p2p = eloop_ctx;
746 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
747 "(running=%d)", p2p->p2p_scan_running);
748 running = p2p->p2p_scan_running;
749 /* Make sure we recover from missed scan results callback */
750 p2p->p2p_scan_running = 0;
753 p2p_run_after_scan(p2p);
757 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
758 enum p2p_discovery_type type)
762 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
764 if (p2p->p2p_scan_running) {
765 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
768 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
769 p2p_clear_timeout(p2p);
770 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
771 p2p->find_type = type;
772 p2p_device_clear_reported(p2p);
773 p2p_set_state(p2p, P2P_SEARCH);
774 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
776 eloop_register_timeout(timeout, 0, p2p_find_timeout,
779 case P2P_FIND_START_WITH_FULL:
780 case P2P_FIND_PROGRESSIVE:
781 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0);
783 case P2P_FIND_ONLY_SOCIAL:
784 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0);
791 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
792 p2p->p2p_scan_running = 1;
793 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
794 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
797 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
805 void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
807 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
808 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
809 p2p_clear_timeout(p2p);
810 p2p_set_state(p2p, P2P_IDLE);
811 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
812 p2p->go_neg_peer = NULL;
814 p2p->invite_peer = NULL;
815 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
816 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
817 "since we are on correct channel for response");
820 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
824 void p2p_stop_find(struct p2p_data *p2p)
826 p2p_stop_find_for_freq(p2p, 0);
830 static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
833 u8 op_reg_class, op_channel;
834 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
835 &op_reg_class, &op_channel) < 0) {
836 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
837 "P2P: Unsupported frequency %u MHz",
841 if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
843 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
844 "P2P: Frequency %u MHz (oper_class %u "
845 "channel %u) not allowed for P2P",
846 force_freq, op_reg_class, op_channel);
849 p2p->op_reg_class = op_reg_class;
850 p2p->op_channel = op_channel;
851 p2p->channels.reg_classes = 1;
852 p2p->channels.reg_class[0].channels = 1;
853 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
854 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
856 u8 op_reg_class, op_channel;
858 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
859 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
860 p2p_freq_to_channel(p2p->cfg->country,
861 p2p->best_freq_overall,
862 &op_reg_class, &op_channel) == 0) {
863 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
864 "P2P: Select best overall channel as "
865 "operating channel preference");
866 p2p->op_reg_class = op_reg_class;
867 p2p->op_channel = op_channel;
868 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
869 p2p_supported_freq(p2p, p2p->best_freq_5) &&
870 p2p_freq_to_channel(p2p->cfg->country,
872 &op_reg_class, &op_channel) ==
874 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
875 "P2P: Select best 5 GHz channel as "
876 "operating channel preference");
877 p2p->op_reg_class = op_reg_class;
878 p2p->op_channel = op_channel;
879 } else if (!p2p->cfg->cfg_op_channel &&
880 p2p->best_freq_24 > 0 &&
881 p2p_supported_freq(p2p, p2p->best_freq_24) &&
882 p2p_freq_to_channel(p2p->cfg->country,
884 &op_reg_class, &op_channel) ==
886 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
887 "P2P: Select best 2.4 GHz channel as "
888 "operating channel preference");
889 p2p->op_reg_class = op_reg_class;
890 p2p->op_channel = op_channel;
892 p2p->op_reg_class = p2p->cfg->op_reg_class;
893 p2p->op_channel = p2p->cfg->op_channel;
896 os_memcpy(&p2p->channels, &p2p->cfg->channels,
897 sizeof(struct p2p_channels));
899 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
900 "P2P: Own preference for operation channel: "
901 "Operating Class %u Channel %u%s",
902 p2p->op_reg_class, p2p->op_channel,
903 force_freq ? " (forced)" : "");
909 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
910 enum p2p_wps_method wps_method,
911 int go_intent, const u8 *own_interface_addr,
912 unsigned int force_freq, int persistent_group)
914 struct p2p_device *dev;
916 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
917 "P2P: Request to start group negotiation - peer=" MACSTR
918 " GO Intent=%d Intended Interface Address=" MACSTR
919 " wps_method=%d persistent_group=%d",
920 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
921 wps_method, persistent_group);
923 if (p2p_prepare_channel(p2p, force_freq) < 0)
926 dev = p2p_get_device(p2p, peer_addr);
927 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
928 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
929 "P2P: Cannot connect to unknown P2P Device " MACSTR,
934 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
935 if (!(dev->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
936 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
937 "P2P: Cannot connect to P2P Device " MACSTR
938 " that is in a group and is not discoverable",
942 if (dev->oper_freq <= 0) {
943 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
944 "P2P: Cannot connect to P2P Device " MACSTR
945 " with incomplete information",
951 * First, try to connect directly. If the peer does not
952 * acknowledge frames, assume it is sleeping and use device
953 * discoverability via the GO at that point.
957 dev->flags &= ~P2P_DEV_NOT_YET_READY;
958 dev->flags &= ~P2P_DEV_USER_REJECTED;
959 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
960 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
961 dev->connect_reqs = 0;
962 dev->go_neg_req_sent = 0;
963 dev->go_state = UNKNOWN_GO;
964 if (persistent_group)
965 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
967 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_GROUP;
968 p2p->go_intent = go_intent;
969 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
971 if (p2p->state != P2P_IDLE)
974 if (p2p->after_scan_tx) {
976 * We need to drop the pending frame to avoid issues with the
977 * new GO Negotiation, e.g., when the pending frame was from a
978 * previous attempt at starting a GO Negotiation.
980 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
981 "previous pending Action frame TX that was waiting "
982 "for p2p_scan completion");
983 os_free(p2p->after_scan_tx);
984 p2p->after_scan_tx = NULL;
987 dev->wps_method = wps_method;
988 dev->status = P2P_SC_SUCCESS;
991 dev->flags |= P2P_DEV_FORCE_FREQ;
993 dev->flags &= ~P2P_DEV_FORCE_FREQ;
995 if (p2p->p2p_scan_running) {
996 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
997 "P2P: p2p_scan running - delay connect send");
998 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
999 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1002 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1004 return p2p_connect_send(p2p, dev);
1008 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1009 enum p2p_wps_method wps_method,
1010 int go_intent, const u8 *own_interface_addr,
1011 unsigned int force_freq, int persistent_group)
1013 struct p2p_device *dev;
1015 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1016 "P2P: Request to authorize group negotiation - peer=" MACSTR
1017 " GO Intent=%d Intended Interface Address=" MACSTR
1018 " wps_method=%d persistent_group=%d",
1019 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1020 wps_method, persistent_group);
1022 if (p2p_prepare_channel(p2p, force_freq) < 0)
1025 dev = p2p_get_device(p2p, peer_addr);
1027 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1028 "P2P: Cannot authorize unknown P2P Device " MACSTR,
1029 MAC2STR(peer_addr));
1033 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1034 dev->flags &= ~P2P_DEV_USER_REJECTED;
1035 dev->go_neg_req_sent = 0;
1036 dev->go_state = UNKNOWN_GO;
1037 if (persistent_group)
1038 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1040 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_GROUP;
1041 p2p->go_intent = go_intent;
1042 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1044 dev->wps_method = wps_method;
1045 dev->status = P2P_SC_SUCCESS;
1048 dev->flags |= P2P_DEV_FORCE_FREQ;
1050 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1056 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1057 struct p2p_device *dev, struct p2p_message *msg)
1059 os_get_time(&dev->last_seen);
1061 if (msg->pri_dev_type)
1062 os_memcpy(dev->pri_dev_type, msg->pri_dev_type,
1063 sizeof(dev->pri_dev_type));
1064 os_memcpy(dev->device_name, msg->device_name,
1065 sizeof(dev->device_name));
1066 dev->config_methods = msg->config_methods ? msg->config_methods :
1067 msg->wps_config_methods;
1068 if (msg->capability) {
1069 dev->dev_capab = msg->capability[0];
1070 dev->group_capab = msg->capability[1];
1072 if (msg->listen_channel) {
1074 freq = p2p_channel_to_freq((char *) msg->listen_channel,
1075 msg->listen_channel[3],
1076 msg->listen_channel[4]);
1078 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1079 "P2P: Unknown peer Listen channel: "
1080 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1081 msg->listen_channel[0],
1082 msg->listen_channel[1],
1083 msg->listen_channel[2],
1084 msg->listen_channel[3],
1085 msg->listen_channel[4]);
1087 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1088 "peer " MACSTR " Listen channel: %u -> %u MHz",
1089 MAC2STR(dev->p2p_device_addr),
1090 dev->listen_freq, freq);
1091 dev->listen_freq = freq;
1094 if (msg->ext_listen_timing) {
1095 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
1096 dev->ext_listen_interval =
1097 WPA_GET_LE16(msg->ext_listen_timing + 2);
1100 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1101 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1102 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1103 "P2P: Completed device entry based on data from "
1104 "GO Negotiation Request");
1106 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1107 "P2P: Created device entry based on GO Neg Req: "
1108 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1110 MAC2STR(dev->p2p_device_addr), dev->dev_capab,
1111 dev->group_capab, dev->device_name, dev->listen_freq);
1114 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1116 if (dev->flags & P2P_DEV_USER_REJECTED) {
1117 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1118 "P2P: Do not report rejected device");
1122 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, dev->p2p_device_addr,
1123 dev->pri_dev_type, dev->device_name,
1124 dev->config_methods, dev->dev_capab,
1129 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1131 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1132 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1133 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1134 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1135 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1139 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1141 p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len);
1142 p2p_random(params->passphrase, 8);
1147 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1149 struct p2p_go_neg_results res;
1150 int go = peer->go_state == LOCAL_GO;
1151 struct p2p_channels intersection;
1155 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1156 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1157 "GO)", MAC2STR(peer->p2p_device_addr),
1158 go ? "local end" : "peer");
1160 os_memset(&res, 0, sizeof(res));
1162 os_memcpy(res.peer_device_addr, peer->p2p_device_addr, ETH_ALEN);
1163 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1164 res.wps_method = peer->wps_method;
1165 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
1166 res.persistent_group = 1;
1169 /* Setup AP mode for WPS provisioning */
1170 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1173 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1174 res.ssid_len = p2p->ssid_len;
1175 p2p_random(res.passphrase, 8);
1177 res.freq = peer->oper_freq;
1178 if (p2p->ssid_len) {
1179 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1180 res.ssid_len = p2p->ssid_len;
1184 p2p_channels_intersect(&p2p->channels, &peer->channels,
1187 for (i = 0; i < intersection.reg_classes; i++) {
1188 struct p2p_reg_class *c = &intersection.reg_class[i];
1189 if (freqs + 1 == P2P_MAX_CHANNELS)
1191 for (j = 0; j < c->channels; j++) {
1193 if (freqs + 1 == P2P_MAX_CHANNELS)
1195 freq = p2p_channel_to_freq(peer->country, c->reg_class,
1199 res.freq_list[freqs++] = freq;
1203 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1205 p2p_clear_timeout(p2p);
1206 peer->go_neg_req_sent = 0;
1207 peer->wps_method = WPS_NOT_READY;
1209 p2p_set_state(p2p, P2P_PROVISIONING);
1210 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1214 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1215 const u8 *data, size_t len, int rx_freq)
1217 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1218 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1219 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1225 case P2P_GO_NEG_REQ:
1226 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1228 case P2P_GO_NEG_RESP:
1229 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1231 case P2P_GO_NEG_CONF:
1232 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1234 case P2P_INVITATION_REQ:
1235 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1238 case P2P_INVITATION_RESP:
1239 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1241 case P2P_PROV_DISC_REQ:
1242 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1244 case P2P_PROV_DISC_RESP:
1245 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1247 case P2P_DEV_DISC_REQ:
1248 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1250 case P2P_DEV_DISC_RESP:
1251 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1254 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1255 "P2P: Unsupported P2P Public Action frame type %d",
1262 void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1263 const u8 *bssid, const u8 *data, size_t len,
1270 case WLAN_PA_VENDOR_SPECIFIC:
1275 if (WPA_GET_BE24(data) != OUI_WFA)
1283 if (*data != P2P_OUI_TYPE)
1286 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1288 case WLAN_PA_GAS_INITIAL_REQ:
1289 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1291 case WLAN_PA_GAS_INITIAL_RESP:
1292 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1294 case WLAN_PA_GAS_COMEBACK_REQ:
1295 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1297 case WLAN_PA_GAS_COMEBACK_RESP:
1298 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1304 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1305 const u8 *bssid, u8 category,
1306 const u8 *data, size_t len, int freq)
1308 if (category == WLAN_ACTION_PUBLIC) {
1309 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1313 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1319 if (WPA_GET_BE24(data) != OUI_WFA)
1324 if (*data != P2P_OUI_TYPE)
1329 /* P2P action frame */
1330 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1331 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1332 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1338 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1339 "P2P: Received P2P Action - Notice of Absence");
1342 case P2P_PRESENCE_REQ:
1343 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1345 case P2P_PRESENCE_RESP:
1346 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1348 case P2P_GO_DISC_REQ:
1349 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1352 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1353 "P2P: Received P2P Action - unknown type %u", data[0]);
1359 static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1361 struct p2p_data *p2p = eloop_ctx;
1362 if (p2p->go_neg_peer == NULL)
1364 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1365 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1366 p2p_connect_send(p2p, p2p->go_neg_peer);
1370 static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1372 struct p2p_data *p2p = eloop_ctx;
1373 if (p2p->invite_peer == NULL)
1375 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1376 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1380 static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1381 const u8 *ie, size_t ie_len)
1383 struct p2p_message msg;
1384 struct p2p_device *dev;
1386 os_memset(&msg, 0, sizeof(msg));
1387 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1389 p2p_parse_free(&msg);
1390 return; /* not a P2P probe */
1393 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1394 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1396 /* The Probe Request is not part of P2P Device Discovery. It is
1397 * not known whether the source address of the frame is the P2P
1398 * Device Address or P2P Interface Address. Do not add a new
1399 * peer entry based on this frames.
1401 p2p_parse_free(&msg);
1405 dev = p2p_get_device(p2p, addr);
1407 if (dev->country[0] == 0 && msg.listen_channel)
1408 os_memcpy(dev->country, msg.listen_channel, 3);
1409 p2p_parse_free(&msg);
1410 return; /* already known */
1413 dev = p2p_create_device(p2p, addr);
1415 p2p_parse_free(&msg);
1419 os_get_time(&dev->last_seen);
1420 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1422 if (msg.capability) {
1423 dev->dev_capab = msg.capability[0];
1424 dev->group_capab = msg.capability[1];
1427 if (msg.listen_channel) {
1428 os_memcpy(dev->country, msg.listen_channel, 3);
1429 dev->listen_freq = p2p_channel_to_freq(dev->country,
1430 msg.listen_channel[3],
1431 msg.listen_channel[4]);
1434 os_memcpy(dev->device_name, msg.device_name, sizeof(dev->device_name));
1436 if (msg.wps_pri_dev_type)
1437 os_memcpy(dev->pri_dev_type, msg.wps_pri_dev_type,
1438 sizeof(dev->pri_dev_type));
1440 p2p_parse_free(&msg);
1442 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1443 "P2P: Created device entry based on Probe Req: " MACSTR
1444 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1445 MAC2STR(dev->p2p_device_addr), dev->dev_capab,
1446 dev->group_capab, dev->device_name, dev->listen_freq);
1450 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1452 struct p2p_message *msg)
1454 struct p2p_device *dev;
1456 dev = p2p_get_device(p2p, addr);
1458 os_get_time(&dev->last_seen);
1459 return dev; /* already known */
1462 dev = p2p_create_device(p2p, addr);
1466 p2p_add_dev_info(p2p, addr, dev, msg);
1472 static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1474 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1476 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1477 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1478 WPA_GET_BE16(&req_dev_type[6]) == 0)
1479 return 1; /* Category match with wildcard OUI/sub-category */
1484 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1485 size_t num_req_dev_type)
1488 for (i = 0; i < num_req_dev_type; i++) {
1489 if (dev_type_match(dev_type, req_dev_type[i]))
1497 * p2p_match_dev_type - Match local device type with requested type
1498 * @p2p: P2P module context from p2p_init()
1499 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1500 * Returns: 1 on match, 0 on mismatch
1502 * This function can be used to match the Requested Device Type attribute in
1503 * WPS IE with the local device types for deciding whether to reply to a Probe
1506 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1508 struct wps_parse_attr attr;
1511 if (wps_parse_msg(wps, &attr))
1512 return 1; /* assume no Requested Device Type attributes */
1514 if (attr.num_req_dev_type == 0)
1515 return 1; /* no Requested Device Type attributes -> match */
1517 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1518 attr.num_req_dev_type))
1519 return 1; /* Own Primary Device Type matches */
1521 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1522 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1524 attr.num_req_dev_type))
1525 return 1; /* Own Secondary Device Type matches */
1527 /* No matching device type found */
1532 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1537 buf = wpabuf_alloc(1000);
1541 /* TODO: add more info into WPS IE; maybe get from WPS module? */
1542 p2p_build_wps_ie(p2p, buf, DEV_PW_DEFAULT, 1);
1545 len = p2p_buf_add_ie_hdr(buf);
1546 p2p_buf_add_capability(buf, p2p->dev_capab, 0);
1547 if (p2p->ext_listen_interval)
1548 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1549 p2p->ext_listen_interval);
1550 p2p_buf_add_device_info(buf, p2p, NULL);
1551 p2p_buf_update_ie_hdr(buf, len);
1557 static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *ie,
1560 struct ieee802_11_elems elems;
1562 struct ieee80211_mgmt *resp;
1566 if (!p2p->in_listen || !p2p->drv_in_listen) {
1567 /* not in Listen state - ignore Probe Request */
1571 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
1573 /* Ignore invalid Probe Request frames */
1577 if (elems.p2p == NULL) {
1578 /* not a P2P probe - ignore it */
1582 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
1583 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
1585 /* not using P2P Wildcard SSID - ignore */
1589 /* Check Requested Device Type match */
1590 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1591 if (wps && !p2p_match_dev_type(p2p, wps)) {
1593 /* No match with Requested Device Type */
1598 if (!p2p->cfg->send_probe_resp)
1599 return; /* Response generated elsewhere */
1601 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1602 "P2P: Reply to P2P Probe Request in Listen state");
1605 * We do not really have a specific BSS that this frame is advertising,
1606 * so build a frame that has some information in valid format. This is
1607 * really only used for discovery purposes, not to learn exact BSS
1610 ies = p2p_build_probe_resp_ies(p2p);
1614 buf = wpabuf_alloc(200 + wpabuf_len(ies));
1621 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
1623 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
1624 (WLAN_FC_STYPE_PROBE_RESP << 4));
1625 os_memcpy(resp->da, addr, ETH_ALEN);
1626 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
1627 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
1628 resp->u.probe_resp.beacon_int = host_to_le16(100);
1629 /* hardware or low-level driver will setup seq_ctrl and timestamp */
1630 resp->u.probe_resp.capab_info =
1631 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
1632 WLAN_CAPABILITY_PRIVACY |
1633 WLAN_CAPABILITY_SHORT_SLOT_TIME);
1635 wpabuf_put_u8(buf, WLAN_EID_SSID);
1636 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
1637 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1639 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
1640 wpabuf_put_u8(buf, 8);
1641 wpabuf_put_u8(buf, (60 / 5) | 0x80);
1642 wpabuf_put_u8(buf, 90 / 5);
1643 wpabuf_put_u8(buf, (120 / 5) | 0x80);
1644 wpabuf_put_u8(buf, 180 / 5);
1645 wpabuf_put_u8(buf, (240 / 5) | 0x80);
1646 wpabuf_put_u8(buf, 360 / 5);
1647 wpabuf_put_u8(buf, 480 / 5);
1648 wpabuf_put_u8(buf, 540 / 5);
1650 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
1651 wpabuf_put_u8(buf, 1);
1652 wpabuf_put_u8(buf, p2p->cfg->channel);
1654 wpabuf_put_buf(buf, ies);
1657 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
1663 int p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *ie,
1666 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
1668 p2p_reply_probe(p2p, addr, ie, ie_len);
1670 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
1672 os_memcmp(addr, p2p->go_neg_peer->p2p_device_addr, ETH_ALEN) == 0)
1674 /* Received a Probe Request from GO Negotiation peer */
1675 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1676 "P2P: Found GO Negotiation peer - try to start GO "
1677 "negotiation from timeout");
1678 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
1682 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
1684 os_memcmp(addr, p2p->invite_peer->p2p_device_addr, ETH_ALEN) == 0)
1686 /* Received a Probe Request from Invite peer */
1687 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1688 "P2P: Found Invite peer - try to start Invite from "
1690 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
1698 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
1699 u8 *buf, size_t len, struct wpabuf *p2p_ie)
1708 return 0; /* WLAN AP is not a P2P manager */
1711 * (Re)Association Request - P2P IE
1712 * P2P Capability attribute (shall be present)
1713 * P2P Interface attribute (present if concurrent device and
1714 * P2P Management is enabled)
1716 tmp = wpabuf_alloc(200);
1720 lpos = p2p_buf_add_ie_hdr(tmp);
1722 if (p2p->num_groups > 0) {
1723 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
1724 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1725 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
1727 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
1729 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
1730 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1731 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
1732 p2p_buf_add_p2p_interface(tmp, p2p);
1733 p2p_buf_update_ie_hdr(tmp, lpos);
1735 tmplen = wpabuf_len(tmp);
1739 os_memcpy(buf, wpabuf_head(tmp), tmplen);
1748 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
1749 size_t len, int p2p_group, struct wpabuf *p2p_ie)
1753 struct p2p_device *peer;
1758 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
1761 * (Re)Association Request - P2P IE
1762 * P2P Capability attribute (shall be present)
1763 * Extended Listen Timing (may be present)
1764 * P2P Device Info attribute (shall be present)
1766 tmp = wpabuf_alloc(200);
1770 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
1772 lpos = p2p_buf_add_ie_hdr(tmp);
1773 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
1774 if (p2p->ext_listen_interval)
1775 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
1776 p2p->ext_listen_interval);
1777 p2p_buf_add_device_info(tmp, p2p, peer);
1778 p2p_buf_update_ie_hdr(tmp, lpos);
1780 tmplen = wpabuf_len(tmp);
1784 os_memcpy(buf, wpabuf_head(tmp), tmplen);
1793 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
1795 struct wpabuf *p2p_ie;
1798 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
1802 ret = p2p_attr_text(p2p_ie, buf, end);
1803 wpabuf_free(p2p_ie);
1808 static void p2p_clear_go_neg(struct p2p_data *p2p)
1810 p2p->go_neg_peer = NULL;
1811 p2p_clear_timeout(p2p);
1812 p2p_set_state(p2p, P2P_IDLE);
1816 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
1818 if (p2p->go_neg_peer == NULL) {
1819 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1820 "P2P: No pending Group Formation - "
1821 "ignore WPS registration success notification");
1822 return; /* No pending Group Formation */
1825 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
1827 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1828 "P2P: Ignore WPS registration success notification "
1829 "for " MACSTR " (GO Negotiation peer " MACSTR ")",
1831 MAC2STR(p2p->go_neg_peer->intended_addr));
1832 return; /* Ignore unexpected peer address */
1835 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1836 "P2P: Group Formation completed successfully with " MACSTR,
1839 p2p_clear_go_neg(p2p);
1843 void p2p_group_formation_failed(struct p2p_data *p2p)
1845 if (p2p->go_neg_peer == NULL) {
1846 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1847 "P2P: No pending Group Formation - "
1848 "ignore group formation failure notification");
1849 return; /* No pending Group Formation */
1852 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1853 "P2P: Group Formation failed with " MACSTR,
1854 MAC2STR(p2p->go_neg_peer->intended_addr));
1856 p2p_clear_go_neg(p2p);
1860 struct p2p_data * p2p_init(const struct p2p_config *cfg)
1862 struct p2p_data *p2p;
1864 if (cfg->max_peers < 1)
1867 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
1870 p2p->cfg = (struct p2p_config *) (p2p + 1);
1871 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
1873 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
1875 p2p->min_disc_int = 1;
1876 p2p->max_disc_int = 3;
1878 os_get_random(&p2p->next_tie_breaker, 1);
1879 p2p->next_tie_breaker &= 0x01;
1880 if (cfg->sd_request)
1881 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
1882 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
1883 if (cfg->concurrent_operations)
1884 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
1885 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
1887 dl_list_init(&p2p->devices);
1889 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
1890 p2p_expiration_timeout, p2p, NULL);
1896 void p2p_deinit(struct p2p_data *p2p)
1898 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
1899 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
1900 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1902 os_free(p2p->cfg->dev_name);
1903 os_free(p2p->groups);
1904 wpabuf_free(p2p->sd_resp);
1905 os_free(p2p->after_scan_tx);
1910 void p2p_flush(struct p2p_data *p2p)
1912 struct p2p_device *dev, *prev;
1913 p2p_clear_timeout(p2p);
1914 p2p_set_state(p2p, P2P_IDLE);
1915 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1916 p2p->go_neg_peer = NULL;
1917 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1918 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
1920 dl_list_del(&dev->list);
1921 p2p_device_free(p2p, dev);
1923 p2p_free_sd_queries(p2p);
1924 os_free(p2p->after_scan_tx);
1925 p2p->after_scan_tx = NULL;
1929 int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
1931 struct p2p_device *dev;
1933 dev = p2p_get_device(p2p, addr);
1937 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
1940 if (p2p->go_neg_peer == dev)
1941 p2p->go_neg_peer = NULL;
1943 dev->wps_method = WPS_NOT_READY;
1944 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1945 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1947 /* Check if after_scan_tx is for this peer. If so free it */
1948 if (p2p->after_scan_tx &&
1949 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
1950 os_free(p2p->after_scan_tx);
1951 p2p->after_scan_tx = NULL;
1958 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
1960 os_free(p2p->cfg->dev_name);
1962 p2p->cfg->dev_name = os_strdup(dev_name);
1963 if (p2p->cfg->dev_name == NULL)
1966 p2p->cfg->dev_name = NULL;
1971 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
1973 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
1978 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
1979 size_t num_dev_types)
1981 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
1982 num_dev_types = P2P_SEC_DEVICE_TYPES;
1983 p2p->cfg->num_sec_dev_types = num_dev_types;
1984 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
1989 int p2p_set_country(struct p2p_data *p2p, const char *country)
1991 os_memcpy(p2p->cfg->country, country, 3);
1996 void p2p_continue_find(struct p2p_data *p2p)
1998 struct p2p_device *dev;
1999 p2p_set_state(p2p, P2P_SEARCH);
2000 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2001 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2002 if (p2p_start_sd(p2p, dev) == 0)
2006 } else if (dev->req_config_methods &&
2007 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2008 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2009 "pending Provisioning Discovery Request to "
2010 MACSTR " (config methods 0x%x)",
2011 MAC2STR(dev->p2p_device_addr),
2012 dev->req_config_methods);
2013 if (p2p_send_prov_disc_req(p2p, dev, 0) == 0)
2018 p2p_listen_in_find(p2p);
2022 static void p2p_sd_cb(struct p2p_data *p2p, int success)
2024 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2025 "P2P: Service Discovery Query TX callback: success=%d",
2027 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2031 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2032 p2p->sd_peer = NULL;
2034 p2p_continue_find(p2p);
2038 if (p2p->sd_peer == NULL) {
2039 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2040 "P2P: No SD peer entry known");
2041 p2p_continue_find(p2p);
2045 /* Wait for response from the peer */
2046 p2p_set_state(p2p, P2P_SD_DURING_FIND);
2047 p2p_set_timeout(p2p, 0, 200000);
2051 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2053 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2054 "P2P: Provision Discovery Request TX callback: success=%d",
2056 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2059 if (p2p->state != P2P_IDLE)
2060 p2p_continue_find(p2p);
2064 /* Wait for response from the peer */
2065 if (p2p->state == P2P_SEARCH)
2066 p2p_set_state(p2p, P2P_PD_DURING_FIND);
2067 p2p_set_timeout(p2p, 0, 200000);
2071 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
2072 int level, const u8 *ies, size_t ies_len)
2074 p2p_add_device(p2p, bssid, freq, level, ies, ies_len);
2076 if (p2p->go_neg_peer && p2p->state == P2P_SEARCH &&
2077 os_memcmp(p2p->go_neg_peer->p2p_device_addr, bssid, ETH_ALEN) == 0)
2079 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2080 "P2P: Found GO Negotiation peer - try to start GO "
2082 p2p_connect_send(p2p, p2p->go_neg_peer);
2090 void p2p_scan_res_handled(struct p2p_data *p2p)
2092 if (!p2p->p2p_scan_running) {
2093 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
2094 "running, but scan results received");
2096 p2p->p2p_scan_running = 0;
2097 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2099 if (p2p_run_after_scan(p2p))
2101 if (p2p->state == P2P_SEARCH)
2102 p2p_continue_find(p2p);
2106 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies)
2108 u8 *len = p2p_buf_add_ie_hdr(ies);
2109 p2p_buf_add_capability(ies, p2p->dev_capab, 0);
2110 if (p2p->cfg->reg_class && p2p->cfg->channel)
2111 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2112 p2p->cfg->reg_class,
2114 if (p2p->ext_listen_interval)
2115 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2116 p2p->ext_listen_interval);
2117 /* TODO: p2p_buf_add_operating_channel() if GO */
2118 p2p_buf_update_ie_hdr(ies, len);
2122 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2124 return p2p_attr_text(p2p_ie, buf, end);
2128 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2130 struct p2p_device *dev = p2p->go_neg_peer;
2132 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2133 "P2P: GO Negotiation Request TX callback: success=%d",
2137 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2138 "P2P: No pending GO Negotiation");
2143 dev->go_neg_req_sent++;
2144 if (dev->flags & P2P_DEV_USER_REJECTED) {
2145 p2p_set_state(p2p, P2P_IDLE);
2151 (dev->dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2152 !is_zero_ether_addr(dev->member_in_go_dev)) {
2153 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2154 "P2P: Peer " MACSTR " did not acknowledge request - "
2155 "try to use device discoverability through its GO",
2156 MAC2STR(dev->p2p_device_addr));
2157 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2158 p2p_send_dev_disc_req(p2p, dev);
2163 * Use P2P find, if needed, to find the other device from its listen
2166 p2p_set_state(p2p, P2P_CONNECT);
2167 p2p_set_timeout(p2p, 0, 100000);
2171 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2173 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2174 "P2P: GO Negotiation Response TX callback: success=%d",
2176 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2177 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2178 "P2P: Ignore TX callback event - GO Negotiation is "
2179 "not running anymore");
2182 p2p_set_state(p2p, P2P_CONNECT);
2183 p2p_set_timeout(p2p, 0, 100000);
2187 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2189 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2190 "P2P: GO Negotiation Response (failure) TX callback: "
2191 "success=%d", success);
2192 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2193 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2194 p2p->go_neg_peer->status);
2199 static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
2200 enum p2p_send_action_result result)
2202 struct p2p_device *dev;
2204 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2205 "P2P: GO Negotiation Confirm TX callback: result=%d",
2207 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2208 if (result == P2P_SEND_ACTION_FAILED) {
2209 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2212 if (result == P2P_SEND_ACTION_NO_ACK) {
2214 * It looks like the TX status for GO Negotiation Confirm is
2215 * often showing failure even when the peer has actually
2216 * received the frame. Since the peer may change channels
2217 * immediately after having received the frame, we may not see
2218 * an Ack for retries, so just dropping a single frame may
2219 * trigger this. To allow the group formation to succeed if the
2220 * peer did indeed receive the frame, continue regardless of
2223 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2224 "P2P: Assume GO Negotiation Confirm TX was actually "
2225 "received by the peer even though Ack was not "
2229 dev = p2p->go_neg_peer;
2233 p2p_go_complete(p2p, dev);
2237 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
2238 const u8 *src, const u8 *bssid,
2239 enum p2p_send_action_result result)
2241 enum p2p_pending_action_state state;
2244 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2245 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
2246 " src=" MACSTR " bssid=" MACSTR " result=%d",
2247 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
2248 MAC2STR(bssid), result);
2249 success = result == P2P_SEND_ACTION_SUCCESS;
2250 state = p2p->pending_action_state;
2251 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2253 case P2P_NO_PENDING_ACTION:
2255 case P2P_PENDING_GO_NEG_REQUEST:
2256 p2p_go_neg_req_cb(p2p, success);
2258 case P2P_PENDING_GO_NEG_RESPONSE:
2259 p2p_go_neg_resp_cb(p2p, success);
2261 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
2262 p2p_go_neg_resp_failure_cb(p2p, success);
2264 case P2P_PENDING_GO_NEG_CONFIRM:
2265 p2p_go_neg_conf_cb(p2p, result);
2267 case P2P_PENDING_SD:
2268 p2p_sd_cb(p2p, success);
2270 case P2P_PENDING_PD:
2271 p2p_prov_disc_cb(p2p, success);
2273 case P2P_PENDING_INVITATION_REQUEST:
2274 p2p_invitation_req_cb(p2p, success);
2276 case P2P_PENDING_INVITATION_RESPONSE:
2277 p2p_invitation_resp_cb(p2p, success);
2279 case P2P_PENDING_DEV_DISC_REQUEST:
2280 p2p_dev_disc_req_cb(p2p, success);
2282 case P2P_PENDING_DEV_DISC_RESPONSE:
2283 p2p_dev_disc_resp_cb(p2p, success);
2285 case P2P_PENDING_GO_DISC_REQ:
2286 p2p_go_disc_req_cb(p2p, success);
2292 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
2293 unsigned int duration)
2295 if (freq == p2p->pending_client_disc_freq) {
2296 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2297 "P2P: Client discoverability remain-awake completed");
2298 p2p->pending_client_disc_freq = 0;
2302 if (freq != p2p->pending_listen_freq) {
2303 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2304 "P2P: Unexpected listen callback for freq=%u "
2305 "duration=%u (pending_listen_freq=%u)",
2306 freq, duration, p2p->pending_listen_freq);
2310 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2311 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
2313 p2p->pending_listen_sec, p2p->pending_listen_usec,
2314 p2p->pending_listen_freq);
2316 p2p->drv_in_listen = freq;
2317 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
2319 * Add 20 msec extra wait to avoid race condition with driver
2320 * remain-on-channel end event, i.e., give driver more time to
2321 * complete the operation before our timeout expires.
2323 p2p_set_timeout(p2p, p2p->pending_listen_sec,
2324 p2p->pending_listen_usec + 20000);
2327 p2p->pending_listen_freq = 0;
2331 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
2333 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
2334 "state (freq=%u)", freq);
2335 p2p->drv_in_listen = 0;
2337 return 0; /* Internal timeout will trigger the next step */
2339 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
2340 if (p2p->go_neg_peer->connect_reqs >= 120) {
2341 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2342 "P2P: Timeout on sending GO Negotiation "
2343 "Request without getting response");
2344 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2348 p2p_set_state(p2p, P2P_CONNECT);
2349 p2p_connect_send(p2p, p2p->go_neg_peer);
2351 } else if (p2p->state == P2P_SEARCH) {
2360 static void p2p_timeout_connect(struct p2p_data *p2p)
2362 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2363 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
2364 p2p_listen_in_find(p2p);
2368 static void p2p_timeout_connect_listen(struct p2p_data *p2p)
2370 if (p2p->go_neg_peer) {
2371 if (p2p->drv_in_listen) {
2372 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
2373 "still in Listen state; wait for it to "
2378 if (p2p->go_neg_peer->connect_reqs >= 120) {
2379 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2380 "P2P: Timeout on sending GO Negotiation "
2381 "Request without getting response");
2382 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2386 p2p_set_state(p2p, P2P_CONNECT);
2387 p2p_connect_send(p2p, p2p->go_neg_peer);
2389 p2p_set_state(p2p, P2P_IDLE);
2393 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
2396 * TODO: could remain constantly in Listen state for some time if there
2397 * are no other concurrent uses for the radio. For now, go to listen
2398 * state once per second to give other uses a chance to use the radio.
2400 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
2401 p2p_set_timeout(p2p, 1, 0);
2405 static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
2407 struct p2p_device *dev = p2p->go_neg_peer;
2410 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2411 "P2P: Unknown GO Neg peer - stop GO Neg wait");
2416 if (dev->wait_count >= 120) {
2417 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2418 "P2P: Timeout on waiting peer to become ready for GO "
2420 p2p_go_neg_failed(p2p, dev, -1);
2424 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2425 "P2P: Go to Listen state while waiting for the peer to become "
2426 "ready for GO Negotiation");
2427 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
2428 p2p_listen_in_find(p2p);
2432 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
2434 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2435 "P2P: Service Discovery Query timeout");
2437 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2438 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2439 p2p->sd_peer = NULL;
2441 p2p_continue_find(p2p);
2445 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
2447 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2448 "P2P: Provision Discovery Request timeout");
2449 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2450 p2p_continue_find(p2p);
2454 static void p2p_timeout_invite(struct p2p_data *p2p)
2456 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2457 p2p_set_state(p2p, P2P_INVITE_LISTEN);
2458 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
2460 * Better remain on operating channel instead of listen channel
2461 * when running a group.
2463 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
2464 "active GO role - wait on operating channel");
2465 p2p_set_timeout(p2p, 0, 100000);
2468 p2p_listen_in_find(p2p);
2472 static void p2p_timeout_invite_listen(struct p2p_data *p2p)
2474 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
2475 p2p_set_state(p2p, P2P_INVITE);
2476 p2p_invite_send(p2p, p2p->invite_peer,
2477 p2p->invite_go_dev_addr);
2479 if (p2p->invite_peer) {
2480 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2481 "P2P: Invitation Request retry limit reached");
2482 if (p2p->cfg->invitation_result)
2483 p2p->cfg->invitation_result(
2484 p2p->cfg->cb_ctx, -1, NULL);
2486 p2p_set_state(p2p, P2P_IDLE);
2491 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
2493 struct p2p_data *p2p = eloop_ctx;
2495 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
2496 p2p_state_txt(p2p->state));
2500 switch (p2p->state) {
2507 p2p_timeout_connect(p2p);
2509 case P2P_CONNECT_LISTEN:
2510 p2p_timeout_connect_listen(p2p);
2514 case P2P_LISTEN_ONLY:
2515 if (p2p->ext_listen_only) {
2516 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2517 "P2P: Extended Listen Timing - Listen State "
2519 p2p->ext_listen_only = 0;
2520 p2p_set_state(p2p, P2P_IDLE);
2523 case P2P_WAIT_PEER_CONNECT:
2524 p2p_timeout_wait_peer_connect(p2p);
2526 case P2P_WAIT_PEER_IDLE:
2527 p2p_timeout_wait_peer_idle(p2p);
2529 case P2P_SD_DURING_FIND:
2530 p2p_timeout_sd_during_find(p2p);
2532 case P2P_PROVISIONING:
2534 case P2P_PD_DURING_FIND:
2535 p2p_timeout_prov_disc_during_find(p2p);
2538 p2p_timeout_invite(p2p);
2540 case P2P_INVITE_LISTEN:
2541 p2p_timeout_invite_listen(p2p);
2547 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
2549 struct p2p_device *dev;
2551 dev = p2p_get_device(p2p, peer_addr);
2552 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
2553 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
2555 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
2556 " unknown", MAC2STR(peer_addr));
2559 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
2560 dev->flags |= P2P_DEV_USER_REJECTED;
2565 static const char * p2p_wps_method_text(enum p2p_wps_method method)
2572 case WPS_PIN_DISPLAY:
2574 case WPS_PIN_KEYPAD:
2584 static const char * p2p_go_state_text(enum p2p_go_state go_state)
2599 int p2p_get_peer_info(struct p2p_data *p2p, const u8 *addr, int next,
2600 char *buf, size_t buflen)
2602 struct p2p_device *dev;
2606 char devtype[WPS_DEV_TYPE_BUFSIZE];
2609 dev = p2p_get_device(p2p, addr);
2611 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
2614 dev = dl_list_first(&dev->list, struct p2p_device, list);
2615 if (&dev->list == &p2p->devices)
2625 res = os_snprintf(pos, end - pos, MACSTR "\n",
2626 MAC2STR(dev->p2p_device_addr));
2627 if (res < 0 || res >= end - pos)
2632 res = os_snprintf(pos, end - pos,
2637 "interface_addr=" MACSTR "\n"
2638 "member_in_go_dev=" MACSTR "\n"
2639 "member_in_go_iface=" MACSTR "\n"
2642 "config_methods=0x%x\n"
2644 "group_capab=0x%x\n"
2645 "go_neg_req_sent=%d\n"
2648 "intended_addr=" MACSTR "\n"
2651 "req_config_methods=0x%x\n"
2652 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
2655 "invitation_reqs=%u\n",
2656 (int) (now.sec - dev->last_seen.sec),
2659 p2p_wps_method_text(dev->wps_method),
2660 MAC2STR(dev->interface_addr),
2661 MAC2STR(dev->member_in_go_dev),
2662 MAC2STR(dev->member_in_go_iface),
2663 wps_dev_type_bin2str(dev->pri_dev_type,
2664 devtype, sizeof(devtype)),
2666 dev->config_methods,
2669 dev->go_neg_req_sent,
2670 p2p_go_state_text(dev->go_state),
2672 MAC2STR(dev->intended_addr),
2673 dev->country[0] ? dev->country[0] : '_',
2674 dev->country[1] ? dev->country[1] : '_',
2676 dev->req_config_methods,
2677 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
2678 "[PROBE_REQ_ONLY]" : "",
2679 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
2680 dev->flags & P2P_DEV_NOT_YET_READY ?
2681 "[NOT_YET_READY]" : "",
2682 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
2683 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
2685 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
2686 "[PD_PEER_DISPLAY]" : "",
2687 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
2688 "[PD_PEER_KEYPAD]" : "",
2689 dev->flags & P2P_DEV_USER_REJECTED ?
2690 "[USER_REJECTED]" : "",
2691 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
2692 "[PEER_WAITING_RESPONSE]" : "",
2693 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
2694 "[PREFER_PERSISTENT_GROUP]" : "",
2695 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
2696 "[WAIT_GO_NEG_RESPONSE]" : "",
2697 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
2698 "[WAIT_GO_NEG_CONFIRM]" : "",
2699 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
2700 "[GROUP_CLIENT_ONLY]" : "",
2701 dev->flags & P2P_DEV_FORCE_FREQ ?
2702 "[FORCE_FREQ]" : "",
2703 dev->flags & P2P_DEV_PD_FOR_JOIN ?
2704 "[PD_FOR_JOIN]" : "",
2707 dev->invitation_reqs);
2708 if (res < 0 || res >= end - pos)
2712 if (dev->ext_listen_period) {
2713 res = os_snprintf(pos, end - pos,
2714 "ext_listen_period=%u\n"
2715 "ext_listen_interval=%u\n",
2716 dev->ext_listen_period,
2717 dev->ext_listen_interval);
2718 if (res < 0 || res >= end - pos)
2723 if (dev->oper_ssid_len) {
2724 res = os_snprintf(pos, end - pos,
2726 wpa_ssid_txt(dev->oper_ssid,
2727 dev->oper_ssid_len));
2728 if (res < 0 || res >= end - pos)
2737 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
2740 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
2741 "discoverability enabled");
2742 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2744 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
2745 "discoverability disabled");
2746 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2751 static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
2752 u32 duration2, u32 interval2)
2755 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
2758 req = wpabuf_alloc(100);
2762 if (duration1 || interval1) {
2763 os_memset(&desc1, 0, sizeof(desc1));
2764 desc1.count_type = 1;
2765 desc1.duration = duration1;
2766 desc1.interval = interval1;
2769 if (duration2 || interval2) {
2770 os_memset(&desc2, 0, sizeof(desc2));
2771 desc2.count_type = 2;
2772 desc2.duration = duration2;
2773 desc2.interval = interval2;
2778 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
2779 len = p2p_buf_add_ie_hdr(req);
2780 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
2781 p2p_buf_update_ie_hdr(req, len);
2787 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
2788 const u8 *own_interface_addr, unsigned int freq,
2789 u32 duration1, u32 interval1, u32 duration2,
2794 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
2795 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
2796 "int1=%u dur2=%u int2=%u",
2797 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
2798 freq, duration1, interval1, duration2, interval2);
2800 req = p2p_build_presence_req(duration1, interval1, duration2,
2805 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2806 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
2808 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
2809 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2810 "P2P: Failed to send Action frame");
2818 static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
2819 size_t noa_len, u8 dialog_token)
2821 struct wpabuf *resp;
2824 resp = wpabuf_alloc(100 + noa_len);
2828 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
2829 len = p2p_buf_add_ie_hdr(resp);
2830 p2p_buf_add_status(resp, status);
2832 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
2833 wpabuf_put_le16(resp, noa_len);
2834 wpabuf_put_data(resp, noa, noa_len);
2836 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
2837 p2p_buf_update_ie_hdr(resp, len);
2843 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
2844 const u8 *sa, const u8 *data, size_t len,
2847 struct p2p_message msg;
2849 struct wpabuf *resp;
2851 struct p2p_group *group = NULL;
2856 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2857 "P2P: Received P2P Action - P2P Presence Request");
2859 for (g = 0; g < p2p->num_groups; g++) {
2860 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
2862 group = p2p->groups[g];
2866 if (group == NULL) {
2867 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2868 "P2P: Ignore P2P Presence Request for unknown group "
2869 MACSTR, MAC2STR(da));
2873 if (p2p_parse(data, len, &msg) < 0) {
2874 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2875 "P2P: Failed to parse P2P Presence Request");
2876 status = P2P_SC_FAIL_INVALID_PARAMS;
2881 if (msg.noa == NULL) {
2882 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2883 "P2P: No NoA attribute in P2P Presence Request");
2884 status = P2P_SC_FAIL_INVALID_PARAMS;
2888 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
2891 if (p2p->cfg->get_noa)
2892 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
2896 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
2897 noa_len > 0 ? noa_len : 0,
2900 p2p_parse_free(&msg);
2904 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2905 if (p2p_send_action(p2p, rx_freq, sa, da, da,
2906 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
2907 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2908 "P2P: Failed to send Action frame");
2914 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
2915 const u8 *sa, const u8 *data, size_t len)
2917 struct p2p_message msg;
2919 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2920 "P2P: Received P2P Action - P2P Presence Response");
2922 if (p2p_parse(data, len, &msg) < 0) {
2923 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2924 "P2P: Failed to parse P2P Presence Response");
2928 if (msg.status == NULL || msg.noa == NULL) {
2929 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2930 "P2P: No Status or NoA attribute in P2P Presence "
2932 p2p_parse_free(&msg);
2937 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2938 "P2P: P2P Presence Request was rejected: status %u",
2940 p2p_parse_free(&msg);
2944 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2945 "P2P: P2P Presence Request was accepted");
2946 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
2947 msg.noa, msg.noa_len);
2948 /* TODO: process NoA */
2949 p2p_parse_free(&msg);
2953 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
2955 struct p2p_data *p2p = eloop_ctx;
2957 if (p2p->ext_listen_interval) {
2958 /* Schedule next extended listen timeout */
2959 eloop_register_timeout(p2p->ext_listen_interval_sec,
2960 p2p->ext_listen_interval_usec,
2961 p2p_ext_listen_timeout, p2p, NULL);
2964 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
2966 * This should not really happen, but it looks like the Listen
2967 * command may fail is something else (e.g., a scan) was
2968 * running at an inconvenient time. As a workaround, allow new
2969 * Extended Listen operation to be started.
2971 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
2972 "Extended Listen operation had not been completed - "
2974 p2p->ext_listen_only = 0;
2975 p2p_set_state(p2p, P2P_IDLE);
2978 if (p2p->state != P2P_IDLE) {
2979 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
2980 "Listen timeout in active state (%s)",
2981 p2p_state_txt(p2p->state));
2985 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
2986 p2p->ext_listen_only = 1;
2987 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
2988 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
2989 "Listen state for Extended Listen Timing");
2990 p2p->ext_listen_only = 0;
2995 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
2996 unsigned int interval)
2998 if (period > 65535 || interval > 65535 || period > interval ||
2999 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
3000 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3001 "P2P: Invalid Extended Listen Timing request: "
3002 "period=%u interval=%u", period, interval);
3006 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
3008 if (interval == 0) {
3009 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3010 "P2P: Disabling Extended Listen Timing");
3011 p2p->ext_listen_period = 0;
3012 p2p->ext_listen_interval = 0;
3016 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3017 "P2P: Enabling Extended Listen Timing: period %u msec, "
3018 "interval %u msec", period, interval);
3019 p2p->ext_listen_period = period;
3020 p2p->ext_listen_interval = interval;
3021 p2p->ext_listen_interval_sec = interval / 1000;
3022 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
3024 eloop_register_timeout(p2p->ext_listen_interval_sec,
3025 p2p->ext_listen_interval_usec,
3026 p2p_ext_listen_timeout, p2p, NULL);
3032 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3033 const u8 *ie, size_t ie_len)
3035 struct p2p_message msg;
3037 if (bssid == NULL || ie == NULL)
3040 os_memset(&msg, 0, sizeof(msg));
3041 if (p2p_parse_ies(ie, ie_len, &msg))
3043 if (msg.minor_reason_code == NULL)
3046 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3047 "P2P: Deauthentication notification BSSID " MACSTR
3048 " reason_code=%u minor_reason_code=%u",
3049 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3051 p2p_parse_free(&msg);
3055 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3056 const u8 *ie, size_t ie_len)
3058 struct p2p_message msg;
3060 if (bssid == NULL || ie == NULL)
3063 os_memset(&msg, 0, sizeof(msg));
3064 if (p2p_parse_ies(ie, ie_len, &msg))
3066 if (msg.minor_reason_code == NULL)
3069 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3070 "P2P: Disassociation notification BSSID " MACSTR
3071 " reason_code=%u minor_reason_code=%u",
3072 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3074 p2p_parse_free(&msg);
3078 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
3081 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3082 "Device operations enabled");
3083 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
3085 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3086 "Device operations disabled");
3087 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
3092 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
3094 if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
3097 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
3098 "reg_class %u channel %u", reg_class, channel);
3099 p2p->cfg->reg_class = reg_class;
3100 p2p->cfg->channel = channel;
3106 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
3108 wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
3109 if (postfix == NULL) {
3110 p2p->cfg->ssid_postfix_len = 0;
3113 if (len > sizeof(p2p->cfg->ssid_postfix))
3115 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
3116 p2p->cfg->ssid_postfix_len = len;
3121 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
3124 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
3125 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
3127 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
3132 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
3135 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3138 os_memcpy(dev_addr, dev->p2p_device_addr, ETH_ALEN);
3143 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
3145 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
3146 if (is_zero_ether_addr(p2p->peer_filter))
3147 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
3150 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
3151 "filter for " MACSTR, MAC2STR(p2p->peer_filter));
3155 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
3157 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
3158 enabled ? "enabled" : "disabled");
3159 if (p2p->cross_connect == enabled)
3161 p2p->cross_connect = enabled;
3162 /* TODO: may need to tear down any action group where we are GO(?) */
3166 int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
3168 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3171 if (dev->oper_freq <= 0)
3173 return dev->oper_freq;
3177 void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
3179 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
3180 enabled ? "enabled" : "disabled");
3181 p2p->cfg->p2p_intra_bss = enabled;
3185 void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
3187 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
3188 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
3192 int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3193 const u8 *src, const u8 *bssid, const u8 *buf,
3194 size_t len, unsigned int wait_time)
3196 if (p2p->p2p_scan_running) {
3197 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
3198 "frame TX until p2p_scan completes");
3199 if (p2p->after_scan_tx) {
3200 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
3201 "previous pending Action frame TX");
3202 os_free(p2p->after_scan_tx);
3204 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
3206 if (p2p->after_scan_tx == NULL)
3208 p2p->after_scan_tx->freq = freq;
3209 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
3210 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
3211 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
3212 p2p->after_scan_tx->len = len;
3213 p2p->after_scan_tx->wait_time = wait_time;
3214 os_memcpy(p2p->after_scan_tx + 1, buf, len);
3218 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
3219 buf, len, wait_time);
3223 void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
3226 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
3227 " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall);
3228 p2p->best_freq_24 = freq_24;
3229 p2p->best_freq_5 = freq_5;
3230 p2p->best_freq_overall = freq_overall;
3234 const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
3236 if (p2p == NULL || p2p->go_neg_peer == NULL)
3238 return p2p->go_neg_peer->p2p_device_addr;