2 * Wi-Fi Direct - P2P module
3 * Copyright (c) 2009-2010, Atheros Communications
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #include "common/ieee802_11_defs.h"
20 #include "common/ieee802_11_common.h"
21 #include "wps/wps_i.h"
26 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
27 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
28 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
29 const u8 *sa, const u8 *data, size_t len,
31 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
32 const u8 *sa, const u8 *data,
34 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
35 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
39 * p2p_scan recovery timeout
41 * Many drivers are using 30 second timeout on scan results. Allow a bit larger
42 * timeout for this to avoid hitting P2P timeout unnecessarily.
44 #define P2P_SCAN_TIMEOUT 35
47 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
48 * entries will be removed
50 #define P2P_PEER_EXPIRATION_AGE 300
52 #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
54 static void p2p_expire_peers(struct p2p_data *p2p)
56 struct p2p_device *dev, *n;
60 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
61 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
63 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer "
64 "entry " MACSTR, MAC2STR(dev->info.p2p_device_addr));
65 dl_list_del(&dev->list);
66 p2p_device_free(p2p, dev);
71 static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
73 struct p2p_data *p2p = eloop_ctx;
74 p2p_expire_peers(p2p);
75 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
76 p2p_expiration_timeout, p2p, NULL);
80 static const char * p2p_state_txt(int state)
89 case P2P_CONNECT_LISTEN:
90 return "CONNECT_LISTEN";
95 case P2P_WAIT_PEER_CONNECT:
96 return "WAIT_PEER_CONNECT";
97 case P2P_WAIT_PEER_IDLE:
98 return "WAIT_PEER_IDLE";
99 case P2P_SD_DURING_FIND:
100 return "SD_DURING_FIND";
101 case P2P_PROVISIONING:
102 return "PROVISIONING";
103 case P2P_PD_DURING_FIND:
104 return "PD_DURING_FIND";
107 case P2P_INVITE_LISTEN:
108 return "INVITE_LISTEN";
109 case P2P_SEARCH_WHEN_READY:
110 return "SEARCH_WHEN_READY";
117 void p2p_set_state(struct p2p_data *p2p, int new_state)
119 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s",
120 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
121 p2p->state = new_state;
125 void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
127 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
128 "P2P: Set timeout (state=%s): %u.%06u sec",
129 p2p_state_txt(p2p->state), sec, usec);
130 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
131 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
135 void p2p_clear_timeout(struct p2p_data *p2p)
137 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)",
138 p2p_state_txt(p2p->state));
139 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
143 void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
146 struct p2p_go_neg_results res;
147 p2p_clear_timeout(p2p);
148 p2p_set_state(p2p, P2P_IDLE);
149 p2p->go_neg_peer = NULL;
151 os_memset(&res, 0, sizeof(res));
154 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr,
156 os_memcpy(res.peer_interface_addr, peer->intended_addr,
159 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
163 static void p2p_listen_in_find(struct p2p_data *p2p)
169 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
170 "P2P: Starting short listen state (state=%s)",
171 p2p_state_txt(p2p->state));
173 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
176 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
177 "P2P: Unknown regulatory class/channel");
181 os_get_random((u8 *) &r, sizeof(r));
182 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
183 p2p->min_disc_int) * 100;
185 p2p->pending_listen_freq = freq;
186 p2p->pending_listen_sec = 0;
187 p2p->pending_listen_usec = 1024 * tu;
189 ies = p2p_build_probe_resp_ies(p2p);
193 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
195 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
196 "P2P: Failed to start listen mode");
197 p2p->pending_listen_freq = 0;
203 int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
208 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
209 "P2P: Going to listen(only) state");
211 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
214 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
215 "P2P: Unknown regulatory class/channel");
219 p2p->pending_listen_freq = freq;
220 p2p->pending_listen_sec = timeout / 1000;
221 p2p->pending_listen_usec = (timeout % 1000) * 1000;
223 if (p2p->p2p_scan_running) {
224 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
225 "P2P: p2p_scan running - delay start of listen state");
226 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
230 ies = p2p_build_probe_resp_ies(p2p);
234 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
235 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
236 "P2P: Failed to start listen mode");
237 p2p->pending_listen_freq = 0;
243 p2p_set_state(p2p, P2P_LISTEN_ONLY);
249 static void p2p_device_clear_reported(struct p2p_data *p2p)
251 struct p2p_device *dev;
252 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
253 dev->flags &= ~P2P_DEV_REPORTED;
258 * p2p_get_device - Fetch a peer entry
259 * @p2p: P2P module context from p2p_init()
260 * @addr: P2P Device Address of the peer
261 * Returns: Pointer to the device entry or %NULL if not found
263 struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
265 struct p2p_device *dev;
266 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
267 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
275 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
276 * @p2p: P2P module context from p2p_init()
277 * @addr: P2P Interface Address of the peer
278 * Returns: Pointer to the device entry or %NULL if not found
280 struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
283 struct p2p_device *dev;
284 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
285 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
293 * p2p_create_device - Create a peer entry
294 * @p2p: P2P module context from p2p_init()
295 * @addr: P2P Device Address of the peer
296 * Returns: Pointer to the device entry or %NULL on failure
298 * If there is already an entry for the peer, it will be returned instead of
299 * creating a new one.
301 static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
304 struct p2p_device *dev, *oldest = NULL;
307 dev = p2p_get_device(p2p, addr);
311 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
313 if (oldest == NULL ||
314 os_time_before(&dev->last_seen, &oldest->last_seen))
317 if (count + 1 > p2p->cfg->max_peers && oldest) {
318 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
319 "P2P: Remove oldest peer entry to make room for a new "
321 dl_list_del(&oldest->list);
322 p2p_device_free(p2p, oldest);
325 dev = os_zalloc(sizeof(*dev));
328 dl_list_add(&p2p->devices, &dev->list);
329 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
335 static void p2p_copy_client_info(struct p2p_device *dev,
336 struct p2p_client_info *cli)
338 os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
339 dev->info.device_name[cli->dev_name_len] = '\0';
340 dev->info.dev_capab = cli->dev_capab;
341 dev->info.config_methods = cli->config_methods;
342 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
343 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
344 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
345 dev->info.wps_sec_dev_type_list_len);
349 static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
350 const u8 *go_interface_addr, int freq,
351 const u8 *gi, size_t gi_len)
353 struct p2p_group_info info;
355 struct p2p_device *dev;
360 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
364 * Clear old data for this group; if the devices are still in the
365 * group, the information will be restored in the loop following this.
367 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
368 if (os_memcpy(dev->member_in_go_iface, go_interface_addr,
370 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
371 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
375 for (c = 0; c < info.num_clients; c++) {
376 struct p2p_client_info *cli = &info.client[c];
377 dev = p2p_get_device(p2p, cli->p2p_device_addr);
380 * Update information only if we have not received this
381 * directly from the client.
383 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
384 P2P_DEV_PROBE_REQ_ONLY))
385 p2p_copy_client_info(dev, cli);
386 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
387 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
390 dev = p2p_create_device(p2p, cli->p2p_device_addr);
393 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
394 p2p_copy_client_info(dev, cli);
395 dev->oper_freq = freq;
396 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
397 dev->info.p2p_device_addr,
399 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
402 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
404 os_get_time(&dev->last_seen);
405 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
406 os_memcpy(dev->member_in_go_iface, go_interface_addr,
414 static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
415 const struct p2p_message *msg)
417 os_memcpy(dev->info.device_name, msg->device_name,
418 sizeof(dev->info.device_name));
420 if (msg->manufacturer &&
421 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
422 os_memset(dev->info.manufacturer, 0,
423 sizeof(dev->info.manufacturer));
424 os_memcpy(dev->info.manufacturer, msg->manufacturer,
425 msg->manufacturer_len);
428 if (msg->model_name &&
429 msg->model_name_len < sizeof(dev->info.model_name)) {
430 os_memset(dev->info.model_name, 0,
431 sizeof(dev->info.model_name));
432 os_memcpy(dev->info.model_name, msg->model_name,
433 msg->model_name_len);
436 if (msg->model_number &&
437 msg->model_number_len < sizeof(dev->info.model_number)) {
438 os_memset(dev->info.model_number, 0,
439 sizeof(dev->info.model_number));
440 os_memcpy(dev->info.model_number, msg->model_number,
441 msg->model_number_len);
444 if (msg->serial_number &&
445 msg->serial_number_len < sizeof(dev->info.serial_number)) {
446 os_memset(dev->info.serial_number, 0,
447 sizeof(dev->info.serial_number));
448 os_memcpy(dev->info.serial_number, msg->serial_number,
449 msg->serial_number_len);
452 if (msg->pri_dev_type)
453 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
454 sizeof(dev->info.pri_dev_type));
455 else if (msg->wps_pri_dev_type)
456 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
457 sizeof(dev->info.pri_dev_type));
459 if (msg->wps_sec_dev_type_list) {
460 os_memcpy(dev->info.wps_sec_dev_type_list,
461 msg->wps_sec_dev_type_list,
462 msg->wps_sec_dev_type_list_len);
463 dev->info.wps_sec_dev_type_list_len =
464 msg->wps_sec_dev_type_list_len;
467 if (msg->capability) {
468 dev->info.dev_capab = msg->capability[0];
469 dev->info.group_capab = msg->capability[1];
472 if (msg->ext_listen_timing) {
473 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
474 dev->ext_listen_interval =
475 WPA_GET_LE16(msg->ext_listen_timing + 2);
479 dev->info.config_methods = msg->config_methods ?
480 msg->config_methods : msg->wps_config_methods;
486 * p2p_add_device - Add peer entries based on scan results
487 * @p2p: P2P module context from p2p_init()
488 * @addr: Source address of Beacon or Probe Response frame (may be either
489 * P2P Device Address or P2P Interface Address)
490 * @level: Signal level (signal strength of the received frame from the peer)
491 * @freq: Frequency on which the Beacon or Probe Response frame was received
492 * @ies: IEs from the Beacon or Probe Response frame
493 * @ies_len: Length of ies buffer in octets
494 * Returns: 0 on success, -1 on failure
496 * If the scan result is for a GO, the clients in the group will also be added
497 * to the peer table. This function can also be used with some other frames
498 * like Provision Discovery Request that contains P2P Capability and P2P Device
501 int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
502 const u8 *ies, size_t ies_len)
504 struct p2p_device *dev;
505 struct p2p_message msg;
506 const u8 *p2p_dev_addr;
509 os_memset(&msg, 0, sizeof(msg));
510 if (p2p_parse_ies(ies, ies_len, &msg)) {
511 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
512 "P2P: Failed to parse P2P IE for a device entry");
513 p2p_parse_free(&msg);
517 if (msg.p2p_device_addr)
518 p2p_dev_addr = msg.p2p_device_addr;
519 else if (msg.device_id)
520 p2p_dev_addr = msg.device_id;
522 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
523 "P2P: Ignore scan data without P2P Device Info or "
525 p2p_parse_free(&msg);
529 if (!is_zero_ether_addr(p2p->peer_filter) &&
530 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
531 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
532 "filter for " MACSTR " due to peer filter",
533 MAC2STR(p2p_dev_addr));
537 dev = p2p_create_device(p2p, p2p_dev_addr);
539 p2p_parse_free(&msg);
542 os_get_time(&dev->last_seen);
543 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
545 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
546 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
548 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
549 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
551 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
552 dev->oper_ssid_len = msg.ssid[1];
555 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
556 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
558 if (*msg.ds_params == 14)
561 ds_freq = 2407 + *msg.ds_params * 5;
562 if (freq != ds_freq) {
563 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
564 "P2P: Update Listen frequency based on DS "
565 "Parameter Set IE: %d -> %d MHz",
571 if (dev->listen_freq && dev->listen_freq != freq) {
572 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
573 "P2P: Update Listen frequency based on scan "
574 "results (" MACSTR " %d -> %d MHz (DS param %d)",
575 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
576 freq, msg.ds_params ? *msg.ds_params : -1);
578 dev->listen_freq = freq;
580 dev->oper_freq = freq;
581 dev->info.level = level;
583 p2p_copy_wps_info(dev, 0, &msg);
585 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
586 wpabuf_free(dev->info.wps_vendor_ext[i]);
587 dev->info.wps_vendor_ext[i] = NULL;
590 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
591 if (msg.wps_vendor_ext[i] == NULL)
593 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
594 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
595 if (dev->info.wps_vendor_ext[i] == NULL)
599 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq, msg.group_info,
602 p2p_parse_free(&msg);
604 if (p2p_pending_sd_req(p2p, dev))
605 dev->flags |= P2P_DEV_SD_SCHEDULE;
607 if (dev->flags & P2P_DEV_REPORTED)
610 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
611 "P2P: Peer found with Listen frequency %d MHz", freq);
612 if (dev->flags & P2P_DEV_USER_REJECTED) {
613 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
614 "P2P: Do not report rejected device");
618 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
619 !(dev->flags & P2P_DEV_REPORTED_ONCE));
620 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
626 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
630 if (p2p->go_neg_peer == dev)
631 p2p->go_neg_peer = NULL;
632 if (p2p->invite_peer == dev)
633 p2p->invite_peer = NULL;
634 if (p2p->sd_peer == dev)
636 if (p2p->pending_client_disc_go == dev)
637 p2p->pending_client_disc_go = NULL;
639 /* dev_lost() device, but only if it was previously dev_found() */
640 if (dev->flags & P2P_DEV_REPORTED_ONCE)
641 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
642 dev->info.p2p_device_addr);
644 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
645 wpabuf_free(dev->info.wps_vendor_ext[i]);
646 dev->info.wps_vendor_ext[i] = NULL;
653 static int p2p_get_next_prog_freq(struct p2p_data *p2p)
655 struct p2p_channels *c;
656 struct p2p_reg_class *cla;
663 c = &p2p->cfg->channels;
664 for (cl = 0; cl < c->reg_classes; cl++) {
665 cla = &c->reg_class[cl];
666 if (cla->reg_class != p2p->last_prog_scan_class)
668 for (ch = 0; ch < cla->channels; ch++) {
669 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
679 /* Start from beginning */
680 reg_class = c->reg_class[0].reg_class;
681 channel = c->reg_class[0].channel[0];
683 /* Pick the next channel */
685 if (ch == cla->channels) {
687 if (cl == c->reg_classes)
691 reg_class = c->reg_class[cl].reg_class;
692 channel = c->reg_class[cl].channel[ch];
695 freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
696 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
697 "channel: reg_class %u channel %u -> %d MHz",
698 reg_class, channel, freq);
699 p2p->last_prog_scan_class = reg_class;
700 p2p->last_prog_scan_chan = channel;
702 if (freq == 2412 || freq == 2437 || freq == 2462)
703 return 0; /* No need to add social channels */
708 static void p2p_search(struct p2p_data *p2p)
711 enum p2p_scan_type type;
713 if (p2p->drv_in_listen) {
714 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
715 "in Listen state - wait for it to end before "
719 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
721 if (p2p->go_neg_peer) {
723 * Only scan the known listen frequency of the peer
724 * during GO Negotiation start.
726 freq = p2p->go_neg_peer->listen_freq;
728 freq = p2p->go_neg_peer->oper_freq;
729 type = P2P_SCAN_SPECIFIC;
730 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
731 "for freq %u (GO Neg)", freq);
732 } else if (p2p->invite_peer) {
734 * Only scan the known listen frequency of the peer
735 * during Invite start.
737 freq = p2p->invite_peer->listen_freq;
739 freq = p2p->invite_peer->oper_freq;
740 type = P2P_SCAN_SPECIFIC;
741 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
742 "for freq %u (Invite)", freq);
743 } else if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
744 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
745 type = P2P_SCAN_SOCIAL_PLUS_ONE;
746 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
747 "(+ freq %u)", freq);
749 type = P2P_SCAN_SOCIAL;
750 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
753 if (p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
754 p2p->num_req_dev_types, p2p->req_dev_types) < 0)
756 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
757 "P2P: Scan request failed");
758 p2p_continue_find(p2p);
760 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
761 p2p->p2p_scan_running = 1;
762 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
763 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
769 static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
771 struct p2p_data *p2p = eloop_ctx;
772 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
777 static int p2p_run_after_scan(struct p2p_data *p2p)
779 struct p2p_device *dev;
780 enum p2p_after_scan op;
782 if (p2p->after_scan_tx) {
783 /* TODO: schedule p2p_run_after_scan to be called from TX
784 * status callback(?) */
785 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
786 "Action frame at p2p_scan completion");
787 p2p->cfg->send_action(p2p->cfg->cb_ctx,
788 p2p->after_scan_tx->freq,
789 p2p->after_scan_tx->dst,
790 p2p->after_scan_tx->src,
791 p2p->after_scan_tx->bssid,
792 (u8 *) (p2p->after_scan_tx + 1),
793 p2p->after_scan_tx->len,
794 p2p->after_scan_tx->wait_time);
795 os_free(p2p->after_scan_tx);
796 p2p->after_scan_tx = NULL;
800 op = p2p->start_after_scan;
801 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
803 case P2P_AFTER_SCAN_NOTHING:
805 case P2P_AFTER_SCAN_LISTEN:
806 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
807 "requested Listen state");
808 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
809 p2p->pending_listen_usec / 1000);
811 case P2P_AFTER_SCAN_CONNECT:
812 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
813 "requested connect with " MACSTR,
814 MAC2STR(p2p->after_scan_peer));
815 dev = p2p_get_device(p2p, p2p->after_scan_peer);
817 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
821 p2p_connect_send(p2p, dev);
829 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
831 struct p2p_data *p2p = eloop_ctx;
833 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
834 "(running=%d)", p2p->p2p_scan_running);
835 running = p2p->p2p_scan_running;
836 /* Make sure we recover from missed scan results callback */
837 p2p->p2p_scan_running = 0;
840 p2p_run_after_scan(p2p);
844 static void p2p_free_req_dev_types(struct p2p_data *p2p)
846 p2p->num_req_dev_types = 0;
847 os_free(p2p->req_dev_types);
848 p2p->req_dev_types = NULL;
852 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
853 enum p2p_discovery_type type,
854 unsigned int num_req_dev_types, const u8 *req_dev_types)
858 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
860 if (p2p->p2p_scan_running) {
861 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
865 p2p_free_req_dev_types(p2p);
866 if (req_dev_types && num_req_dev_types) {
867 p2p->req_dev_types = os_malloc(num_req_dev_types *
869 if (p2p->req_dev_types == NULL)
871 os_memcpy(p2p->req_dev_types, req_dev_types,
872 num_req_dev_types * WPS_DEV_TYPE_LEN);
873 p2p->num_req_dev_types = num_req_dev_types;
876 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
877 p2p_clear_timeout(p2p);
878 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
879 p2p->find_type = type;
880 p2p_device_clear_reported(p2p);
881 p2p_set_state(p2p, P2P_SEARCH);
882 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
883 p2p->last_p2p_find_timeout = timeout;
885 eloop_register_timeout(timeout, 0, p2p_find_timeout,
888 case P2P_FIND_START_WITH_FULL:
889 case P2P_FIND_PROGRESSIVE:
890 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
891 p2p->num_req_dev_types,
894 case P2P_FIND_ONLY_SOCIAL:
895 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
896 p2p->num_req_dev_types,
904 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
905 p2p->p2p_scan_running = 1;
906 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
907 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
909 } else if (res == 1) {
910 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
911 "p2p_scan at this point - will try again after "
912 "previous scan completes");
914 p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
915 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
917 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
919 p2p_set_state(p2p, P2P_IDLE);
920 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
927 int p2p_other_scan_completed(struct p2p_data *p2p)
929 if (p2p->state != P2P_SEARCH_WHEN_READY)
931 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find "
932 "now that previous scan was completed");
933 if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
934 p2p->num_req_dev_types, p2p->req_dev_types) < 0)
940 void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
942 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
943 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
944 p2p_clear_timeout(p2p);
945 p2p_set_state(p2p, P2P_IDLE);
946 p2p_free_req_dev_types(p2p);
947 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
948 p2p->go_neg_peer = NULL;
950 p2p->invite_peer = NULL;
951 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
952 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
953 "since we are on correct channel for response");
956 if (p2p->drv_in_listen) {
958 * The driver may not deliver callback to p2p_listen_end()
959 * when the operation gets canceled, so clear the internal
960 * variable that is tracking driver state.
962 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear "
963 "drv_in_listen (%d)", p2p->drv_in_listen);
964 p2p->drv_in_listen = 0;
966 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
970 void p2p_stop_find(struct p2p_data *p2p)
972 p2p_stop_find_for_freq(p2p, 0);
976 static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
979 u8 op_reg_class, op_channel;
980 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
981 &op_reg_class, &op_channel) < 0) {
982 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
983 "P2P: Unsupported frequency %u MHz",
987 if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
989 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
990 "P2P: Frequency %u MHz (oper_class %u "
991 "channel %u) not allowed for P2P",
992 force_freq, op_reg_class, op_channel);
995 p2p->op_reg_class = op_reg_class;
996 p2p->op_channel = op_channel;
997 p2p->channels.reg_classes = 1;
998 p2p->channels.reg_class[0].channels = 1;
999 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1000 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
1002 u8 op_reg_class, op_channel;
1004 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1005 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
1006 p2p_freq_to_channel(p2p->cfg->country,
1007 p2p->best_freq_overall,
1008 &op_reg_class, &op_channel) == 0) {
1009 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1010 "P2P: Select best overall channel as "
1011 "operating channel preference");
1012 p2p->op_reg_class = op_reg_class;
1013 p2p->op_channel = op_channel;
1014 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1015 p2p_supported_freq(p2p, p2p->best_freq_5) &&
1016 p2p_freq_to_channel(p2p->cfg->country,
1018 &op_reg_class, &op_channel) ==
1020 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1021 "P2P: Select best 5 GHz channel as "
1022 "operating channel preference");
1023 p2p->op_reg_class = op_reg_class;
1024 p2p->op_channel = op_channel;
1025 } else if (!p2p->cfg->cfg_op_channel &&
1026 p2p->best_freq_24 > 0 &&
1027 p2p_supported_freq(p2p, p2p->best_freq_24) &&
1028 p2p_freq_to_channel(p2p->cfg->country,
1030 &op_reg_class, &op_channel) ==
1032 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1033 "P2P: Select best 2.4 GHz channel as "
1034 "operating channel preference");
1035 p2p->op_reg_class = op_reg_class;
1036 p2p->op_channel = op_channel;
1038 p2p->op_reg_class = p2p->cfg->op_reg_class;
1039 p2p->op_channel = p2p->cfg->op_channel;
1042 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1043 sizeof(struct p2p_channels));
1045 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1046 "P2P: Own preference for operation channel: "
1047 "Operating Class %u Channel %u%s",
1048 p2p->op_reg_class, p2p->op_channel,
1049 force_freq ? " (forced)" : "");
1055 static void p2p_set_dev_persistent(struct p2p_device *dev,
1056 int persistent_group)
1058 switch (persistent_group) {
1060 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1061 P2P_DEV_PREFER_PERSISTENT_RECONN);
1064 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1065 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1068 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1069 P2P_DEV_PREFER_PERSISTENT_RECONN;
1075 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1076 enum p2p_wps_method wps_method,
1077 int go_intent, const u8 *own_interface_addr,
1078 unsigned int force_freq, int persistent_group)
1080 struct p2p_device *dev;
1082 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1083 "P2P: Request to start group negotiation - peer=" MACSTR
1084 " GO Intent=%d Intended Interface Address=" MACSTR
1085 " wps_method=%d persistent_group=%d",
1086 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1087 wps_method, persistent_group);
1089 if (p2p_prepare_channel(p2p, force_freq) < 0)
1093 dev = p2p_get_device(p2p, peer_addr);
1094 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1095 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1096 "P2P: Cannot connect to unknown P2P Device " MACSTR,
1097 MAC2STR(peer_addr));
1101 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1102 if (!(dev->info.dev_capab &
1103 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1104 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1105 "P2P: Cannot connect to P2P Device " MACSTR
1106 " that is in a group and is not discoverable",
1107 MAC2STR(peer_addr));
1110 if (dev->oper_freq <= 0) {
1111 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1112 "P2P: Cannot connect to P2P Device " MACSTR
1113 " with incomplete information",
1114 MAC2STR(peer_addr));
1119 * First, try to connect directly. If the peer does not
1120 * acknowledge frames, assume it is sleeping and use device
1121 * discoverability via the GO at that point.
1125 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1126 dev->flags &= ~P2P_DEV_USER_REJECTED;
1127 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1128 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1129 dev->connect_reqs = 0;
1130 dev->go_neg_req_sent = 0;
1131 dev->go_state = UNKNOWN_GO;
1132 p2p_set_dev_persistent(dev, persistent_group);
1133 p2p->go_intent = go_intent;
1134 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1136 if (p2p->state != P2P_IDLE)
1139 if (p2p->after_scan_tx) {
1141 * We need to drop the pending frame to avoid issues with the
1142 * new GO Negotiation, e.g., when the pending frame was from a
1143 * previous attempt at starting a GO Negotiation.
1145 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
1146 "previous pending Action frame TX that was waiting "
1147 "for p2p_scan completion");
1148 os_free(p2p->after_scan_tx);
1149 p2p->after_scan_tx = NULL;
1152 dev->wps_method = wps_method;
1153 dev->status = P2P_SC_SUCCESS;
1156 dev->flags |= P2P_DEV_FORCE_FREQ;
1158 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1160 if (p2p->p2p_scan_running) {
1161 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1162 "P2P: p2p_scan running - delay connect send");
1163 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1164 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1167 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1169 return p2p_connect_send(p2p, dev);
1173 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1174 enum p2p_wps_method wps_method,
1175 int go_intent, const u8 *own_interface_addr,
1176 unsigned int force_freq, int persistent_group)
1178 struct p2p_device *dev;
1180 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1181 "P2P: Request to authorize group negotiation - peer=" MACSTR
1182 " GO Intent=%d Intended Interface Address=" MACSTR
1183 " wps_method=%d persistent_group=%d",
1184 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1185 wps_method, persistent_group);
1187 if (p2p_prepare_channel(p2p, force_freq) < 0)
1190 dev = p2p_get_device(p2p, peer_addr);
1192 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1193 "P2P: Cannot authorize unknown P2P Device " MACSTR,
1194 MAC2STR(peer_addr));
1198 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1199 dev->flags &= ~P2P_DEV_USER_REJECTED;
1200 dev->go_neg_req_sent = 0;
1201 dev->go_state = UNKNOWN_GO;
1202 p2p_set_dev_persistent(dev, persistent_group);
1203 p2p->go_intent = go_intent;
1204 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1206 dev->wps_method = wps_method;
1207 dev->status = P2P_SC_SUCCESS;
1210 dev->flags |= P2P_DEV_FORCE_FREQ;
1212 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1218 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1219 struct p2p_device *dev, struct p2p_message *msg)
1221 os_get_time(&dev->last_seen);
1223 p2p_copy_wps_info(dev, 0, msg);
1225 if (msg->listen_channel) {
1227 freq = p2p_channel_to_freq((char *) msg->listen_channel,
1228 msg->listen_channel[3],
1229 msg->listen_channel[4]);
1231 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1232 "P2P: Unknown peer Listen channel: "
1233 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1234 msg->listen_channel[0],
1235 msg->listen_channel[1],
1236 msg->listen_channel[2],
1237 msg->listen_channel[3],
1238 msg->listen_channel[4]);
1240 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1241 "peer " MACSTR " Listen channel: %u -> %u MHz",
1242 MAC2STR(dev->info.p2p_device_addr),
1243 dev->listen_freq, freq);
1244 dev->listen_freq = freq;
1248 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1249 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1250 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1251 "P2P: Completed device entry based on data from "
1252 "GO Negotiation Request");
1254 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1255 "P2P: Created device entry based on GO Neg Req: "
1256 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1258 MAC2STR(dev->info.p2p_device_addr),
1259 dev->info.dev_capab, dev->info.group_capab,
1260 dev->info.device_name, dev->listen_freq);
1263 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1265 if (dev->flags & P2P_DEV_USER_REJECTED) {
1266 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1267 "P2P: Do not report rejected device");
1271 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1272 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1273 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1277 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1279 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1280 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1281 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1282 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1283 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1287 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1289 p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len);
1290 p2p_random(params->passphrase, 8);
1295 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1297 struct p2p_go_neg_results res;
1298 int go = peer->go_state == LOCAL_GO;
1299 struct p2p_channels intersection;
1303 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1304 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1305 "GO)", MAC2STR(peer->info.p2p_device_addr),
1306 go ? "local end" : "peer");
1308 os_memset(&res, 0, sizeof(res));
1310 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1311 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1312 res.wps_method = peer->wps_method;
1313 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1314 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1315 res.persistent_group = 2;
1317 res.persistent_group = 1;
1321 /* Setup AP mode for WPS provisioning */
1322 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1325 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1326 res.ssid_len = p2p->ssid_len;
1327 p2p_random(res.passphrase, 8);
1329 res.freq = peer->oper_freq;
1330 if (p2p->ssid_len) {
1331 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1332 res.ssid_len = p2p->ssid_len;
1336 p2p_channels_intersect(&p2p->channels, &peer->channels,
1339 for (i = 0; i < intersection.reg_classes; i++) {
1340 struct p2p_reg_class *c = &intersection.reg_class[i];
1341 if (freqs + 1 == P2P_MAX_CHANNELS)
1343 for (j = 0; j < c->channels; j++) {
1345 if (freqs + 1 == P2P_MAX_CHANNELS)
1347 freq = p2p_channel_to_freq(peer->country, c->reg_class,
1351 res.freq_list[freqs++] = freq;
1355 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1357 p2p_clear_timeout(p2p);
1359 peer->go_neg_req_sent = 0;
1360 peer->wps_method = WPS_NOT_READY;
1362 p2p_set_state(p2p, P2P_PROVISIONING);
1363 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1367 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1368 const u8 *data, size_t len, int rx_freq)
1370 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1371 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1372 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1378 case P2P_GO_NEG_REQ:
1379 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1381 case P2P_GO_NEG_RESP:
1382 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1384 case P2P_GO_NEG_CONF:
1385 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1387 case P2P_INVITATION_REQ:
1388 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1391 case P2P_INVITATION_RESP:
1392 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1394 case P2P_PROV_DISC_REQ:
1395 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1397 case P2P_PROV_DISC_RESP:
1398 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1400 case P2P_DEV_DISC_REQ:
1401 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1403 case P2P_DEV_DISC_RESP:
1404 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1407 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1408 "P2P: Unsupported P2P Public Action frame type %d",
1415 void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1416 const u8 *bssid, const u8 *data, size_t len,
1423 case WLAN_PA_VENDOR_SPECIFIC:
1428 if (WPA_GET_BE24(data) != OUI_WFA)
1436 if (*data != P2P_OUI_TYPE)
1439 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1441 case WLAN_PA_GAS_INITIAL_REQ:
1442 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1444 case WLAN_PA_GAS_INITIAL_RESP:
1445 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1447 case WLAN_PA_GAS_COMEBACK_REQ:
1448 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1450 case WLAN_PA_GAS_COMEBACK_RESP:
1451 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1457 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1458 const u8 *bssid, u8 category,
1459 const u8 *data, size_t len, int freq)
1461 if (category == WLAN_ACTION_PUBLIC) {
1462 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1466 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1472 if (WPA_GET_BE24(data) != OUI_WFA)
1477 if (*data != P2P_OUI_TYPE)
1482 /* P2P action frame */
1483 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1484 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1485 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1491 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1492 "P2P: Received P2P Action - Notice of Absence");
1495 case P2P_PRESENCE_REQ:
1496 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1498 case P2P_PRESENCE_RESP:
1499 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1501 case P2P_GO_DISC_REQ:
1502 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1505 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1506 "P2P: Received P2P Action - unknown type %u", data[0]);
1512 static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1514 struct p2p_data *p2p = eloop_ctx;
1515 if (p2p->go_neg_peer == NULL)
1517 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1518 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1519 p2p_connect_send(p2p, p2p->go_neg_peer);
1523 static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1525 struct p2p_data *p2p = eloop_ctx;
1526 if (p2p->invite_peer == NULL)
1528 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1529 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1533 static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1534 const u8 *ie, size_t ie_len)
1536 struct p2p_message msg;
1537 struct p2p_device *dev;
1539 os_memset(&msg, 0, sizeof(msg));
1540 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1542 p2p_parse_free(&msg);
1543 return; /* not a P2P probe */
1546 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1547 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1549 /* The Probe Request is not part of P2P Device Discovery. It is
1550 * not known whether the source address of the frame is the P2P
1551 * Device Address or P2P Interface Address. Do not add a new
1552 * peer entry based on this frames.
1554 p2p_parse_free(&msg);
1558 dev = p2p_get_device(p2p, addr);
1560 if (dev->country[0] == 0 && msg.listen_channel)
1561 os_memcpy(dev->country, msg.listen_channel, 3);
1562 os_get_time(&dev->last_seen);
1563 p2p_parse_free(&msg);
1564 return; /* already known */
1567 dev = p2p_create_device(p2p, addr);
1569 p2p_parse_free(&msg);
1573 os_get_time(&dev->last_seen);
1574 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1576 if (msg.listen_channel) {
1577 os_memcpy(dev->country, msg.listen_channel, 3);
1578 dev->listen_freq = p2p_channel_to_freq(dev->country,
1579 msg.listen_channel[3],
1580 msg.listen_channel[4]);
1583 p2p_copy_wps_info(dev, 1, &msg);
1585 p2p_parse_free(&msg);
1587 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1588 "P2P: Created device entry based on Probe Req: " MACSTR
1589 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1590 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1591 dev->info.group_capab, dev->info.device_name,
1596 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1598 struct p2p_message *msg)
1600 struct p2p_device *dev;
1602 dev = p2p_get_device(p2p, addr);
1604 os_get_time(&dev->last_seen);
1605 return dev; /* already known */
1608 dev = p2p_create_device(p2p, addr);
1612 p2p_add_dev_info(p2p, addr, dev, msg);
1618 static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1620 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1622 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1623 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1624 WPA_GET_BE16(&req_dev_type[6]) == 0)
1625 return 1; /* Category match with wildcard OUI/sub-category */
1630 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1631 size_t num_req_dev_type)
1634 for (i = 0; i < num_req_dev_type; i++) {
1635 if (dev_type_match(dev_type, req_dev_type[i]))
1643 * p2p_match_dev_type - Match local device type with requested type
1644 * @p2p: P2P module context from p2p_init()
1645 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1646 * Returns: 1 on match, 0 on mismatch
1648 * This function can be used to match the Requested Device Type attribute in
1649 * WPS IE with the local device types for deciding whether to reply to a Probe
1652 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1654 struct wps_parse_attr attr;
1657 if (wps_parse_msg(wps, &attr))
1658 return 1; /* assume no Requested Device Type attributes */
1660 if (attr.num_req_dev_type == 0)
1661 return 1; /* no Requested Device Type attributes -> match */
1663 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1664 attr.num_req_dev_type))
1665 return 1; /* Own Primary Device Type matches */
1667 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1668 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1670 attr.num_req_dev_type))
1671 return 1; /* Own Secondary Device Type matches */
1673 /* No matching device type found */
1678 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1683 buf = wpabuf_alloc(1000);
1687 p2p_build_wps_ie(p2p, buf, DEV_PW_DEFAULT, 1);
1690 len = p2p_buf_add_ie_hdr(buf);
1691 p2p_buf_add_capability(buf, p2p->dev_capab, 0);
1692 if (p2p->ext_listen_interval)
1693 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1694 p2p->ext_listen_interval);
1695 p2p_buf_add_device_info(buf, p2p, NULL);
1696 p2p_buf_update_ie_hdr(buf, len);
1702 static int is_11b(u8 rate)
1704 return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
1708 static int supp_rates_11b_only(struct ieee802_11_elems *elems)
1710 int num_11b = 0, num_others = 0;
1713 if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
1716 for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
1717 if (is_11b(elems->supp_rates[i]))
1723 for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
1725 if (is_11b(elems->ext_supp_rates[i]))
1731 return num_11b > 0 && num_others == 0;
1735 static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr,
1736 const u8 *dst, const u8 *bssid, const u8 *ie,
1739 struct ieee802_11_elems elems;
1741 struct ieee80211_mgmt *resp;
1742 struct p2p_message msg;
1745 if (!p2p->in_listen || !p2p->drv_in_listen) {
1746 /* not in Listen state - ignore Probe Request */
1750 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
1752 /* Ignore invalid Probe Request frames */
1756 if (elems.p2p == NULL) {
1757 /* not a P2P probe - ignore it */
1761 if (dst && !is_broadcast_ether_addr(dst) &&
1762 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
1763 /* Not sent to the broadcast address or our P2P Device Address
1768 if (bssid && !is_broadcast_ether_addr(bssid)) {
1769 /* Not sent to the Wildcard BSSID */
1773 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
1774 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
1776 /* not using P2P Wildcard SSID - ignore */
1780 if (supp_rates_11b_only(&elems)) {
1781 /* Indicates support for 11b rates only */
1785 os_memset(&msg, 0, sizeof(msg));
1786 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
1787 /* Could not parse P2P attributes */
1791 if (msg.device_id &&
1792 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN != 0)) {
1793 /* Device ID did not match */
1794 p2p_parse_free(&msg);
1798 /* Check Requested Device Type match */
1799 if (msg.wps_attributes &&
1800 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
1801 /* No match with Requested Device Type */
1802 p2p_parse_free(&msg);
1805 p2p_parse_free(&msg);
1807 if (!p2p->cfg->send_probe_resp)
1808 return; /* Response generated elsewhere */
1810 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1811 "P2P: Reply to P2P Probe Request in Listen state");
1814 * We do not really have a specific BSS that this frame is advertising,
1815 * so build a frame that has some information in valid format. This is
1816 * really only used for discovery purposes, not to learn exact BSS
1819 ies = p2p_build_probe_resp_ies(p2p);
1823 buf = wpabuf_alloc(200 + wpabuf_len(ies));
1830 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
1832 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
1833 (WLAN_FC_STYPE_PROBE_RESP << 4));
1834 os_memcpy(resp->da, addr, ETH_ALEN);
1835 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
1836 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
1837 resp->u.probe_resp.beacon_int = host_to_le16(100);
1838 /* hardware or low-level driver will setup seq_ctrl and timestamp */
1839 resp->u.probe_resp.capab_info =
1840 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
1841 WLAN_CAPABILITY_PRIVACY |
1842 WLAN_CAPABILITY_SHORT_SLOT_TIME);
1844 wpabuf_put_u8(buf, WLAN_EID_SSID);
1845 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
1846 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1848 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
1849 wpabuf_put_u8(buf, 8);
1850 wpabuf_put_u8(buf, (60 / 5) | 0x80);
1851 wpabuf_put_u8(buf, 90 / 5);
1852 wpabuf_put_u8(buf, (120 / 5) | 0x80);
1853 wpabuf_put_u8(buf, 180 / 5);
1854 wpabuf_put_u8(buf, (240 / 5) | 0x80);
1855 wpabuf_put_u8(buf, 360 / 5);
1856 wpabuf_put_u8(buf, 480 / 5);
1857 wpabuf_put_u8(buf, 540 / 5);
1859 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
1860 wpabuf_put_u8(buf, 1);
1861 wpabuf_put_u8(buf, p2p->cfg->channel);
1863 wpabuf_put_buf(buf, ies);
1866 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
1872 int p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
1873 const u8 *bssid, const u8 *ie, size_t ie_len)
1875 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
1877 p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
1879 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
1881 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
1883 /* Received a Probe Request from GO Negotiation peer */
1884 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1885 "P2P: Found GO Negotiation peer - try to start GO "
1886 "negotiation from timeout");
1887 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
1891 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
1893 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
1895 /* Received a Probe Request from Invite peer */
1896 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1897 "P2P: Found Invite peer - try to start Invite from "
1899 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
1907 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
1908 u8 *buf, size_t len, struct wpabuf *p2p_ie)
1917 return 0; /* WLAN AP is not a P2P manager */
1920 * (Re)Association Request - P2P IE
1921 * P2P Capability attribute (shall be present)
1922 * P2P Interface attribute (present if concurrent device and
1923 * P2P Management is enabled)
1925 tmp = wpabuf_alloc(200);
1929 lpos = p2p_buf_add_ie_hdr(tmp);
1931 if (p2p->num_groups > 0) {
1932 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
1933 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1934 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
1936 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
1938 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
1939 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1940 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
1941 p2p_buf_add_p2p_interface(tmp, p2p);
1942 p2p_buf_update_ie_hdr(tmp, lpos);
1944 tmplen = wpabuf_len(tmp);
1948 os_memcpy(buf, wpabuf_head(tmp), tmplen);
1957 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
1958 size_t len, int p2p_group, struct wpabuf *p2p_ie)
1962 struct p2p_device *peer;
1967 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
1970 * (Re)Association Request - P2P IE
1971 * P2P Capability attribute (shall be present)
1972 * Extended Listen Timing (may be present)
1973 * P2P Device Info attribute (shall be present)
1975 tmp = wpabuf_alloc(200);
1979 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
1981 lpos = p2p_buf_add_ie_hdr(tmp);
1982 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
1983 if (p2p->ext_listen_interval)
1984 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
1985 p2p->ext_listen_interval);
1986 p2p_buf_add_device_info(tmp, p2p, peer);
1987 p2p_buf_update_ie_hdr(tmp, lpos);
1989 tmplen = wpabuf_len(tmp);
1993 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2002 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2004 struct wpabuf *p2p_ie;
2007 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2011 ret = p2p_attr_text(p2p_ie, buf, end);
2012 wpabuf_free(p2p_ie);
2017 static void p2p_clear_go_neg(struct p2p_data *p2p)
2019 p2p->go_neg_peer = NULL;
2020 p2p_clear_timeout(p2p);
2021 p2p_set_state(p2p, P2P_IDLE);
2025 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2027 if (p2p->go_neg_peer == NULL) {
2028 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2029 "P2P: No pending Group Formation - "
2030 "ignore WPS registration success notification");
2031 return; /* No pending Group Formation */
2034 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2036 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2037 "P2P: Ignore WPS registration success notification "
2038 "for " MACSTR " (GO Negotiation peer " MACSTR ")",
2040 MAC2STR(p2p->go_neg_peer->intended_addr));
2041 return; /* Ignore unexpected peer address */
2044 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2045 "P2P: Group Formation completed successfully with " MACSTR,
2048 p2p_clear_go_neg(p2p);
2052 void p2p_group_formation_failed(struct p2p_data *p2p)
2054 if (p2p->go_neg_peer == NULL) {
2055 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2056 "P2P: No pending Group Formation - "
2057 "ignore group formation failure notification");
2058 return; /* No pending Group Formation */
2061 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2062 "P2P: Group Formation failed with " MACSTR,
2063 MAC2STR(p2p->go_neg_peer->intended_addr));
2065 p2p_clear_go_neg(p2p);
2069 struct p2p_data * p2p_init(const struct p2p_config *cfg)
2071 struct p2p_data *p2p;
2073 if (cfg->max_peers < 1)
2076 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2079 p2p->cfg = (struct p2p_config *) (p2p + 1);
2080 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2082 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2083 if (cfg->manufacturer)
2084 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2085 if (cfg->model_name)
2086 p2p->cfg->model_name = os_strdup(cfg->model_name);
2087 if (cfg->model_number)
2088 p2p->cfg->model_number = os_strdup(cfg->model_number);
2089 if (cfg->serial_number)
2090 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
2092 p2p->min_disc_int = 1;
2093 p2p->max_disc_int = 3;
2095 os_get_random(&p2p->next_tie_breaker, 1);
2096 p2p->next_tie_breaker &= 0x01;
2097 if (cfg->sd_request)
2098 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2099 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2100 if (cfg->concurrent_operations)
2101 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2102 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2104 dl_list_init(&p2p->devices);
2106 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
2107 p2p_expiration_timeout, p2p, NULL);
2113 void p2p_deinit(struct p2p_data *p2p)
2115 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
2116 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2117 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2119 p2p_free_req_dev_types(p2p);
2120 os_free(p2p->cfg->dev_name);
2121 os_free(p2p->cfg->manufacturer);
2122 os_free(p2p->cfg->model_name);
2123 os_free(p2p->cfg->model_number);
2124 os_free(p2p->cfg->serial_number);
2125 os_free(p2p->groups);
2126 wpabuf_free(p2p->sd_resp);
2127 os_free(p2p->after_scan_tx);
2128 p2p_remove_wps_vendor_extensions(p2p);
2133 void p2p_flush(struct p2p_data *p2p)
2135 struct p2p_device *dev, *prev;
2136 p2p_clear_timeout(p2p);
2137 p2p_set_state(p2p, P2P_IDLE);
2138 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
2139 p2p->go_neg_peer = NULL;
2140 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
2141 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2143 dl_list_del(&dev->list);
2144 p2p_device_free(p2p, dev);
2146 p2p_free_sd_queries(p2p);
2147 os_free(p2p->after_scan_tx);
2148 p2p->after_scan_tx = NULL;
2152 int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2154 struct p2p_device *dev;
2156 dev = p2p_get_device(p2p, addr);
2160 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
2163 if (p2p->go_neg_peer == dev)
2164 p2p->go_neg_peer = NULL;
2166 dev->wps_method = WPS_NOT_READY;
2167 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2168 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2170 /* Check if after_scan_tx is for this peer. If so free it */
2171 if (p2p->after_scan_tx &&
2172 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2173 os_free(p2p->after_scan_tx);
2174 p2p->after_scan_tx = NULL;
2181 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2183 os_free(p2p->cfg->dev_name);
2185 p2p->cfg->dev_name = os_strdup(dev_name);
2186 if (p2p->cfg->dev_name == NULL)
2189 p2p->cfg->dev_name = NULL;
2194 int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2196 os_free(p2p->cfg->manufacturer);
2197 p2p->cfg->manufacturer = NULL;
2199 p2p->cfg->manufacturer = os_strdup(manufacturer);
2200 if (p2p->cfg->manufacturer == NULL)
2208 int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2210 os_free(p2p->cfg->model_name);
2211 p2p->cfg->model_name = NULL;
2213 p2p->cfg->model_name = os_strdup(model_name);
2214 if (p2p->cfg->model_name == NULL)
2222 int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2224 os_free(p2p->cfg->model_number);
2225 p2p->cfg->model_number = NULL;
2227 p2p->cfg->model_number = os_strdup(model_number);
2228 if (p2p->cfg->model_number == NULL)
2236 int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2238 os_free(p2p->cfg->serial_number);
2239 p2p->cfg->serial_number = NULL;
2240 if (serial_number) {
2241 p2p->cfg->serial_number = os_strdup(serial_number);
2242 if (p2p->cfg->serial_number == NULL)
2250 void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2252 p2p->cfg->config_methods = config_methods;
2256 void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2258 os_memcpy(p2p->cfg->uuid, uuid, 16);
2262 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2264 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2269 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2270 size_t num_dev_types)
2272 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2273 num_dev_types = P2P_SEC_DEVICE_TYPES;
2274 p2p->cfg->num_sec_dev_types = num_dev_types;
2275 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2280 void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2284 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2285 wpabuf_free(p2p->wps_vendor_ext[i]);
2286 p2p->wps_vendor_ext[i] = NULL;
2291 int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2292 const struct wpabuf *vendor_ext)
2296 if (vendor_ext == NULL)
2299 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2300 if (p2p->wps_vendor_ext[i] == NULL)
2303 if (i >= P2P_MAX_WPS_VENDOR_EXT)
2306 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2307 if (p2p->wps_vendor_ext[i] == NULL)
2314 int p2p_set_country(struct p2p_data *p2p, const char *country)
2316 os_memcpy(p2p->cfg->country, country, 3);
2321 void p2p_continue_find(struct p2p_data *p2p)
2323 struct p2p_device *dev;
2324 p2p_set_state(p2p, P2P_SEARCH);
2325 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2326 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2327 if (p2p_start_sd(p2p, dev) == 0)
2331 } else if (dev->req_config_methods &&
2332 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2333 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2334 "pending Provisioning Discovery Request to "
2335 MACSTR " (config methods 0x%x)",
2336 MAC2STR(dev->info.p2p_device_addr),
2337 dev->req_config_methods);
2338 if (p2p_send_prov_disc_req(p2p, dev, 0) == 0)
2343 p2p_listen_in_find(p2p);
2347 static void p2p_sd_cb(struct p2p_data *p2p, int success)
2349 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2350 "P2P: Service Discovery Query TX callback: success=%d",
2352 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2356 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2357 p2p->sd_peer = NULL;
2359 p2p_continue_find(p2p);
2363 if (p2p->sd_peer == NULL) {
2364 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2365 "P2P: No SD peer entry known");
2366 p2p_continue_find(p2p);
2370 /* Wait for response from the peer */
2371 p2p_set_state(p2p, P2P_SD_DURING_FIND);
2372 p2p_set_timeout(p2p, 0, 200000);
2377 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2378 * @p2p: P2P module context from p2p_init()
2380 void p2p_retry_pd(struct p2p_data *p2p)
2382 struct p2p_device *dev;
2384 if (p2p->state != P2P_IDLE)
2388 * Retry the prov disc req attempt only for the peer that the user had
2389 * requested for and provided a join has not been initiated on it
2393 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2394 if (os_memcmp(p2p->pending_pd_devaddr,
2395 dev->info.p2p_device_addr, ETH_ALEN) != 0)
2397 if (!dev->req_config_methods)
2399 if (dev->flags & P2P_DEV_PD_FOR_JOIN)
2402 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2403 "pending Provisioning Discovery Request to "
2404 MACSTR " (config methods 0x%x)",
2405 MAC2STR(dev->info.p2p_device_addr),
2406 dev->req_config_methods);
2407 p2p_send_prov_disc_req(p2p, dev, 0);
2413 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2415 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2416 "P2P: Provision Discovery Request TX callback: success=%d",
2420 * Postpone resetting the pending action state till after we actually
2421 * time out. This allows us to take some action like notifying any
2422 * interested parties about no response to the request.
2424 * When the timer (below) goes off we check in IDLE, SEARCH, or
2425 * LISTEN_ONLY state, which are the only allowed states to issue a PD
2426 * requests in, if this was still pending and then raise notification.
2430 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2432 if (p2p->state != P2P_IDLE)
2433 p2p_continue_find(p2p);
2434 else if (p2p->user_initiated_pd) {
2435 p2p->pending_action_state = P2P_PENDING_PD;
2436 p2p_set_timeout(p2p, 0, 300000);
2442 * This postponing, of resetting pending_action_state, needs to be
2443 * done only for user initiated PD requests and not internal ones.
2445 if (p2p->user_initiated_pd)
2446 p2p->pending_action_state = P2P_PENDING_PD;
2448 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2450 /* Wait for response from the peer */
2451 if (p2p->state == P2P_SEARCH)
2452 p2p_set_state(p2p, P2P_PD_DURING_FIND);
2453 p2p_set_timeout(p2p, 0, 200000);
2457 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
2458 int level, const u8 *ies, size_t ies_len)
2460 p2p_add_device(p2p, bssid, freq, level, ies, ies_len);
2462 if (p2p->go_neg_peer && p2p->state == P2P_SEARCH &&
2463 os_memcmp(p2p->go_neg_peer->info.p2p_device_addr, bssid, ETH_ALEN)
2465 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2466 "P2P: Found GO Negotiation peer - try to start GO "
2468 p2p_connect_send(p2p, p2p->go_neg_peer);
2476 void p2p_scan_res_handled(struct p2p_data *p2p)
2478 if (!p2p->p2p_scan_running) {
2479 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
2480 "running, but scan results received");
2482 p2p->p2p_scan_running = 0;
2483 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2485 if (p2p_run_after_scan(p2p))
2487 if (p2p->state == P2P_SEARCH)
2488 p2p_continue_find(p2p);
2492 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies)
2494 u8 *len = p2p_buf_add_ie_hdr(ies);
2495 p2p_buf_add_capability(ies, p2p->dev_capab, 0);
2496 if (p2p->cfg->reg_class && p2p->cfg->channel)
2497 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2498 p2p->cfg->reg_class,
2500 if (p2p->ext_listen_interval)
2501 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2502 p2p->ext_listen_interval);
2503 /* TODO: p2p_buf_add_operating_channel() if GO */
2504 p2p_buf_update_ie_hdr(ies, len);
2508 size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
2514 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2516 return p2p_attr_text(p2p_ie, buf, end);
2520 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2522 struct p2p_device *dev = p2p->go_neg_peer;
2524 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2525 "P2P: GO Negotiation Request TX callback: success=%d",
2529 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2530 "P2P: No pending GO Negotiation");
2535 dev->go_neg_req_sent++;
2536 if (dev->flags & P2P_DEV_USER_REJECTED) {
2537 p2p_set_state(p2p, P2P_IDLE);
2543 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2544 !is_zero_ether_addr(dev->member_in_go_dev)) {
2545 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2546 "P2P: Peer " MACSTR " did not acknowledge request - "
2547 "try to use device discoverability through its GO",
2548 MAC2STR(dev->info.p2p_device_addr));
2549 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2550 p2p_send_dev_disc_req(p2p, dev);
2555 * Use P2P find, if needed, to find the other device from its listen
2558 p2p_set_state(p2p, P2P_CONNECT);
2559 p2p_set_timeout(p2p, 0, 100000);
2563 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2565 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2566 "P2P: GO Negotiation Response TX callback: success=%d",
2568 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2569 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2570 "P2P: Ignore TX callback event - GO Negotiation is "
2571 "not running anymore");
2574 p2p_set_state(p2p, P2P_CONNECT);
2575 p2p_set_timeout(p2p, 0, 100000);
2579 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2581 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2582 "P2P: GO Negotiation Response (failure) TX callback: "
2583 "success=%d", success);
2584 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2585 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2586 p2p->go_neg_peer->status);
2591 static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
2592 enum p2p_send_action_result result)
2594 struct p2p_device *dev;
2596 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2597 "P2P: GO Negotiation Confirm TX callback: result=%d",
2599 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2600 if (result == P2P_SEND_ACTION_FAILED) {
2601 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2604 if (result == P2P_SEND_ACTION_NO_ACK) {
2606 * It looks like the TX status for GO Negotiation Confirm is
2607 * often showing failure even when the peer has actually
2608 * received the frame. Since the peer may change channels
2609 * immediately after having received the frame, we may not see
2610 * an Ack for retries, so just dropping a single frame may
2611 * trigger this. To allow the group formation to succeed if the
2612 * peer did indeed receive the frame, continue regardless of
2615 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2616 "P2P: Assume GO Negotiation Confirm TX was actually "
2617 "received by the peer even though Ack was not "
2621 dev = p2p->go_neg_peer;
2625 p2p_go_complete(p2p, dev);
2629 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
2630 const u8 *src, const u8 *bssid,
2631 enum p2p_send_action_result result)
2633 enum p2p_pending_action_state state;
2636 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2637 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
2638 " src=" MACSTR " bssid=" MACSTR " result=%d",
2639 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
2640 MAC2STR(bssid), result);
2641 success = result == P2P_SEND_ACTION_SUCCESS;
2642 state = p2p->pending_action_state;
2643 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2645 case P2P_NO_PENDING_ACTION:
2647 case P2P_PENDING_GO_NEG_REQUEST:
2648 p2p_go_neg_req_cb(p2p, success);
2650 case P2P_PENDING_GO_NEG_RESPONSE:
2651 p2p_go_neg_resp_cb(p2p, success);
2653 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
2654 p2p_go_neg_resp_failure_cb(p2p, success);
2656 case P2P_PENDING_GO_NEG_CONFIRM:
2657 p2p_go_neg_conf_cb(p2p, result);
2659 case P2P_PENDING_SD:
2660 p2p_sd_cb(p2p, success);
2662 case P2P_PENDING_PD:
2663 p2p_prov_disc_cb(p2p, success);
2665 case P2P_PENDING_INVITATION_REQUEST:
2666 p2p_invitation_req_cb(p2p, success);
2668 case P2P_PENDING_INVITATION_RESPONSE:
2669 p2p_invitation_resp_cb(p2p, success);
2671 case P2P_PENDING_DEV_DISC_REQUEST:
2672 p2p_dev_disc_req_cb(p2p, success);
2674 case P2P_PENDING_DEV_DISC_RESPONSE:
2675 p2p_dev_disc_resp_cb(p2p, success);
2677 case P2P_PENDING_GO_DISC_REQ:
2678 p2p_go_disc_req_cb(p2p, success);
2684 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
2685 unsigned int duration)
2687 if (freq == p2p->pending_client_disc_freq) {
2688 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2689 "P2P: Client discoverability remain-awake completed");
2690 p2p->pending_client_disc_freq = 0;
2694 if (freq != p2p->pending_listen_freq) {
2695 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2696 "P2P: Unexpected listen callback for freq=%u "
2697 "duration=%u (pending_listen_freq=%u)",
2698 freq, duration, p2p->pending_listen_freq);
2702 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2703 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
2705 p2p->pending_listen_sec, p2p->pending_listen_usec,
2706 p2p->pending_listen_freq);
2708 p2p->drv_in_listen = freq;
2709 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
2711 * Add 20 msec extra wait to avoid race condition with driver
2712 * remain-on-channel end event, i.e., give driver more time to
2713 * complete the operation before our timeout expires.
2715 p2p_set_timeout(p2p, p2p->pending_listen_sec,
2716 p2p->pending_listen_usec + 20000);
2719 p2p->pending_listen_freq = 0;
2723 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
2725 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
2726 "state (freq=%u)", freq);
2727 p2p->drv_in_listen = 0;
2729 return 0; /* Internal timeout will trigger the next step */
2731 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
2732 if (p2p->go_neg_peer->connect_reqs >= 120) {
2733 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2734 "P2P: Timeout on sending GO Negotiation "
2735 "Request without getting response");
2736 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2740 p2p_set_state(p2p, P2P_CONNECT);
2741 p2p_connect_send(p2p, p2p->go_neg_peer);
2743 } else if (p2p->state == P2P_SEARCH) {
2752 static void p2p_timeout_connect(struct p2p_data *p2p)
2754 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2755 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
2756 p2p_listen_in_find(p2p);
2760 static void p2p_timeout_connect_listen(struct p2p_data *p2p)
2762 if (p2p->go_neg_peer) {
2763 if (p2p->drv_in_listen) {
2764 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
2765 "still in Listen state; wait for it to "
2770 if (p2p->go_neg_peer->connect_reqs >= 120) {
2771 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2772 "P2P: Timeout on sending GO Negotiation "
2773 "Request without getting response");
2774 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2778 p2p_set_state(p2p, P2P_CONNECT);
2779 p2p_connect_send(p2p, p2p->go_neg_peer);
2781 p2p_set_state(p2p, P2P_IDLE);
2785 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
2788 * TODO: could remain constantly in Listen state for some time if there
2789 * are no other concurrent uses for the radio. For now, go to listen
2790 * state once per second to give other uses a chance to use the radio.
2792 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
2793 p2p_set_timeout(p2p, 1, 0);
2797 static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
2799 struct p2p_device *dev = p2p->go_neg_peer;
2802 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2803 "P2P: Unknown GO Neg peer - stop GO Neg wait");
2808 if (dev->wait_count >= 120) {
2809 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2810 "P2P: Timeout on waiting peer to become ready for GO "
2812 p2p_go_neg_failed(p2p, dev, -1);
2816 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2817 "P2P: Go to Listen state while waiting for the peer to become "
2818 "ready for GO Negotiation");
2819 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
2820 p2p_listen_in_find(p2p);
2824 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
2826 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2827 "P2P: Service Discovery Query timeout");
2829 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2830 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2831 p2p->sd_peer = NULL;
2833 p2p_continue_find(p2p);
2837 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
2839 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2840 "P2P: Provision Discovery Request timeout");
2841 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2842 p2p_continue_find(p2p);
2846 static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
2848 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2851 * For user initiated PD requests that we have not gotten any responses
2852 * for while in IDLE state, we retry them a couple of times before
2855 if (!p2p->user_initiated_pd)
2858 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2859 "P2P: User initiated Provision Discovery Request timeout");
2861 if (p2p->pd_retries) {
2865 if (p2p->cfg->prov_disc_fail)
2866 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
2867 p2p->pending_pd_devaddr,
2868 P2P_PROV_DISC_TIMEOUT);
2869 p2p_reset_pending_pd(p2p);
2874 static void p2p_timeout_invite(struct p2p_data *p2p)
2876 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2877 p2p_set_state(p2p, P2P_INVITE_LISTEN);
2878 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
2880 * Better remain on operating channel instead of listen channel
2881 * when running a group.
2883 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
2884 "active GO role - wait on operating channel");
2885 p2p_set_timeout(p2p, 0, 100000);
2888 p2p_listen_in_find(p2p);
2892 static void p2p_timeout_invite_listen(struct p2p_data *p2p)
2894 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
2895 p2p_set_state(p2p, P2P_INVITE);
2896 p2p_invite_send(p2p, p2p->invite_peer,
2897 p2p->invite_go_dev_addr);
2899 if (p2p->invite_peer) {
2900 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2901 "P2P: Invitation Request retry limit reached");
2902 if (p2p->cfg->invitation_result)
2903 p2p->cfg->invitation_result(
2904 p2p->cfg->cb_ctx, -1, NULL);
2906 p2p_set_state(p2p, P2P_IDLE);
2911 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
2913 struct p2p_data *p2p = eloop_ctx;
2915 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
2916 p2p_state_txt(p2p->state));
2920 switch (p2p->state) {
2922 /* Check if we timed out waiting for PD req */
2923 if (p2p->pending_action_state == P2P_PENDING_PD)
2924 p2p_timeout_prov_disc_req(p2p);
2927 /* Check if we timed out waiting for PD req */
2928 if (p2p->pending_action_state == P2P_PENDING_PD)
2929 p2p_timeout_prov_disc_req(p2p);
2933 p2p_timeout_connect(p2p);
2935 case P2P_CONNECT_LISTEN:
2936 p2p_timeout_connect_listen(p2p);
2940 case P2P_LISTEN_ONLY:
2941 /* Check if we timed out waiting for PD req */
2942 if (p2p->pending_action_state == P2P_PENDING_PD)
2943 p2p_timeout_prov_disc_req(p2p);
2945 if (p2p->ext_listen_only) {
2946 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2947 "P2P: Extended Listen Timing - Listen State "
2949 p2p->ext_listen_only = 0;
2950 p2p_set_state(p2p, P2P_IDLE);
2953 case P2P_WAIT_PEER_CONNECT:
2954 p2p_timeout_wait_peer_connect(p2p);
2956 case P2P_WAIT_PEER_IDLE:
2957 p2p_timeout_wait_peer_idle(p2p);
2959 case P2P_SD_DURING_FIND:
2960 p2p_timeout_sd_during_find(p2p);
2962 case P2P_PROVISIONING:
2964 case P2P_PD_DURING_FIND:
2965 p2p_timeout_prov_disc_during_find(p2p);
2968 p2p_timeout_invite(p2p);
2970 case P2P_INVITE_LISTEN:
2971 p2p_timeout_invite_listen(p2p);
2973 case P2P_SEARCH_WHEN_READY:
2979 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
2981 struct p2p_device *dev;
2983 dev = p2p_get_device(p2p, peer_addr);
2984 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
2985 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
2987 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
2988 " unknown", MAC2STR(peer_addr));
2991 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
2992 dev->flags |= P2P_DEV_USER_REJECTED;
2997 static const char * p2p_wps_method_text(enum p2p_wps_method method)
3002 case WPS_PIN_DISPLAY:
3004 case WPS_PIN_KEYPAD:
3014 static const char * p2p_go_state_text(enum p2p_go_state go_state)
3029 int p2p_get_peer_info(struct p2p_data *p2p, const u8 *addr, int next,
3030 char *buf, size_t buflen)
3032 struct p2p_device *dev;
3036 char devtype[WPS_DEV_TYPE_BUFSIZE];
3039 dev = p2p_get_device(p2p, addr);
3041 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3044 dev = dl_list_first(&dev->list, struct p2p_device, list);
3045 if (&dev->list == &p2p->devices)
3055 res = os_snprintf(pos, end - pos, MACSTR "\n",
3056 MAC2STR(dev->info.p2p_device_addr));
3057 if (res < 0 || res >= end - pos)
3062 res = os_snprintf(pos, end - pos,
3067 "interface_addr=" MACSTR "\n"
3068 "member_in_go_dev=" MACSTR "\n"
3069 "member_in_go_iface=" MACSTR "\n"
3075 "serial_number=%s\n"
3076 "config_methods=0x%x\n"
3078 "group_capab=0x%x\n"
3079 "go_neg_req_sent=%d\n"
3082 "intended_addr=" MACSTR "\n"
3085 "req_config_methods=0x%x\n"
3086 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
3089 "invitation_reqs=%u\n",
3090 (int) (now.sec - dev->last_seen.sec),
3093 p2p_wps_method_text(dev->wps_method),
3094 MAC2STR(dev->interface_addr),
3095 MAC2STR(dev->member_in_go_dev),
3096 MAC2STR(dev->member_in_go_iface),
3097 wps_dev_type_bin2str(dev->info.pri_dev_type,
3098 devtype, sizeof(devtype)),
3099 dev->info.device_name,
3100 dev->info.manufacturer,
3101 dev->info.model_name,
3102 dev->info.model_number,
3103 dev->info.serial_number,
3104 dev->info.config_methods,
3105 dev->info.dev_capab,
3106 dev->info.group_capab,
3107 dev->go_neg_req_sent,
3108 p2p_go_state_text(dev->go_state),
3110 MAC2STR(dev->intended_addr),
3111 dev->country[0] ? dev->country[0] : '_',
3112 dev->country[1] ? dev->country[1] : '_',
3114 dev->req_config_methods,
3115 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
3116 "[PROBE_REQ_ONLY]" : "",
3117 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
3118 dev->flags & P2P_DEV_NOT_YET_READY ?
3119 "[NOT_YET_READY]" : "",
3120 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
3121 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
3123 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
3124 "[PD_PEER_DISPLAY]" : "",
3125 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
3126 "[PD_PEER_KEYPAD]" : "",
3127 dev->flags & P2P_DEV_USER_REJECTED ?
3128 "[USER_REJECTED]" : "",
3129 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
3130 "[PEER_WAITING_RESPONSE]" : "",
3131 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
3132 "[PREFER_PERSISTENT_GROUP]" : "",
3133 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
3134 "[WAIT_GO_NEG_RESPONSE]" : "",
3135 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
3136 "[WAIT_GO_NEG_CONFIRM]" : "",
3137 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
3138 "[GROUP_CLIENT_ONLY]" : "",
3139 dev->flags & P2P_DEV_FORCE_FREQ ?
3140 "[FORCE_FREQ]" : "",
3141 dev->flags & P2P_DEV_PD_FOR_JOIN ?
3142 "[PD_FOR_JOIN]" : "",
3145 dev->invitation_reqs);
3146 if (res < 0 || res >= end - pos)
3150 if (dev->ext_listen_period) {
3151 res = os_snprintf(pos, end - pos,
3152 "ext_listen_period=%u\n"
3153 "ext_listen_interval=%u\n",
3154 dev->ext_listen_period,
3155 dev->ext_listen_interval);
3156 if (res < 0 || res >= end - pos)
3161 if (dev->oper_ssid_len) {
3162 res = os_snprintf(pos, end - pos,
3164 wpa_ssid_txt(dev->oper_ssid,
3165 dev->oper_ssid_len));
3166 if (res < 0 || res >= end - pos)
3175 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
3178 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3179 "discoverability enabled");
3180 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3182 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3183 "discoverability disabled");
3184 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3189 static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
3190 u32 duration2, u32 interval2)
3193 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
3196 req = wpabuf_alloc(100);
3200 if (duration1 || interval1) {
3201 os_memset(&desc1, 0, sizeof(desc1));
3202 desc1.count_type = 1;
3203 desc1.duration = duration1;
3204 desc1.interval = interval1;
3207 if (duration2 || interval2) {
3208 os_memset(&desc2, 0, sizeof(desc2));
3209 desc2.count_type = 2;
3210 desc2.duration = duration2;
3211 desc2.interval = interval2;
3216 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
3217 len = p2p_buf_add_ie_hdr(req);
3218 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
3219 p2p_buf_update_ie_hdr(req, len);
3225 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
3226 const u8 *own_interface_addr, unsigned int freq,
3227 u32 duration1, u32 interval1, u32 duration2,
3232 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
3233 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
3234 "int1=%u dur2=%u int2=%u",
3235 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
3236 freq, duration1, interval1, duration2, interval2);
3238 req = p2p_build_presence_req(duration1, interval1, duration2,
3243 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3244 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3246 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
3247 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3248 "P2P: Failed to send Action frame");
3256 static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3257 size_t noa_len, u8 dialog_token)
3259 struct wpabuf *resp;
3262 resp = wpabuf_alloc(100 + noa_len);
3266 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3267 len = p2p_buf_add_ie_hdr(resp);
3268 p2p_buf_add_status(resp, status);
3270 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3271 wpabuf_put_le16(resp, noa_len);
3272 wpabuf_put_data(resp, noa, noa_len);
3274 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3275 p2p_buf_update_ie_hdr(resp, len);
3281 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3282 const u8 *sa, const u8 *data, size_t len,
3285 struct p2p_message msg;
3287 struct wpabuf *resp;
3289 struct p2p_group *group = NULL;
3294 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3295 "P2P: Received P2P Action - P2P Presence Request");
3297 for (g = 0; g < p2p->num_groups; g++) {
3298 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3300 group = p2p->groups[g];
3304 if (group == NULL) {
3305 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3306 "P2P: Ignore P2P Presence Request for unknown group "
3307 MACSTR, MAC2STR(da));
3311 if (p2p_parse(data, len, &msg) < 0) {
3312 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3313 "P2P: Failed to parse P2P Presence Request");
3314 status = P2P_SC_FAIL_INVALID_PARAMS;
3319 if (msg.noa == NULL) {
3320 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3321 "P2P: No NoA attribute in P2P Presence Request");
3322 status = P2P_SC_FAIL_INVALID_PARAMS;
3326 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3329 if (p2p->cfg->get_noa)
3330 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3334 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3335 noa_len > 0 ? noa_len : 0,
3338 p2p_parse_free(&msg);
3342 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3343 if (p2p_send_action(p2p, rx_freq, sa, da, da,
3344 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
3345 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3346 "P2P: Failed to send Action frame");
3352 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3353 const u8 *sa, const u8 *data, size_t len)
3355 struct p2p_message msg;
3357 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3358 "P2P: Received P2P Action - P2P Presence Response");
3360 if (p2p_parse(data, len, &msg) < 0) {
3361 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3362 "P2P: Failed to parse P2P Presence Response");
3366 if (msg.status == NULL || msg.noa == NULL) {
3367 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3368 "P2P: No Status or NoA attribute in P2P Presence "
3370 p2p_parse_free(&msg);
3375 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3376 "P2P: P2P Presence Request was rejected: status %u",
3378 p2p_parse_free(&msg);
3382 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3383 "P2P: P2P Presence Request was accepted");
3384 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
3385 msg.noa, msg.noa_len);
3386 /* TODO: process NoA */
3387 p2p_parse_free(&msg);
3391 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3393 struct p2p_data *p2p = eloop_ctx;
3395 if (p2p->ext_listen_interval) {
3396 /* Schedule next extended listen timeout */
3397 eloop_register_timeout(p2p->ext_listen_interval_sec,
3398 p2p->ext_listen_interval_usec,
3399 p2p_ext_listen_timeout, p2p, NULL);
3402 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
3404 * This should not really happen, but it looks like the Listen
3405 * command may fail is something else (e.g., a scan) was
3406 * running at an inconvenient time. As a workaround, allow new
3407 * Extended Listen operation to be started.
3409 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
3410 "Extended Listen operation had not been completed - "
3412 p2p->ext_listen_only = 0;
3413 p2p_set_state(p2p, P2P_IDLE);
3416 if (p2p->state != P2P_IDLE) {
3417 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
3418 "Listen timeout in active state (%s)",
3419 p2p_state_txt(p2p->state));
3423 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
3424 p2p->ext_listen_only = 1;
3425 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
3426 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
3427 "Listen state for Extended Listen Timing");
3428 p2p->ext_listen_only = 0;
3433 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
3434 unsigned int interval)
3436 if (period > 65535 || interval > 65535 || period > interval ||
3437 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
3438 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3439 "P2P: Invalid Extended Listen Timing request: "
3440 "period=%u interval=%u", period, interval);
3444 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
3446 if (interval == 0) {
3447 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3448 "P2P: Disabling Extended Listen Timing");
3449 p2p->ext_listen_period = 0;
3450 p2p->ext_listen_interval = 0;
3454 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3455 "P2P: Enabling Extended Listen Timing: period %u msec, "
3456 "interval %u msec", period, interval);
3457 p2p->ext_listen_period = period;
3458 p2p->ext_listen_interval = interval;
3459 p2p->ext_listen_interval_sec = interval / 1000;
3460 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
3462 eloop_register_timeout(p2p->ext_listen_interval_sec,
3463 p2p->ext_listen_interval_usec,
3464 p2p_ext_listen_timeout, p2p, NULL);
3470 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3471 const u8 *ie, size_t ie_len)
3473 struct p2p_message msg;
3475 if (bssid == NULL || ie == NULL)
3478 os_memset(&msg, 0, sizeof(msg));
3479 if (p2p_parse_ies(ie, ie_len, &msg))
3481 if (msg.minor_reason_code == NULL)
3484 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3485 "P2P: Deauthentication notification BSSID " MACSTR
3486 " reason_code=%u minor_reason_code=%u",
3487 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3489 p2p_parse_free(&msg);
3493 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3494 const u8 *ie, size_t ie_len)
3496 struct p2p_message msg;
3498 if (bssid == NULL || ie == NULL)
3501 os_memset(&msg, 0, sizeof(msg));
3502 if (p2p_parse_ies(ie, ie_len, &msg))
3504 if (msg.minor_reason_code == NULL)
3507 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3508 "P2P: Disassociation notification BSSID " MACSTR
3509 " reason_code=%u minor_reason_code=%u",
3510 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3512 p2p_parse_free(&msg);
3516 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
3519 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3520 "Device operations enabled");
3521 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
3523 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3524 "Device operations disabled");
3525 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
3530 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
3532 if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
3535 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
3536 "reg_class %u channel %u", reg_class, channel);
3537 p2p->cfg->reg_class = reg_class;
3538 p2p->cfg->channel = channel;
3544 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
3546 wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
3547 if (postfix == NULL) {
3548 p2p->cfg->ssid_postfix_len = 0;
3551 if (len > sizeof(p2p->cfg->ssid_postfix))
3553 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
3554 p2p->cfg->ssid_postfix_len = len;
3559 int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
3562 if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
3566 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
3567 "reg_class %u channel %u", op_reg_class, op_channel);
3568 p2p->cfg->op_reg_class = op_reg_class;
3569 p2p->cfg->op_channel = op_channel;
3570 p2p->cfg->cfg_op_channel = cfg_op_channel;
3575 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
3578 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
3579 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
3581 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
3586 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
3589 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3592 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
3597 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
3599 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
3600 if (is_zero_ether_addr(p2p->peer_filter))
3601 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
3604 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
3605 "filter for " MACSTR, MAC2STR(p2p->peer_filter));
3609 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
3611 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
3612 enabled ? "enabled" : "disabled");
3613 if (p2p->cross_connect == enabled)
3615 p2p->cross_connect = enabled;
3616 /* TODO: may need to tear down any action group where we are GO(?) */
3620 int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
3622 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3625 if (dev->oper_freq <= 0)
3627 return dev->oper_freq;
3631 void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
3633 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
3634 enabled ? "enabled" : "disabled");
3635 p2p->cfg->p2p_intra_bss = enabled;
3639 void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
3641 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
3642 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
3646 int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3647 const u8 *src, const u8 *bssid, const u8 *buf,
3648 size_t len, unsigned int wait_time)
3650 if (p2p->p2p_scan_running) {
3651 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
3652 "frame TX until p2p_scan completes");
3653 if (p2p->after_scan_tx) {
3654 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
3655 "previous pending Action frame TX");
3656 os_free(p2p->after_scan_tx);
3658 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
3660 if (p2p->after_scan_tx == NULL)
3662 p2p->after_scan_tx->freq = freq;
3663 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
3664 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
3665 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
3666 p2p->after_scan_tx->len = len;
3667 p2p->after_scan_tx->wait_time = wait_time;
3668 os_memcpy(p2p->after_scan_tx + 1, buf, len);
3672 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
3673 buf, len, wait_time);
3677 void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
3680 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
3681 " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall);
3682 p2p->best_freq_24 = freq_24;
3683 p2p->best_freq_5 = freq_5;
3684 p2p->best_freq_overall = freq_overall;
3688 const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
3690 if (p2p == NULL || p2p->go_neg_peer == NULL)
3692 return p2p->go_neg_peer->info.p2p_device_addr;
3696 const struct p2p_peer_info *
3697 p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
3699 struct p2p_device *dev;
3702 dev = p2p_get_device(p2p, addr);
3707 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
3713 dev = dl_list_first(&dev->list,
3716 if (&dev->list == &p2p->devices)
3718 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
3721 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3724 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
3725 dev = dl_list_first(&dev->list,
3728 if (&dev->list == &p2p->devices)
3737 int p2p_in_progress(struct p2p_data *p2p)
3741 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;