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";
139 u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
141 struct p2p_device *dev = NULL;
146 dev = p2p_get_device(p2p, addr);
148 return dev->wps_prov_info;
154 void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
156 struct p2p_device *dev = NULL;
161 dev = p2p_get_device(p2p, addr);
163 dev->wps_prov_info = 0;
167 void p2p_set_state(struct p2p_data *p2p, int new_state)
169 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s",
170 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
171 p2p->state = new_state;
175 void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
177 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
178 "P2P: Set timeout (state=%s): %u.%06u sec",
179 p2p_state_txt(p2p->state), sec, usec);
180 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
181 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
185 void p2p_clear_timeout(struct p2p_data *p2p)
187 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)",
188 p2p_state_txt(p2p->state));
189 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
193 void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
196 struct p2p_go_neg_results res;
197 p2p_clear_timeout(p2p);
198 p2p_set_state(p2p, P2P_IDLE);
199 if (p2p->go_neg_peer)
200 p2p->go_neg_peer->wps_method = WPS_NOT_READY;
201 p2p->go_neg_peer = NULL;
203 os_memset(&res, 0, sizeof(res));
206 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr,
208 os_memcpy(res.peer_interface_addr, peer->intended_addr,
211 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
215 static void p2p_listen_in_find(struct p2p_data *p2p)
221 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
222 "P2P: Starting short listen state (state=%s)",
223 p2p_state_txt(p2p->state));
225 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
228 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
229 "P2P: Unknown regulatory class/channel");
233 os_get_random((u8 *) &r, sizeof(r));
234 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
235 p2p->min_disc_int) * 100;
237 p2p->pending_listen_freq = freq;
238 p2p->pending_listen_sec = 0;
239 p2p->pending_listen_usec = 1024 * tu;
241 ies = p2p_build_probe_resp_ies(p2p);
245 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
247 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
248 "P2P: Failed to start listen mode");
249 p2p->pending_listen_freq = 0;
255 int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
260 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
261 "P2P: Going to listen(only) state");
263 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
266 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
267 "P2P: Unknown regulatory class/channel");
271 p2p->pending_listen_freq = freq;
272 p2p->pending_listen_sec = timeout / 1000;
273 p2p->pending_listen_usec = (timeout % 1000) * 1000;
275 if (p2p->p2p_scan_running) {
276 if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
277 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
278 "P2P: p2p_scan running - connect is already "
279 "pending - skip listen");
282 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
283 "P2P: p2p_scan running - delay start of listen state");
284 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
288 ies = p2p_build_probe_resp_ies(p2p);
292 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
293 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
294 "P2P: Failed to start listen mode");
295 p2p->pending_listen_freq = 0;
301 p2p_set_state(p2p, P2P_LISTEN_ONLY);
307 static void p2p_device_clear_reported(struct p2p_data *p2p)
309 struct p2p_device *dev;
310 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
311 dev->flags &= ~P2P_DEV_REPORTED;
316 * p2p_get_device - Fetch a peer entry
317 * @p2p: P2P module context from p2p_init()
318 * @addr: P2P Device Address of the peer
319 * Returns: Pointer to the device entry or %NULL if not found
321 struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
323 struct p2p_device *dev;
324 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
325 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
333 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
334 * @p2p: P2P module context from p2p_init()
335 * @addr: P2P Interface Address of the peer
336 * Returns: Pointer to the device entry or %NULL if not found
338 struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
341 struct p2p_device *dev;
342 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
343 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
351 * p2p_create_device - Create a peer entry
352 * @p2p: P2P module context from p2p_init()
353 * @addr: P2P Device Address of the peer
354 * Returns: Pointer to the device entry or %NULL on failure
356 * If there is already an entry for the peer, it will be returned instead of
357 * creating a new one.
359 static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
362 struct p2p_device *dev, *oldest = NULL;
365 dev = p2p_get_device(p2p, addr);
369 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
371 if (oldest == NULL ||
372 os_time_before(&dev->last_seen, &oldest->last_seen))
375 if (count + 1 > p2p->cfg->max_peers && oldest) {
376 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
377 "P2P: Remove oldest peer entry to make room for a new "
379 dl_list_del(&oldest->list);
380 p2p_device_free(p2p, oldest);
383 dev = os_zalloc(sizeof(*dev));
386 dl_list_add(&p2p->devices, &dev->list);
387 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
393 static void p2p_copy_client_info(struct p2p_device *dev,
394 struct p2p_client_info *cli)
396 os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
397 dev->info.device_name[cli->dev_name_len] = '\0';
398 dev->info.dev_capab = cli->dev_capab;
399 dev->info.config_methods = cli->config_methods;
400 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
401 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
402 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
403 dev->info.wps_sec_dev_type_list_len);
407 static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
408 const u8 *go_interface_addr, int freq,
409 const u8 *gi, size_t gi_len)
411 struct p2p_group_info info;
413 struct p2p_device *dev;
418 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
422 * Clear old data for this group; if the devices are still in the
423 * group, the information will be restored in the loop following this.
425 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
426 if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
428 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
429 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
433 for (c = 0; c < info.num_clients; c++) {
434 struct p2p_client_info *cli = &info.client[c];
435 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
437 continue; /* ignore our own entry */
438 dev = p2p_get_device(p2p, cli->p2p_device_addr);
441 * Update information only if we have not received this
442 * directly from the client.
444 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
445 P2P_DEV_PROBE_REQ_ONLY))
446 p2p_copy_client_info(dev, cli);
447 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
448 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
451 dev = p2p_create_device(p2p, cli->p2p_device_addr);
454 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
455 p2p_copy_client_info(dev, cli);
456 dev->oper_freq = freq;
457 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
458 dev->info.p2p_device_addr,
460 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
463 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
465 os_get_time(&dev->last_seen);
466 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
467 os_memcpy(dev->member_in_go_iface, go_interface_addr,
475 static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
476 const struct p2p_message *msg)
478 os_memcpy(dev->info.device_name, msg->device_name,
479 sizeof(dev->info.device_name));
481 if (msg->manufacturer &&
482 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
483 os_memset(dev->info.manufacturer, 0,
484 sizeof(dev->info.manufacturer));
485 os_memcpy(dev->info.manufacturer, msg->manufacturer,
486 msg->manufacturer_len);
489 if (msg->model_name &&
490 msg->model_name_len < sizeof(dev->info.model_name)) {
491 os_memset(dev->info.model_name, 0,
492 sizeof(dev->info.model_name));
493 os_memcpy(dev->info.model_name, msg->model_name,
494 msg->model_name_len);
497 if (msg->model_number &&
498 msg->model_number_len < sizeof(dev->info.model_number)) {
499 os_memset(dev->info.model_number, 0,
500 sizeof(dev->info.model_number));
501 os_memcpy(dev->info.model_number, msg->model_number,
502 msg->model_number_len);
505 if (msg->serial_number &&
506 msg->serial_number_len < sizeof(dev->info.serial_number)) {
507 os_memset(dev->info.serial_number, 0,
508 sizeof(dev->info.serial_number));
509 os_memcpy(dev->info.serial_number, msg->serial_number,
510 msg->serial_number_len);
513 if (msg->pri_dev_type)
514 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
515 sizeof(dev->info.pri_dev_type));
516 else if (msg->wps_pri_dev_type)
517 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
518 sizeof(dev->info.pri_dev_type));
520 if (msg->wps_sec_dev_type_list) {
521 os_memcpy(dev->info.wps_sec_dev_type_list,
522 msg->wps_sec_dev_type_list,
523 msg->wps_sec_dev_type_list_len);
524 dev->info.wps_sec_dev_type_list_len =
525 msg->wps_sec_dev_type_list_len;
528 if (msg->capability) {
529 dev->info.dev_capab = msg->capability[0];
530 dev->info.group_capab = msg->capability[1];
533 if (msg->ext_listen_timing) {
534 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
535 dev->ext_listen_interval =
536 WPA_GET_LE16(msg->ext_listen_timing + 2);
540 dev->info.config_methods = msg->config_methods ?
541 msg->config_methods : msg->wps_config_methods;
547 * p2p_add_device - Add peer entries based on scan results or P2P frames
548 * @p2p: P2P module context from p2p_init()
549 * @addr: Source address of Beacon or Probe Response frame (may be either
550 * P2P Device Address or P2P Interface Address)
551 * @level: Signal level (signal strength of the received frame from the peer)
552 * @freq: Frequency on which the Beacon or Probe Response frame was received
553 * @ies: IEs from the Beacon or Probe Response frame
554 * @ies_len: Length of ies buffer in octets
555 * @scan_res: Whether this was based on scan results
556 * Returns: 0 on success, -1 on failure
558 * If the scan result is for a GO, the clients in the group will also be added
559 * to the peer table. This function can also be used with some other frames
560 * like Provision Discovery Request that contains P2P Capability and P2P Device
563 int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
564 const u8 *ies, size_t ies_len, int scan_res)
566 struct p2p_device *dev;
567 struct p2p_message msg;
568 const u8 *p2p_dev_addr;
571 os_memset(&msg, 0, sizeof(msg));
572 if (p2p_parse_ies(ies, ies_len, &msg)) {
573 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
574 "P2P: Failed to parse P2P IE for a device entry");
575 p2p_parse_free(&msg);
579 if (msg.p2p_device_addr)
580 p2p_dev_addr = msg.p2p_device_addr;
581 else if (msg.device_id)
582 p2p_dev_addr = msg.device_id;
584 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
585 "P2P: Ignore scan data without P2P Device Info or "
587 p2p_parse_free(&msg);
591 if (!is_zero_ether_addr(p2p->peer_filter) &&
592 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
593 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
594 "filter for " MACSTR " due to peer filter",
595 MAC2STR(p2p_dev_addr));
599 dev = p2p_create_device(p2p, p2p_dev_addr);
601 p2p_parse_free(&msg);
604 os_get_time(&dev->last_seen);
605 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
607 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
608 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
610 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
611 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
613 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
614 dev->oper_ssid_len = msg.ssid[1];
617 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
618 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
620 if (*msg.ds_params == 14)
623 ds_freq = 2407 + *msg.ds_params * 5;
624 if (freq != ds_freq) {
625 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
626 "P2P: Update Listen frequency based on DS "
627 "Parameter Set IE: %d -> %d MHz",
633 if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
634 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
635 "P2P: Update Listen frequency based on scan "
636 "results (" MACSTR " %d -> %d MHz (DS param %d)",
637 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
638 freq, msg.ds_params ? *msg.ds_params : -1);
641 dev->listen_freq = freq;
643 dev->oper_freq = freq;
645 dev->info.level = level;
647 p2p_copy_wps_info(dev, 0, &msg);
649 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
650 wpabuf_free(dev->info.wps_vendor_ext[i]);
651 dev->info.wps_vendor_ext[i] = NULL;
654 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
655 if (msg.wps_vendor_ext[i] == NULL)
657 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
658 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
659 if (dev->info.wps_vendor_ext[i] == NULL)
664 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
665 msg.group_info, msg.group_info_len);
668 p2p_parse_free(&msg);
670 if (p2p_pending_sd_req(p2p, dev))
671 dev->flags |= P2P_DEV_SD_SCHEDULE;
673 if (dev->flags & P2P_DEV_REPORTED)
676 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
677 "P2P: Peer found with Listen frequency %d MHz", freq);
678 if (dev->flags & P2P_DEV_USER_REJECTED) {
679 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
680 "P2P: Do not report rejected device");
684 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
685 !(dev->flags & P2P_DEV_REPORTED_ONCE));
686 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
692 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
696 if (p2p->go_neg_peer == dev) {
698 * If GO Negotiation is in progress, report that it has failed.
700 p2p_go_neg_failed(p2p, dev, -1);
701 p2p->go_neg_peer = NULL;
703 if (p2p->invite_peer == dev)
704 p2p->invite_peer = NULL;
705 if (p2p->sd_peer == dev)
707 if (p2p->pending_client_disc_go == dev)
708 p2p->pending_client_disc_go = NULL;
710 /* dev_lost() device, but only if it was previously dev_found() */
711 if (dev->flags & P2P_DEV_REPORTED_ONCE)
712 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
713 dev->info.p2p_device_addr);
715 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
716 wpabuf_free(dev->info.wps_vendor_ext[i]);
717 dev->info.wps_vendor_ext[i] = NULL;
724 static int p2p_get_next_prog_freq(struct p2p_data *p2p)
726 struct p2p_channels *c;
727 struct p2p_reg_class *cla;
734 c = &p2p->cfg->channels;
735 for (cl = 0; cl < c->reg_classes; cl++) {
736 cla = &c->reg_class[cl];
737 if (cla->reg_class != p2p->last_prog_scan_class)
739 for (ch = 0; ch < cla->channels; ch++) {
740 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
750 /* Start from beginning */
751 reg_class = c->reg_class[0].reg_class;
752 channel = c->reg_class[0].channel[0];
754 /* Pick the next channel */
756 if (ch == cla->channels) {
758 if (cl == c->reg_classes)
762 reg_class = c->reg_class[cl].reg_class;
763 channel = c->reg_class[cl].channel[ch];
766 freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
767 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
768 "channel: reg_class %u channel %u -> %d MHz",
769 reg_class, channel, freq);
770 p2p->last_prog_scan_class = reg_class;
771 p2p->last_prog_scan_chan = channel;
773 if (freq == 2412 || freq == 2437 || freq == 2462)
774 return 0; /* No need to add social channels */
779 static void p2p_search(struct p2p_data *p2p)
782 enum p2p_scan_type type;
783 u16 pw_id = DEV_PW_DEFAULT;
785 if (p2p->drv_in_listen) {
786 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
787 "in Listen state - wait for it to end before "
791 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
793 if (p2p->go_neg_peer) {
795 * Only scan the known listen frequency of the peer
796 * during GO Negotiation start.
798 freq = p2p->go_neg_peer->listen_freq;
800 freq = p2p->go_neg_peer->oper_freq;
801 type = P2P_SCAN_SPECIFIC;
802 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
803 "for freq %u (GO Neg)", freq);
805 /* Advertise immediate availability of WPS credential */
806 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
807 } else if (p2p->invite_peer) {
809 * Only scan the known listen frequency of the peer
810 * during Invite start.
812 freq = p2p->invite_peer->listen_freq;
814 freq = p2p->invite_peer->oper_freq;
815 type = P2P_SCAN_SPECIFIC;
816 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
817 "for freq %u (Invite)", freq);
818 } else if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
819 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
820 type = P2P_SCAN_SOCIAL_PLUS_ONE;
821 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
822 "(+ freq %u)", freq);
824 type = P2P_SCAN_SOCIAL;
825 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
828 if (p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
829 p2p->num_req_dev_types, p2p->req_dev_types,
830 p2p->find_dev_id, pw_id)) {
831 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
832 "P2P: Scan request failed");
833 p2p_continue_find(p2p);
835 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
836 p2p->p2p_scan_running = 1;
837 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
838 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
844 static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
846 struct p2p_data *p2p = eloop_ctx;
847 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
852 static int p2p_run_after_scan(struct p2p_data *p2p)
854 struct p2p_device *dev;
855 enum p2p_after_scan op;
857 if (p2p->after_scan_tx) {
858 /* TODO: schedule p2p_run_after_scan to be called from TX
859 * status callback(?) */
860 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
861 "Action frame at p2p_scan completion");
862 p2p->cfg->send_action(p2p->cfg->cb_ctx,
863 p2p->after_scan_tx->freq,
864 p2p->after_scan_tx->dst,
865 p2p->after_scan_tx->src,
866 p2p->after_scan_tx->bssid,
867 (u8 *) (p2p->after_scan_tx + 1),
868 p2p->after_scan_tx->len,
869 p2p->after_scan_tx->wait_time);
870 os_free(p2p->after_scan_tx);
871 p2p->after_scan_tx = NULL;
875 op = p2p->start_after_scan;
876 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
878 case P2P_AFTER_SCAN_NOTHING:
880 case P2P_AFTER_SCAN_LISTEN:
881 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
882 "requested Listen state");
883 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
884 p2p->pending_listen_usec / 1000);
886 case P2P_AFTER_SCAN_CONNECT:
887 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
888 "requested connect with " MACSTR,
889 MAC2STR(p2p->after_scan_peer));
890 dev = p2p_get_device(p2p, p2p->after_scan_peer);
892 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
896 p2p_connect_send(p2p, dev);
904 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
906 struct p2p_data *p2p = eloop_ctx;
908 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
909 "(running=%d)", p2p->p2p_scan_running);
910 running = p2p->p2p_scan_running;
911 /* Make sure we recover from missed scan results callback */
912 p2p->p2p_scan_running = 0;
915 p2p_run_after_scan(p2p);
919 static void p2p_free_req_dev_types(struct p2p_data *p2p)
921 p2p->num_req_dev_types = 0;
922 os_free(p2p->req_dev_types);
923 p2p->req_dev_types = NULL;
927 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
928 enum p2p_discovery_type type,
929 unsigned int num_req_dev_types, const u8 *req_dev_types,
934 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
936 if (p2p->p2p_scan_running) {
937 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
941 p2p_free_req_dev_types(p2p);
942 if (req_dev_types && num_req_dev_types) {
943 p2p->req_dev_types = os_malloc(num_req_dev_types *
945 if (p2p->req_dev_types == NULL)
947 os_memcpy(p2p->req_dev_types, req_dev_types,
948 num_req_dev_types * WPS_DEV_TYPE_LEN);
949 p2p->num_req_dev_types = num_req_dev_types;
953 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
954 p2p->find_dev_id = p2p->find_dev_id_buf;
956 p2p->find_dev_id = NULL;
958 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
959 p2p_clear_timeout(p2p);
960 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
961 p2p->find_type = type;
962 p2p_device_clear_reported(p2p);
963 p2p_set_state(p2p, P2P_SEARCH);
964 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
965 p2p->last_p2p_find_timeout = timeout;
967 eloop_register_timeout(timeout, 0, p2p_find_timeout,
970 case P2P_FIND_START_WITH_FULL:
971 case P2P_FIND_PROGRESSIVE:
972 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
973 p2p->num_req_dev_types,
974 p2p->req_dev_types, dev_id,
977 case P2P_FIND_ONLY_SOCIAL:
978 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
979 p2p->num_req_dev_types,
980 p2p->req_dev_types, dev_id,
988 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
989 p2p->p2p_scan_running = 1;
990 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
991 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
993 } else if (res == 1) {
994 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
995 "p2p_scan at this point - will try again after "
996 "previous scan completes");
998 p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
999 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1001 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
1003 p2p_set_state(p2p, P2P_IDLE);
1004 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1011 int p2p_other_scan_completed(struct p2p_data *p2p)
1013 if (p2p->state != P2P_SEARCH_WHEN_READY)
1015 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find "
1016 "now that previous scan was completed");
1017 if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
1018 p2p->num_req_dev_types, p2p->req_dev_types,
1019 p2p->find_dev_id) < 0)
1025 void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1027 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
1028 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1029 p2p_clear_timeout(p2p);
1030 if (p2p->state == P2P_SEARCH)
1031 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1032 p2p_set_state(p2p, P2P_IDLE);
1033 p2p_free_req_dev_types(p2p);
1034 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1035 p2p->go_neg_peer = NULL;
1036 p2p->sd_peer = NULL;
1037 p2p->invite_peer = NULL;
1038 p2p_stop_listen_for_freq(p2p, freq);
1042 void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1044 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
1045 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
1046 "since we are on correct channel for response");
1049 if (p2p->in_listen) {
1051 p2p_clear_timeout(p2p);
1053 if (p2p->drv_in_listen) {
1055 * The driver may not deliver callback to p2p_listen_end()
1056 * when the operation gets canceled, so clear the internal
1057 * variable that is tracking driver state.
1059 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear "
1060 "drv_in_listen (%d)", p2p->drv_in_listen);
1061 p2p->drv_in_listen = 0;
1063 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1067 void p2p_stop_find(struct p2p_data *p2p)
1069 p2p_stop_find_for_freq(p2p, 0);
1073 static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
1076 u8 op_reg_class, op_channel;
1077 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
1078 &op_reg_class, &op_channel) < 0) {
1079 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1080 "P2P: Unsupported frequency %u MHz",
1084 if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
1086 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1087 "P2P: Frequency %u MHz (oper_class %u "
1088 "channel %u) not allowed for P2P",
1089 force_freq, op_reg_class, op_channel);
1092 p2p->op_reg_class = op_reg_class;
1093 p2p->op_channel = op_channel;
1094 p2p->channels.reg_classes = 1;
1095 p2p->channels.reg_class[0].channels = 1;
1096 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1097 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
1099 u8 op_reg_class, op_channel;
1101 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1102 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
1103 p2p_freq_to_channel(p2p->cfg->country,
1104 p2p->best_freq_overall,
1105 &op_reg_class, &op_channel) == 0) {
1106 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1107 "P2P: Select best overall channel as "
1108 "operating channel preference");
1109 p2p->op_reg_class = op_reg_class;
1110 p2p->op_channel = op_channel;
1111 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1112 p2p_supported_freq(p2p, p2p->best_freq_5) &&
1113 p2p_freq_to_channel(p2p->cfg->country,
1115 &op_reg_class, &op_channel) ==
1117 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1118 "P2P: Select best 5 GHz channel as "
1119 "operating channel preference");
1120 p2p->op_reg_class = op_reg_class;
1121 p2p->op_channel = op_channel;
1122 } else if (!p2p->cfg->cfg_op_channel &&
1123 p2p->best_freq_24 > 0 &&
1124 p2p_supported_freq(p2p, p2p->best_freq_24) &&
1125 p2p_freq_to_channel(p2p->cfg->country,
1127 &op_reg_class, &op_channel) ==
1129 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1130 "P2P: Select best 2.4 GHz channel as "
1131 "operating channel preference");
1132 p2p->op_reg_class = op_reg_class;
1133 p2p->op_channel = op_channel;
1135 p2p->op_reg_class = p2p->cfg->op_reg_class;
1136 p2p->op_channel = p2p->cfg->op_channel;
1139 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1140 sizeof(struct p2p_channels));
1142 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1143 "P2P: Own preference for operation channel: "
1144 "Operating Class %u Channel %u%s",
1145 p2p->op_reg_class, p2p->op_channel,
1146 force_freq ? " (forced)" : "");
1152 static void p2p_set_dev_persistent(struct p2p_device *dev,
1153 int persistent_group)
1155 switch (persistent_group) {
1157 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1158 P2P_DEV_PREFER_PERSISTENT_RECONN);
1161 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1162 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1165 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1166 P2P_DEV_PREFER_PERSISTENT_RECONN;
1172 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1173 enum p2p_wps_method wps_method,
1174 int go_intent, const u8 *own_interface_addr,
1175 unsigned int force_freq, int persistent_group,
1176 const u8 *force_ssid, size_t force_ssid_len)
1178 struct p2p_device *dev;
1180 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1181 "P2P: Request to start group negotiation - peer=" MACSTR
1182 " GO Intent=%d Intended Interface Address=" MACSTR
1183 " wps_method=%d persistent_group=%d",
1184 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1185 wps_method, persistent_group);
1187 if (p2p_prepare_channel(p2p, force_freq) < 0)
1190 dev = p2p_get_device(p2p, peer_addr);
1191 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1192 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1193 "P2P: Cannot connect to unknown P2P Device " MACSTR,
1194 MAC2STR(peer_addr));
1198 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1199 if (!(dev->info.dev_capab &
1200 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1201 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1202 "P2P: Cannot connect to P2P Device " MACSTR
1203 " that is in a group and is not discoverable",
1204 MAC2STR(peer_addr));
1207 if (dev->oper_freq <= 0) {
1208 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1209 "P2P: Cannot connect to P2P Device " MACSTR
1210 " with incomplete information",
1211 MAC2STR(peer_addr));
1216 * First, try to connect directly. If the peer does not
1217 * acknowledge frames, assume it is sleeping and use device
1218 * discoverability via the GO at that point.
1224 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1225 force_ssid, force_ssid_len);
1226 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1227 p2p->ssid_len = force_ssid_len;
1231 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1232 dev->flags &= ~P2P_DEV_USER_REJECTED;
1233 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1234 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1235 dev->connect_reqs = 0;
1236 dev->go_neg_req_sent = 0;
1237 dev->go_state = UNKNOWN_GO;
1238 p2p_set_dev_persistent(dev, persistent_group);
1239 p2p->go_intent = go_intent;
1240 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1242 if (p2p->state != P2P_IDLE)
1245 if (p2p->after_scan_tx) {
1247 * We need to drop the pending frame to avoid issues with the
1248 * new GO Negotiation, e.g., when the pending frame was from a
1249 * previous attempt at starting a GO Negotiation.
1251 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
1252 "previous pending Action frame TX that was waiting "
1253 "for p2p_scan completion");
1254 os_free(p2p->after_scan_tx);
1255 p2p->after_scan_tx = NULL;
1258 dev->wps_method = wps_method;
1259 dev->status = P2P_SC_SUCCESS;
1262 dev->flags |= P2P_DEV_FORCE_FREQ;
1264 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1266 if (p2p->p2p_scan_running) {
1267 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1268 "P2P: p2p_scan running - delay connect send");
1269 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1270 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1273 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1275 return p2p_connect_send(p2p, dev);
1279 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1280 enum p2p_wps_method wps_method,
1281 int go_intent, const u8 *own_interface_addr,
1282 unsigned int force_freq, int persistent_group,
1283 const u8 *force_ssid, size_t force_ssid_len)
1285 struct p2p_device *dev;
1287 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1288 "P2P: Request to authorize group negotiation - peer=" MACSTR
1289 " GO Intent=%d Intended Interface Address=" MACSTR
1290 " wps_method=%d persistent_group=%d",
1291 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1292 wps_method, persistent_group);
1294 if (p2p_prepare_channel(p2p, force_freq) < 0)
1297 dev = p2p_get_device(p2p, peer_addr);
1299 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1300 "P2P: Cannot authorize unknown P2P Device " MACSTR,
1301 MAC2STR(peer_addr));
1307 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1308 force_ssid, force_ssid_len);
1309 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1310 p2p->ssid_len = force_ssid_len;
1314 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1315 dev->flags &= ~P2P_DEV_USER_REJECTED;
1316 dev->go_neg_req_sent = 0;
1317 dev->go_state = UNKNOWN_GO;
1318 p2p_set_dev_persistent(dev, persistent_group);
1319 p2p->go_intent = go_intent;
1320 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1322 dev->wps_method = wps_method;
1323 dev->status = P2P_SC_SUCCESS;
1326 dev->flags |= P2P_DEV_FORCE_FREQ;
1328 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1334 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1335 struct p2p_device *dev, struct p2p_message *msg)
1337 os_get_time(&dev->last_seen);
1339 p2p_copy_wps_info(dev, 0, msg);
1341 if (msg->listen_channel) {
1343 freq = p2p_channel_to_freq((char *) msg->listen_channel,
1344 msg->listen_channel[3],
1345 msg->listen_channel[4]);
1347 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1348 "P2P: Unknown peer Listen channel: "
1349 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1350 msg->listen_channel[0],
1351 msg->listen_channel[1],
1352 msg->listen_channel[2],
1353 msg->listen_channel[3],
1354 msg->listen_channel[4]);
1356 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1357 "peer " MACSTR " Listen channel: %u -> %u MHz",
1358 MAC2STR(dev->info.p2p_device_addr),
1359 dev->listen_freq, freq);
1360 dev->listen_freq = freq;
1364 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1365 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1366 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1367 "P2P: Completed device entry based on data from "
1368 "GO Negotiation Request");
1370 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1371 "P2P: Created device entry based on GO Neg Req: "
1372 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1374 MAC2STR(dev->info.p2p_device_addr),
1375 dev->info.dev_capab, dev->info.group_capab,
1376 dev->info.device_name, dev->listen_freq);
1379 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1381 if (dev->flags & P2P_DEV_USER_REJECTED) {
1382 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1383 "P2P: Do not report rejected device");
1387 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1388 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1389 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1393 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1395 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1396 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1397 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1398 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1399 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1403 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1405 p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len);
1406 p2p_random(params->passphrase, 8);
1411 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1413 struct p2p_go_neg_results res;
1414 int go = peer->go_state == LOCAL_GO;
1415 struct p2p_channels intersection;
1419 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1420 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1421 "GO)", MAC2STR(peer->info.p2p_device_addr),
1422 go ? "local end" : "peer");
1424 os_memset(&res, 0, sizeof(res));
1426 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1427 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1428 res.wps_method = peer->wps_method;
1429 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1430 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1431 res.persistent_group = 2;
1433 res.persistent_group = 1;
1437 /* Setup AP mode for WPS provisioning */
1438 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1441 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1442 res.ssid_len = p2p->ssid_len;
1443 p2p_random(res.passphrase, 8);
1445 res.freq = peer->oper_freq;
1446 if (p2p->ssid_len) {
1447 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1448 res.ssid_len = p2p->ssid_len;
1452 p2p_channels_intersect(&p2p->channels, &peer->channels,
1455 for (i = 0; i < intersection.reg_classes; i++) {
1456 struct p2p_reg_class *c = &intersection.reg_class[i];
1457 if (freqs + 1 == P2P_MAX_CHANNELS)
1459 for (j = 0; j < c->channels; j++) {
1461 if (freqs + 1 == P2P_MAX_CHANNELS)
1463 freq = p2p_channel_to_freq(peer->country, c->reg_class,
1467 res.freq_list[freqs++] = freq;
1471 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1473 p2p_clear_timeout(p2p);
1475 peer->go_neg_req_sent = 0;
1476 peer->wps_method = WPS_NOT_READY;
1478 p2p_set_state(p2p, P2P_PROVISIONING);
1479 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1483 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1484 const u8 *data, size_t len, int rx_freq)
1486 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1487 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1488 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1494 case P2P_GO_NEG_REQ:
1495 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1497 case P2P_GO_NEG_RESP:
1498 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1500 case P2P_GO_NEG_CONF:
1501 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1503 case P2P_INVITATION_REQ:
1504 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1507 case P2P_INVITATION_RESP:
1508 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1510 case P2P_PROV_DISC_REQ:
1511 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1513 case P2P_PROV_DISC_RESP:
1514 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1516 case P2P_DEV_DISC_REQ:
1517 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1519 case P2P_DEV_DISC_RESP:
1520 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1523 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1524 "P2P: Unsupported P2P Public Action frame type %d",
1531 static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1532 const u8 *sa, const u8 *bssid, const u8 *data,
1533 size_t len, int freq)
1539 case WLAN_PA_VENDOR_SPECIFIC:
1544 if (WPA_GET_BE24(data) != OUI_WFA)
1552 if (*data != P2P_OUI_TYPE)
1555 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1557 case WLAN_PA_GAS_INITIAL_REQ:
1558 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1560 case WLAN_PA_GAS_INITIAL_RESP:
1561 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1563 case WLAN_PA_GAS_COMEBACK_REQ:
1564 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1566 case WLAN_PA_GAS_COMEBACK_RESP:
1567 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1573 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1574 const u8 *bssid, u8 category,
1575 const u8 *data, size_t len, int freq)
1577 if (category == WLAN_ACTION_PUBLIC) {
1578 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1582 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1588 if (WPA_GET_BE24(data) != OUI_WFA)
1593 if (*data != P2P_OUI_TYPE)
1598 /* P2P action frame */
1599 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1600 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1601 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1607 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1608 "P2P: Received P2P Action - Notice of Absence");
1611 case P2P_PRESENCE_REQ:
1612 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1614 case P2P_PRESENCE_RESP:
1615 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1617 case P2P_GO_DISC_REQ:
1618 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1621 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1622 "P2P: Received P2P Action - unknown type %u", data[0]);
1628 static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1630 struct p2p_data *p2p = eloop_ctx;
1631 if (p2p->go_neg_peer == NULL)
1633 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1634 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1635 p2p_connect_send(p2p, p2p->go_neg_peer);
1639 static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1641 struct p2p_data *p2p = eloop_ctx;
1642 if (p2p->invite_peer == NULL)
1644 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1645 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1649 static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1650 const u8 *ie, size_t ie_len)
1652 struct p2p_message msg;
1653 struct p2p_device *dev;
1655 os_memset(&msg, 0, sizeof(msg));
1656 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1658 p2p_parse_free(&msg);
1659 return; /* not a P2P probe */
1662 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1663 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1665 /* The Probe Request is not part of P2P Device Discovery. It is
1666 * not known whether the source address of the frame is the P2P
1667 * Device Address or P2P Interface Address. Do not add a new
1668 * peer entry based on this frames.
1670 p2p_parse_free(&msg);
1674 dev = p2p_get_device(p2p, addr);
1676 if (dev->country[0] == 0 && msg.listen_channel)
1677 os_memcpy(dev->country, msg.listen_channel, 3);
1678 os_get_time(&dev->last_seen);
1679 p2p_parse_free(&msg);
1680 return; /* already known */
1683 dev = p2p_create_device(p2p, addr);
1685 p2p_parse_free(&msg);
1689 os_get_time(&dev->last_seen);
1690 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1692 if (msg.listen_channel) {
1693 os_memcpy(dev->country, msg.listen_channel, 3);
1694 dev->listen_freq = p2p_channel_to_freq(dev->country,
1695 msg.listen_channel[3],
1696 msg.listen_channel[4]);
1699 p2p_copy_wps_info(dev, 1, &msg);
1701 p2p_parse_free(&msg);
1703 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1704 "P2P: Created device entry based on Probe Req: " MACSTR
1705 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1706 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1707 dev->info.group_capab, dev->info.device_name,
1712 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1714 struct p2p_message *msg)
1716 struct p2p_device *dev;
1718 dev = p2p_get_device(p2p, addr);
1720 os_get_time(&dev->last_seen);
1721 return dev; /* already known */
1724 dev = p2p_create_device(p2p, addr);
1728 p2p_add_dev_info(p2p, addr, dev, msg);
1734 static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1736 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1738 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1739 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1740 WPA_GET_BE16(&req_dev_type[6]) == 0)
1741 return 1; /* Category match with wildcard OUI/sub-category */
1746 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1747 size_t num_req_dev_type)
1750 for (i = 0; i < num_req_dev_type; i++) {
1751 if (dev_type_match(dev_type, req_dev_type[i]))
1759 * p2p_match_dev_type - Match local device type with requested type
1760 * @p2p: P2P module context from p2p_init()
1761 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1762 * Returns: 1 on match, 0 on mismatch
1764 * This function can be used to match the Requested Device Type attribute in
1765 * WPS IE with the local device types for deciding whether to reply to a Probe
1768 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1770 struct wps_parse_attr attr;
1773 if (wps_parse_msg(wps, &attr))
1774 return 1; /* assume no Requested Device Type attributes */
1776 if (attr.num_req_dev_type == 0)
1777 return 1; /* no Requested Device Type attributes -> match */
1779 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1780 attr.num_req_dev_type))
1781 return 1; /* Own Primary Device Type matches */
1783 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1784 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1786 attr.num_req_dev_type))
1787 return 1; /* Own Secondary Device Type matches */
1789 /* No matching device type found */
1794 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1800 buf = wpabuf_alloc(1000);
1804 if (p2p->go_neg_peer) {
1805 /* Advertise immediate availability of WPS credential */
1806 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
1809 p2p_build_wps_ie(p2p, buf, pw_id, 1);
1812 len = p2p_buf_add_ie_hdr(buf);
1813 p2p_buf_add_capability(buf, p2p->dev_capab, 0);
1814 if (p2p->ext_listen_interval)
1815 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1816 p2p->ext_listen_interval);
1817 p2p_buf_add_device_info(buf, p2p, NULL);
1818 p2p_buf_update_ie_hdr(buf, len);
1824 static int is_11b(u8 rate)
1826 return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
1830 static int supp_rates_11b_only(struct ieee802_11_elems *elems)
1832 int num_11b = 0, num_others = 0;
1835 if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
1838 for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
1839 if (is_11b(elems->supp_rates[i]))
1845 for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
1847 if (is_11b(elems->ext_supp_rates[i]))
1853 return num_11b > 0 && num_others == 0;
1857 static enum p2p_probe_req_status
1858 p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
1859 const u8 *bssid, const u8 *ie, size_t ie_len)
1861 struct ieee802_11_elems elems;
1863 struct ieee80211_mgmt *resp;
1864 struct p2p_message msg;
1867 if (!p2p->in_listen || !p2p->drv_in_listen) {
1868 /* not in Listen state - ignore Probe Request */
1869 return P2P_PREQ_NOT_LISTEN;
1872 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
1874 /* Ignore invalid Probe Request frames */
1875 return P2P_PREQ_MALFORMED;
1878 if (elems.p2p == NULL) {
1879 /* not a P2P probe - ignore it */
1880 return P2P_PREQ_NOT_P2P;
1883 if (dst && !is_broadcast_ether_addr(dst) &&
1884 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
1885 /* Not sent to the broadcast address or our P2P Device Address
1887 return P2P_PREQ_NOT_PROCESSED;
1890 if (bssid && !is_broadcast_ether_addr(bssid)) {
1891 /* Not sent to the Wildcard BSSID */
1892 return P2P_PREQ_NOT_PROCESSED;
1895 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
1896 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
1898 /* not using P2P Wildcard SSID - ignore */
1899 return P2P_PREQ_NOT_PROCESSED;
1902 if (supp_rates_11b_only(&elems)) {
1903 /* Indicates support for 11b rates only */
1904 return P2P_PREQ_NOT_P2P;
1907 os_memset(&msg, 0, sizeof(msg));
1908 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
1909 /* Could not parse P2P attributes */
1910 return P2P_PREQ_NOT_P2P;
1913 if (msg.device_id &&
1914 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
1915 /* Device ID did not match */
1916 p2p_parse_free(&msg);
1917 return P2P_PREQ_NOT_PROCESSED;
1920 /* Check Requested Device Type match */
1921 if (msg.wps_attributes &&
1922 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
1923 /* No match with Requested Device Type */
1924 p2p_parse_free(&msg);
1925 return P2P_PREQ_NOT_PROCESSED;
1927 p2p_parse_free(&msg);
1929 if (!p2p->cfg->send_probe_resp) {
1930 /* Response generated elsewhere */
1931 return P2P_PREQ_NOT_PROCESSED;
1934 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1935 "P2P: Reply to P2P Probe Request in Listen state");
1938 * We do not really have a specific BSS that this frame is advertising,
1939 * so build a frame that has some information in valid format. This is
1940 * really only used for discovery purposes, not to learn exact BSS
1943 ies = p2p_build_probe_resp_ies(p2p);
1945 return P2P_PREQ_NOT_PROCESSED;
1947 buf = wpabuf_alloc(200 + wpabuf_len(ies));
1950 return P2P_PREQ_NOT_PROCESSED;
1954 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
1956 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
1957 (WLAN_FC_STYPE_PROBE_RESP << 4));
1958 os_memcpy(resp->da, addr, ETH_ALEN);
1959 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
1960 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
1961 resp->u.probe_resp.beacon_int = host_to_le16(100);
1962 /* hardware or low-level driver will setup seq_ctrl and timestamp */
1963 resp->u.probe_resp.capab_info =
1964 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
1965 WLAN_CAPABILITY_PRIVACY |
1966 WLAN_CAPABILITY_SHORT_SLOT_TIME);
1968 wpabuf_put_u8(buf, WLAN_EID_SSID);
1969 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
1970 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1972 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
1973 wpabuf_put_u8(buf, 8);
1974 wpabuf_put_u8(buf, (60 / 5) | 0x80);
1975 wpabuf_put_u8(buf, 90 / 5);
1976 wpabuf_put_u8(buf, (120 / 5) | 0x80);
1977 wpabuf_put_u8(buf, 180 / 5);
1978 wpabuf_put_u8(buf, (240 / 5) | 0x80);
1979 wpabuf_put_u8(buf, 360 / 5);
1980 wpabuf_put_u8(buf, 480 / 5);
1981 wpabuf_put_u8(buf, 540 / 5);
1983 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
1984 wpabuf_put_u8(buf, 1);
1985 wpabuf_put_u8(buf, p2p->cfg->channel);
1987 wpabuf_put_buf(buf, ies);
1990 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
1994 return P2P_PREQ_NOT_PROCESSED;
1998 enum p2p_probe_req_status
1999 p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2000 const u8 *bssid, const u8 *ie, size_t ie_len)
2002 enum p2p_probe_req_status res;
2004 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2006 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
2008 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2010 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
2012 /* Received a Probe Request from GO Negotiation peer */
2013 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2014 "P2P: Found GO Negotiation peer - try to start GO "
2015 "negotiation from timeout");
2016 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
2017 return P2P_PREQ_PROCESSED;
2020 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2022 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2024 /* Received a Probe Request from Invite peer */
2025 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2026 "P2P: Found Invite peer - try to start Invite from "
2028 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
2029 return P2P_PREQ_PROCESSED;
2036 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2037 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2046 return 0; /* WLAN AP is not a P2P manager */
2049 * (Re)Association Request - P2P IE
2050 * P2P Capability attribute (shall be present)
2051 * P2P Interface attribute (present if concurrent device and
2052 * P2P Management is enabled)
2054 tmp = wpabuf_alloc(200);
2058 lpos = p2p_buf_add_ie_hdr(tmp);
2060 if (p2p->num_groups > 0) {
2061 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2062 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2063 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2065 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2067 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2068 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2069 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2070 p2p_buf_add_p2p_interface(tmp, p2p);
2071 p2p_buf_update_ie_hdr(tmp, lpos);
2073 tmplen = wpabuf_len(tmp);
2077 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2086 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2087 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2091 struct p2p_device *peer;
2096 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2099 * (Re)Association Request - P2P IE
2100 * P2P Capability attribute (shall be present)
2101 * Extended Listen Timing (may be present)
2102 * P2P Device Info attribute (shall be present)
2104 tmp = wpabuf_alloc(200);
2108 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2110 lpos = p2p_buf_add_ie_hdr(tmp);
2111 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2112 if (p2p->ext_listen_interval)
2113 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2114 p2p->ext_listen_interval);
2115 p2p_buf_add_device_info(tmp, p2p, peer);
2116 p2p_buf_update_ie_hdr(tmp, lpos);
2118 tmplen = wpabuf_len(tmp);
2122 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2131 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2133 struct wpabuf *p2p_ie;
2136 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2140 ret = p2p_attr_text(p2p_ie, buf, end);
2141 wpabuf_free(p2p_ie);
2146 int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2148 struct wpabuf *p2p_ie;
2149 struct p2p_message msg;
2152 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2153 P2P_IE_VENDOR_TYPE);
2156 os_memset(&msg, 0, sizeof(msg));
2157 if (p2p_parse_p2p_ie(p2p_ie, &msg)) {
2158 wpabuf_free(p2p_ie);
2162 if (msg.p2p_device_addr) {
2163 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2165 } else if (msg.device_id) {
2166 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2170 wpabuf_free(p2p_ie);
2175 static void p2p_clear_go_neg(struct p2p_data *p2p)
2177 p2p->go_neg_peer = NULL;
2178 p2p_clear_timeout(p2p);
2179 p2p_set_state(p2p, P2P_IDLE);
2183 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2185 if (p2p->go_neg_peer == NULL) {
2186 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2187 "P2P: No pending Group Formation - "
2188 "ignore WPS registration success notification");
2189 return; /* No pending Group Formation */
2192 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2194 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2195 "P2P: Ignore WPS registration success notification "
2196 "for " MACSTR " (GO Negotiation peer " MACSTR ")",
2198 MAC2STR(p2p->go_neg_peer->intended_addr));
2199 return; /* Ignore unexpected peer address */
2202 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2203 "P2P: Group Formation completed successfully with " MACSTR,
2206 p2p_clear_go_neg(p2p);
2210 void p2p_group_formation_failed(struct p2p_data *p2p)
2212 if (p2p->go_neg_peer == NULL) {
2213 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2214 "P2P: No pending Group Formation - "
2215 "ignore group formation failure notification");
2216 return; /* No pending Group Formation */
2219 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2220 "P2P: Group Formation failed with " MACSTR,
2221 MAC2STR(p2p->go_neg_peer->intended_addr));
2223 p2p_clear_go_neg(p2p);
2227 struct p2p_data * p2p_init(const struct p2p_config *cfg)
2229 struct p2p_data *p2p;
2231 if (cfg->max_peers < 1)
2234 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2237 p2p->cfg = (struct p2p_config *) (p2p + 1);
2238 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2240 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2241 if (cfg->manufacturer)
2242 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2243 if (cfg->model_name)
2244 p2p->cfg->model_name = os_strdup(cfg->model_name);
2245 if (cfg->model_number)
2246 p2p->cfg->model_number = os_strdup(cfg->model_number);
2247 if (cfg->serial_number)
2248 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
2249 if (cfg->pref_chan) {
2250 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2251 sizeof(struct p2p_channel));
2252 if (p2p->cfg->pref_chan) {
2253 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2254 cfg->num_pref_chan *
2255 sizeof(struct p2p_channel));
2257 p2p->cfg->num_pref_chan = 0;
2260 p2p->min_disc_int = 1;
2261 p2p->max_disc_int = 3;
2263 os_get_random(&p2p->next_tie_breaker, 1);
2264 p2p->next_tie_breaker &= 0x01;
2265 if (cfg->sd_request)
2266 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2267 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2268 if (cfg->concurrent_operations)
2269 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2270 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2272 dl_list_init(&p2p->devices);
2274 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
2275 p2p_expiration_timeout, p2p, NULL);
2281 void p2p_deinit(struct p2p_data *p2p)
2283 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
2284 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2285 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2287 p2p_free_req_dev_types(p2p);
2288 os_free(p2p->cfg->dev_name);
2289 os_free(p2p->cfg->manufacturer);
2290 os_free(p2p->cfg->model_name);
2291 os_free(p2p->cfg->model_number);
2292 os_free(p2p->cfg->serial_number);
2293 os_free(p2p->cfg->pref_chan);
2294 os_free(p2p->groups);
2295 wpabuf_free(p2p->sd_resp);
2296 os_free(p2p->after_scan_tx);
2297 p2p_remove_wps_vendor_extensions(p2p);
2302 void p2p_flush(struct p2p_data *p2p)
2304 struct p2p_device *dev, *prev;
2306 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2308 dl_list_del(&dev->list);
2309 p2p_device_free(p2p, dev);
2311 p2p_free_sd_queries(p2p);
2312 os_free(p2p->after_scan_tx);
2313 p2p->after_scan_tx = NULL;
2317 int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2319 struct p2p_device *dev;
2321 dev = p2p_get_device(p2p, addr);
2325 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
2328 if (p2p->go_neg_peer == dev)
2329 p2p->go_neg_peer = NULL;
2331 dev->wps_method = WPS_NOT_READY;
2332 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2333 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2335 /* Check if after_scan_tx is for this peer. If so free it */
2336 if (p2p->after_scan_tx &&
2337 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2338 os_free(p2p->after_scan_tx);
2339 p2p->after_scan_tx = NULL;
2346 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2348 os_free(p2p->cfg->dev_name);
2350 p2p->cfg->dev_name = os_strdup(dev_name);
2351 if (p2p->cfg->dev_name == NULL)
2354 p2p->cfg->dev_name = NULL;
2359 int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2361 os_free(p2p->cfg->manufacturer);
2362 p2p->cfg->manufacturer = NULL;
2364 p2p->cfg->manufacturer = os_strdup(manufacturer);
2365 if (p2p->cfg->manufacturer == NULL)
2373 int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2375 os_free(p2p->cfg->model_name);
2376 p2p->cfg->model_name = NULL;
2378 p2p->cfg->model_name = os_strdup(model_name);
2379 if (p2p->cfg->model_name == NULL)
2387 int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2389 os_free(p2p->cfg->model_number);
2390 p2p->cfg->model_number = NULL;
2392 p2p->cfg->model_number = os_strdup(model_number);
2393 if (p2p->cfg->model_number == NULL)
2401 int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2403 os_free(p2p->cfg->serial_number);
2404 p2p->cfg->serial_number = NULL;
2405 if (serial_number) {
2406 p2p->cfg->serial_number = os_strdup(serial_number);
2407 if (p2p->cfg->serial_number == NULL)
2415 void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2417 p2p->cfg->config_methods = config_methods;
2421 void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2423 os_memcpy(p2p->cfg->uuid, uuid, 16);
2427 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2429 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2434 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2435 size_t num_dev_types)
2437 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2438 num_dev_types = P2P_SEC_DEVICE_TYPES;
2439 p2p->cfg->num_sec_dev_types = num_dev_types;
2440 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2445 void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2449 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2450 wpabuf_free(p2p->wps_vendor_ext[i]);
2451 p2p->wps_vendor_ext[i] = NULL;
2456 int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2457 const struct wpabuf *vendor_ext)
2461 if (vendor_ext == NULL)
2464 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2465 if (p2p->wps_vendor_ext[i] == NULL)
2468 if (i >= P2P_MAX_WPS_VENDOR_EXT)
2471 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2472 if (p2p->wps_vendor_ext[i] == NULL)
2479 int p2p_set_country(struct p2p_data *p2p, const char *country)
2481 os_memcpy(p2p->cfg->country, country, 3);
2486 void p2p_continue_find(struct p2p_data *p2p)
2488 struct p2p_device *dev;
2489 p2p_set_state(p2p, P2P_SEARCH);
2490 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2491 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2492 if (p2p_start_sd(p2p, dev) == 0)
2496 } else if (dev->req_config_methods &&
2497 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2498 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2499 "pending Provision Discovery Request to "
2500 MACSTR " (config methods 0x%x)",
2501 MAC2STR(dev->info.p2p_device_addr),
2502 dev->req_config_methods);
2503 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
2508 p2p_listen_in_find(p2p);
2512 static void p2p_sd_cb(struct p2p_data *p2p, int success)
2514 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2515 "P2P: Service Discovery Query TX callback: success=%d",
2517 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2521 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2522 p2p->sd_peer = NULL;
2524 p2p_continue_find(p2p);
2528 if (p2p->sd_peer == NULL) {
2529 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2530 "P2P: No SD peer entry known");
2531 p2p_continue_find(p2p);
2535 /* Wait for response from the peer */
2536 p2p_set_state(p2p, P2P_SD_DURING_FIND);
2537 p2p_set_timeout(p2p, 0, 200000);
2542 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2543 * @p2p: P2P module context from p2p_init()
2545 static void p2p_retry_pd(struct p2p_data *p2p)
2547 struct p2p_device *dev;
2549 if (p2p->state != P2P_IDLE)
2553 * Retry the prov disc req attempt only for the peer that the user had
2554 * requested for and provided a join has not been initiated on it
2558 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2559 if (os_memcmp(p2p->pending_pd_devaddr,
2560 dev->info.p2p_device_addr, ETH_ALEN) != 0)
2562 if (!dev->req_config_methods)
2564 if (dev->flags & P2P_DEV_PD_FOR_JOIN)
2567 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2568 "pending Provision Discovery Request to "
2569 MACSTR " (config methods 0x%x)",
2570 MAC2STR(dev->info.p2p_device_addr),
2571 dev->req_config_methods);
2572 p2p_send_prov_disc_req(p2p, dev, 0, 0);
2578 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2580 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2581 "P2P: Provision Discovery Request TX callback: success=%d",
2585 * Postpone resetting the pending action state till after we actually
2586 * time out. This allows us to take some action like notifying any
2587 * interested parties about no response to the request.
2589 * When the timer (below) goes off we check in IDLE, SEARCH, or
2590 * LISTEN_ONLY state, which are the only allowed states to issue a PD
2591 * requests in, if this was still pending and then raise notification.
2595 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2597 if (p2p->state != P2P_IDLE)
2598 p2p_continue_find(p2p);
2599 else if (p2p->user_initiated_pd) {
2600 p2p->pending_action_state = P2P_PENDING_PD;
2601 p2p_set_timeout(p2p, 0, 300000);
2607 * This postponing, of resetting pending_action_state, needs to be
2608 * done only for user initiated PD requests and not internal ones.
2610 if (p2p->user_initiated_pd)
2611 p2p->pending_action_state = P2P_PENDING_PD;
2613 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2615 /* Wait for response from the peer */
2616 if (p2p->state == P2P_SEARCH)
2617 p2p_set_state(p2p, P2P_PD_DURING_FIND);
2618 p2p_set_timeout(p2p, 0, 200000);
2622 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
2623 int level, const u8 *ies, size_t ies_len)
2625 p2p_add_device(p2p, bssid, freq, level, ies, ies_len, 1);
2627 if (p2p->go_neg_peer && p2p->state == P2P_SEARCH &&
2628 os_memcmp(p2p->go_neg_peer->info.p2p_device_addr, bssid, ETH_ALEN)
2630 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2631 "P2P: Found GO Negotiation peer - try to start GO "
2633 p2p_connect_send(p2p, p2p->go_neg_peer);
2641 void p2p_scan_res_handled(struct p2p_data *p2p)
2643 if (!p2p->p2p_scan_running) {
2644 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
2645 "running, but scan results received");
2647 p2p->p2p_scan_running = 0;
2648 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2650 if (p2p_run_after_scan(p2p))
2652 if (p2p->state == P2P_SEARCH)
2653 p2p_continue_find(p2p);
2657 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
2659 u8 *len = p2p_buf_add_ie_hdr(ies);
2660 p2p_buf_add_capability(ies, p2p->dev_capab, 0);
2662 p2p_buf_add_device_id(ies, dev_id);
2663 if (p2p->cfg->reg_class && p2p->cfg->channel)
2664 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2665 p2p->cfg->reg_class,
2667 if (p2p->ext_listen_interval)
2668 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2669 p2p->ext_listen_interval);
2670 /* TODO: p2p_buf_add_operating_channel() if GO */
2671 p2p_buf_update_ie_hdr(ies, len);
2675 size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
2681 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2683 return p2p_attr_text(p2p_ie, buf, end);
2687 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2689 struct p2p_device *dev = p2p->go_neg_peer;
2691 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2692 "P2P: GO Negotiation Request TX callback: success=%d",
2696 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2697 "P2P: No pending GO Negotiation");
2702 dev->go_neg_req_sent++;
2703 if (dev->flags & P2P_DEV_USER_REJECTED) {
2704 p2p_set_state(p2p, P2P_IDLE);
2710 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2711 !is_zero_ether_addr(dev->member_in_go_dev)) {
2712 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2713 "P2P: Peer " MACSTR " did not acknowledge request - "
2714 "try to use device discoverability through its GO",
2715 MAC2STR(dev->info.p2p_device_addr));
2716 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2717 p2p_send_dev_disc_req(p2p, dev);
2722 * Use P2P find, if needed, to find the other device from its listen
2725 p2p_set_state(p2p, P2P_CONNECT);
2726 p2p_set_timeout(p2p, 0, 100000);
2730 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2732 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2733 "P2P: GO Negotiation Response TX callback: success=%d",
2735 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2736 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2737 "P2P: Ignore TX callback event - GO Negotiation is "
2738 "not running anymore");
2741 p2p_set_state(p2p, P2P_CONNECT);
2742 p2p_set_timeout(p2p, 0, 100000);
2746 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2748 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2749 "P2P: GO Negotiation Response (failure) TX callback: "
2750 "success=%d", success);
2751 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2752 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2753 p2p->go_neg_peer->status);
2758 static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
2759 enum p2p_send_action_result result)
2761 struct p2p_device *dev;
2763 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2764 "P2P: GO Negotiation Confirm TX callback: result=%d",
2766 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2767 if (result == P2P_SEND_ACTION_FAILED) {
2768 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2771 if (result == P2P_SEND_ACTION_NO_ACK) {
2773 * It looks like the TX status for GO Negotiation Confirm is
2774 * often showing failure even when the peer has actually
2775 * received the frame. Since the peer may change channels
2776 * immediately after having received the frame, we may not see
2777 * an Ack for retries, so just dropping a single frame may
2778 * trigger this. To allow the group formation to succeed if the
2779 * peer did indeed receive the frame, continue regardless of
2782 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2783 "P2P: Assume GO Negotiation Confirm TX was actually "
2784 "received by the peer even though Ack was not "
2788 dev = p2p->go_neg_peer;
2792 p2p_go_complete(p2p, dev);
2796 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
2797 const u8 *src, const u8 *bssid,
2798 enum p2p_send_action_result result)
2800 enum p2p_pending_action_state state;
2803 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2804 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
2805 " src=" MACSTR " bssid=" MACSTR " result=%d",
2806 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
2807 MAC2STR(bssid), result);
2808 success = result == P2P_SEND_ACTION_SUCCESS;
2809 state = p2p->pending_action_state;
2810 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2812 case P2P_NO_PENDING_ACTION:
2814 case P2P_PENDING_GO_NEG_REQUEST:
2815 p2p_go_neg_req_cb(p2p, success);
2817 case P2P_PENDING_GO_NEG_RESPONSE:
2818 p2p_go_neg_resp_cb(p2p, success);
2820 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
2821 p2p_go_neg_resp_failure_cb(p2p, success);
2823 case P2P_PENDING_GO_NEG_CONFIRM:
2824 p2p_go_neg_conf_cb(p2p, result);
2826 case P2P_PENDING_SD:
2827 p2p_sd_cb(p2p, success);
2829 case P2P_PENDING_PD:
2830 p2p_prov_disc_cb(p2p, success);
2832 case P2P_PENDING_INVITATION_REQUEST:
2833 p2p_invitation_req_cb(p2p, success);
2835 case P2P_PENDING_INVITATION_RESPONSE:
2836 p2p_invitation_resp_cb(p2p, success);
2838 case P2P_PENDING_DEV_DISC_REQUEST:
2839 p2p_dev_disc_req_cb(p2p, success);
2841 case P2P_PENDING_DEV_DISC_RESPONSE:
2842 p2p_dev_disc_resp_cb(p2p, success);
2844 case P2P_PENDING_GO_DISC_REQ:
2845 p2p_go_disc_req_cb(p2p, success);
2851 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
2852 unsigned int duration)
2854 if (freq == p2p->pending_client_disc_freq) {
2855 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2856 "P2P: Client discoverability remain-awake completed");
2857 p2p->pending_client_disc_freq = 0;
2861 if (freq != p2p->pending_listen_freq) {
2862 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2863 "P2P: Unexpected listen callback for freq=%u "
2864 "duration=%u (pending_listen_freq=%u)",
2865 freq, duration, p2p->pending_listen_freq);
2869 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2870 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
2872 p2p->pending_listen_sec, p2p->pending_listen_usec,
2873 p2p->pending_listen_freq);
2875 p2p->drv_in_listen = freq;
2876 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
2878 * Add 20 msec extra wait to avoid race condition with driver
2879 * remain-on-channel end event, i.e., give driver more time to
2880 * complete the operation before our timeout expires.
2882 p2p_set_timeout(p2p, p2p->pending_listen_sec,
2883 p2p->pending_listen_usec + 20000);
2886 p2p->pending_listen_freq = 0;
2890 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
2892 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
2893 "state (freq=%u)", freq);
2894 p2p->drv_in_listen = 0;
2896 return 0; /* Internal timeout will trigger the next step */
2898 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
2899 if (p2p->go_neg_peer->connect_reqs >= 120) {
2900 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2901 "P2P: Timeout on sending GO Negotiation "
2902 "Request without getting response");
2903 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2907 p2p_set_state(p2p, P2P_CONNECT);
2908 p2p_connect_send(p2p, p2p->go_neg_peer);
2910 } else if (p2p->state == P2P_SEARCH) {
2911 if (p2p->p2p_scan_running) {
2913 * Search is already in progress. This can happen if
2914 * an Action frame RX is reported immediately after
2915 * the end of a remain-on-channel operation and the
2916 * response frame to that is sent using an offchannel
2917 * operation while in p2p_find. Avoid an attempt to
2918 * restart a scan here.
2920 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan "
2921 "already in progress - do not try to start a "
2933 static void p2p_timeout_connect(struct p2p_data *p2p)
2935 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2936 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
2937 p2p_listen_in_find(p2p);
2941 static void p2p_timeout_connect_listen(struct p2p_data *p2p)
2943 if (p2p->go_neg_peer) {
2944 if (p2p->drv_in_listen) {
2945 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
2946 "still in Listen state; wait for it to "
2951 if (p2p->go_neg_peer->connect_reqs >= 120) {
2952 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2953 "P2P: Timeout on sending GO Negotiation "
2954 "Request without getting response");
2955 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2959 p2p_set_state(p2p, P2P_CONNECT);
2960 p2p_connect_send(p2p, p2p->go_neg_peer);
2962 p2p_set_state(p2p, P2P_IDLE);
2966 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
2969 * TODO: could remain constantly in Listen state for some time if there
2970 * are no other concurrent uses for the radio. For now, go to listen
2971 * state once per second to give other uses a chance to use the radio.
2973 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
2974 p2p_set_timeout(p2p, 0, 500000);
2978 static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
2980 struct p2p_device *dev = p2p->go_neg_peer;
2983 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2984 "P2P: Unknown GO Neg peer - stop GO Neg wait");
2989 if (dev->wait_count >= 120) {
2990 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2991 "P2P: Timeout on waiting peer to become ready for GO "
2993 p2p_go_neg_failed(p2p, dev, -1);
2997 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2998 "P2P: Go to Listen state while waiting for the peer to become "
2999 "ready for GO Negotiation");
3000 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
3001 p2p_listen_in_find(p2p);
3005 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3007 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3008 "P2P: Service Discovery Query timeout");
3010 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3011 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
3012 p2p->sd_peer = NULL;
3014 p2p_continue_find(p2p);
3018 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
3020 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3021 "P2P: Provision Discovery Request timeout");
3022 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3023 p2p_continue_find(p2p);
3027 static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
3029 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3032 * For user initiated PD requests that we have not gotten any responses
3033 * for while in IDLE state, we retry them a couple of times before
3036 if (!p2p->user_initiated_pd)
3039 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3040 "P2P: User initiated Provision Discovery Request timeout");
3042 if (p2p->pd_retries) {
3046 if (p2p->cfg->prov_disc_fail)
3047 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
3048 p2p->pending_pd_devaddr,
3049 P2P_PROV_DISC_TIMEOUT);
3050 p2p_reset_pending_pd(p2p);
3055 static void p2p_timeout_invite(struct p2p_data *p2p)
3057 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3058 p2p_set_state(p2p, P2P_INVITE_LISTEN);
3059 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
3061 * Better remain on operating channel instead of listen channel
3062 * when running a group.
3064 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
3065 "active GO role - wait on operating channel");
3066 p2p_set_timeout(p2p, 0, 100000);
3069 p2p_listen_in_find(p2p);
3073 static void p2p_timeout_invite_listen(struct p2p_data *p2p)
3075 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
3076 p2p_set_state(p2p, P2P_INVITE);
3077 p2p_invite_send(p2p, p2p->invite_peer,
3078 p2p->invite_go_dev_addr);
3080 if (p2p->invite_peer) {
3081 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3082 "P2P: Invitation Request retry limit reached");
3083 if (p2p->cfg->invitation_result)
3084 p2p->cfg->invitation_result(
3085 p2p->cfg->cb_ctx, -1, NULL);
3087 p2p_set_state(p2p, P2P_IDLE);
3092 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
3094 struct p2p_data *p2p = eloop_ctx;
3096 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
3097 p2p_state_txt(p2p->state));
3101 switch (p2p->state) {
3103 /* Check if we timed out waiting for PD req */
3104 if (p2p->pending_action_state == P2P_PENDING_PD)
3105 p2p_timeout_prov_disc_req(p2p);
3108 /* Check if we timed out waiting for PD req */
3109 if (p2p->pending_action_state == P2P_PENDING_PD)
3110 p2p_timeout_prov_disc_req(p2p);
3114 p2p_timeout_connect(p2p);
3116 case P2P_CONNECT_LISTEN:
3117 p2p_timeout_connect_listen(p2p);
3121 case P2P_LISTEN_ONLY:
3122 /* Check if we timed out waiting for PD req */
3123 if (p2p->pending_action_state == P2P_PENDING_PD)
3124 p2p_timeout_prov_disc_req(p2p);
3126 if (p2p->ext_listen_only) {
3127 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3128 "P2P: Extended Listen Timing - Listen State "
3130 p2p->ext_listen_only = 0;
3131 p2p_set_state(p2p, P2P_IDLE);
3134 case P2P_WAIT_PEER_CONNECT:
3135 p2p_timeout_wait_peer_connect(p2p);
3137 case P2P_WAIT_PEER_IDLE:
3138 p2p_timeout_wait_peer_idle(p2p);
3140 case P2P_SD_DURING_FIND:
3141 p2p_timeout_sd_during_find(p2p);
3143 case P2P_PROVISIONING:
3145 case P2P_PD_DURING_FIND:
3146 p2p_timeout_prov_disc_during_find(p2p);
3149 p2p_timeout_invite(p2p);
3151 case P2P_INVITE_LISTEN:
3152 p2p_timeout_invite_listen(p2p);
3154 case P2P_SEARCH_WHEN_READY:
3160 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
3162 struct p2p_device *dev;
3164 dev = p2p_get_device(p2p, peer_addr);
3165 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
3166 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
3168 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
3169 " unknown", MAC2STR(peer_addr));
3172 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
3173 dev->flags |= P2P_DEV_USER_REJECTED;
3178 const char * p2p_wps_method_text(enum p2p_wps_method method)
3183 case WPS_PIN_DISPLAY:
3185 case WPS_PIN_KEYPAD:
3195 static const char * p2p_go_state_text(enum p2p_go_state go_state)
3210 const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
3211 const u8 *addr, int next)
3213 struct p2p_device *dev;
3216 dev = p2p_get_device(p2p, addr);
3218 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3221 dev = dl_list_first(&dev->list, struct p2p_device, list);
3222 if (&dev->list == &p2p->devices)
3233 int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
3234 char *buf, size_t buflen)
3236 struct p2p_device *dev;
3244 dev = (struct p2p_device *) (((u8 *) info) -
3245 offsetof(struct p2p_device, info));
3251 res = os_snprintf(pos, end - pos,
3255 "interface_addr=" MACSTR "\n"
3256 "member_in_go_dev=" MACSTR "\n"
3257 "member_in_go_iface=" MACSTR "\n"
3258 "go_neg_req_sent=%d\n"
3261 "intended_addr=" MACSTR "\n"
3264 "req_config_methods=0x%x\n"
3265 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
3268 "invitation_reqs=%u\n",
3269 (int) (now.sec - dev->last_seen.sec),
3271 p2p_wps_method_text(dev->wps_method),
3272 MAC2STR(dev->interface_addr),
3273 MAC2STR(dev->member_in_go_dev),
3274 MAC2STR(dev->member_in_go_iface),
3275 dev->go_neg_req_sent,
3276 p2p_go_state_text(dev->go_state),
3278 MAC2STR(dev->intended_addr),
3279 dev->country[0] ? dev->country[0] : '_',
3280 dev->country[1] ? dev->country[1] : '_',
3282 dev->req_config_methods,
3283 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
3284 "[PROBE_REQ_ONLY]" : "",
3285 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
3286 dev->flags & P2P_DEV_NOT_YET_READY ?
3287 "[NOT_YET_READY]" : "",
3288 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
3289 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
3291 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
3292 "[PD_PEER_DISPLAY]" : "",
3293 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
3294 "[PD_PEER_KEYPAD]" : "",
3295 dev->flags & P2P_DEV_USER_REJECTED ?
3296 "[USER_REJECTED]" : "",
3297 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
3298 "[PEER_WAITING_RESPONSE]" : "",
3299 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
3300 "[PREFER_PERSISTENT_GROUP]" : "",
3301 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
3302 "[WAIT_GO_NEG_RESPONSE]" : "",
3303 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
3304 "[WAIT_GO_NEG_CONFIRM]" : "",
3305 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
3306 "[GROUP_CLIENT_ONLY]" : "",
3307 dev->flags & P2P_DEV_FORCE_FREQ ?
3308 "[FORCE_FREQ]" : "",
3309 dev->flags & P2P_DEV_PD_FOR_JOIN ?
3310 "[PD_FOR_JOIN]" : "",
3313 dev->invitation_reqs);
3314 if (res < 0 || res >= end - pos)
3318 if (dev->ext_listen_period) {
3319 res = os_snprintf(pos, end - pos,
3320 "ext_listen_period=%u\n"
3321 "ext_listen_interval=%u\n",
3322 dev->ext_listen_period,
3323 dev->ext_listen_interval);
3324 if (res < 0 || res >= end - pos)
3329 if (dev->oper_ssid_len) {
3330 res = os_snprintf(pos, end - pos,
3332 wpa_ssid_txt(dev->oper_ssid,
3333 dev->oper_ssid_len));
3334 if (res < 0 || res >= end - pos)
3343 int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
3345 return p2p_get_device(p2p, addr) != NULL;
3349 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
3352 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3353 "discoverability enabled");
3354 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3356 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3357 "discoverability disabled");
3358 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3363 static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
3364 u32 duration2, u32 interval2)
3367 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
3370 req = wpabuf_alloc(100);
3374 if (duration1 || interval1) {
3375 os_memset(&desc1, 0, sizeof(desc1));
3376 desc1.count_type = 1;
3377 desc1.duration = duration1;
3378 desc1.interval = interval1;
3381 if (duration2 || interval2) {
3382 os_memset(&desc2, 0, sizeof(desc2));
3383 desc2.count_type = 2;
3384 desc2.duration = duration2;
3385 desc2.interval = interval2;
3390 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
3391 len = p2p_buf_add_ie_hdr(req);
3392 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
3393 p2p_buf_update_ie_hdr(req, len);
3399 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
3400 const u8 *own_interface_addr, unsigned int freq,
3401 u32 duration1, u32 interval1, u32 duration2,
3406 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
3407 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
3408 "int1=%u dur2=%u int2=%u",
3409 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
3410 freq, duration1, interval1, duration2, interval2);
3412 req = p2p_build_presence_req(duration1, interval1, duration2,
3417 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3418 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3420 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
3421 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3422 "P2P: Failed to send Action frame");
3430 static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3431 size_t noa_len, u8 dialog_token)
3433 struct wpabuf *resp;
3436 resp = wpabuf_alloc(100 + noa_len);
3440 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3441 len = p2p_buf_add_ie_hdr(resp);
3442 p2p_buf_add_status(resp, status);
3444 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3445 wpabuf_put_le16(resp, noa_len);
3446 wpabuf_put_data(resp, noa, noa_len);
3448 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3449 p2p_buf_update_ie_hdr(resp, len);
3455 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3456 const u8 *sa, const u8 *data, size_t len,
3459 struct p2p_message msg;
3461 struct wpabuf *resp;
3463 struct p2p_group *group = NULL;
3468 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3469 "P2P: Received P2P Action - P2P Presence Request");
3471 for (g = 0; g < p2p->num_groups; g++) {
3472 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3474 group = p2p->groups[g];
3478 if (group == NULL) {
3479 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3480 "P2P: Ignore P2P Presence Request for unknown group "
3481 MACSTR, MAC2STR(da));
3485 if (p2p_parse(data, len, &msg) < 0) {
3486 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3487 "P2P: Failed to parse P2P Presence Request");
3488 status = P2P_SC_FAIL_INVALID_PARAMS;
3493 if (msg.noa == NULL) {
3494 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3495 "P2P: No NoA attribute in P2P Presence Request");
3496 status = P2P_SC_FAIL_INVALID_PARAMS;
3500 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3503 if (p2p->cfg->get_noa)
3504 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3508 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3509 noa_len > 0 ? noa_len : 0,
3512 p2p_parse_free(&msg);
3516 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3517 if (p2p_send_action(p2p, rx_freq, sa, da, da,
3518 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
3519 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3520 "P2P: Failed to send Action frame");
3526 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3527 const u8 *sa, const u8 *data, size_t len)
3529 struct p2p_message msg;
3531 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3532 "P2P: Received P2P Action - P2P Presence Response");
3534 if (p2p_parse(data, len, &msg) < 0) {
3535 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3536 "P2P: Failed to parse P2P Presence Response");
3540 if (msg.status == NULL || msg.noa == NULL) {
3541 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3542 "P2P: No Status or NoA attribute in P2P Presence "
3544 p2p_parse_free(&msg);
3549 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3550 "P2P: P2P Presence Request was rejected: status %u",
3552 p2p_parse_free(&msg);
3556 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3557 "P2P: P2P Presence Request was accepted");
3558 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
3559 msg.noa, msg.noa_len);
3560 /* TODO: process NoA */
3561 p2p_parse_free(&msg);
3565 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3567 struct p2p_data *p2p = eloop_ctx;
3569 if (p2p->ext_listen_interval) {
3570 /* Schedule next extended listen timeout */
3571 eloop_register_timeout(p2p->ext_listen_interval_sec,
3572 p2p->ext_listen_interval_usec,
3573 p2p_ext_listen_timeout, p2p, NULL);
3576 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
3578 * This should not really happen, but it looks like the Listen
3579 * command may fail is something else (e.g., a scan) was
3580 * running at an inconvenient time. As a workaround, allow new
3581 * Extended Listen operation to be started.
3583 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
3584 "Extended Listen operation had not been completed - "
3586 p2p->ext_listen_only = 0;
3587 p2p_set_state(p2p, P2P_IDLE);
3590 if (p2p->state != P2P_IDLE) {
3591 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
3592 "Listen timeout in active state (%s)",
3593 p2p_state_txt(p2p->state));
3597 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
3598 p2p->ext_listen_only = 1;
3599 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
3600 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
3601 "Listen state for Extended Listen Timing");
3602 p2p->ext_listen_only = 0;
3607 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
3608 unsigned int interval)
3610 if (period > 65535 || interval > 65535 || period > interval ||
3611 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
3612 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3613 "P2P: Invalid Extended Listen Timing request: "
3614 "period=%u interval=%u", period, interval);
3618 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
3620 if (interval == 0) {
3621 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3622 "P2P: Disabling Extended Listen Timing");
3623 p2p->ext_listen_period = 0;
3624 p2p->ext_listen_interval = 0;
3628 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3629 "P2P: Enabling Extended Listen Timing: period %u msec, "
3630 "interval %u msec", period, interval);
3631 p2p->ext_listen_period = period;
3632 p2p->ext_listen_interval = interval;
3633 p2p->ext_listen_interval_sec = interval / 1000;
3634 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
3636 eloop_register_timeout(p2p->ext_listen_interval_sec,
3637 p2p->ext_listen_interval_usec,
3638 p2p_ext_listen_timeout, p2p, NULL);
3644 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3645 const u8 *ie, size_t ie_len)
3647 struct p2p_message msg;
3649 if (bssid == NULL || ie == NULL)
3652 os_memset(&msg, 0, sizeof(msg));
3653 if (p2p_parse_ies(ie, ie_len, &msg))
3655 if (msg.minor_reason_code == NULL)
3658 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3659 "P2P: Deauthentication notification BSSID " MACSTR
3660 " reason_code=%u minor_reason_code=%u",
3661 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3663 p2p_parse_free(&msg);
3667 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3668 const u8 *ie, size_t ie_len)
3670 struct p2p_message msg;
3672 if (bssid == NULL || ie == NULL)
3675 os_memset(&msg, 0, sizeof(msg));
3676 if (p2p_parse_ies(ie, ie_len, &msg))
3678 if (msg.minor_reason_code == NULL)
3681 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3682 "P2P: Disassociation notification BSSID " MACSTR
3683 " reason_code=%u minor_reason_code=%u",
3684 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3686 p2p_parse_free(&msg);
3690 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
3693 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3694 "Device operations enabled");
3695 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
3697 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3698 "Device operations disabled");
3699 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
3704 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
3706 if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
3709 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
3710 "reg_class %u channel %u", reg_class, channel);
3711 p2p->cfg->reg_class = reg_class;
3712 p2p->cfg->channel = channel;
3718 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
3720 wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
3721 if (postfix == NULL) {
3722 p2p->cfg->ssid_postfix_len = 0;
3725 if (len > sizeof(p2p->cfg->ssid_postfix))
3727 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
3728 p2p->cfg->ssid_postfix_len = len;
3733 int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
3736 if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
3740 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
3741 "reg_class %u channel %u", op_reg_class, op_channel);
3742 p2p->cfg->op_reg_class = op_reg_class;
3743 p2p->cfg->op_channel = op_channel;
3744 p2p->cfg->cfg_op_channel = cfg_op_channel;
3749 int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
3750 const struct p2p_channel *pref_chan)
3752 struct p2p_channel *n;
3755 n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
3758 os_memcpy(n, pref_chan,
3759 num_pref_chan * sizeof(struct p2p_channel));
3763 os_free(p2p->cfg->pref_chan);
3764 p2p->cfg->pref_chan = n;
3765 p2p->cfg->num_pref_chan = num_pref_chan;
3771 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
3774 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
3775 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
3777 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
3782 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
3785 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3788 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
3793 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
3795 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
3796 if (is_zero_ether_addr(p2p->peer_filter))
3797 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
3800 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
3801 "filter for " MACSTR, MAC2STR(p2p->peer_filter));
3805 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
3807 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
3808 enabled ? "enabled" : "disabled");
3809 if (p2p->cross_connect == enabled)
3811 p2p->cross_connect = enabled;
3812 /* TODO: may need to tear down any action group where we are GO(?) */
3816 int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
3818 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3821 if (dev->oper_freq <= 0)
3823 return dev->oper_freq;
3827 void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
3829 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
3830 enabled ? "enabled" : "disabled");
3831 p2p->cfg->p2p_intra_bss = enabled;
3835 void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
3837 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
3838 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
3842 int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3843 const u8 *src, const u8 *bssid, const u8 *buf,
3844 size_t len, unsigned int wait_time)
3846 if (p2p->p2p_scan_running) {
3847 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
3848 "frame TX until p2p_scan completes");
3849 if (p2p->after_scan_tx) {
3850 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
3851 "previous pending Action frame TX");
3852 os_free(p2p->after_scan_tx);
3854 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
3856 if (p2p->after_scan_tx == NULL)
3858 p2p->after_scan_tx->freq = freq;
3859 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
3860 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
3861 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
3862 p2p->after_scan_tx->len = len;
3863 p2p->after_scan_tx->wait_time = wait_time;
3864 os_memcpy(p2p->after_scan_tx + 1, buf, len);
3868 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
3869 buf, len, wait_time);
3873 void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
3876 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
3877 " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall);
3878 p2p->best_freq_24 = freq_24;
3879 p2p->best_freq_5 = freq_5;
3880 p2p->best_freq_overall = freq_overall;
3884 const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
3886 if (p2p == NULL || p2p->go_neg_peer == NULL)
3888 return p2p->go_neg_peer->info.p2p_device_addr;
3892 const struct p2p_peer_info *
3893 p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
3895 struct p2p_device *dev;
3898 dev = p2p_get_device(p2p, addr);
3903 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
3909 dev = dl_list_first(&dev->list,
3912 if (&dev->list == &p2p->devices)
3914 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
3917 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3920 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
3921 dev = dl_list_first(&dev->list,
3924 if (&dev->list == &p2p->devices)
3933 int p2p_in_progress(struct p2p_data *p2p)
3937 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;