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 *iface_addr)
156 struct p2p_device *dev = NULL;
158 if (!iface_addr || !p2p)
161 dev = p2p_get_device_interface(p2p, iface_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_NOTHING) {
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
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 * Returns: 0 on success, -1 on failure
557 * If the scan result is for a GO, the clients in the group will also be added
558 * to the peer table. This function can also be used with some other frames
559 * like Provision Discovery Request that contains P2P Capability and P2P Device
562 int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
563 const u8 *ies, size_t ies_len)
565 struct p2p_device *dev;
566 struct p2p_message msg;
567 const u8 *p2p_dev_addr;
570 os_memset(&msg, 0, sizeof(msg));
571 if (p2p_parse_ies(ies, ies_len, &msg)) {
572 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
573 "P2P: Failed to parse P2P IE for a device entry");
574 p2p_parse_free(&msg);
578 if (msg.p2p_device_addr)
579 p2p_dev_addr = msg.p2p_device_addr;
580 else if (msg.device_id)
581 p2p_dev_addr = msg.device_id;
583 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
584 "P2P: Ignore scan data without P2P Device Info or "
586 p2p_parse_free(&msg);
590 if (!is_zero_ether_addr(p2p->peer_filter) &&
591 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
592 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
593 "filter for " MACSTR " due to peer filter",
594 MAC2STR(p2p_dev_addr));
598 dev = p2p_create_device(p2p, p2p_dev_addr);
600 p2p_parse_free(&msg);
603 os_get_time(&dev->last_seen);
604 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
606 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
607 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
609 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
610 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
612 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
613 dev->oper_ssid_len = msg.ssid[1];
616 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
617 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
619 if (*msg.ds_params == 14)
622 ds_freq = 2407 + *msg.ds_params * 5;
623 if (freq != ds_freq) {
624 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
625 "P2P: Update Listen frequency based on DS "
626 "Parameter Set IE: %d -> %d MHz",
632 if (dev->listen_freq && dev->listen_freq != freq) {
633 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
634 "P2P: Update Listen frequency based on scan "
635 "results (" MACSTR " %d -> %d MHz (DS param %d)",
636 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
637 freq, msg.ds_params ? *msg.ds_params : -1);
639 dev->listen_freq = freq;
641 dev->oper_freq = freq;
642 dev->info.level = level;
644 p2p_copy_wps_info(dev, 0, &msg);
646 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
647 wpabuf_free(dev->info.wps_vendor_ext[i]);
648 dev->info.wps_vendor_ext[i] = NULL;
651 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
652 if (msg.wps_vendor_ext[i] == NULL)
654 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
655 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
656 if (dev->info.wps_vendor_ext[i] == NULL)
660 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq, msg.group_info,
663 p2p_parse_free(&msg);
665 if (p2p_pending_sd_req(p2p, dev))
666 dev->flags |= P2P_DEV_SD_SCHEDULE;
668 if (dev->flags & P2P_DEV_REPORTED)
671 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
672 "P2P: Peer found with Listen frequency %d MHz", freq);
673 if (dev->flags & P2P_DEV_USER_REJECTED) {
674 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
675 "P2P: Do not report rejected device");
679 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
680 !(dev->flags & P2P_DEV_REPORTED_ONCE));
681 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
687 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
691 if (p2p->go_neg_peer == dev) {
693 * If GO Negotiation is in progress, report that it has failed.
695 p2p_go_neg_failed(p2p, dev, -1);
696 p2p->go_neg_peer = NULL;
698 if (p2p->invite_peer == dev)
699 p2p->invite_peer = NULL;
700 if (p2p->sd_peer == dev)
702 if (p2p->pending_client_disc_go == dev)
703 p2p->pending_client_disc_go = NULL;
705 /* dev_lost() device, but only if it was previously dev_found() */
706 if (dev->flags & P2P_DEV_REPORTED_ONCE)
707 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
708 dev->info.p2p_device_addr);
710 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
711 wpabuf_free(dev->info.wps_vendor_ext[i]);
712 dev->info.wps_vendor_ext[i] = NULL;
719 static int p2p_get_next_prog_freq(struct p2p_data *p2p)
721 struct p2p_channels *c;
722 struct p2p_reg_class *cla;
729 c = &p2p->cfg->channels;
730 for (cl = 0; cl < c->reg_classes; cl++) {
731 cla = &c->reg_class[cl];
732 if (cla->reg_class != p2p->last_prog_scan_class)
734 for (ch = 0; ch < cla->channels; ch++) {
735 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
745 /* Start from beginning */
746 reg_class = c->reg_class[0].reg_class;
747 channel = c->reg_class[0].channel[0];
749 /* Pick the next channel */
751 if (ch == cla->channels) {
753 if (cl == c->reg_classes)
757 reg_class = c->reg_class[cl].reg_class;
758 channel = c->reg_class[cl].channel[ch];
761 freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
762 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
763 "channel: reg_class %u channel %u -> %d MHz",
764 reg_class, channel, freq);
765 p2p->last_prog_scan_class = reg_class;
766 p2p->last_prog_scan_chan = channel;
768 if (freq == 2412 || freq == 2437 || freq == 2462)
769 return 0; /* No need to add social channels */
774 static void p2p_search(struct p2p_data *p2p)
777 enum p2p_scan_type type;
779 if (p2p->drv_in_listen) {
780 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
781 "in Listen state - wait for it to end before "
785 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
787 if (p2p->go_neg_peer) {
789 * Only scan the known listen frequency of the peer
790 * during GO Negotiation start.
792 freq = p2p->go_neg_peer->listen_freq;
794 freq = p2p->go_neg_peer->oper_freq;
795 type = P2P_SCAN_SPECIFIC;
796 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
797 "for freq %u (GO Neg)", freq);
798 } else if (p2p->invite_peer) {
800 * Only scan the known listen frequency of the peer
801 * during Invite start.
803 freq = p2p->invite_peer->listen_freq;
805 freq = p2p->invite_peer->oper_freq;
806 type = P2P_SCAN_SPECIFIC;
807 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
808 "for freq %u (Invite)", freq);
809 } else if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
810 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
811 type = P2P_SCAN_SOCIAL_PLUS_ONE;
812 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
813 "(+ freq %u)", freq);
815 type = P2P_SCAN_SOCIAL;
816 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
819 if (p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
820 p2p->num_req_dev_types, p2p->req_dev_types,
822 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
823 "P2P: Scan request failed");
824 p2p_continue_find(p2p);
826 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
827 p2p->p2p_scan_running = 1;
828 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
829 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
835 static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
837 struct p2p_data *p2p = eloop_ctx;
838 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
843 static int p2p_run_after_scan(struct p2p_data *p2p)
845 struct p2p_device *dev;
846 enum p2p_after_scan op;
848 if (p2p->after_scan_tx) {
849 /* TODO: schedule p2p_run_after_scan to be called from TX
850 * status callback(?) */
851 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
852 "Action frame at p2p_scan completion");
853 p2p->cfg->send_action(p2p->cfg->cb_ctx,
854 p2p->after_scan_tx->freq,
855 p2p->after_scan_tx->dst,
856 p2p->after_scan_tx->src,
857 p2p->after_scan_tx->bssid,
858 (u8 *) (p2p->after_scan_tx + 1),
859 p2p->after_scan_tx->len,
860 p2p->after_scan_tx->wait_time);
861 os_free(p2p->after_scan_tx);
862 p2p->after_scan_tx = NULL;
866 op = p2p->start_after_scan;
867 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
869 case P2P_AFTER_SCAN_NOTHING:
871 case P2P_AFTER_SCAN_LISTEN:
872 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
873 "requested Listen state");
874 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
875 p2p->pending_listen_usec / 1000);
877 case P2P_AFTER_SCAN_CONNECT:
878 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
879 "requested connect with " MACSTR,
880 MAC2STR(p2p->after_scan_peer));
881 dev = p2p_get_device(p2p, p2p->after_scan_peer);
883 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
887 p2p_connect_send(p2p, dev);
895 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
897 struct p2p_data *p2p = eloop_ctx;
899 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
900 "(running=%d)", p2p->p2p_scan_running);
901 running = p2p->p2p_scan_running;
902 /* Make sure we recover from missed scan results callback */
903 p2p->p2p_scan_running = 0;
906 p2p_run_after_scan(p2p);
910 static void p2p_free_req_dev_types(struct p2p_data *p2p)
912 p2p->num_req_dev_types = 0;
913 os_free(p2p->req_dev_types);
914 p2p->req_dev_types = NULL;
918 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
919 enum p2p_discovery_type type,
920 unsigned int num_req_dev_types, const u8 *req_dev_types,
925 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
927 if (p2p->p2p_scan_running) {
928 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
932 p2p_free_req_dev_types(p2p);
933 if (req_dev_types && num_req_dev_types) {
934 p2p->req_dev_types = os_malloc(num_req_dev_types *
936 if (p2p->req_dev_types == NULL)
938 os_memcpy(p2p->req_dev_types, req_dev_types,
939 num_req_dev_types * WPS_DEV_TYPE_LEN);
940 p2p->num_req_dev_types = num_req_dev_types;
944 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
945 p2p->find_dev_id = p2p->find_dev_id_buf;
947 p2p->find_dev_id = NULL;
949 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
950 p2p_clear_timeout(p2p);
951 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
952 p2p->find_type = type;
953 p2p_device_clear_reported(p2p);
954 p2p_set_state(p2p, P2P_SEARCH);
955 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
956 p2p->last_p2p_find_timeout = timeout;
958 eloop_register_timeout(timeout, 0, p2p_find_timeout,
961 case P2P_FIND_START_WITH_FULL:
962 case P2P_FIND_PROGRESSIVE:
963 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
964 p2p->num_req_dev_types,
965 p2p->req_dev_types, dev_id);
967 case P2P_FIND_ONLY_SOCIAL:
968 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
969 p2p->num_req_dev_types,
970 p2p->req_dev_types, dev_id);
977 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
978 p2p->p2p_scan_running = 1;
979 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
980 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
982 } else if (res == 1) {
983 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
984 "p2p_scan at this point - will try again after "
985 "previous scan completes");
987 p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
988 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
990 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
992 p2p_set_state(p2p, P2P_IDLE);
993 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1000 int p2p_other_scan_completed(struct p2p_data *p2p)
1002 if (p2p->state != P2P_SEARCH_WHEN_READY)
1004 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find "
1005 "now that previous scan was completed");
1006 if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
1007 p2p->num_req_dev_types, p2p->req_dev_types,
1008 p2p->find_dev_id) < 0)
1014 void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1016 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
1017 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1018 p2p_clear_timeout(p2p);
1019 if (p2p->state == P2P_SEARCH)
1020 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1021 p2p_set_state(p2p, P2P_IDLE);
1022 p2p_free_req_dev_types(p2p);
1023 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1024 p2p->go_neg_peer = NULL;
1025 p2p->sd_peer = NULL;
1026 p2p->invite_peer = NULL;
1027 p2p_stop_listen_for_freq(p2p, freq);
1031 void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1033 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
1034 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
1035 "since we are on correct channel for response");
1038 if (p2p->in_listen) {
1040 p2p_clear_timeout(p2p);
1042 if (p2p->drv_in_listen) {
1044 * The driver may not deliver callback to p2p_listen_end()
1045 * when the operation gets canceled, so clear the internal
1046 * variable that is tracking driver state.
1048 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear "
1049 "drv_in_listen (%d)", p2p->drv_in_listen);
1050 p2p->drv_in_listen = 0;
1052 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1056 void p2p_stop_find(struct p2p_data *p2p)
1058 p2p_stop_find_for_freq(p2p, 0);
1062 static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
1065 u8 op_reg_class, op_channel;
1066 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
1067 &op_reg_class, &op_channel) < 0) {
1068 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1069 "P2P: Unsupported frequency %u MHz",
1073 if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
1075 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1076 "P2P: Frequency %u MHz (oper_class %u "
1077 "channel %u) not allowed for P2P",
1078 force_freq, op_reg_class, op_channel);
1081 p2p->op_reg_class = op_reg_class;
1082 p2p->op_channel = op_channel;
1083 p2p->channels.reg_classes = 1;
1084 p2p->channels.reg_class[0].channels = 1;
1085 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1086 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
1088 u8 op_reg_class, op_channel;
1090 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1091 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
1092 p2p_freq_to_channel(p2p->cfg->country,
1093 p2p->best_freq_overall,
1094 &op_reg_class, &op_channel) == 0) {
1095 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1096 "P2P: Select best overall channel as "
1097 "operating channel preference");
1098 p2p->op_reg_class = op_reg_class;
1099 p2p->op_channel = op_channel;
1100 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1101 p2p_supported_freq(p2p, p2p->best_freq_5) &&
1102 p2p_freq_to_channel(p2p->cfg->country,
1104 &op_reg_class, &op_channel) ==
1106 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1107 "P2P: Select best 5 GHz 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 &&
1112 p2p->best_freq_24 > 0 &&
1113 p2p_supported_freq(p2p, p2p->best_freq_24) &&
1114 p2p_freq_to_channel(p2p->cfg->country,
1116 &op_reg_class, &op_channel) ==
1118 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1119 "P2P: Select best 2.4 GHz channel as "
1120 "operating channel preference");
1121 p2p->op_reg_class = op_reg_class;
1122 p2p->op_channel = op_channel;
1124 p2p->op_reg_class = p2p->cfg->op_reg_class;
1125 p2p->op_channel = p2p->cfg->op_channel;
1128 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1129 sizeof(struct p2p_channels));
1131 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1132 "P2P: Own preference for operation channel: "
1133 "Operating Class %u Channel %u%s",
1134 p2p->op_reg_class, p2p->op_channel,
1135 force_freq ? " (forced)" : "");
1141 static void p2p_set_dev_persistent(struct p2p_device *dev,
1142 int persistent_group)
1144 switch (persistent_group) {
1146 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1147 P2P_DEV_PREFER_PERSISTENT_RECONN);
1150 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1151 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1154 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1155 P2P_DEV_PREFER_PERSISTENT_RECONN;
1161 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1162 enum p2p_wps_method wps_method,
1163 int go_intent, const u8 *own_interface_addr,
1164 unsigned int force_freq, int persistent_group)
1166 struct p2p_device *dev;
1168 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1169 "P2P: Request to start group negotiation - peer=" MACSTR
1170 " GO Intent=%d Intended Interface Address=" MACSTR
1171 " wps_method=%d persistent_group=%d",
1172 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1173 wps_method, persistent_group);
1175 if (p2p_prepare_channel(p2p, force_freq) < 0)
1179 dev = p2p_get_device(p2p, peer_addr);
1180 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1181 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1182 "P2P: Cannot connect to unknown P2P Device " MACSTR,
1183 MAC2STR(peer_addr));
1187 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1188 if (!(dev->info.dev_capab &
1189 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1190 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1191 "P2P: Cannot connect to P2P Device " MACSTR
1192 " that is in a group and is not discoverable",
1193 MAC2STR(peer_addr));
1196 if (dev->oper_freq <= 0) {
1197 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1198 "P2P: Cannot connect to P2P Device " MACSTR
1199 " with incomplete information",
1200 MAC2STR(peer_addr));
1205 * First, try to connect directly. If the peer does not
1206 * acknowledge frames, assume it is sleeping and use device
1207 * discoverability via the GO at that point.
1211 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1212 dev->flags &= ~P2P_DEV_USER_REJECTED;
1213 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1214 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1215 dev->connect_reqs = 0;
1216 dev->go_neg_req_sent = 0;
1217 dev->go_state = UNKNOWN_GO;
1218 p2p_set_dev_persistent(dev, persistent_group);
1219 p2p->go_intent = go_intent;
1220 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1222 if (p2p->state != P2P_IDLE)
1225 if (p2p->after_scan_tx) {
1227 * We need to drop the pending frame to avoid issues with the
1228 * new GO Negotiation, e.g., when the pending frame was from a
1229 * previous attempt at starting a GO Negotiation.
1231 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
1232 "previous pending Action frame TX that was waiting "
1233 "for p2p_scan completion");
1234 os_free(p2p->after_scan_tx);
1235 p2p->after_scan_tx = NULL;
1238 dev->wps_method = wps_method;
1239 dev->status = P2P_SC_SUCCESS;
1242 dev->flags |= P2P_DEV_FORCE_FREQ;
1244 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1246 if (p2p->p2p_scan_running) {
1247 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1248 "P2P: p2p_scan running - delay connect send");
1249 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1250 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1253 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1255 return p2p_connect_send(p2p, dev);
1259 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1260 enum p2p_wps_method wps_method,
1261 int go_intent, const u8 *own_interface_addr,
1262 unsigned int force_freq, int persistent_group)
1264 struct p2p_device *dev;
1266 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1267 "P2P: Request to authorize group negotiation - peer=" MACSTR
1268 " GO Intent=%d Intended Interface Address=" MACSTR
1269 " wps_method=%d persistent_group=%d",
1270 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1271 wps_method, persistent_group);
1273 if (p2p_prepare_channel(p2p, force_freq) < 0)
1276 dev = p2p_get_device(p2p, peer_addr);
1278 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1279 "P2P: Cannot authorize unknown P2P Device " MACSTR,
1280 MAC2STR(peer_addr));
1284 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1285 dev->flags &= ~P2P_DEV_USER_REJECTED;
1286 dev->go_neg_req_sent = 0;
1287 dev->go_state = UNKNOWN_GO;
1288 p2p_set_dev_persistent(dev, persistent_group);
1289 p2p->go_intent = go_intent;
1290 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1292 dev->wps_method = wps_method;
1293 dev->status = P2P_SC_SUCCESS;
1296 dev->flags |= P2P_DEV_FORCE_FREQ;
1298 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1304 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1305 struct p2p_device *dev, struct p2p_message *msg)
1307 os_get_time(&dev->last_seen);
1309 p2p_copy_wps_info(dev, 0, msg);
1311 if (msg->listen_channel) {
1313 freq = p2p_channel_to_freq((char *) msg->listen_channel,
1314 msg->listen_channel[3],
1315 msg->listen_channel[4]);
1317 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1318 "P2P: Unknown peer Listen channel: "
1319 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1320 msg->listen_channel[0],
1321 msg->listen_channel[1],
1322 msg->listen_channel[2],
1323 msg->listen_channel[3],
1324 msg->listen_channel[4]);
1326 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1327 "peer " MACSTR " Listen channel: %u -> %u MHz",
1328 MAC2STR(dev->info.p2p_device_addr),
1329 dev->listen_freq, freq);
1330 dev->listen_freq = freq;
1334 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1335 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1336 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1337 "P2P: Completed device entry based on data from "
1338 "GO Negotiation Request");
1340 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1341 "P2P: Created device entry based on GO Neg Req: "
1342 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1344 MAC2STR(dev->info.p2p_device_addr),
1345 dev->info.dev_capab, dev->info.group_capab,
1346 dev->info.device_name, dev->listen_freq);
1349 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1351 if (dev->flags & P2P_DEV_USER_REJECTED) {
1352 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1353 "P2P: Do not report rejected device");
1357 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1358 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1359 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1363 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1365 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1366 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1367 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1368 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1369 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1373 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1375 p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len);
1376 p2p_random(params->passphrase, 8);
1381 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1383 struct p2p_go_neg_results res;
1384 int go = peer->go_state == LOCAL_GO;
1385 struct p2p_channels intersection;
1389 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1390 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1391 "GO)", MAC2STR(peer->info.p2p_device_addr),
1392 go ? "local end" : "peer");
1394 os_memset(&res, 0, sizeof(res));
1396 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1397 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1398 res.wps_method = peer->wps_method;
1399 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1400 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1401 res.persistent_group = 2;
1403 res.persistent_group = 1;
1407 /* Setup AP mode for WPS provisioning */
1408 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1411 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1412 res.ssid_len = p2p->ssid_len;
1413 p2p_random(res.passphrase, 8);
1415 res.freq = peer->oper_freq;
1416 if (p2p->ssid_len) {
1417 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1418 res.ssid_len = p2p->ssid_len;
1422 p2p_channels_intersect(&p2p->channels, &peer->channels,
1425 for (i = 0; i < intersection.reg_classes; i++) {
1426 struct p2p_reg_class *c = &intersection.reg_class[i];
1427 if (freqs + 1 == P2P_MAX_CHANNELS)
1429 for (j = 0; j < c->channels; j++) {
1431 if (freqs + 1 == P2P_MAX_CHANNELS)
1433 freq = p2p_channel_to_freq(peer->country, c->reg_class,
1437 res.freq_list[freqs++] = freq;
1441 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1443 p2p_clear_timeout(p2p);
1445 peer->go_neg_req_sent = 0;
1446 peer->wps_method = WPS_NOT_READY;
1448 p2p_set_state(p2p, P2P_PROVISIONING);
1449 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1453 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1454 const u8 *data, size_t len, int rx_freq)
1456 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1457 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1458 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1464 case P2P_GO_NEG_REQ:
1465 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1467 case P2P_GO_NEG_RESP:
1468 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1470 case P2P_GO_NEG_CONF:
1471 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1473 case P2P_INVITATION_REQ:
1474 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1477 case P2P_INVITATION_RESP:
1478 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1480 case P2P_PROV_DISC_REQ:
1481 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1483 case P2P_PROV_DISC_RESP:
1484 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1486 case P2P_DEV_DISC_REQ:
1487 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1489 case P2P_DEV_DISC_RESP:
1490 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1493 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1494 "P2P: Unsupported P2P Public Action frame type %d",
1501 static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1502 const u8 *sa, const u8 *bssid, const u8 *data,
1503 size_t len, int freq)
1509 case WLAN_PA_VENDOR_SPECIFIC:
1514 if (WPA_GET_BE24(data) != OUI_WFA)
1522 if (*data != P2P_OUI_TYPE)
1525 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1527 case WLAN_PA_GAS_INITIAL_REQ:
1528 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1530 case WLAN_PA_GAS_INITIAL_RESP:
1531 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1533 case WLAN_PA_GAS_COMEBACK_REQ:
1534 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1536 case WLAN_PA_GAS_COMEBACK_RESP:
1537 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1543 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1544 const u8 *bssid, u8 category,
1545 const u8 *data, size_t len, int freq)
1547 if (category == WLAN_ACTION_PUBLIC) {
1548 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1552 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1558 if (WPA_GET_BE24(data) != OUI_WFA)
1563 if (*data != P2P_OUI_TYPE)
1568 /* P2P action frame */
1569 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1570 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1571 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1577 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1578 "P2P: Received P2P Action - Notice of Absence");
1581 case P2P_PRESENCE_REQ:
1582 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1584 case P2P_PRESENCE_RESP:
1585 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1587 case P2P_GO_DISC_REQ:
1588 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1591 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1592 "P2P: Received P2P Action - unknown type %u", data[0]);
1598 static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1600 struct p2p_data *p2p = eloop_ctx;
1601 if (p2p->go_neg_peer == NULL)
1603 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1604 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1605 p2p_connect_send(p2p, p2p->go_neg_peer);
1609 static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1611 struct p2p_data *p2p = eloop_ctx;
1612 if (p2p->invite_peer == NULL)
1614 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1615 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1619 static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1620 const u8 *ie, size_t ie_len)
1622 struct p2p_message msg;
1623 struct p2p_device *dev;
1625 os_memset(&msg, 0, sizeof(msg));
1626 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1628 p2p_parse_free(&msg);
1629 return; /* not a P2P probe */
1632 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1633 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1635 /* The Probe Request is not part of P2P Device Discovery. It is
1636 * not known whether the source address of the frame is the P2P
1637 * Device Address or P2P Interface Address. Do not add a new
1638 * peer entry based on this frames.
1640 p2p_parse_free(&msg);
1644 dev = p2p_get_device(p2p, addr);
1646 if (dev->country[0] == 0 && msg.listen_channel)
1647 os_memcpy(dev->country, msg.listen_channel, 3);
1648 os_get_time(&dev->last_seen);
1649 p2p_parse_free(&msg);
1650 return; /* already known */
1653 dev = p2p_create_device(p2p, addr);
1655 p2p_parse_free(&msg);
1659 os_get_time(&dev->last_seen);
1660 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1662 if (msg.listen_channel) {
1663 os_memcpy(dev->country, msg.listen_channel, 3);
1664 dev->listen_freq = p2p_channel_to_freq(dev->country,
1665 msg.listen_channel[3],
1666 msg.listen_channel[4]);
1669 p2p_copy_wps_info(dev, 1, &msg);
1671 p2p_parse_free(&msg);
1673 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1674 "P2P: Created device entry based on Probe Req: " MACSTR
1675 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1676 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1677 dev->info.group_capab, dev->info.device_name,
1682 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1684 struct p2p_message *msg)
1686 struct p2p_device *dev;
1688 dev = p2p_get_device(p2p, addr);
1690 os_get_time(&dev->last_seen);
1691 return dev; /* already known */
1694 dev = p2p_create_device(p2p, addr);
1698 p2p_add_dev_info(p2p, addr, dev, msg);
1704 static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1706 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1708 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1709 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1710 WPA_GET_BE16(&req_dev_type[6]) == 0)
1711 return 1; /* Category match with wildcard OUI/sub-category */
1716 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1717 size_t num_req_dev_type)
1720 for (i = 0; i < num_req_dev_type; i++) {
1721 if (dev_type_match(dev_type, req_dev_type[i]))
1729 * p2p_match_dev_type - Match local device type with requested type
1730 * @p2p: P2P module context from p2p_init()
1731 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1732 * Returns: 1 on match, 0 on mismatch
1734 * This function can be used to match the Requested Device Type attribute in
1735 * WPS IE with the local device types for deciding whether to reply to a Probe
1738 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1740 struct wps_parse_attr attr;
1743 if (wps_parse_msg(wps, &attr))
1744 return 1; /* assume no Requested Device Type attributes */
1746 if (attr.num_req_dev_type == 0)
1747 return 1; /* no Requested Device Type attributes -> match */
1749 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1750 attr.num_req_dev_type))
1751 return 1; /* Own Primary Device Type matches */
1753 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1754 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1756 attr.num_req_dev_type))
1757 return 1; /* Own Secondary Device Type matches */
1759 /* No matching device type found */
1764 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1769 buf = wpabuf_alloc(1000);
1773 p2p_build_wps_ie(p2p, buf, DEV_PW_DEFAULT, 1);
1776 len = p2p_buf_add_ie_hdr(buf);
1777 p2p_buf_add_capability(buf, p2p->dev_capab, 0);
1778 if (p2p->ext_listen_interval)
1779 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1780 p2p->ext_listen_interval);
1781 p2p_buf_add_device_info(buf, p2p, NULL);
1782 p2p_buf_update_ie_hdr(buf, len);
1788 static int is_11b(u8 rate)
1790 return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
1794 static int supp_rates_11b_only(struct ieee802_11_elems *elems)
1796 int num_11b = 0, num_others = 0;
1799 if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
1802 for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
1803 if (is_11b(elems->supp_rates[i]))
1809 for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
1811 if (is_11b(elems->ext_supp_rates[i]))
1817 return num_11b > 0 && num_others == 0;
1821 static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr,
1822 const u8 *dst, const u8 *bssid, const u8 *ie,
1825 struct ieee802_11_elems elems;
1827 struct ieee80211_mgmt *resp;
1828 struct p2p_message msg;
1831 if (!p2p->in_listen || !p2p->drv_in_listen) {
1832 /* not in Listen state - ignore Probe Request */
1836 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
1838 /* Ignore invalid Probe Request frames */
1842 if (elems.p2p == NULL) {
1843 /* not a P2P probe - ignore it */
1847 if (dst && !is_broadcast_ether_addr(dst) &&
1848 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
1849 /* Not sent to the broadcast address or our P2P Device Address
1854 if (bssid && !is_broadcast_ether_addr(bssid)) {
1855 /* Not sent to the Wildcard BSSID */
1859 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
1860 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
1862 /* not using P2P Wildcard SSID - ignore */
1866 if (supp_rates_11b_only(&elems)) {
1867 /* Indicates support for 11b rates only */
1871 os_memset(&msg, 0, sizeof(msg));
1872 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
1873 /* Could not parse P2P attributes */
1877 if (msg.device_id &&
1878 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN != 0)) {
1879 /* Device ID did not match */
1880 p2p_parse_free(&msg);
1884 /* Check Requested Device Type match */
1885 if (msg.wps_attributes &&
1886 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
1887 /* No match with Requested Device Type */
1888 p2p_parse_free(&msg);
1891 p2p_parse_free(&msg);
1893 if (!p2p->cfg->send_probe_resp)
1894 return; /* Response generated elsewhere */
1896 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1897 "P2P: Reply to P2P Probe Request in Listen state");
1900 * We do not really have a specific BSS that this frame is advertising,
1901 * so build a frame that has some information in valid format. This is
1902 * really only used for discovery purposes, not to learn exact BSS
1905 ies = p2p_build_probe_resp_ies(p2p);
1909 buf = wpabuf_alloc(200 + wpabuf_len(ies));
1916 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
1918 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
1919 (WLAN_FC_STYPE_PROBE_RESP << 4));
1920 os_memcpy(resp->da, addr, ETH_ALEN);
1921 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
1922 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
1923 resp->u.probe_resp.beacon_int = host_to_le16(100);
1924 /* hardware or low-level driver will setup seq_ctrl and timestamp */
1925 resp->u.probe_resp.capab_info =
1926 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
1927 WLAN_CAPABILITY_PRIVACY |
1928 WLAN_CAPABILITY_SHORT_SLOT_TIME);
1930 wpabuf_put_u8(buf, WLAN_EID_SSID);
1931 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
1932 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1934 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
1935 wpabuf_put_u8(buf, 8);
1936 wpabuf_put_u8(buf, (60 / 5) | 0x80);
1937 wpabuf_put_u8(buf, 90 / 5);
1938 wpabuf_put_u8(buf, (120 / 5) | 0x80);
1939 wpabuf_put_u8(buf, 180 / 5);
1940 wpabuf_put_u8(buf, (240 / 5) | 0x80);
1941 wpabuf_put_u8(buf, 360 / 5);
1942 wpabuf_put_u8(buf, 480 / 5);
1943 wpabuf_put_u8(buf, 540 / 5);
1945 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
1946 wpabuf_put_u8(buf, 1);
1947 wpabuf_put_u8(buf, p2p->cfg->channel);
1949 wpabuf_put_buf(buf, ies);
1952 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
1958 int p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
1959 const u8 *bssid, const u8 *ie, size_t ie_len)
1961 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
1963 p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
1965 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
1967 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
1969 /* Received a Probe Request from GO Negotiation peer */
1970 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1971 "P2P: Found GO Negotiation peer - try to start GO "
1972 "negotiation from timeout");
1973 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
1977 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
1979 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
1981 /* Received a Probe Request from Invite peer */
1982 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1983 "P2P: Found Invite peer - try to start Invite from "
1985 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
1993 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
1994 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2003 return 0; /* WLAN AP is not a P2P manager */
2006 * (Re)Association Request - P2P IE
2007 * P2P Capability attribute (shall be present)
2008 * P2P Interface attribute (present if concurrent device and
2009 * P2P Management is enabled)
2011 tmp = wpabuf_alloc(200);
2015 lpos = p2p_buf_add_ie_hdr(tmp);
2017 if (p2p->num_groups > 0) {
2018 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2019 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2020 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2022 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2024 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2025 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2026 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2027 p2p_buf_add_p2p_interface(tmp, p2p);
2028 p2p_buf_update_ie_hdr(tmp, lpos);
2030 tmplen = wpabuf_len(tmp);
2034 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2043 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2044 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2048 struct p2p_device *peer;
2053 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2056 * (Re)Association Request - P2P IE
2057 * P2P Capability attribute (shall be present)
2058 * Extended Listen Timing (may be present)
2059 * P2P Device Info attribute (shall be present)
2061 tmp = wpabuf_alloc(200);
2065 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2067 lpos = p2p_buf_add_ie_hdr(tmp);
2068 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2069 if (p2p->ext_listen_interval)
2070 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2071 p2p->ext_listen_interval);
2072 p2p_buf_add_device_info(tmp, p2p, peer);
2073 p2p_buf_update_ie_hdr(tmp, lpos);
2075 tmplen = wpabuf_len(tmp);
2079 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2088 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2090 struct wpabuf *p2p_ie;
2093 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2097 ret = p2p_attr_text(p2p_ie, buf, end);
2098 wpabuf_free(p2p_ie);
2103 int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2105 struct wpabuf *p2p_ie;
2106 struct p2p_message msg;
2108 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2109 P2P_IE_VENDOR_TYPE);
2112 os_memset(&msg, 0, sizeof(msg));
2113 if (p2p_parse_p2p_ie(p2p_ie, &msg)) {
2114 wpabuf_free(p2p_ie);
2118 if (msg.p2p_device_addr == NULL) {
2119 wpabuf_free(p2p_ie);
2123 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2124 wpabuf_free(p2p_ie);
2129 static void p2p_clear_go_neg(struct p2p_data *p2p)
2131 p2p->go_neg_peer = NULL;
2132 p2p_clear_timeout(p2p);
2133 p2p_set_state(p2p, P2P_IDLE);
2137 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2139 if (p2p->go_neg_peer == NULL) {
2140 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2141 "P2P: No pending Group Formation - "
2142 "ignore WPS registration success notification");
2143 return; /* No pending Group Formation */
2146 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2148 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2149 "P2P: Ignore WPS registration success notification "
2150 "for " MACSTR " (GO Negotiation peer " MACSTR ")",
2152 MAC2STR(p2p->go_neg_peer->intended_addr));
2153 return; /* Ignore unexpected peer address */
2156 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2157 "P2P: Group Formation completed successfully with " MACSTR,
2160 p2p_clear_go_neg(p2p);
2164 void p2p_group_formation_failed(struct p2p_data *p2p)
2166 if (p2p->go_neg_peer == NULL) {
2167 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2168 "P2P: No pending Group Formation - "
2169 "ignore group formation failure notification");
2170 return; /* No pending Group Formation */
2173 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2174 "P2P: Group Formation failed with " MACSTR,
2175 MAC2STR(p2p->go_neg_peer->intended_addr));
2177 p2p_clear_go_neg(p2p);
2181 struct p2p_data * p2p_init(const struct p2p_config *cfg)
2183 struct p2p_data *p2p;
2185 if (cfg->max_peers < 1)
2188 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2191 p2p->cfg = (struct p2p_config *) (p2p + 1);
2192 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2194 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2195 if (cfg->manufacturer)
2196 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2197 if (cfg->model_name)
2198 p2p->cfg->model_name = os_strdup(cfg->model_name);
2199 if (cfg->model_number)
2200 p2p->cfg->model_number = os_strdup(cfg->model_number);
2201 if (cfg->serial_number)
2202 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
2204 p2p->min_disc_int = 1;
2205 p2p->max_disc_int = 3;
2207 os_get_random(&p2p->next_tie_breaker, 1);
2208 p2p->next_tie_breaker &= 0x01;
2209 if (cfg->sd_request)
2210 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2211 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2212 if (cfg->concurrent_operations)
2213 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2214 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2216 dl_list_init(&p2p->devices);
2218 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
2219 p2p_expiration_timeout, p2p, NULL);
2225 void p2p_deinit(struct p2p_data *p2p)
2227 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
2228 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2229 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2231 p2p_free_req_dev_types(p2p);
2232 os_free(p2p->cfg->dev_name);
2233 os_free(p2p->cfg->manufacturer);
2234 os_free(p2p->cfg->model_name);
2235 os_free(p2p->cfg->model_number);
2236 os_free(p2p->cfg->serial_number);
2237 os_free(p2p->groups);
2238 wpabuf_free(p2p->sd_resp);
2239 os_free(p2p->after_scan_tx);
2240 p2p_remove_wps_vendor_extensions(p2p);
2245 void p2p_flush(struct p2p_data *p2p)
2247 struct p2p_device *dev, *prev;
2249 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2251 dl_list_del(&dev->list);
2252 p2p_device_free(p2p, dev);
2254 p2p_free_sd_queries(p2p);
2255 os_free(p2p->after_scan_tx);
2256 p2p->after_scan_tx = NULL;
2260 int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2262 struct p2p_device *dev;
2264 dev = p2p_get_device(p2p, addr);
2268 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
2271 if (p2p->go_neg_peer == dev)
2272 p2p->go_neg_peer = NULL;
2274 dev->wps_method = WPS_NOT_READY;
2275 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2276 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2278 /* Check if after_scan_tx is for this peer. If so free it */
2279 if (p2p->after_scan_tx &&
2280 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2281 os_free(p2p->after_scan_tx);
2282 p2p->after_scan_tx = NULL;
2289 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2291 os_free(p2p->cfg->dev_name);
2293 p2p->cfg->dev_name = os_strdup(dev_name);
2294 if (p2p->cfg->dev_name == NULL)
2297 p2p->cfg->dev_name = NULL;
2302 int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2304 os_free(p2p->cfg->manufacturer);
2305 p2p->cfg->manufacturer = NULL;
2307 p2p->cfg->manufacturer = os_strdup(manufacturer);
2308 if (p2p->cfg->manufacturer == NULL)
2316 int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2318 os_free(p2p->cfg->model_name);
2319 p2p->cfg->model_name = NULL;
2321 p2p->cfg->model_name = os_strdup(model_name);
2322 if (p2p->cfg->model_name == NULL)
2330 int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2332 os_free(p2p->cfg->model_number);
2333 p2p->cfg->model_number = NULL;
2335 p2p->cfg->model_number = os_strdup(model_number);
2336 if (p2p->cfg->model_number == NULL)
2344 int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2346 os_free(p2p->cfg->serial_number);
2347 p2p->cfg->serial_number = NULL;
2348 if (serial_number) {
2349 p2p->cfg->serial_number = os_strdup(serial_number);
2350 if (p2p->cfg->serial_number == NULL)
2358 void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2360 p2p->cfg->config_methods = config_methods;
2364 void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2366 os_memcpy(p2p->cfg->uuid, uuid, 16);
2370 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2372 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2377 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2378 size_t num_dev_types)
2380 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2381 num_dev_types = P2P_SEC_DEVICE_TYPES;
2382 p2p->cfg->num_sec_dev_types = num_dev_types;
2383 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2388 void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2392 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2393 wpabuf_free(p2p->wps_vendor_ext[i]);
2394 p2p->wps_vendor_ext[i] = NULL;
2399 int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2400 const struct wpabuf *vendor_ext)
2404 if (vendor_ext == NULL)
2407 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2408 if (p2p->wps_vendor_ext[i] == NULL)
2411 if (i >= P2P_MAX_WPS_VENDOR_EXT)
2414 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2415 if (p2p->wps_vendor_ext[i] == NULL)
2422 int p2p_set_country(struct p2p_data *p2p, const char *country)
2424 os_memcpy(p2p->cfg->country, country, 3);
2429 void p2p_continue_find(struct p2p_data *p2p)
2431 struct p2p_device *dev;
2432 p2p_set_state(p2p, P2P_SEARCH);
2433 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2434 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2435 if (p2p_start_sd(p2p, dev) == 0)
2439 } else if (dev->req_config_methods &&
2440 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2441 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2442 "pending Provision Discovery Request to "
2443 MACSTR " (config methods 0x%x)",
2444 MAC2STR(dev->info.p2p_device_addr),
2445 dev->req_config_methods);
2446 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
2451 p2p_listen_in_find(p2p);
2455 static void p2p_sd_cb(struct p2p_data *p2p, int success)
2457 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2458 "P2P: Service Discovery Query TX callback: success=%d",
2460 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2464 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2465 p2p->sd_peer = NULL;
2467 p2p_continue_find(p2p);
2471 if (p2p->sd_peer == NULL) {
2472 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2473 "P2P: No SD peer entry known");
2474 p2p_continue_find(p2p);
2478 /* Wait for response from the peer */
2479 p2p_set_state(p2p, P2P_SD_DURING_FIND);
2480 p2p_set_timeout(p2p, 0, 200000);
2485 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2486 * @p2p: P2P module context from p2p_init()
2488 static void p2p_retry_pd(struct p2p_data *p2p)
2490 struct p2p_device *dev;
2492 if (p2p->state != P2P_IDLE)
2496 * Retry the prov disc req attempt only for the peer that the user had
2497 * requested for and provided a join has not been initiated on it
2501 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2502 if (os_memcmp(p2p->pending_pd_devaddr,
2503 dev->info.p2p_device_addr, ETH_ALEN) != 0)
2505 if (!dev->req_config_methods)
2507 if (dev->flags & P2P_DEV_PD_FOR_JOIN)
2510 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2511 "pending Provision Discovery Request to "
2512 MACSTR " (config methods 0x%x)",
2513 MAC2STR(dev->info.p2p_device_addr),
2514 dev->req_config_methods);
2515 p2p_send_prov_disc_req(p2p, dev, 0, 0);
2521 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2523 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2524 "P2P: Provision Discovery Request TX callback: success=%d",
2528 * Postpone resetting the pending action state till after we actually
2529 * time out. This allows us to take some action like notifying any
2530 * interested parties about no response to the request.
2532 * When the timer (below) goes off we check in IDLE, SEARCH, or
2533 * LISTEN_ONLY state, which are the only allowed states to issue a PD
2534 * requests in, if this was still pending and then raise notification.
2538 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2540 if (p2p->state != P2P_IDLE)
2541 p2p_continue_find(p2p);
2542 else if (p2p->user_initiated_pd) {
2543 p2p->pending_action_state = P2P_PENDING_PD;
2544 p2p_set_timeout(p2p, 0, 300000);
2550 * This postponing, of resetting pending_action_state, needs to be
2551 * done only for user initiated PD requests and not internal ones.
2553 if (p2p->user_initiated_pd)
2554 p2p->pending_action_state = P2P_PENDING_PD;
2556 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2558 /* Wait for response from the peer */
2559 if (p2p->state == P2P_SEARCH)
2560 p2p_set_state(p2p, P2P_PD_DURING_FIND);
2561 p2p_set_timeout(p2p, 0, 200000);
2565 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
2566 int level, const u8 *ies, size_t ies_len)
2568 p2p_add_device(p2p, bssid, freq, level, ies, ies_len);
2570 if (p2p->go_neg_peer && p2p->state == P2P_SEARCH &&
2571 os_memcmp(p2p->go_neg_peer->info.p2p_device_addr, bssid, ETH_ALEN)
2573 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2574 "P2P: Found GO Negotiation peer - try to start GO "
2576 p2p_connect_send(p2p, p2p->go_neg_peer);
2584 void p2p_scan_res_handled(struct p2p_data *p2p)
2586 if (!p2p->p2p_scan_running) {
2587 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
2588 "running, but scan results received");
2590 p2p->p2p_scan_running = 0;
2591 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2593 if (p2p_run_after_scan(p2p))
2595 if (p2p->state == P2P_SEARCH)
2596 p2p_continue_find(p2p);
2600 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
2602 u8 *len = p2p_buf_add_ie_hdr(ies);
2603 p2p_buf_add_capability(ies, p2p->dev_capab, 0);
2605 p2p_buf_add_device_id(ies, dev_id);
2606 if (p2p->cfg->reg_class && p2p->cfg->channel)
2607 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2608 p2p->cfg->reg_class,
2610 if (p2p->ext_listen_interval)
2611 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2612 p2p->ext_listen_interval);
2613 /* TODO: p2p_buf_add_operating_channel() if GO */
2614 p2p_buf_update_ie_hdr(ies, len);
2618 size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
2624 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2626 return p2p_attr_text(p2p_ie, buf, end);
2630 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2632 struct p2p_device *dev = p2p->go_neg_peer;
2634 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2635 "P2P: GO Negotiation Request TX callback: success=%d",
2639 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2640 "P2P: No pending GO Negotiation");
2645 dev->go_neg_req_sent++;
2646 if (dev->flags & P2P_DEV_USER_REJECTED) {
2647 p2p_set_state(p2p, P2P_IDLE);
2653 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2654 !is_zero_ether_addr(dev->member_in_go_dev)) {
2655 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2656 "P2P: Peer " MACSTR " did not acknowledge request - "
2657 "try to use device discoverability through its GO",
2658 MAC2STR(dev->info.p2p_device_addr));
2659 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2660 p2p_send_dev_disc_req(p2p, dev);
2665 * Use P2P find, if needed, to find the other device from its listen
2668 p2p_set_state(p2p, P2P_CONNECT);
2669 p2p_set_timeout(p2p, 0, 100000);
2673 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2675 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2676 "P2P: GO Negotiation Response TX callback: success=%d",
2678 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2679 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2680 "P2P: Ignore TX callback event - GO Negotiation is "
2681 "not running anymore");
2684 p2p_set_state(p2p, P2P_CONNECT);
2685 p2p_set_timeout(p2p, 0, 100000);
2689 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2691 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2692 "P2P: GO Negotiation Response (failure) TX callback: "
2693 "success=%d", success);
2694 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2695 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2696 p2p->go_neg_peer->status);
2701 static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
2702 enum p2p_send_action_result result)
2704 struct p2p_device *dev;
2706 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2707 "P2P: GO Negotiation Confirm TX callback: result=%d",
2709 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2710 if (result == P2P_SEND_ACTION_FAILED) {
2711 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2714 if (result == P2P_SEND_ACTION_NO_ACK) {
2716 * It looks like the TX status for GO Negotiation Confirm is
2717 * often showing failure even when the peer has actually
2718 * received the frame. Since the peer may change channels
2719 * immediately after having received the frame, we may not see
2720 * an Ack for retries, so just dropping a single frame may
2721 * trigger this. To allow the group formation to succeed if the
2722 * peer did indeed receive the frame, continue regardless of
2725 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2726 "P2P: Assume GO Negotiation Confirm TX was actually "
2727 "received by the peer even though Ack was not "
2731 dev = p2p->go_neg_peer;
2735 p2p_go_complete(p2p, dev);
2739 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
2740 const u8 *src, const u8 *bssid,
2741 enum p2p_send_action_result result)
2743 enum p2p_pending_action_state state;
2746 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2747 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
2748 " src=" MACSTR " bssid=" MACSTR " result=%d",
2749 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
2750 MAC2STR(bssid), result);
2751 success = result == P2P_SEND_ACTION_SUCCESS;
2752 state = p2p->pending_action_state;
2753 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2755 case P2P_NO_PENDING_ACTION:
2757 case P2P_PENDING_GO_NEG_REQUEST:
2758 p2p_go_neg_req_cb(p2p, success);
2760 case P2P_PENDING_GO_NEG_RESPONSE:
2761 p2p_go_neg_resp_cb(p2p, success);
2763 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
2764 p2p_go_neg_resp_failure_cb(p2p, success);
2766 case P2P_PENDING_GO_NEG_CONFIRM:
2767 p2p_go_neg_conf_cb(p2p, result);
2769 case P2P_PENDING_SD:
2770 p2p_sd_cb(p2p, success);
2772 case P2P_PENDING_PD:
2773 p2p_prov_disc_cb(p2p, success);
2775 case P2P_PENDING_INVITATION_REQUEST:
2776 p2p_invitation_req_cb(p2p, success);
2778 case P2P_PENDING_INVITATION_RESPONSE:
2779 p2p_invitation_resp_cb(p2p, success);
2781 case P2P_PENDING_DEV_DISC_REQUEST:
2782 p2p_dev_disc_req_cb(p2p, success);
2784 case P2P_PENDING_DEV_DISC_RESPONSE:
2785 p2p_dev_disc_resp_cb(p2p, success);
2787 case P2P_PENDING_GO_DISC_REQ:
2788 p2p_go_disc_req_cb(p2p, success);
2794 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
2795 unsigned int duration)
2797 if (freq == p2p->pending_client_disc_freq) {
2798 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2799 "P2P: Client discoverability remain-awake completed");
2800 p2p->pending_client_disc_freq = 0;
2804 if (freq != p2p->pending_listen_freq) {
2805 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2806 "P2P: Unexpected listen callback for freq=%u "
2807 "duration=%u (pending_listen_freq=%u)",
2808 freq, duration, p2p->pending_listen_freq);
2812 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2813 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
2815 p2p->pending_listen_sec, p2p->pending_listen_usec,
2816 p2p->pending_listen_freq);
2818 p2p->drv_in_listen = freq;
2819 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
2821 * Add 20 msec extra wait to avoid race condition with driver
2822 * remain-on-channel end event, i.e., give driver more time to
2823 * complete the operation before our timeout expires.
2825 p2p_set_timeout(p2p, p2p->pending_listen_sec,
2826 p2p->pending_listen_usec + 20000);
2829 p2p->pending_listen_freq = 0;
2833 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
2835 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
2836 "state (freq=%u)", freq);
2837 p2p->drv_in_listen = 0;
2839 return 0; /* Internal timeout will trigger the next step */
2841 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
2842 if (p2p->go_neg_peer->connect_reqs >= 120) {
2843 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2844 "P2P: Timeout on sending GO Negotiation "
2845 "Request without getting response");
2846 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2850 p2p_set_state(p2p, P2P_CONNECT);
2851 p2p_connect_send(p2p, p2p->go_neg_peer);
2853 } else if (p2p->state == P2P_SEARCH) {
2854 if (p2p->p2p_scan_running) {
2856 * Search is already in progress. This can happen if
2857 * an Action frame RX is reported immediately after
2858 * the end of a remain-on-channel operation and the
2859 * response frame to that is sent using an offchannel
2860 * operation while in p2p_find. Avoid an attempt to
2861 * restart a scan here.
2863 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan "
2864 "already in progress - do not try to start a "
2876 static void p2p_timeout_connect(struct p2p_data *p2p)
2878 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2879 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
2880 p2p_listen_in_find(p2p);
2884 static void p2p_timeout_connect_listen(struct p2p_data *p2p)
2886 if (p2p->go_neg_peer) {
2887 if (p2p->drv_in_listen) {
2888 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
2889 "still in Listen state; wait for it to "
2894 if (p2p->go_neg_peer->connect_reqs >= 120) {
2895 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2896 "P2P: Timeout on sending GO Negotiation "
2897 "Request without getting response");
2898 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2902 p2p_set_state(p2p, P2P_CONNECT);
2903 p2p_connect_send(p2p, p2p->go_neg_peer);
2905 p2p_set_state(p2p, P2P_IDLE);
2909 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
2912 * TODO: could remain constantly in Listen state for some time if there
2913 * are no other concurrent uses for the radio. For now, go to listen
2914 * state once per second to give other uses a chance to use the radio.
2916 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
2917 p2p_set_timeout(p2p, 0, 500000);
2921 static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
2923 struct p2p_device *dev = p2p->go_neg_peer;
2926 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2927 "P2P: Unknown GO Neg peer - stop GO Neg wait");
2932 if (dev->wait_count >= 120) {
2933 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2934 "P2P: Timeout on waiting peer to become ready for GO "
2936 p2p_go_neg_failed(p2p, dev, -1);
2940 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2941 "P2P: Go to Listen state while waiting for the peer to become "
2942 "ready for GO Negotiation");
2943 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
2944 p2p_listen_in_find(p2p);
2948 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
2950 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2951 "P2P: Service Discovery Query timeout");
2953 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2954 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2955 p2p->sd_peer = NULL;
2957 p2p_continue_find(p2p);
2961 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
2963 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2964 "P2P: Provision Discovery Request timeout");
2965 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2966 p2p_continue_find(p2p);
2970 static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
2972 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2975 * For user initiated PD requests that we have not gotten any responses
2976 * for while in IDLE state, we retry them a couple of times before
2979 if (!p2p->user_initiated_pd)
2982 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2983 "P2P: User initiated Provision Discovery Request timeout");
2985 if (p2p->pd_retries) {
2989 if (p2p->cfg->prov_disc_fail)
2990 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
2991 p2p->pending_pd_devaddr,
2992 P2P_PROV_DISC_TIMEOUT);
2993 p2p_reset_pending_pd(p2p);
2998 static void p2p_timeout_invite(struct p2p_data *p2p)
3000 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3001 p2p_set_state(p2p, P2P_INVITE_LISTEN);
3002 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
3004 * Better remain on operating channel instead of listen channel
3005 * when running a group.
3007 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
3008 "active GO role - wait on operating channel");
3009 p2p_set_timeout(p2p, 0, 100000);
3012 p2p_listen_in_find(p2p);
3016 static void p2p_timeout_invite_listen(struct p2p_data *p2p)
3018 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
3019 p2p_set_state(p2p, P2P_INVITE);
3020 p2p_invite_send(p2p, p2p->invite_peer,
3021 p2p->invite_go_dev_addr);
3023 if (p2p->invite_peer) {
3024 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3025 "P2P: Invitation Request retry limit reached");
3026 if (p2p->cfg->invitation_result)
3027 p2p->cfg->invitation_result(
3028 p2p->cfg->cb_ctx, -1, NULL);
3030 p2p_set_state(p2p, P2P_IDLE);
3035 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
3037 struct p2p_data *p2p = eloop_ctx;
3039 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
3040 p2p_state_txt(p2p->state));
3044 switch (p2p->state) {
3046 /* Check if we timed out waiting for PD req */
3047 if (p2p->pending_action_state == P2P_PENDING_PD)
3048 p2p_timeout_prov_disc_req(p2p);
3051 /* Check if we timed out waiting for PD req */
3052 if (p2p->pending_action_state == P2P_PENDING_PD)
3053 p2p_timeout_prov_disc_req(p2p);
3057 p2p_timeout_connect(p2p);
3059 case P2P_CONNECT_LISTEN:
3060 p2p_timeout_connect_listen(p2p);
3064 case P2P_LISTEN_ONLY:
3065 /* Check if we timed out waiting for PD req */
3066 if (p2p->pending_action_state == P2P_PENDING_PD)
3067 p2p_timeout_prov_disc_req(p2p);
3069 if (p2p->ext_listen_only) {
3070 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3071 "P2P: Extended Listen Timing - Listen State "
3073 p2p->ext_listen_only = 0;
3074 p2p_set_state(p2p, P2P_IDLE);
3077 case P2P_WAIT_PEER_CONNECT:
3078 p2p_timeout_wait_peer_connect(p2p);
3080 case P2P_WAIT_PEER_IDLE:
3081 p2p_timeout_wait_peer_idle(p2p);
3083 case P2P_SD_DURING_FIND:
3084 p2p_timeout_sd_during_find(p2p);
3086 case P2P_PROVISIONING:
3088 case P2P_PD_DURING_FIND:
3089 p2p_timeout_prov_disc_during_find(p2p);
3092 p2p_timeout_invite(p2p);
3094 case P2P_INVITE_LISTEN:
3095 p2p_timeout_invite_listen(p2p);
3097 case P2P_SEARCH_WHEN_READY:
3103 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
3105 struct p2p_device *dev;
3107 dev = p2p_get_device(p2p, peer_addr);
3108 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
3109 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
3111 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
3112 " unknown", MAC2STR(peer_addr));
3115 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
3116 dev->flags |= P2P_DEV_USER_REJECTED;
3121 const char * p2p_wps_method_text(enum p2p_wps_method method)
3126 case WPS_PIN_DISPLAY:
3128 case WPS_PIN_KEYPAD:
3138 static const char * p2p_go_state_text(enum p2p_go_state go_state)
3153 const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
3154 const u8 *addr, int next)
3156 struct p2p_device *dev;
3159 dev = p2p_get_device(p2p, addr);
3161 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3164 dev = dl_list_first(&dev->list, struct p2p_device, list);
3165 if (&dev->list == &p2p->devices)
3176 int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
3177 char *buf, size_t buflen)
3179 struct p2p_device *dev;
3187 dev = (struct p2p_device *) (((u8 *) info) -
3188 offsetof(struct p2p_device, info));
3194 res = os_snprintf(pos, end - pos,
3198 "interface_addr=" MACSTR "\n"
3199 "member_in_go_dev=" MACSTR "\n"
3200 "member_in_go_iface=" MACSTR "\n"
3201 "go_neg_req_sent=%d\n"
3204 "intended_addr=" MACSTR "\n"
3207 "req_config_methods=0x%x\n"
3208 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
3211 "invitation_reqs=%u\n",
3212 (int) (now.sec - dev->last_seen.sec),
3214 p2p_wps_method_text(dev->wps_method),
3215 MAC2STR(dev->interface_addr),
3216 MAC2STR(dev->member_in_go_dev),
3217 MAC2STR(dev->member_in_go_iface),
3218 dev->go_neg_req_sent,
3219 p2p_go_state_text(dev->go_state),
3221 MAC2STR(dev->intended_addr),
3222 dev->country[0] ? dev->country[0] : '_',
3223 dev->country[1] ? dev->country[1] : '_',
3225 dev->req_config_methods,
3226 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
3227 "[PROBE_REQ_ONLY]" : "",
3228 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
3229 dev->flags & P2P_DEV_NOT_YET_READY ?
3230 "[NOT_YET_READY]" : "",
3231 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
3232 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
3234 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
3235 "[PD_PEER_DISPLAY]" : "",
3236 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
3237 "[PD_PEER_KEYPAD]" : "",
3238 dev->flags & P2P_DEV_USER_REJECTED ?
3239 "[USER_REJECTED]" : "",
3240 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
3241 "[PEER_WAITING_RESPONSE]" : "",
3242 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
3243 "[PREFER_PERSISTENT_GROUP]" : "",
3244 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
3245 "[WAIT_GO_NEG_RESPONSE]" : "",
3246 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
3247 "[WAIT_GO_NEG_CONFIRM]" : "",
3248 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
3249 "[GROUP_CLIENT_ONLY]" : "",
3250 dev->flags & P2P_DEV_FORCE_FREQ ?
3251 "[FORCE_FREQ]" : "",
3252 dev->flags & P2P_DEV_PD_FOR_JOIN ?
3253 "[PD_FOR_JOIN]" : "",
3256 dev->invitation_reqs);
3257 if (res < 0 || res >= end - pos)
3261 if (dev->ext_listen_period) {
3262 res = os_snprintf(pos, end - pos,
3263 "ext_listen_period=%u\n"
3264 "ext_listen_interval=%u\n",
3265 dev->ext_listen_period,
3266 dev->ext_listen_interval);
3267 if (res < 0 || res >= end - pos)
3272 if (dev->oper_ssid_len) {
3273 res = os_snprintf(pos, end - pos,
3275 wpa_ssid_txt(dev->oper_ssid,
3276 dev->oper_ssid_len));
3277 if (res < 0 || res >= end - pos)
3286 int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
3288 return p2p_get_device(p2p, addr) != NULL;
3292 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
3295 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3296 "discoverability enabled");
3297 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3299 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3300 "discoverability disabled");
3301 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3306 static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
3307 u32 duration2, u32 interval2)
3310 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
3313 req = wpabuf_alloc(100);
3317 if (duration1 || interval1) {
3318 os_memset(&desc1, 0, sizeof(desc1));
3319 desc1.count_type = 1;
3320 desc1.duration = duration1;
3321 desc1.interval = interval1;
3324 if (duration2 || interval2) {
3325 os_memset(&desc2, 0, sizeof(desc2));
3326 desc2.count_type = 2;
3327 desc2.duration = duration2;
3328 desc2.interval = interval2;
3333 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
3334 len = p2p_buf_add_ie_hdr(req);
3335 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
3336 p2p_buf_update_ie_hdr(req, len);
3342 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
3343 const u8 *own_interface_addr, unsigned int freq,
3344 u32 duration1, u32 interval1, u32 duration2,
3349 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
3350 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
3351 "int1=%u dur2=%u int2=%u",
3352 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
3353 freq, duration1, interval1, duration2, interval2);
3355 req = p2p_build_presence_req(duration1, interval1, duration2,
3360 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3361 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3363 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
3364 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3365 "P2P: Failed to send Action frame");
3373 static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3374 size_t noa_len, u8 dialog_token)
3376 struct wpabuf *resp;
3379 resp = wpabuf_alloc(100 + noa_len);
3383 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3384 len = p2p_buf_add_ie_hdr(resp);
3385 p2p_buf_add_status(resp, status);
3387 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3388 wpabuf_put_le16(resp, noa_len);
3389 wpabuf_put_data(resp, noa, noa_len);
3391 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3392 p2p_buf_update_ie_hdr(resp, len);
3398 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3399 const u8 *sa, const u8 *data, size_t len,
3402 struct p2p_message msg;
3404 struct wpabuf *resp;
3406 struct p2p_group *group = NULL;
3411 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3412 "P2P: Received P2P Action - P2P Presence Request");
3414 for (g = 0; g < p2p->num_groups; g++) {
3415 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3417 group = p2p->groups[g];
3421 if (group == NULL) {
3422 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3423 "P2P: Ignore P2P Presence Request for unknown group "
3424 MACSTR, MAC2STR(da));
3428 if (p2p_parse(data, len, &msg) < 0) {
3429 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3430 "P2P: Failed to parse P2P Presence Request");
3431 status = P2P_SC_FAIL_INVALID_PARAMS;
3436 if (msg.noa == NULL) {
3437 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3438 "P2P: No NoA attribute in P2P Presence Request");
3439 status = P2P_SC_FAIL_INVALID_PARAMS;
3443 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3446 if (p2p->cfg->get_noa)
3447 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3451 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3452 noa_len > 0 ? noa_len : 0,
3455 p2p_parse_free(&msg);
3459 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3460 if (p2p_send_action(p2p, rx_freq, sa, da, da,
3461 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
3462 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3463 "P2P: Failed to send Action frame");
3469 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3470 const u8 *sa, const u8 *data, size_t len)
3472 struct p2p_message msg;
3474 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3475 "P2P: Received P2P Action - P2P Presence Response");
3477 if (p2p_parse(data, len, &msg) < 0) {
3478 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3479 "P2P: Failed to parse P2P Presence Response");
3483 if (msg.status == NULL || msg.noa == NULL) {
3484 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3485 "P2P: No Status or NoA attribute in P2P Presence "
3487 p2p_parse_free(&msg);
3492 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3493 "P2P: P2P Presence Request was rejected: status %u",
3495 p2p_parse_free(&msg);
3499 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3500 "P2P: P2P Presence Request was accepted");
3501 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
3502 msg.noa, msg.noa_len);
3503 /* TODO: process NoA */
3504 p2p_parse_free(&msg);
3508 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3510 struct p2p_data *p2p = eloop_ctx;
3512 if (p2p->ext_listen_interval) {
3513 /* Schedule next extended listen timeout */
3514 eloop_register_timeout(p2p->ext_listen_interval_sec,
3515 p2p->ext_listen_interval_usec,
3516 p2p_ext_listen_timeout, p2p, NULL);
3519 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
3521 * This should not really happen, but it looks like the Listen
3522 * command may fail is something else (e.g., a scan) was
3523 * running at an inconvenient time. As a workaround, allow new
3524 * Extended Listen operation to be started.
3526 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
3527 "Extended Listen operation had not been completed - "
3529 p2p->ext_listen_only = 0;
3530 p2p_set_state(p2p, P2P_IDLE);
3533 if (p2p->state != P2P_IDLE) {
3534 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
3535 "Listen timeout in active state (%s)",
3536 p2p_state_txt(p2p->state));
3540 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
3541 p2p->ext_listen_only = 1;
3542 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
3543 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
3544 "Listen state for Extended Listen Timing");
3545 p2p->ext_listen_only = 0;
3550 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
3551 unsigned int interval)
3553 if (period > 65535 || interval > 65535 || period > interval ||
3554 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
3555 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3556 "P2P: Invalid Extended Listen Timing request: "
3557 "period=%u interval=%u", period, interval);
3561 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
3563 if (interval == 0) {
3564 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3565 "P2P: Disabling Extended Listen Timing");
3566 p2p->ext_listen_period = 0;
3567 p2p->ext_listen_interval = 0;
3571 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3572 "P2P: Enabling Extended Listen Timing: period %u msec, "
3573 "interval %u msec", period, interval);
3574 p2p->ext_listen_period = period;
3575 p2p->ext_listen_interval = interval;
3576 p2p->ext_listen_interval_sec = interval / 1000;
3577 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
3579 eloop_register_timeout(p2p->ext_listen_interval_sec,
3580 p2p->ext_listen_interval_usec,
3581 p2p_ext_listen_timeout, p2p, NULL);
3587 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3588 const u8 *ie, size_t ie_len)
3590 struct p2p_message msg;
3592 if (bssid == NULL || ie == NULL)
3595 os_memset(&msg, 0, sizeof(msg));
3596 if (p2p_parse_ies(ie, ie_len, &msg))
3598 if (msg.minor_reason_code == NULL)
3601 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3602 "P2P: Deauthentication notification BSSID " MACSTR
3603 " reason_code=%u minor_reason_code=%u",
3604 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3606 p2p_parse_free(&msg);
3610 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3611 const u8 *ie, size_t ie_len)
3613 struct p2p_message msg;
3615 if (bssid == NULL || ie == NULL)
3618 os_memset(&msg, 0, sizeof(msg));
3619 if (p2p_parse_ies(ie, ie_len, &msg))
3621 if (msg.minor_reason_code == NULL)
3624 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3625 "P2P: Disassociation notification BSSID " MACSTR
3626 " reason_code=%u minor_reason_code=%u",
3627 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3629 p2p_parse_free(&msg);
3633 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
3636 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3637 "Device operations enabled");
3638 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
3640 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3641 "Device operations disabled");
3642 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
3647 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
3649 if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
3652 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
3653 "reg_class %u channel %u", reg_class, channel);
3654 p2p->cfg->reg_class = reg_class;
3655 p2p->cfg->channel = channel;
3661 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
3663 wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
3664 if (postfix == NULL) {
3665 p2p->cfg->ssid_postfix_len = 0;
3668 if (len > sizeof(p2p->cfg->ssid_postfix))
3670 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
3671 p2p->cfg->ssid_postfix_len = len;
3676 int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
3679 if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
3683 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
3684 "reg_class %u channel %u", op_reg_class, op_channel);
3685 p2p->cfg->op_reg_class = op_reg_class;
3686 p2p->cfg->op_channel = op_channel;
3687 p2p->cfg->cfg_op_channel = cfg_op_channel;
3692 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
3695 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
3696 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
3698 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
3703 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
3706 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3709 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
3714 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
3716 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
3717 if (is_zero_ether_addr(p2p->peer_filter))
3718 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
3721 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
3722 "filter for " MACSTR, MAC2STR(p2p->peer_filter));
3726 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
3728 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
3729 enabled ? "enabled" : "disabled");
3730 if (p2p->cross_connect == enabled)
3732 p2p->cross_connect = enabled;
3733 /* TODO: may need to tear down any action group where we are GO(?) */
3737 int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
3739 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3742 if (dev->oper_freq <= 0)
3744 return dev->oper_freq;
3748 void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
3750 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
3751 enabled ? "enabled" : "disabled");
3752 p2p->cfg->p2p_intra_bss = enabled;
3756 void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
3758 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
3759 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
3763 int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3764 const u8 *src, const u8 *bssid, const u8 *buf,
3765 size_t len, unsigned int wait_time)
3767 if (p2p->p2p_scan_running) {
3768 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
3769 "frame TX until p2p_scan completes");
3770 if (p2p->after_scan_tx) {
3771 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
3772 "previous pending Action frame TX");
3773 os_free(p2p->after_scan_tx);
3775 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
3777 if (p2p->after_scan_tx == NULL)
3779 p2p->after_scan_tx->freq = freq;
3780 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
3781 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
3782 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
3783 p2p->after_scan_tx->len = len;
3784 p2p->after_scan_tx->wait_time = wait_time;
3785 os_memcpy(p2p->after_scan_tx + 1, buf, len);
3789 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
3790 buf, len, wait_time);
3794 void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
3797 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
3798 " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall);
3799 p2p->best_freq_24 = freq_24;
3800 p2p->best_freq_5 = freq_5;
3801 p2p->best_freq_overall = freq_overall;
3805 const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
3807 if (p2p == NULL || p2p->go_neg_peer == NULL)
3809 return p2p->go_neg_peer->info.p2p_device_addr;
3813 const struct p2p_peer_info *
3814 p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
3816 struct p2p_device *dev;
3819 dev = p2p_get_device(p2p, addr);
3824 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
3830 dev = dl_list_first(&dev->list,
3833 if (&dev->list == &p2p->devices)
3835 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
3838 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3841 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
3842 dev = dl_list_first(&dev->list,
3845 if (&dev->list == &p2p->devices)
3854 int p2p_in_progress(struct p2p_data *p2p)
3858 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;