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
45 #define P2P_PEER_EXPIRATION_AGE 300
47 #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
49 static void p2p_expire_peers(struct p2p_data *p2p)
51 struct p2p_device *dev, *n;
56 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
57 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
60 if (p2p->cfg->go_connected &&
61 p2p->cfg->go_connected(p2p->cfg->cb_ctx,
62 dev->info.p2p_device_addr)) {
64 * We are connected as a client to a group in which the
65 * peer is the GO, so do not expire the peer entry.
67 os_get_time(&dev->last_seen);
71 for (i = 0; i < p2p->num_groups; i++) {
72 if (p2p_group_is_client_connected(
73 p2p->groups[i], dev->info.p2p_device_addr))
76 if (i < p2p->num_groups) {
78 * The peer is connected as a client in a group where
79 * we are the GO, so do not expire the peer entry.
81 os_get_time(&dev->last_seen);
85 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer "
86 "entry " MACSTR, MAC2STR(dev->info.p2p_device_addr));
87 dl_list_del(&dev->list);
88 p2p_device_free(p2p, dev);
93 static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
95 struct p2p_data *p2p = eloop_ctx;
96 p2p_expire_peers(p2p);
97 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
98 p2p_expiration_timeout, p2p, NULL);
102 static const char * p2p_state_txt(int state)
111 case P2P_CONNECT_LISTEN:
112 return "CONNECT_LISTEN";
115 case P2P_LISTEN_ONLY:
116 return "LISTEN_ONLY";
117 case P2P_WAIT_PEER_CONNECT:
118 return "WAIT_PEER_CONNECT";
119 case P2P_WAIT_PEER_IDLE:
120 return "WAIT_PEER_IDLE";
121 case P2P_SD_DURING_FIND:
122 return "SD_DURING_FIND";
123 case P2P_PROVISIONING:
124 return "PROVISIONING";
125 case P2P_PD_DURING_FIND:
126 return "PD_DURING_FIND";
129 case P2P_INVITE_LISTEN:
130 return "INVITE_LISTEN";
131 case P2P_SEARCH_WHEN_READY:
132 return "SEARCH_WHEN_READY";
133 case P2P_CONTINUE_SEARCH_WHEN_READY:
134 return "CONTINUE_SEARCH_WHEN_READY";
141 u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
143 struct p2p_device *dev = NULL;
148 dev = p2p_get_device(p2p, addr);
150 return dev->wps_prov_info;
156 void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
158 struct p2p_device *dev = NULL;
163 dev = p2p_get_device(p2p, addr);
165 dev->wps_prov_info = 0;
169 void p2p_set_state(struct p2p_data *p2p, int new_state)
171 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s",
172 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
173 p2p->state = new_state;
177 void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
179 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
180 "P2P: Set timeout (state=%s): %u.%06u sec",
181 p2p_state_txt(p2p->state), sec, usec);
182 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
183 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
187 void p2p_clear_timeout(struct p2p_data *p2p)
189 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)",
190 p2p_state_txt(p2p->state));
191 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
195 void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
198 struct p2p_go_neg_results res;
199 p2p_clear_timeout(p2p);
200 p2p_set_state(p2p, P2P_IDLE);
201 if (p2p->go_neg_peer)
202 p2p->go_neg_peer->wps_method = WPS_NOT_READY;
203 p2p->go_neg_peer = NULL;
205 os_memset(&res, 0, sizeof(res));
208 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr,
210 os_memcpy(res.peer_interface_addr, peer->intended_addr,
213 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
217 static void p2p_listen_in_find(struct p2p_data *p2p)
223 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
224 "P2P: Starting short listen state (state=%s)",
225 p2p_state_txt(p2p->state));
227 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
230 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
231 "P2P: Unknown regulatory class/channel");
235 os_get_random((u8 *) &r, sizeof(r));
236 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
237 p2p->min_disc_int) * 100;
239 p2p->pending_listen_freq = freq;
240 p2p->pending_listen_sec = 0;
241 p2p->pending_listen_usec = 1024 * tu;
243 ies = p2p_build_probe_resp_ies(p2p);
247 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
249 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
250 "P2P: Failed to start listen mode");
251 p2p->pending_listen_freq = 0;
257 int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
262 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
263 "P2P: Going to listen(only) state");
265 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
268 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
269 "P2P: Unknown regulatory class/channel");
273 p2p->pending_listen_freq = freq;
274 p2p->pending_listen_sec = timeout / 1000;
275 p2p->pending_listen_usec = (timeout % 1000) * 1000;
277 if (p2p->p2p_scan_running) {
278 if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
279 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
280 "P2P: p2p_scan running - connect is already "
281 "pending - skip listen");
284 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
285 "P2P: p2p_scan running - delay start of listen state");
286 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
290 ies = p2p_build_probe_resp_ies(p2p);
294 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
295 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
296 "P2P: Failed to start listen mode");
297 p2p->pending_listen_freq = 0;
303 p2p_set_state(p2p, P2P_LISTEN_ONLY);
309 static void p2p_device_clear_reported(struct p2p_data *p2p)
311 struct p2p_device *dev;
312 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
313 dev->flags &= ~P2P_DEV_REPORTED;
318 * p2p_get_device - Fetch a peer entry
319 * @p2p: P2P module context from p2p_init()
320 * @addr: P2P Device Address of the peer
321 * Returns: Pointer to the device entry or %NULL if not found
323 struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
325 struct p2p_device *dev;
326 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
327 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
335 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
336 * @p2p: P2P module context from p2p_init()
337 * @addr: P2P Interface Address of the peer
338 * Returns: Pointer to the device entry or %NULL if not found
340 struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
343 struct p2p_device *dev;
344 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
345 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
353 * p2p_create_device - Create a peer entry
354 * @p2p: P2P module context from p2p_init()
355 * @addr: P2P Device Address of the peer
356 * Returns: Pointer to the device entry or %NULL on failure
358 * If there is already an entry for the peer, it will be returned instead of
359 * creating a new one.
361 static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
364 struct p2p_device *dev, *oldest = NULL;
367 dev = p2p_get_device(p2p, addr);
371 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
373 if (oldest == NULL ||
374 os_time_before(&dev->last_seen, &oldest->last_seen))
377 if (count + 1 > p2p->cfg->max_peers && oldest) {
378 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
379 "P2P: Remove oldest peer entry to make room for a new "
381 dl_list_del(&oldest->list);
382 p2p_device_free(p2p, oldest);
385 dev = os_zalloc(sizeof(*dev));
388 dl_list_add(&p2p->devices, &dev->list);
389 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
395 static void p2p_copy_client_info(struct p2p_device *dev,
396 struct p2p_client_info *cli)
398 os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
399 dev->info.device_name[cli->dev_name_len] = '\0';
400 dev->info.dev_capab = cli->dev_capab;
401 dev->info.config_methods = cli->config_methods;
402 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
403 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
404 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
405 dev->info.wps_sec_dev_type_list_len);
409 static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
410 const u8 *go_interface_addr, int freq,
411 const u8 *gi, size_t gi_len)
413 struct p2p_group_info info;
415 struct p2p_device *dev;
420 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
424 * Clear old data for this group; if the devices are still in the
425 * group, the information will be restored in the loop following this.
427 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
428 if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
430 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
431 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
435 for (c = 0; c < info.num_clients; c++) {
436 struct p2p_client_info *cli = &info.client[c];
437 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
439 continue; /* ignore our own entry */
440 dev = p2p_get_device(p2p, cli->p2p_device_addr);
442 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
443 P2P_DEV_PROBE_REQ_ONLY)) {
445 * Update information since we have not
446 * received this directly from the client.
448 p2p_copy_client_info(dev, cli);
451 * Need to update P2P Client Discoverability
452 * flag since it is valid only in P2P Group
455 dev->info.dev_capab &=
456 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
457 dev->info.dev_capab |=
459 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
461 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
462 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
465 dev = p2p_create_device(p2p, cli->p2p_device_addr);
468 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
469 p2p_copy_client_info(dev, cli);
470 dev->oper_freq = freq;
471 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
472 dev->info.p2p_device_addr,
474 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
477 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
479 os_get_time(&dev->last_seen);
480 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
481 os_memcpy(dev->member_in_go_iface, go_interface_addr,
489 static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
490 const struct p2p_message *msg)
492 os_memcpy(dev->info.device_name, msg->device_name,
493 sizeof(dev->info.device_name));
495 if (msg->manufacturer &&
496 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
497 os_memset(dev->info.manufacturer, 0,
498 sizeof(dev->info.manufacturer));
499 os_memcpy(dev->info.manufacturer, msg->manufacturer,
500 msg->manufacturer_len);
503 if (msg->model_name &&
504 msg->model_name_len < sizeof(dev->info.model_name)) {
505 os_memset(dev->info.model_name, 0,
506 sizeof(dev->info.model_name));
507 os_memcpy(dev->info.model_name, msg->model_name,
508 msg->model_name_len);
511 if (msg->model_number &&
512 msg->model_number_len < sizeof(dev->info.model_number)) {
513 os_memset(dev->info.model_number, 0,
514 sizeof(dev->info.model_number));
515 os_memcpy(dev->info.model_number, msg->model_number,
516 msg->model_number_len);
519 if (msg->serial_number &&
520 msg->serial_number_len < sizeof(dev->info.serial_number)) {
521 os_memset(dev->info.serial_number, 0,
522 sizeof(dev->info.serial_number));
523 os_memcpy(dev->info.serial_number, msg->serial_number,
524 msg->serial_number_len);
527 if (msg->pri_dev_type)
528 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
529 sizeof(dev->info.pri_dev_type));
530 else if (msg->wps_pri_dev_type)
531 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
532 sizeof(dev->info.pri_dev_type));
534 if (msg->wps_sec_dev_type_list) {
535 os_memcpy(dev->info.wps_sec_dev_type_list,
536 msg->wps_sec_dev_type_list,
537 msg->wps_sec_dev_type_list_len);
538 dev->info.wps_sec_dev_type_list_len =
539 msg->wps_sec_dev_type_list_len;
542 if (msg->capability) {
544 * P2P Client Discoverability bit is reserved in all frames
545 * that use this function, so do not change its value here.
547 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
548 dev->info.dev_capab |= msg->capability[0] &
549 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
550 dev->info.group_capab = msg->capability[1];
553 if (msg->ext_listen_timing) {
554 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
555 dev->ext_listen_interval =
556 WPA_GET_LE16(msg->ext_listen_timing + 2);
560 dev->info.config_methods = msg->config_methods ?
561 msg->config_methods : msg->wps_config_methods;
567 * p2p_add_device - Add peer entries based on scan results or P2P frames
568 * @p2p: P2P module context from p2p_init()
569 * @addr: Source address of Beacon or Probe Response frame (may be either
570 * P2P Device Address or P2P Interface Address)
571 * @level: Signal level (signal strength of the received frame from the peer)
572 * @freq: Frequency on which the Beacon or Probe Response frame was received
573 * @ies: IEs from the Beacon or Probe Response frame
574 * @ies_len: Length of ies buffer in octets
575 * @scan_res: Whether this was based on scan results
576 * Returns: 0 on success, -1 on failure
578 * If the scan result is for a GO, the clients in the group will also be added
579 * to the peer table. This function can also be used with some other frames
580 * like Provision Discovery Request that contains P2P Capability and P2P Device
583 int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
584 const u8 *ies, size_t ies_len, int scan_res)
586 struct p2p_device *dev;
587 struct p2p_message msg;
588 const u8 *p2p_dev_addr;
591 os_memset(&msg, 0, sizeof(msg));
592 if (p2p_parse_ies(ies, ies_len, &msg)) {
593 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
594 "P2P: Failed to parse P2P IE for a device entry");
595 p2p_parse_free(&msg);
599 if (msg.p2p_device_addr)
600 p2p_dev_addr = msg.p2p_device_addr;
601 else if (msg.device_id)
602 p2p_dev_addr = msg.device_id;
604 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
605 "P2P: Ignore scan data without P2P Device Info or "
607 p2p_parse_free(&msg);
611 if (!is_zero_ether_addr(p2p->peer_filter) &&
612 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
613 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
614 "filter for " MACSTR " due to peer filter",
615 MAC2STR(p2p_dev_addr));
619 dev = p2p_create_device(p2p, p2p_dev_addr);
621 p2p_parse_free(&msg);
624 os_get_time(&dev->last_seen);
625 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
627 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
628 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
630 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
631 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
633 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
634 dev->oper_ssid_len = msg.ssid[1];
637 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
638 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
640 if (*msg.ds_params == 14)
643 ds_freq = 2407 + *msg.ds_params * 5;
644 if (freq != ds_freq) {
645 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
646 "P2P: Update Listen frequency based on DS "
647 "Parameter Set IE: %d -> %d MHz",
653 if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
654 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
655 "P2P: Update Listen frequency based on scan "
656 "results (" MACSTR " %d -> %d MHz (DS param %d)",
657 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
658 freq, msg.ds_params ? *msg.ds_params : -1);
661 dev->listen_freq = freq;
663 dev->oper_freq = freq;
665 dev->info.level = level;
667 p2p_copy_wps_info(dev, 0, &msg);
669 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
670 wpabuf_free(dev->info.wps_vendor_ext[i]);
671 dev->info.wps_vendor_ext[i] = NULL;
674 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
675 if (msg.wps_vendor_ext[i] == NULL)
677 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
678 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
679 if (dev->info.wps_vendor_ext[i] == NULL)
683 if (msg.wfd_subelems) {
684 wpabuf_free(dev->info.wfd_subelems);
685 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
689 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
690 msg.group_info, msg.group_info_len);
693 p2p_parse_free(&msg);
695 if (p2p_pending_sd_req(p2p, dev))
696 dev->flags |= P2P_DEV_SD_SCHEDULE;
698 if (dev->flags & P2P_DEV_REPORTED)
701 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
702 "P2P: Peer found with Listen frequency %d MHz", freq);
703 if (dev->flags & P2P_DEV_USER_REJECTED) {
704 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
705 "P2P: Do not report rejected device");
709 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
710 !(dev->flags & P2P_DEV_REPORTED_ONCE));
711 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
717 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
721 if (p2p->go_neg_peer == dev) {
723 * If GO Negotiation is in progress, report that it has failed.
725 p2p_go_neg_failed(p2p, dev, -1);
726 p2p->go_neg_peer = NULL;
728 if (p2p->invite_peer == dev)
729 p2p->invite_peer = NULL;
730 if (p2p->sd_peer == dev)
732 if (p2p->pending_client_disc_go == dev)
733 p2p->pending_client_disc_go = NULL;
735 /* dev_lost() device, but only if it was previously dev_found() */
736 if (dev->flags & P2P_DEV_REPORTED_ONCE)
737 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
738 dev->info.p2p_device_addr);
740 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
741 wpabuf_free(dev->info.wps_vendor_ext[i]);
742 dev->info.wps_vendor_ext[i] = NULL;
745 wpabuf_free(dev->info.wfd_subelems);
751 static int p2p_get_next_prog_freq(struct p2p_data *p2p)
753 struct p2p_channels *c;
754 struct p2p_reg_class *cla;
761 c = &p2p->cfg->channels;
762 for (cl = 0; cl < c->reg_classes; cl++) {
763 cla = &c->reg_class[cl];
764 if (cla->reg_class != p2p->last_prog_scan_class)
766 for (ch = 0; ch < cla->channels; ch++) {
767 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
777 /* Start from beginning */
778 reg_class = c->reg_class[0].reg_class;
779 channel = c->reg_class[0].channel[0];
781 /* Pick the next channel */
783 if (ch == cla->channels) {
785 if (cl == c->reg_classes)
789 reg_class = c->reg_class[cl].reg_class;
790 channel = c->reg_class[cl].channel[ch];
793 freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
794 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
795 "channel: reg_class %u channel %u -> %d MHz",
796 reg_class, channel, freq);
797 p2p->last_prog_scan_class = reg_class;
798 p2p->last_prog_scan_chan = channel;
800 if (freq == 2412 || freq == 2437 || freq == 2462)
801 return 0; /* No need to add social channels */
806 static void p2p_search(struct p2p_data *p2p)
809 enum p2p_scan_type type;
810 u16 pw_id = DEV_PW_DEFAULT;
813 if (p2p->drv_in_listen) {
814 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
815 "in Listen state - wait for it to end before "
819 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
821 if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
822 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
823 type = P2P_SCAN_SOCIAL_PLUS_ONE;
824 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
825 "(+ freq %u)", freq);
827 type = P2P_SCAN_SOCIAL;
828 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
831 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
832 p2p->num_req_dev_types, p2p->req_dev_types,
833 p2p->find_dev_id, pw_id);
835 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
836 "P2P: Scan request failed");
837 p2p_continue_find(p2p);
838 } else if (res == 1) {
839 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
840 "p2p_scan at this point - will try again after "
841 "previous scan completes");
842 p2p_set_state(p2p, P2P_CONTINUE_SEARCH_WHEN_READY);
844 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
845 p2p->p2p_scan_running = 1;
846 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
847 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
853 static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
855 struct p2p_data *p2p = eloop_ctx;
856 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
861 static int p2p_run_after_scan(struct p2p_data *p2p)
863 struct p2p_device *dev;
864 enum p2p_after_scan op;
866 if (p2p->after_scan_tx) {
867 /* TODO: schedule p2p_run_after_scan to be called from TX
868 * status callback(?) */
869 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
870 "Action frame at p2p_scan completion");
871 p2p->cfg->send_action(p2p->cfg->cb_ctx,
872 p2p->after_scan_tx->freq,
873 p2p->after_scan_tx->dst,
874 p2p->after_scan_tx->src,
875 p2p->after_scan_tx->bssid,
876 (u8 *) (p2p->after_scan_tx + 1),
877 p2p->after_scan_tx->len,
878 p2p->after_scan_tx->wait_time);
879 os_free(p2p->after_scan_tx);
880 p2p->after_scan_tx = NULL;
884 op = p2p->start_after_scan;
885 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
887 case P2P_AFTER_SCAN_NOTHING:
889 case P2P_AFTER_SCAN_LISTEN:
890 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
891 "requested Listen state");
892 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
893 p2p->pending_listen_usec / 1000);
895 case P2P_AFTER_SCAN_CONNECT:
896 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
897 "requested connect with " MACSTR,
898 MAC2STR(p2p->after_scan_peer));
899 dev = p2p_get_device(p2p, p2p->after_scan_peer);
901 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
905 p2p_connect_send(p2p, dev);
913 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
915 struct p2p_data *p2p = eloop_ctx;
917 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
918 "(running=%d)", p2p->p2p_scan_running);
919 running = p2p->p2p_scan_running;
920 /* Make sure we recover from missed scan results callback */
921 p2p->p2p_scan_running = 0;
924 p2p_run_after_scan(p2p);
928 static void p2p_free_req_dev_types(struct p2p_data *p2p)
930 p2p->num_req_dev_types = 0;
931 os_free(p2p->req_dev_types);
932 p2p->req_dev_types = NULL;
936 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
937 enum p2p_discovery_type type,
938 unsigned int num_req_dev_types, const u8 *req_dev_types,
939 const u8 *dev_id, unsigned int search_delay)
943 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
945 if (p2p->p2p_scan_running) {
946 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
950 p2p_free_req_dev_types(p2p);
951 if (req_dev_types && num_req_dev_types) {
952 p2p->req_dev_types = os_malloc(num_req_dev_types *
954 if (p2p->req_dev_types == NULL)
956 os_memcpy(p2p->req_dev_types, req_dev_types,
957 num_req_dev_types * WPS_DEV_TYPE_LEN);
958 p2p->num_req_dev_types = num_req_dev_types;
962 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
963 p2p->find_dev_id = p2p->find_dev_id_buf;
965 p2p->find_dev_id = NULL;
967 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
968 p2p_clear_timeout(p2p);
969 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
970 p2p->find_type = type;
971 p2p_device_clear_reported(p2p);
972 p2p_set_state(p2p, P2P_SEARCH);
973 p2p->search_delay = search_delay;
974 p2p->in_search_delay = 0;
975 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
976 p2p->last_p2p_find_timeout = timeout;
978 eloop_register_timeout(timeout, 0, p2p_find_timeout,
981 case P2P_FIND_START_WITH_FULL:
982 case P2P_FIND_PROGRESSIVE:
983 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
984 p2p->num_req_dev_types,
985 p2p->req_dev_types, dev_id,
988 case P2P_FIND_ONLY_SOCIAL:
989 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
990 p2p->num_req_dev_types,
991 p2p->req_dev_types, dev_id,
999 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
1000 p2p->p2p_scan_running = 1;
1001 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
1002 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
1004 } else if (res == 1) {
1005 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
1006 "p2p_scan at this point - will try again after "
1007 "previous scan completes");
1009 p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
1010 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1012 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
1014 p2p_set_state(p2p, P2P_IDLE);
1015 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1022 int p2p_other_scan_completed(struct p2p_data *p2p)
1024 if (p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY) {
1025 p2p_set_state(p2p, P2P_SEARCH);
1029 if (p2p->state != P2P_SEARCH_WHEN_READY)
1031 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find "
1032 "now that previous scan was completed");
1033 if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
1034 p2p->num_req_dev_types, p2p->req_dev_types,
1035 p2p->find_dev_id, p2p->search_delay) < 0)
1041 void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1043 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
1044 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1045 p2p_clear_timeout(p2p);
1046 if (p2p->state == P2P_SEARCH)
1047 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1048 p2p_set_state(p2p, P2P_IDLE);
1049 p2p_free_req_dev_types(p2p);
1050 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1051 p2p->go_neg_peer = NULL;
1052 p2p->sd_peer = NULL;
1053 p2p->invite_peer = NULL;
1054 p2p_stop_listen_for_freq(p2p, freq);
1058 void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1060 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
1061 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
1062 "since we are on correct channel for response");
1065 if (p2p->in_listen) {
1067 p2p_clear_timeout(p2p);
1069 if (p2p->drv_in_listen) {
1071 * The driver may not deliver callback to p2p_listen_end()
1072 * when the operation gets canceled, so clear the internal
1073 * variable that is tracking driver state.
1075 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear "
1076 "drv_in_listen (%d)", p2p->drv_in_listen);
1077 p2p->drv_in_listen = 0;
1079 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1083 void p2p_stop_find(struct p2p_data *p2p)
1085 p2p_stop_find_for_freq(p2p, 0);
1089 static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq,
1090 unsigned int pref_freq)
1092 if (force_freq || pref_freq) {
1093 u8 op_reg_class, op_channel;
1094 unsigned int freq = force_freq ? force_freq : pref_freq;
1095 if (p2p_freq_to_channel(p2p->cfg->country, freq,
1096 &op_reg_class, &op_channel) < 0) {
1097 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1098 "P2P: Unsupported frequency %u MHz",
1102 if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
1104 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1105 "P2P: Frequency %u MHz (oper_class %u "
1106 "channel %u) not allowed for P2P",
1107 freq, op_reg_class, op_channel);
1110 p2p->op_reg_class = op_reg_class;
1111 p2p->op_channel = op_channel;
1113 p2p->channels.reg_classes = 1;
1114 p2p->channels.reg_class[0].channels = 1;
1115 p2p->channels.reg_class[0].reg_class =
1117 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
1119 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1120 sizeof(struct p2p_channels));
1123 u8 op_reg_class, op_channel;
1125 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1126 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
1127 p2p_freq_to_channel(p2p->cfg->country,
1128 p2p->best_freq_overall,
1129 &op_reg_class, &op_channel) == 0) {
1130 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1131 "P2P: Select best overall channel as "
1132 "operating channel preference");
1133 p2p->op_reg_class = op_reg_class;
1134 p2p->op_channel = op_channel;
1135 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1136 p2p_supported_freq(p2p, p2p->best_freq_5) &&
1137 p2p_freq_to_channel(p2p->cfg->country,
1139 &op_reg_class, &op_channel) ==
1141 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1142 "P2P: Select best 5 GHz channel as "
1143 "operating channel preference");
1144 p2p->op_reg_class = op_reg_class;
1145 p2p->op_channel = op_channel;
1146 } else if (!p2p->cfg->cfg_op_channel &&
1147 p2p->best_freq_24 > 0 &&
1148 p2p_supported_freq(p2p, p2p->best_freq_24) &&
1149 p2p_freq_to_channel(p2p->cfg->country,
1151 &op_reg_class, &op_channel) ==
1153 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1154 "P2P: Select best 2.4 GHz channel as "
1155 "operating channel preference");
1156 p2p->op_reg_class = op_reg_class;
1157 p2p->op_channel = op_channel;
1159 p2p->op_reg_class = p2p->cfg->op_reg_class;
1160 p2p->op_channel = p2p->cfg->op_channel;
1163 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1164 sizeof(struct p2p_channels));
1166 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1167 "P2P: Own preference for operation channel: "
1168 "Operating Class %u Channel %u%s",
1169 p2p->op_reg_class, p2p->op_channel,
1170 force_freq ? " (forced)" : "");
1176 static void p2p_set_dev_persistent(struct p2p_device *dev,
1177 int persistent_group)
1179 switch (persistent_group) {
1181 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1182 P2P_DEV_PREFER_PERSISTENT_RECONN);
1185 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1186 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1189 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1190 P2P_DEV_PREFER_PERSISTENT_RECONN;
1196 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1197 enum p2p_wps_method wps_method,
1198 int go_intent, const u8 *own_interface_addr,
1199 unsigned int force_freq, int persistent_group,
1200 const u8 *force_ssid, size_t force_ssid_len,
1201 int pd_before_go_neg, unsigned int pref_freq)
1203 struct p2p_device *dev;
1205 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1206 "P2P: Request to start group negotiation - peer=" MACSTR
1207 " GO Intent=%d Intended Interface Address=" MACSTR
1208 " wps_method=%d persistent_group=%d pd_before_go_neg=%d",
1209 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1210 wps_method, persistent_group, pd_before_go_neg);
1212 if (p2p_prepare_channel(p2p, force_freq, pref_freq) < 0)
1215 dev = p2p_get_device(p2p, peer_addr);
1216 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1217 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1218 "P2P: Cannot connect to unknown P2P Device " MACSTR,
1219 MAC2STR(peer_addr));
1223 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1224 if (!(dev->info.dev_capab &
1225 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1226 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1227 "P2P: Cannot connect to P2P Device " MACSTR
1228 " that is in a group and is not discoverable",
1229 MAC2STR(peer_addr));
1232 if (dev->oper_freq <= 0) {
1233 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1234 "P2P: Cannot connect to P2P Device " MACSTR
1235 " with incomplete information",
1236 MAC2STR(peer_addr));
1241 * First, try to connect directly. If the peer does not
1242 * acknowledge frames, assume it is sleeping and use device
1243 * discoverability via the GO at that point.
1249 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1250 force_ssid, force_ssid_len);
1251 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1252 p2p->ssid_len = force_ssid_len;
1256 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1257 dev->flags &= ~P2P_DEV_USER_REJECTED;
1258 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1259 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1260 if (pd_before_go_neg)
1261 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
1263 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
1264 dev->connect_reqs = 0;
1265 dev->go_neg_req_sent = 0;
1266 dev->go_state = UNKNOWN_GO;
1267 p2p_set_dev_persistent(dev, persistent_group);
1268 p2p->go_intent = go_intent;
1269 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1271 if (p2p->state != P2P_IDLE)
1274 if (p2p->after_scan_tx) {
1276 * We need to drop the pending frame to avoid issues with the
1277 * new GO Negotiation, e.g., when the pending frame was from a
1278 * previous attempt at starting a GO Negotiation.
1280 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
1281 "previous pending Action frame TX that was waiting "
1282 "for p2p_scan completion");
1283 os_free(p2p->after_scan_tx);
1284 p2p->after_scan_tx = NULL;
1287 dev->wps_method = wps_method;
1288 dev->status = P2P_SC_SUCCESS;
1291 dev->flags |= P2P_DEV_FORCE_FREQ;
1293 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1295 if (p2p->p2p_scan_running) {
1296 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1297 "P2P: p2p_scan running - delay connect send");
1298 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1299 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1302 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1304 return p2p_connect_send(p2p, dev);
1308 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1309 enum p2p_wps_method wps_method,
1310 int go_intent, const u8 *own_interface_addr,
1311 unsigned int force_freq, int persistent_group,
1312 const u8 *force_ssid, size_t force_ssid_len,
1313 unsigned int pref_freq)
1315 struct p2p_device *dev;
1317 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1318 "P2P: Request to authorize group negotiation - peer=" MACSTR
1319 " GO Intent=%d Intended Interface Address=" MACSTR
1320 " wps_method=%d persistent_group=%d",
1321 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1322 wps_method, persistent_group);
1324 if (p2p_prepare_channel(p2p, force_freq, pref_freq) < 0)
1327 dev = p2p_get_device(p2p, peer_addr);
1329 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1330 "P2P: Cannot authorize unknown P2P Device " MACSTR,
1331 MAC2STR(peer_addr));
1337 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1338 force_ssid, force_ssid_len);
1339 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1340 p2p->ssid_len = force_ssid_len;
1344 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1345 dev->flags &= ~P2P_DEV_USER_REJECTED;
1346 dev->go_neg_req_sent = 0;
1347 dev->go_state = UNKNOWN_GO;
1348 p2p_set_dev_persistent(dev, persistent_group);
1349 p2p->go_intent = go_intent;
1350 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1352 dev->wps_method = wps_method;
1353 dev->status = P2P_SC_SUCCESS;
1356 dev->flags |= P2P_DEV_FORCE_FREQ;
1358 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1364 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1365 struct p2p_device *dev, struct p2p_message *msg)
1367 os_get_time(&dev->last_seen);
1369 p2p_copy_wps_info(dev, 0, msg);
1371 if (msg->listen_channel) {
1373 freq = p2p_channel_to_freq((char *) msg->listen_channel,
1374 msg->listen_channel[3],
1375 msg->listen_channel[4]);
1377 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1378 "P2P: Unknown peer Listen channel: "
1379 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1380 msg->listen_channel[0],
1381 msg->listen_channel[1],
1382 msg->listen_channel[2],
1383 msg->listen_channel[3],
1384 msg->listen_channel[4]);
1386 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1387 "peer " MACSTR " Listen channel: %u -> %u MHz",
1388 MAC2STR(dev->info.p2p_device_addr),
1389 dev->listen_freq, freq);
1390 dev->listen_freq = freq;
1394 if (msg->wfd_subelems) {
1395 wpabuf_free(dev->info.wfd_subelems);
1396 dev->info.wfd_subelems = wpabuf_dup(msg->wfd_subelems);
1399 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1400 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1401 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1402 "P2P: Completed device entry based on data from "
1403 "GO Negotiation Request");
1405 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1406 "P2P: Created device entry based on GO Neg Req: "
1407 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1409 MAC2STR(dev->info.p2p_device_addr),
1410 dev->info.dev_capab, dev->info.group_capab,
1411 dev->info.device_name, dev->listen_freq);
1414 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1416 if (dev->flags & P2P_DEV_USER_REJECTED) {
1417 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1418 "P2P: Do not report rejected device");
1422 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1423 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1424 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1428 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1430 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1431 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1432 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1433 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1434 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1438 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1440 p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len);
1441 p2p_random(params->passphrase, 8);
1446 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1448 struct p2p_go_neg_results res;
1449 int go = peer->go_state == LOCAL_GO;
1450 struct p2p_channels intersection;
1454 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1455 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1456 "GO)", MAC2STR(peer->info.p2p_device_addr),
1457 go ? "local end" : "peer");
1459 os_memset(&res, 0, sizeof(res));
1461 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1462 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1463 res.wps_method = peer->wps_method;
1464 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1465 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1466 res.persistent_group = 2;
1468 res.persistent_group = 1;
1472 /* Setup AP mode for WPS provisioning */
1473 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1476 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1477 res.ssid_len = p2p->ssid_len;
1478 p2p_random(res.passphrase, 8);
1480 res.freq = peer->oper_freq;
1481 if (p2p->ssid_len) {
1482 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1483 res.ssid_len = p2p->ssid_len;
1487 p2p_channels_intersect(&p2p->channels, &peer->channels,
1490 for (i = 0; i < intersection.reg_classes; i++) {
1491 struct p2p_reg_class *c = &intersection.reg_class[i];
1492 if (freqs + 1 == P2P_MAX_CHANNELS)
1494 for (j = 0; j < c->channels; j++) {
1496 if (freqs + 1 == P2P_MAX_CHANNELS)
1498 freq = p2p_channel_to_freq(peer->country, c->reg_class,
1502 res.freq_list[freqs++] = freq;
1506 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1508 p2p_clear_timeout(p2p);
1510 peer->go_neg_req_sent = 0;
1511 peer->wps_method = WPS_NOT_READY;
1513 p2p_set_state(p2p, P2P_PROVISIONING);
1514 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1518 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1519 const u8 *data, size_t len, int rx_freq)
1521 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1522 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1523 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1529 case P2P_GO_NEG_REQ:
1530 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1532 case P2P_GO_NEG_RESP:
1533 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1535 case P2P_GO_NEG_CONF:
1536 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1538 case P2P_INVITATION_REQ:
1539 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1542 case P2P_INVITATION_RESP:
1543 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1545 case P2P_PROV_DISC_REQ:
1546 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1548 case P2P_PROV_DISC_RESP:
1549 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1551 case P2P_DEV_DISC_REQ:
1552 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1554 case P2P_DEV_DISC_RESP:
1555 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1558 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1559 "P2P: Unsupported P2P Public Action frame type %d",
1566 static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1567 const u8 *sa, const u8 *bssid, const u8 *data,
1568 size_t len, int freq)
1574 case WLAN_PA_VENDOR_SPECIFIC:
1579 if (WPA_GET_BE24(data) != OUI_WFA)
1587 if (*data != P2P_OUI_TYPE)
1590 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1592 case WLAN_PA_GAS_INITIAL_REQ:
1593 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1595 case WLAN_PA_GAS_INITIAL_RESP:
1596 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1598 case WLAN_PA_GAS_COMEBACK_REQ:
1599 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1601 case WLAN_PA_GAS_COMEBACK_RESP:
1602 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1608 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1609 const u8 *bssid, u8 category,
1610 const u8 *data, size_t len, int freq)
1612 if (category == WLAN_ACTION_PUBLIC) {
1613 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1617 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1623 if (WPA_GET_BE24(data) != OUI_WFA)
1628 if (*data != P2P_OUI_TYPE)
1633 /* P2P action frame */
1634 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1635 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1636 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1642 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1643 "P2P: Received P2P Action - Notice of Absence");
1646 case P2P_PRESENCE_REQ:
1647 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1649 case P2P_PRESENCE_RESP:
1650 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1652 case P2P_GO_DISC_REQ:
1653 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1656 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1657 "P2P: Received P2P Action - unknown type %u", data[0]);
1663 static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1665 struct p2p_data *p2p = eloop_ctx;
1666 if (p2p->go_neg_peer == NULL)
1668 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1669 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1670 p2p_connect_send(p2p, p2p->go_neg_peer);
1674 static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1676 struct p2p_data *p2p = eloop_ctx;
1677 if (p2p->invite_peer == NULL)
1679 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1680 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1684 static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1685 const u8 *ie, size_t ie_len)
1687 struct p2p_message msg;
1688 struct p2p_device *dev;
1690 os_memset(&msg, 0, sizeof(msg));
1691 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1693 p2p_parse_free(&msg);
1694 return; /* not a P2P probe */
1697 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1698 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1700 /* The Probe Request is not part of P2P Device Discovery. It is
1701 * not known whether the source address of the frame is the P2P
1702 * Device Address or P2P Interface Address. Do not add a new
1703 * peer entry based on this frames.
1705 p2p_parse_free(&msg);
1709 dev = p2p_get_device(p2p, addr);
1711 if (dev->country[0] == 0 && msg.listen_channel)
1712 os_memcpy(dev->country, msg.listen_channel, 3);
1713 os_get_time(&dev->last_seen);
1714 p2p_parse_free(&msg);
1715 return; /* already known */
1718 dev = p2p_create_device(p2p, addr);
1720 p2p_parse_free(&msg);
1724 os_get_time(&dev->last_seen);
1725 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1727 if (msg.listen_channel) {
1728 os_memcpy(dev->country, msg.listen_channel, 3);
1729 dev->listen_freq = p2p_channel_to_freq(dev->country,
1730 msg.listen_channel[3],
1731 msg.listen_channel[4]);
1734 p2p_copy_wps_info(dev, 1, &msg);
1736 if (msg.wfd_subelems) {
1737 wpabuf_free(dev->info.wfd_subelems);
1738 dev->info.wfd_subelems = wpabuf_dup(msg.wfd_subelems);
1741 p2p_parse_free(&msg);
1743 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1744 "P2P: Created device entry based on Probe Req: " MACSTR
1745 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1746 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1747 dev->info.group_capab, dev->info.device_name,
1752 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1754 struct p2p_message *msg)
1756 struct p2p_device *dev;
1758 dev = p2p_get_device(p2p, addr);
1760 os_get_time(&dev->last_seen);
1761 return dev; /* already known */
1764 dev = p2p_create_device(p2p, addr);
1768 p2p_add_dev_info(p2p, addr, dev, msg);
1774 static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1776 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1778 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1779 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1780 WPA_GET_BE16(&req_dev_type[6]) == 0)
1781 return 1; /* Category match with wildcard OUI/sub-category */
1786 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1787 size_t num_req_dev_type)
1790 for (i = 0; i < num_req_dev_type; i++) {
1791 if (dev_type_match(dev_type, req_dev_type[i]))
1799 * p2p_match_dev_type - Match local device type with requested type
1800 * @p2p: P2P module context from p2p_init()
1801 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1802 * Returns: 1 on match, 0 on mismatch
1804 * This function can be used to match the Requested Device Type attribute in
1805 * WPS IE with the local device types for deciding whether to reply to a Probe
1808 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1810 struct wps_parse_attr attr;
1813 if (wps_parse_msg(wps, &attr))
1814 return 1; /* assume no Requested Device Type attributes */
1816 if (attr.num_req_dev_type == 0)
1817 return 1; /* no Requested Device Type attributes -> match */
1819 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1820 attr.num_req_dev_type))
1821 return 1; /* Own Primary Device Type matches */
1823 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1824 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1826 attr.num_req_dev_type))
1827 return 1; /* Own Secondary Device Type matches */
1829 /* No matching device type found */
1834 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1841 #ifdef CONFIG_WIFI_DISPLAY
1842 if (p2p->wfd_ie_probe_resp)
1843 extra = wpabuf_len(p2p->wfd_ie_probe_resp);
1844 #endif /* CONFIG_WIFI_DISPLAY */
1846 buf = wpabuf_alloc(1000 + extra);
1850 if (p2p->go_neg_peer) {
1851 /* Advertise immediate availability of WPS credential */
1852 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
1855 p2p_build_wps_ie(p2p, buf, pw_id, 1);
1857 #ifdef CONFIG_WIFI_DISPLAY
1858 if (p2p->wfd_ie_probe_resp)
1859 wpabuf_put_buf(buf, p2p->wfd_ie_probe_resp);
1860 #endif /* CONFIG_WIFI_DISPLAY */
1863 len = p2p_buf_add_ie_hdr(buf);
1864 p2p_buf_add_capability(buf, p2p->dev_capab &
1865 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
1866 if (p2p->ext_listen_interval)
1867 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1868 p2p->ext_listen_interval);
1869 p2p_buf_add_device_info(buf, p2p, NULL);
1870 p2p_buf_update_ie_hdr(buf, len);
1876 static int is_11b(u8 rate)
1878 return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
1882 static int supp_rates_11b_only(struct ieee802_11_elems *elems)
1884 int num_11b = 0, num_others = 0;
1887 if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
1890 for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
1891 if (is_11b(elems->supp_rates[i]))
1897 for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
1899 if (is_11b(elems->ext_supp_rates[i]))
1905 return num_11b > 0 && num_others == 0;
1909 static enum p2p_probe_req_status
1910 p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
1911 const u8 *bssid, const u8 *ie, size_t ie_len)
1913 struct ieee802_11_elems elems;
1915 struct ieee80211_mgmt *resp;
1916 struct p2p_message msg;
1919 if (!p2p->in_listen || !p2p->drv_in_listen) {
1920 /* not in Listen state - ignore Probe Request */
1921 return P2P_PREQ_NOT_LISTEN;
1924 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
1926 /* Ignore invalid Probe Request frames */
1927 return P2P_PREQ_MALFORMED;
1930 if (elems.p2p == NULL) {
1931 /* not a P2P probe - ignore it */
1932 return P2P_PREQ_NOT_P2P;
1935 if (dst && !is_broadcast_ether_addr(dst) &&
1936 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
1937 /* Not sent to the broadcast address or our P2P Device Address
1939 return P2P_PREQ_NOT_PROCESSED;
1942 if (bssid && !is_broadcast_ether_addr(bssid)) {
1943 /* Not sent to the Wildcard BSSID */
1944 return P2P_PREQ_NOT_PROCESSED;
1947 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
1948 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
1950 /* not using P2P Wildcard SSID - ignore */
1951 return P2P_PREQ_NOT_PROCESSED;
1954 if (supp_rates_11b_only(&elems)) {
1955 /* Indicates support for 11b rates only */
1956 return P2P_PREQ_NOT_P2P;
1959 os_memset(&msg, 0, sizeof(msg));
1960 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
1961 /* Could not parse P2P attributes */
1962 return P2P_PREQ_NOT_P2P;
1965 if (msg.device_id &&
1966 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
1967 /* Device ID did not match */
1968 p2p_parse_free(&msg);
1969 return P2P_PREQ_NOT_PROCESSED;
1972 /* Check Requested Device Type match */
1973 if (msg.wps_attributes &&
1974 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
1975 /* No match with Requested Device Type */
1976 p2p_parse_free(&msg);
1977 return P2P_PREQ_NOT_PROCESSED;
1979 p2p_parse_free(&msg);
1981 if (!p2p->cfg->send_probe_resp) {
1982 /* Response generated elsewhere */
1983 return P2P_PREQ_NOT_PROCESSED;
1986 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1987 "P2P: Reply to P2P Probe Request in Listen state");
1990 * We do not really have a specific BSS that this frame is advertising,
1991 * so build a frame that has some information in valid format. This is
1992 * really only used for discovery purposes, not to learn exact BSS
1995 ies = p2p_build_probe_resp_ies(p2p);
1997 return P2P_PREQ_NOT_PROCESSED;
1999 buf = wpabuf_alloc(200 + wpabuf_len(ies));
2002 return P2P_PREQ_NOT_PROCESSED;
2006 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
2008 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
2009 (WLAN_FC_STYPE_PROBE_RESP << 4));
2010 os_memcpy(resp->da, addr, ETH_ALEN);
2011 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
2012 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
2013 resp->u.probe_resp.beacon_int = host_to_le16(100);
2014 /* hardware or low-level driver will setup seq_ctrl and timestamp */
2015 resp->u.probe_resp.capab_info =
2016 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
2017 WLAN_CAPABILITY_PRIVACY |
2018 WLAN_CAPABILITY_SHORT_SLOT_TIME);
2020 wpabuf_put_u8(buf, WLAN_EID_SSID);
2021 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
2022 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
2024 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
2025 wpabuf_put_u8(buf, 8);
2026 wpabuf_put_u8(buf, (60 / 5) | 0x80);
2027 wpabuf_put_u8(buf, 90 / 5);
2028 wpabuf_put_u8(buf, (120 / 5) | 0x80);
2029 wpabuf_put_u8(buf, 180 / 5);
2030 wpabuf_put_u8(buf, (240 / 5) | 0x80);
2031 wpabuf_put_u8(buf, 360 / 5);
2032 wpabuf_put_u8(buf, 480 / 5);
2033 wpabuf_put_u8(buf, 540 / 5);
2035 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
2036 wpabuf_put_u8(buf, 1);
2037 wpabuf_put_u8(buf, p2p->cfg->channel);
2039 wpabuf_put_buf(buf, ies);
2042 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
2046 return P2P_PREQ_NOT_PROCESSED;
2050 enum p2p_probe_req_status
2051 p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2052 const u8 *bssid, const u8 *ie, size_t ie_len)
2054 enum p2p_probe_req_status res;
2056 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2058 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
2060 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2062 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
2064 /* Received a Probe Request from GO Negotiation peer */
2065 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2066 "P2P: Found GO Negotiation peer - try to start GO "
2067 "negotiation from timeout");
2068 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
2069 return P2P_PREQ_PROCESSED;
2072 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2074 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2076 /* Received a Probe Request from Invite peer */
2077 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2078 "P2P: Found Invite peer - try to start Invite from "
2080 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
2081 return P2P_PREQ_PROCESSED;
2088 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2089 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2098 return 0; /* WLAN AP is not a P2P manager */
2101 * (Re)Association Request - P2P IE
2102 * P2P Capability attribute (shall be present)
2103 * P2P Interface attribute (present if concurrent device and
2104 * P2P Management is enabled)
2106 tmp = wpabuf_alloc(200);
2110 lpos = p2p_buf_add_ie_hdr(tmp);
2112 if (p2p->num_groups > 0) {
2113 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2114 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2115 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2117 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2119 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2120 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2121 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2122 p2p_buf_add_p2p_interface(tmp, p2p);
2123 p2p_buf_update_ie_hdr(tmp, lpos);
2125 tmplen = wpabuf_len(tmp);
2129 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2138 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2139 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2143 struct p2p_device *peer;
2149 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2151 #ifdef CONFIG_WIFI_DISPLAY
2152 if (p2p->wfd_ie_assoc_req)
2153 extra = wpabuf_len(p2p->wfd_ie_assoc_req);
2154 #endif /* CONFIG_WIFI_DISPLAY */
2157 * (Re)Association Request - P2P IE
2158 * P2P Capability attribute (shall be present)
2159 * Extended Listen Timing (may be present)
2160 * P2P Device Info attribute (shall be present)
2162 tmp = wpabuf_alloc(200 + extra);
2166 #ifdef CONFIG_WIFI_DISPLAY
2167 if (p2p->wfd_ie_assoc_req)
2168 wpabuf_put_buf(tmp, p2p->wfd_ie_assoc_req);
2169 #endif /* CONFIG_WIFI_DISPLAY */
2171 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2173 lpos = p2p_buf_add_ie_hdr(tmp);
2174 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2175 if (p2p->ext_listen_interval)
2176 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2177 p2p->ext_listen_interval);
2178 p2p_buf_add_device_info(tmp, p2p, peer);
2179 p2p_buf_update_ie_hdr(tmp, lpos);
2181 tmplen = wpabuf_len(tmp);
2185 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2194 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2196 struct wpabuf *p2p_ie;
2199 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2203 ret = p2p_attr_text(p2p_ie, buf, end);
2204 wpabuf_free(p2p_ie);
2209 int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2211 struct p2p_message msg;
2213 os_memset(&msg, 0, sizeof(msg));
2214 if (p2p_parse_p2p_ie(p2p_ie, &msg))
2217 if (msg.p2p_device_addr) {
2218 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2220 } else if (msg.device_id) {
2221 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2228 int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2230 struct wpabuf *p2p_ie;
2233 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2234 P2P_IE_VENDOR_TYPE);
2237 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
2238 wpabuf_free(p2p_ie);
2243 static void p2p_clear_go_neg(struct p2p_data *p2p)
2245 p2p->go_neg_peer = NULL;
2246 p2p_clear_timeout(p2p);
2247 p2p_set_state(p2p, P2P_IDLE);
2251 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2253 if (p2p->go_neg_peer == NULL) {
2254 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2255 "P2P: No pending Group Formation - "
2256 "ignore WPS registration success notification");
2257 return; /* No pending Group Formation */
2260 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2262 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2263 "P2P: Ignore WPS registration success notification "
2264 "for " MACSTR " (GO Negotiation peer " MACSTR ")",
2266 MAC2STR(p2p->go_neg_peer->intended_addr));
2267 return; /* Ignore unexpected peer address */
2270 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2271 "P2P: Group Formation completed successfully with " MACSTR,
2274 p2p_clear_go_neg(p2p);
2278 void p2p_group_formation_failed(struct p2p_data *p2p)
2280 if (p2p->go_neg_peer == NULL) {
2281 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2282 "P2P: No pending Group Formation - "
2283 "ignore group formation failure notification");
2284 return; /* No pending Group Formation */
2287 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2288 "P2P: Group Formation failed with " MACSTR,
2289 MAC2STR(p2p->go_neg_peer->intended_addr));
2291 p2p_clear_go_neg(p2p);
2295 struct p2p_data * p2p_init(const struct p2p_config *cfg)
2297 struct p2p_data *p2p;
2299 if (cfg->max_peers < 1)
2302 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2305 p2p->cfg = (struct p2p_config *) (p2p + 1);
2306 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2308 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2309 if (cfg->manufacturer)
2310 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2311 if (cfg->model_name)
2312 p2p->cfg->model_name = os_strdup(cfg->model_name);
2313 if (cfg->model_number)
2314 p2p->cfg->model_number = os_strdup(cfg->model_number);
2315 if (cfg->serial_number)
2316 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
2317 if (cfg->pref_chan) {
2318 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2319 sizeof(struct p2p_channel));
2320 if (p2p->cfg->pref_chan) {
2321 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2322 cfg->num_pref_chan *
2323 sizeof(struct p2p_channel));
2325 p2p->cfg->num_pref_chan = 0;
2328 p2p->min_disc_int = 1;
2329 p2p->max_disc_int = 3;
2331 os_get_random(&p2p->next_tie_breaker, 1);
2332 p2p->next_tie_breaker &= 0x01;
2333 if (cfg->sd_request)
2334 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2335 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2336 if (cfg->concurrent_operations)
2337 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2338 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2340 dl_list_init(&p2p->devices);
2342 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
2343 p2p_expiration_timeout, p2p, NULL);
2345 p2p->go_timeout = 100;
2346 p2p->client_timeout = 20;
2352 void p2p_deinit(struct p2p_data *p2p)
2354 #ifdef CONFIG_WIFI_DISPLAY
2355 wpabuf_free(p2p->wfd_ie_beacon);
2356 wpabuf_free(p2p->wfd_ie_probe_req);
2357 wpabuf_free(p2p->wfd_ie_probe_resp);
2358 wpabuf_free(p2p->wfd_ie_assoc_req);
2359 wpabuf_free(p2p->wfd_ie_invitation);
2360 wpabuf_free(p2p->wfd_ie_prov_disc_req);
2361 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
2362 wpabuf_free(p2p->wfd_ie_go_neg);
2363 wpabuf_free(p2p->wfd_dev_info);
2364 wpabuf_free(p2p->wfd_assoc_bssid);
2365 wpabuf_free(p2p->wfd_coupled_sink_info);
2366 #endif /* CONFIG_WIFI_DISPLAY */
2368 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
2369 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2370 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2372 p2p_free_req_dev_types(p2p);
2373 os_free(p2p->cfg->dev_name);
2374 os_free(p2p->cfg->manufacturer);
2375 os_free(p2p->cfg->model_name);
2376 os_free(p2p->cfg->model_number);
2377 os_free(p2p->cfg->serial_number);
2378 os_free(p2p->cfg->pref_chan);
2379 os_free(p2p->groups);
2380 wpabuf_free(p2p->sd_resp);
2381 os_free(p2p->after_scan_tx);
2382 p2p_remove_wps_vendor_extensions(p2p);
2387 void p2p_flush(struct p2p_data *p2p)
2389 struct p2p_device *dev, *prev;
2391 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2393 dl_list_del(&dev->list);
2394 p2p_device_free(p2p, dev);
2396 p2p_free_sd_queries(p2p);
2397 os_free(p2p->after_scan_tx);
2398 p2p->after_scan_tx = NULL;
2402 int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2404 struct p2p_device *dev;
2406 dev = p2p_get_device(p2p, addr);
2410 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
2413 if (p2p->go_neg_peer == dev)
2414 p2p->go_neg_peer = NULL;
2416 dev->wps_method = WPS_NOT_READY;
2417 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2418 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2420 /* Check if after_scan_tx is for this peer. If so free it */
2421 if (p2p->after_scan_tx &&
2422 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2423 os_free(p2p->after_scan_tx);
2424 p2p->after_scan_tx = NULL;
2431 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2433 os_free(p2p->cfg->dev_name);
2435 p2p->cfg->dev_name = os_strdup(dev_name);
2436 if (p2p->cfg->dev_name == NULL)
2439 p2p->cfg->dev_name = NULL;
2444 int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2446 os_free(p2p->cfg->manufacturer);
2447 p2p->cfg->manufacturer = NULL;
2449 p2p->cfg->manufacturer = os_strdup(manufacturer);
2450 if (p2p->cfg->manufacturer == NULL)
2458 int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2460 os_free(p2p->cfg->model_name);
2461 p2p->cfg->model_name = NULL;
2463 p2p->cfg->model_name = os_strdup(model_name);
2464 if (p2p->cfg->model_name == NULL)
2472 int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2474 os_free(p2p->cfg->model_number);
2475 p2p->cfg->model_number = NULL;
2477 p2p->cfg->model_number = os_strdup(model_number);
2478 if (p2p->cfg->model_number == NULL)
2486 int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2488 os_free(p2p->cfg->serial_number);
2489 p2p->cfg->serial_number = NULL;
2490 if (serial_number) {
2491 p2p->cfg->serial_number = os_strdup(serial_number);
2492 if (p2p->cfg->serial_number == NULL)
2500 void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2502 p2p->cfg->config_methods = config_methods;
2506 void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2508 os_memcpy(p2p->cfg->uuid, uuid, 16);
2512 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2514 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2519 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2520 size_t num_dev_types)
2522 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2523 num_dev_types = P2P_SEC_DEVICE_TYPES;
2524 p2p->cfg->num_sec_dev_types = num_dev_types;
2525 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2530 void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2534 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2535 wpabuf_free(p2p->wps_vendor_ext[i]);
2536 p2p->wps_vendor_ext[i] = NULL;
2541 int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2542 const struct wpabuf *vendor_ext)
2546 if (vendor_ext == NULL)
2549 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2550 if (p2p->wps_vendor_ext[i] == NULL)
2553 if (i >= P2P_MAX_WPS_VENDOR_EXT)
2556 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2557 if (p2p->wps_vendor_ext[i] == NULL)
2564 int p2p_set_country(struct p2p_data *p2p, const char *country)
2566 os_memcpy(p2p->cfg->country, country, 3);
2571 void p2p_continue_find(struct p2p_data *p2p)
2573 struct p2p_device *dev;
2574 p2p_set_state(p2p, P2P_SEARCH);
2575 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2576 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2577 if (p2p_start_sd(p2p, dev) == 0)
2581 } else if (dev->req_config_methods &&
2582 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2583 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2584 "pending Provision Discovery Request to "
2585 MACSTR " (config methods 0x%x)",
2586 MAC2STR(dev->info.p2p_device_addr),
2587 dev->req_config_methods);
2588 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
2593 p2p_listen_in_find(p2p);
2597 static void p2p_sd_cb(struct p2p_data *p2p, int success)
2599 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2600 "P2P: Service Discovery Query TX callback: success=%d",
2602 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2606 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2607 p2p->sd_peer = NULL;
2609 p2p_continue_find(p2p);
2613 if (p2p->sd_peer == NULL) {
2614 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2615 "P2P: No SD peer entry known");
2616 p2p_continue_find(p2p);
2620 /* Wait for response from the peer */
2621 p2p_set_state(p2p, P2P_SD_DURING_FIND);
2622 p2p_set_timeout(p2p, 0, 200000);
2627 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2628 * @p2p: P2P module context from p2p_init()
2630 static void p2p_retry_pd(struct p2p_data *p2p)
2632 struct p2p_device *dev;
2634 if (p2p->state != P2P_IDLE)
2638 * Retry the prov disc req attempt only for the peer that the user had
2639 * requested for and provided a join has not been initiated on it
2643 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2644 if (os_memcmp(p2p->pending_pd_devaddr,
2645 dev->info.p2p_device_addr, ETH_ALEN) != 0)
2647 if (!dev->req_config_methods)
2649 if (dev->flags & P2P_DEV_PD_FOR_JOIN)
2652 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2653 "pending Provision Discovery Request to "
2654 MACSTR " (config methods 0x%x)",
2655 MAC2STR(dev->info.p2p_device_addr),
2656 dev->req_config_methods);
2657 p2p_send_prov_disc_req(p2p, dev, 0, 0);
2663 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2665 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2666 "P2P: Provision Discovery Request TX callback: success=%d",
2670 * Postpone resetting the pending action state till after we actually
2671 * time out. This allows us to take some action like notifying any
2672 * interested parties about no response to the request.
2674 * When the timer (below) goes off we check in IDLE, SEARCH, or
2675 * LISTEN_ONLY state, which are the only allowed states to issue a PD
2676 * requests in, if this was still pending and then raise notification.
2680 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2682 if (p2p->user_initiated_pd &&
2683 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
2685 /* Retry request from timeout to avoid busy loops */
2686 p2p->pending_action_state = P2P_PENDING_PD;
2687 p2p_set_timeout(p2p, 0, 50000);
2688 } else if (p2p->state != P2P_IDLE)
2689 p2p_continue_find(p2p);
2690 else if (p2p->user_initiated_pd) {
2691 p2p->pending_action_state = P2P_PENDING_PD;
2692 p2p_set_timeout(p2p, 0, 300000);
2698 * This postponing, of resetting pending_action_state, needs to be
2699 * done only for user initiated PD requests and not internal ones.
2701 if (p2p->user_initiated_pd)
2702 p2p->pending_action_state = P2P_PENDING_PD;
2704 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2706 /* Wait for response from the peer */
2707 if (p2p->state == P2P_SEARCH)
2708 p2p_set_state(p2p, P2P_PD_DURING_FIND);
2709 p2p_set_timeout(p2p, 0, 200000);
2713 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
2714 int level, const u8 *ies, size_t ies_len)
2716 p2p_add_device(p2p, bssid, freq, level, ies, ies_len, 1);
2722 void p2p_scan_res_handled(struct p2p_data *p2p)
2724 if (!p2p->p2p_scan_running) {
2725 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
2726 "running, but scan results received");
2728 p2p->p2p_scan_running = 0;
2729 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2731 if (p2p_run_after_scan(p2p))
2733 if (p2p->state == P2P_SEARCH)
2734 p2p_continue_find(p2p);
2738 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
2742 #ifdef CONFIG_WIFI_DISPLAY
2743 if (p2p->wfd_ie_probe_req)
2744 wpabuf_put_buf(ies, p2p->wfd_ie_probe_req);
2745 #endif /* CONFIG_WIFI_DISPLAY */
2747 len = p2p_buf_add_ie_hdr(ies);
2748 p2p_buf_add_capability(ies, p2p->dev_capab &
2749 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
2751 p2p_buf_add_device_id(ies, dev_id);
2752 if (p2p->cfg->reg_class && p2p->cfg->channel)
2753 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2754 p2p->cfg->reg_class,
2756 if (p2p->ext_listen_interval)
2757 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2758 p2p->ext_listen_interval);
2759 /* TODO: p2p_buf_add_operating_channel() if GO */
2760 p2p_buf_update_ie_hdr(ies, len);
2764 size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
2768 #ifdef CONFIG_WIFI_DISPLAY
2769 if (p2p && p2p->wfd_ie_probe_req)
2770 len += wpabuf_len(p2p->wfd_ie_probe_req);
2771 #endif /* CONFIG_WIFI_DISPLAY */
2777 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2779 return p2p_attr_text(p2p_ie, buf, end);
2783 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2785 struct p2p_device *dev = p2p->go_neg_peer;
2787 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2788 "P2P: GO Negotiation Request TX callback: success=%d",
2792 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2793 "P2P: No pending GO Negotiation");
2798 if (dev->flags & P2P_DEV_USER_REJECTED) {
2799 p2p_set_state(p2p, P2P_IDLE);
2802 } else if (dev->go_neg_req_sent) {
2803 /* Cancel the increment from p2p_connect_send() on failure */
2804 dev->go_neg_req_sent--;
2808 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2809 !is_zero_ether_addr(dev->member_in_go_dev)) {
2810 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2811 "P2P: Peer " MACSTR " did not acknowledge request - "
2812 "try to use device discoverability through its GO",
2813 MAC2STR(dev->info.p2p_device_addr));
2814 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2815 p2p_send_dev_disc_req(p2p, dev);
2820 * Use P2P find, if needed, to find the other device from its listen
2823 p2p_set_state(p2p, P2P_CONNECT);
2824 p2p_set_timeout(p2p, 0, success ? 200000 : 100000);
2828 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2830 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2831 "P2P: GO Negotiation Response TX callback: success=%d",
2833 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2834 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2835 "P2P: Ignore TX callback event - GO Negotiation is "
2836 "not running anymore");
2839 p2p_set_state(p2p, P2P_CONNECT);
2840 p2p_set_timeout(p2p, 0, 250000);
2844 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2846 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2847 "P2P: GO Negotiation Response (failure) TX callback: "
2848 "success=%d", success);
2849 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2850 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2851 p2p->go_neg_peer->status);
2856 static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
2857 enum p2p_send_action_result result)
2859 struct p2p_device *dev;
2861 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2862 "P2P: GO Negotiation Confirm TX callback: result=%d",
2864 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2865 if (result == P2P_SEND_ACTION_FAILED) {
2866 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2869 if (result == P2P_SEND_ACTION_NO_ACK) {
2871 * It looks like the TX status for GO Negotiation Confirm is
2872 * often showing failure even when the peer has actually
2873 * received the frame. Since the peer may change channels
2874 * immediately after having received the frame, we may not see
2875 * an Ack for retries, so just dropping a single frame may
2876 * trigger this. To allow the group formation to succeed if the
2877 * peer did indeed receive the frame, continue regardless of
2880 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2881 "P2P: Assume GO Negotiation Confirm TX was actually "
2882 "received by the peer even though Ack was not "
2886 dev = p2p->go_neg_peer;
2890 p2p_go_complete(p2p, dev);
2894 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
2895 const u8 *src, const u8 *bssid,
2896 enum p2p_send_action_result result)
2898 enum p2p_pending_action_state state;
2901 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2902 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
2903 " src=" MACSTR " bssid=" MACSTR " result=%d",
2904 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
2905 MAC2STR(bssid), result);
2906 success = result == P2P_SEND_ACTION_SUCCESS;
2907 state = p2p->pending_action_state;
2908 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2910 case P2P_NO_PENDING_ACTION:
2912 case P2P_PENDING_GO_NEG_REQUEST:
2913 p2p_go_neg_req_cb(p2p, success);
2915 case P2P_PENDING_GO_NEG_RESPONSE:
2916 p2p_go_neg_resp_cb(p2p, success);
2918 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
2919 p2p_go_neg_resp_failure_cb(p2p, success);
2921 case P2P_PENDING_GO_NEG_CONFIRM:
2922 p2p_go_neg_conf_cb(p2p, result);
2924 case P2P_PENDING_SD:
2925 p2p_sd_cb(p2p, success);
2927 case P2P_PENDING_PD:
2928 p2p_prov_disc_cb(p2p, success);
2930 case P2P_PENDING_INVITATION_REQUEST:
2931 p2p_invitation_req_cb(p2p, success);
2933 case P2P_PENDING_INVITATION_RESPONSE:
2934 p2p_invitation_resp_cb(p2p, success);
2936 case P2P_PENDING_DEV_DISC_REQUEST:
2937 p2p_dev_disc_req_cb(p2p, success);
2939 case P2P_PENDING_DEV_DISC_RESPONSE:
2940 p2p_dev_disc_resp_cb(p2p, success);
2942 case P2P_PENDING_GO_DISC_REQ:
2943 p2p_go_disc_req_cb(p2p, success);
2949 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
2950 unsigned int duration)
2952 if (freq == p2p->pending_client_disc_freq) {
2953 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2954 "P2P: Client discoverability remain-awake completed");
2955 p2p->pending_client_disc_freq = 0;
2959 if (freq != p2p->pending_listen_freq) {
2960 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2961 "P2P: Unexpected listen callback for freq=%u "
2962 "duration=%u (pending_listen_freq=%u)",
2963 freq, duration, p2p->pending_listen_freq);
2967 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2968 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
2970 p2p->pending_listen_sec, p2p->pending_listen_usec,
2971 p2p->pending_listen_freq);
2973 p2p->drv_in_listen = freq;
2974 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
2976 * Add 20 msec extra wait to avoid race condition with driver
2977 * remain-on-channel end event, i.e., give driver more time to
2978 * complete the operation before our timeout expires.
2980 p2p_set_timeout(p2p, p2p->pending_listen_sec,
2981 p2p->pending_listen_usec + 20000);
2984 p2p->pending_listen_freq = 0;
2988 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
2990 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
2991 "state (freq=%u)", freq);
2992 p2p->drv_in_listen = 0;
2994 return 0; /* Internal timeout will trigger the next step */
2996 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
2997 if (p2p->go_neg_peer->connect_reqs >= 120) {
2998 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2999 "P2P: Timeout on sending GO Negotiation "
3000 "Request without getting response");
3001 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3005 p2p_set_state(p2p, P2P_CONNECT);
3006 p2p_connect_send(p2p, p2p->go_neg_peer);
3008 } else if (p2p->state == P2P_SEARCH) {
3009 if (p2p->p2p_scan_running) {
3011 * Search is already in progress. This can happen if
3012 * an Action frame RX is reported immediately after
3013 * the end of a remain-on-channel operation and the
3014 * response frame to that is sent using an offchannel
3015 * operation while in p2p_find. Avoid an attempt to
3016 * restart a scan here.
3018 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan "
3019 "already in progress - do not try to start a "
3023 if (p2p->pending_listen_freq) {
3025 * Better wait a bit if the driver is unable to start
3026 * offchannel operation for some reason. p2p_search()
3027 * will be started from internal timeout.
3029 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Listen "
3030 "operation did not seem to start - delay "
3031 "search phase to avoid busy loop");
3032 p2p_set_timeout(p2p, 0, 100000);
3035 if (p2p->search_delay) {
3036 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
3037 "search operation by %u ms",
3039 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3040 (p2p->search_delay % 1000) * 1000);
3051 static void p2p_timeout_connect(struct p2p_data *p2p)
3053 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3054 if (p2p->go_neg_peer &&
3055 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
3056 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Wait for GO "
3057 "Negotiation Confirm timed out - assume GO "
3058 "Negotiation failed");
3059 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3062 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
3063 p2p_listen_in_find(p2p);
3067 static void p2p_timeout_connect_listen(struct p2p_data *p2p)
3069 if (p2p->go_neg_peer) {
3070 if (p2p->drv_in_listen) {
3071 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
3072 "still in Listen state; wait for it to "
3077 if (p2p->go_neg_peer->connect_reqs >= 120) {
3078 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3079 "P2P: Timeout on sending GO Negotiation "
3080 "Request without getting response");
3081 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3085 p2p_set_state(p2p, P2P_CONNECT);
3086 p2p_connect_send(p2p, p2p->go_neg_peer);
3088 p2p_set_state(p2p, P2P_IDLE);
3092 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3095 * TODO: could remain constantly in Listen state for some time if there
3096 * are no other concurrent uses for the radio. For now, go to listen
3097 * state once per second to give other uses a chance to use the radio.
3099 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
3100 p2p_set_timeout(p2p, 0, 500000);
3104 static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3106 struct p2p_device *dev = p2p->go_neg_peer;
3109 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3110 "P2P: Unknown GO Neg peer - stop GO Neg wait");
3115 if (dev->wait_count >= 120) {
3116 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3117 "P2P: Timeout on waiting peer to become ready for GO "
3119 p2p_go_neg_failed(p2p, dev, -1);
3123 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3124 "P2P: Go to Listen state while waiting for the peer to become "
3125 "ready for GO Negotiation");
3126 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
3127 p2p_listen_in_find(p2p);
3131 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3133 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3134 "P2P: Service Discovery Query timeout");
3136 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3137 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
3138 p2p->sd_peer = NULL;
3140 p2p_continue_find(p2p);
3144 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
3146 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3147 "P2P: Provision Discovery Request timeout");
3148 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3149 p2p_continue_find(p2p);
3153 static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
3155 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3158 * For user initiated PD requests that we have not gotten any responses
3159 * for while in IDLE state, we retry them a couple of times before
3162 if (!p2p->user_initiated_pd)
3165 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3166 "P2P: User initiated Provision Discovery Request timeout");
3168 if (p2p->pd_retries) {
3172 if (p2p->cfg->prov_disc_fail)
3173 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
3174 p2p->pending_pd_devaddr,
3175 P2P_PROV_DISC_TIMEOUT);
3176 p2p_reset_pending_pd(p2p);
3181 static void p2p_timeout_invite(struct p2p_data *p2p)
3183 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3184 p2p_set_state(p2p, P2P_INVITE_LISTEN);
3185 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
3187 * Better remain on operating channel instead of listen channel
3188 * when running a group.
3190 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
3191 "active GO role - wait on operating channel");
3192 p2p_set_timeout(p2p, 0, 100000);
3195 p2p_listen_in_find(p2p);
3199 static void p2p_timeout_invite_listen(struct p2p_data *p2p)
3201 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
3202 p2p_set_state(p2p, P2P_INVITE);
3203 p2p_invite_send(p2p, p2p->invite_peer,
3204 p2p->invite_go_dev_addr);
3206 if (p2p->invite_peer) {
3207 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3208 "P2P: Invitation Request retry limit reached");
3209 if (p2p->cfg->invitation_result)
3210 p2p->cfg->invitation_result(
3211 p2p->cfg->cb_ctx, -1, NULL);
3213 p2p_set_state(p2p, P2P_IDLE);
3218 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
3220 struct p2p_data *p2p = eloop_ctx;
3222 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
3223 p2p_state_txt(p2p->state));
3227 switch (p2p->state) {
3229 /* Check if we timed out waiting for PD req */
3230 if (p2p->pending_action_state == P2P_PENDING_PD)
3231 p2p_timeout_prov_disc_req(p2p);
3234 /* Check if we timed out waiting for PD req */
3235 if (p2p->pending_action_state == P2P_PENDING_PD)
3236 p2p_timeout_prov_disc_req(p2p);
3237 if (p2p->search_delay && !p2p->in_search_delay) {
3238 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
3239 "search operation by %u ms",
3241 p2p->in_search_delay = 1;
3242 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3243 (p2p->search_delay % 1000) * 1000);
3246 p2p->in_search_delay = 0;
3250 p2p_timeout_connect(p2p);
3252 case P2P_CONNECT_LISTEN:
3253 p2p_timeout_connect_listen(p2p);
3257 case P2P_LISTEN_ONLY:
3258 /* Check if we timed out waiting for PD req */
3259 if (p2p->pending_action_state == P2P_PENDING_PD)
3260 p2p_timeout_prov_disc_req(p2p);
3262 if (p2p->ext_listen_only) {
3263 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3264 "P2P: Extended Listen Timing - Listen State "
3266 p2p->ext_listen_only = 0;
3267 p2p_set_state(p2p, P2P_IDLE);
3270 case P2P_WAIT_PEER_CONNECT:
3271 p2p_timeout_wait_peer_connect(p2p);
3273 case P2P_WAIT_PEER_IDLE:
3274 p2p_timeout_wait_peer_idle(p2p);
3276 case P2P_SD_DURING_FIND:
3277 p2p_timeout_sd_during_find(p2p);
3279 case P2P_PROVISIONING:
3281 case P2P_PD_DURING_FIND:
3282 p2p_timeout_prov_disc_during_find(p2p);
3285 p2p_timeout_invite(p2p);
3287 case P2P_INVITE_LISTEN:
3288 p2p_timeout_invite_listen(p2p);
3290 case P2P_SEARCH_WHEN_READY:
3292 case P2P_CONTINUE_SEARCH_WHEN_READY:
3298 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
3300 struct p2p_device *dev;
3302 dev = p2p_get_device(p2p, peer_addr);
3303 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
3304 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
3306 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
3307 " unknown", MAC2STR(peer_addr));
3310 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
3311 dev->flags |= P2P_DEV_USER_REJECTED;
3316 const char * p2p_wps_method_text(enum p2p_wps_method method)
3321 case WPS_PIN_DISPLAY:
3323 case WPS_PIN_KEYPAD:
3333 static const char * p2p_go_state_text(enum p2p_go_state go_state)
3348 const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
3349 const u8 *addr, int next)
3351 struct p2p_device *dev;
3354 dev = p2p_get_device(p2p, addr);
3356 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3359 dev = dl_list_first(&dev->list, struct p2p_device, list);
3360 if (&dev->list == &p2p->devices)
3371 int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
3372 char *buf, size_t buflen)
3374 struct p2p_device *dev;
3382 dev = (struct p2p_device *) (((u8 *) info) -
3383 offsetof(struct p2p_device, info));
3389 res = os_snprintf(pos, end - pos,
3393 "interface_addr=" MACSTR "\n"
3394 "member_in_go_dev=" MACSTR "\n"
3395 "member_in_go_iface=" MACSTR "\n"
3396 "go_neg_req_sent=%d\n"
3399 "intended_addr=" MACSTR "\n"
3402 "req_config_methods=0x%x\n"
3403 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
3406 "invitation_reqs=%u\n",
3407 (int) (now.sec - dev->last_seen.sec),
3409 p2p_wps_method_text(dev->wps_method),
3410 MAC2STR(dev->interface_addr),
3411 MAC2STR(dev->member_in_go_dev),
3412 MAC2STR(dev->member_in_go_iface),
3413 dev->go_neg_req_sent,
3414 p2p_go_state_text(dev->go_state),
3416 MAC2STR(dev->intended_addr),
3417 dev->country[0] ? dev->country[0] : '_',
3418 dev->country[1] ? dev->country[1] : '_',
3420 dev->req_config_methods,
3421 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
3422 "[PROBE_REQ_ONLY]" : "",
3423 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
3424 dev->flags & P2P_DEV_NOT_YET_READY ?
3425 "[NOT_YET_READY]" : "",
3426 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
3427 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
3429 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
3430 "[PD_PEER_DISPLAY]" : "",
3431 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
3432 "[PD_PEER_KEYPAD]" : "",
3433 dev->flags & P2P_DEV_USER_REJECTED ?
3434 "[USER_REJECTED]" : "",
3435 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
3436 "[PEER_WAITING_RESPONSE]" : "",
3437 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
3438 "[PREFER_PERSISTENT_GROUP]" : "",
3439 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
3440 "[WAIT_GO_NEG_RESPONSE]" : "",
3441 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
3442 "[WAIT_GO_NEG_CONFIRM]" : "",
3443 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
3444 "[GROUP_CLIENT_ONLY]" : "",
3445 dev->flags & P2P_DEV_FORCE_FREQ ?
3446 "[FORCE_FREQ]" : "",
3447 dev->flags & P2P_DEV_PD_FOR_JOIN ?
3448 "[PD_FOR_JOIN]" : "",
3451 dev->invitation_reqs);
3452 if (res < 0 || res >= end - pos)
3456 if (dev->ext_listen_period) {
3457 res = os_snprintf(pos, end - pos,
3458 "ext_listen_period=%u\n"
3459 "ext_listen_interval=%u\n",
3460 dev->ext_listen_period,
3461 dev->ext_listen_interval);
3462 if (res < 0 || res >= end - pos)
3467 if (dev->oper_ssid_len) {
3468 res = os_snprintf(pos, end - pos,
3470 wpa_ssid_txt(dev->oper_ssid,
3471 dev->oper_ssid_len));
3472 if (res < 0 || res >= end - pos)
3477 #ifdef CONFIG_WIFI_DISPLAY
3478 if (dev->info.wfd_subelems) {
3479 res = os_snprintf(pos, end - pos, "wfd_subelems=");
3480 if (res < 0 || res >= end - pos)
3484 pos += wpa_snprintf_hex(pos, end - pos,
3485 wpabuf_head(dev->info.wfd_subelems),
3486 wpabuf_len(dev->info.wfd_subelems));
3488 res = os_snprintf(pos, end - pos, "\n");
3489 if (res < 0 || res >= end - pos)
3493 #endif /* CONFIG_WIFI_DISPLAY */
3499 int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
3501 return p2p_get_device(p2p, addr) != NULL;
3505 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
3508 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3509 "discoverability enabled");
3510 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3512 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3513 "discoverability disabled");
3514 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3519 static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
3520 u32 duration2, u32 interval2)
3523 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
3526 req = wpabuf_alloc(100);
3530 if (duration1 || interval1) {
3531 os_memset(&desc1, 0, sizeof(desc1));
3532 desc1.count_type = 1;
3533 desc1.duration = duration1;
3534 desc1.interval = interval1;
3537 if (duration2 || interval2) {
3538 os_memset(&desc2, 0, sizeof(desc2));
3539 desc2.count_type = 2;
3540 desc2.duration = duration2;
3541 desc2.interval = interval2;
3546 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
3547 len = p2p_buf_add_ie_hdr(req);
3548 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
3549 p2p_buf_update_ie_hdr(req, len);
3555 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
3556 const u8 *own_interface_addr, unsigned int freq,
3557 u32 duration1, u32 interval1, u32 duration2,
3562 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
3563 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
3564 "int1=%u dur2=%u int2=%u",
3565 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
3566 freq, duration1, interval1, duration2, interval2);
3568 req = p2p_build_presence_req(duration1, interval1, duration2,
3573 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3574 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3576 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
3577 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3578 "P2P: Failed to send Action frame");
3586 static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3587 size_t noa_len, u8 dialog_token)
3589 struct wpabuf *resp;
3592 resp = wpabuf_alloc(100 + noa_len);
3596 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3597 len = p2p_buf_add_ie_hdr(resp);
3598 p2p_buf_add_status(resp, status);
3600 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3601 wpabuf_put_le16(resp, noa_len);
3602 wpabuf_put_data(resp, noa, noa_len);
3604 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3605 p2p_buf_update_ie_hdr(resp, len);
3611 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3612 const u8 *sa, const u8 *data, size_t len,
3615 struct p2p_message msg;
3617 struct wpabuf *resp;
3619 struct p2p_group *group = NULL;
3624 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3625 "P2P: Received P2P Action - P2P Presence Request");
3627 for (g = 0; g < p2p->num_groups; g++) {
3628 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3630 group = p2p->groups[g];
3634 if (group == NULL) {
3635 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3636 "P2P: Ignore P2P Presence Request for unknown group "
3637 MACSTR, MAC2STR(da));
3641 if (p2p_parse(data, len, &msg) < 0) {
3642 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3643 "P2P: Failed to parse P2P Presence Request");
3644 status = P2P_SC_FAIL_INVALID_PARAMS;
3649 if (msg.noa == NULL) {
3650 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3651 "P2P: No NoA attribute in P2P Presence Request");
3652 status = P2P_SC_FAIL_INVALID_PARAMS;
3656 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3659 if (p2p->cfg->get_noa)
3660 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3664 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3665 noa_len > 0 ? noa_len : 0,
3668 p2p_parse_free(&msg);
3672 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3673 if (p2p_send_action(p2p, rx_freq, sa, da, da,
3674 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
3675 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3676 "P2P: Failed to send Action frame");
3682 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3683 const u8 *sa, const u8 *data, size_t len)
3685 struct p2p_message msg;
3687 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3688 "P2P: Received P2P Action - P2P Presence Response");
3690 if (p2p_parse(data, len, &msg) < 0) {
3691 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3692 "P2P: Failed to parse P2P Presence Response");
3696 if (msg.status == NULL || msg.noa == NULL) {
3697 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3698 "P2P: No Status or NoA attribute in P2P Presence "
3700 p2p_parse_free(&msg);
3705 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3706 "P2P: P2P Presence Request was rejected: status %u",
3708 p2p_parse_free(&msg);
3712 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3713 "P2P: P2P Presence Request was accepted");
3714 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
3715 msg.noa, msg.noa_len);
3716 /* TODO: process NoA */
3717 p2p_parse_free(&msg);
3721 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3723 struct p2p_data *p2p = eloop_ctx;
3725 if (p2p->ext_listen_interval) {
3726 /* Schedule next extended listen timeout */
3727 eloop_register_timeout(p2p->ext_listen_interval_sec,
3728 p2p->ext_listen_interval_usec,
3729 p2p_ext_listen_timeout, p2p, NULL);
3732 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
3734 * This should not really happen, but it looks like the Listen
3735 * command may fail is something else (e.g., a scan) was
3736 * running at an inconvenient time. As a workaround, allow new
3737 * Extended Listen operation to be started.
3739 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
3740 "Extended Listen operation had not been completed - "
3742 p2p->ext_listen_only = 0;
3743 p2p_set_state(p2p, P2P_IDLE);
3746 if (p2p->state != P2P_IDLE) {
3747 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
3748 "Listen timeout in active state (%s)",
3749 p2p_state_txt(p2p->state));
3753 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
3754 p2p->ext_listen_only = 1;
3755 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
3756 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
3757 "Listen state for Extended Listen Timing");
3758 p2p->ext_listen_only = 0;
3763 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
3764 unsigned int interval)
3766 if (period > 65535 || interval > 65535 || period > interval ||
3767 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
3768 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3769 "P2P: Invalid Extended Listen Timing request: "
3770 "period=%u interval=%u", period, interval);
3774 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
3776 if (interval == 0) {
3777 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3778 "P2P: Disabling Extended Listen Timing");
3779 p2p->ext_listen_period = 0;
3780 p2p->ext_listen_interval = 0;
3784 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3785 "P2P: Enabling Extended Listen Timing: period %u msec, "
3786 "interval %u msec", period, interval);
3787 p2p->ext_listen_period = period;
3788 p2p->ext_listen_interval = interval;
3789 p2p->ext_listen_interval_sec = interval / 1000;
3790 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
3792 eloop_register_timeout(p2p->ext_listen_interval_sec,
3793 p2p->ext_listen_interval_usec,
3794 p2p_ext_listen_timeout, p2p, NULL);
3800 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3801 const u8 *ie, size_t ie_len)
3803 struct p2p_message msg;
3805 if (bssid == NULL || ie == NULL)
3808 os_memset(&msg, 0, sizeof(msg));
3809 if (p2p_parse_ies(ie, ie_len, &msg))
3811 if (msg.minor_reason_code == NULL)
3814 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3815 "P2P: Deauthentication notification BSSID " MACSTR
3816 " reason_code=%u minor_reason_code=%u",
3817 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3819 p2p_parse_free(&msg);
3823 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3824 const u8 *ie, size_t ie_len)
3826 struct p2p_message msg;
3828 if (bssid == NULL || ie == NULL)
3831 os_memset(&msg, 0, sizeof(msg));
3832 if (p2p_parse_ies(ie, ie_len, &msg))
3834 if (msg.minor_reason_code == NULL)
3837 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3838 "P2P: Disassociation notification BSSID " MACSTR
3839 " reason_code=%u minor_reason_code=%u",
3840 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3842 p2p_parse_free(&msg);
3846 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
3849 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3850 "Device operations enabled");
3851 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
3853 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3854 "Device operations disabled");
3855 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
3860 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
3862 if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
3865 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
3866 "reg_class %u channel %u", reg_class, channel);
3867 p2p->cfg->reg_class = reg_class;
3868 p2p->cfg->channel = channel;
3874 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
3876 wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
3877 if (postfix == NULL) {
3878 p2p->cfg->ssid_postfix_len = 0;
3881 if (len > sizeof(p2p->cfg->ssid_postfix))
3883 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
3884 p2p->cfg->ssid_postfix_len = len;
3889 int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
3892 if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
3896 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
3897 "reg_class %u channel %u", op_reg_class, op_channel);
3898 p2p->cfg->op_reg_class = op_reg_class;
3899 p2p->cfg->op_channel = op_channel;
3900 p2p->cfg->cfg_op_channel = cfg_op_channel;
3905 int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
3906 const struct p2p_channel *pref_chan)
3908 struct p2p_channel *n;
3911 n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
3914 os_memcpy(n, pref_chan,
3915 num_pref_chan * sizeof(struct p2p_channel));
3919 os_free(p2p->cfg->pref_chan);
3920 p2p->cfg->pref_chan = n;
3921 p2p->cfg->num_pref_chan = num_pref_chan;
3927 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
3930 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
3931 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
3933 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
3938 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
3941 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3944 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
3949 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
3951 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
3952 if (is_zero_ether_addr(p2p->peer_filter))
3953 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
3956 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
3957 "filter for " MACSTR, MAC2STR(p2p->peer_filter));
3961 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
3963 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
3964 enabled ? "enabled" : "disabled");
3965 if (p2p->cross_connect == enabled)
3967 p2p->cross_connect = enabled;
3968 /* TODO: may need to tear down any action group where we are GO(?) */
3972 int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
3974 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3977 if (dev->oper_freq <= 0)
3979 return dev->oper_freq;
3983 void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
3985 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
3986 enabled ? "enabled" : "disabled");
3987 p2p->cfg->p2p_intra_bss = enabled;
3991 void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
3993 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
3994 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
3998 int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3999 const u8 *src, const u8 *bssid, const u8 *buf,
4000 size_t len, unsigned int wait_time)
4002 if (p2p->p2p_scan_running) {
4003 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
4004 "frame TX until p2p_scan completes");
4005 if (p2p->after_scan_tx) {
4006 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
4007 "previous pending Action frame TX");
4008 os_free(p2p->after_scan_tx);
4010 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
4012 if (p2p->after_scan_tx == NULL)
4014 p2p->after_scan_tx->freq = freq;
4015 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
4016 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
4017 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
4018 p2p->after_scan_tx->len = len;
4019 p2p->after_scan_tx->wait_time = wait_time;
4020 os_memcpy(p2p->after_scan_tx + 1, buf, len);
4024 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
4025 buf, len, wait_time);
4029 void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
4032 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
4033 " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall);
4034 p2p->best_freq_24 = freq_24;
4035 p2p->best_freq_5 = freq_5;
4036 p2p->best_freq_overall = freq_overall;
4040 const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
4042 if (p2p == NULL || p2p->go_neg_peer == NULL)
4044 return p2p->go_neg_peer->info.p2p_device_addr;
4048 const struct p2p_peer_info *
4049 p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
4051 struct p2p_device *dev;
4054 dev = p2p_get_device(p2p, addr);
4059 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
4065 dev = dl_list_first(&dev->list,
4068 if (&dev->list == &p2p->devices)
4070 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
4073 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
4076 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
4077 dev = dl_list_first(&dev->list,
4080 if (&dev->list == &p2p->devices)
4089 int p2p_in_progress(struct p2p_data *p2p)
4093 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SEARCH_WHEN_READY ||
4094 p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY)
4096 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
4100 void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
4104 p2p->go_timeout = go_timeout;
4105 p2p->client_timeout = client_timeout;
4110 void p2p_increase_search_delay(struct p2p_data *p2p, unsigned int delay)
4112 if (p2p && p2p->search_delay < delay)
4113 p2p->search_delay = delay;
4117 #ifdef CONFIG_WIFI_DISPLAY
4119 static void p2p_update_wfd_ie_groups(struct p2p_data *p2p)
4122 struct p2p_group *group;
4124 for (g = 0; g < p2p->num_groups; g++) {
4125 group = p2p->groups[g];
4126 p2p_group_update_ies(group);
4131 int p2p_set_wfd_ie_beacon(struct p2p_data *p2p, struct wpabuf *ie)
4133 wpabuf_free(p2p->wfd_ie_beacon);
4134 p2p->wfd_ie_beacon = ie;
4135 p2p_update_wfd_ie_groups(p2p);
4140 int p2p_set_wfd_ie_probe_req(struct p2p_data *p2p, struct wpabuf *ie)
4142 wpabuf_free(p2p->wfd_ie_probe_req);
4143 p2p->wfd_ie_probe_req = ie;
4148 int p2p_set_wfd_ie_probe_resp(struct p2p_data *p2p, struct wpabuf *ie)
4150 wpabuf_free(p2p->wfd_ie_probe_resp);
4151 p2p->wfd_ie_probe_resp = ie;
4152 p2p_update_wfd_ie_groups(p2p);
4157 int p2p_set_wfd_ie_assoc_req(struct p2p_data *p2p, struct wpabuf *ie)
4159 wpabuf_free(p2p->wfd_ie_assoc_req);
4160 p2p->wfd_ie_assoc_req = ie;
4165 int p2p_set_wfd_ie_invitation(struct p2p_data *p2p, struct wpabuf *ie)
4167 wpabuf_free(p2p->wfd_ie_invitation);
4168 p2p->wfd_ie_invitation = ie;
4173 int p2p_set_wfd_ie_prov_disc_req(struct p2p_data *p2p, struct wpabuf *ie)
4175 wpabuf_free(p2p->wfd_ie_prov_disc_req);
4176 p2p->wfd_ie_prov_disc_req = ie;
4181 int p2p_set_wfd_ie_prov_disc_resp(struct p2p_data *p2p, struct wpabuf *ie)
4183 wpabuf_free(p2p->wfd_ie_prov_disc_resp);
4184 p2p->wfd_ie_prov_disc_resp = ie;
4189 int p2p_set_wfd_ie_go_neg(struct p2p_data *p2p, struct wpabuf *ie)
4191 wpabuf_free(p2p->wfd_ie_go_neg);
4192 p2p->wfd_ie_go_neg = ie;
4197 int p2p_set_wfd_dev_info(struct p2p_data *p2p, const struct wpabuf *elem)
4199 wpabuf_free(p2p->wfd_dev_info);
4201 p2p->wfd_dev_info = wpabuf_dup(elem);
4202 if (p2p->wfd_dev_info == NULL)
4205 p2p->wfd_dev_info = NULL;
4211 int p2p_set_wfd_assoc_bssid(struct p2p_data *p2p, const struct wpabuf *elem)
4213 wpabuf_free(p2p->wfd_assoc_bssid);
4215 p2p->wfd_assoc_bssid = wpabuf_dup(elem);
4216 if (p2p->wfd_assoc_bssid == NULL)
4219 p2p->wfd_assoc_bssid = NULL;
4225 int p2p_set_wfd_coupled_sink_info(struct p2p_data *p2p,
4226 const struct wpabuf *elem)
4228 wpabuf_free(p2p->wfd_coupled_sink_info);
4230 p2p->wfd_coupled_sink_info = wpabuf_dup(elem);
4231 if (p2p->wfd_coupled_sink_info == NULL)
4234 p2p->wfd_coupled_sink_info = NULL;
4239 #endif /* CONFIG_WIFI_DISPLAY */