2 * Wi-Fi Direct - P2P module
3 * Copyright (c) 2009-2010, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #include "common/ieee802_11_defs.h"
14 #include "common/ieee802_11_common.h"
15 #include "common/wpa_ctrl.h"
16 #include "wps/wps_i.h"
21 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
22 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
23 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
24 const u8 *sa, const u8 *data, size_t len,
26 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
27 const u8 *sa, const u8 *data,
29 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
30 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
34 * p2p_scan recovery timeout
36 * Many drivers are using 30 second timeout on scan results. Allow a bit larger
37 * timeout for this to avoid hitting P2P timeout unnecessarily.
39 #define P2P_SCAN_TIMEOUT 35
42 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
43 * entries will be removed
45 #define P2P_PEER_EXPIRATION_AGE 300
47 #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
49 static void p2p_expire_peers(struct p2p_data *p2p)
51 struct p2p_device *dev, *n;
56 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
57 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
60 if (p2p->cfg->go_connected &&
61 p2p->cfg->go_connected(p2p->cfg->cb_ctx,
62 dev->info.p2p_device_addr)) {
64 * We are connected as a client to a group in which the
65 * peer is the GO, so do not expire the peer entry.
67 os_get_time(&dev->last_seen);
71 for (i = 0; i < p2p->num_groups; i++) {
72 if (p2p_group_is_client_connected(
73 p2p->groups[i], dev->info.p2p_device_addr))
76 if (i < p2p->num_groups) {
78 * The peer is connected as a client in a group where
79 * we are the GO, so do not expire the peer entry.
81 os_get_time(&dev->last_seen);
85 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer "
86 "entry " MACSTR, MAC2STR(dev->info.p2p_device_addr));
87 dl_list_del(&dev->list);
88 p2p_device_free(p2p, dev);
93 static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
95 struct p2p_data *p2p = eloop_ctx;
96 p2p_expire_peers(p2p);
97 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
98 p2p_expiration_timeout, p2p, NULL);
102 static const char * p2p_state_txt(int state)
111 case P2P_CONNECT_LISTEN:
112 return "CONNECT_LISTEN";
115 case P2P_LISTEN_ONLY:
116 return "LISTEN_ONLY";
117 case P2P_WAIT_PEER_CONNECT:
118 return "WAIT_PEER_CONNECT";
119 case P2P_WAIT_PEER_IDLE:
120 return "WAIT_PEER_IDLE";
121 case P2P_SD_DURING_FIND:
122 return "SD_DURING_FIND";
123 case P2P_PROVISIONING:
124 return "PROVISIONING";
125 case P2P_PD_DURING_FIND:
126 return "PD_DURING_FIND";
129 case P2P_INVITE_LISTEN:
130 return "INVITE_LISTEN";
131 case P2P_SEARCH_WHEN_READY:
132 return "SEARCH_WHEN_READY";
133 case P2P_CONTINUE_SEARCH_WHEN_READY:
134 return "CONTINUE_SEARCH_WHEN_READY";
141 u16 p2p_get_provisioning_info(struct p2p_data *p2p, const u8 *addr)
143 struct p2p_device *dev = NULL;
148 dev = p2p_get_device(p2p, addr);
150 return dev->wps_prov_info;
156 void p2p_clear_provisioning_info(struct p2p_data *p2p, const u8 *addr)
158 struct p2p_device *dev = NULL;
163 dev = p2p_get_device(p2p, addr);
165 dev->wps_prov_info = 0;
169 void p2p_set_state(struct p2p_data *p2p, int new_state)
171 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s",
172 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
173 p2p->state = new_state;
177 void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
179 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
180 "P2P: Set timeout (state=%s): %u.%06u sec",
181 p2p_state_txt(p2p->state), sec, usec);
182 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
183 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
187 void p2p_clear_timeout(struct p2p_data *p2p)
189 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)",
190 p2p_state_txt(p2p->state));
191 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
195 void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
198 struct p2p_go_neg_results res;
199 p2p_clear_timeout(p2p);
200 p2p_set_state(p2p, P2P_IDLE);
201 if (p2p->go_neg_peer)
202 p2p->go_neg_peer->wps_method = WPS_NOT_READY;
203 p2p->go_neg_peer = NULL;
205 os_memset(&res, 0, sizeof(res));
208 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr,
210 os_memcpy(res.peer_interface_addr, peer->intended_addr,
213 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
217 static void p2p_listen_in_find(struct p2p_data *p2p)
223 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
224 "P2P: Starting short listen state (state=%s)",
225 p2p_state_txt(p2p->state));
227 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
230 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
231 "P2P: Unknown regulatory class/channel");
235 os_get_random((u8 *) &r, sizeof(r));
236 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
237 p2p->min_disc_int) * 100;
239 p2p->pending_listen_freq = freq;
240 p2p->pending_listen_sec = 0;
241 p2p->pending_listen_usec = 1024 * tu;
243 ies = p2p_build_probe_resp_ies(p2p);
247 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
249 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
250 "P2P: Failed to start listen mode");
251 p2p->pending_listen_freq = 0;
257 int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
262 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
263 "P2P: Going to listen(only) state");
265 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
268 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
269 "P2P: Unknown regulatory class/channel");
273 p2p->pending_listen_freq = freq;
274 p2p->pending_listen_sec = timeout / 1000;
275 p2p->pending_listen_usec = (timeout % 1000) * 1000;
277 if (p2p->p2p_scan_running) {
278 if (p2p->start_after_scan == P2P_AFTER_SCAN_CONNECT) {
279 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
280 "P2P: p2p_scan running - connect is already "
281 "pending - skip listen");
284 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
285 "P2P: p2p_scan running - delay start of listen state");
286 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
290 ies = p2p_build_probe_resp_ies(p2p);
294 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
295 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
296 "P2P: Failed to start listen mode");
297 p2p->pending_listen_freq = 0;
303 p2p_set_state(p2p, P2P_LISTEN_ONLY);
309 static void p2p_device_clear_reported(struct p2p_data *p2p)
311 struct p2p_device *dev;
312 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
313 dev->flags &= ~P2P_DEV_REPORTED;
318 * p2p_get_device - Fetch a peer entry
319 * @p2p: P2P module context from p2p_init()
320 * @addr: P2P Device Address of the peer
321 * Returns: Pointer to the device entry or %NULL if not found
323 struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
325 struct p2p_device *dev;
326 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
327 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
335 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
336 * @p2p: P2P module context from p2p_init()
337 * @addr: P2P Interface Address of the peer
338 * Returns: Pointer to the device entry or %NULL if not found
340 struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
343 struct p2p_device *dev;
344 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
345 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
353 * p2p_create_device - Create a peer entry
354 * @p2p: P2P module context from p2p_init()
355 * @addr: P2P Device Address of the peer
356 * Returns: Pointer to the device entry or %NULL on failure
358 * If there is already an entry for the peer, it will be returned instead of
359 * creating a new one.
361 static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
364 struct p2p_device *dev, *oldest = NULL;
367 dev = p2p_get_device(p2p, addr);
371 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
373 if (oldest == NULL ||
374 os_time_before(&dev->last_seen, &oldest->last_seen))
377 if (count + 1 > p2p->cfg->max_peers && oldest) {
378 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
379 "P2P: Remove oldest peer entry to make room for a new "
381 dl_list_del(&oldest->list);
382 p2p_device_free(p2p, oldest);
385 dev = os_zalloc(sizeof(*dev));
388 dl_list_add(&p2p->devices, &dev->list);
389 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
395 static void p2p_copy_client_info(struct p2p_device *dev,
396 struct p2p_client_info *cli)
398 os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
399 dev->info.device_name[cli->dev_name_len] = '\0';
400 dev->info.dev_capab = cli->dev_capab;
401 dev->info.config_methods = cli->config_methods;
402 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
403 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
404 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
405 dev->info.wps_sec_dev_type_list_len);
409 static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
410 const u8 *go_interface_addr, int freq,
411 const u8 *gi, size_t gi_len)
413 struct p2p_group_info info;
415 struct p2p_device *dev;
420 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
424 * Clear old data for this group; if the devices are still in the
425 * group, the information will be restored in the loop following this.
427 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
428 if (os_memcmp(dev->member_in_go_iface, go_interface_addr,
430 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
431 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
435 for (c = 0; c < info.num_clients; c++) {
436 struct p2p_client_info *cli = &info.client[c];
437 if (os_memcmp(cli->p2p_device_addr, p2p->cfg->dev_addr,
439 continue; /* ignore our own entry */
440 dev = p2p_get_device(p2p, cli->p2p_device_addr);
442 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
443 P2P_DEV_PROBE_REQ_ONLY)) {
445 * Update information since we have not
446 * received this directly from the client.
448 p2p_copy_client_info(dev, cli);
451 * Need to update P2P Client Discoverability
452 * flag since it is valid only in P2P Group
455 dev->info.dev_capab &=
456 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
457 dev->info.dev_capab |=
459 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
461 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
462 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
465 dev = p2p_create_device(p2p, cli->p2p_device_addr);
468 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
469 p2p_copy_client_info(dev, cli);
470 dev->oper_freq = freq;
471 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
472 dev->info.p2p_device_addr,
474 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
477 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
479 os_get_time(&dev->last_seen);
480 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
481 os_memcpy(dev->member_in_go_iface, go_interface_addr,
489 static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
490 const struct p2p_message *msg)
492 os_memcpy(dev->info.device_name, msg->device_name,
493 sizeof(dev->info.device_name));
495 if (msg->manufacturer &&
496 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
497 os_memset(dev->info.manufacturer, 0,
498 sizeof(dev->info.manufacturer));
499 os_memcpy(dev->info.manufacturer, msg->manufacturer,
500 msg->manufacturer_len);
503 if (msg->model_name &&
504 msg->model_name_len < sizeof(dev->info.model_name)) {
505 os_memset(dev->info.model_name, 0,
506 sizeof(dev->info.model_name));
507 os_memcpy(dev->info.model_name, msg->model_name,
508 msg->model_name_len);
511 if (msg->model_number &&
512 msg->model_number_len < sizeof(dev->info.model_number)) {
513 os_memset(dev->info.model_number, 0,
514 sizeof(dev->info.model_number));
515 os_memcpy(dev->info.model_number, msg->model_number,
516 msg->model_number_len);
519 if (msg->serial_number &&
520 msg->serial_number_len < sizeof(dev->info.serial_number)) {
521 os_memset(dev->info.serial_number, 0,
522 sizeof(dev->info.serial_number));
523 os_memcpy(dev->info.serial_number, msg->serial_number,
524 msg->serial_number_len);
527 if (msg->pri_dev_type)
528 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
529 sizeof(dev->info.pri_dev_type));
530 else if (msg->wps_pri_dev_type)
531 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
532 sizeof(dev->info.pri_dev_type));
534 if (msg->wps_sec_dev_type_list) {
535 os_memcpy(dev->info.wps_sec_dev_type_list,
536 msg->wps_sec_dev_type_list,
537 msg->wps_sec_dev_type_list_len);
538 dev->info.wps_sec_dev_type_list_len =
539 msg->wps_sec_dev_type_list_len;
542 if (msg->capability) {
544 * P2P Client Discoverability bit is reserved in all frames
545 * that use this function, so do not change its value here.
547 dev->info.dev_capab &= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
548 dev->info.dev_capab |= msg->capability[0] &
549 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
550 dev->info.group_capab = msg->capability[1];
553 if (msg->ext_listen_timing) {
554 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
555 dev->ext_listen_interval =
556 WPA_GET_LE16(msg->ext_listen_timing + 2);
560 dev->info.config_methods = msg->config_methods ?
561 msg->config_methods : msg->wps_config_methods;
567 * p2p_add_device - Add peer entries based on scan results or P2P frames
568 * @p2p: P2P module context from p2p_init()
569 * @addr: Source address of Beacon or Probe Response frame (may be either
570 * P2P Device Address or P2P Interface Address)
571 * @level: Signal level (signal strength of the received frame from the peer)
572 * @freq: Frequency on which the Beacon or Probe Response frame was received
573 * @ies: IEs from the Beacon or Probe Response frame
574 * @ies_len: Length of ies buffer in octets
575 * @scan_res: Whether this was based on scan results
576 * Returns: 0 on success, -1 on failure
578 * If the scan result is for a GO, the clients in the group will also be added
579 * to the peer table. This function can also be used with some other frames
580 * like Provision Discovery Request that contains P2P Capability and P2P Device
583 int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
584 const u8 *ies, size_t ies_len, int scan_res)
586 struct p2p_device *dev;
587 struct p2p_message msg;
588 const u8 *p2p_dev_addr;
591 os_memset(&msg, 0, sizeof(msg));
592 if (p2p_parse_ies(ies, ies_len, &msg)) {
593 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
594 "P2P: Failed to parse P2P IE for a device entry");
595 p2p_parse_free(&msg);
599 if (msg.p2p_device_addr)
600 p2p_dev_addr = msg.p2p_device_addr;
601 else if (msg.device_id)
602 p2p_dev_addr = msg.device_id;
604 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
605 "P2P: Ignore scan data without P2P Device Info or "
607 p2p_parse_free(&msg);
611 if (!is_zero_ether_addr(p2p->peer_filter) &&
612 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
613 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
614 "filter for " MACSTR " due to peer filter",
615 MAC2STR(p2p_dev_addr));
619 dev = p2p_create_device(p2p, p2p_dev_addr);
621 p2p_parse_free(&msg);
624 os_get_time(&dev->last_seen);
625 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
627 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
628 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
630 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
631 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
633 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
634 dev->oper_ssid_len = msg.ssid[1];
637 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
638 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
640 if (*msg.ds_params == 14)
643 ds_freq = 2407 + *msg.ds_params * 5;
644 if (freq != ds_freq) {
645 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
646 "P2P: Update Listen frequency based on DS "
647 "Parameter Set IE: %d -> %d MHz",
653 if (dev->listen_freq && dev->listen_freq != freq && scan_res) {
654 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
655 "P2P: Update Listen frequency based on scan "
656 "results (" MACSTR " %d -> %d MHz (DS param %d)",
657 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
658 freq, msg.ds_params ? *msg.ds_params : -1);
661 dev->listen_freq = freq;
663 dev->oper_freq = freq;
665 dev->info.level = level;
667 p2p_copy_wps_info(dev, 0, &msg);
669 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
670 wpabuf_free(dev->info.wps_vendor_ext[i]);
671 dev->info.wps_vendor_ext[i] = NULL;
674 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
675 if (msg.wps_vendor_ext[i] == NULL)
677 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
678 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
679 if (dev->info.wps_vendor_ext[i] == NULL)
684 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq,
685 msg.group_info, msg.group_info_len);
688 p2p_parse_free(&msg);
690 if (p2p_pending_sd_req(p2p, dev))
691 dev->flags |= P2P_DEV_SD_SCHEDULE;
693 if (dev->flags & P2P_DEV_REPORTED)
696 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
697 "P2P: Peer found with Listen frequency %d MHz", freq);
698 if (dev->flags & P2P_DEV_USER_REJECTED) {
699 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
700 "P2P: Do not report rejected device");
704 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
705 !(dev->flags & P2P_DEV_REPORTED_ONCE));
706 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
712 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
716 if (p2p->go_neg_peer == dev) {
718 * If GO Negotiation is in progress, report that it has failed.
720 p2p_go_neg_failed(p2p, dev, -1);
721 p2p->go_neg_peer = NULL;
723 if (p2p->invite_peer == dev)
724 p2p->invite_peer = NULL;
725 if (p2p->sd_peer == dev)
727 if (p2p->pending_client_disc_go == dev)
728 p2p->pending_client_disc_go = NULL;
730 /* dev_lost() device, but only if it was previously dev_found() */
731 if (dev->flags & P2P_DEV_REPORTED_ONCE)
732 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
733 dev->info.p2p_device_addr);
735 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
736 wpabuf_free(dev->info.wps_vendor_ext[i]);
737 dev->info.wps_vendor_ext[i] = NULL;
744 static int p2p_get_next_prog_freq(struct p2p_data *p2p)
746 struct p2p_channels *c;
747 struct p2p_reg_class *cla;
754 c = &p2p->cfg->channels;
755 for (cl = 0; cl < c->reg_classes; cl++) {
756 cla = &c->reg_class[cl];
757 if (cla->reg_class != p2p->last_prog_scan_class)
759 for (ch = 0; ch < cla->channels; ch++) {
760 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
770 /* Start from beginning */
771 reg_class = c->reg_class[0].reg_class;
772 channel = c->reg_class[0].channel[0];
774 /* Pick the next channel */
776 if (ch == cla->channels) {
778 if (cl == c->reg_classes)
782 reg_class = c->reg_class[cl].reg_class;
783 channel = c->reg_class[cl].channel[ch];
786 freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
787 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
788 "channel: reg_class %u channel %u -> %d MHz",
789 reg_class, channel, freq);
790 p2p->last_prog_scan_class = reg_class;
791 p2p->last_prog_scan_chan = channel;
793 if (freq == 2412 || freq == 2437 || freq == 2462)
794 return 0; /* No need to add social channels */
799 static void p2p_search(struct p2p_data *p2p)
802 enum p2p_scan_type type;
803 u16 pw_id = DEV_PW_DEFAULT;
806 if (p2p->drv_in_listen) {
807 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
808 "in Listen state - wait for it to end before "
812 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
814 if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
815 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
816 type = P2P_SCAN_SOCIAL_PLUS_ONE;
817 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
818 "(+ freq %u)", freq);
820 type = P2P_SCAN_SOCIAL;
821 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
824 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
825 p2p->num_req_dev_types, p2p->req_dev_types,
826 p2p->find_dev_id, pw_id);
828 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
829 "P2P: Scan request failed");
830 p2p_continue_find(p2p);
831 } else if (res == 1) {
832 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
833 "p2p_scan at this point - will try again after "
834 "previous scan completes");
835 p2p_set_state(p2p, P2P_CONTINUE_SEARCH_WHEN_READY);
837 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
838 p2p->p2p_scan_running = 1;
839 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
840 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
846 static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
848 struct p2p_data *p2p = eloop_ctx;
849 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
854 static int p2p_run_after_scan(struct p2p_data *p2p)
856 struct p2p_device *dev;
857 enum p2p_after_scan op;
859 if (p2p->after_scan_tx) {
860 /* TODO: schedule p2p_run_after_scan to be called from TX
861 * status callback(?) */
862 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
863 "Action frame at p2p_scan completion");
864 p2p->cfg->send_action(p2p->cfg->cb_ctx,
865 p2p->after_scan_tx->freq,
866 p2p->after_scan_tx->dst,
867 p2p->after_scan_tx->src,
868 p2p->after_scan_tx->bssid,
869 (u8 *) (p2p->after_scan_tx + 1),
870 p2p->after_scan_tx->len,
871 p2p->after_scan_tx->wait_time);
872 os_free(p2p->after_scan_tx);
873 p2p->after_scan_tx = NULL;
877 op = p2p->start_after_scan;
878 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
880 case P2P_AFTER_SCAN_NOTHING:
882 case P2P_AFTER_SCAN_LISTEN:
883 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
884 "requested Listen state");
885 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
886 p2p->pending_listen_usec / 1000);
888 case P2P_AFTER_SCAN_CONNECT:
889 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
890 "requested connect with " MACSTR,
891 MAC2STR(p2p->after_scan_peer));
892 dev = p2p_get_device(p2p, p2p->after_scan_peer);
894 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
898 p2p_connect_send(p2p, dev);
906 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
908 struct p2p_data *p2p = eloop_ctx;
910 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
911 "(running=%d)", p2p->p2p_scan_running);
912 running = p2p->p2p_scan_running;
913 /* Make sure we recover from missed scan results callback */
914 p2p->p2p_scan_running = 0;
917 p2p_run_after_scan(p2p);
921 static void p2p_free_req_dev_types(struct p2p_data *p2p)
923 p2p->num_req_dev_types = 0;
924 os_free(p2p->req_dev_types);
925 p2p->req_dev_types = NULL;
929 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
930 enum p2p_discovery_type type,
931 unsigned int num_req_dev_types, const u8 *req_dev_types,
932 const u8 *dev_id, unsigned int search_delay)
936 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
938 if (p2p->p2p_scan_running) {
939 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
943 p2p_free_req_dev_types(p2p);
944 if (req_dev_types && num_req_dev_types) {
945 p2p->req_dev_types = os_malloc(num_req_dev_types *
947 if (p2p->req_dev_types == NULL)
949 os_memcpy(p2p->req_dev_types, req_dev_types,
950 num_req_dev_types * WPS_DEV_TYPE_LEN);
951 p2p->num_req_dev_types = num_req_dev_types;
955 os_memcpy(p2p->find_dev_id_buf, dev_id, ETH_ALEN);
956 p2p->find_dev_id = p2p->find_dev_id_buf;
958 p2p->find_dev_id = NULL;
960 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
961 p2p_clear_timeout(p2p);
962 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
963 p2p->find_type = type;
964 p2p_device_clear_reported(p2p);
965 p2p_set_state(p2p, P2P_SEARCH);
966 p2p->search_delay = search_delay;
967 p2p->in_search_delay = 0;
968 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
969 p2p->last_p2p_find_timeout = timeout;
971 eloop_register_timeout(timeout, 0, p2p_find_timeout,
974 case P2P_FIND_START_WITH_FULL:
975 case P2P_FIND_PROGRESSIVE:
976 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
977 p2p->num_req_dev_types,
978 p2p->req_dev_types, dev_id,
981 case P2P_FIND_ONLY_SOCIAL:
982 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
983 p2p->num_req_dev_types,
984 p2p->req_dev_types, dev_id,
992 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
993 p2p->p2p_scan_running = 1;
994 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
995 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
997 } else if (res == 1) {
998 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Could not start "
999 "p2p_scan at this point - will try again after "
1000 "previous scan completes");
1002 p2p_set_state(p2p, P2P_SEARCH_WHEN_READY);
1003 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1005 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
1007 p2p_set_state(p2p, P2P_IDLE);
1008 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1015 int p2p_other_scan_completed(struct p2p_data *p2p)
1017 if (p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY) {
1018 p2p_set_state(p2p, P2P_SEARCH);
1022 if (p2p->state != P2P_SEARCH_WHEN_READY)
1024 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting pending P2P find "
1025 "now that previous scan was completed");
1026 if (p2p_find(p2p, p2p->last_p2p_find_timeout, p2p->find_type,
1027 p2p->num_req_dev_types, p2p->req_dev_types,
1028 p2p->find_dev_id, p2p->search_delay) < 0)
1034 void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
1036 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
1037 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
1038 p2p_clear_timeout(p2p);
1039 if (p2p->state == P2P_SEARCH)
1040 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1041 p2p_set_state(p2p, P2P_IDLE);
1042 p2p_free_req_dev_types(p2p);
1043 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1044 p2p->go_neg_peer = NULL;
1045 p2p->sd_peer = NULL;
1046 p2p->invite_peer = NULL;
1047 p2p_stop_listen_for_freq(p2p, freq);
1051 void p2p_stop_listen_for_freq(struct p2p_data *p2p, int freq)
1053 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
1054 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
1055 "since we are on correct channel for response");
1058 if (p2p->in_listen) {
1060 p2p_clear_timeout(p2p);
1062 if (p2p->drv_in_listen) {
1064 * The driver may not deliver callback to p2p_listen_end()
1065 * when the operation gets canceled, so clear the internal
1066 * variable that is tracking driver state.
1068 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear "
1069 "drv_in_listen (%d)", p2p->drv_in_listen);
1070 p2p->drv_in_listen = 0;
1072 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1076 void p2p_stop_find(struct p2p_data *p2p)
1078 p2p_stop_find_for_freq(p2p, 0);
1082 static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
1085 u8 op_reg_class, op_channel;
1086 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
1087 &op_reg_class, &op_channel) < 0) {
1088 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1089 "P2P: Unsupported frequency %u MHz",
1093 if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
1095 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1096 "P2P: Frequency %u MHz (oper_class %u "
1097 "channel %u) not allowed for P2P",
1098 force_freq, op_reg_class, op_channel);
1101 p2p->op_reg_class = op_reg_class;
1102 p2p->op_channel = op_channel;
1103 p2p->channels.reg_classes = 1;
1104 p2p->channels.reg_class[0].channels = 1;
1105 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
1106 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
1108 u8 op_reg_class, op_channel;
1110 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
1111 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
1112 p2p_freq_to_channel(p2p->cfg->country,
1113 p2p->best_freq_overall,
1114 &op_reg_class, &op_channel) == 0) {
1115 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1116 "P2P: Select best overall channel as "
1117 "operating channel preference");
1118 p2p->op_reg_class = op_reg_class;
1119 p2p->op_channel = op_channel;
1120 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
1121 p2p_supported_freq(p2p, p2p->best_freq_5) &&
1122 p2p_freq_to_channel(p2p->cfg->country,
1124 &op_reg_class, &op_channel) ==
1126 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1127 "P2P: Select best 5 GHz channel as "
1128 "operating channel preference");
1129 p2p->op_reg_class = op_reg_class;
1130 p2p->op_channel = op_channel;
1131 } else if (!p2p->cfg->cfg_op_channel &&
1132 p2p->best_freq_24 > 0 &&
1133 p2p_supported_freq(p2p, p2p->best_freq_24) &&
1134 p2p_freq_to_channel(p2p->cfg->country,
1136 &op_reg_class, &op_channel) ==
1138 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1139 "P2P: Select best 2.4 GHz channel as "
1140 "operating channel preference");
1141 p2p->op_reg_class = op_reg_class;
1142 p2p->op_channel = op_channel;
1144 p2p->op_reg_class = p2p->cfg->op_reg_class;
1145 p2p->op_channel = p2p->cfg->op_channel;
1148 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1149 sizeof(struct p2p_channels));
1151 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1152 "P2P: Own preference for operation channel: "
1153 "Operating Class %u Channel %u%s",
1154 p2p->op_reg_class, p2p->op_channel,
1155 force_freq ? " (forced)" : "");
1161 static void p2p_set_dev_persistent(struct p2p_device *dev,
1162 int persistent_group)
1164 switch (persistent_group) {
1166 dev->flags &= ~(P2P_DEV_PREFER_PERSISTENT_GROUP |
1167 P2P_DEV_PREFER_PERSISTENT_RECONN);
1170 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1171 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_RECONN;
1174 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP |
1175 P2P_DEV_PREFER_PERSISTENT_RECONN;
1181 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1182 enum p2p_wps_method wps_method,
1183 int go_intent, const u8 *own_interface_addr,
1184 unsigned int force_freq, int persistent_group,
1185 const u8 *force_ssid, size_t force_ssid_len,
1186 int pd_before_go_neg)
1188 struct p2p_device *dev;
1190 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1191 "P2P: Request to start group negotiation - peer=" MACSTR
1192 " GO Intent=%d Intended Interface Address=" MACSTR
1193 " wps_method=%d persistent_group=%d pd_before_go_neg=%d",
1194 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1195 wps_method, persistent_group, pd_before_go_neg);
1197 if (p2p_prepare_channel(p2p, force_freq) < 0)
1200 dev = p2p_get_device(p2p, peer_addr);
1201 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1202 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1203 "P2P: Cannot connect to unknown P2P Device " MACSTR,
1204 MAC2STR(peer_addr));
1208 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1209 if (!(dev->info.dev_capab &
1210 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1211 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1212 "P2P: Cannot connect to P2P Device " MACSTR
1213 " that is in a group and is not discoverable",
1214 MAC2STR(peer_addr));
1217 if (dev->oper_freq <= 0) {
1218 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1219 "P2P: Cannot connect to P2P Device " MACSTR
1220 " with incomplete information",
1221 MAC2STR(peer_addr));
1226 * First, try to connect directly. If the peer does not
1227 * acknowledge frames, assume it is sleeping and use device
1228 * discoverability via the GO at that point.
1234 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1235 force_ssid, force_ssid_len);
1236 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1237 p2p->ssid_len = force_ssid_len;
1241 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1242 dev->flags &= ~P2P_DEV_USER_REJECTED;
1243 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1244 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1245 if (pd_before_go_neg)
1246 dev->flags |= P2P_DEV_PD_BEFORE_GO_NEG;
1248 dev->flags &= ~P2P_DEV_PD_BEFORE_GO_NEG;
1249 dev->connect_reqs = 0;
1250 dev->go_neg_req_sent = 0;
1251 dev->go_state = UNKNOWN_GO;
1252 p2p_set_dev_persistent(dev, persistent_group);
1253 p2p->go_intent = go_intent;
1254 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1256 if (p2p->state != P2P_IDLE)
1259 if (p2p->after_scan_tx) {
1261 * We need to drop the pending frame to avoid issues with the
1262 * new GO Negotiation, e.g., when the pending frame was from a
1263 * previous attempt at starting a GO Negotiation.
1265 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
1266 "previous pending Action frame TX that was waiting "
1267 "for p2p_scan completion");
1268 os_free(p2p->after_scan_tx);
1269 p2p->after_scan_tx = NULL;
1272 dev->wps_method = wps_method;
1273 dev->status = P2P_SC_SUCCESS;
1276 dev->flags |= P2P_DEV_FORCE_FREQ;
1278 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1280 if (p2p->p2p_scan_running) {
1281 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1282 "P2P: p2p_scan running - delay connect send");
1283 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1284 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1287 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1289 return p2p_connect_send(p2p, dev);
1293 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1294 enum p2p_wps_method wps_method,
1295 int go_intent, const u8 *own_interface_addr,
1296 unsigned int force_freq, int persistent_group,
1297 const u8 *force_ssid, size_t force_ssid_len)
1299 struct p2p_device *dev;
1301 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1302 "P2P: Request to authorize group negotiation - peer=" MACSTR
1303 " GO Intent=%d Intended Interface Address=" MACSTR
1304 " wps_method=%d persistent_group=%d",
1305 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1306 wps_method, persistent_group);
1308 if (p2p_prepare_channel(p2p, force_freq) < 0)
1311 dev = p2p_get_device(p2p, peer_addr);
1313 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1314 "P2P: Cannot authorize unknown P2P Device " MACSTR,
1315 MAC2STR(peer_addr));
1321 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Forced SSID",
1322 force_ssid, force_ssid_len);
1323 os_memcpy(p2p->ssid, force_ssid, force_ssid_len);
1324 p2p->ssid_len = force_ssid_len;
1328 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1329 dev->flags &= ~P2P_DEV_USER_REJECTED;
1330 dev->go_neg_req_sent = 0;
1331 dev->go_state = UNKNOWN_GO;
1332 p2p_set_dev_persistent(dev, persistent_group);
1333 p2p->go_intent = go_intent;
1334 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1336 dev->wps_method = wps_method;
1337 dev->status = P2P_SC_SUCCESS;
1340 dev->flags |= P2P_DEV_FORCE_FREQ;
1342 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1348 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1349 struct p2p_device *dev, struct p2p_message *msg)
1351 os_get_time(&dev->last_seen);
1353 p2p_copy_wps_info(dev, 0, msg);
1355 if (msg->listen_channel) {
1357 freq = p2p_channel_to_freq((char *) msg->listen_channel,
1358 msg->listen_channel[3],
1359 msg->listen_channel[4]);
1361 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1362 "P2P: Unknown peer Listen channel: "
1363 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1364 msg->listen_channel[0],
1365 msg->listen_channel[1],
1366 msg->listen_channel[2],
1367 msg->listen_channel[3],
1368 msg->listen_channel[4]);
1370 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1371 "peer " MACSTR " Listen channel: %u -> %u MHz",
1372 MAC2STR(dev->info.p2p_device_addr),
1373 dev->listen_freq, freq);
1374 dev->listen_freq = freq;
1378 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1379 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1380 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1381 "P2P: Completed device entry based on data from "
1382 "GO Negotiation Request");
1384 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1385 "P2P: Created device entry based on GO Neg Req: "
1386 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1388 MAC2STR(dev->info.p2p_device_addr),
1389 dev->info.dev_capab, dev->info.group_capab,
1390 dev->info.device_name, dev->listen_freq);
1393 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1395 if (dev->flags & P2P_DEV_USER_REJECTED) {
1396 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1397 "P2P: Do not report rejected device");
1401 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1402 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1403 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1407 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1409 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1410 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1411 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1412 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1413 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1417 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1419 p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len);
1420 p2p_random(params->passphrase, 8);
1425 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1427 struct p2p_go_neg_results res;
1428 int go = peer->go_state == LOCAL_GO;
1429 struct p2p_channels intersection;
1433 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1434 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1435 "GO)", MAC2STR(peer->info.p2p_device_addr),
1436 go ? "local end" : "peer");
1438 os_memset(&res, 0, sizeof(res));
1440 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1441 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1442 res.wps_method = peer->wps_method;
1443 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP) {
1444 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_RECONN)
1445 res.persistent_group = 2;
1447 res.persistent_group = 1;
1451 /* Setup AP mode for WPS provisioning */
1452 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1455 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1456 res.ssid_len = p2p->ssid_len;
1457 p2p_random(res.passphrase, 8);
1459 res.freq = peer->oper_freq;
1460 if (p2p->ssid_len) {
1461 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1462 res.ssid_len = p2p->ssid_len;
1466 p2p_channels_intersect(&p2p->channels, &peer->channels,
1469 for (i = 0; i < intersection.reg_classes; i++) {
1470 struct p2p_reg_class *c = &intersection.reg_class[i];
1471 if (freqs + 1 == P2P_MAX_CHANNELS)
1473 for (j = 0; j < c->channels; j++) {
1475 if (freqs + 1 == P2P_MAX_CHANNELS)
1477 freq = p2p_channel_to_freq(peer->country, c->reg_class,
1481 res.freq_list[freqs++] = freq;
1485 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1487 p2p_clear_timeout(p2p);
1489 peer->go_neg_req_sent = 0;
1490 peer->wps_method = WPS_NOT_READY;
1492 p2p_set_state(p2p, P2P_PROVISIONING);
1493 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1497 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1498 const u8 *data, size_t len, int rx_freq)
1500 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1501 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1502 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1508 case P2P_GO_NEG_REQ:
1509 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1511 case P2P_GO_NEG_RESP:
1512 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1514 case P2P_GO_NEG_CONF:
1515 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1517 case P2P_INVITATION_REQ:
1518 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1521 case P2P_INVITATION_RESP:
1522 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1524 case P2P_PROV_DISC_REQ:
1525 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1527 case P2P_PROV_DISC_RESP:
1528 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1530 case P2P_DEV_DISC_REQ:
1531 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1533 case P2P_DEV_DISC_RESP:
1534 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1537 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1538 "P2P: Unsupported P2P Public Action frame type %d",
1545 static void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da,
1546 const u8 *sa, const u8 *bssid, const u8 *data,
1547 size_t len, int freq)
1553 case WLAN_PA_VENDOR_SPECIFIC:
1558 if (WPA_GET_BE24(data) != OUI_WFA)
1566 if (*data != P2P_OUI_TYPE)
1569 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1571 case WLAN_PA_GAS_INITIAL_REQ:
1572 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1574 case WLAN_PA_GAS_INITIAL_RESP:
1575 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1577 case WLAN_PA_GAS_COMEBACK_REQ:
1578 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1580 case WLAN_PA_GAS_COMEBACK_RESP:
1581 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1587 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1588 const u8 *bssid, u8 category,
1589 const u8 *data, size_t len, int freq)
1591 if (category == WLAN_ACTION_PUBLIC) {
1592 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1596 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1602 if (WPA_GET_BE24(data) != OUI_WFA)
1607 if (*data != P2P_OUI_TYPE)
1612 /* P2P action frame */
1613 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1614 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1615 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1621 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1622 "P2P: Received P2P Action - Notice of Absence");
1625 case P2P_PRESENCE_REQ:
1626 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1628 case P2P_PRESENCE_RESP:
1629 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1631 case P2P_GO_DISC_REQ:
1632 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1635 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1636 "P2P: Received P2P Action - unknown type %u", data[0]);
1642 static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1644 struct p2p_data *p2p = eloop_ctx;
1645 if (p2p->go_neg_peer == NULL)
1647 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1648 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1649 p2p_connect_send(p2p, p2p->go_neg_peer);
1653 static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1655 struct p2p_data *p2p = eloop_ctx;
1656 if (p2p->invite_peer == NULL)
1658 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1659 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1663 static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1664 const u8 *ie, size_t ie_len)
1666 struct p2p_message msg;
1667 struct p2p_device *dev;
1669 os_memset(&msg, 0, sizeof(msg));
1670 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1672 p2p_parse_free(&msg);
1673 return; /* not a P2P probe */
1676 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1677 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1679 /* The Probe Request is not part of P2P Device Discovery. It is
1680 * not known whether the source address of the frame is the P2P
1681 * Device Address or P2P Interface Address. Do not add a new
1682 * peer entry based on this frames.
1684 p2p_parse_free(&msg);
1688 dev = p2p_get_device(p2p, addr);
1690 if (dev->country[0] == 0 && msg.listen_channel)
1691 os_memcpy(dev->country, msg.listen_channel, 3);
1692 os_get_time(&dev->last_seen);
1693 p2p_parse_free(&msg);
1694 return; /* already known */
1697 dev = p2p_create_device(p2p, addr);
1699 p2p_parse_free(&msg);
1703 os_get_time(&dev->last_seen);
1704 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1706 if (msg.listen_channel) {
1707 os_memcpy(dev->country, msg.listen_channel, 3);
1708 dev->listen_freq = p2p_channel_to_freq(dev->country,
1709 msg.listen_channel[3],
1710 msg.listen_channel[4]);
1713 p2p_copy_wps_info(dev, 1, &msg);
1715 p2p_parse_free(&msg);
1717 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1718 "P2P: Created device entry based on Probe Req: " MACSTR
1719 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1720 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1721 dev->info.group_capab, dev->info.device_name,
1726 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1728 struct p2p_message *msg)
1730 struct p2p_device *dev;
1732 dev = p2p_get_device(p2p, addr);
1734 os_get_time(&dev->last_seen);
1735 return dev; /* already known */
1738 dev = p2p_create_device(p2p, addr);
1742 p2p_add_dev_info(p2p, addr, dev, msg);
1748 static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1750 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1752 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1753 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1754 WPA_GET_BE16(&req_dev_type[6]) == 0)
1755 return 1; /* Category match with wildcard OUI/sub-category */
1760 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1761 size_t num_req_dev_type)
1764 for (i = 0; i < num_req_dev_type; i++) {
1765 if (dev_type_match(dev_type, req_dev_type[i]))
1773 * p2p_match_dev_type - Match local device type with requested type
1774 * @p2p: P2P module context from p2p_init()
1775 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1776 * Returns: 1 on match, 0 on mismatch
1778 * This function can be used to match the Requested Device Type attribute in
1779 * WPS IE with the local device types for deciding whether to reply to a Probe
1782 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1784 struct wps_parse_attr attr;
1787 if (wps_parse_msg(wps, &attr))
1788 return 1; /* assume no Requested Device Type attributes */
1790 if (attr.num_req_dev_type == 0)
1791 return 1; /* no Requested Device Type attributes -> match */
1793 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1794 attr.num_req_dev_type))
1795 return 1; /* Own Primary Device Type matches */
1797 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1798 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1800 attr.num_req_dev_type))
1801 return 1; /* Own Secondary Device Type matches */
1803 /* No matching device type found */
1808 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1814 buf = wpabuf_alloc(1000);
1818 if (p2p->go_neg_peer) {
1819 /* Advertise immediate availability of WPS credential */
1820 pw_id = p2p_wps_method_pw_id(p2p->go_neg_peer->wps_method);
1823 p2p_build_wps_ie(p2p, buf, pw_id, 1);
1826 len = p2p_buf_add_ie_hdr(buf);
1827 p2p_buf_add_capability(buf, p2p->dev_capab &
1828 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
1829 if (p2p->ext_listen_interval)
1830 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1831 p2p->ext_listen_interval);
1832 p2p_buf_add_device_info(buf, p2p, NULL);
1833 p2p_buf_update_ie_hdr(buf, len);
1839 static int is_11b(u8 rate)
1841 return rate == 0x02 || rate == 0x04 || rate == 0x0b || rate == 0x16;
1845 static int supp_rates_11b_only(struct ieee802_11_elems *elems)
1847 int num_11b = 0, num_others = 0;
1850 if (elems->supp_rates == NULL && elems->ext_supp_rates == NULL)
1853 for (i = 0; elems->supp_rates && i < elems->supp_rates_len; i++) {
1854 if (is_11b(elems->supp_rates[i]))
1860 for (i = 0; elems->ext_supp_rates && i < elems->ext_supp_rates_len;
1862 if (is_11b(elems->ext_supp_rates[i]))
1868 return num_11b > 0 && num_others == 0;
1872 static enum p2p_probe_req_status
1873 p2p_reply_probe(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
1874 const u8 *bssid, const u8 *ie, size_t ie_len)
1876 struct ieee802_11_elems elems;
1878 struct ieee80211_mgmt *resp;
1879 struct p2p_message msg;
1882 if (!p2p->in_listen || !p2p->drv_in_listen) {
1883 /* not in Listen state - ignore Probe Request */
1884 return P2P_PREQ_NOT_LISTEN;
1887 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
1889 /* Ignore invalid Probe Request frames */
1890 return P2P_PREQ_MALFORMED;
1893 if (elems.p2p == NULL) {
1894 /* not a P2P probe - ignore it */
1895 return P2P_PREQ_NOT_P2P;
1898 if (dst && !is_broadcast_ether_addr(dst) &&
1899 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
1900 /* Not sent to the broadcast address or our P2P Device Address
1902 return P2P_PREQ_NOT_PROCESSED;
1905 if (bssid && !is_broadcast_ether_addr(bssid)) {
1906 /* Not sent to the Wildcard BSSID */
1907 return P2P_PREQ_NOT_PROCESSED;
1910 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
1911 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
1913 /* not using P2P Wildcard SSID - ignore */
1914 return P2P_PREQ_NOT_PROCESSED;
1917 if (supp_rates_11b_only(&elems)) {
1918 /* Indicates support for 11b rates only */
1919 return P2P_PREQ_NOT_P2P;
1922 os_memset(&msg, 0, sizeof(msg));
1923 if (p2p_parse_ies(ie, ie_len, &msg) < 0) {
1924 /* Could not parse P2P attributes */
1925 return P2P_PREQ_NOT_P2P;
1928 if (msg.device_id &&
1929 os_memcmp(msg.device_id, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
1930 /* Device ID did not match */
1931 p2p_parse_free(&msg);
1932 return P2P_PREQ_NOT_PROCESSED;
1935 /* Check Requested Device Type match */
1936 if (msg.wps_attributes &&
1937 !p2p_match_dev_type(p2p, msg.wps_attributes)) {
1938 /* No match with Requested Device Type */
1939 p2p_parse_free(&msg);
1940 return P2P_PREQ_NOT_PROCESSED;
1942 p2p_parse_free(&msg);
1944 if (!p2p->cfg->send_probe_resp) {
1945 /* Response generated elsewhere */
1946 return P2P_PREQ_NOT_PROCESSED;
1949 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1950 "P2P: Reply to P2P Probe Request in Listen state");
1953 * We do not really have a specific BSS that this frame is advertising,
1954 * so build a frame that has some information in valid format. This is
1955 * really only used for discovery purposes, not to learn exact BSS
1958 ies = p2p_build_probe_resp_ies(p2p);
1960 return P2P_PREQ_NOT_PROCESSED;
1962 buf = wpabuf_alloc(200 + wpabuf_len(ies));
1965 return P2P_PREQ_NOT_PROCESSED;
1969 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
1971 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
1972 (WLAN_FC_STYPE_PROBE_RESP << 4));
1973 os_memcpy(resp->da, addr, ETH_ALEN);
1974 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
1975 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
1976 resp->u.probe_resp.beacon_int = host_to_le16(100);
1977 /* hardware or low-level driver will setup seq_ctrl and timestamp */
1978 resp->u.probe_resp.capab_info =
1979 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
1980 WLAN_CAPABILITY_PRIVACY |
1981 WLAN_CAPABILITY_SHORT_SLOT_TIME);
1983 wpabuf_put_u8(buf, WLAN_EID_SSID);
1984 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
1985 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1987 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
1988 wpabuf_put_u8(buf, 8);
1989 wpabuf_put_u8(buf, (60 / 5) | 0x80);
1990 wpabuf_put_u8(buf, 90 / 5);
1991 wpabuf_put_u8(buf, (120 / 5) | 0x80);
1992 wpabuf_put_u8(buf, 180 / 5);
1993 wpabuf_put_u8(buf, (240 / 5) | 0x80);
1994 wpabuf_put_u8(buf, 360 / 5);
1995 wpabuf_put_u8(buf, 480 / 5);
1996 wpabuf_put_u8(buf, 540 / 5);
1998 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
1999 wpabuf_put_u8(buf, 1);
2000 wpabuf_put_u8(buf, p2p->cfg->channel);
2002 wpabuf_put_buf(buf, ies);
2005 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
2009 return P2P_PREQ_NOT_PROCESSED;
2013 enum p2p_probe_req_status
2014 p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
2015 const u8 *bssid, const u8 *ie, size_t ie_len)
2017 enum p2p_probe_req_status res;
2019 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
2021 res = p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
2023 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
2025 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
2027 /* Received a Probe Request from GO Negotiation peer */
2028 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2029 "P2P: Found GO Negotiation peer - try to start GO "
2030 "negotiation from timeout");
2031 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
2032 return P2P_PREQ_PROCESSED;
2035 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
2037 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
2039 /* Received a Probe Request from Invite peer */
2040 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2041 "P2P: Found Invite peer - try to start Invite from "
2043 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
2044 return P2P_PREQ_PROCESSED;
2051 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
2052 u8 *buf, size_t len, struct wpabuf *p2p_ie)
2061 return 0; /* WLAN AP is not a P2P manager */
2064 * (Re)Association Request - P2P IE
2065 * P2P Capability attribute (shall be present)
2066 * P2P Interface attribute (present if concurrent device and
2067 * P2P Management is enabled)
2069 tmp = wpabuf_alloc(200);
2073 lpos = p2p_buf_add_ie_hdr(tmp);
2075 if (p2p->num_groups > 0) {
2076 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
2077 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2078 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
2080 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
2082 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
2083 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
2084 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
2085 p2p_buf_add_p2p_interface(tmp, p2p);
2086 p2p_buf_update_ie_hdr(tmp, lpos);
2088 tmplen = wpabuf_len(tmp);
2092 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2101 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
2102 size_t len, int p2p_group, struct wpabuf *p2p_ie)
2106 struct p2p_device *peer;
2111 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
2114 * (Re)Association Request - P2P IE
2115 * P2P Capability attribute (shall be present)
2116 * Extended Listen Timing (may be present)
2117 * P2P Device Info attribute (shall be present)
2119 tmp = wpabuf_alloc(200);
2123 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
2125 lpos = p2p_buf_add_ie_hdr(tmp);
2126 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
2127 if (p2p->ext_listen_interval)
2128 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
2129 p2p->ext_listen_interval);
2130 p2p_buf_add_device_info(tmp, p2p, peer);
2131 p2p_buf_update_ie_hdr(tmp, lpos);
2133 tmplen = wpabuf_len(tmp);
2137 os_memcpy(buf, wpabuf_head(tmp), tmplen);
2146 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
2148 struct wpabuf *p2p_ie;
2151 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
2155 ret = p2p_attr_text(p2p_ie, buf, end);
2156 wpabuf_free(p2p_ie);
2161 int p2p_parse_dev_addr_in_p2p_ie(struct wpabuf *p2p_ie, u8 *dev_addr)
2163 struct p2p_message msg;
2165 os_memset(&msg, 0, sizeof(msg));
2166 if (p2p_parse_p2p_ie(p2p_ie, &msg))
2169 if (msg.p2p_device_addr) {
2170 os_memcpy(dev_addr, msg.p2p_device_addr, ETH_ALEN);
2172 } else if (msg.device_id) {
2173 os_memcpy(dev_addr, msg.device_id, ETH_ALEN);
2180 int p2p_parse_dev_addr(const u8 *ies, size_t ies_len, u8 *dev_addr)
2182 struct wpabuf *p2p_ie;
2185 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len,
2186 P2P_IE_VENDOR_TYPE);
2189 ret = p2p_parse_dev_addr_in_p2p_ie(p2p_ie, dev_addr);
2190 wpabuf_free(p2p_ie);
2195 static void p2p_clear_go_neg(struct p2p_data *p2p)
2197 p2p->go_neg_peer = NULL;
2198 p2p_clear_timeout(p2p);
2199 p2p_set_state(p2p, P2P_IDLE);
2203 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
2205 if (p2p->go_neg_peer == NULL) {
2206 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2207 "P2P: No pending Group Formation - "
2208 "ignore WPS registration success notification");
2209 return; /* No pending Group Formation */
2212 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
2214 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2215 "P2P: Ignore WPS registration success notification "
2216 "for " MACSTR " (GO Negotiation peer " MACSTR ")",
2218 MAC2STR(p2p->go_neg_peer->intended_addr));
2219 return; /* Ignore unexpected peer address */
2222 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2223 "P2P: Group Formation completed successfully with " MACSTR,
2226 p2p_clear_go_neg(p2p);
2230 void p2p_group_formation_failed(struct p2p_data *p2p)
2232 if (p2p->go_neg_peer == NULL) {
2233 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2234 "P2P: No pending Group Formation - "
2235 "ignore group formation failure notification");
2236 return; /* No pending Group Formation */
2239 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2240 "P2P: Group Formation failed with " MACSTR,
2241 MAC2STR(p2p->go_neg_peer->intended_addr));
2243 p2p_clear_go_neg(p2p);
2247 struct p2p_data * p2p_init(const struct p2p_config *cfg)
2249 struct p2p_data *p2p;
2251 if (cfg->max_peers < 1)
2254 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
2257 p2p->cfg = (struct p2p_config *) (p2p + 1);
2258 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
2260 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
2261 if (cfg->manufacturer)
2262 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
2263 if (cfg->model_name)
2264 p2p->cfg->model_name = os_strdup(cfg->model_name);
2265 if (cfg->model_number)
2266 p2p->cfg->model_number = os_strdup(cfg->model_number);
2267 if (cfg->serial_number)
2268 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
2269 if (cfg->pref_chan) {
2270 p2p->cfg->pref_chan = os_malloc(cfg->num_pref_chan *
2271 sizeof(struct p2p_channel));
2272 if (p2p->cfg->pref_chan) {
2273 os_memcpy(p2p->cfg->pref_chan, cfg->pref_chan,
2274 cfg->num_pref_chan *
2275 sizeof(struct p2p_channel));
2277 p2p->cfg->num_pref_chan = 0;
2280 p2p->min_disc_int = 1;
2281 p2p->max_disc_int = 3;
2283 os_get_random(&p2p->next_tie_breaker, 1);
2284 p2p->next_tie_breaker &= 0x01;
2285 if (cfg->sd_request)
2286 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
2287 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
2288 if (cfg->concurrent_operations)
2289 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
2290 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
2292 dl_list_init(&p2p->devices);
2294 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
2295 p2p_expiration_timeout, p2p, NULL);
2297 p2p->go_timeout = 100;
2298 p2p->client_timeout = 20;
2304 void p2p_deinit(struct p2p_data *p2p)
2306 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
2307 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2308 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2310 p2p_free_req_dev_types(p2p);
2311 os_free(p2p->cfg->dev_name);
2312 os_free(p2p->cfg->manufacturer);
2313 os_free(p2p->cfg->model_name);
2314 os_free(p2p->cfg->model_number);
2315 os_free(p2p->cfg->serial_number);
2316 os_free(p2p->cfg->pref_chan);
2317 os_free(p2p->groups);
2318 wpabuf_free(p2p->sd_resp);
2319 os_free(p2p->after_scan_tx);
2320 p2p_remove_wps_vendor_extensions(p2p);
2325 void p2p_flush(struct p2p_data *p2p)
2327 struct p2p_device *dev, *prev;
2329 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2331 dl_list_del(&dev->list);
2332 p2p_device_free(p2p, dev);
2334 p2p_free_sd_queries(p2p);
2335 os_free(p2p->after_scan_tx);
2336 p2p->after_scan_tx = NULL;
2340 int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2342 struct p2p_device *dev;
2344 dev = p2p_get_device(p2p, addr);
2348 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
2351 if (p2p->go_neg_peer == dev)
2352 p2p->go_neg_peer = NULL;
2354 dev->wps_method = WPS_NOT_READY;
2355 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2356 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2358 /* Check if after_scan_tx is for this peer. If so free it */
2359 if (p2p->after_scan_tx &&
2360 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2361 os_free(p2p->after_scan_tx);
2362 p2p->after_scan_tx = NULL;
2369 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2371 os_free(p2p->cfg->dev_name);
2373 p2p->cfg->dev_name = os_strdup(dev_name);
2374 if (p2p->cfg->dev_name == NULL)
2377 p2p->cfg->dev_name = NULL;
2382 int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2384 os_free(p2p->cfg->manufacturer);
2385 p2p->cfg->manufacturer = NULL;
2387 p2p->cfg->manufacturer = os_strdup(manufacturer);
2388 if (p2p->cfg->manufacturer == NULL)
2396 int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2398 os_free(p2p->cfg->model_name);
2399 p2p->cfg->model_name = NULL;
2401 p2p->cfg->model_name = os_strdup(model_name);
2402 if (p2p->cfg->model_name == NULL)
2410 int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2412 os_free(p2p->cfg->model_number);
2413 p2p->cfg->model_number = NULL;
2415 p2p->cfg->model_number = os_strdup(model_number);
2416 if (p2p->cfg->model_number == NULL)
2424 int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2426 os_free(p2p->cfg->serial_number);
2427 p2p->cfg->serial_number = NULL;
2428 if (serial_number) {
2429 p2p->cfg->serial_number = os_strdup(serial_number);
2430 if (p2p->cfg->serial_number == NULL)
2438 void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2440 p2p->cfg->config_methods = config_methods;
2444 void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2446 os_memcpy(p2p->cfg->uuid, uuid, 16);
2450 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2452 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2457 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2458 size_t num_dev_types)
2460 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2461 num_dev_types = P2P_SEC_DEVICE_TYPES;
2462 p2p->cfg->num_sec_dev_types = num_dev_types;
2463 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2468 void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2472 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2473 wpabuf_free(p2p->wps_vendor_ext[i]);
2474 p2p->wps_vendor_ext[i] = NULL;
2479 int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2480 const struct wpabuf *vendor_ext)
2484 if (vendor_ext == NULL)
2487 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2488 if (p2p->wps_vendor_ext[i] == NULL)
2491 if (i >= P2P_MAX_WPS_VENDOR_EXT)
2494 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2495 if (p2p->wps_vendor_ext[i] == NULL)
2502 int p2p_set_country(struct p2p_data *p2p, const char *country)
2504 os_memcpy(p2p->cfg->country, country, 3);
2509 void p2p_continue_find(struct p2p_data *p2p)
2511 struct p2p_device *dev;
2512 p2p_set_state(p2p, P2P_SEARCH);
2513 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2514 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2515 if (p2p_start_sd(p2p, dev) == 0)
2519 } else if (dev->req_config_methods &&
2520 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2521 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2522 "pending Provision Discovery Request to "
2523 MACSTR " (config methods 0x%x)",
2524 MAC2STR(dev->info.p2p_device_addr),
2525 dev->req_config_methods);
2526 if (p2p_send_prov_disc_req(p2p, dev, 0, 0) == 0)
2531 p2p_listen_in_find(p2p);
2535 static void p2p_sd_cb(struct p2p_data *p2p, int success)
2537 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2538 "P2P: Service Discovery Query TX callback: success=%d",
2540 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2544 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2545 p2p->sd_peer = NULL;
2547 p2p_continue_find(p2p);
2551 if (p2p->sd_peer == NULL) {
2552 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2553 "P2P: No SD peer entry known");
2554 p2p_continue_find(p2p);
2558 /* Wait for response from the peer */
2559 p2p_set_state(p2p, P2P_SD_DURING_FIND);
2560 p2p_set_timeout(p2p, 0, 200000);
2565 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2566 * @p2p: P2P module context from p2p_init()
2568 static void p2p_retry_pd(struct p2p_data *p2p)
2570 struct p2p_device *dev;
2572 if (p2p->state != P2P_IDLE)
2576 * Retry the prov disc req attempt only for the peer that the user had
2577 * requested for and provided a join has not been initiated on it
2581 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2582 if (os_memcmp(p2p->pending_pd_devaddr,
2583 dev->info.p2p_device_addr, ETH_ALEN) != 0)
2585 if (!dev->req_config_methods)
2587 if (dev->flags & P2P_DEV_PD_FOR_JOIN)
2590 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2591 "pending Provision Discovery Request to "
2592 MACSTR " (config methods 0x%x)",
2593 MAC2STR(dev->info.p2p_device_addr),
2594 dev->req_config_methods);
2595 p2p_send_prov_disc_req(p2p, dev, 0, 0);
2601 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2603 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2604 "P2P: Provision Discovery Request TX callback: success=%d",
2608 * Postpone resetting the pending action state till after we actually
2609 * time out. This allows us to take some action like notifying any
2610 * interested parties about no response to the request.
2612 * When the timer (below) goes off we check in IDLE, SEARCH, or
2613 * LISTEN_ONLY state, which are the only allowed states to issue a PD
2614 * requests in, if this was still pending and then raise notification.
2618 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2620 if (p2p->user_initiated_pd &&
2621 (p2p->state == P2P_SEARCH || p2p->state == P2P_LISTEN_ONLY))
2623 /* Retry request from timeout to avoid busy loops */
2624 p2p->pending_action_state = P2P_PENDING_PD;
2625 p2p_set_timeout(p2p, 0, 50000);
2626 } else if (p2p->state != P2P_IDLE)
2627 p2p_continue_find(p2p);
2628 else if (p2p->user_initiated_pd) {
2629 p2p->pending_action_state = P2P_PENDING_PD;
2630 p2p_set_timeout(p2p, 0, 300000);
2636 * This postponing, of resetting pending_action_state, needs to be
2637 * done only for user initiated PD requests and not internal ones.
2639 if (p2p->user_initiated_pd)
2640 p2p->pending_action_state = P2P_PENDING_PD;
2642 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2644 /* Wait for response from the peer */
2645 if (p2p->state == P2P_SEARCH)
2646 p2p_set_state(p2p, P2P_PD_DURING_FIND);
2647 p2p_set_timeout(p2p, 0, 200000);
2651 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
2652 int level, const u8 *ies, size_t ies_len)
2654 p2p_add_device(p2p, bssid, freq, level, ies, ies_len, 1);
2660 void p2p_scan_res_handled(struct p2p_data *p2p)
2662 if (!p2p->p2p_scan_running) {
2663 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
2664 "running, but scan results received");
2666 p2p->p2p_scan_running = 0;
2667 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2669 if (p2p_run_after_scan(p2p))
2671 if (p2p->state == P2P_SEARCH)
2672 p2p_continue_find(p2p);
2676 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies, const u8 *dev_id)
2678 u8 *len = p2p_buf_add_ie_hdr(ies);
2679 p2p_buf_add_capability(ies, p2p->dev_capab &
2680 ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY, 0);
2682 p2p_buf_add_device_id(ies, dev_id);
2683 if (p2p->cfg->reg_class && p2p->cfg->channel)
2684 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2685 p2p->cfg->reg_class,
2687 if (p2p->ext_listen_interval)
2688 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2689 p2p->ext_listen_interval);
2690 /* TODO: p2p_buf_add_operating_channel() if GO */
2691 p2p_buf_update_ie_hdr(ies, len);
2695 size_t p2p_scan_ie_buf_len(struct p2p_data *p2p)
2701 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2703 return p2p_attr_text(p2p_ie, buf, end);
2707 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2709 struct p2p_device *dev = p2p->go_neg_peer;
2711 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2712 "P2P: GO Negotiation Request TX callback: success=%d",
2716 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2717 "P2P: No pending GO Negotiation");
2722 if (dev->flags & P2P_DEV_USER_REJECTED) {
2723 p2p_set_state(p2p, P2P_IDLE);
2726 } else if (dev->go_neg_req_sent) {
2727 /* Cancel the increment from p2p_connect_send() on failure */
2728 dev->go_neg_req_sent--;
2732 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2733 !is_zero_ether_addr(dev->member_in_go_dev)) {
2734 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2735 "P2P: Peer " MACSTR " did not acknowledge request - "
2736 "try to use device discoverability through its GO",
2737 MAC2STR(dev->info.p2p_device_addr));
2738 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2739 p2p_send_dev_disc_req(p2p, dev);
2744 * Use P2P find, if needed, to find the other device from its listen
2747 p2p_set_state(p2p, P2P_CONNECT);
2748 p2p_set_timeout(p2p, 0, success ? 200000 : 100000);
2752 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2754 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2755 "P2P: GO Negotiation Response TX callback: success=%d",
2757 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2758 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2759 "P2P: Ignore TX callback event - GO Negotiation is "
2760 "not running anymore");
2763 p2p_set_state(p2p, P2P_CONNECT);
2764 p2p_set_timeout(p2p, 0, 250000);
2768 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2770 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2771 "P2P: GO Negotiation Response (failure) TX callback: "
2772 "success=%d", success);
2773 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2774 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2775 p2p->go_neg_peer->status);
2780 static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
2781 enum p2p_send_action_result result)
2783 struct p2p_device *dev;
2785 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2786 "P2P: GO Negotiation Confirm TX callback: result=%d",
2788 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2789 if (result == P2P_SEND_ACTION_FAILED) {
2790 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2793 if (result == P2P_SEND_ACTION_NO_ACK) {
2795 * It looks like the TX status for GO Negotiation Confirm is
2796 * often showing failure even when the peer has actually
2797 * received the frame. Since the peer may change channels
2798 * immediately after having received the frame, we may not see
2799 * an Ack for retries, so just dropping a single frame may
2800 * trigger this. To allow the group formation to succeed if the
2801 * peer did indeed receive the frame, continue regardless of
2804 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2805 "P2P: Assume GO Negotiation Confirm TX was actually "
2806 "received by the peer even though Ack was not "
2810 dev = p2p->go_neg_peer;
2814 p2p_go_complete(p2p, dev);
2818 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
2819 const u8 *src, const u8 *bssid,
2820 enum p2p_send_action_result result)
2822 enum p2p_pending_action_state state;
2825 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2826 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
2827 " src=" MACSTR " bssid=" MACSTR " result=%d",
2828 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
2829 MAC2STR(bssid), result);
2830 success = result == P2P_SEND_ACTION_SUCCESS;
2831 state = p2p->pending_action_state;
2832 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2834 case P2P_NO_PENDING_ACTION:
2836 case P2P_PENDING_GO_NEG_REQUEST:
2837 p2p_go_neg_req_cb(p2p, success);
2839 case P2P_PENDING_GO_NEG_RESPONSE:
2840 p2p_go_neg_resp_cb(p2p, success);
2842 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
2843 p2p_go_neg_resp_failure_cb(p2p, success);
2845 case P2P_PENDING_GO_NEG_CONFIRM:
2846 p2p_go_neg_conf_cb(p2p, result);
2848 case P2P_PENDING_SD:
2849 p2p_sd_cb(p2p, success);
2851 case P2P_PENDING_PD:
2852 p2p_prov_disc_cb(p2p, success);
2854 case P2P_PENDING_INVITATION_REQUEST:
2855 p2p_invitation_req_cb(p2p, success);
2857 case P2P_PENDING_INVITATION_RESPONSE:
2858 p2p_invitation_resp_cb(p2p, success);
2860 case P2P_PENDING_DEV_DISC_REQUEST:
2861 p2p_dev_disc_req_cb(p2p, success);
2863 case P2P_PENDING_DEV_DISC_RESPONSE:
2864 p2p_dev_disc_resp_cb(p2p, success);
2866 case P2P_PENDING_GO_DISC_REQ:
2867 p2p_go_disc_req_cb(p2p, success);
2873 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
2874 unsigned int duration)
2876 if (freq == p2p->pending_client_disc_freq) {
2877 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2878 "P2P: Client discoverability remain-awake completed");
2879 p2p->pending_client_disc_freq = 0;
2883 if (freq != p2p->pending_listen_freq) {
2884 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2885 "P2P: Unexpected listen callback for freq=%u "
2886 "duration=%u (pending_listen_freq=%u)",
2887 freq, duration, p2p->pending_listen_freq);
2891 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2892 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
2894 p2p->pending_listen_sec, p2p->pending_listen_usec,
2895 p2p->pending_listen_freq);
2897 p2p->drv_in_listen = freq;
2898 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
2900 * Add 20 msec extra wait to avoid race condition with driver
2901 * remain-on-channel end event, i.e., give driver more time to
2902 * complete the operation before our timeout expires.
2904 p2p_set_timeout(p2p, p2p->pending_listen_sec,
2905 p2p->pending_listen_usec + 20000);
2908 p2p->pending_listen_freq = 0;
2912 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
2914 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
2915 "state (freq=%u)", freq);
2916 p2p->drv_in_listen = 0;
2918 return 0; /* Internal timeout will trigger the next step */
2920 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
2921 if (p2p->go_neg_peer->connect_reqs >= 120) {
2922 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2923 "P2P: Timeout on sending GO Negotiation "
2924 "Request without getting response");
2925 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2929 p2p_set_state(p2p, P2P_CONNECT);
2930 p2p_connect_send(p2p, p2p->go_neg_peer);
2932 } else if (p2p->state == P2P_SEARCH) {
2933 if (p2p->p2p_scan_running) {
2935 * Search is already in progress. This can happen if
2936 * an Action frame RX is reported immediately after
2937 * the end of a remain-on-channel operation and the
2938 * response frame to that is sent using an offchannel
2939 * operation while in p2p_find. Avoid an attempt to
2940 * restart a scan here.
2942 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan "
2943 "already in progress - do not try to start a "
2947 if (p2p->pending_listen_freq) {
2949 * Better wait a bit if the driver is unable to start
2950 * offchannel operation for some reason. p2p_search()
2951 * will be started from internal timeout.
2953 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Listen "
2954 "operation did not seem to start - delay "
2955 "search phase to avoid busy loop");
2956 p2p_set_timeout(p2p, 0, 100000);
2959 if (p2p->search_delay) {
2960 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
2961 "search operation by %u ms",
2963 p2p_set_timeout(p2p, p2p->search_delay / 1000,
2964 (p2p->search_delay % 1000) * 1000);
2975 static void p2p_timeout_connect(struct p2p_data *p2p)
2977 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2978 if (p2p->go_neg_peer &&
2979 (p2p->go_neg_peer->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM)) {
2980 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Wait for GO "
2981 "Negotiation Confirm timed out - assume GO "
2982 "Negotiation failed");
2983 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2986 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
2987 p2p_listen_in_find(p2p);
2991 static void p2p_timeout_connect_listen(struct p2p_data *p2p)
2993 if (p2p->go_neg_peer) {
2994 if (p2p->drv_in_listen) {
2995 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
2996 "still in Listen state; wait for it to "
3001 if (p2p->go_neg_peer->connect_reqs >= 120) {
3002 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3003 "P2P: Timeout on sending GO Negotiation "
3004 "Request without getting response");
3005 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
3009 p2p_set_state(p2p, P2P_CONNECT);
3010 p2p_connect_send(p2p, p2p->go_neg_peer);
3012 p2p_set_state(p2p, P2P_IDLE);
3016 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
3019 * TODO: could remain constantly in Listen state for some time if there
3020 * are no other concurrent uses for the radio. For now, go to listen
3021 * state once per second to give other uses a chance to use the radio.
3023 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
3024 p2p_set_timeout(p2p, 0, 500000);
3028 static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
3030 struct p2p_device *dev = p2p->go_neg_peer;
3033 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3034 "P2P: Unknown GO Neg peer - stop GO Neg wait");
3039 if (dev->wait_count >= 120) {
3040 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3041 "P2P: Timeout on waiting peer to become ready for GO "
3043 p2p_go_neg_failed(p2p, dev, -1);
3047 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3048 "P2P: Go to Listen state while waiting for the peer to become "
3049 "ready for GO Negotiation");
3050 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
3051 p2p_listen_in_find(p2p);
3055 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
3057 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3058 "P2P: Service Discovery Query timeout");
3060 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3061 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
3062 p2p->sd_peer = NULL;
3064 p2p_continue_find(p2p);
3068 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
3070 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3071 "P2P: Provision Discovery Request timeout");
3072 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3073 p2p_continue_find(p2p);
3077 static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
3079 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3082 * For user initiated PD requests that we have not gotten any responses
3083 * for while in IDLE state, we retry them a couple of times before
3086 if (!p2p->user_initiated_pd)
3089 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3090 "P2P: User initiated Provision Discovery Request timeout");
3092 if (p2p->pd_retries) {
3096 if (p2p->cfg->prov_disc_fail)
3097 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
3098 p2p->pending_pd_devaddr,
3099 P2P_PROV_DISC_TIMEOUT);
3100 p2p_reset_pending_pd(p2p);
3105 static void p2p_timeout_invite(struct p2p_data *p2p)
3107 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
3108 p2p_set_state(p2p, P2P_INVITE_LISTEN);
3109 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
3111 * Better remain on operating channel instead of listen channel
3112 * when running a group.
3114 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
3115 "active GO role - wait on operating channel");
3116 p2p_set_timeout(p2p, 0, 100000);
3119 p2p_listen_in_find(p2p);
3123 static void p2p_timeout_invite_listen(struct p2p_data *p2p)
3125 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
3126 p2p_set_state(p2p, P2P_INVITE);
3127 p2p_invite_send(p2p, p2p->invite_peer,
3128 p2p->invite_go_dev_addr);
3130 if (p2p->invite_peer) {
3131 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3132 "P2P: Invitation Request retry limit reached");
3133 if (p2p->cfg->invitation_result)
3134 p2p->cfg->invitation_result(
3135 p2p->cfg->cb_ctx, -1, NULL);
3137 p2p_set_state(p2p, P2P_IDLE);
3142 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
3144 struct p2p_data *p2p = eloop_ctx;
3146 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
3147 p2p_state_txt(p2p->state));
3151 switch (p2p->state) {
3153 /* Check if we timed out waiting for PD req */
3154 if (p2p->pending_action_state == P2P_PENDING_PD)
3155 p2p_timeout_prov_disc_req(p2p);
3158 /* Check if we timed out waiting for PD req */
3159 if (p2p->pending_action_state == P2P_PENDING_PD)
3160 p2p_timeout_prov_disc_req(p2p);
3161 if (p2p->search_delay && !p2p->in_search_delay) {
3162 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay "
3163 "search operation by %u ms",
3165 p2p->in_search_delay = 1;
3166 p2p_set_timeout(p2p, p2p->search_delay / 1000,
3167 (p2p->search_delay % 1000) * 1000);
3170 p2p->in_search_delay = 0;
3174 p2p_timeout_connect(p2p);
3176 case P2P_CONNECT_LISTEN:
3177 p2p_timeout_connect_listen(p2p);
3181 case P2P_LISTEN_ONLY:
3182 /* Check if we timed out waiting for PD req */
3183 if (p2p->pending_action_state == P2P_PENDING_PD)
3184 p2p_timeout_prov_disc_req(p2p);
3186 if (p2p->ext_listen_only) {
3187 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3188 "P2P: Extended Listen Timing - Listen State "
3190 p2p->ext_listen_only = 0;
3191 p2p_set_state(p2p, P2P_IDLE);
3194 case P2P_WAIT_PEER_CONNECT:
3195 p2p_timeout_wait_peer_connect(p2p);
3197 case P2P_WAIT_PEER_IDLE:
3198 p2p_timeout_wait_peer_idle(p2p);
3200 case P2P_SD_DURING_FIND:
3201 p2p_timeout_sd_during_find(p2p);
3203 case P2P_PROVISIONING:
3205 case P2P_PD_DURING_FIND:
3206 p2p_timeout_prov_disc_during_find(p2p);
3209 p2p_timeout_invite(p2p);
3211 case P2P_INVITE_LISTEN:
3212 p2p_timeout_invite_listen(p2p);
3214 case P2P_SEARCH_WHEN_READY:
3216 case P2P_CONTINUE_SEARCH_WHEN_READY:
3222 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
3224 struct p2p_device *dev;
3226 dev = p2p_get_device(p2p, peer_addr);
3227 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
3228 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
3230 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
3231 " unknown", MAC2STR(peer_addr));
3234 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
3235 dev->flags |= P2P_DEV_USER_REJECTED;
3240 const char * p2p_wps_method_text(enum p2p_wps_method method)
3245 case WPS_PIN_DISPLAY:
3247 case WPS_PIN_KEYPAD:
3257 static const char * p2p_go_state_text(enum p2p_go_state go_state)
3272 const struct p2p_peer_info * p2p_get_peer_info(struct p2p_data *p2p,
3273 const u8 *addr, int next)
3275 struct p2p_device *dev;
3278 dev = p2p_get_device(p2p, addr);
3280 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3283 dev = dl_list_first(&dev->list, struct p2p_device, list);
3284 if (&dev->list == &p2p->devices)
3295 int p2p_get_peer_info_txt(const struct p2p_peer_info *info,
3296 char *buf, size_t buflen)
3298 struct p2p_device *dev;
3306 dev = (struct p2p_device *) (((u8 *) info) -
3307 offsetof(struct p2p_device, info));
3313 res = os_snprintf(pos, end - pos,
3317 "interface_addr=" MACSTR "\n"
3318 "member_in_go_dev=" MACSTR "\n"
3319 "member_in_go_iface=" MACSTR "\n"
3320 "go_neg_req_sent=%d\n"
3323 "intended_addr=" MACSTR "\n"
3326 "req_config_methods=0x%x\n"
3327 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
3330 "invitation_reqs=%u\n",
3331 (int) (now.sec - dev->last_seen.sec),
3333 p2p_wps_method_text(dev->wps_method),
3334 MAC2STR(dev->interface_addr),
3335 MAC2STR(dev->member_in_go_dev),
3336 MAC2STR(dev->member_in_go_iface),
3337 dev->go_neg_req_sent,
3338 p2p_go_state_text(dev->go_state),
3340 MAC2STR(dev->intended_addr),
3341 dev->country[0] ? dev->country[0] : '_',
3342 dev->country[1] ? dev->country[1] : '_',
3344 dev->req_config_methods,
3345 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
3346 "[PROBE_REQ_ONLY]" : "",
3347 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
3348 dev->flags & P2P_DEV_NOT_YET_READY ?
3349 "[NOT_YET_READY]" : "",
3350 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
3351 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
3353 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
3354 "[PD_PEER_DISPLAY]" : "",
3355 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
3356 "[PD_PEER_KEYPAD]" : "",
3357 dev->flags & P2P_DEV_USER_REJECTED ?
3358 "[USER_REJECTED]" : "",
3359 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
3360 "[PEER_WAITING_RESPONSE]" : "",
3361 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
3362 "[PREFER_PERSISTENT_GROUP]" : "",
3363 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
3364 "[WAIT_GO_NEG_RESPONSE]" : "",
3365 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
3366 "[WAIT_GO_NEG_CONFIRM]" : "",
3367 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
3368 "[GROUP_CLIENT_ONLY]" : "",
3369 dev->flags & P2P_DEV_FORCE_FREQ ?
3370 "[FORCE_FREQ]" : "",
3371 dev->flags & P2P_DEV_PD_FOR_JOIN ?
3372 "[PD_FOR_JOIN]" : "",
3375 dev->invitation_reqs);
3376 if (res < 0 || res >= end - pos)
3380 if (dev->ext_listen_period) {
3381 res = os_snprintf(pos, end - pos,
3382 "ext_listen_period=%u\n"
3383 "ext_listen_interval=%u\n",
3384 dev->ext_listen_period,
3385 dev->ext_listen_interval);
3386 if (res < 0 || res >= end - pos)
3391 if (dev->oper_ssid_len) {
3392 res = os_snprintf(pos, end - pos,
3394 wpa_ssid_txt(dev->oper_ssid,
3395 dev->oper_ssid_len));
3396 if (res < 0 || res >= end - pos)
3405 int p2p_peer_known(struct p2p_data *p2p, const u8 *addr)
3407 return p2p_get_device(p2p, addr) != NULL;
3411 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
3414 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3415 "discoverability enabled");
3416 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3418 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3419 "discoverability disabled");
3420 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3425 static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
3426 u32 duration2, u32 interval2)
3429 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
3432 req = wpabuf_alloc(100);
3436 if (duration1 || interval1) {
3437 os_memset(&desc1, 0, sizeof(desc1));
3438 desc1.count_type = 1;
3439 desc1.duration = duration1;
3440 desc1.interval = interval1;
3443 if (duration2 || interval2) {
3444 os_memset(&desc2, 0, sizeof(desc2));
3445 desc2.count_type = 2;
3446 desc2.duration = duration2;
3447 desc2.interval = interval2;
3452 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
3453 len = p2p_buf_add_ie_hdr(req);
3454 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
3455 p2p_buf_update_ie_hdr(req, len);
3461 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
3462 const u8 *own_interface_addr, unsigned int freq,
3463 u32 duration1, u32 interval1, u32 duration2,
3468 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
3469 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
3470 "int1=%u dur2=%u int2=%u",
3471 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
3472 freq, duration1, interval1, duration2, interval2);
3474 req = p2p_build_presence_req(duration1, interval1, duration2,
3479 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3480 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3482 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
3483 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3484 "P2P: Failed to send Action frame");
3492 static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3493 size_t noa_len, u8 dialog_token)
3495 struct wpabuf *resp;
3498 resp = wpabuf_alloc(100 + noa_len);
3502 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3503 len = p2p_buf_add_ie_hdr(resp);
3504 p2p_buf_add_status(resp, status);
3506 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3507 wpabuf_put_le16(resp, noa_len);
3508 wpabuf_put_data(resp, noa, noa_len);
3510 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3511 p2p_buf_update_ie_hdr(resp, len);
3517 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3518 const u8 *sa, const u8 *data, size_t len,
3521 struct p2p_message msg;
3523 struct wpabuf *resp;
3525 struct p2p_group *group = NULL;
3530 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3531 "P2P: Received P2P Action - P2P Presence Request");
3533 for (g = 0; g < p2p->num_groups; g++) {
3534 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3536 group = p2p->groups[g];
3540 if (group == NULL) {
3541 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3542 "P2P: Ignore P2P Presence Request for unknown group "
3543 MACSTR, MAC2STR(da));
3547 if (p2p_parse(data, len, &msg) < 0) {
3548 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3549 "P2P: Failed to parse P2P Presence Request");
3550 status = P2P_SC_FAIL_INVALID_PARAMS;
3555 if (msg.noa == NULL) {
3556 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3557 "P2P: No NoA attribute in P2P Presence Request");
3558 status = P2P_SC_FAIL_INVALID_PARAMS;
3562 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3565 if (p2p->cfg->get_noa)
3566 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3570 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3571 noa_len > 0 ? noa_len : 0,
3574 p2p_parse_free(&msg);
3578 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3579 if (p2p_send_action(p2p, rx_freq, sa, da, da,
3580 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
3581 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3582 "P2P: Failed to send Action frame");
3588 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3589 const u8 *sa, const u8 *data, size_t len)
3591 struct p2p_message msg;
3593 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3594 "P2P: Received P2P Action - P2P Presence Response");
3596 if (p2p_parse(data, len, &msg) < 0) {
3597 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3598 "P2P: Failed to parse P2P Presence Response");
3602 if (msg.status == NULL || msg.noa == NULL) {
3603 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3604 "P2P: No Status or NoA attribute in P2P Presence "
3606 p2p_parse_free(&msg);
3611 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3612 "P2P: P2P Presence Request was rejected: status %u",
3614 p2p_parse_free(&msg);
3618 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3619 "P2P: P2P Presence Request was accepted");
3620 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
3621 msg.noa, msg.noa_len);
3622 /* TODO: process NoA */
3623 p2p_parse_free(&msg);
3627 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3629 struct p2p_data *p2p = eloop_ctx;
3631 if (p2p->ext_listen_interval) {
3632 /* Schedule next extended listen timeout */
3633 eloop_register_timeout(p2p->ext_listen_interval_sec,
3634 p2p->ext_listen_interval_usec,
3635 p2p_ext_listen_timeout, p2p, NULL);
3638 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
3640 * This should not really happen, but it looks like the Listen
3641 * command may fail is something else (e.g., a scan) was
3642 * running at an inconvenient time. As a workaround, allow new
3643 * Extended Listen operation to be started.
3645 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
3646 "Extended Listen operation had not been completed - "
3648 p2p->ext_listen_only = 0;
3649 p2p_set_state(p2p, P2P_IDLE);
3652 if (p2p->state != P2P_IDLE) {
3653 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
3654 "Listen timeout in active state (%s)",
3655 p2p_state_txt(p2p->state));
3659 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
3660 p2p->ext_listen_only = 1;
3661 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
3662 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
3663 "Listen state for Extended Listen Timing");
3664 p2p->ext_listen_only = 0;
3669 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
3670 unsigned int interval)
3672 if (period > 65535 || interval > 65535 || period > interval ||
3673 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
3674 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3675 "P2P: Invalid Extended Listen Timing request: "
3676 "period=%u interval=%u", period, interval);
3680 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
3682 if (interval == 0) {
3683 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3684 "P2P: Disabling Extended Listen Timing");
3685 p2p->ext_listen_period = 0;
3686 p2p->ext_listen_interval = 0;
3690 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3691 "P2P: Enabling Extended Listen Timing: period %u msec, "
3692 "interval %u msec", period, interval);
3693 p2p->ext_listen_period = period;
3694 p2p->ext_listen_interval = interval;
3695 p2p->ext_listen_interval_sec = interval / 1000;
3696 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
3698 eloop_register_timeout(p2p->ext_listen_interval_sec,
3699 p2p->ext_listen_interval_usec,
3700 p2p_ext_listen_timeout, p2p, NULL);
3706 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3707 const u8 *ie, size_t ie_len)
3709 struct p2p_message msg;
3711 if (bssid == NULL || ie == NULL)
3714 os_memset(&msg, 0, sizeof(msg));
3715 if (p2p_parse_ies(ie, ie_len, &msg))
3717 if (msg.minor_reason_code == NULL)
3720 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3721 "P2P: Deauthentication notification BSSID " MACSTR
3722 " reason_code=%u minor_reason_code=%u",
3723 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3725 p2p_parse_free(&msg);
3729 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3730 const u8 *ie, size_t ie_len)
3732 struct p2p_message msg;
3734 if (bssid == NULL || ie == NULL)
3737 os_memset(&msg, 0, sizeof(msg));
3738 if (p2p_parse_ies(ie, ie_len, &msg))
3740 if (msg.minor_reason_code == NULL)
3743 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3744 "P2P: Disassociation notification BSSID " MACSTR
3745 " reason_code=%u minor_reason_code=%u",
3746 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3748 p2p_parse_free(&msg);
3752 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
3755 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3756 "Device operations enabled");
3757 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
3759 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3760 "Device operations disabled");
3761 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
3766 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
3768 if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
3771 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
3772 "reg_class %u channel %u", reg_class, channel);
3773 p2p->cfg->reg_class = reg_class;
3774 p2p->cfg->channel = channel;
3780 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
3782 wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
3783 if (postfix == NULL) {
3784 p2p->cfg->ssid_postfix_len = 0;
3787 if (len > sizeof(p2p->cfg->ssid_postfix))
3789 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
3790 p2p->cfg->ssid_postfix_len = len;
3795 int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
3798 if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
3802 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
3803 "reg_class %u channel %u", op_reg_class, op_channel);
3804 p2p->cfg->op_reg_class = op_reg_class;
3805 p2p->cfg->op_channel = op_channel;
3806 p2p->cfg->cfg_op_channel = cfg_op_channel;
3811 int p2p_set_pref_chan(struct p2p_data *p2p, unsigned int num_pref_chan,
3812 const struct p2p_channel *pref_chan)
3814 struct p2p_channel *n;
3817 n = os_malloc(num_pref_chan * sizeof(struct p2p_channel));
3820 os_memcpy(n, pref_chan,
3821 num_pref_chan * sizeof(struct p2p_channel));
3825 os_free(p2p->cfg->pref_chan);
3826 p2p->cfg->pref_chan = n;
3827 p2p->cfg->num_pref_chan = num_pref_chan;
3833 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
3836 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
3837 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
3839 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
3844 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
3847 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3850 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
3855 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
3857 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
3858 if (is_zero_ether_addr(p2p->peer_filter))
3859 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
3862 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
3863 "filter for " MACSTR, MAC2STR(p2p->peer_filter));
3867 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
3869 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
3870 enabled ? "enabled" : "disabled");
3871 if (p2p->cross_connect == enabled)
3873 p2p->cross_connect = enabled;
3874 /* TODO: may need to tear down any action group where we are GO(?) */
3878 int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
3880 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3883 if (dev->oper_freq <= 0)
3885 return dev->oper_freq;
3889 void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
3891 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
3892 enabled ? "enabled" : "disabled");
3893 p2p->cfg->p2p_intra_bss = enabled;
3897 void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
3899 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
3900 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
3904 int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3905 const u8 *src, const u8 *bssid, const u8 *buf,
3906 size_t len, unsigned int wait_time)
3908 if (p2p->p2p_scan_running) {
3909 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
3910 "frame TX until p2p_scan completes");
3911 if (p2p->after_scan_tx) {
3912 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
3913 "previous pending Action frame TX");
3914 os_free(p2p->after_scan_tx);
3916 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
3918 if (p2p->after_scan_tx == NULL)
3920 p2p->after_scan_tx->freq = freq;
3921 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
3922 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
3923 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
3924 p2p->after_scan_tx->len = len;
3925 p2p->after_scan_tx->wait_time = wait_time;
3926 os_memcpy(p2p->after_scan_tx + 1, buf, len);
3930 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
3931 buf, len, wait_time);
3935 void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
3938 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
3939 " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall);
3940 p2p->best_freq_24 = freq_24;
3941 p2p->best_freq_5 = freq_5;
3942 p2p->best_freq_overall = freq_overall;
3946 const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
3948 if (p2p == NULL || p2p->go_neg_peer == NULL)
3950 return p2p->go_neg_peer->info.p2p_device_addr;
3954 const struct p2p_peer_info *
3955 p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
3957 struct p2p_device *dev;
3960 dev = p2p_get_device(p2p, addr);
3965 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
3971 dev = dl_list_first(&dev->list,
3974 if (&dev->list == &p2p->devices)
3976 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
3979 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3982 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
3983 dev = dl_list_first(&dev->list,
3986 if (&dev->list == &p2p->devices)
3995 int p2p_in_progress(struct p2p_data *p2p)
3999 if (p2p->state == P2P_SEARCH || p2p->state == P2P_SEARCH_WHEN_READY ||
4000 p2p->state == P2P_CONTINUE_SEARCH_WHEN_READY)
4002 return p2p->state != P2P_IDLE && p2p->state != P2P_PROVISIONING;
4006 void p2p_set_config_timeout(struct p2p_data *p2p, u8 go_timeout,
4010 p2p->go_timeout = go_timeout;
4011 p2p->client_timeout = client_timeout;
4016 void p2p_increase_search_delay(struct p2p_data *p2p, unsigned int delay)
4018 if (p2p && p2p->search_delay < delay)
4019 p2p->search_delay = delay;