2 * Wi-Fi Direct - P2P module
3 * Copyright (c) 2009-2010, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #include "common/ieee802_11_defs.h"
14 #include "common/ieee802_11_common.h"
15 #include "common/wpa_ctrl.h"
16 #include "wps/wps_i.h"
21 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
22 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
23 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
24 const u8 *sa, const u8 *data, size_t len,
26 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
27 const u8 *sa, const u8 *data,
29 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
30 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
34 * p2p_scan recovery timeout
36 * Many drivers are using 30 second timeout on scan results. Allow a bit larger
37 * timeout for this to avoid hitting P2P timeout unnecessarily.
39 #define P2P_SCAN_TIMEOUT 35
42 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
43 * entries will be removed
46 #define P2P_PEER_EXPIRATION_AGE 30
48 #define P2P_PEER_EXPIRATION_AGE 300
51 #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
54 int p2p_connection_in_progress(struct p2p_data *p2p)
60 case P2P_CONNECT_LISTEN:
62 case P2P_WAIT_PEER_CONNECT:
63 case P2P_WAIT_PEER_IDLE:
64 case P2P_PROVISIONING:
66 case P2P_INVITE_LISTEN:
71 wpa_printf(MSG_DEBUG, "p2p_connection_in_progress state %d", p2p->state);
79 static void p2p_expire_peers(struct p2p_data *p2p)
81 struct p2p_device *dev, *n;
86 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
87 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
90 if (p2p->cfg->go_connected &&
91 p2p->cfg->go_connected(p2p->cfg->cb_ctx,
92 dev->info.p2p_device_addr)) {
94 * We are connected as a client to a group in which the
95 * peer is the GO, so do not expire the peer entry.
97 os_get_time(&dev->last_seen);
101 for (i = 0; i < p2p->num_groups; i++) {
102 if (p2p_group_is_client_connected(
103 p2p->groups[i], dev->info.p2p_device_addr))
106 if (i < p2p->num_groups) {
108 * The peer is connected as a client in a group where
109 * we are the GO, so do not expire the peer entry.
111 os_get_time(&dev->last_seen);
116 /* If Connection is in progress, don't expire the peer
118 if (p2p_connection_in_progress(p2p))
122 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer "
123 "entry " MACSTR, MAC2STR(dev->info.p2p_device_addr));
125 /* SD_FAIR_POLICY: Update the current sd_dev_list pointer to next device */
126 if(&dev->list == p2p->sd_dev_list)
127 p2p->sd_dev_list = dev->list.next;
129 dl_list_del(&dev->list);
130 p2p_device_free(p2p, dev);
135 static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
137 struct p2p_data *p2p = eloop_ctx;
138 p2p_expire_peers(p2p);
139 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
140 p2p_expiration_timeout, p2p, NULL);
144 static const char * p2p_state_txt(int state)
153 case P2P_CONNECT_LISTEN:
154 return "CONNECT_LISTEN";
157 case P2P_LISTEN_ONLY:
158 return "LISTEN_ONLY";
159 case P2P_WAIT_PEER_CONNECT:
160 return "WAIT_PEER_CONNECT";
161 case P2P_WAIT_PEER_IDLE:
162 return "WAIT_PEER_IDLE";
163 case P2P_SD_DURING_FIND:
164 return "SD_DURING_FIND";
165 case P2P_PROVISIONING:
166 return "PROVISIONING";
167 case P2P_PD_DURING_FIND:
168 return "PD_DURING_FIND";
171 case P2P_INVITE_LISTEN:
172 return "INVITE_LISTEN";
173 case P2P_SEARCH_WHEN_READY:
174 return "SEARCH_WHEN_READY";
175 case P2P_CONTINUE_SEARCH_WHEN_READY:
176 return "CONTINUE_SEARCH_WHEN_READY";
183 u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
185 struct p2p_device *dev = NULL;
190 dev = p2p_get_device(p2p, addr);
192 return dev->wps_prov_info;
198 void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
200 struct p2p_device *dev = NULL;
205 dev = p2p_get_device(p2p, addr);
207 dev->wps_prov_info = 0;
211 void p2p_set_state(struct p2p_data *p2p, int new_state)
213 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s",
214 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
215 p2p->state = new_state;
219 void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
221 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
222 "P2P: Set timeout (state=%s): %u.%06u sec",
223 p2p_state_txt(p2p->state), sec, usec);
224 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
225 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
229 void p2p_clear_timeout(struct p2p_data *p2p)
231 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)",
232 p2p_state_txt(p2p->state));
233 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
237 void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
240 struct p2p_go_neg_results res;
241 p2p_clear_timeout(p2p);
242 p2p_set_state(p2p, P2P_IDLE);
243 if (p2p->go_neg_peer)
244 p2p->go_neg_peer->wps_method = WPS_NOT_READY;
245 p2p->go_neg_peer = NULL;
247 os_memset(&res, 0, sizeof(res));
250 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr,
252 os_memcpy(res.peer_interface_addr, peer->intended_addr,
255 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
259 static void p2p_listen_in_find(struct p2p_data *p2p)
265 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
266 "P2P: Starting short listen state (state=%s)",
267 p2p_state_txt(p2p->state));
269 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
272 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
273 "P2P: Unknown regulatory class/channel");
277 os_get_random((u8 *) &r, sizeof(r));
278 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
279 p2p->min_disc_int) * 100;
281 p2p->pending_listen_freq = freq;
282 p2p->pending_listen_sec = 0;
283 p2p->pending_listen_usec = 1024 * tu;
285 ies = p2p_build_probe_resp_ies(p2p);
289 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
291 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
292 "P2P: Failed to start listen mode");
293 p2p->pending_listen_freq = 0;
299 int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
304 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
305 "P2P: Going to listen(only) state");
307 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
310 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
311 "P2P: Unknown regulatory class/channel");
315 p2p->pending_listen_freq = freq;
316 p2p->pending_listen_sec = timeout / 1000;
317 p2p->pending_listen_usec = (timeout % 1000) * 1000;
319 if (p2p->p2p_scan_running) {
320 if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
321 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
322 "P2P: p2p_scan running - connect is already "
323 "pending - skip listen");
326 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
327 "P2P: p2p_scan running - delay start of listen state");
328 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
332 ies = p2p_build_probe_resp_ies(p2p);
336 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
337 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
338 "P2P: Failed to start listen mode");
339 p2p->pending_listen_freq = 0;
345 p2p_set_state(p2p, P2P_LISTEN_ONLY);
351 static void p2p_device_clear_reported(struct p2p_data *p2p)
353 struct p2p_device *dev;
354 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
355 dev->flags &= ~P2P_DEV_REPORTED;
360 * p2p_get_device - Fetch a peer entry
361 * @p2p: P2P module context from p2p_init()
362 * @addr: P2P Device Address of the peer
363 * Returns: Pointer to the device entry or %NULL if not found
365 struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
367 struct p2p_device *dev;
368 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
369 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
377 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
378 * @p2p: P2P module context from p2p_init()
379 * @addr: P2P Interface Address of the peer
380 * Returns: Pointer to the device entry or %NULL if not found
382 struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
385 struct p2p_device *dev;
386 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
387 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
395 * p2p_create_device - Create a peer entry
396 * @p2p: P2P module context from p2p_init()
397 * @addr: P2P Device Address of the peer
398 * Returns: Pointer to the device entry or %NULL on failure
400 * If there is already an entry for the peer, it will be returned instead of
401 * creating a new one.
403 static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
406 struct p2p_device *dev, *oldest = NULL;
409 dev = p2p_get_device(p2p, addr);
413 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
415 if (oldest == NULL ||
416 os_time_before(&dev->last_seen, &oldest->last_seen))
419 if (count + 1 > p2p->cfg->max_peers && oldest) {
420 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
421 "P2P: Remove oldest peer entry to make room for a new "
424 /* SD_FAIR_POLICY: Update the current sd_dev_list pointer to next device */
425 if(&oldest->list == p2p->sd_dev_list)
426 p2p->sd_dev_list = oldest->list.next;
428 dl_list_del(&oldest->list);
429 p2p_device_free(p2p, oldest);
432 dev = os_zalloc(sizeof(*dev));
435 dl_list_add(&p2p->devices, &dev->list);
436 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
442 static void p2p_copy_client_info(struct p2p_device *dev,
443 struct p2p_client_info *cli)
445 os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
446 dev->info.device_name[cli->dev_name_len] = '\0';
447 dev->info.dev_capab = cli->dev_capab;
448 dev->info.config_methods = cli->config_methods;
449 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
450 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
451 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
452 dev->info.wps_sec_dev_type_list_len);
456 static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
457 const u8 *go_interface_addr, int freq,
458 const u8 *gi, size_t gi_len)
460 struct p2p_group_info info;
462 struct p2p_device *dev;
467 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
471 * Clear old data for this group; if the devices are still in the
472 * group, the information will be restored in the loop following this.
474 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
475 if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
477 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
478 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
482 for (c = 0; c < info.num_clients; c++) {
483 struct p2p_client_info *cli = &info.client[c];
484 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
486 continue; /* ignore our own entry */
487 dev = p2p_get_device(p2p, cli->p2p_device_addr);
489 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
490 P2P_DEV_PROBE_REQ_ONLY)) {
492 * Update information since we have not
493 * received this directly from the client.
495 p2p_copy_client_info(dev, cli);
498 * Need to update P2P Client Discoverability
499 * flag since it is valid only in P2P Group
502 dev->info.dev_capab &=
503 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
504 dev->info.dev_capab |=
506 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
508 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
509 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
512 dev = p2p_create_device(p2p, cli->p2p_device_addr);
515 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
516 p2p_copy_client_info(dev, cli);
517 dev->oper_freq = freq;
518 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
519 dev->info.p2p_device_addr,
521 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
524 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
526 os_get_time(&dev->last_seen);
527 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
528 os_memcpy(dev->member_in_go_iface, go_interface_addr,
536 static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
537 const struct p2p_message *msg)
539 os_memcpy(dev->info.device_name, msg->device_name,
540 sizeof(dev->info.device_name));
542 if (msg->manufacturer &&
543 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
544 os_memset(dev->info.manufacturer, 0,
545 sizeof(dev->info.manufacturer));
546 os_memcpy(dev->info.manufacturer, msg->manufacturer,
547 msg->manufacturer_len);
550 if (msg->model_name &&
551 msg->model_name_len < sizeof(dev->info.model_name)) {
552 os_memset(dev->info.model_name, 0,
553 sizeof(dev->info.model_name));
554 os_memcpy(dev->info.model_name, msg->model_name,
555 msg->model_name_len);
558 if (msg->model_number &&
559 msg->model_number_len < sizeof(dev->info.model_number)) {
560 os_memset(dev->info.model_number, 0,
561 sizeof(dev->info.model_number));
562 os_memcpy(dev->info.model_number, msg->model_number,
563 msg->model_number_len);
566 if (msg->serial_number &&
567 msg->serial_number_len < sizeof(dev->info.serial_number)) {
568 os_memset(dev->info.serial_number, 0,
569 sizeof(dev->info.serial_number));
570 os_memcpy(dev->info.serial_number, msg->serial_number,
571 msg->serial_number_len);
574 if (msg->pri_dev_type)
575 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
576 sizeof(dev->info.pri_dev_type));
577 else if (msg->wps_pri_dev_type)
578 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
579 sizeof(dev->info.pri_dev_type));
581 if (msg->wps_sec_dev_type_list) {
582 os_memcpy(dev->info.wps_sec_dev_type_list,
583 msg->wps_sec_dev_type_list,
584 msg->wps_sec_dev_type_list_len);
585 dev->info.wps_sec_dev_type_list_len =
586 msg->wps_sec_dev_type_list_len;
589 if (msg->capability) {
591 * P2P Client Discoverability bit is reserved in all frames
592 * that use this function, so do not change its value here.
594 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
595 dev->info.dev_capab |= msg->capability[0] &
596 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
597 dev->info.group_capab = msg->capability[1];
600 if (msg->ext_listen_timing) {
601 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
602 dev->ext_listen_interval =
603 WPA_GET_LE16(msg->ext_listen_timing + 2);
607 dev->info.config_methods = msg->config_methods ?
608 msg->config_methods : msg->wps_config_methods;
614 * p2p_add_device - Add peer entries based on scan results or P2P frames
615 * @p2p: P2P module context from p2p_init()
616 * @addr: Source address of Beacon or Probe Response frame (may be either
617 * P2P Device Address or P2P Interface Address)
618 * @level: Signal level (signal strength of the received frame from the peer)
619 * @freq: Frequency on which the Beacon or Probe Response frame was received
620 * @ies: IEs from the Beacon or Probe Response frame
621 * @ies_len: Length of ies buffer in octets
622 * @scan_res: Whether this was based on scan results
623 * Returns: 0 on success, -1 on failure
625 * If the scan result is for a GO, the clients in the group will also be added
626 * to the peer table. This function can also be used with some other frames
627 * like Provision Discovery Request that contains P2P Capability and P2P Device
630 int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
631 const u8 *ies, size_t ies_len, int scan_res)
633 struct p2p_device *dev;
634 struct p2p_message msg;
635 const u8 *p2p_dev_addr;
638 os_memset(&msg, 0, sizeof(msg));
639 if (p2p_parse_ies(ies, ies_len, &msg)) {
640 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
641 "P2P: Failed to parse P2P IE for a device entry");
642 p2p_parse_free(&msg);
646 if (msg.p2p_device_addr)
647 p2p_dev_addr = msg.p2p_device_addr;
648 else if (msg.device_id)
649 p2p_dev_addr = msg.device_id;
651 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
652 "P2P: Ignore scan data without P2P Device Info or "
654 p2p_parse_free(&msg);
658 if (!is_zero_ether_addr(p2p->peer_filter) &&
659 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
660 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
661 "filter for " MACSTR " due to peer filter",
662 MAC2STR(p2p_dev_addr));
666 dev = p2p_create_device(p2p, p2p_dev_addr);
668 p2p_parse_free(&msg);
671 os_get_time(&dev->last_seen);
672 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
674 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
675 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
677 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
678 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
680 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
681 dev->oper_ssid_len = msg.ssid[1];
684 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
685 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
687 if (*msg.ds_params == 14)
690 ds_freq = 2407 + *msg.ds_params * 5;
691 if (freq != ds_freq) {
692 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
693 "P2P: Update Listen frequency based on DS "
694 "Parameter Set IE: %d -> %d MHz",
700 if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
701 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
702 "P2P: Update Listen frequency based on scan "
703 "results (" MACSTR " %d -> %d MHz (DS param %d)",
704 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
705 freq, msg.ds_params ? *msg.ds_params : -1);
708 dev->listen_freq = freq;
710 dev->oper_freq = freq;
712 dev->info.level = level;
714 p2p_copy_wps_info(dev, 0, &msg);
716 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
717 wpabuf_free(dev->info.wps_vendor_ext[i]);
718 dev->info.wps_vendor_ext[i] = NULL;
721 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
722 if (msg.wps_vendor_ext[i] == NULL)
724 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
725 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
726 if (dev->info.wps_vendor_ext[i] == NULL)
730 if (msg.wfd_subelems) {
731 wpabuf_free(dev->info.wfd_subelems);
732 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
736 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
737 msg.group_info, msg.group_info_len);
740 p2p_parse_free(&msg);
742 if (p2p_pending_sd_req(p2p, dev))
743 dev->flags |= P2P_DEV_SD_SCHEDULE;
745 if (dev->flags & P2P_DEV_REPORTED)
748 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
749 "P2P: Peer found with Listen frequency %d MHz", freq);
750 if (dev->flags & P2P_DEV_USER_REJECTED) {
751 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
752 "P2P: Do not report rejected device");
756 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
757 !(dev->flags & P2P_DEV_REPORTED_ONCE));
758 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
764 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
768 if (p2p->go_neg_peer == dev) {
770 * If GO Negotiation is in progress, report that it has failed.
772 p2p_go_neg_failed(p2p, dev, -1);
773 p2p->go_neg_peer = NULL;
775 if (p2p->invite_peer == dev)
776 p2p->invite_peer = NULL;
777 if (p2p->sd_peer == dev)
779 if (p2p->pending_client_disc_go == dev)
780 p2p->pending_client_disc_go = NULL;
782 /* dev_lost() device, but only if it was previously dev_found() */
783 if (dev->flags & P2P_DEV_REPORTED_ONCE)
784 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
785 dev->info.p2p_device_addr);
787 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
788 wpabuf_free(dev->info.wps_vendor_ext[i]);
789 dev->info.wps_vendor_ext[i] = NULL;
792 wpabuf_free(dev->info.wfd_subelems);
798 static int p2p_get_next_prog_freq(struct p2p_data *p2p)
800 struct p2p_channels *c;
801 struct p2p_reg_class *cla;
808 c = &p2p->cfg->channels;
809 for (cl = 0; cl < c->reg_classes; cl++) {
810 cla = &c->reg_class[cl];
811 if (cla->reg_class != p2p->last_prog_scan_class)
813 for (ch = 0; ch < cla->channels; ch++) {
814 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
824 /* Start from beginning */
825 reg_class = c->reg_class[0].reg_class;
826 channel = c->reg_class[0].channel[0];
828 /* Pick the next channel */
830 if (ch == cla->channels) {
832 if (cl == c->reg_classes)
836 reg_class = c->reg_class[cl].reg_class;
837 channel = c->reg_class[cl].channel[ch];
840 freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
841 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
842 "channel: reg_class %u channel %u -> %d MHz",
843 reg_class, channel, freq);
844 p2p->last_prog_scan_class = reg_class;
845 p2p->last_prog_scan_chan = channel;
847 if (freq == 2412 || freq == 2437 || freq == 2462)
848 return 0; /* No need to add social channels */
853 static void p2p_search(struct p2p_data *p2p)
856 enum p2p_scan_type type;
857 u16 pw_id = DEV_PW_DEFAULT;
860 if (p2p->drv_in_listen) {
861 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
862 "in Listen state - wait for it to end before "
866 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
868 if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
869 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
870 type = P2P_SCAN_SOCIAL_PLUS_ONE;
871 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
872 "(+ freq %u)", freq);
874 type = P2P_SCAN_SOCIAL;
875 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
878 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
879 p2p->num_req_dev_types, p2p->req_dev_types,
880 p2p->find_dev_id, pw_id);
882 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
883 "P2P: Scan request failed");
884 p2p_continue_find(p2p);
885 } else if (res == 1) {
886 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
887 "p2p_scan at this point - will try again after "
888 "previous scan completes");
889 p2p_set_state(p2p, P2P_CONTINUE_SEARCH_WHEN_READY);
891 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
892 p2p->p2p_scan_running = 1;
893 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
894 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
900 static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
902 struct p2p_data *p2p = eloop_ctx;
903 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
908 static int p2p_run_after_scan(struct p2p_data *p2p)
910 struct p2p_device *dev;
911 enum p2p_after_scan op;
913 if (p2p->after_scan_tx) {
914 /* TODO: schedule p2p_run_after_scan to be called from TX
915 * status callback(?) */
916 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
917 "Action frame at p2p_scan completion");
918 p2p->cfg->send_action(p2p->cfg->cb_ctx,
919 p2p->after_scan_tx->freq,
920 p2p->after_scan_tx->dst,
921 p2p->after_scan_tx->src,
922 p2p->after_scan_tx->bssid,
923 (u8 *) (p2p->after_scan_tx + 1),
924 p2p->after_scan_tx->len,
925 p2p->after_scan_tx->wait_time);
926 os_free(p2p->after_scan_tx);
927 p2p->after_scan_tx = NULL;
929 /* For SD frames, there is a scenario, where we can receive a SD request frame during p2p_scan.
930 * At that moment, we will send the SD response from this context. After sending the SD response,
931 * we need to continue p2p_find. But if we return 1 from here, p2p_find is going to be stopped.
939 op = p2p->start_after_scan;
940 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
942 case P2P_AFTER_SCAN_NOTHING:
944 case P2P_AFTER_SCAN_LISTEN:
945 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
946 "requested Listen state");
947 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
948 p2p->pending_listen_usec / 1000);
950 case P2P_AFTER_SCAN_CONNECT:
951 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
952 "requested connect with " MACSTR,
953 MAC2STR(p2p->after_scan_peer));
954 dev = p2p_get_device(p2p, p2p->after_scan_peer);
956 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
960 p2p_connect_send(p2p, dev);
968 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
970 struct p2p_data *p2p = eloop_ctx;
972 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
973 "(running=%d)", p2p->p2p_scan_running);
974 running = p2p->p2p_scan_running;
975 /* Make sure we recover from missed scan results callback */
976 p2p->p2p_scan_running = 0;
979 p2p_run_after_scan(p2p);
983 static void p2p_free_req_dev_types(struct p2p_data *p2p)
985 p2p->num_req_dev_types = 0;
986 os_free(p2p->req_dev_types);
987 p2p->req_dev_types = NULL;
991 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
992 enum p2p_discovery_type type,
993 unsigned int num_req_dev_types, const u8 *req_dev_types,
994 const u8 *dev_id, unsigned int search_delay)
998 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
1000 if (p2p->p2p_scan_running) {
1001 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
1005 p2p_free_req_dev_types(p2p);
1006 if (req_dev_types && num_req_dev_types) {
1007 p2p->req_dev_types = os_malloc(num_req_dev_types *
1009 if (p2p->req_dev_types == NULL)
1011 os_memcpy(p2p->req_dev_types, req_dev_types,
1012 num_req_dev_types * WPS_DEV_TYPE_LEN);
1013 p2p->num_req_dev_types = num_req_dev_types;
1017 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
1018 p2p->find_dev_id = p2p->find_dev_id_buf;
1020 p2p->find_dev_id = NULL;
1022 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1023 p2p_clear_timeout(p2p);
1024 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1025 p2p->find_type = type;
1026 p2p_device_clear_reported(p2p);
1027 p2p_set_state(p2p, P2P_SEARCH);
1028 p2p->search_delay = search_delay;
1029 p2p->in_search_delay = 0;
1030 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1031 p2p->last_p2p_find_timeout = timeout;
1033 eloop_register_timeout(timeout, 0, p2p_find_timeout,
1036 case P2P_FIND_START_WITH_FULL:
1037 case P2P_FIND_PROGRESSIVE:
1038 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
1039 p2p->num_req_dev_types,
1040 p2p->req_dev_types, dev_id,
1043 case P2P_FIND_ONLY_SOCIAL:
1044 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
1045 p2p->num_req_dev_types,
1046 p2p->req_dev_types, dev_id,
1054 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
1055 p2p->p2p_scan_running = 1;
1056 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1057 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1059 } else if (res == 1) {
1060 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
1061 "p2p_scan at this point - will try again after "
1062 "previous scan completes");
1064 p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
1065 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1067 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
1069 p2p_set_state(p2p, P2P_IDLE);
1070 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1077 int p2p_search_pending(struct p2p_data *p2p)
1082 if(p2p->state == P2P_SEARCH_WHEN_READY)
1089 int p2p_other_scan_completed(struct p2p_data *p2p)
1091 if (p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY) {
1092 p2p_set_state(p2p, P2P_SEARCH);
1096 if (p2p->state != P2P_SEARCH_WHEN_READY)
1098 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find "
1099 "now that previous scan was completed");
1100 if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
1101 p2p->num_req_dev_types, p2p->req_dev_types,
1102 p2p->find_dev_id, p2p->search_delay) < 0)
1108 void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1110 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
1111 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1112 p2p_clear_timeout(p2p);
1113 if (p2p->state == P2P_SEARCH)
1114 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1115 p2p_set_state(p2p, P2P_IDLE);
1116 p2p_free_req_dev_types(p2p);
1117 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1118 p2p->go_neg_peer = NULL;
1119 p2p->sd_peer = NULL;
1120 p2p->invite_peer = NULL;
1121 p2p_stop_listen_for_freq(p2p, freq);
1125 void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1127 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
1128 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
1129 "since we are on correct channel for response");
1132 if (p2p->in_listen) {
1134 p2p_clear_timeout(p2p);
1136 if (p2p->drv_in_listen) {
1138 * The driver may not deliver callback to p2p_listen_end()
1139 * when the operation gets canceled, so clear the internal
1140 * variable that is tracking driver state.
1142 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear "
1143 "drv_in_listen (%d)", p2p->drv_in_listen);
1144 p2p->drv_in_listen = 0;
1146 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1150 void p2p_stop_find(struct p2p_data *p2p)
1152 p2p_stop_find_for_freq(p2p, 0);
1156 static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
1159 u8 op_reg_class, op_channel;
1160 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
1161 &op_reg_class, &op_channel) < 0) {
1162 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1163 "P2P: Unsupported frequency %u MHz",
1167 if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
1169 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1170 "P2P: Frequency %u MHz (oper_class %u "
1171 "channel %u) not allowed for P2P",
1172 force_freq, op_reg_class, op_channel);
1175 p2p->op_reg_class = op_reg_class;
1176 p2p->op_channel = op_channel;
1178 p2p->channels.reg_classes = 1;
1179 p2p->channels.reg_class[0].channels = 1;
1180 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1181 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
1183 if(p2p->cfg->p2p_concurrency == P2P_MULTI_CHANNEL_CONCURRENT) {
1184 /* We we are requesting for a preferred channel. But since
1185 * are multichannel concurrent, we have to poplulate the
1186 * p2p_channels with list of channels that we support.
1188 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1189 sizeof(struct p2p_channels));
1191 p2p->channels.reg_classes = 1;
1192 p2p->channels.reg_class[0].channels = 1;
1193 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1194 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
1198 u8 op_reg_class, op_channel;
1200 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1201 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
1202 p2p_freq_to_channel(p2p->cfg->country,
1203 p2p->best_freq_overall,
1204 &op_reg_class, &op_channel) == 0) {
1205 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1206 "P2P: Select best overall channel as "
1207 "operating channel preference");
1208 p2p->op_reg_class = op_reg_class;
1209 p2p->op_channel = op_channel;
1210 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1211 p2p_supported_freq(p2p, p2p->best_freq_5) &&
1212 p2p_freq_to_channel(p2p->cfg->country,
1214 &op_reg_class, &op_channel) ==
1216 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1217 "P2P: Select best 5 GHz channel as "
1218 "operating channel preference");
1219 p2p->op_reg_class = op_reg_class;
1220 p2p->op_channel = op_channel;
1221 } else if (!p2p->cfg->cfg_op_channel &&
1222 p2p->best_freq_24 > 0 &&
1223 p2p_supported_freq(p2p, p2p->best_freq_24) &&
1224 p2p_freq_to_channel(p2p->cfg->country,
1226 &op_reg_class, &op_channel) ==
1228 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1229 "P2P: Select best 2.4 GHz channel as "
1230 "operating channel preference");
1231 p2p->op_reg_class = op_reg_class;
1232 p2p->op_channel = op_channel;
1234 p2p->op_reg_class = p2p->cfg->op_reg_class;
1235 p2p->op_channel = p2p->cfg->op_channel;
1238 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1239 sizeof(struct p2p_channels));
1241 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1242 "P2P: Own preference for operation channel: "
1243 "Operating Class %u Channel %u%s",
1244 p2p->op_reg_class, p2p->op_channel,
1245 force_freq ? " (forced)" : "");
1251 static void p2p_set_dev_persistent(struct p2p_device *dev,
1252 int persistent_group)
1254 switch (persistent_group) {
1256 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1257 P2P_DEV_PREFER_PERSISTENT_RECONN);
1260 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1261 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1264 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1265 P2P_DEV_PREFER_PERSISTENT_RECONN;
1271 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1272 enum p2p_wps_method wps_method,
1273 int go_intent, const u8 *own_interface_addr,
1274 unsigned int force_freq, int persistent_group,
1275 const u8 *force_ssid, size_t force_ssid_len,
1276 int pd_before_go_neg)
1278 struct p2p_device *dev;
1280 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1281 "P2P: Request to start group negotiation - peer=" MACSTR
1282 " GO Intent=%d Intended Interface Address=" MACSTR
1283 " wps_method=%d persistent_group=%d pd_before_go_neg=%d",
1284 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1285 wps_method, persistent_group, pd_before_go_neg);
1287 if (p2p_prepare_channel(p2p, force_freq) < 0)
1290 dev = p2p_get_device(p2p, peer_addr);
1291 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1292 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1293 "P2P: Cannot connect to unknown P2P Device " MACSTR,
1294 MAC2STR(peer_addr));
1298 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1299 if (!(dev->info.dev_capab &
1300 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1301 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1302 "P2P: Cannot connect to P2P Device " MACSTR
1303 " that is in a group and is not discoverable",
1304 MAC2STR(peer_addr));
1307 if (dev->oper_freq <= 0) {
1308 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1309 "P2P: Cannot connect to P2P Device " MACSTR
1310 " with incomplete information",
1311 MAC2STR(peer_addr));
1316 * First, try to connect directly. If the peer does not
1317 * acknowledge frames, assume it is sleeping and use device
1318 * discoverability via the GO at that point.
1324 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1325 force_ssid, force_ssid_len);
1326 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1327 p2p->ssid_len = force_ssid_len;
1331 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1332 dev->flags &= ~P2P_DEV_USER_REJECTED;
1333 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1334 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1335 if (pd_before_go_neg)
1336 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
1338 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
1339 dev->connect_reqs = 0;
1340 dev->go_neg_req_sent = 0;
1341 dev->go_state = UNKNOWN_GO;
1342 p2p_set_dev_persistent(dev, persistent_group);
1343 p2p->go_intent = go_intent;
1344 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1346 if (p2p->state != P2P_IDLE)
1349 if (p2p->after_scan_tx) {
1351 * We need to drop the pending frame to avoid issues with the
1352 * new GO Negotiation, e.g., when the pending frame was from a
1353 * previous attempt at starting a GO Negotiation.
1355 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
1356 "previous pending Action frame TX that was waiting "
1357 "for p2p_scan completion");
1358 os_free(p2p->after_scan_tx);
1359 p2p->after_scan_tx = NULL;
1362 dev->wps_method = wps_method;
1363 dev->status = P2P_SC_SUCCESS;
1366 dev->flags |= P2P_DEV_FORCE_FREQ;
1368 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1370 if (p2p->p2p_scan_running) {
1371 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1372 "P2P: p2p_scan running - delay connect send");
1373 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1374 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1377 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1379 return p2p_connect_send(p2p, dev);
1383 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1384 enum p2p_wps_method wps_method,
1385 int go_intent, const u8 *own_interface_addr,
1386 unsigned int force_freq, int persistent_group,
1387 const u8 *force_ssid, size_t force_ssid_len)
1389 struct p2p_device *dev;
1391 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1392 "P2P: Request to authorize group negotiation - peer=" MACSTR
1393 " GO Intent=%d Intended Interface Address=" MACSTR
1394 " wps_method=%d persistent_group=%d",
1395 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1396 wps_method, persistent_group);
1398 if (p2p_prepare_channel(p2p, force_freq) < 0)
1401 dev = p2p_get_device(p2p, peer_addr);
1403 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1404 "P2P: Cannot authorize unknown P2P Device " MACSTR,
1405 MAC2STR(peer_addr));
1411 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1412 force_ssid, force_ssid_len);
1413 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1414 p2p->ssid_len = force_ssid_len;
1418 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1419 dev->flags &= ~P2P_DEV_USER_REJECTED;
1420 dev->go_neg_req_sent = 0;
1421 dev->go_state = UNKNOWN_GO;
1422 p2p_set_dev_persistent(dev, persistent_group);
1423 p2p->go_intent = go_intent;
1424 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1426 dev->wps_method = wps_method;
1427 dev->status = P2P_SC_SUCCESS;
1430 dev->flags |= P2P_DEV_FORCE_FREQ;
1432 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1438 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1439 struct p2p_device *dev, struct p2p_message *msg)
1441 os_get_time(&dev->last_seen);
1443 p2p_copy_wps_info(dev, 0, msg);
1445 if (msg->listen_channel) {
1447 freq = p2p_channel_to_freq((char *) msg->listen_channel,
1448 msg->listen_channel[3],
1449 msg->listen_channel[4]);
1451 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1452 "P2P: Unknown peer Listen channel: "
1453 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1454 msg->listen_channel[0],
1455 msg->listen_channel[1],
1456 msg->listen_channel[2],
1457 msg->listen_channel[3],
1458 msg->listen_channel[4]);
1460 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1461 "peer " MACSTR " Listen channel: %u -> %u MHz",
1462 MAC2STR(dev->info.p2p_device_addr),
1463 dev->listen_freq, freq);
1464 dev->listen_freq = freq;
1468 if (msg->wfd_subelems) {
1469 wpabuf_free(dev->info.wfd_subelems);
1470 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1473 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1474 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1475 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1476 "P2P: Completed device entry based on data from "
1477 "GO Negotiation Request");
1479 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1480 "P2P: Created device entry based on GO Neg Req: "
1481 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1483 MAC2STR(dev->info.p2p_device_addr),
1484 dev->info.dev_capab, dev->info.group_capab,
1485 dev->info.device_name, dev->listen_freq);
1488 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1490 if (dev->flags & P2P_DEV_USER_REJECTED) {
1491 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1492 "P2P: Do not report rejected device");
1496 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1497 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1498 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1502 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1504 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1505 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1506 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1507 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1508 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1512 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1514 p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len);
1515 p2p_random(params->passphrase, 8);
1520 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1522 struct p2p_go_neg_results res;
1523 int go = peer->go_state == LOCAL_GO;
1524 struct p2p_channels intersection;
1528 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1529 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1530 "GO)", MAC2STR(peer->info.p2p_device_addr),
1531 go ? "local end" : "peer");
1533 os_memset(&res, 0, sizeof(res));
1535 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1536 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1537 res.wps_method = peer->wps_method;
1538 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1539 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1540 res.persistent_group = 2;
1542 res.persistent_group = 1;
1546 /* Setup AP mode for WPS provisioning */
1547 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1550 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1551 res.ssid_len = p2p->ssid_len;
1552 p2p_random(res.passphrase, 8);
1554 res.freq = peer->oper_freq;
1555 if (p2p->ssid_len) {
1556 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1557 res.ssid_len = p2p->ssid_len;
1561 p2p_channels_intersect(&p2p->channels, &peer->channels,
1564 for (i = 0; i < intersection.reg_classes; i++) {
1565 struct p2p_reg_class *c = &intersection.reg_class[i];
1566 if (freqs + 1 == P2P_MAX_CHANNELS)
1568 for (j = 0; j < c->channels; j++) {
1570 if (freqs + 1 == P2P_MAX_CHANNELS)
1572 freq = p2p_channel_to_freq(peer->country, c->reg_class,
1576 res.freq_list[freqs++] = freq;
1580 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1582 p2p_clear_timeout(p2p);
1584 peer->go_neg_req_sent = 0;
1585 peer->wps_method = WPS_NOT_READY;
1587 p2p_set_state(p2p, P2P_PROVISIONING);
1588 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1592 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1593 const u8 *data, size_t len, int rx_freq)
1595 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1596 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1597 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1603 case P2P_GO_NEG_REQ:
1604 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1606 case P2P_GO_NEG_RESP:
1607 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1609 case P2P_GO_NEG_CONF:
1610 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1612 case P2P_INVITATION_REQ:
1613 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1616 case P2P_INVITATION_RESP:
1617 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1619 case P2P_PROV_DISC_REQ:
1620 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1622 case P2P_PROV_DISC_RESP:
1623 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1625 case P2P_DEV_DISC_REQ:
1626 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1628 case P2P_DEV_DISC_RESP:
1629 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1632 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1633 "P2P: Unsupported P2P Public Action frame type %d",
1640 static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1641 const u8 *sa, const u8 *bssid, const u8 *data,
1642 size_t len, int freq)
1648 case WLAN_PA_VENDOR_SPECIFIC:
1653 if (WPA_GET_BE24(data) != OUI_WFA)
1661 if (*data != P2P_OUI_TYPE)
1664 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1666 case WLAN_PA_GAS_INITIAL_REQ:
1667 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1669 case WLAN_PA_GAS_INITIAL_RESP:
1670 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1672 case WLAN_PA_GAS_COMEBACK_REQ:
1673 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1675 case WLAN_PA_GAS_COMEBACK_RESP:
1676 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1682 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1683 const u8 *bssid, u8 category,
1684 const u8 *data, size_t len, int freq)
1686 if (category == WLAN_ACTION_PUBLIC) {
1687 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1691 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1697 if (WPA_GET_BE24(data) != OUI_WFA)
1702 if (*data != P2P_OUI_TYPE)
1707 /* P2P action frame */
1708 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1709 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1710 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1716 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1717 "P2P: Received P2P Action - Notice of Absence");
1720 case P2P_PRESENCE_REQ:
1721 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1723 case P2P_PRESENCE_RESP:
1724 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1726 case P2P_GO_DISC_REQ:
1727 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1730 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1731 "P2P: Received P2P Action - unknown type %u", data[0]);
1737 static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1739 struct p2p_data *p2p = eloop_ctx;
1740 if (p2p->go_neg_peer == NULL)
1742 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1743 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1744 p2p_connect_send(p2p, p2p->go_neg_peer);
1748 static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1750 struct p2p_data *p2p = eloop_ctx;
1751 if (p2p->invite_peer == NULL)
1753 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1754 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1758 static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1759 const u8 *ie, size_t ie_len)
1761 struct p2p_message msg;
1762 struct p2p_device *dev;
1764 os_memset(&msg, 0, sizeof(msg));
1765 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1767 p2p_parse_free(&msg);
1768 return; /* not a P2P probe */
1771 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1772 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1774 /* The Probe Request is not part of P2P Device Discovery. It is
1775 * not known whether the source address of the frame is the P2P
1776 * Device Address or P2P Interface Address. Do not add a new
1777 * peer entry based on this frames.
1779 p2p_parse_free(&msg);
1783 dev = p2p_get_device(p2p, addr);
1785 if (dev->country[0] == 0 && msg.listen_channel)
1786 os_memcpy(dev->country, msg.listen_channel, 3);
1787 os_get_time(&dev->last_seen);
1788 p2p_parse_free(&msg);
1789 return; /* already known */
1792 dev = p2p_create_device(p2p, addr);
1794 p2p_parse_free(&msg);
1798 os_get_time(&dev->last_seen);
1799 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1801 if (msg.listen_channel) {
1802 os_memcpy(dev->country, msg.listen_channel, 3);
1803 dev->listen_freq = p2p_channel_to_freq(dev->country,
1804 msg.listen_channel[3],
1805 msg.listen_channel[4]);
1808 p2p_copy_wps_info(dev, 1, &msg);
1810 if (msg.wfd_subelems) {
1811 wpabuf_free(dev->info.wfd_subelems);
1812 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
1815 p2p_parse_free(&msg);
1817 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1818 "P2P: Created device entry based on Probe Req: " MACSTR
1819 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1820 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1821 dev->info.group_capab, dev->info.device_name,
1826 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1828 struct p2p_message *msg)
1830 struct p2p_device *dev;
1832 dev = p2p_get_device(p2p, addr);
1834 os_get_time(&dev->last_seen);
1835 return dev; /* already known */
1838 dev = p2p_create_device(p2p, addr);
1842 p2p_add_dev_info(p2p, addr, dev, msg);
1848 static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1850 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1852 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1853 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1854 WPA_GET_BE16(&req_dev_type[6]) == 0)
1855 return 1; /* Category match with wildcard OUI/sub-category */
1860 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1861 size_t num_req_dev_type)
1864 for (i = 0; i < num_req_dev_type; i++) {
1865 if (dev_type_match(dev_type, req_dev_type[i]))
1873 * p2p_match_dev_type - Match local device type with requested type
1874 * @p2p: P2P module context from p2p_init()
1875 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1876 * Returns: 1 on match, 0 on mismatch
1878 * This function can be used to match the Requested Device Type attribute in
1879 * WPS IE with the local device types for deciding whether to reply to a Probe
1882 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1884 struct wps_parse_attr attr;
1887 if (wps_parse_msg(wps, &attr))
1888 return 1; /* assume no Requested Device Type attributes */
1890 if (attr.num_req_dev_type == 0)
1891 return 1; /* no Requested Device Type attributes -> match */
1893 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1894 attr.num_req_dev_type))
1895 return 1; /* Own Primary Device Type matches */
1897 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1898 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1900 attr.num_req_dev_type))
1901 return 1; /* Own Secondary Device Type matches */
1903 /* No matching device type found */
1908 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1915 #ifdef CONFIG_WIFI_DISPLAY
1916 if (p2p->wfd_ie_probe_resp)
1917 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
1918 #endif /* CONFIG_WIFI_DISPLAY */
1920 buf = wpabuf_alloc(1000 + extra);
1924 if (p2p->go_neg_peer) {
1925 /* Advertise immediate availability of WPS credential */
1926 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
1929 p2p_build_wps_ie(p2p, buf, pw_id, 1);
1931 #ifdef CONFIG_WIFI_DISPLAY
1932 if (p2p->wfd_ie_probe_resp)
1933 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
1934 #endif /* CONFIG_WIFI_DISPLAY */
1937 len = p2p_buf_add_ie_hdr(buf);
1938 p2p_buf_add_capability(buf, p2p->dev_capab &
1939 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
1940 if (p2p->ext_listen_interval)
1941 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1942 p2p->ext_listen_interval);
1943 p2p_buf_add_device_info(buf, p2p, NULL);
1944 p2p_buf_update_ie_hdr(buf, len);
1950 static int is_11b(u8 rate)
1952 return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
1956 static int supp_rates_11b_only(struct ieee802_11_elems *elems)
1958 int num_11b = 0, num_others = 0;
1961 if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
1964 for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
1965 if (is_11b(elems->supp_rates[i]))
1971 for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
1973 if (is_11b(elems->ext_supp_rates[i]))
1979 return num_11b > 0 && num_others == 0;
1983 static enum p2p_probe_req_status
1984 p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
1985 const u8 *bssid, const u8 *ie, size_t ie_len)
1987 struct ieee802_11_elems elems;
1989 struct ieee80211_mgmt *resp;
1990 struct p2p_message msg;
1993 if (!p2p->in_listen || !p2p->drv_in_listen) {
1994 /* not in Listen state - ignore Probe Request */
1995 return P2P_PREQ_NOT_LISTEN;
1998 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
2000 /* Ignore invalid Probe Request frames */
2001 return P2P_PREQ_MALFORMED;
2004 if (elems.p2p == NULL) {
2005 /* not a P2P probe - ignore it */
2006 return P2P_PREQ_NOT_P2P;
2009 if (dst && !is_broadcast_ether_addr(dst) &&
2010 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2011 /* Not sent to the broadcast address or our P2P Device Address
2013 return P2P_PREQ_NOT_PROCESSED;
2016 if (bssid && !is_broadcast_ether_addr(bssid)) {
2017 /* Not sent to the Wildcard BSSID */
2018 return P2P_PREQ_NOT_PROCESSED;
2021 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
2022 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
2024 /* not using P2P Wildcard SSID - ignore */
2025 return P2P_PREQ_NOT_PROCESSED;
2028 if (supp_rates_11b_only(&elems)) {
2029 /* Indicates support for 11b rates only */
2030 return P2P_PREQ_NOT_P2P;
2033 os_memset(&msg, 0, sizeof(msg));
2034 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
2035 /* Could not parse P2P attributes */
2036 return P2P_PREQ_NOT_P2P;
2039 if (msg.device_id &&
2040 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
2041 /* Device ID did not match */
2042 p2p_parse_free(&msg);
2043 return P2P_PREQ_NOT_PROCESSED;
2046 /* Check Requested Device Type match */
2047 if (msg.wps_attributes &&
2048 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
2049 /* No match with Requested Device Type */
2050 p2p_parse_free(&msg);
2051 return P2P_PREQ_NOT_PROCESSED;
2053 p2p_parse_free(&msg);
2055 if (!p2p->cfg->send_probe_resp) {
2056 /* Response generated elsewhere */
2057 return P2P_PREQ_NOT_PROCESSED;
2060 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2061 "P2P: Reply to P2P Probe Request in Listen state");
2064 * We do not really have a specific BSS that this frame is advertising,
2065 * so build a frame that has some information in valid format. This is
2066 * really only used for discovery purposes, not to learn exact BSS
2069 ies = p2p_build_probe_resp_ies(p2p);
2071 return P2P_PREQ_NOT_PROCESSED;
2073 buf = wpabuf_alloc(200 + wpabuf_len(ies));
2076 return P2P_PREQ_NOT_PROCESSED;
2080 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
2082 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2083 (WLAN_FC_STYPE_PROBE_RESP << 4));
2084 os_memcpy(resp->da, addr, ETH_ALEN);
2085 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2086 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2087 resp->u.probe_resp.beacon_int = host_to_le16(100);
2088 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2089 resp->u.probe_resp.capab_info =
2090 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2091 WLAN_CAPABILITY_PRIVACY |
2092 WLAN_CAPABILITY_SHORT_SLOT_TIME);
2094 wpabuf_put_u8(buf, WLAN_EID_SSID);
2095 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2096 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2098 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2099 wpabuf_put_u8(buf, 8);
2100 wpabuf_put_u8(buf, (60 / 5) | 0x80);
2101 wpabuf_put_u8(buf, 90 / 5);
2102 wpabuf_put_u8(buf, (120 / 5) | 0x80);
2103 wpabuf_put_u8(buf, 180 / 5);
2104 wpabuf_put_u8(buf, (240 / 5) | 0x80);
2105 wpabuf_put_u8(buf, 360 / 5);
2106 wpabuf_put_u8(buf, 480 / 5);
2107 wpabuf_put_u8(buf, 540 / 5);
2109 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2110 wpabuf_put_u8(buf, 1);
2111 wpabuf_put_u8(buf, p2p->cfg->channel);
2113 wpabuf_put_buf(buf, ies);
2116 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
2120 return P2P_PREQ_NOT_PROCESSED;
2124 enum p2p_probe_req_status
2125 p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2126 const u8 *bssid, const u8 *ie, size_t ie_len)
2128 enum p2p_probe_req_status res;
2130 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2132 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
2134 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2136 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
2138 /* Received a Probe Request from GO Negotiation peer */
2139 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2140 "P2P: Found GO Negotiation peer - try to start GO "
2141 "negotiation from timeout");
2142 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
2143 return P2P_PREQ_PROCESSED;
2146 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2148 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2150 /* Received a Probe Request from Invite peer */
2151 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2152 "P2P: Found Invite peer - try to start Invite from "
2154 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
2155 return P2P_PREQ_PROCESSED;
2162 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2163 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2172 return 0; /* WLAN AP is not a P2P manager */
2175 * (Re)Association Request - P2P IE
2176 * P2P Capability attribute (shall be present)
2177 * P2P Interface attribute (present if concurrent device and
2178 * P2P Management is enabled)
2180 tmp = wpabuf_alloc(200);
2184 lpos = p2p_buf_add_ie_hdr(tmp);
2186 if (p2p->num_groups > 0) {
2187 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2188 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2189 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2191 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2193 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2194 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2195 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2196 p2p_buf_add_p2p_interface(tmp, p2p);
2197 p2p_buf_update_ie_hdr(tmp, lpos);
2199 tmplen = wpabuf_len(tmp);
2203 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2212 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2213 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2217 struct p2p_device *peer;
2223 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2225 #ifdef CONFIG_WIFI_DISPLAY
2226 if (p2p->wfd_ie_assoc_req)
2227 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2228 #endif /* CONFIG_WIFI_DISPLAY */
2231 * (Re)Association Request - P2P IE
2232 * P2P Capability attribute (shall be present)
2233 * Extended Listen Timing (may be present)
2234 * P2P Device Info attribute (shall be present)
2236 tmp = wpabuf_alloc(200 + extra);
2240 #ifdef CONFIG_WIFI_DISPLAY
2241 if (p2p->wfd_ie_assoc_req)
2242 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2243 #endif /* CONFIG_WIFI_DISPLAY */
2245 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2247 lpos = p2p_buf_add_ie_hdr(tmp);
2248 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2249 if (p2p->ext_listen_interval)
2250 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2251 p2p->ext_listen_interval);
2252 p2p_buf_add_device_info(tmp, p2p, peer);
2253 p2p_buf_update_ie_hdr(tmp, lpos);
2255 tmplen = wpabuf_len(tmp);
2259 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2268 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2270 struct wpabuf *p2p_ie;
2273 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2277 ret = p2p_attr_text(p2p_ie, buf, end);
2278 wpabuf_free(p2p_ie);
2283 int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2285 struct p2p_message msg;
2287 os_memset(&msg, 0, sizeof(msg));
2288 if (p2p_parse_p2p_ie(p2p_ie, &msg))
2291 if (msg.p2p_device_addr) {
2292 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2294 } else if (msg.device_id) {
2295 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2302 int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2304 struct wpabuf *p2p_ie;
2307 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2308 P2P_IE_VENDOR_TYPE);
2311 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
2312 wpabuf_free(p2p_ie);
2317 static void p2p_clear_go_neg(struct p2p_data *p2p)
2319 p2p->go_neg_peer = NULL;
2320 p2p_clear_timeout(p2p);
2321 p2p_set_state(p2p, P2P_IDLE);
2325 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2327 if (p2p->go_neg_peer == NULL) {
2328 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2329 "P2P: No pending Group Formation - "
2330 "ignore WPS registration success notification");
2331 return; /* No pending Group Formation */
2334 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2336 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2337 "P2P: Ignore WPS registration success notification "
2338 "for " MACSTR " (GO Negotiation peer " MACSTR ")",
2340 MAC2STR(p2p->go_neg_peer->intended_addr));
2341 return; /* Ignore unexpected peer address */
2344 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2345 "P2P: Group Formation completed successfully with " MACSTR,
2348 p2p_clear_go_neg(p2p);
2352 void p2p_group_formation_failed(struct p2p_data *p2p)
2354 if (p2p->go_neg_peer == NULL) {
2355 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2356 "P2P: No pending Group Formation - "
2357 "ignore group formation failure notification");
2358 return; /* No pending Group Formation */
2361 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2362 "P2P: Group Formation failed with " MACSTR,
2363 MAC2STR(p2p->go_neg_peer->intended_addr));
2365 p2p_clear_go_neg(p2p);
2369 struct p2p_data * p2p_init(const struct p2p_config *cfg)
2371 struct p2p_data *p2p;
2373 if (cfg->max_peers < 1)
2376 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2379 p2p->cfg = (struct p2p_config *) (p2p + 1);
2380 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2382 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2383 if (cfg->manufacturer)
2384 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2385 if (cfg->model_name)
2386 p2p->cfg->model_name = os_strdup(cfg->model_name);
2387 if (cfg->model_number)
2388 p2p->cfg->model_number = os_strdup(cfg->model_number);
2389 if (cfg->serial_number)
2390 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
2391 if (cfg->pref_chan) {
2392 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2393 sizeof(struct p2p_channel));
2394 if (p2p->cfg->pref_chan) {
2395 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2396 cfg->num_pref_chan *
2397 sizeof(struct p2p_channel));
2399 p2p->cfg->num_pref_chan = 0;
2403 /* 100ms listen time is too less to receive the response frames in some scenarios
2404 * increasing min listen time to 200ms.
2406 p2p->min_disc_int = 2;
2407 /* SD_FAIR_POLICY: Initializing the SD current serviced pointer to NULL */
2408 p2p->sd_dev_list = NULL;
2410 p2p->min_disc_int = 1;
2412 p2p->max_disc_int = 3;
2414 os_get_random(&p2p->next_tie_breaker, 1);
2415 p2p->next_tie_breaker &= 0x01;
2416 if (cfg->sd_request)
2417 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2418 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2419 if (cfg->concurrent_operations)
2420 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2421 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2423 dl_list_init(&p2p->devices);
2425 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
2426 p2p_expiration_timeout, p2p, NULL);
2428 p2p->go_timeout = 100;
2429 p2p->client_timeout = 20;
2435 void p2p_deinit(struct p2p_data *p2p)
2437 #ifdef CONFIG_WIFI_DISPLAY
2438 wpabuf_free(p2p->wfd_ie_beacon);
2439 wpabuf_free(p2p->wfd_ie_probe_req);
2440 wpabuf_free(p2p->wfd_ie_probe_resp);
2441 wpabuf_free(p2p->wfd_ie_assoc_req);
2442 wpabuf_free(p2p->wfd_ie_invitation);
2443 wpabuf_free(p2p->wfd_ie_prov_disc_req);
2444 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
2445 wpabuf_free(p2p->wfd_ie_go_neg);
2446 wpabuf_free(p2p->wfd_dev_info);
2447 wpabuf_free(p2p->wfd_assoc_bssid);
2448 wpabuf_free(p2p->wfd_coupled_sink_info);
2449 #endif /* CONFIG_WIFI_DISPLAY */
2451 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
2452 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2453 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2455 p2p_free_req_dev_types(p2p);
2456 os_free(p2p->cfg->dev_name);
2457 os_free(p2p->cfg->manufacturer);
2458 os_free(p2p->cfg->model_name);
2459 os_free(p2p->cfg->model_number);
2460 os_free(p2p->cfg->serial_number);
2461 os_free(p2p->cfg->pref_chan);
2462 os_free(p2p->groups);
2463 wpabuf_free(p2p->sd_resp);
2464 os_free(p2p->after_scan_tx);
2465 p2p_remove_wps_vendor_extensions(p2p);
2470 void p2p_flush(struct p2p_data *p2p)
2472 struct p2p_device *dev, *prev;
2474 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2476 dl_list_del(&dev->list);
2477 p2p_device_free(p2p, dev);
2480 /* SD_FAIR_POLICY: Initializing the SD current serviced pointer to NULL */
2481 p2p->sd_dev_list = NULL;
2483 p2p_free_sd_queries(p2p);
2484 os_free(p2p->after_scan_tx);
2485 p2p->after_scan_tx = NULL;
2489 int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2491 struct p2p_device *dev;
2493 dev = p2p_get_device(p2p, addr);
2497 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
2500 if (p2p->go_neg_peer == dev)
2501 p2p->go_neg_peer = NULL;
2503 dev->wps_method = WPS_NOT_READY;
2504 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2505 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2507 /* Check if after_scan_tx is for this peer. If so free it */
2508 if (p2p->after_scan_tx &&
2509 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2510 os_free(p2p->after_scan_tx);
2511 p2p->after_scan_tx = NULL;
2518 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2520 os_free(p2p->cfg->dev_name);
2522 p2p->cfg->dev_name = os_strdup(dev_name);
2523 if (p2p->cfg->dev_name == NULL)
2526 p2p->cfg->dev_name = NULL;
2531 int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2533 os_free(p2p->cfg->manufacturer);
2534 p2p->cfg->manufacturer = NULL;
2536 p2p->cfg->manufacturer = os_strdup(manufacturer);
2537 if (p2p->cfg->manufacturer == NULL)
2545 int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2547 os_free(p2p->cfg->model_name);
2548 p2p->cfg->model_name = NULL;
2550 p2p->cfg->model_name = os_strdup(model_name);
2551 if (p2p->cfg->model_name == NULL)
2559 int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2561 os_free(p2p->cfg->model_number);
2562 p2p->cfg->model_number = NULL;
2564 p2p->cfg->model_number = os_strdup(model_number);
2565 if (p2p->cfg->model_number == NULL)
2573 int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2575 os_free(p2p->cfg->serial_number);
2576 p2p->cfg->serial_number = NULL;
2577 if (serial_number) {
2578 p2p->cfg->serial_number = os_strdup(serial_number);
2579 if (p2p->cfg->serial_number == NULL)
2587 void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2589 p2p->cfg->config_methods = config_methods;
2593 void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2595 os_memcpy(p2p->cfg->uuid, uuid, 16);
2599 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2601 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2606 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2607 size_t num_dev_types)
2609 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2610 num_dev_types = P2P_SEC_DEVICE_TYPES;
2611 p2p->cfg->num_sec_dev_types = num_dev_types;
2612 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2617 void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2621 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2622 wpabuf_free(p2p->wps_vendor_ext[i]);
2623 p2p->wps_vendor_ext[i] = NULL;
2628 int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2629 const struct wpabuf *vendor_ext)
2633 if (vendor_ext == NULL)
2636 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2637 if (p2p->wps_vendor_ext[i] == NULL)
2640 if (i >= P2P_MAX_WPS_VENDOR_EXT)
2643 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2644 if (p2p->wps_vendor_ext[i] == NULL)
2651 int p2p_set_country(struct p2p_data *p2p, const char *country)
2653 os_memcpy(p2p->cfg->country, country, 3);
2658 void p2p_continue_find(struct p2p_data *p2p)
2660 struct p2p_device *dev;
2664 p2p_set_state(p2p, P2P_SEARCH);
2665 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2667 /* SD_FAIR_POLICY: We need to give chance to all devices in the device list
2668 * There may be a scenario, where a particular peer device have
2669 * not registered any query response. When we send a SD request to such device,
2670 * no response will be received. And if we continue to get probe responses from that device,
2671 * and if that device happens to be on top in our device list,
2672 * we will always continue to send SD requests always to that peer only.
2673 * We will not be able to send SD requests to other devices in that case.
2674 * This implementation keeps track of last serviced peer device.
2675 * And then takes the next one from the device list, in the next iteration.
2677 if (p2p->sd_dev_list && p2p->sd_dev_list != &p2p->devices) {
2679 if ((&dev->list == p2p->sd_dev_list) ) {
2681 if (dev->list.next == &p2p->devices)
2682 p2p->sd_dev_list = NULL;
2687 p2p->sd_dev_list = &dev->list;
2688 wpa_printf(MSG_DEBUG, "P2P: ### Servicing %p dev->flags 0x%x SD schedule %s devaddr " MACSTR,
2689 p2p->sd_dev_list, dev->flags, dev->flags & P2P_DEV_SD_SCHEDULE ? "TRUE": "FALSE",
2690 MAC2STR(dev->info.p2p_device_addr));
2692 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2693 if (p2p_start_sd(p2p, dev) == 0)
2697 } else if (dev->req_config_methods &&
2698 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2699 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2700 "pending Provision Discovery Request to "
2701 MACSTR " (config methods 0x%x)",
2702 MAC2STR(dev->info.p2p_device_addr),
2703 dev->req_config_methods);
2704 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
2709 p2p_listen_in_find(p2p);
2713 static void p2p_sd_cb(struct p2p_data *p2p, int success)
2715 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2716 "P2P: Service Discovery Query TX callback: success=%d",
2718 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2722 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2723 p2p->sd_peer = NULL;
2725 p2p_continue_find(p2p);
2729 if (p2p->sd_peer == NULL) {
2730 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2731 "P2P: No SD peer entry known");
2732 p2p_continue_find(p2p);
2736 /* Wait for response from the peer */
2737 p2p_set_state(p2p, P2P_SD_DURING_FIND);
2738 p2p_set_timeout(p2p, 0, 200000);
2743 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2744 * @p2p: P2P module context from p2p_init()
2746 static void p2p_retry_pd(struct p2p_data *p2p)
2748 struct p2p_device *dev;
2750 if (p2p->state != P2P_IDLE)
2754 * Retry the prov disc req attempt only for the peer that the user had
2755 * requested for and provided a join has not been initiated on it
2759 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2760 if (os_memcmp(p2p->pending_pd_devaddr,
2761 dev->info.p2p_device_addr, ETH_ALEN) != 0)
2763 if (!dev->req_config_methods)
2765 if (dev->flags & P2P_DEV_PD_FOR_JOIN)
2768 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2769 "pending Provision Discovery Request to "
2770 MACSTR " (config methods 0x%x)",
2771 MAC2STR(dev->info.p2p_device_addr),
2772 dev->req_config_methods);
2773 p2p_send_prov_disc_req(p2p, dev, 0, 0);
2779 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2781 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2782 "P2P: Provision Discovery Request TX callback: success=%d",
2786 * Postpone resetting the pending action state till after we actually
2787 * time out. This allows us to take some action like notifying any
2788 * interested parties about no response to the request.
2790 * When the timer (below) goes off we check in IDLE, SEARCH, or
2791 * LISTEN_ONLY state, which are the only allowed states to issue a PD
2792 * requests in, if this was still pending and then raise notification.
2796 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2798 if (p2p->user_initiated_pd &&
2799 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
2801 /* Retry request from timeout to avoid busy loops */
2802 p2p->pending_action_state = P2P_PENDING_PD;
2803 p2p_set_timeout(p2p, 0, 50000);
2804 } else if (p2p->state != P2P_IDLE)
2805 p2p_continue_find(p2p);
2806 else if (p2p->user_initiated_pd) {
2807 p2p->pending_action_state = P2P_PENDING_PD;
2808 p2p_set_timeout(p2p, 0, 300000);
2814 * This postponing, of resetting pending_action_state, needs to be
2815 * done only for user initiated PD requests and not internal ones.
2817 if (p2p->user_initiated_pd)
2818 p2p->pending_action_state = P2P_PENDING_PD;
2820 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2822 /* Wait for response from the peer */
2823 if (p2p->state == P2P_SEARCH)
2824 p2p_set_state(p2p, P2P_PD_DURING_FIND);
2825 p2p_set_timeout(p2p, 0, 200000);
2829 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
2830 int level, const u8 *ies, size_t ies_len)
2832 p2p_add_device(p2p, bssid, freq, level, ies, ies_len, 1);
2838 void p2p_scan_res_handled(struct p2p_data *p2p)
2840 if (!p2p->p2p_scan_running) {
2841 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
2842 "running, but scan results received");
2844 p2p->p2p_scan_running = 0;
2845 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2847 if (p2p_run_after_scan(p2p))
2849 if (p2p->state == P2P_SEARCH)
2850 p2p_continue_find(p2p);
2854 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
2858 #ifdef CONFIG_WIFI_DISPLAY
2859 if (p2p->wfd_ie_probe_req)
2860 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
2861 #endif /* CONFIG_WIFI_DISPLAY */
2863 len = p2p_buf_add_ie_hdr(ies);
2864 p2p_buf_add_capability(ies, p2p->dev_capab &
2865 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
2867 p2p_buf_add_device_id(ies, dev_id);
2868 if (p2p->cfg->reg_class && p2p->cfg->channel)
2869 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2870 p2p->cfg->reg_class,
2872 if (p2p->ext_listen_interval)
2873 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2874 p2p->ext_listen_interval);
2875 /* TODO: p2p_buf_add_operating_channel() if GO */
2876 p2p_buf_update_ie_hdr(ies, len);
2880 size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
2884 #ifdef CONFIG_WIFI_DISPLAY
2885 if (p2p && p2p->wfd_ie_probe_req)
2886 len += wpabuf_len(p2p->wfd_ie_probe_req);
2887 #endif /* CONFIG_WIFI_DISPLAY */
2893 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2895 return p2p_attr_text(p2p_ie, buf, end);
2899 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2901 struct p2p_device *dev = p2p->go_neg_peer;
2903 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2904 "P2P: GO Negotiation Request TX callback: success=%d",
2908 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2909 "P2P: No pending GO Negotiation");
2914 if (dev->flags & P2P_DEV_USER_REJECTED) {
2915 p2p_set_state(p2p, P2P_IDLE);
2918 } else if (dev->go_neg_req_sent) {
2919 /* Cancel the increment from p2p_connect_send() on failure */
2920 dev->go_neg_req_sent--;
2924 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2925 !is_zero_ether_addr(dev->member_in_go_dev)) {
2926 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2927 "P2P: Peer " MACSTR " did not acknowledge request - "
2928 "try to use device discoverability through its GO",
2929 MAC2STR(dev->info.p2p_device_addr));
2930 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2931 p2p_send_dev_disc_req(p2p, dev);
2936 * Use P2P find, if needed, to find the other device from its listen
2939 p2p_set_state(p2p, P2P_CONNECT);
2940 p2p_set_timeout(p2p, 0, success ? 200000 : 100000);
2944 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2946 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2947 "P2P: GO Negotiation Response TX callback: success=%d",
2949 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2950 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2951 "P2P: Ignore TX callback event - GO Negotiation is "
2952 "not running anymore");
2955 p2p_set_state(p2p, P2P_CONNECT);
2956 p2p_set_timeout(p2p, 0, 250000);
2960 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2962 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2963 "P2P: GO Negotiation Response (failure) TX callback: "
2964 "success=%d", success);
2965 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2966 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2967 p2p->go_neg_peer->status);
2972 static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
2973 enum p2p_send_action_result result)
2975 struct p2p_device *dev;
2977 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2978 "P2P: GO Negotiation Confirm TX callback: result=%d",
2980 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2981 if (result == P2P_SEND_ACTION_FAILED) {
2982 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2985 if (result == P2P_SEND_ACTION_NO_ACK) {
2987 * It looks like the TX status for GO Negotiation Confirm is
2988 * often showing failure even when the peer has actually
2989 * received the frame. Since the peer may change channels
2990 * immediately after having received the frame, we may not see
2991 * an Ack for retries, so just dropping a single frame may
2992 * trigger this. To allow the group formation to succeed if the
2993 * peer did indeed receive the frame, continue regardless of
2996 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2997 "P2P: Assume GO Negotiation Confirm TX was actually "
2998 "received by the peer even though Ack was not "
3002 dev = p2p->go_neg_peer;
3006 p2p_go_complete(p2p, dev);
3010 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3011 const u8 *src, const u8 *bssid,
3012 enum p2p_send_action_result result)
3014 enum p2p_pending_action_state state;
3017 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3018 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
3019 " src=" MACSTR " bssid=" MACSTR " result=%d",
3020 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
3021 MAC2STR(bssid), result);
3022 success = result == P2P_SEND_ACTION_SUCCESS;
3023 state = p2p->pending_action_state;
3024 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3026 case P2P_NO_PENDING_ACTION:
3028 case P2P_PENDING_GO_NEG_REQUEST:
3029 p2p_go_neg_req_cb(p2p, success);
3031 case P2P_PENDING_GO_NEG_RESPONSE:
3032 p2p_go_neg_resp_cb(p2p, success);
3034 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
3035 p2p_go_neg_resp_failure_cb(p2p, success);
3037 case P2P_PENDING_GO_NEG_CONFIRM:
3038 p2p_go_neg_conf_cb(p2p, result);
3040 case P2P_PENDING_SD:
3041 p2p_sd_cb(p2p, success);
3043 case P2P_PENDING_PD:
3044 p2p_prov_disc_cb(p2p, success);
3046 case P2P_PENDING_INVITATION_REQUEST:
3047 p2p_invitation_req_cb(p2p, success);
3049 case P2P_PENDING_INVITATION_RESPONSE:
3050 p2p_invitation_resp_cb(p2p, success);
3052 case P2P_PENDING_DEV_DISC_REQUEST:
3053 p2p_dev_disc_req_cb(p2p, success);
3055 case P2P_PENDING_DEV_DISC_RESPONSE:
3056 p2p_dev_disc_resp_cb(p2p, success);
3058 case P2P_PENDING_GO_DISC_REQ:
3059 p2p_go_disc_req_cb(p2p, success);
3065 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
3066 unsigned int duration)
3068 if (freq == p2p->pending_client_disc_freq) {
3069 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3070 "P2P: Client discoverability remain-awake completed");
3071 p2p->pending_client_disc_freq = 0;
3075 if (freq != p2p->pending_listen_freq) {
3076 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3077 "P2P: Unexpected listen callback for freq=%u "
3078 "duration=%u (pending_listen_freq=%u)",
3079 freq, duration, p2p->pending_listen_freq);
3083 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3084 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
3086 p2p->pending_listen_sec, p2p->pending_listen_usec,
3087 p2p->pending_listen_freq);
3089 p2p->drv_in_listen = freq;
3090 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
3092 * Add 20 msec extra wait to avoid race condition with driver
3093 * remain-on-channel end event, i.e., give driver more time to
3094 * complete the operation before our timeout expires.
3096 p2p_set_timeout(p2p, p2p->pending_listen_sec,
3097 p2p->pending_listen_usec + 20000);
3100 p2p->pending_listen_freq = 0;
3104 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
3106 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
3107 "state (freq=%u)", freq);
3108 p2p->drv_in_listen = 0;
3110 return 0; /* Internal timeout will trigger the next step */
3112 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
3113 if (p2p->go_neg_peer->connect_reqs >= 120) {
3114 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3115 "P2P: Timeout on sending GO Negotiation "
3116 "Request without getting response");
3117 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3121 p2p_set_state(p2p, P2P_CONNECT);
3122 p2p_connect_send(p2p, p2p->go_neg_peer);
3124 } else if (p2p->state == P2P_SEARCH) {
3125 if (p2p->p2p_scan_running) {
3127 * Search is already in progress. This can happen if
3128 * an Action frame RX is reported immediately after
3129 * the end of a remain-on-channel operation and the
3130 * response frame to that is sent using an offchannel
3131 * operation while in p2p_find. Avoid an attempt to
3132 * restart a scan here.
3134 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan "
3135 "already in progress - do not try to start a "
3139 if (p2p->pending_listen_freq) {
3141 * Better wait a bit if the driver is unable to start
3142 * offchannel operation for some reason. p2p_search()
3143 * will be started from internal timeout.
3145 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Listen "
3146 "operation did not seem to start - delay "
3147 "search phase to avoid busy loop");
3148 p2p_set_timeout(p2p, 0, 100000);
3151 if (p2p->search_delay) {
3152 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
3153 "search operation by %u ms",
3155 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3156 (p2p->search_delay % 1000) * 1000);
3167 static void p2p_timeout_connect(struct p2p_data *p2p)
3169 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3170 if (p2p->go_neg_peer &&
3171 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
3172 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Wait for GO "
3173 "Negotiation Confirm timed out - assume GO "
3174 "Negotiation failed");
3175 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3178 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3179 p2p_listen_in_find(p2p);
3183 static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3185 if (p2p->go_neg_peer) {
3186 if (p2p->drv_in_listen) {
3187 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
3188 "still in Listen state; wait for it to "
3193 if (p2p->go_neg_peer->connect_reqs >= 120) {
3194 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3195 "P2P: Timeout on sending GO Negotiation "
3196 "Request without getting response");
3197 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3201 p2p_set_state(p2p, P2P_CONNECT);
3202 p2p_connect_send(p2p, p2p->go_neg_peer);
3204 p2p_set_state(p2p, P2P_IDLE);
3208 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3211 * TODO: could remain constantly in Listen state for some time if there
3212 * are no other concurrent uses for the radio. For now, go to listen
3213 * state once per second to give other uses a chance to use the radio.
3215 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
3216 p2p_set_timeout(p2p, 0, 500000);
3220 static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3222 struct p2p_device *dev = p2p->go_neg_peer;
3225 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3226 "P2P: Unknown GO Neg peer - stop GO Neg wait");
3231 if (dev->wait_count >= 120) {
3232 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3233 "P2P: Timeout on waiting peer to become ready for GO "
3235 p2p_go_neg_failed(p2p, dev, -1);
3239 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3240 "P2P: Go to Listen state while waiting for the peer to become "
3241 "ready for GO Negotiation");
3242 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
3243 p2p_listen_in_find(p2p);
3247 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3249 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3250 "P2P: Service Discovery Query timeout");
3252 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3253 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
3254 p2p->sd_peer = NULL;
3256 p2p_continue_find(p2p);
3260 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
3262 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3263 "P2P: Provision Discovery Request timeout");
3264 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3265 p2p_continue_find(p2p);
3269 static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
3271 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3274 * For user initiated PD requests that we have not gotten any responses
3275 * for while in IDLE state, we retry them a couple of times before
3278 if (!p2p->user_initiated_pd)
3281 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3282 "P2P: User initiated Provision Discovery Request timeout");
3284 if (p2p->pd_retries) {
3288 if (p2p->cfg->prov_disc_fail)
3289 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
3290 p2p->pending_pd_devaddr,
3291 P2P_PROV_DISC_TIMEOUT);
3292 p2p_reset_pending_pd(p2p);
3297 static void p2p_timeout_invite(struct p2p_data *p2p)
3299 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3300 p2p_set_state(p2p, P2P_INVITE_LISTEN);
3301 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
3303 * Better remain on operating channel instead of listen channel
3304 * when running a group.
3306 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
3307 "active GO role - wait on operating channel");
3308 p2p_set_timeout(p2p, 0, 100000);
3311 p2p_listen_in_find(p2p);
3315 static void p2p_timeout_invite_listen(struct p2p_data *p2p)
3317 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
3318 p2p_set_state(p2p, P2P_INVITE);
3319 p2p_invite_send(p2p, p2p->invite_peer,
3320 p2p->invite_go_dev_addr);
3322 if (p2p->invite_peer) {
3323 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3324 "P2P: Invitation Request retry limit reached");
3325 if (p2p->cfg->invitation_result)
3326 p2p->cfg->invitation_result(
3327 p2p->cfg->cb_ctx, -1, NULL);
3329 p2p_set_state(p2p, P2P_IDLE);
3334 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
3336 struct p2p_data *p2p = eloop_ctx;
3338 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
3339 p2p_state_txt(p2p->state));
3343 switch (p2p->state) {
3345 /* Check if we timed out waiting for PD req */
3346 if (p2p->pending_action_state == P2P_PENDING_PD)
3347 p2p_timeout_prov_disc_req(p2p);
3350 /* Check if we timed out waiting for PD req */
3351 if (p2p->pending_action_state == P2P_PENDING_PD)
3352 p2p_timeout_prov_disc_req(p2p);
3353 if (p2p->search_delay && !p2p->in_search_delay) {
3354 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
3355 "search operation by %u ms",
3357 p2p->in_search_delay = 1;
3358 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3359 (p2p->search_delay % 1000) * 1000);
3362 p2p->in_search_delay = 0;
3366 p2p_timeout_connect(p2p);
3368 case P2P_CONNECT_LISTEN:
3369 p2p_timeout_connect_listen(p2p);
3373 case P2P_LISTEN_ONLY:
3374 /* Check if we timed out waiting for PD req */
3375 if (p2p->pending_action_state == P2P_PENDING_PD)
3376 p2p_timeout_prov_disc_req(p2p);
3378 if (p2p->ext_listen_only) {
3379 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3380 "P2P: Extended Listen Timing - Listen State "
3382 p2p->ext_listen_only = 0;
3383 p2p_set_state(p2p, P2P_IDLE);
3386 case P2P_WAIT_PEER_CONNECT:
3387 p2p_timeout_wait_peer_connect(p2p);
3389 case P2P_WAIT_PEER_IDLE:
3390 p2p_timeout_wait_peer_idle(p2p);
3392 case P2P_SD_DURING_FIND:
3393 p2p_timeout_sd_during_find(p2p);
3395 case P2P_PROVISIONING:
3397 case P2P_PD_DURING_FIND:
3398 p2p_timeout_prov_disc_during_find(p2p);
3401 p2p_timeout_invite(p2p);
3403 case P2P_INVITE_LISTEN:
3404 p2p_timeout_invite_listen(p2p);
3406 case P2P_SEARCH_WHEN_READY:
3408 case P2P_CONTINUE_SEARCH_WHEN_READY:
3414 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
3416 struct p2p_device *dev;
3418 dev = p2p_get_device(p2p, peer_addr);
3419 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
3420 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
3422 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
3423 " unknown", MAC2STR(peer_addr));
3426 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
3427 dev->flags |= P2P_DEV_USER_REJECTED;
3432 const char * p2p_wps_method_text(enum p2p_wps_method method)
3437 case WPS_PIN_DISPLAY:
3439 case WPS_PIN_KEYPAD:
3449 static const char * p2p_go_state_text(enum p2p_go_state go_state)
3464 const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
3465 const u8 *addr, int next)
3467 struct p2p_device *dev;
3470 dev = p2p_get_device(p2p, addr);
3472 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3475 dev = dl_list_first(&dev->list, struct p2p_device, list);
3476 if (&dev->list == &p2p->devices)
3487 int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
3488 char *buf, size_t buflen)
3490 struct p2p_device *dev;
3498 dev = (struct p2p_device *) (((u8 *) info) -
3499 offsetof(struct p2p_device, info));
3505 res = os_snprintf(pos, end - pos,
3509 "interface_addr=" MACSTR "\n"
3510 "member_in_go_dev=" MACSTR "\n"
3511 "member_in_go_iface=" MACSTR "\n"
3512 "go_neg_req_sent=%d\n"
3515 "intended_addr=" MACSTR "\n"
3518 "req_config_methods=0x%x\n"
3519 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
3522 "invitation_reqs=%u\n",
3523 (int) (now.sec - dev->last_seen.sec),
3525 p2p_wps_method_text(dev->wps_method),
3526 MAC2STR(dev->interface_addr),
3527 MAC2STR(dev->member_in_go_dev),
3528 MAC2STR(dev->member_in_go_iface),
3529 dev->go_neg_req_sent,
3530 p2p_go_state_text(dev->go_state),
3532 MAC2STR(dev->intended_addr),
3533 dev->country[0] ? dev->country[0] : '_',
3534 dev->country[1] ? dev->country[1] : '_',
3536 dev->req_config_methods,
3537 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
3538 "[PROBE_REQ_ONLY]" : "",
3539 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
3540 dev->flags & P2P_DEV_NOT_YET_READY ?
3541 "[NOT_YET_READY]" : "",
3542 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
3543 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
3545 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
3546 "[PD_PEER_DISPLAY]" : "",
3547 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
3548 "[PD_PEER_KEYPAD]" : "",
3549 dev->flags & P2P_DEV_USER_REJECTED ?
3550 "[USER_REJECTED]" : "",
3551 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
3552 "[PEER_WAITING_RESPONSE]" : "",
3553 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
3554 "[PREFER_PERSISTENT_GROUP]" : "",
3555 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
3556 "[WAIT_GO_NEG_RESPONSE]" : "",
3557 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
3558 "[WAIT_GO_NEG_CONFIRM]" : "",
3559 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
3560 "[GROUP_CLIENT_ONLY]" : "",
3561 dev->flags & P2P_DEV_FORCE_FREQ ?
3562 "[FORCE_FREQ]" : "",
3563 dev->flags & P2P_DEV_PD_FOR_JOIN ?
3564 "[PD_FOR_JOIN]" : "",
3567 dev->invitation_reqs);
3568 if (res < 0 || res >= end - pos)
3572 if (dev->ext_listen_period) {
3573 res = os_snprintf(pos, end - pos,
3574 "ext_listen_period=%u\n"
3575 "ext_listen_interval=%u\n",
3576 dev->ext_listen_period,
3577 dev->ext_listen_interval);
3578 if (res < 0 || res >= end - pos)
3583 if (dev->oper_ssid_len) {
3584 res = os_snprintf(pos, end - pos,
3586 wpa_ssid_txt(dev->oper_ssid,
3587 dev->oper_ssid_len));
3588 if (res < 0 || res >= end - pos)
3593 #ifdef CONFIG_WIFI_DISPLAY
3594 if (dev->info.wfd_subelems) {
3595 res = os_snprintf(pos, end - pos, "wfd_subelems=");
3596 if (res < 0 || res >= end - pos)
3600 pos += wpa_snprintf_hex(pos, end - pos,
3601 wpabuf_head(dev->info.wfd_subelems),
3602 wpabuf_len(dev->info.wfd_subelems));
3604 res = os_snprintf(pos, end - pos, "\n");
3605 if (res < 0 || res >= end - pos)
3609 #endif /* CONFIG_WIFI_DISPLAY */
3615 int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
3617 return p2p_get_device(p2p, addr) != NULL;
3621 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
3624 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3625 "discoverability enabled");
3626 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3628 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3629 "discoverability disabled");
3630 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3635 static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
3636 u32 duration2, u32 interval2)
3639 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
3642 req = wpabuf_alloc(100);
3646 if (duration1 || interval1) {
3647 os_memset(&desc1, 0, sizeof(desc1));
3648 desc1.count_type = 1;
3649 desc1.duration = duration1;
3650 desc1.interval = interval1;
3653 if (duration2 || interval2) {
3654 os_memset(&desc2, 0, sizeof(desc2));
3655 desc2.count_type = 2;
3656 desc2.duration = duration2;
3657 desc2.interval = interval2;
3662 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
3663 len = p2p_buf_add_ie_hdr(req);
3664 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
3665 p2p_buf_update_ie_hdr(req, len);
3671 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
3672 const u8 *own_interface_addr, unsigned int freq,
3673 u32 duration1, u32 interval1, u32 duration2,
3678 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
3679 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
3680 "int1=%u dur2=%u int2=%u",
3681 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
3682 freq, duration1, interval1, duration2, interval2);
3684 req = p2p_build_presence_req(duration1, interval1, duration2,
3689 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3690 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3692 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
3693 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3694 "P2P: Failed to send Action frame");
3702 static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3703 size_t noa_len, u8 dialog_token)
3705 struct wpabuf *resp;
3708 resp = wpabuf_alloc(100 + noa_len);
3712 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3713 len = p2p_buf_add_ie_hdr(resp);
3714 p2p_buf_add_status(resp, status);
3716 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3717 wpabuf_put_le16(resp, noa_len);
3718 wpabuf_put_data(resp, noa, noa_len);
3720 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3721 p2p_buf_update_ie_hdr(resp, len);
3727 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3728 const u8 *sa, const u8 *data, size_t len,
3731 struct p2p_message msg;
3733 struct wpabuf *resp;
3735 struct p2p_group *group = NULL;
3740 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3741 "P2P: Received P2P Action - P2P Presence Request");
3743 for (g = 0; g < p2p->num_groups; g++) {
3744 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3746 group = p2p->groups[g];
3750 if (group == NULL) {
3751 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3752 "P2P: Ignore P2P Presence Request for unknown group "
3753 MACSTR, MAC2STR(da));
3757 if (p2p_parse(data, len, &msg) < 0) {
3758 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3759 "P2P: Failed to parse P2P Presence Request");
3760 status = P2P_SC_FAIL_INVALID_PARAMS;
3765 if (msg.noa == NULL) {
3766 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3767 "P2P: No NoA attribute in P2P Presence Request");
3768 status = P2P_SC_FAIL_INVALID_PARAMS;
3772 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3775 if (p2p->cfg->get_noa)
3776 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3780 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3781 noa_len > 0 ? noa_len : 0,
3784 p2p_parse_free(&msg);
3788 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3789 if (p2p_send_action(p2p, rx_freq, sa, da, da,
3790 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
3791 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3792 "P2P: Failed to send Action frame");
3798 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3799 const u8 *sa, const u8 *data, size_t len)
3801 struct p2p_message msg;
3803 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3804 "P2P: Received P2P Action - P2P Presence Response");
3806 if (p2p_parse(data, len, &msg) < 0) {
3807 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3808 "P2P: Failed to parse P2P Presence Response");
3812 if (msg.status == NULL || msg.noa == NULL) {
3813 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3814 "P2P: No Status or NoA attribute in P2P Presence "
3816 p2p_parse_free(&msg);
3821 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3822 "P2P: P2P Presence Request was rejected: status %u",
3824 p2p_parse_free(&msg);
3828 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3829 "P2P: P2P Presence Request was accepted");
3830 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
3831 msg.noa, msg.noa_len);
3832 /* TODO: process NoA */
3833 p2p_parse_free(&msg);
3837 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3839 struct p2p_data *p2p = eloop_ctx;
3841 if (p2p->ext_listen_interval) {
3842 /* Schedule next extended listen timeout */
3843 eloop_register_timeout(p2p->ext_listen_interval_sec,
3844 p2p->ext_listen_interval_usec,
3845 p2p_ext_listen_timeout, p2p, NULL);
3848 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
3850 * This should not really happen, but it looks like the Listen
3851 * command may fail is something else (e.g., a scan) was
3852 * running at an inconvenient time. As a workaround, allow new
3853 * Extended Listen operation to be started.
3855 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
3856 "Extended Listen operation had not been completed - "
3858 p2p->ext_listen_only = 0;
3859 p2p_set_state(p2p, P2P_IDLE);
3862 if (p2p->state != P2P_IDLE) {
3863 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
3864 "Listen timeout in active state (%s)",
3865 p2p_state_txt(p2p->state));
3869 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
3870 p2p->ext_listen_only = 1;
3871 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
3872 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
3873 "Listen state for Extended Listen Timing");
3874 p2p->ext_listen_only = 0;
3879 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
3880 unsigned int interval)
3882 if (period > 65535 || interval > 65535 || period > interval ||
3883 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
3884 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3885 "P2P: Invalid Extended Listen Timing request: "
3886 "period=%u interval=%u", period, interval);
3890 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
3892 if (interval == 0) {
3893 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3894 "P2P: Disabling Extended Listen Timing");
3895 p2p->ext_listen_period = 0;
3896 p2p->ext_listen_interval = 0;
3900 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3901 "P2P: Enabling Extended Listen Timing: period %u msec, "
3902 "interval %u msec", period, interval);
3903 p2p->ext_listen_period = period;
3904 p2p->ext_listen_interval = interval;
3905 p2p->ext_listen_interval_sec = interval / 1000;
3906 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
3908 eloop_register_timeout(p2p->ext_listen_interval_sec,
3909 p2p->ext_listen_interval_usec,
3910 p2p_ext_listen_timeout, p2p, NULL);
3916 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3917 const u8 *ie, size_t ie_len)
3919 struct p2p_message msg;
3921 if (bssid == NULL || ie == NULL)
3924 os_memset(&msg, 0, sizeof(msg));
3925 if (p2p_parse_ies(ie, ie_len, &msg))
3927 if (msg.minor_reason_code == NULL)
3930 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3931 "P2P: Deauthentication notification BSSID " MACSTR
3932 " reason_code=%u minor_reason_code=%u",
3933 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3935 p2p_parse_free(&msg);
3939 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3940 const u8 *ie, size_t ie_len)
3942 struct p2p_message msg;
3944 if (bssid == NULL || ie == NULL)
3947 os_memset(&msg, 0, sizeof(msg));
3948 if (p2p_parse_ies(ie, ie_len, &msg))
3950 if (msg.minor_reason_code == NULL)
3953 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3954 "P2P: Disassociation notification BSSID " MACSTR
3955 " reason_code=%u minor_reason_code=%u",
3956 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3958 p2p_parse_free(&msg);
3962 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
3965 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3966 "Device operations enabled");
3967 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
3969 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3970 "Device operations disabled");
3971 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
3976 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
3978 if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
3981 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
3982 "reg_class %u channel %u", reg_class, channel);
3983 p2p->cfg->reg_class = reg_class;
3984 p2p->cfg->channel = channel;
3990 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
3992 wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
3993 if (postfix == NULL) {
3994 p2p->cfg->ssid_postfix_len = 0;
3997 if (len > sizeof(p2p->cfg->ssid_postfix))
3999 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
4000 p2p->cfg->ssid_postfix_len = len;
4005 int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
4008 if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
4012 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
4013 "reg_class %u channel %u", op_reg_class, op_channel);
4014 p2p->cfg->op_reg_class = op_reg_class;
4015 p2p->cfg->op_channel = op_channel;
4016 p2p->cfg->cfg_op_channel = cfg_op_channel;
4021 int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
4022 const struct p2p_channel *pref_chan)
4024 struct p2p_channel *n;
4027 n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
4030 os_memcpy(n, pref_chan,
4031 num_pref_chan * sizeof(struct p2p_channel));
4035 os_free(p2p->cfg->pref_chan);
4036 p2p->cfg->pref_chan = n;
4037 p2p->cfg->num_pref_chan = num_pref_chan;
4043 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
4046 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
4047 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
4049 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
4054 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
4057 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4060 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
4065 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
4067 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
4068 if (is_zero_ether_addr(p2p->peer_filter))
4069 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
4072 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
4073 "filter for " MACSTR, MAC2STR(p2p->peer_filter));
4077 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
4079 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
4080 enabled ? "enabled" : "disabled");
4081 if (p2p->cross_connect == enabled)
4083 p2p->cross_connect = enabled;
4084 /* TODO: may need to tear down any action group where we are GO(?) */
4088 int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
4090 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
4093 if (dev->oper_freq <= 0)
4095 return dev->oper_freq;
4099 void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
4101 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
4102 enabled ? "enabled" : "disabled");
4103 p2p->cfg->p2p_intra_bss = enabled;
4107 void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
4109 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
4110 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
4114 int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
4115 const u8 *src, const u8 *bssid, const u8 *buf,
4116 size_t len, unsigned int wait_time)
4118 if (p2p->p2p_scan_running) {
4119 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
4120 "frame TX until p2p_scan completes");
4121 if (p2p->after_scan_tx) {
4122 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
4123 "previous pending Action frame TX");
4124 os_free(p2p->after_scan_tx);
4126 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
4128 if (p2p->after_scan_tx == NULL)
4130 p2p->after_scan_tx->freq = freq;
4131 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
4132 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
4133 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
4134 p2p->after_scan_tx->len = len;
4135 p2p->after_scan_tx->wait_time = wait_time;
4136 os_memcpy(p2p->after_scan_tx + 1, buf, len);
4140 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4141 buf, len, wait_time);
4145 void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4148 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
4149 " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall);
4150 p2p->best_freq_24 = freq_24;
4151 p2p->best_freq_5 = freq_5;
4152 p2p->best_freq_overall = freq_overall;
4156 const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4158 if (p2p == NULL || p2p->go_neg_peer == NULL)
4160 return p2p->go_neg_peer->info.p2p_device_addr;
4164 const struct p2p_peer_info *
4165 p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4167 struct p2p_device *dev;
4170 dev = p2p_get_device(p2p, addr);
4175 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
4181 dev = dl_list_first(&dev->list,
4184 if (&dev->list == &p2p->devices)
4186 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
4189 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4192 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
4193 dev = dl_list_first(&dev->list,
4196 if (&dev->list == &p2p->devices)
4205 int p2p_search_in_progress(struct p2p_data *p2p)
4210 return p2p->state == P2P_SEARCH;
4214 int p2p_in_progress(struct p2p_data *p2p)
4218 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SEARCH_WHEN_READY ||
4219 p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY)
4221 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
4225 void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
4229 p2p->go_timeout = go_timeout;
4230 p2p->client_timeout = client_timeout;
4235 void p2p_increase_search_delay(struct p2p_data *p2p, unsigned int delay)
4237 if (p2p && p2p->search_delay < delay)
4238 p2p->search_delay = delay;
4242 #ifdef CONFIG_WIFI_DISPLAY
4244 static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
4247 struct p2p_group *group;
4249 for (g = 0; g < p2p->num_groups; g++) {
4250 group = p2p->groups[g];
4251 p2p_group_update_ies(group);
4256 int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
4258 wpabuf_free(p2p->wfd_ie_beacon);
4259 p2p->wfd_ie_beacon = ie;
4260 p2p_update_wfd_ie_groups(p2p);
4265 int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
4267 wpabuf_free(p2p->wfd_ie_probe_req);
4268 p2p->wfd_ie_probe_req = ie;
4273 int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
4275 wpabuf_free(p2p->wfd_ie_probe_resp);
4276 p2p->wfd_ie_probe_resp = ie;
4277 p2p_update_wfd_ie_groups(p2p);
4282 int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
4284 wpabuf_free(p2p->wfd_ie_assoc_req);
4285 p2p->wfd_ie_assoc_req = ie;
4290 int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
4292 wpabuf_free(p2p->wfd_ie_invitation);
4293 p2p->wfd_ie_invitation = ie;
4298 int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
4300 wpabuf_free(p2p->wfd_ie_prov_disc_req);
4301 p2p->wfd_ie_prov_disc_req = ie;
4306 int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
4308 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
4309 p2p->wfd_ie_prov_disc_resp = ie;
4314 int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
4316 wpabuf_free(p2p->wfd_ie_go_neg);
4317 p2p->wfd_ie_go_neg = ie;
4322 int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
4324 wpabuf_free(p2p->wfd_dev_info);
4326 p2p->wfd_dev_info = wpabuf_dup(elem);
4327 if (p2p->wfd_dev_info == NULL)
4330 p2p->wfd_dev_info = NULL;
4336 int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
4338 wpabuf_free(p2p->wfd_assoc_bssid);
4340 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
4341 if (p2p->wfd_assoc_bssid == NULL)
4344 p2p->wfd_assoc_bssid = NULL;
4350 int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
4351 const struct wpabuf *elem)
4353 wpabuf_free(p2p->wfd_coupled_sink_info);
4355 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
4356 if (p2p->wfd_coupled_sink_info == NULL)
4359 p2p->wfd_coupled_sink_info = NULL;
4364 #endif /* CONFIG_WIFI_DISPLAY */