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->info.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->info.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->info.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->info.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->info.device_name, cli->dev_name, cli->dev_name_len);
337 dev->info.device_name[cli->dev_name_len] = '\0';
338 dev->info.dev_capab = cli->dev_capab;
339 dev->info.config_methods = cli->config_methods;
340 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
341 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
342 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
343 dev->info.wps_sec_dev_type_list_len);
347 static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
348 const u8 *go_interface_addr, int freq,
349 const u8 *gi, size_t gi_len)
351 struct p2p_group_info info;
353 struct p2p_device *dev;
358 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
362 * Clear old data for this group; if the devices are still in the
363 * group, the information will be restored in the loop following this.
365 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
366 if (os_memcpy(dev->member_in_go_iface, go_interface_addr,
368 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
369 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
373 for (c = 0; c < info.num_clients; c++) {
374 struct p2p_client_info *cli = &info.client[c];
375 dev = p2p_get_device(p2p, cli->p2p_device_addr);
378 * Update information only if we have not received this
379 * directly from the client.
381 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
382 P2P_DEV_PROBE_REQ_ONLY))
383 p2p_copy_client_info(dev, cli);
384 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
385 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
388 dev = p2p_create_device(p2p, cli->p2p_device_addr);
391 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
392 p2p_copy_client_info(dev, cli);
393 dev->oper_freq = freq;
394 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
395 dev->info.p2p_device_addr,
397 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
400 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
402 os_get_time(&dev->last_seen);
403 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
404 os_memcpy(dev->member_in_go_iface, go_interface_addr,
413 * p2p_add_device - Add peer entries based on scan results
414 * @p2p: P2P module context from p2p_init()
415 * @addr: Source address of Beacon or Probe Response frame (may be either
416 * P2P Device Address or P2P Interface Address)
417 * @level: Signal level (signal strength of the received frame from the peer)
418 * @freq: Frequency on which the Beacon or Probe Response frame was received
419 * @ies: IEs from the Beacon or Probe Response frame
420 * @ies_len: Length of ies buffer in octets
421 * Returns: 0 on success, -1 on failure
423 * If the scan result is for a GO, the clients in the group will also be added
424 * to the peer table. This function can also be used with some other frames
425 * like Provision Discovery Request that contains P2P Capability and P2P Device
428 int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
429 const u8 *ies, size_t ies_len)
431 struct p2p_device *dev;
432 struct p2p_message msg;
433 const u8 *p2p_dev_addr;
436 os_memset(&msg, 0, sizeof(msg));
437 if (p2p_parse_ies(ies, ies_len, &msg)) {
438 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
439 "P2P: Failed to parse P2P IE for a device entry");
440 p2p_parse_free(&msg);
444 if (msg.p2p_device_addr)
445 p2p_dev_addr = msg.p2p_device_addr;
446 else if (msg.device_id)
447 p2p_dev_addr = msg.device_id;
449 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
450 "P2P: Ignore scan data without P2P Device Info or "
452 p2p_parse_free(&msg);
456 if (!is_zero_ether_addr(p2p->peer_filter) &&
457 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
458 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
459 "filter for " MACSTR " due to peer filter",
460 MAC2STR(p2p_dev_addr));
464 dev = p2p_create_device(p2p, p2p_dev_addr);
466 p2p_parse_free(&msg);
469 os_get_time(&dev->last_seen);
470 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
472 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
473 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
475 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
476 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
478 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
479 dev->oper_ssid_len = msg.ssid[1];
482 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
483 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
485 if (*msg.ds_params == 14)
488 ds_freq = 2407 + *msg.ds_params * 5;
489 if (freq != ds_freq) {
490 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
491 "P2P: Update Listen frequency based on DS "
492 "Parameter Set IE: %d -> %d MHz",
498 if (dev->listen_freq && dev->listen_freq != freq) {
499 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
500 "P2P: Update Listen frequency based on scan "
501 "results (" MACSTR " %d -> %d MHz (DS param %d)",
502 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
503 freq, msg.ds_params ? *msg.ds_params : -1);
505 dev->listen_freq = freq;
507 dev->oper_freq = freq;
510 if (msg.pri_dev_type)
511 os_memcpy(dev->info.pri_dev_type, msg.pri_dev_type,
512 sizeof(dev->info.pri_dev_type));
513 os_memcpy(dev->info.device_name, msg.device_name,
514 sizeof(dev->info.device_name));
515 dev->info.config_methods = msg.config_methods ? msg.config_methods :
516 msg.wps_config_methods;
518 if (msg.wps_sec_dev_type_list) {
519 os_memcpy(dev->info.wps_sec_dev_type_list,
520 msg.wps_sec_dev_type_list,
521 msg.wps_sec_dev_type_list_len);
522 dev->info.wps_sec_dev_type_list_len =
523 msg.wps_sec_dev_type_list_len;
526 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
527 wpabuf_free(dev->info.wps_vendor_ext[i]);
528 dev->info.wps_vendor_ext[i] = NULL;
531 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
532 if (msg.wps_vendor_ext[i] == NULL)
534 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
535 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
536 if (dev->info.wps_vendor_ext[i] == NULL)
540 if (msg.capability) {
541 dev->info.dev_capab = msg.capability[0];
542 dev->info.group_capab = msg.capability[1];
545 if (msg.ext_listen_timing) {
546 dev->ext_listen_period = WPA_GET_LE16(msg.ext_listen_timing);
547 dev->ext_listen_interval =
548 WPA_GET_LE16(msg.ext_listen_timing + 2);
551 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq, msg.group_info,
554 p2p_parse_free(&msg);
556 if (p2p_pending_sd_req(p2p, dev))
557 dev->flags |= P2P_DEV_SD_SCHEDULE;
559 if (dev->flags & P2P_DEV_REPORTED)
562 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
563 "P2P: Peer found with Listen frequency %d MHz", freq);
564 if (dev->flags & P2P_DEV_USER_REJECTED) {
565 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
566 "P2P: Do not report rejected device");
570 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
571 !(dev->flags & P2P_DEV_REPORTED_ONCE));
572 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
578 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
582 if (p2p->go_neg_peer == dev)
583 p2p->go_neg_peer = NULL;
584 if (p2p->invite_peer == dev)
585 p2p->invite_peer = NULL;
586 if (p2p->sd_peer == dev)
588 if (p2p->pending_client_disc_go == dev)
589 p2p->pending_client_disc_go = NULL;
591 p2p->cfg->dev_lost(p2p->cfg->cb_ctx, dev->info.p2p_device_addr);
593 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
594 wpabuf_free(dev->info.wps_vendor_ext[i]);
595 dev->info.wps_vendor_ext[i] = NULL;
602 static int p2p_get_next_prog_freq(struct p2p_data *p2p)
604 struct p2p_channels *c;
605 struct p2p_reg_class *cla;
612 c = &p2p->cfg->channels;
613 for (cl = 0; cl < c->reg_classes; cl++) {
614 cla = &c->reg_class[cl];
615 if (cla->reg_class != p2p->last_prog_scan_class)
617 for (ch = 0; ch < cla->channels; ch++) {
618 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
628 /* Start from beginning */
629 reg_class = c->reg_class[0].reg_class;
630 channel = c->reg_class[0].channel[0];
632 /* Pick the next channel */
634 if (ch == cla->channels) {
636 if (cl == c->reg_classes)
640 reg_class = c->reg_class[cl].reg_class;
641 channel = c->reg_class[cl].channel[ch];
644 freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
645 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
646 "channel: reg_class %u channel %u -> %d MHz",
647 reg_class, channel, freq);
648 p2p->last_prog_scan_class = reg_class;
649 p2p->last_prog_scan_chan = channel;
651 if (freq == 2412 || freq == 2437 || freq == 2462)
652 return 0; /* No need to add social channels */
657 static void p2p_search(struct p2p_data *p2p)
660 enum p2p_scan_type type;
662 if (p2p->drv_in_listen) {
663 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
664 "in Listen state - wait for it to end before "
668 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
670 if (p2p->go_neg_peer) {
672 * Only scan the known listen frequency of the peer
673 * during GO Negotiation start.
675 freq = p2p->go_neg_peer->listen_freq;
677 freq = p2p->go_neg_peer->oper_freq;
678 type = P2P_SCAN_SPECIFIC;
679 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
680 "for freq %u (GO Neg)", freq);
681 } else if (p2p->invite_peer) {
683 * Only scan the known listen frequency of the peer
684 * during Invite start.
686 freq = p2p->invite_peer->listen_freq;
688 freq = p2p->invite_peer->oper_freq;
689 type = P2P_SCAN_SPECIFIC;
690 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
691 "for freq %u (Invite)", freq);
692 } else if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
693 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
694 type = P2P_SCAN_SOCIAL_PLUS_ONE;
695 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
696 "(+ freq %u)", freq);
698 type = P2P_SCAN_SOCIAL;
699 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
702 if (p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
703 p2p->num_req_dev_types, p2p->req_dev_types) < 0)
705 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
706 "P2P: Scan request failed");
707 p2p_continue_find(p2p);
709 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
710 p2p->p2p_scan_running = 1;
711 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
712 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
718 static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
720 struct p2p_data *p2p = eloop_ctx;
721 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
726 static int p2p_run_after_scan(struct p2p_data *p2p)
728 struct p2p_device *dev;
729 enum p2p_after_scan op;
731 if (p2p->after_scan_tx) {
733 /* TODO: schedule p2p_run_after_scan to be called from TX
734 * status callback(?) */
735 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
736 "Action frame at p2p_scan completion");
737 ret = p2p->cfg->send_action(p2p->cfg->cb_ctx,
738 p2p->after_scan_tx->freq,
739 p2p->after_scan_tx->dst,
740 p2p->after_scan_tx->src,
741 p2p->after_scan_tx->bssid,
742 (u8 *) (p2p->after_scan_tx + 1),
743 p2p->after_scan_tx->len,
744 p2p->after_scan_tx->wait_time);
745 os_free(p2p->after_scan_tx);
746 p2p->after_scan_tx = NULL;
750 op = p2p->start_after_scan;
751 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
753 case P2P_AFTER_SCAN_NOTHING:
755 case P2P_AFTER_SCAN_LISTEN:
756 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
757 "requested Listen state");
758 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
759 p2p->pending_listen_usec / 1000);
761 case P2P_AFTER_SCAN_CONNECT:
762 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
763 "requested connect with " MACSTR,
764 MAC2STR(p2p->after_scan_peer));
765 dev = p2p_get_device(p2p, p2p->after_scan_peer);
767 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
771 p2p_connect_send(p2p, dev);
779 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
781 struct p2p_data *p2p = eloop_ctx;
783 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
784 "(running=%d)", p2p->p2p_scan_running);
785 running = p2p->p2p_scan_running;
786 /* Make sure we recover from missed scan results callback */
787 p2p->p2p_scan_running = 0;
790 p2p_run_after_scan(p2p);
794 static void p2p_free_req_dev_types(struct p2p_data *p2p)
796 p2p->num_req_dev_types = 0;
797 os_free(p2p->req_dev_types);
798 p2p->req_dev_types = NULL;
802 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
803 enum p2p_discovery_type type,
804 unsigned int num_req_dev_types, const u8 *req_dev_types)
808 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
810 if (p2p->p2p_scan_running) {
811 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
815 p2p_free_req_dev_types(p2p);
816 if (req_dev_types && num_req_dev_types) {
817 p2p->req_dev_types = os_malloc(num_req_dev_types *
819 if (p2p->req_dev_types == NULL)
821 os_memcpy(p2p->req_dev_types, req_dev_types,
822 num_req_dev_types * WPS_DEV_TYPE_LEN);
823 p2p->num_req_dev_types = num_req_dev_types;
826 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
827 p2p_clear_timeout(p2p);
828 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
829 p2p->find_type = type;
830 p2p_device_clear_reported(p2p);
831 p2p_set_state(p2p, P2P_SEARCH);
832 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
834 eloop_register_timeout(timeout, 0, p2p_find_timeout,
837 case P2P_FIND_START_WITH_FULL:
838 case P2P_FIND_PROGRESSIVE:
839 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
840 p2p->num_req_dev_types,
843 case P2P_FIND_ONLY_SOCIAL:
844 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
845 p2p->num_req_dev_types,
853 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
854 p2p->p2p_scan_running = 1;
855 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
856 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
859 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
867 void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
869 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
870 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
871 p2p_clear_timeout(p2p);
872 p2p_set_state(p2p, P2P_IDLE);
873 p2p_free_req_dev_types(p2p);
874 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
875 p2p->go_neg_peer = NULL;
877 p2p->invite_peer = NULL;
878 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
879 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
880 "since we are on correct channel for response");
883 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
887 void p2p_stop_find(struct p2p_data *p2p)
889 p2p_stop_find_for_freq(p2p, 0);
893 static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
896 u8 op_reg_class, op_channel;
897 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
898 &op_reg_class, &op_channel) < 0) {
899 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
900 "P2P: Unsupported frequency %u MHz",
904 if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
906 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
907 "P2P: Frequency %u MHz (oper_class %u "
908 "channel %u) not allowed for P2P",
909 force_freq, op_reg_class, op_channel);
912 p2p->op_reg_class = op_reg_class;
913 p2p->op_channel = op_channel;
914 p2p->channels.reg_classes = 1;
915 p2p->channels.reg_class[0].channels = 1;
916 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
917 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
919 u8 op_reg_class, op_channel;
921 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
922 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
923 p2p_freq_to_channel(p2p->cfg->country,
924 p2p->best_freq_overall,
925 &op_reg_class, &op_channel) == 0) {
926 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
927 "P2P: Select best overall channel as "
928 "operating channel preference");
929 p2p->op_reg_class = op_reg_class;
930 p2p->op_channel = op_channel;
931 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
932 p2p_supported_freq(p2p, p2p->best_freq_5) &&
933 p2p_freq_to_channel(p2p->cfg->country,
935 &op_reg_class, &op_channel) ==
937 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
938 "P2P: Select best 5 GHz channel as "
939 "operating channel preference");
940 p2p->op_reg_class = op_reg_class;
941 p2p->op_channel = op_channel;
942 } else if (!p2p->cfg->cfg_op_channel &&
943 p2p->best_freq_24 > 0 &&
944 p2p_supported_freq(p2p, p2p->best_freq_24) &&
945 p2p_freq_to_channel(p2p->cfg->country,
947 &op_reg_class, &op_channel) ==
949 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
950 "P2P: Select best 2.4 GHz channel as "
951 "operating channel preference");
952 p2p->op_reg_class = op_reg_class;
953 p2p->op_channel = op_channel;
955 p2p->op_reg_class = p2p->cfg->op_reg_class;
956 p2p->op_channel = p2p->cfg->op_channel;
959 os_memcpy(&p2p->channels, &p2p->cfg->channels,
960 sizeof(struct p2p_channels));
962 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
963 "P2P: Own preference for operation channel: "
964 "Operating Class %u Channel %u%s",
965 p2p->op_reg_class, p2p->op_channel,
966 force_freq ? " (forced)" : "");
972 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
973 enum p2p_wps_method wps_method,
974 int go_intent, const u8 *own_interface_addr,
975 unsigned int force_freq, int persistent_group)
977 struct p2p_device *dev;
979 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
980 "P2P: Request to start group negotiation - peer=" MACSTR
981 " GO Intent=%d Intended Interface Address=" MACSTR
982 " wps_method=%d persistent_group=%d",
983 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
984 wps_method, persistent_group);
986 if (p2p_prepare_channel(p2p, force_freq) < 0)
989 dev = p2p_get_device(p2p, peer_addr);
990 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
991 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
992 "P2P: Cannot connect to unknown P2P Device " MACSTR,
997 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
998 if (!(dev->info.dev_capab &
999 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1000 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1001 "P2P: Cannot connect to P2P Device " MACSTR
1002 " that is in a group and is not discoverable",
1003 MAC2STR(peer_addr));
1006 if (dev->oper_freq <= 0) {
1007 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1008 "P2P: Cannot connect to P2P Device " MACSTR
1009 " with incomplete information",
1010 MAC2STR(peer_addr));
1015 * First, try to connect directly. If the peer does not
1016 * acknowledge frames, assume it is sleeping and use device
1017 * discoverability via the GO at that point.
1021 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1022 dev->flags &= ~P2P_DEV_USER_REJECTED;
1023 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1024 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1025 dev->connect_reqs = 0;
1026 dev->go_neg_req_sent = 0;
1027 dev->go_state = UNKNOWN_GO;
1028 if (persistent_group)
1029 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1031 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_GROUP;
1032 p2p->go_intent = go_intent;
1033 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1035 if (p2p->state != P2P_IDLE)
1038 if (p2p->after_scan_tx) {
1040 * We need to drop the pending frame to avoid issues with the
1041 * new GO Negotiation, e.g., when the pending frame was from a
1042 * previous attempt at starting a GO Negotiation.
1044 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
1045 "previous pending Action frame TX that was waiting "
1046 "for p2p_scan completion");
1047 os_free(p2p->after_scan_tx);
1048 p2p->after_scan_tx = NULL;
1051 dev->wps_method = wps_method;
1052 dev->status = P2P_SC_SUCCESS;
1055 dev->flags |= P2P_DEV_FORCE_FREQ;
1057 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1059 if (p2p->p2p_scan_running) {
1060 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1061 "P2P: p2p_scan running - delay connect send");
1062 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1063 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1066 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1068 return p2p_connect_send(p2p, dev);
1072 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1073 enum p2p_wps_method wps_method,
1074 int go_intent, const u8 *own_interface_addr,
1075 unsigned int force_freq, int persistent_group)
1077 struct p2p_device *dev;
1079 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1080 "P2P: Request to authorize group negotiation - peer=" MACSTR
1081 " GO Intent=%d Intended Interface Address=" MACSTR
1082 " wps_method=%d persistent_group=%d",
1083 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1084 wps_method, persistent_group);
1086 if (p2p_prepare_channel(p2p, force_freq) < 0)
1089 dev = p2p_get_device(p2p, peer_addr);
1091 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1092 "P2P: Cannot authorize unknown P2P Device " MACSTR,
1093 MAC2STR(peer_addr));
1097 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1098 dev->flags &= ~P2P_DEV_USER_REJECTED;
1099 dev->go_neg_req_sent = 0;
1100 dev->go_state = UNKNOWN_GO;
1101 if (persistent_group)
1102 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1104 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_GROUP;
1105 p2p->go_intent = go_intent;
1106 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1108 dev->wps_method = wps_method;
1109 dev->status = P2P_SC_SUCCESS;
1112 dev->flags |= P2P_DEV_FORCE_FREQ;
1114 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1120 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1121 struct p2p_device *dev, struct p2p_message *msg)
1123 os_get_time(&dev->last_seen);
1125 if (msg->pri_dev_type)
1126 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
1127 sizeof(dev->info.pri_dev_type));
1128 os_memcpy(dev->info.device_name, msg->device_name,
1129 sizeof(dev->info.device_name));
1130 dev->info.config_methods = msg->config_methods ? msg->config_methods :
1131 msg->wps_config_methods;
1133 if (msg->wps_sec_dev_type_list) {
1134 os_memcpy(dev->info.wps_sec_dev_type_list,
1135 msg->wps_sec_dev_type_list,
1136 msg->wps_sec_dev_type_list_len);
1137 dev->info.wps_sec_dev_type_list_len =
1138 msg->wps_sec_dev_type_list_len;
1141 if (msg->capability) {
1142 dev->info.dev_capab = msg->capability[0];
1143 dev->info.group_capab = msg->capability[1];
1145 if (msg->listen_channel) {
1147 freq = p2p_channel_to_freq((char *) msg->listen_channel,
1148 msg->listen_channel[3],
1149 msg->listen_channel[4]);
1151 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1152 "P2P: Unknown peer Listen channel: "
1153 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1154 msg->listen_channel[0],
1155 msg->listen_channel[1],
1156 msg->listen_channel[2],
1157 msg->listen_channel[3],
1158 msg->listen_channel[4]);
1160 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1161 "peer " MACSTR " Listen channel: %u -> %u MHz",
1162 MAC2STR(dev->info.p2p_device_addr),
1163 dev->listen_freq, freq);
1164 dev->listen_freq = freq;
1167 if (msg->ext_listen_timing) {
1168 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
1169 dev->ext_listen_interval =
1170 WPA_GET_LE16(msg->ext_listen_timing + 2);
1173 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1174 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1175 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1176 "P2P: Completed device entry based on data from "
1177 "GO Negotiation Request");
1179 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1180 "P2P: Created device entry based on GO Neg Req: "
1181 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1183 MAC2STR(dev->info.p2p_device_addr),
1184 dev->info.dev_capab, dev->info.group_capab,
1185 dev->info.device_name, dev->listen_freq);
1188 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1190 if (dev->flags & P2P_DEV_USER_REJECTED) {
1191 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1192 "P2P: Do not report rejected device");
1196 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1197 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1198 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1202 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1204 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1205 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1206 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1207 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1208 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1212 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1214 p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len);
1215 p2p_random(params->passphrase, 8);
1220 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1222 struct p2p_go_neg_results res;
1223 int go = peer->go_state == LOCAL_GO;
1224 struct p2p_channels intersection;
1228 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1229 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1230 "GO)", MAC2STR(peer->info.p2p_device_addr),
1231 go ? "local end" : "peer");
1233 os_memset(&res, 0, sizeof(res));
1235 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1236 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1237 res.wps_method = peer->wps_method;
1238 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
1239 res.persistent_group = 1;
1242 /* Setup AP mode for WPS provisioning */
1243 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1246 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1247 res.ssid_len = p2p->ssid_len;
1248 p2p_random(res.passphrase, 8);
1250 res.freq = peer->oper_freq;
1251 if (p2p->ssid_len) {
1252 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1253 res.ssid_len = p2p->ssid_len;
1257 p2p_channels_intersect(&p2p->channels, &peer->channels,
1260 for (i = 0; i < intersection.reg_classes; i++) {
1261 struct p2p_reg_class *c = &intersection.reg_class[i];
1262 if (freqs + 1 == P2P_MAX_CHANNELS)
1264 for (j = 0; j < c->channels; j++) {
1266 if (freqs + 1 == P2P_MAX_CHANNELS)
1268 freq = p2p_channel_to_freq(peer->country, c->reg_class,
1272 res.freq_list[freqs++] = freq;
1276 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1278 p2p_clear_timeout(p2p);
1279 peer->go_neg_req_sent = 0;
1280 peer->wps_method = WPS_NOT_READY;
1282 p2p_set_state(p2p, P2P_PROVISIONING);
1283 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1287 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1288 const u8 *data, size_t len, int rx_freq)
1290 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1291 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1292 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1298 case P2P_GO_NEG_REQ:
1299 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1301 case P2P_GO_NEG_RESP:
1302 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1304 case P2P_GO_NEG_CONF:
1305 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1307 case P2P_INVITATION_REQ:
1308 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1311 case P2P_INVITATION_RESP:
1312 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1314 case P2P_PROV_DISC_REQ:
1315 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1317 case P2P_PROV_DISC_RESP:
1318 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1320 case P2P_DEV_DISC_REQ:
1321 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1323 case P2P_DEV_DISC_RESP:
1324 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1327 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1328 "P2P: Unsupported P2P Public Action frame type %d",
1335 void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1336 const u8 *bssid, const u8 *data, size_t len,
1343 case WLAN_PA_VENDOR_SPECIFIC:
1348 if (WPA_GET_BE24(data) != OUI_WFA)
1356 if (*data != P2P_OUI_TYPE)
1359 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1361 case WLAN_PA_GAS_INITIAL_REQ:
1362 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1364 case WLAN_PA_GAS_INITIAL_RESP:
1365 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1367 case WLAN_PA_GAS_COMEBACK_REQ:
1368 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1370 case WLAN_PA_GAS_COMEBACK_RESP:
1371 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1377 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1378 const u8 *bssid, u8 category,
1379 const u8 *data, size_t len, int freq)
1381 if (category == WLAN_ACTION_PUBLIC) {
1382 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1386 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1392 if (WPA_GET_BE24(data) != OUI_WFA)
1397 if (*data != P2P_OUI_TYPE)
1402 /* P2P action frame */
1403 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1404 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1405 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1411 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1412 "P2P: Received P2P Action - Notice of Absence");
1415 case P2P_PRESENCE_REQ:
1416 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1418 case P2P_PRESENCE_RESP:
1419 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1421 case P2P_GO_DISC_REQ:
1422 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1425 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1426 "P2P: Received P2P Action - unknown type %u", data[0]);
1432 static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1434 struct p2p_data *p2p = eloop_ctx;
1435 if (p2p->go_neg_peer == NULL)
1437 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1438 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1439 p2p_connect_send(p2p, p2p->go_neg_peer);
1443 static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1445 struct p2p_data *p2p = eloop_ctx;
1446 if (p2p->invite_peer == NULL)
1448 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1449 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1453 static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1454 const u8 *ie, size_t ie_len)
1456 struct p2p_message msg;
1457 struct p2p_device *dev;
1459 os_memset(&msg, 0, sizeof(msg));
1460 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1462 p2p_parse_free(&msg);
1463 return; /* not a P2P probe */
1466 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1467 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1469 /* The Probe Request is not part of P2P Device Discovery. It is
1470 * not known whether the source address of the frame is the P2P
1471 * Device Address or P2P Interface Address. Do not add a new
1472 * peer entry based on this frames.
1474 p2p_parse_free(&msg);
1478 dev = p2p_get_device(p2p, addr);
1480 if (dev->country[0] == 0 && msg.listen_channel)
1481 os_memcpy(dev->country, msg.listen_channel, 3);
1482 p2p_parse_free(&msg);
1483 return; /* already known */
1486 dev = p2p_create_device(p2p, addr);
1488 p2p_parse_free(&msg);
1492 os_get_time(&dev->last_seen);
1493 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1495 if (msg.capability) {
1496 dev->info.dev_capab = msg.capability[0];
1497 dev->info.group_capab = msg.capability[1];
1500 if (msg.listen_channel) {
1501 os_memcpy(dev->country, msg.listen_channel, 3);
1502 dev->listen_freq = p2p_channel_to_freq(dev->country,
1503 msg.listen_channel[3],
1504 msg.listen_channel[4]);
1507 os_memcpy(dev->info.device_name, msg.device_name,
1508 sizeof(dev->info.device_name));
1510 if (msg.wps_pri_dev_type)
1511 os_memcpy(dev->info.pri_dev_type, msg.wps_pri_dev_type,
1512 sizeof(dev->info.pri_dev_type));
1514 if (msg.wps_sec_dev_type_list) {
1515 os_memcpy(dev->info.wps_sec_dev_type_list,
1516 msg.wps_sec_dev_type_list,
1517 msg.wps_sec_dev_type_list_len);
1518 dev->info.wps_sec_dev_type_list_len =
1519 msg.wps_sec_dev_type_list_len;
1522 p2p_parse_free(&msg);
1524 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1525 "P2P: Created device entry based on Probe Req: " MACSTR
1526 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1527 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1528 dev->info.group_capab, dev->info.device_name,
1533 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1535 struct p2p_message *msg)
1537 struct p2p_device *dev;
1539 dev = p2p_get_device(p2p, addr);
1541 os_get_time(&dev->last_seen);
1542 return dev; /* already known */
1545 dev = p2p_create_device(p2p, addr);
1549 p2p_add_dev_info(p2p, addr, dev, msg);
1555 static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1557 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1559 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1560 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1561 WPA_GET_BE16(&req_dev_type[6]) == 0)
1562 return 1; /* Category match with wildcard OUI/sub-category */
1567 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1568 size_t num_req_dev_type)
1571 for (i = 0; i < num_req_dev_type; i++) {
1572 if (dev_type_match(dev_type, req_dev_type[i]))
1580 * p2p_match_dev_type - Match local device type with requested type
1581 * @p2p: P2P module context from p2p_init()
1582 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1583 * Returns: 1 on match, 0 on mismatch
1585 * This function can be used to match the Requested Device Type attribute in
1586 * WPS IE with the local device types for deciding whether to reply to a Probe
1589 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1591 struct wps_parse_attr attr;
1594 if (wps_parse_msg(wps, &attr))
1595 return 1; /* assume no Requested Device Type attributes */
1597 if (attr.num_req_dev_type == 0)
1598 return 1; /* no Requested Device Type attributes -> match */
1600 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1601 attr.num_req_dev_type))
1602 return 1; /* Own Primary Device Type matches */
1604 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1605 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1607 attr.num_req_dev_type))
1608 return 1; /* Own Secondary Device Type matches */
1610 /* No matching device type found */
1615 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1620 buf = wpabuf_alloc(1000);
1624 p2p_build_wps_ie(p2p, buf, DEV_PW_DEFAULT, 1);
1627 len = p2p_buf_add_ie_hdr(buf);
1628 p2p_buf_add_capability(buf, p2p->dev_capab, 0);
1629 if (p2p->ext_listen_interval)
1630 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1631 p2p->ext_listen_interval);
1632 p2p_buf_add_device_info(buf, p2p, NULL);
1633 p2p_buf_update_ie_hdr(buf, len);
1639 static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *ie,
1642 struct ieee802_11_elems elems;
1644 struct ieee80211_mgmt *resp;
1648 if (!p2p->in_listen || !p2p->drv_in_listen) {
1649 /* not in Listen state - ignore Probe Request */
1653 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
1655 /* Ignore invalid Probe Request frames */
1659 if (elems.p2p == NULL) {
1660 /* not a P2P probe - ignore it */
1664 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
1665 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
1667 /* not using P2P Wildcard SSID - ignore */
1671 /* Check Requested Device Type match */
1672 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1673 if (wps && !p2p_match_dev_type(p2p, wps)) {
1675 /* No match with Requested Device Type */
1680 if (!p2p->cfg->send_probe_resp)
1681 return; /* Response generated elsewhere */
1683 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1684 "P2P: Reply to P2P Probe Request in Listen state");
1687 * We do not really have a specific BSS that this frame is advertising,
1688 * so build a frame that has some information in valid format. This is
1689 * really only used for discovery purposes, not to learn exact BSS
1692 ies = p2p_build_probe_resp_ies(p2p);
1696 buf = wpabuf_alloc(200 + wpabuf_len(ies));
1703 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
1705 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
1706 (WLAN_FC_STYPE_PROBE_RESP << 4));
1707 os_memcpy(resp->da, addr, ETH_ALEN);
1708 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
1709 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
1710 resp->u.probe_resp.beacon_int = host_to_le16(100);
1711 /* hardware or low-level driver will setup seq_ctrl and timestamp */
1712 resp->u.probe_resp.capab_info =
1713 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
1714 WLAN_CAPABILITY_PRIVACY |
1715 WLAN_CAPABILITY_SHORT_SLOT_TIME);
1717 wpabuf_put_u8(buf, WLAN_EID_SSID);
1718 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
1719 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1721 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
1722 wpabuf_put_u8(buf, 8);
1723 wpabuf_put_u8(buf, (60 / 5) | 0x80);
1724 wpabuf_put_u8(buf, 90 / 5);
1725 wpabuf_put_u8(buf, (120 / 5) | 0x80);
1726 wpabuf_put_u8(buf, 180 / 5);
1727 wpabuf_put_u8(buf, (240 / 5) | 0x80);
1728 wpabuf_put_u8(buf, 360 / 5);
1729 wpabuf_put_u8(buf, 480 / 5);
1730 wpabuf_put_u8(buf, 540 / 5);
1732 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
1733 wpabuf_put_u8(buf, 1);
1734 wpabuf_put_u8(buf, p2p->cfg->channel);
1736 wpabuf_put_buf(buf, ies);
1739 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
1745 int p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *ie,
1748 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
1750 p2p_reply_probe(p2p, addr, ie, ie_len);
1752 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
1754 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
1756 /* Received a Probe Request from GO Negotiation peer */
1757 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1758 "P2P: Found GO Negotiation peer - try to start GO "
1759 "negotiation from timeout");
1760 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
1764 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
1766 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
1768 /* Received a Probe Request from Invite peer */
1769 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1770 "P2P: Found Invite peer - try to start Invite from "
1772 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
1780 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
1781 u8 *buf, size_t len, struct wpabuf *p2p_ie)
1790 return 0; /* WLAN AP is not a P2P manager */
1793 * (Re)Association Request - P2P IE
1794 * P2P Capability attribute (shall be present)
1795 * P2P Interface attribute (present if concurrent device and
1796 * P2P Management is enabled)
1798 tmp = wpabuf_alloc(200);
1802 lpos = p2p_buf_add_ie_hdr(tmp);
1804 if (p2p->num_groups > 0) {
1805 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
1806 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1807 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
1809 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
1811 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
1812 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1813 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
1814 p2p_buf_add_p2p_interface(tmp, p2p);
1815 p2p_buf_update_ie_hdr(tmp, lpos);
1817 tmplen = wpabuf_len(tmp);
1821 os_memcpy(buf, wpabuf_head(tmp), tmplen);
1830 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
1831 size_t len, int p2p_group, struct wpabuf *p2p_ie)
1835 struct p2p_device *peer;
1840 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
1843 * (Re)Association Request - P2P IE
1844 * P2P Capability attribute (shall be present)
1845 * Extended Listen Timing (may be present)
1846 * P2P Device Info attribute (shall be present)
1848 tmp = wpabuf_alloc(200);
1852 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
1854 lpos = p2p_buf_add_ie_hdr(tmp);
1855 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
1856 if (p2p->ext_listen_interval)
1857 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
1858 p2p->ext_listen_interval);
1859 p2p_buf_add_device_info(tmp, p2p, peer);
1860 p2p_buf_update_ie_hdr(tmp, lpos);
1862 tmplen = wpabuf_len(tmp);
1866 os_memcpy(buf, wpabuf_head(tmp), tmplen);
1875 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
1877 struct wpabuf *p2p_ie;
1880 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
1884 ret = p2p_attr_text(p2p_ie, buf, end);
1885 wpabuf_free(p2p_ie);
1890 static void p2p_clear_go_neg(struct p2p_data *p2p)
1892 p2p->go_neg_peer = NULL;
1893 p2p_clear_timeout(p2p);
1894 p2p_set_state(p2p, P2P_IDLE);
1898 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
1900 if (p2p->go_neg_peer == NULL) {
1901 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1902 "P2P: No pending Group Formation - "
1903 "ignore WPS registration success notification");
1904 return; /* No pending Group Formation */
1907 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
1909 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1910 "P2P: Ignore WPS registration success notification "
1911 "for " MACSTR " (GO Negotiation peer " MACSTR ")",
1913 MAC2STR(p2p->go_neg_peer->intended_addr));
1914 return; /* Ignore unexpected peer address */
1917 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1918 "P2P: Group Formation completed successfully with " MACSTR,
1921 p2p_clear_go_neg(p2p);
1925 void p2p_group_formation_failed(struct p2p_data *p2p)
1927 if (p2p->go_neg_peer == NULL) {
1928 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1929 "P2P: No pending Group Formation - "
1930 "ignore group formation failure notification");
1931 return; /* No pending Group Formation */
1934 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1935 "P2P: Group Formation failed with " MACSTR,
1936 MAC2STR(p2p->go_neg_peer->intended_addr));
1938 p2p_clear_go_neg(p2p);
1942 struct p2p_data * p2p_init(const struct p2p_config *cfg)
1944 struct p2p_data *p2p;
1946 if (cfg->max_peers < 1)
1949 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
1952 p2p->cfg = (struct p2p_config *) (p2p + 1);
1953 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
1955 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
1956 if (cfg->manufacturer)
1957 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
1958 if (cfg->model_name)
1959 p2p->cfg->model_name = os_strdup(cfg->model_name);
1960 if (cfg->model_number)
1961 p2p->cfg->model_number = os_strdup(cfg->model_number);
1962 if (cfg->serial_number)
1963 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
1965 p2p->min_disc_int = 1;
1966 p2p->max_disc_int = 3;
1968 os_get_random(&p2p->next_tie_breaker, 1);
1969 p2p->next_tie_breaker &= 0x01;
1970 if (cfg->sd_request)
1971 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
1972 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
1973 if (cfg->concurrent_operations)
1974 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
1975 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
1977 dl_list_init(&p2p->devices);
1979 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
1980 p2p_expiration_timeout, p2p, NULL);
1986 void p2p_deinit(struct p2p_data *p2p)
1988 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
1989 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
1990 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1992 p2p_free_req_dev_types(p2p);
1993 os_free(p2p->cfg->dev_name);
1994 os_free(p2p->cfg->manufacturer);
1995 os_free(p2p->cfg->model_name);
1996 os_free(p2p->cfg->model_number);
1997 os_free(p2p->cfg->serial_number);
1998 os_free(p2p->groups);
1999 wpabuf_free(p2p->sd_resp);
2000 os_free(p2p->after_scan_tx);
2001 p2p_remove_wps_vendor_extensions(p2p);
2006 void p2p_flush(struct p2p_data *p2p)
2008 struct p2p_device *dev, *prev;
2009 p2p_clear_timeout(p2p);
2010 p2p_set_state(p2p, P2P_IDLE);
2011 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
2012 p2p->go_neg_peer = NULL;
2013 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
2014 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2016 dl_list_del(&dev->list);
2017 p2p_device_free(p2p, dev);
2019 p2p_free_sd_queries(p2p);
2020 os_free(p2p->after_scan_tx);
2021 p2p->after_scan_tx = NULL;
2025 int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2027 struct p2p_device *dev;
2029 dev = p2p_get_device(p2p, addr);
2033 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
2036 if (p2p->go_neg_peer == dev)
2037 p2p->go_neg_peer = NULL;
2039 dev->wps_method = WPS_NOT_READY;
2040 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2041 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2043 /* Check if after_scan_tx is for this peer. If so free it */
2044 if (p2p->after_scan_tx &&
2045 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2046 os_free(p2p->after_scan_tx);
2047 p2p->after_scan_tx = NULL;
2054 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2056 os_free(p2p->cfg->dev_name);
2058 p2p->cfg->dev_name = os_strdup(dev_name);
2059 if (p2p->cfg->dev_name == NULL)
2062 p2p->cfg->dev_name = NULL;
2067 int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2069 os_free(p2p->cfg->manufacturer);
2070 p2p->cfg->manufacturer = NULL;
2072 p2p->cfg->manufacturer = os_strdup(manufacturer);
2073 if (p2p->cfg->manufacturer == NULL)
2081 int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2083 os_free(p2p->cfg->model_name);
2084 p2p->cfg->model_name = NULL;
2086 p2p->cfg->model_name = os_strdup(model_name);
2087 if (p2p->cfg->model_name == NULL)
2095 int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2097 os_free(p2p->cfg->model_number);
2098 p2p->cfg->model_number = NULL;
2100 p2p->cfg->model_number = os_strdup(model_number);
2101 if (p2p->cfg->model_number == NULL)
2109 int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2111 os_free(p2p->cfg->serial_number);
2112 p2p->cfg->serial_number = NULL;
2113 if (serial_number) {
2114 p2p->cfg->serial_number = os_strdup(serial_number);
2115 if (p2p->cfg->serial_number == NULL)
2123 void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2125 p2p->cfg->config_methods = config_methods;
2129 void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2131 os_memcpy(p2p->cfg->uuid, uuid, 16);
2135 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2137 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2142 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2143 size_t num_dev_types)
2145 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2146 num_dev_types = P2P_SEC_DEVICE_TYPES;
2147 p2p->cfg->num_sec_dev_types = num_dev_types;
2148 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2153 void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2157 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2158 wpabuf_free(p2p->wps_vendor_ext[i]);
2159 p2p->wps_vendor_ext[i] = NULL;
2164 int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2165 const struct wpabuf *vendor_ext)
2169 if (vendor_ext == NULL)
2172 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2173 if (p2p->wps_vendor_ext[i] == NULL)
2176 if (i >= P2P_MAX_WPS_VENDOR_EXT)
2179 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2180 if (p2p->wps_vendor_ext[i] == NULL)
2187 int p2p_set_country(struct p2p_data *p2p, const char *country)
2189 os_memcpy(p2p->cfg->country, country, 3);
2194 void p2p_continue_find(struct p2p_data *p2p)
2196 struct p2p_device *dev;
2197 p2p_set_state(p2p, P2P_SEARCH);
2198 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2199 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2200 if (p2p_start_sd(p2p, dev) == 0)
2204 } else if (dev->req_config_methods &&
2205 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2206 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2207 "pending Provisioning Discovery Request to "
2208 MACSTR " (config methods 0x%x)",
2209 MAC2STR(dev->info.p2p_device_addr),
2210 dev->req_config_methods);
2211 if (p2p_send_prov_disc_req(p2p, dev, 0) == 0)
2216 p2p_listen_in_find(p2p);
2220 static void p2p_sd_cb(struct p2p_data *p2p, int success)
2222 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2223 "P2P: Service Discovery Query TX callback: success=%d",
2225 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2229 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2230 p2p->sd_peer = NULL;
2232 p2p_continue_find(p2p);
2236 if (p2p->sd_peer == NULL) {
2237 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2238 "P2P: No SD peer entry known");
2239 p2p_continue_find(p2p);
2243 /* Wait for response from the peer */
2244 p2p_set_state(p2p, P2P_SD_DURING_FIND);
2245 p2p_set_timeout(p2p, 0, 200000);
2248 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2250 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2251 "P2P: Provision Discovery Request TX callback: success=%d",
2253 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2256 if (p2p->state != P2P_IDLE)
2257 p2p_continue_find(p2p);
2261 /* Wait for response from the peer */
2262 if (p2p->state == P2P_SEARCH)
2263 p2p_set_state(p2p, P2P_PD_DURING_FIND);
2264 p2p_set_timeout(p2p, 0, 200000);
2268 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
2269 int level, const u8 *ies, size_t ies_len)
2271 p2p_add_device(p2p, bssid, freq, level, ies, ies_len);
2273 if (p2p->go_neg_peer && p2p->state == P2P_SEARCH &&
2274 os_memcmp(p2p->go_neg_peer->info.p2p_device_addr, bssid, ETH_ALEN)
2276 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2277 "P2P: Found GO Negotiation peer - try to start GO "
2279 p2p_connect_send(p2p, p2p->go_neg_peer);
2287 void p2p_scan_res_handled(struct p2p_data *p2p)
2289 if (!p2p->p2p_scan_running) {
2290 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
2291 "running, but scan results received");
2293 p2p->p2p_scan_running = 0;
2294 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2296 if (p2p_run_after_scan(p2p))
2298 if (p2p->state == P2P_SEARCH)
2299 p2p_continue_find(p2p);
2303 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies)
2305 u8 *len = p2p_buf_add_ie_hdr(ies);
2306 p2p_buf_add_capability(ies, p2p->dev_capab, 0);
2307 if (p2p->cfg->reg_class && p2p->cfg->channel)
2308 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2309 p2p->cfg->reg_class,
2311 if (p2p->ext_listen_interval)
2312 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2313 p2p->ext_listen_interval);
2314 /* TODO: p2p_buf_add_operating_channel() if GO */
2315 p2p_buf_update_ie_hdr(ies, len);
2319 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2321 return p2p_attr_text(p2p_ie, buf, end);
2325 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2327 struct p2p_device *dev = p2p->go_neg_peer;
2329 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2330 "P2P: GO Negotiation Request TX callback: success=%d",
2334 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2335 "P2P: No pending GO Negotiation");
2340 dev->go_neg_req_sent++;
2341 if (dev->flags & P2P_DEV_USER_REJECTED) {
2342 p2p_set_state(p2p, P2P_IDLE);
2348 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2349 !is_zero_ether_addr(dev->member_in_go_dev)) {
2350 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2351 "P2P: Peer " MACSTR " did not acknowledge request - "
2352 "try to use device discoverability through its GO",
2353 MAC2STR(dev->info.p2p_device_addr));
2354 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2355 p2p_send_dev_disc_req(p2p, dev);
2360 * Use P2P find, if needed, to find the other device from its listen
2363 p2p_set_state(p2p, P2P_CONNECT);
2364 p2p_set_timeout(p2p, 0, 100000);
2368 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2370 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2371 "P2P: GO Negotiation Response TX callback: success=%d",
2373 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2374 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2375 "P2P: Ignore TX callback event - GO Negotiation is "
2376 "not running anymore");
2379 p2p_set_state(p2p, P2P_CONNECT);
2380 p2p_set_timeout(p2p, 0, 100000);
2384 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2386 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2387 "P2P: GO Negotiation Response (failure) TX callback: "
2388 "success=%d", success);
2389 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2390 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2391 p2p->go_neg_peer->status);
2396 static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
2397 enum p2p_send_action_result result)
2399 struct p2p_device *dev;
2401 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2402 "P2P: GO Negotiation Confirm TX callback: result=%d",
2404 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2405 if (result == P2P_SEND_ACTION_FAILED) {
2406 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2409 if (result == P2P_SEND_ACTION_NO_ACK) {
2411 * It looks like the TX status for GO Negotiation Confirm is
2412 * often showing failure even when the peer has actually
2413 * received the frame. Since the peer may change channels
2414 * immediately after having received the frame, we may not see
2415 * an Ack for retries, so just dropping a single frame may
2416 * trigger this. To allow the group formation to succeed if the
2417 * peer did indeed receive the frame, continue regardless of
2420 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2421 "P2P: Assume GO Negotiation Confirm TX was actually "
2422 "received by the peer even though Ack was not "
2426 dev = p2p->go_neg_peer;
2430 p2p_go_complete(p2p, dev);
2434 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
2435 const u8 *src, const u8 *bssid,
2436 enum p2p_send_action_result result)
2438 enum p2p_pending_action_state state;
2441 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2442 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
2443 " src=" MACSTR " bssid=" MACSTR " result=%d",
2444 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
2445 MAC2STR(bssid), result);
2446 success = result == P2P_SEND_ACTION_SUCCESS;
2447 state = p2p->pending_action_state;
2448 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2450 case P2P_NO_PENDING_ACTION:
2452 case P2P_PENDING_GO_NEG_REQUEST:
2453 p2p_go_neg_req_cb(p2p, success);
2455 case P2P_PENDING_GO_NEG_RESPONSE:
2456 p2p_go_neg_resp_cb(p2p, success);
2458 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
2459 p2p_go_neg_resp_failure_cb(p2p, success);
2461 case P2P_PENDING_GO_NEG_CONFIRM:
2462 p2p_go_neg_conf_cb(p2p, result);
2464 case P2P_PENDING_SD:
2465 p2p_sd_cb(p2p, success);
2467 case P2P_PENDING_PD:
2468 p2p_prov_disc_cb(p2p, success);
2470 case P2P_PENDING_INVITATION_REQUEST:
2471 p2p_invitation_req_cb(p2p, success);
2473 case P2P_PENDING_INVITATION_RESPONSE:
2474 p2p_invitation_resp_cb(p2p, success);
2476 case P2P_PENDING_DEV_DISC_REQUEST:
2477 p2p_dev_disc_req_cb(p2p, success);
2479 case P2P_PENDING_DEV_DISC_RESPONSE:
2480 p2p_dev_disc_resp_cb(p2p, success);
2482 case P2P_PENDING_GO_DISC_REQ:
2483 p2p_go_disc_req_cb(p2p, success);
2489 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
2490 unsigned int duration)
2492 if (freq == p2p->pending_client_disc_freq) {
2493 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2494 "P2P: Client discoverability remain-awake completed");
2495 p2p->pending_client_disc_freq = 0;
2499 if (freq != p2p->pending_listen_freq) {
2500 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2501 "P2P: Unexpected listen callback for freq=%u "
2502 "duration=%u (pending_listen_freq=%u)",
2503 freq, duration, p2p->pending_listen_freq);
2507 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2508 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
2510 p2p->pending_listen_sec, p2p->pending_listen_usec,
2511 p2p->pending_listen_freq);
2513 p2p->drv_in_listen = freq;
2514 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
2516 * Add 20 msec extra wait to avoid race condition with driver
2517 * remain-on-channel end event, i.e., give driver more time to
2518 * complete the operation before our timeout expires.
2520 p2p_set_timeout(p2p, p2p->pending_listen_sec,
2521 p2p->pending_listen_usec + 20000);
2524 p2p->pending_listen_freq = 0;
2528 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
2530 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
2531 "state (freq=%u)", freq);
2532 p2p->drv_in_listen = 0;
2534 return 0; /* Internal timeout will trigger the next step */
2536 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
2537 if (p2p->go_neg_peer->connect_reqs >= 120) {
2538 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2539 "P2P: Timeout on sending GO Negotiation "
2540 "Request without getting response");
2541 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2545 p2p_set_state(p2p, P2P_CONNECT);
2546 p2p_connect_send(p2p, p2p->go_neg_peer);
2548 } else if (p2p->state == P2P_SEARCH) {
2557 static void p2p_timeout_connect(struct p2p_data *p2p)
2559 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2560 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
2561 p2p_listen_in_find(p2p);
2565 static void p2p_timeout_connect_listen(struct p2p_data *p2p)
2567 if (p2p->go_neg_peer) {
2568 if (p2p->drv_in_listen) {
2569 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
2570 "still in Listen state; wait for it to "
2575 if (p2p->go_neg_peer->connect_reqs >= 120) {
2576 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2577 "P2P: Timeout on sending GO Negotiation "
2578 "Request without getting response");
2579 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2583 p2p_set_state(p2p, P2P_CONNECT);
2584 p2p_connect_send(p2p, p2p->go_neg_peer);
2586 p2p_set_state(p2p, P2P_IDLE);
2590 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
2593 * TODO: could remain constantly in Listen state for some time if there
2594 * are no other concurrent uses for the radio. For now, go to listen
2595 * state once per second to give other uses a chance to use the radio.
2597 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
2598 p2p_set_timeout(p2p, 1, 0);
2602 static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
2604 struct p2p_device *dev = p2p->go_neg_peer;
2607 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2608 "P2P: Unknown GO Neg peer - stop GO Neg wait");
2613 if (dev->wait_count >= 120) {
2614 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2615 "P2P: Timeout on waiting peer to become ready for GO "
2617 p2p_go_neg_failed(p2p, dev, -1);
2621 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2622 "P2P: Go to Listen state while waiting for the peer to become "
2623 "ready for GO Negotiation");
2624 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
2625 p2p_listen_in_find(p2p);
2629 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
2631 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2632 "P2P: Service Discovery Query timeout");
2634 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2635 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2636 p2p->sd_peer = NULL;
2638 p2p_continue_find(p2p);
2642 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
2644 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2645 "P2P: Provision Discovery Request timeout");
2646 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2647 p2p_continue_find(p2p);
2651 static void p2p_timeout_invite(struct p2p_data *p2p)
2653 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2654 p2p_set_state(p2p, P2P_INVITE_LISTEN);
2655 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
2657 * Better remain on operating channel instead of listen channel
2658 * when running a group.
2660 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
2661 "active GO role - wait on operating channel");
2662 p2p_set_timeout(p2p, 0, 100000);
2665 p2p_listen_in_find(p2p);
2669 static void p2p_timeout_invite_listen(struct p2p_data *p2p)
2671 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
2672 p2p_set_state(p2p, P2P_INVITE);
2673 p2p_invite_send(p2p, p2p->invite_peer,
2674 p2p->invite_go_dev_addr);
2676 if (p2p->invite_peer) {
2677 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2678 "P2P: Invitation Request retry limit reached");
2679 if (p2p->cfg->invitation_result)
2680 p2p->cfg->invitation_result(
2681 p2p->cfg->cb_ctx, -1, NULL);
2683 p2p_set_state(p2p, P2P_IDLE);
2688 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
2690 struct p2p_data *p2p = eloop_ctx;
2692 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
2693 p2p_state_txt(p2p->state));
2697 switch (p2p->state) {
2704 p2p_timeout_connect(p2p);
2706 case P2P_CONNECT_LISTEN:
2707 p2p_timeout_connect_listen(p2p);
2711 case P2P_LISTEN_ONLY:
2712 if (p2p->ext_listen_only) {
2713 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2714 "P2P: Extended Listen Timing - Listen State "
2716 p2p->ext_listen_only = 0;
2717 p2p_set_state(p2p, P2P_IDLE);
2720 case P2P_WAIT_PEER_CONNECT:
2721 p2p_timeout_wait_peer_connect(p2p);
2723 case P2P_WAIT_PEER_IDLE:
2724 p2p_timeout_wait_peer_idle(p2p);
2726 case P2P_SD_DURING_FIND:
2727 p2p_timeout_sd_during_find(p2p);
2729 case P2P_PROVISIONING:
2731 case P2P_PD_DURING_FIND:
2732 p2p_timeout_prov_disc_during_find(p2p);
2735 p2p_timeout_invite(p2p);
2737 case P2P_INVITE_LISTEN:
2738 p2p_timeout_invite_listen(p2p);
2744 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
2746 struct p2p_device *dev;
2748 dev = p2p_get_device(p2p, peer_addr);
2749 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
2750 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
2752 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
2753 " unknown", MAC2STR(peer_addr));
2756 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
2757 dev->flags |= P2P_DEV_USER_REJECTED;
2762 static const char * p2p_wps_method_text(enum p2p_wps_method method)
2769 case WPS_PIN_DISPLAY:
2771 case WPS_PIN_KEYPAD:
2781 static const char * p2p_go_state_text(enum p2p_go_state go_state)
2796 int p2p_get_peer_info(struct p2p_data *p2p, const u8 *addr, int next,
2797 char *buf, size_t buflen)
2799 struct p2p_device *dev;
2803 char devtype[WPS_DEV_TYPE_BUFSIZE];
2806 dev = p2p_get_device(p2p, addr);
2808 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
2811 dev = dl_list_first(&dev->list, struct p2p_device, list);
2812 if (&dev->list == &p2p->devices)
2822 res = os_snprintf(pos, end - pos, MACSTR "\n",
2823 MAC2STR(dev->info.p2p_device_addr));
2824 if (res < 0 || res >= end - pos)
2829 res = os_snprintf(pos, end - pos,
2834 "interface_addr=" MACSTR "\n"
2835 "member_in_go_dev=" MACSTR "\n"
2836 "member_in_go_iface=" MACSTR "\n"
2839 "config_methods=0x%x\n"
2841 "group_capab=0x%x\n"
2842 "go_neg_req_sent=%d\n"
2845 "intended_addr=" MACSTR "\n"
2848 "req_config_methods=0x%x\n"
2849 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
2852 "invitation_reqs=%u\n",
2853 (int) (now.sec - dev->last_seen.sec),
2856 p2p_wps_method_text(dev->wps_method),
2857 MAC2STR(dev->interface_addr),
2858 MAC2STR(dev->member_in_go_dev),
2859 MAC2STR(dev->member_in_go_iface),
2860 wps_dev_type_bin2str(dev->info.pri_dev_type,
2861 devtype, sizeof(devtype)),
2862 dev->info.device_name,
2863 dev->info.config_methods,
2864 dev->info.dev_capab,
2865 dev->info.group_capab,
2866 dev->go_neg_req_sent,
2867 p2p_go_state_text(dev->go_state),
2869 MAC2STR(dev->intended_addr),
2870 dev->country[0] ? dev->country[0] : '_',
2871 dev->country[1] ? dev->country[1] : '_',
2873 dev->req_config_methods,
2874 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
2875 "[PROBE_REQ_ONLY]" : "",
2876 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
2877 dev->flags & P2P_DEV_NOT_YET_READY ?
2878 "[NOT_YET_READY]" : "",
2879 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
2880 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
2882 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
2883 "[PD_PEER_DISPLAY]" : "",
2884 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
2885 "[PD_PEER_KEYPAD]" : "",
2886 dev->flags & P2P_DEV_USER_REJECTED ?
2887 "[USER_REJECTED]" : "",
2888 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
2889 "[PEER_WAITING_RESPONSE]" : "",
2890 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
2891 "[PREFER_PERSISTENT_GROUP]" : "",
2892 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
2893 "[WAIT_GO_NEG_RESPONSE]" : "",
2894 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
2895 "[WAIT_GO_NEG_CONFIRM]" : "",
2896 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
2897 "[GROUP_CLIENT_ONLY]" : "",
2898 dev->flags & P2P_DEV_FORCE_FREQ ?
2899 "[FORCE_FREQ]" : "",
2900 dev->flags & P2P_DEV_PD_FOR_JOIN ?
2901 "[PD_FOR_JOIN]" : "",
2904 dev->invitation_reqs);
2905 if (res < 0 || res >= end - pos)
2909 if (dev->ext_listen_period) {
2910 res = os_snprintf(pos, end - pos,
2911 "ext_listen_period=%u\n"
2912 "ext_listen_interval=%u\n",
2913 dev->ext_listen_period,
2914 dev->ext_listen_interval);
2915 if (res < 0 || res >= end - pos)
2920 if (dev->oper_ssid_len) {
2921 res = os_snprintf(pos, end - pos,
2923 wpa_ssid_txt(dev->oper_ssid,
2924 dev->oper_ssid_len));
2925 if (res < 0 || res >= end - pos)
2934 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
2937 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
2938 "discoverability enabled");
2939 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2941 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
2942 "discoverability disabled");
2943 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2948 static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
2949 u32 duration2, u32 interval2)
2952 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
2955 req = wpabuf_alloc(100);
2959 if (duration1 || interval1) {
2960 os_memset(&desc1, 0, sizeof(desc1));
2961 desc1.count_type = 1;
2962 desc1.duration = duration1;
2963 desc1.interval = interval1;
2966 if (duration2 || interval2) {
2967 os_memset(&desc2, 0, sizeof(desc2));
2968 desc2.count_type = 2;
2969 desc2.duration = duration2;
2970 desc2.interval = interval2;
2975 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
2976 len = p2p_buf_add_ie_hdr(req);
2977 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
2978 p2p_buf_update_ie_hdr(req, len);
2984 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
2985 const u8 *own_interface_addr, unsigned int freq,
2986 u32 duration1, u32 interval1, u32 duration2,
2991 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
2992 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
2993 "int1=%u dur2=%u int2=%u",
2994 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
2995 freq, duration1, interval1, duration2, interval2);
2997 req = p2p_build_presence_req(duration1, interval1, duration2,
3002 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3003 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3005 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
3006 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3007 "P2P: Failed to send Action frame");
3015 static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3016 size_t noa_len, u8 dialog_token)
3018 struct wpabuf *resp;
3021 resp = wpabuf_alloc(100 + noa_len);
3025 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3026 len = p2p_buf_add_ie_hdr(resp);
3027 p2p_buf_add_status(resp, status);
3029 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3030 wpabuf_put_le16(resp, noa_len);
3031 wpabuf_put_data(resp, noa, noa_len);
3033 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3034 p2p_buf_update_ie_hdr(resp, len);
3040 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3041 const u8 *sa, const u8 *data, size_t len,
3044 struct p2p_message msg;
3046 struct wpabuf *resp;
3048 struct p2p_group *group = NULL;
3053 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3054 "P2P: Received P2P Action - P2P Presence Request");
3056 for (g = 0; g < p2p->num_groups; g++) {
3057 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3059 group = p2p->groups[g];
3063 if (group == NULL) {
3064 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3065 "P2P: Ignore P2P Presence Request for unknown group "
3066 MACSTR, MAC2STR(da));
3070 if (p2p_parse(data, len, &msg) < 0) {
3071 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3072 "P2P: Failed to parse P2P Presence Request");
3073 status = P2P_SC_FAIL_INVALID_PARAMS;
3078 if (msg.noa == NULL) {
3079 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3080 "P2P: No NoA attribute in P2P Presence Request");
3081 status = P2P_SC_FAIL_INVALID_PARAMS;
3085 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3088 if (p2p->cfg->get_noa)
3089 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3093 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3094 noa_len > 0 ? noa_len : 0,
3097 p2p_parse_free(&msg);
3101 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3102 if (p2p_send_action(p2p, rx_freq, sa, da, da,
3103 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
3104 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3105 "P2P: Failed to send Action frame");
3111 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3112 const u8 *sa, const u8 *data, size_t len)
3114 struct p2p_message msg;
3116 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3117 "P2P: Received P2P Action - P2P Presence Response");
3119 if (p2p_parse(data, len, &msg) < 0) {
3120 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3121 "P2P: Failed to parse P2P Presence Response");
3125 if (msg.status == NULL || msg.noa == NULL) {
3126 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3127 "P2P: No Status or NoA attribute in P2P Presence "
3129 p2p_parse_free(&msg);
3134 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3135 "P2P: P2P Presence Request was rejected: status %u",
3137 p2p_parse_free(&msg);
3141 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3142 "P2P: P2P Presence Request was accepted");
3143 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
3144 msg.noa, msg.noa_len);
3145 /* TODO: process NoA */
3146 p2p_parse_free(&msg);
3150 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3152 struct p2p_data *p2p = eloop_ctx;
3154 if (p2p->ext_listen_interval) {
3155 /* Schedule next extended listen timeout */
3156 eloop_register_timeout(p2p->ext_listen_interval_sec,
3157 p2p->ext_listen_interval_usec,
3158 p2p_ext_listen_timeout, p2p, NULL);
3161 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
3163 * This should not really happen, but it looks like the Listen
3164 * command may fail is something else (e.g., a scan) was
3165 * running at an inconvenient time. As a workaround, allow new
3166 * Extended Listen operation to be started.
3168 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
3169 "Extended Listen operation had not been completed - "
3171 p2p->ext_listen_only = 0;
3172 p2p_set_state(p2p, P2P_IDLE);
3175 if (p2p->state != P2P_IDLE) {
3176 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
3177 "Listen timeout in active state (%s)",
3178 p2p_state_txt(p2p->state));
3182 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
3183 p2p->ext_listen_only = 1;
3184 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
3185 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
3186 "Listen state for Extended Listen Timing");
3187 p2p->ext_listen_only = 0;
3192 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
3193 unsigned int interval)
3195 if (period > 65535 || interval > 65535 || period > interval ||
3196 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
3197 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3198 "P2P: Invalid Extended Listen Timing request: "
3199 "period=%u interval=%u", period, interval);
3203 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
3205 if (interval == 0) {
3206 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3207 "P2P: Disabling Extended Listen Timing");
3208 p2p->ext_listen_period = 0;
3209 p2p->ext_listen_interval = 0;
3213 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3214 "P2P: Enabling Extended Listen Timing: period %u msec, "
3215 "interval %u msec", period, interval);
3216 p2p->ext_listen_period = period;
3217 p2p->ext_listen_interval = interval;
3218 p2p->ext_listen_interval_sec = interval / 1000;
3219 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
3221 eloop_register_timeout(p2p->ext_listen_interval_sec,
3222 p2p->ext_listen_interval_usec,
3223 p2p_ext_listen_timeout, p2p, NULL);
3229 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3230 const u8 *ie, size_t ie_len)
3232 struct p2p_message msg;
3234 if (bssid == NULL || ie == NULL)
3237 os_memset(&msg, 0, sizeof(msg));
3238 if (p2p_parse_ies(ie, ie_len, &msg))
3240 if (msg.minor_reason_code == NULL)
3243 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3244 "P2P: Deauthentication notification BSSID " MACSTR
3245 " reason_code=%u minor_reason_code=%u",
3246 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3248 p2p_parse_free(&msg);
3252 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3253 const u8 *ie, size_t ie_len)
3255 struct p2p_message msg;
3257 if (bssid == NULL || ie == NULL)
3260 os_memset(&msg, 0, sizeof(msg));
3261 if (p2p_parse_ies(ie, ie_len, &msg))
3263 if (msg.minor_reason_code == NULL)
3266 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3267 "P2P: Disassociation notification BSSID " MACSTR
3268 " reason_code=%u minor_reason_code=%u",
3269 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3271 p2p_parse_free(&msg);
3275 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
3278 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3279 "Device operations enabled");
3280 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
3282 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3283 "Device operations disabled");
3284 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
3289 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
3291 if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
3294 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
3295 "reg_class %u channel %u", reg_class, channel);
3296 p2p->cfg->reg_class = reg_class;
3297 p2p->cfg->channel = channel;
3303 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
3305 wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
3306 if (postfix == NULL) {
3307 p2p->cfg->ssid_postfix_len = 0;
3310 if (len > sizeof(p2p->cfg->ssid_postfix))
3312 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
3313 p2p->cfg->ssid_postfix_len = len;
3318 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
3321 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
3322 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
3324 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
3329 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
3332 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3335 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
3340 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
3342 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
3343 if (is_zero_ether_addr(p2p->peer_filter))
3344 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
3347 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
3348 "filter for " MACSTR, MAC2STR(p2p->peer_filter));
3352 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
3354 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
3355 enabled ? "enabled" : "disabled");
3356 if (p2p->cross_connect == enabled)
3358 p2p->cross_connect = enabled;
3359 /* TODO: may need to tear down any action group where we are GO(?) */
3363 int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
3365 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3368 if (dev->oper_freq <= 0)
3370 return dev->oper_freq;
3374 void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
3376 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
3377 enabled ? "enabled" : "disabled");
3378 p2p->cfg->p2p_intra_bss = enabled;
3382 void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
3384 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
3385 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
3389 int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3390 const u8 *src, const u8 *bssid, const u8 *buf,
3391 size_t len, unsigned int wait_time)
3393 if (p2p->p2p_scan_running) {
3394 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
3395 "frame TX until p2p_scan completes");
3396 if (p2p->after_scan_tx) {
3397 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
3398 "previous pending Action frame TX");
3399 os_free(p2p->after_scan_tx);
3401 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
3403 if (p2p->after_scan_tx == NULL)
3405 p2p->after_scan_tx->freq = freq;
3406 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
3407 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
3408 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
3409 p2p->after_scan_tx->len = len;
3410 p2p->after_scan_tx->wait_time = wait_time;
3411 os_memcpy(p2p->after_scan_tx + 1, buf, len);
3415 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
3416 buf, len, wait_time);
3420 void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
3423 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
3424 " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall);
3425 p2p->best_freq_24 = freq_24;
3426 p2p->best_freq_5 = freq_5;
3427 p2p->best_freq_overall = freq_overall;
3431 const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
3433 if (p2p == NULL || p2p->go_neg_peer == NULL)
3435 return p2p->go_neg_peer->info.p2p_device_addr;
3439 const struct p2p_peer_info *
3440 p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
3442 struct p2p_device *dev;
3445 dev = p2p_get_device(p2p, addr);
3450 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
3456 dev = dl_list_first(&dev->list,
3459 if (&dev->list == &p2p->devices)
3461 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
3464 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3467 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
3468 dev = dl_list_first(&dev->list,
3471 if (&dev->list == &p2p->devices)