2 * Wi-Fi Direct - P2P module
3 * Copyright (c) 2009-2010, Atheros Communications
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
9 * Alternatively, this software may be distributed under the terms of BSD
12 * See README and COPYING for more details.
19 #include "common/ieee802_11_defs.h"
20 #include "common/ieee802_11_common.h"
21 #include "wps/wps_i.h"
26 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx);
27 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev);
28 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
29 const u8 *sa, const u8 *data, size_t len,
31 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
32 const u8 *sa, const u8 *data,
34 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx);
35 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx);
39 * p2p_scan recovery timeout
41 * Many drivers are using 30 second timeout on scan results. Allow a bit larger
42 * timeout for this to avoid hitting P2P timeout unnecessarily.
44 #define P2P_SCAN_TIMEOUT 35
47 * P2P_PEER_EXPIRATION_AGE - Number of seconds after which inactive peer
48 * entries will be removed
50 #define P2P_PEER_EXPIRATION_AGE 300
52 #define P2P_PEER_EXPIRATION_INTERVAL (P2P_PEER_EXPIRATION_AGE / 2)
54 static void p2p_expire_peers(struct p2p_data *p2p)
56 struct p2p_device *dev, *n;
60 dl_list_for_each_safe(dev, n, &p2p->devices, struct p2p_device, list) {
61 if (dev->last_seen.sec + P2P_PEER_EXPIRATION_AGE >= now.sec)
63 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Expiring old peer "
64 "entry " MACSTR, MAC2STR(dev->info.p2p_device_addr));
65 dl_list_del(&dev->list);
66 p2p_device_free(p2p, dev);
71 static void p2p_expiration_timeout(void *eloop_ctx, void *timeout_ctx)
73 struct p2p_data *p2p = eloop_ctx;
74 p2p_expire_peers(p2p);
75 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
76 p2p_expiration_timeout, p2p, NULL);
80 static const char * p2p_state_txt(int state)
89 case P2P_CONNECT_LISTEN:
90 return "CONNECT_LISTEN";
95 case P2P_WAIT_PEER_CONNECT:
96 return "WAIT_PEER_CONNECT";
97 case P2P_WAIT_PEER_IDLE:
98 return "WAIT_PEER_IDLE";
99 case P2P_SD_DURING_FIND:
100 return "SD_DURING_FIND";
101 case P2P_PROVISIONING:
102 return "PROVISIONING";
103 case P2P_PD_DURING_FIND:
104 return "PD_DURING_FIND";
107 case P2P_INVITE_LISTEN:
108 return "INVITE_LISTEN";
115 void p2p_set_state(struct p2p_data *p2p, int new_state)
117 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: State %s -> %s",
118 p2p_state_txt(p2p->state), p2p_state_txt(new_state));
119 p2p->state = new_state;
123 void p2p_set_timeout(struct p2p_data *p2p, unsigned int sec, unsigned int usec)
125 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
126 "P2P: Set timeout (state=%s): %u.%06u sec",
127 p2p_state_txt(p2p->state), sec, usec);
128 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
129 eloop_register_timeout(sec, usec, p2p_state_timeout, p2p, NULL);
133 void p2p_clear_timeout(struct p2p_data *p2p)
135 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Clear timeout (state=%s)",
136 p2p_state_txt(p2p->state));
137 eloop_cancel_timeout(p2p_state_timeout, p2p, NULL);
141 void p2p_go_neg_failed(struct p2p_data *p2p, struct p2p_device *peer,
144 struct p2p_go_neg_results res;
145 p2p_clear_timeout(p2p);
146 p2p_set_state(p2p, P2P_IDLE);
147 p2p->go_neg_peer = NULL;
149 os_memset(&res, 0, sizeof(res));
152 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr,
154 os_memcpy(res.peer_interface_addr, peer->intended_addr,
157 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
161 static void p2p_listen_in_find(struct p2p_data *p2p)
167 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
168 "P2P: Starting short listen state (state=%s)",
169 p2p_state_txt(p2p->state));
171 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
174 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
175 "P2P: Unknown regulatory class/channel");
179 os_get_random((u8 *) &r, sizeof(r));
180 tu = (r % ((p2p->max_disc_int - p2p->min_disc_int) + 1) +
181 p2p->min_disc_int) * 100;
183 p2p->pending_listen_freq = freq;
184 p2p->pending_listen_sec = 0;
185 p2p->pending_listen_usec = 1024 * tu;
187 ies = p2p_build_probe_resp_ies(p2p);
191 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, 1024 * tu / 1000,
193 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
194 "P2P: Failed to start listen mode");
195 p2p->pending_listen_freq = 0;
201 int p2p_listen(struct p2p_data *p2p, unsigned int timeout)
206 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
207 "P2P: Going to listen(only) state");
209 freq = p2p_channel_to_freq(p2p->cfg->country, p2p->cfg->reg_class,
212 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
213 "P2P: Unknown regulatory class/channel");
217 p2p->pending_listen_freq = freq;
218 p2p->pending_listen_sec = timeout / 1000;
219 p2p->pending_listen_usec = (timeout % 1000) * 1000;
221 if (p2p->p2p_scan_running) {
222 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
223 "P2P: p2p_scan running - delay start of listen state");
224 p2p->start_after_scan = P2P_AFTER_SCAN_LISTEN;
228 ies = p2p_build_probe_resp_ies(p2p);
232 if (p2p->cfg->start_listen(p2p->cfg->cb_ctx, freq, timeout, ies) < 0) {
233 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
234 "P2P: Failed to start listen mode");
235 p2p->pending_listen_freq = 0;
241 p2p_set_state(p2p, P2P_LISTEN_ONLY);
247 static void p2p_device_clear_reported(struct p2p_data *p2p)
249 struct p2p_device *dev;
250 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list)
251 dev->flags &= ~P2P_DEV_REPORTED;
256 * p2p_get_device - Fetch a peer entry
257 * @p2p: P2P module context from p2p_init()
258 * @addr: P2P Device Address of the peer
259 * Returns: Pointer to the device entry or %NULL if not found
261 struct p2p_device * p2p_get_device(struct p2p_data *p2p, const u8 *addr)
263 struct p2p_device *dev;
264 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
265 if (os_memcmp(dev->info.p2p_device_addr, addr, ETH_ALEN) == 0)
273 * p2p_get_device_interface - Fetch a peer entry based on P2P Interface Address
274 * @p2p: P2P module context from p2p_init()
275 * @addr: P2P Interface Address of the peer
276 * Returns: Pointer to the device entry or %NULL if not found
278 struct p2p_device * p2p_get_device_interface(struct p2p_data *p2p,
281 struct p2p_device *dev;
282 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
283 if (os_memcmp(dev->interface_addr, addr, ETH_ALEN) == 0)
291 * p2p_create_device - Create a peer entry
292 * @p2p: P2P module context from p2p_init()
293 * @addr: P2P Device Address of the peer
294 * Returns: Pointer to the device entry or %NULL on failure
296 * If there is already an entry for the peer, it will be returned instead of
297 * creating a new one.
299 static struct p2p_device * p2p_create_device(struct p2p_data *p2p,
302 struct p2p_device *dev, *oldest = NULL;
305 dev = p2p_get_device(p2p, addr);
309 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
311 if (oldest == NULL ||
312 os_time_before(&dev->last_seen, &oldest->last_seen))
315 if (count + 1 > p2p->cfg->max_peers && oldest) {
316 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
317 "P2P: Remove oldest peer entry to make room for a new "
319 dl_list_del(&oldest->list);
320 p2p_device_free(p2p, oldest);
323 dev = os_zalloc(sizeof(*dev));
326 dl_list_add(&p2p->devices, &dev->list);
327 os_memcpy(dev->info.p2p_device_addr, addr, ETH_ALEN);
333 static void p2p_copy_client_info(struct p2p_device *dev,
334 struct p2p_client_info *cli)
336 os_memcpy(dev->info.device_name, cli->dev_name, cli->dev_name_len);
337 dev->info.device_name[cli->dev_name_len] = '\0';
338 dev->info.dev_capab = cli->dev_capab;
339 dev->info.config_methods = cli->config_methods;
340 os_memcpy(dev->info.pri_dev_type, cli->pri_dev_type, 8);
341 dev->info.wps_sec_dev_type_list_len = 8 * cli->num_sec_dev_types;
342 os_memcpy(dev->info.wps_sec_dev_type_list, cli->sec_dev_types,
343 dev->info.wps_sec_dev_type_list_len);
347 static int p2p_add_group_clients(struct p2p_data *p2p, const u8 *go_dev_addr,
348 const u8 *go_interface_addr, int freq,
349 const u8 *gi, size_t gi_len)
351 struct p2p_group_info info;
353 struct p2p_device *dev;
358 if (p2p_group_info_parse(gi, gi_len, &info) < 0)
362 * Clear old data for this group; if the devices are still in the
363 * group, the information will be restored in the loop following this.
365 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
366 if (os_memcpy(dev->member_in_go_iface, go_interface_addr,
368 os_memset(dev->member_in_go_iface, 0, ETH_ALEN);
369 os_memset(dev->member_in_go_dev, 0, ETH_ALEN);
373 for (c = 0; c < info.num_clients; c++) {
374 struct p2p_client_info *cli = &info.client[c];
375 dev = p2p_get_device(p2p, cli->p2p_device_addr);
378 * Update information only if we have not received this
379 * directly from the client.
381 if (dev->flags & (P2P_DEV_GROUP_CLIENT_ONLY |
382 P2P_DEV_PROBE_REQ_ONLY))
383 p2p_copy_client_info(dev, cli);
384 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
385 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
388 dev = p2p_create_device(p2p, cli->p2p_device_addr);
391 dev->flags |= P2P_DEV_GROUP_CLIENT_ONLY;
392 p2p_copy_client_info(dev, cli);
393 dev->oper_freq = freq;
394 p2p->cfg->dev_found(p2p->cfg->cb_ctx,
395 dev->info.p2p_device_addr,
397 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
400 os_memcpy(dev->interface_addr, cli->p2p_interface_addr,
402 os_get_time(&dev->last_seen);
403 os_memcpy(dev->member_in_go_dev, go_dev_addr, ETH_ALEN);
404 os_memcpy(dev->member_in_go_iface, go_interface_addr,
412 static void p2p_copy_wps_info(struct p2p_device *dev, int probe_req,
413 const struct p2p_message *msg)
415 os_memcpy(dev->info.device_name, msg->device_name,
416 sizeof(dev->info.device_name));
418 if (msg->manufacturer &&
419 msg->manufacturer_len < sizeof(dev->info.manufacturer)) {
420 os_memset(dev->info.manufacturer, 0,
421 sizeof(dev->info.manufacturer));
422 os_memcpy(dev->info.manufacturer, msg->manufacturer,
423 msg->manufacturer_len);
426 if (msg->model_name &&
427 msg->model_name_len < sizeof(dev->info.model_name)) {
428 os_memset(dev->info.model_name, 0,
429 sizeof(dev->info.model_name));
430 os_memcpy(dev->info.model_name, msg->model_name,
431 msg->model_name_len);
434 if (msg->model_number &&
435 msg->model_number_len < sizeof(dev->info.model_number)) {
436 os_memset(dev->info.model_number, 0,
437 sizeof(dev->info.model_number));
438 os_memcpy(dev->info.model_number, msg->model_number,
439 msg->model_number_len);
442 if (msg->serial_number &&
443 msg->serial_number_len < sizeof(dev->info.serial_number)) {
444 os_memset(dev->info.serial_number, 0,
445 sizeof(dev->info.serial_number));
446 os_memcpy(dev->info.serial_number, msg->serial_number,
447 msg->serial_number_len);
450 if (msg->pri_dev_type)
451 os_memcpy(dev->info.pri_dev_type, msg->pri_dev_type,
452 sizeof(dev->info.pri_dev_type));
453 else if (msg->wps_pri_dev_type)
454 os_memcpy(dev->info.pri_dev_type, msg->wps_pri_dev_type,
455 sizeof(dev->info.pri_dev_type));
457 if (msg->wps_sec_dev_type_list) {
458 os_memcpy(dev->info.wps_sec_dev_type_list,
459 msg->wps_sec_dev_type_list,
460 msg->wps_sec_dev_type_list_len);
461 dev->info.wps_sec_dev_type_list_len =
462 msg->wps_sec_dev_type_list_len;
465 if (msg->capability) {
466 dev->info.dev_capab = msg->capability[0];
467 dev->info.group_capab = msg->capability[1];
470 if (msg->ext_listen_timing) {
471 dev->ext_listen_period = WPA_GET_LE16(msg->ext_listen_timing);
472 dev->ext_listen_interval =
473 WPA_GET_LE16(msg->ext_listen_timing + 2);
477 dev->info.config_methods = msg->config_methods ?
478 msg->config_methods : msg->wps_config_methods;
484 * p2p_add_device - Add peer entries based on scan results
485 * @p2p: P2P module context from p2p_init()
486 * @addr: Source address of Beacon or Probe Response frame (may be either
487 * P2P Device Address or P2P Interface Address)
488 * @level: Signal level (signal strength of the received frame from the peer)
489 * @freq: Frequency on which the Beacon or Probe Response frame was received
490 * @ies: IEs from the Beacon or Probe Response frame
491 * @ies_len: Length of ies buffer in octets
492 * Returns: 0 on success, -1 on failure
494 * If the scan result is for a GO, the clients in the group will also be added
495 * to the peer table. This function can also be used with some other frames
496 * like Provision Discovery Request that contains P2P Capability and P2P Device
499 int p2p_add_device(struct p2p_data *p2p, const u8 *addr, int freq, int level,
500 const u8 *ies, size_t ies_len)
502 struct p2p_device *dev;
503 struct p2p_message msg;
504 const u8 *p2p_dev_addr;
507 os_memset(&msg, 0, sizeof(msg));
508 if (p2p_parse_ies(ies, ies_len, &msg)) {
509 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
510 "P2P: Failed to parse P2P IE for a device entry");
511 p2p_parse_free(&msg);
515 if (msg.p2p_device_addr)
516 p2p_dev_addr = msg.p2p_device_addr;
517 else if (msg.device_id)
518 p2p_dev_addr = msg.device_id;
520 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
521 "P2P: Ignore scan data without P2P Device Info or "
523 p2p_parse_free(&msg);
527 if (!is_zero_ether_addr(p2p->peer_filter) &&
528 os_memcmp(p2p_dev_addr, p2p->peer_filter, ETH_ALEN) != 0) {
529 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Do not add peer "
530 "filter for " MACSTR " due to peer filter",
531 MAC2STR(p2p_dev_addr));
535 dev = p2p_create_device(p2p, p2p_dev_addr);
537 p2p_parse_free(&msg);
540 os_get_time(&dev->last_seen);
541 dev->flags &= ~(P2P_DEV_PROBE_REQ_ONLY | P2P_DEV_GROUP_CLIENT_ONLY);
543 if (os_memcmp(addr, p2p_dev_addr, ETH_ALEN) != 0)
544 os_memcpy(dev->interface_addr, addr, ETH_ALEN);
546 (msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
547 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
549 os_memcpy(dev->oper_ssid, msg.ssid + 2, msg.ssid[1]);
550 dev->oper_ssid_len = msg.ssid[1];
553 if (freq >= 2412 && freq <= 2484 && msg.ds_params &&
554 *msg.ds_params >= 1 && *msg.ds_params <= 14) {
556 if (*msg.ds_params == 14)
559 ds_freq = 2407 + *msg.ds_params * 5;
560 if (freq != ds_freq) {
561 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
562 "P2P: Update Listen frequency based on DS "
563 "Parameter Set IE: %d -> %d MHz",
569 if (dev->listen_freq && dev->listen_freq != freq) {
570 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
571 "P2P: Update Listen frequency based on scan "
572 "results (" MACSTR " %d -> %d MHz (DS param %d)",
573 MAC2STR(dev->info.p2p_device_addr), dev->listen_freq,
574 freq, msg.ds_params ? *msg.ds_params : -1);
576 dev->listen_freq = freq;
578 dev->oper_freq = freq;
579 dev->info.level = level;
581 p2p_copy_wps_info(dev, 0, &msg);
583 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
584 wpabuf_free(dev->info.wps_vendor_ext[i]);
585 dev->info.wps_vendor_ext[i] = NULL;
588 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
589 if (msg.wps_vendor_ext[i] == NULL)
591 dev->info.wps_vendor_ext[i] = wpabuf_alloc_copy(
592 msg.wps_vendor_ext[i], msg.wps_vendor_ext_len[i]);
593 if (dev->info.wps_vendor_ext[i] == NULL)
597 p2p_add_group_clients(p2p, p2p_dev_addr, addr, freq, msg.group_info,
600 p2p_parse_free(&msg);
602 if (p2p_pending_sd_req(p2p, dev))
603 dev->flags |= P2P_DEV_SD_SCHEDULE;
605 if (dev->flags & P2P_DEV_REPORTED)
608 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
609 "P2P: Peer found with Listen frequency %d MHz", freq);
610 if (dev->flags & P2P_DEV_USER_REJECTED) {
611 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
612 "P2P: Do not report rejected device");
616 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
617 !(dev->flags & P2P_DEV_REPORTED_ONCE));
618 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
624 static void p2p_device_free(struct p2p_data *p2p, struct p2p_device *dev)
628 if (p2p->go_neg_peer == dev)
629 p2p->go_neg_peer = NULL;
630 if (p2p->invite_peer == dev)
631 p2p->invite_peer = NULL;
632 if (p2p->sd_peer == dev)
634 if (p2p->pending_client_disc_go == dev)
635 p2p->pending_client_disc_go = NULL;
637 /* dev_lost() device, but only if it was previously dev_found() */
638 if (dev->flags & P2P_DEV_REPORTED_ONCE)
639 p2p->cfg->dev_lost(p2p->cfg->cb_ctx,
640 dev->info.p2p_device_addr);
642 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
643 wpabuf_free(dev->info.wps_vendor_ext[i]);
644 dev->info.wps_vendor_ext[i] = NULL;
651 static int p2p_get_next_prog_freq(struct p2p_data *p2p)
653 struct p2p_channels *c;
654 struct p2p_reg_class *cla;
661 c = &p2p->cfg->channels;
662 for (cl = 0; cl < c->reg_classes; cl++) {
663 cla = &c->reg_class[cl];
664 if (cla->reg_class != p2p->last_prog_scan_class)
666 for (ch = 0; ch < cla->channels; ch++) {
667 if (cla->channel[ch] == p2p->last_prog_scan_chan) {
677 /* Start from beginning */
678 reg_class = c->reg_class[0].reg_class;
679 channel = c->reg_class[0].channel[0];
681 /* Pick the next channel */
683 if (ch == cla->channels) {
685 if (cl == c->reg_classes)
689 reg_class = c->reg_class[cl].reg_class;
690 channel = c->reg_class[cl].channel[ch];
693 freq = p2p_channel_to_freq(p2p->cfg->country, reg_class, channel);
694 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Next progressive search "
695 "channel: reg_class %u channel %u -> %d MHz",
696 reg_class, channel, freq);
697 p2p->last_prog_scan_class = reg_class;
698 p2p->last_prog_scan_chan = channel;
700 if (freq == 2412 || freq == 2437 || freq == 2462)
701 return 0; /* No need to add social channels */
706 static void p2p_search(struct p2p_data *p2p)
709 enum p2p_scan_type type;
711 if (p2p->drv_in_listen) {
712 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is still "
713 "in Listen state - wait for it to end before "
717 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
719 if (p2p->go_neg_peer) {
721 * Only scan the known listen frequency of the peer
722 * during GO Negotiation start.
724 freq = p2p->go_neg_peer->listen_freq;
726 freq = p2p->go_neg_peer->oper_freq;
727 type = P2P_SCAN_SPECIFIC;
728 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
729 "for freq %u (GO Neg)", freq);
730 } else if (p2p->invite_peer) {
732 * Only scan the known listen frequency of the peer
733 * during Invite start.
735 freq = p2p->invite_peer->listen_freq;
737 freq = p2p->invite_peer->oper_freq;
738 type = P2P_SCAN_SPECIFIC;
739 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
740 "for freq %u (Invite)", freq);
741 } else if (p2p->find_type == P2P_FIND_PROGRESSIVE &&
742 (freq = p2p_get_next_prog_freq(p2p)) > 0) {
743 type = P2P_SCAN_SOCIAL_PLUS_ONE;
744 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search "
745 "(+ freq %u)", freq);
747 type = P2P_SCAN_SOCIAL;
748 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting search");
751 if (p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, type, freq,
752 p2p->num_req_dev_types, p2p->req_dev_types) < 0)
754 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
755 "P2P: Scan request failed");
756 p2p_continue_find(p2p);
758 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
759 p2p->p2p_scan_running = 1;
760 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
761 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
767 static void p2p_find_timeout(void *eloop_ctx, void *timeout_ctx)
769 struct p2p_data *p2p = eloop_ctx;
770 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Find timeout -> stop");
775 static int p2p_run_after_scan(struct p2p_data *p2p)
777 struct p2p_device *dev;
778 enum p2p_after_scan op;
780 if (p2p->after_scan_tx) {
782 /* TODO: schedule p2p_run_after_scan to be called from TX
783 * status callback(?) */
784 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send pending "
785 "Action frame at p2p_scan completion");
786 ret = p2p->cfg->send_action(p2p->cfg->cb_ctx,
787 p2p->after_scan_tx->freq,
788 p2p->after_scan_tx->dst,
789 p2p->after_scan_tx->src,
790 p2p->after_scan_tx->bssid,
791 (u8 *) (p2p->after_scan_tx + 1),
792 p2p->after_scan_tx->len,
793 p2p->after_scan_tx->wait_time);
794 os_free(p2p->after_scan_tx);
795 p2p->after_scan_tx = NULL;
799 op = p2p->start_after_scan;
800 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
802 case P2P_AFTER_SCAN_NOTHING:
804 case P2P_AFTER_SCAN_LISTEN:
805 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
806 "requested Listen state");
807 p2p_listen(p2p, p2p->pending_listen_sec * 1000 +
808 p2p->pending_listen_usec / 1000);
810 case P2P_AFTER_SCAN_CONNECT:
811 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Start previously "
812 "requested connect with " MACSTR,
813 MAC2STR(p2p->after_scan_peer));
814 dev = p2p_get_device(p2p, p2p->after_scan_peer);
816 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer not "
820 p2p_connect_send(p2p, dev);
828 static void p2p_scan_timeout(void *eloop_ctx, void *timeout_ctx)
830 struct p2p_data *p2p = eloop_ctx;
832 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan timeout "
833 "(running=%d)", p2p->p2p_scan_running);
834 running = p2p->p2p_scan_running;
835 /* Make sure we recover from missed scan results callback */
836 p2p->p2p_scan_running = 0;
839 p2p_run_after_scan(p2p);
843 static void p2p_free_req_dev_types(struct p2p_data *p2p)
845 p2p->num_req_dev_types = 0;
846 os_free(p2p->req_dev_types);
847 p2p->req_dev_types = NULL;
851 int p2p_find(struct p2p_data *p2p, unsigned int timeout,
852 enum p2p_discovery_type type,
853 unsigned int num_req_dev_types, const u8 *req_dev_types)
857 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Starting find (type=%d)",
859 if (p2p->p2p_scan_running) {
860 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan is "
864 p2p_free_req_dev_types(p2p);
865 if (req_dev_types && num_req_dev_types) {
866 p2p->req_dev_types = os_malloc(num_req_dev_types *
868 if (p2p->req_dev_types == NULL)
870 os_memcpy(p2p->req_dev_types, req_dev_types,
871 num_req_dev_types * WPS_DEV_TYPE_LEN);
872 p2p->num_req_dev_types = num_req_dev_types;
875 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
876 p2p_clear_timeout(p2p);
877 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
878 p2p->find_type = type;
879 p2p_device_clear_reported(p2p);
880 p2p_set_state(p2p, P2P_SEARCH);
881 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
883 eloop_register_timeout(timeout, 0, p2p_find_timeout,
886 case P2P_FIND_START_WITH_FULL:
887 case P2P_FIND_PROGRESSIVE:
888 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_FULL, 0,
889 p2p->num_req_dev_types,
892 case P2P_FIND_ONLY_SOCIAL:
893 res = p2p->cfg->p2p_scan(p2p->cfg->cb_ctx, P2P_SCAN_SOCIAL, 0,
894 p2p->num_req_dev_types,
902 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Running p2p_scan");
903 p2p->p2p_scan_running = 1;
904 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
905 eloop_register_timeout(P2P_SCAN_TIMEOUT, 0, p2p_scan_timeout,
908 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
916 void p2p_stop_find_for_freq(struct p2p_data *p2p, int freq)
918 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Stopping find");
919 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
920 p2p_clear_timeout(p2p);
921 p2p_set_state(p2p, P2P_IDLE);
922 p2p_free_req_dev_types(p2p);
923 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
924 p2p->go_neg_peer = NULL;
926 p2p->invite_peer = NULL;
927 if (freq > 0 && p2p->drv_in_listen == freq && p2p->in_listen) {
928 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip stop_listen "
929 "since we are on correct channel for response");
932 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
936 void p2p_stop_find(struct p2p_data *p2p)
938 p2p_stop_find_for_freq(p2p, 0);
942 static int p2p_prepare_channel(struct p2p_data *p2p, unsigned int force_freq)
945 u8 op_reg_class, op_channel;
946 if (p2p_freq_to_channel(p2p->cfg->country, force_freq,
947 &op_reg_class, &op_channel) < 0) {
948 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
949 "P2P: Unsupported frequency %u MHz",
953 if (!p2p_channels_includes(&p2p->cfg->channels, op_reg_class,
955 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
956 "P2P: Frequency %u MHz (oper_class %u "
957 "channel %u) not allowed for P2P",
958 force_freq, op_reg_class, op_channel);
961 p2p->op_reg_class = op_reg_class;
962 p2p->op_channel = op_channel;
963 p2p->channels.reg_classes = 1;
964 p2p->channels.reg_class[0].channels = 1;
965 p2p->channels.reg_class[0].reg_class = p2p->op_reg_class;
966 p2p->channels.reg_class[0].channel[0] = p2p->op_channel;
968 u8 op_reg_class, op_channel;
970 if (!p2p->cfg->cfg_op_channel && p2p->best_freq_overall > 0 &&
971 p2p_supported_freq(p2p, p2p->best_freq_overall) &&
972 p2p_freq_to_channel(p2p->cfg->country,
973 p2p->best_freq_overall,
974 &op_reg_class, &op_channel) == 0) {
975 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
976 "P2P: Select best overall channel as "
977 "operating channel preference");
978 p2p->op_reg_class = op_reg_class;
979 p2p->op_channel = op_channel;
980 } else if (!p2p->cfg->cfg_op_channel && p2p->best_freq_5 > 0 &&
981 p2p_supported_freq(p2p, p2p->best_freq_5) &&
982 p2p_freq_to_channel(p2p->cfg->country,
984 &op_reg_class, &op_channel) ==
986 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
987 "P2P: Select best 5 GHz channel as "
988 "operating channel preference");
989 p2p->op_reg_class = op_reg_class;
990 p2p->op_channel = op_channel;
991 } else if (!p2p->cfg->cfg_op_channel &&
992 p2p->best_freq_24 > 0 &&
993 p2p_supported_freq(p2p, p2p->best_freq_24) &&
994 p2p_freq_to_channel(p2p->cfg->country,
996 &op_reg_class, &op_channel) ==
998 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
999 "P2P: Select best 2.4 GHz channel as "
1000 "operating channel preference");
1001 p2p->op_reg_class = op_reg_class;
1002 p2p->op_channel = op_channel;
1004 p2p->op_reg_class = p2p->cfg->op_reg_class;
1005 p2p->op_channel = p2p->cfg->op_channel;
1008 os_memcpy(&p2p->channels, &p2p->cfg->channels,
1009 sizeof(struct p2p_channels));
1011 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1012 "P2P: Own preference for operation channel: "
1013 "Operating Class %u Channel %u%s",
1014 p2p->op_reg_class, p2p->op_channel,
1015 force_freq ? " (forced)" : "");
1021 int p2p_connect(struct p2p_data *p2p, const u8 *peer_addr,
1022 enum p2p_wps_method wps_method,
1023 int go_intent, const u8 *own_interface_addr,
1024 unsigned int force_freq, int persistent_group)
1026 struct p2p_device *dev;
1028 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1029 "P2P: Request to start group negotiation - peer=" MACSTR
1030 " GO Intent=%d Intended Interface Address=" MACSTR
1031 " wps_method=%d persistent_group=%d",
1032 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1033 wps_method, persistent_group);
1035 if (p2p_prepare_channel(p2p, force_freq) < 0)
1038 dev = p2p_get_device(p2p, peer_addr);
1039 if (dev == NULL || (dev->flags & P2P_DEV_PROBE_REQ_ONLY)) {
1040 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1041 "P2P: Cannot connect to unknown P2P Device " MACSTR,
1042 MAC2STR(peer_addr));
1046 if (dev->flags & P2P_DEV_GROUP_CLIENT_ONLY) {
1047 if (!(dev->info.dev_capab &
1048 P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY)) {
1049 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1050 "P2P: Cannot connect to P2P Device " MACSTR
1051 " that is in a group and is not discoverable",
1052 MAC2STR(peer_addr));
1055 if (dev->oper_freq <= 0) {
1056 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1057 "P2P: Cannot connect to P2P Device " MACSTR
1058 " with incomplete information",
1059 MAC2STR(peer_addr));
1064 * First, try to connect directly. If the peer does not
1065 * acknowledge frames, assume it is sleeping and use device
1066 * discoverability via the GO at that point.
1070 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1071 dev->flags &= ~P2P_DEV_USER_REJECTED;
1072 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
1073 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
1074 dev->connect_reqs = 0;
1075 dev->go_neg_req_sent = 0;
1076 dev->go_state = UNKNOWN_GO;
1077 if (persistent_group)
1078 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1080 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_GROUP;
1081 p2p->go_intent = go_intent;
1082 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1084 if (p2p->state != P2P_IDLE)
1087 if (p2p->after_scan_tx) {
1089 * We need to drop the pending frame to avoid issues with the
1090 * new GO Negotiation, e.g., when the pending frame was from a
1091 * previous attempt at starting a GO Negotiation.
1093 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
1094 "previous pending Action frame TX that was waiting "
1095 "for p2p_scan completion");
1096 os_free(p2p->after_scan_tx);
1097 p2p->after_scan_tx = NULL;
1100 dev->wps_method = wps_method;
1101 dev->status = P2P_SC_SUCCESS;
1104 dev->flags |= P2P_DEV_FORCE_FREQ;
1106 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1108 if (p2p->p2p_scan_running) {
1109 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1110 "P2P: p2p_scan running - delay connect send");
1111 p2p->start_after_scan = P2P_AFTER_SCAN_CONNECT;
1112 os_memcpy(p2p->after_scan_peer, peer_addr, ETH_ALEN);
1115 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
1117 return p2p_connect_send(p2p, dev);
1121 int p2p_authorize(struct p2p_data *p2p, const u8 *peer_addr,
1122 enum p2p_wps_method wps_method,
1123 int go_intent, const u8 *own_interface_addr,
1124 unsigned int force_freq, int persistent_group)
1126 struct p2p_device *dev;
1128 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1129 "P2P: Request to authorize group negotiation - peer=" MACSTR
1130 " GO Intent=%d Intended Interface Address=" MACSTR
1131 " wps_method=%d persistent_group=%d",
1132 MAC2STR(peer_addr), go_intent, MAC2STR(own_interface_addr),
1133 wps_method, persistent_group);
1135 if (p2p_prepare_channel(p2p, force_freq) < 0)
1138 dev = p2p_get_device(p2p, peer_addr);
1140 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1141 "P2P: Cannot authorize unknown P2P Device " MACSTR,
1142 MAC2STR(peer_addr));
1146 dev->flags &= ~P2P_DEV_NOT_YET_READY;
1147 dev->flags &= ~P2P_DEV_USER_REJECTED;
1148 dev->go_neg_req_sent = 0;
1149 dev->go_state = UNKNOWN_GO;
1150 if (persistent_group)
1151 dev->flags |= P2P_DEV_PREFER_PERSISTENT_GROUP;
1153 dev->flags &= ~P2P_DEV_PREFER_PERSISTENT_GROUP;
1154 p2p->go_intent = go_intent;
1155 os_memcpy(p2p->intended_addr, own_interface_addr, ETH_ALEN);
1157 dev->wps_method = wps_method;
1158 dev->status = P2P_SC_SUCCESS;
1161 dev->flags |= P2P_DEV_FORCE_FREQ;
1163 dev->flags &= ~P2P_DEV_FORCE_FREQ;
1169 void p2p_add_dev_info(struct p2p_data *p2p, const u8 *addr,
1170 struct p2p_device *dev, struct p2p_message *msg)
1172 os_get_time(&dev->last_seen);
1174 p2p_copy_wps_info(dev, 0, msg);
1176 if (msg->listen_channel) {
1178 freq = p2p_channel_to_freq((char *) msg->listen_channel,
1179 msg->listen_channel[3],
1180 msg->listen_channel[4]);
1182 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1183 "P2P: Unknown peer Listen channel: "
1184 "country=%c%c(0x%02x) reg_class=%u channel=%u",
1185 msg->listen_channel[0],
1186 msg->listen_channel[1],
1187 msg->listen_channel[2],
1188 msg->listen_channel[3],
1189 msg->listen_channel[4]);
1191 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update "
1192 "peer " MACSTR " Listen channel: %u -> %u MHz",
1193 MAC2STR(dev->info.p2p_device_addr),
1194 dev->listen_freq, freq);
1195 dev->listen_freq = freq;
1199 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
1200 dev->flags &= ~P2P_DEV_PROBE_REQ_ONLY;
1201 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1202 "P2P: Completed device entry based on data from "
1203 "GO Negotiation Request");
1205 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1206 "P2P: Created device entry based on GO Neg Req: "
1207 MACSTR " dev_capab=0x%x group_capab=0x%x name='%s' "
1209 MAC2STR(dev->info.p2p_device_addr),
1210 dev->info.dev_capab, dev->info.group_capab,
1211 dev->info.device_name, dev->listen_freq);
1214 dev->flags &= ~P2P_DEV_GROUP_CLIENT_ONLY;
1216 if (dev->flags & P2P_DEV_USER_REJECTED) {
1217 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1218 "P2P: Do not report rejected device");
1222 p2p->cfg->dev_found(p2p->cfg->cb_ctx, addr, &dev->info,
1223 !(dev->flags & P2P_DEV_REPORTED_ONCE));
1224 dev->flags |= P2P_DEV_REPORTED | P2P_DEV_REPORTED_ONCE;
1228 void p2p_build_ssid(struct p2p_data *p2p, u8 *ssid, size_t *ssid_len)
1230 os_memcpy(ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1231 p2p_random((char *) &ssid[P2P_WILDCARD_SSID_LEN], 2);
1232 os_memcpy(&ssid[P2P_WILDCARD_SSID_LEN + 2],
1233 p2p->cfg->ssid_postfix, p2p->cfg->ssid_postfix_len);
1234 *ssid_len = P2P_WILDCARD_SSID_LEN + 2 + p2p->cfg->ssid_postfix_len;
1238 int p2p_go_params(struct p2p_data *p2p, struct p2p_go_neg_results *params)
1240 p2p_build_ssid(p2p, params->ssid, ¶ms->ssid_len);
1241 p2p_random(params->passphrase, 8);
1246 void p2p_go_complete(struct p2p_data *p2p, struct p2p_device *peer)
1248 struct p2p_go_neg_results res;
1249 int go = peer->go_state == LOCAL_GO;
1250 struct p2p_channels intersection;
1254 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1255 "P2P: GO Negotiation with " MACSTR " completed (%s will be "
1256 "GO)", MAC2STR(peer->info.p2p_device_addr),
1257 go ? "local end" : "peer");
1259 os_memset(&res, 0, sizeof(res));
1261 os_memcpy(res.peer_device_addr, peer->info.p2p_device_addr, ETH_ALEN);
1262 os_memcpy(res.peer_interface_addr, peer->intended_addr, ETH_ALEN);
1263 res.wps_method = peer->wps_method;
1264 if (peer->flags & P2P_DEV_PREFER_PERSISTENT_GROUP)
1265 res.persistent_group = 1;
1268 /* Setup AP mode for WPS provisioning */
1269 res.freq = p2p_channel_to_freq(p2p->cfg->country,
1272 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1273 res.ssid_len = p2p->ssid_len;
1274 p2p_random(res.passphrase, 8);
1276 res.freq = peer->oper_freq;
1277 if (p2p->ssid_len) {
1278 os_memcpy(res.ssid, p2p->ssid, p2p->ssid_len);
1279 res.ssid_len = p2p->ssid_len;
1283 p2p_channels_intersect(&p2p->channels, &peer->channels,
1286 for (i = 0; i < intersection.reg_classes; i++) {
1287 struct p2p_reg_class *c = &intersection.reg_class[i];
1288 if (freqs + 1 == P2P_MAX_CHANNELS)
1290 for (j = 0; j < c->channels; j++) {
1292 if (freqs + 1 == P2P_MAX_CHANNELS)
1294 freq = p2p_channel_to_freq(peer->country, c->reg_class,
1298 res.freq_list[freqs++] = freq;
1302 res.peer_config_timeout = go ? peer->client_timeout : peer->go_timeout;
1304 p2p_clear_timeout(p2p);
1305 peer->go_neg_req_sent = 0;
1306 peer->wps_method = WPS_NOT_READY;
1308 p2p_set_state(p2p, P2P_PROVISIONING);
1309 p2p->cfg->go_neg_completed(p2p->cfg->cb_ctx, &res);
1313 static void p2p_rx_p2p_action(struct p2p_data *p2p, const u8 *sa,
1314 const u8 *data, size_t len, int rx_freq)
1316 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1317 "P2P: RX P2P Public Action from " MACSTR, MAC2STR(sa));
1318 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Public Action contents", data, len);
1324 case P2P_GO_NEG_REQ:
1325 p2p_process_go_neg_req(p2p, sa, data + 1, len - 1, rx_freq);
1327 case P2P_GO_NEG_RESP:
1328 p2p_process_go_neg_resp(p2p, sa, data + 1, len - 1, rx_freq);
1330 case P2P_GO_NEG_CONF:
1331 p2p_process_go_neg_conf(p2p, sa, data + 1, len - 1);
1333 case P2P_INVITATION_REQ:
1334 p2p_process_invitation_req(p2p, sa, data + 1, len - 1,
1337 case P2P_INVITATION_RESP:
1338 p2p_process_invitation_resp(p2p, sa, data + 1, len - 1);
1340 case P2P_PROV_DISC_REQ:
1341 p2p_process_prov_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1343 case P2P_PROV_DISC_RESP:
1344 p2p_process_prov_disc_resp(p2p, sa, data + 1, len - 1);
1346 case P2P_DEV_DISC_REQ:
1347 p2p_process_dev_disc_req(p2p, sa, data + 1, len - 1, rx_freq);
1349 case P2P_DEV_DISC_RESP:
1350 p2p_process_dev_disc_resp(p2p, sa, data + 1, len - 1);
1353 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1354 "P2P: Unsupported P2P Public Action frame type %d",
1361 void p2p_rx_action_public(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1362 const u8 *bssid, const u8 *data, size_t len,
1369 case WLAN_PA_VENDOR_SPECIFIC:
1374 if (WPA_GET_BE24(data) != OUI_WFA)
1382 if (*data != P2P_OUI_TYPE)
1385 p2p_rx_p2p_action(p2p, sa, data + 1, len - 1, freq);
1387 case WLAN_PA_GAS_INITIAL_REQ:
1388 p2p_rx_gas_initial_req(p2p, sa, data + 1, len - 1, freq);
1390 case WLAN_PA_GAS_INITIAL_RESP:
1391 p2p_rx_gas_initial_resp(p2p, sa, data + 1, len - 1, freq);
1393 case WLAN_PA_GAS_COMEBACK_REQ:
1394 p2p_rx_gas_comeback_req(p2p, sa, data + 1, len - 1, freq);
1396 case WLAN_PA_GAS_COMEBACK_RESP:
1397 p2p_rx_gas_comeback_resp(p2p, sa, data + 1, len - 1, freq);
1403 void p2p_rx_action(struct p2p_data *p2p, const u8 *da, const u8 *sa,
1404 const u8 *bssid, u8 category,
1405 const u8 *data, size_t len, int freq)
1407 if (category == WLAN_ACTION_PUBLIC) {
1408 p2p_rx_action_public(p2p, da, sa, bssid, data, len, freq);
1412 if (category != WLAN_ACTION_VENDOR_SPECIFIC)
1418 if (WPA_GET_BE24(data) != OUI_WFA)
1423 if (*data != P2P_OUI_TYPE)
1428 /* P2P action frame */
1429 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1430 "P2P: RX P2P Action from " MACSTR, MAC2STR(sa));
1431 wpa_hexdump(MSG_MSGDUMP, "P2P: P2P Action contents", data, len);
1437 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1438 "P2P: Received P2P Action - Notice of Absence");
1441 case P2P_PRESENCE_REQ:
1442 p2p_process_presence_req(p2p, da, sa, data + 1, len - 1, freq);
1444 case P2P_PRESENCE_RESP:
1445 p2p_process_presence_resp(p2p, da, sa, data + 1, len - 1);
1447 case P2P_GO_DISC_REQ:
1448 p2p_process_go_disc_req(p2p, da, sa, data + 1, len - 1, freq);
1451 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1452 "P2P: Received P2P Action - unknown type %u", data[0]);
1458 static void p2p_go_neg_start(void *eloop_ctx, void *timeout_ctx)
1460 struct p2p_data *p2p = eloop_ctx;
1461 if (p2p->go_neg_peer == NULL)
1463 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1464 p2p->go_neg_peer->status = P2P_SC_SUCCESS;
1465 p2p_connect_send(p2p, p2p->go_neg_peer);
1469 static void p2p_invite_start(void *eloop_ctx, void *timeout_ctx)
1471 struct p2p_data *p2p = eloop_ctx;
1472 if (p2p->invite_peer == NULL)
1474 p2p->cfg->stop_listen(p2p->cfg->cb_ctx);
1475 p2p_invite_send(p2p, p2p->invite_peer, p2p->invite_go_dev_addr);
1479 static void p2p_add_dev_from_probe_req(struct p2p_data *p2p, const u8 *addr,
1480 const u8 *ie, size_t ie_len)
1482 struct p2p_message msg;
1483 struct p2p_device *dev;
1485 os_memset(&msg, 0, sizeof(msg));
1486 if (p2p_parse_ies(ie, ie_len, &msg) < 0 || msg.p2p_attributes == NULL)
1488 p2p_parse_free(&msg);
1489 return; /* not a P2P probe */
1492 if (msg.ssid == NULL || msg.ssid[1] != P2P_WILDCARD_SSID_LEN ||
1493 os_memcmp(msg.ssid + 2, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN)
1495 /* The Probe Request is not part of P2P Device Discovery. It is
1496 * not known whether the source address of the frame is the P2P
1497 * Device Address or P2P Interface Address. Do not add a new
1498 * peer entry based on this frames.
1500 p2p_parse_free(&msg);
1504 dev = p2p_get_device(p2p, addr);
1506 if (dev->country[0] == 0 && msg.listen_channel)
1507 os_memcpy(dev->country, msg.listen_channel, 3);
1508 p2p_parse_free(&msg);
1509 return; /* already known */
1512 dev = p2p_create_device(p2p, addr);
1514 p2p_parse_free(&msg);
1518 os_get_time(&dev->last_seen);
1519 dev->flags |= P2P_DEV_PROBE_REQ_ONLY;
1521 if (msg.listen_channel) {
1522 os_memcpy(dev->country, msg.listen_channel, 3);
1523 dev->listen_freq = p2p_channel_to_freq(dev->country,
1524 msg.listen_channel[3],
1525 msg.listen_channel[4]);
1528 p2p_copy_wps_info(dev, 1, &msg);
1530 p2p_parse_free(&msg);
1532 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1533 "P2P: Created device entry based on Probe Req: " MACSTR
1534 " dev_capab=0x%x group_capab=0x%x name='%s' listen_freq=%d",
1535 MAC2STR(dev->info.p2p_device_addr), dev->info.dev_capab,
1536 dev->info.group_capab, dev->info.device_name,
1541 struct p2p_device * p2p_add_dev_from_go_neg_req(struct p2p_data *p2p,
1543 struct p2p_message *msg)
1545 struct p2p_device *dev;
1547 dev = p2p_get_device(p2p, addr);
1549 os_get_time(&dev->last_seen);
1550 return dev; /* already known */
1553 dev = p2p_create_device(p2p, addr);
1557 p2p_add_dev_info(p2p, addr, dev, msg);
1563 static int dev_type_match(const u8 *dev_type, const u8 *req_dev_type)
1565 if (os_memcmp(dev_type, req_dev_type, WPS_DEV_TYPE_LEN) == 0)
1567 if (os_memcmp(dev_type, req_dev_type, 2) == 0 &&
1568 WPA_GET_BE32(&req_dev_type[2]) == 0 &&
1569 WPA_GET_BE16(&req_dev_type[6]) == 0)
1570 return 1; /* Category match with wildcard OUI/sub-category */
1575 int dev_type_list_match(const u8 *dev_type, const u8 *req_dev_type[],
1576 size_t num_req_dev_type)
1579 for (i = 0; i < num_req_dev_type; i++) {
1580 if (dev_type_match(dev_type, req_dev_type[i]))
1588 * p2p_match_dev_type - Match local device type with requested type
1589 * @p2p: P2P module context from p2p_init()
1590 * @wps: WPS TLVs from Probe Request frame (concatenated WPS IEs)
1591 * Returns: 1 on match, 0 on mismatch
1593 * This function can be used to match the Requested Device Type attribute in
1594 * WPS IE with the local device types for deciding whether to reply to a Probe
1597 int p2p_match_dev_type(struct p2p_data *p2p, struct wpabuf *wps)
1599 struct wps_parse_attr attr;
1602 if (wps_parse_msg(wps, &attr))
1603 return 1; /* assume no Requested Device Type attributes */
1605 if (attr.num_req_dev_type == 0)
1606 return 1; /* no Requested Device Type attributes -> match */
1608 if (dev_type_list_match(p2p->cfg->pri_dev_type, attr.req_dev_type,
1609 attr.num_req_dev_type))
1610 return 1; /* Own Primary Device Type matches */
1612 for (i = 0; i < p2p->cfg->num_sec_dev_types; i++)
1613 if (dev_type_list_match(p2p->cfg->sec_dev_type[i],
1615 attr.num_req_dev_type))
1616 return 1; /* Own Secondary Device Type matches */
1618 /* No matching device type found */
1623 struct wpabuf * p2p_build_probe_resp_ies(struct p2p_data *p2p)
1628 buf = wpabuf_alloc(1000);
1632 p2p_build_wps_ie(p2p, buf, DEV_PW_DEFAULT, 1);
1635 len = p2p_buf_add_ie_hdr(buf);
1636 p2p_buf_add_capability(buf, p2p->dev_capab, 0);
1637 if (p2p->ext_listen_interval)
1638 p2p_buf_add_ext_listen_timing(buf, p2p->ext_listen_period,
1639 p2p->ext_listen_interval);
1640 p2p_buf_add_device_info(buf, p2p, NULL);
1641 p2p_buf_update_ie_hdr(buf, len);
1647 static void p2p_reply_probe(struct p2p_data *p2p, const u8 *addr,
1648 const u8 *dst, const u8 *bssid, const u8 *ie,
1651 struct ieee802_11_elems elems;
1653 struct ieee80211_mgmt *resp;
1657 if (!p2p->in_listen || !p2p->drv_in_listen) {
1658 /* not in Listen state - ignore Probe Request */
1662 if (ieee802_11_parse_elems((u8 *) ie, ie_len, &elems, 0) ==
1664 /* Ignore invalid Probe Request frames */
1668 if (elems.p2p == NULL) {
1669 /* not a P2P probe - ignore it */
1673 if (dst && !is_broadcast_ether_addr(dst) &&
1674 os_memcmp(dst, p2p->cfg->dev_addr, ETH_ALEN) != 0) {
1675 /* Not sent to the broadcast address or our P2P Device Address
1680 if (bssid && !is_broadcast_ether_addr(bssid)) {
1681 /* Not sent to the Wildcard BSSID */
1685 if (elems.ssid == NULL || elems.ssid_len != P2P_WILDCARD_SSID_LEN ||
1686 os_memcmp(elems.ssid, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN) !=
1688 /* not using P2P Wildcard SSID - ignore */
1692 /* Check Requested Device Type match */
1693 wps = ieee802_11_vendor_ie_concat(ie, ie_len, WPS_DEV_OUI_WFA);
1694 if (wps && !p2p_match_dev_type(p2p, wps)) {
1696 /* No match with Requested Device Type */
1701 if (!p2p->cfg->send_probe_resp)
1702 return; /* Response generated elsewhere */
1704 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1705 "P2P: Reply to P2P Probe Request in Listen state");
1708 * We do not really have a specific BSS that this frame is advertising,
1709 * so build a frame that has some information in valid format. This is
1710 * really only used for discovery purposes, not to learn exact BSS
1713 ies = p2p_build_probe_resp_ies(p2p);
1717 buf = wpabuf_alloc(200 + wpabuf_len(ies));
1724 resp = wpabuf_put(buf, resp->u.probe_resp.variable - (u8 *) resp);
1726 resp->frame_control = host_to_le16((WLAN_FC_TYPE_MGMT << 2) |
1727 (WLAN_FC_STYPE_PROBE_RESP << 4));
1728 os_memcpy(resp->da, addr, ETH_ALEN);
1729 os_memcpy(resp->sa, p2p->cfg->dev_addr, ETH_ALEN);
1730 os_memcpy(resp->bssid, p2p->cfg->dev_addr, ETH_ALEN);
1731 resp->u.probe_resp.beacon_int = host_to_le16(100);
1732 /* hardware or low-level driver will setup seq_ctrl and timestamp */
1733 resp->u.probe_resp.capab_info =
1734 host_to_le16(WLAN_CAPABILITY_SHORT_PREAMBLE |
1735 WLAN_CAPABILITY_PRIVACY |
1736 WLAN_CAPABILITY_SHORT_SLOT_TIME);
1738 wpabuf_put_u8(buf, WLAN_EID_SSID);
1739 wpabuf_put_u8(buf, P2P_WILDCARD_SSID_LEN);
1740 wpabuf_put_data(buf, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
1742 wpabuf_put_u8(buf, WLAN_EID_SUPP_RATES);
1743 wpabuf_put_u8(buf, 8);
1744 wpabuf_put_u8(buf, (60 / 5) | 0x80);
1745 wpabuf_put_u8(buf, 90 / 5);
1746 wpabuf_put_u8(buf, (120 / 5) | 0x80);
1747 wpabuf_put_u8(buf, 180 / 5);
1748 wpabuf_put_u8(buf, (240 / 5) | 0x80);
1749 wpabuf_put_u8(buf, 360 / 5);
1750 wpabuf_put_u8(buf, 480 / 5);
1751 wpabuf_put_u8(buf, 540 / 5);
1753 wpabuf_put_u8(buf, WLAN_EID_DS_PARAMS);
1754 wpabuf_put_u8(buf, 1);
1755 wpabuf_put_u8(buf, p2p->cfg->channel);
1757 wpabuf_put_buf(buf, ies);
1760 p2p->cfg->send_probe_resp(p2p->cfg->cb_ctx, buf);
1766 int p2p_probe_req_rx(struct p2p_data *p2p, const u8 *addr, const u8 *dst,
1767 const u8 *bssid, const u8 *ie, size_t ie_len)
1769 p2p_add_dev_from_probe_req(p2p, addr, ie, ie_len);
1771 p2p_reply_probe(p2p, addr, dst, bssid, ie, ie_len);
1773 if ((p2p->state == P2P_CONNECT || p2p->state == P2P_CONNECT_LISTEN) &&
1775 os_memcmp(addr, p2p->go_neg_peer->info.p2p_device_addr, ETH_ALEN)
1777 /* Received a Probe Request from GO Negotiation peer */
1778 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1779 "P2P: Found GO Negotiation peer - try to start GO "
1780 "negotiation from timeout");
1781 eloop_register_timeout(0, 0, p2p_go_neg_start, p2p, NULL);
1785 if ((p2p->state == P2P_INVITE || p2p->state == P2P_INVITE_LISTEN) &&
1787 os_memcmp(addr, p2p->invite_peer->info.p2p_device_addr, ETH_ALEN)
1789 /* Received a Probe Request from Invite peer */
1790 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1791 "P2P: Found Invite peer - try to start Invite from "
1793 eloop_register_timeout(0, 0, p2p_invite_start, p2p, NULL);
1801 static int p2p_assoc_req_ie_wlan_ap(struct p2p_data *p2p, const u8 *bssid,
1802 u8 *buf, size_t len, struct wpabuf *p2p_ie)
1811 return 0; /* WLAN AP is not a P2P manager */
1814 * (Re)Association Request - P2P IE
1815 * P2P Capability attribute (shall be present)
1816 * P2P Interface attribute (present if concurrent device and
1817 * P2P Management is enabled)
1819 tmp = wpabuf_alloc(200);
1823 lpos = p2p_buf_add_ie_hdr(tmp);
1825 if (p2p->num_groups > 0) {
1826 group_capab |= P2P_GROUP_CAPAB_GROUP_OWNER;
1827 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1828 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED) &&
1830 group_capab |= P2P_GROUP_CAPAB_CROSS_CONN;
1832 p2p_buf_add_capability(tmp, p2p->dev_capab, group_capab);
1833 if ((p2p->dev_capab & P2P_DEV_CAPAB_CONCURRENT_OPER) &&
1834 (p2p->dev_capab & P2P_DEV_CAPAB_INFRA_MANAGED))
1835 p2p_buf_add_p2p_interface(tmp, p2p);
1836 p2p_buf_update_ie_hdr(tmp, lpos);
1838 tmplen = wpabuf_len(tmp);
1842 os_memcpy(buf, wpabuf_head(tmp), tmplen);
1851 int p2p_assoc_req_ie(struct p2p_data *p2p, const u8 *bssid, u8 *buf,
1852 size_t len, int p2p_group, struct wpabuf *p2p_ie)
1856 struct p2p_device *peer;
1861 return p2p_assoc_req_ie_wlan_ap(p2p, bssid, buf, len, p2p_ie);
1864 * (Re)Association Request - P2P IE
1865 * P2P Capability attribute (shall be present)
1866 * Extended Listen Timing (may be present)
1867 * P2P Device Info attribute (shall be present)
1869 tmp = wpabuf_alloc(200);
1873 peer = bssid ? p2p_get_device(p2p, bssid) : NULL;
1875 lpos = p2p_buf_add_ie_hdr(tmp);
1876 p2p_buf_add_capability(tmp, p2p->dev_capab, 0);
1877 if (p2p->ext_listen_interval)
1878 p2p_buf_add_ext_listen_timing(tmp, p2p->ext_listen_period,
1879 p2p->ext_listen_interval);
1880 p2p_buf_add_device_info(tmp, p2p, peer);
1881 p2p_buf_update_ie_hdr(tmp, lpos);
1883 tmplen = wpabuf_len(tmp);
1887 os_memcpy(buf, wpabuf_head(tmp), tmplen);
1896 int p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf, char *end)
1898 struct wpabuf *p2p_ie;
1901 p2p_ie = ieee802_11_vendor_ie_concat(ies, ies_len, P2P_IE_VENDOR_TYPE);
1905 ret = p2p_attr_text(p2p_ie, buf, end);
1906 wpabuf_free(p2p_ie);
1911 static void p2p_clear_go_neg(struct p2p_data *p2p)
1913 p2p->go_neg_peer = NULL;
1914 p2p_clear_timeout(p2p);
1915 p2p_set_state(p2p, P2P_IDLE);
1919 void p2p_wps_success_cb(struct p2p_data *p2p, const u8 *mac_addr)
1921 if (p2p->go_neg_peer == NULL) {
1922 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1923 "P2P: No pending Group Formation - "
1924 "ignore WPS registration success notification");
1925 return; /* No pending Group Formation */
1928 if (os_memcmp(mac_addr, p2p->go_neg_peer->intended_addr, ETH_ALEN) !=
1930 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1931 "P2P: Ignore WPS registration success notification "
1932 "for " MACSTR " (GO Negotiation peer " MACSTR ")",
1934 MAC2STR(p2p->go_neg_peer->intended_addr));
1935 return; /* Ignore unexpected peer address */
1938 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1939 "P2P: Group Formation completed successfully with " MACSTR,
1942 p2p_clear_go_neg(p2p);
1946 void p2p_group_formation_failed(struct p2p_data *p2p)
1948 if (p2p->go_neg_peer == NULL) {
1949 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1950 "P2P: No pending Group Formation - "
1951 "ignore group formation failure notification");
1952 return; /* No pending Group Formation */
1955 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
1956 "P2P: Group Formation failed with " MACSTR,
1957 MAC2STR(p2p->go_neg_peer->intended_addr));
1959 p2p_clear_go_neg(p2p);
1963 struct p2p_data * p2p_init(const struct p2p_config *cfg)
1965 struct p2p_data *p2p;
1967 if (cfg->max_peers < 1)
1970 p2p = os_zalloc(sizeof(*p2p) + sizeof(*cfg));
1973 p2p->cfg = (struct p2p_config *) (p2p + 1);
1974 os_memcpy(p2p->cfg, cfg, sizeof(*cfg));
1976 p2p->cfg->dev_name = os_strdup(cfg->dev_name);
1977 if (cfg->manufacturer)
1978 p2p->cfg->manufacturer = os_strdup(cfg->manufacturer);
1979 if (cfg->model_name)
1980 p2p->cfg->model_name = os_strdup(cfg->model_name);
1981 if (cfg->model_number)
1982 p2p->cfg->model_number = os_strdup(cfg->model_number);
1983 if (cfg->serial_number)
1984 p2p->cfg->serial_number = os_strdup(cfg->serial_number);
1986 p2p->min_disc_int = 1;
1987 p2p->max_disc_int = 3;
1989 os_get_random(&p2p->next_tie_breaker, 1);
1990 p2p->next_tie_breaker &= 0x01;
1991 if (cfg->sd_request)
1992 p2p->dev_capab |= P2P_DEV_CAPAB_SERVICE_DISCOVERY;
1993 p2p->dev_capab |= P2P_DEV_CAPAB_INVITATION_PROCEDURE;
1994 if (cfg->concurrent_operations)
1995 p2p->dev_capab |= P2P_DEV_CAPAB_CONCURRENT_OPER;
1996 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
1998 dl_list_init(&p2p->devices);
2000 eloop_register_timeout(P2P_PEER_EXPIRATION_INTERVAL, 0,
2001 p2p_expiration_timeout, p2p, NULL);
2007 void p2p_deinit(struct p2p_data *p2p)
2009 eloop_cancel_timeout(p2p_expiration_timeout, p2p, NULL);
2010 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
2011 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2013 p2p_free_req_dev_types(p2p);
2014 os_free(p2p->cfg->dev_name);
2015 os_free(p2p->cfg->manufacturer);
2016 os_free(p2p->cfg->model_name);
2017 os_free(p2p->cfg->model_number);
2018 os_free(p2p->cfg->serial_number);
2019 os_free(p2p->groups);
2020 wpabuf_free(p2p->sd_resp);
2021 os_free(p2p->after_scan_tx);
2022 p2p_remove_wps_vendor_extensions(p2p);
2027 void p2p_flush(struct p2p_data *p2p)
2029 struct p2p_device *dev, *prev;
2030 p2p_clear_timeout(p2p);
2031 p2p_set_state(p2p, P2P_IDLE);
2032 p2p->start_after_scan = P2P_AFTER_SCAN_NOTHING;
2033 p2p->go_neg_peer = NULL;
2034 eloop_cancel_timeout(p2p_find_timeout, p2p, NULL);
2035 dl_list_for_each_safe(dev, prev, &p2p->devices, struct p2p_device,
2037 dl_list_del(&dev->list);
2038 p2p_device_free(p2p, dev);
2040 p2p_free_sd_queries(p2p);
2041 os_free(p2p->after_scan_tx);
2042 p2p->after_scan_tx = NULL;
2046 int p2p_unauthorize(struct p2p_data *p2p, const u8 *addr)
2048 struct p2p_device *dev;
2050 dev = p2p_get_device(p2p, addr);
2054 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Unauthorizing " MACSTR,
2057 if (p2p->go_neg_peer == dev)
2058 p2p->go_neg_peer = NULL;
2060 dev->wps_method = WPS_NOT_READY;
2061 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_RESPONSE;
2062 dev->flags &= ~P2P_DEV_WAIT_GO_NEG_CONFIRM;
2064 /* Check if after_scan_tx is for this peer. If so free it */
2065 if (p2p->after_scan_tx &&
2066 os_memcmp(addr, p2p->after_scan_tx->dst, ETH_ALEN) == 0) {
2067 os_free(p2p->after_scan_tx);
2068 p2p->after_scan_tx = NULL;
2075 int p2p_set_dev_name(struct p2p_data *p2p, const char *dev_name)
2077 os_free(p2p->cfg->dev_name);
2079 p2p->cfg->dev_name = os_strdup(dev_name);
2080 if (p2p->cfg->dev_name == NULL)
2083 p2p->cfg->dev_name = NULL;
2088 int p2p_set_manufacturer(struct p2p_data *p2p, const char *manufacturer)
2090 os_free(p2p->cfg->manufacturer);
2091 p2p->cfg->manufacturer = NULL;
2093 p2p->cfg->manufacturer = os_strdup(manufacturer);
2094 if (p2p->cfg->manufacturer == NULL)
2102 int p2p_set_model_name(struct p2p_data *p2p, const char *model_name)
2104 os_free(p2p->cfg->model_name);
2105 p2p->cfg->model_name = NULL;
2107 p2p->cfg->model_name = os_strdup(model_name);
2108 if (p2p->cfg->model_name == NULL)
2116 int p2p_set_model_number(struct p2p_data *p2p, const char *model_number)
2118 os_free(p2p->cfg->model_number);
2119 p2p->cfg->model_number = NULL;
2121 p2p->cfg->model_number = os_strdup(model_number);
2122 if (p2p->cfg->model_number == NULL)
2130 int p2p_set_serial_number(struct p2p_data *p2p, const char *serial_number)
2132 os_free(p2p->cfg->serial_number);
2133 p2p->cfg->serial_number = NULL;
2134 if (serial_number) {
2135 p2p->cfg->serial_number = os_strdup(serial_number);
2136 if (p2p->cfg->serial_number == NULL)
2144 void p2p_set_config_methods(struct p2p_data *p2p, u16 config_methods)
2146 p2p->cfg->config_methods = config_methods;
2150 void p2p_set_uuid(struct p2p_data *p2p, const u8 *uuid)
2152 os_memcpy(p2p->cfg->uuid, uuid, 16);
2156 int p2p_set_pri_dev_type(struct p2p_data *p2p, const u8 *pri_dev_type)
2158 os_memcpy(p2p->cfg->pri_dev_type, pri_dev_type, 8);
2163 int p2p_set_sec_dev_types(struct p2p_data *p2p, const u8 dev_types[][8],
2164 size_t num_dev_types)
2166 if (num_dev_types > P2P_SEC_DEVICE_TYPES)
2167 num_dev_types = P2P_SEC_DEVICE_TYPES;
2168 p2p->cfg->num_sec_dev_types = num_dev_types;
2169 os_memcpy(p2p->cfg->sec_dev_type, dev_types, num_dev_types * 8);
2174 void p2p_remove_wps_vendor_extensions(struct p2p_data *p2p)
2178 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2179 wpabuf_free(p2p->wps_vendor_ext[i]);
2180 p2p->wps_vendor_ext[i] = NULL;
2185 int p2p_add_wps_vendor_extension(struct p2p_data *p2p,
2186 const struct wpabuf *vendor_ext)
2190 if (vendor_ext == NULL)
2193 for (i = 0; i < P2P_MAX_WPS_VENDOR_EXT; i++) {
2194 if (p2p->wps_vendor_ext[i] == NULL)
2197 if (i >= P2P_MAX_WPS_VENDOR_EXT)
2200 p2p->wps_vendor_ext[i] = wpabuf_dup(vendor_ext);
2201 if (p2p->wps_vendor_ext[i] == NULL)
2208 int p2p_set_country(struct p2p_data *p2p, const char *country)
2210 os_memcpy(p2p->cfg->country, country, 3);
2215 void p2p_continue_find(struct p2p_data *p2p)
2217 struct p2p_device *dev;
2218 p2p_set_state(p2p, P2P_SEARCH);
2219 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2220 if (dev->flags & P2P_DEV_SD_SCHEDULE) {
2221 if (p2p_start_sd(p2p, dev) == 0)
2225 } else if (dev->req_config_methods &&
2226 !(dev->flags & P2P_DEV_PD_FOR_JOIN)) {
2227 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2228 "pending Provisioning Discovery Request to "
2229 MACSTR " (config methods 0x%x)",
2230 MAC2STR(dev->info.p2p_device_addr),
2231 dev->req_config_methods);
2232 if (p2p_send_prov_disc_req(p2p, dev, 0) == 0)
2237 p2p_listen_in_find(p2p);
2241 static void p2p_sd_cb(struct p2p_data *p2p, int success)
2243 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2244 "P2P: Service Discovery Query TX callback: success=%d",
2246 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2250 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2251 p2p->sd_peer = NULL;
2253 p2p_continue_find(p2p);
2257 if (p2p->sd_peer == NULL) {
2258 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2259 "P2P: No SD peer entry known");
2260 p2p_continue_find(p2p);
2264 /* Wait for response from the peer */
2265 p2p_set_state(p2p, P2P_SD_DURING_FIND);
2266 p2p_set_timeout(p2p, 0, 200000);
2271 * p2p_retry_pd - Retry any pending provision disc requests in IDLE state
2272 * @p2p: P2P module context from p2p_init()
2274 void p2p_retry_pd(struct p2p_data *p2p)
2276 struct p2p_device *dev;
2278 if (p2p->state != P2P_IDLE)
2282 * Retry the prov disc req attempt only for the peer that the user had
2283 * requested for and provided a join has not been initiated on it
2287 dl_list_for_each(dev, &p2p->devices, struct p2p_device, list) {
2288 if (os_memcmp(p2p->pending_pd_devaddr,
2289 dev->info.p2p_device_addr, ETH_ALEN) != 0)
2291 if (!dev->req_config_methods)
2293 if (dev->flags & P2P_DEV_PD_FOR_JOIN)
2296 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send "
2297 "pending Provisioning Discovery Request to "
2298 MACSTR " (config methods 0x%x)",
2299 MAC2STR(dev->info.p2p_device_addr),
2300 dev->req_config_methods);
2301 p2p_send_prov_disc_req(p2p, dev, 0);
2307 static void p2p_prov_disc_cb(struct p2p_data *p2p, int success)
2309 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2310 "P2P: Provision Discovery Request TX callback: success=%d",
2314 * Postpone resetting the pending action state till after we actually
2315 * time out. This allows us to take some action like notifying any
2316 * interested parties about no response to the request.
2318 * When the timer (below) goes off we check in IDLE, SEARCH, or
2319 * LISTEN_ONLY state, which are the only allowed states to issue a PD
2320 * requests in, if this was still pending and then raise notification.
2324 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2326 if (p2p->state != P2P_IDLE)
2327 p2p_continue_find(p2p);
2328 else if (p2p->user_initiated_pd) {
2329 p2p->pending_action_state = P2P_PENDING_PD;
2330 p2p_set_timeout(p2p, 0, 300000);
2336 * This postponing, of resetting pending_action_state, needs to be
2337 * done only for user initiated PD requests and not internal ones.
2339 if (p2p->user_initiated_pd)
2340 p2p->pending_action_state = P2P_PENDING_PD;
2342 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2344 /* Wait for response from the peer */
2345 if (p2p->state == P2P_SEARCH)
2346 p2p_set_state(p2p, P2P_PD_DURING_FIND);
2347 p2p_set_timeout(p2p, 0, 200000);
2351 int p2p_scan_res_handler(struct p2p_data *p2p, const u8 *bssid, int freq,
2352 int level, const u8 *ies, size_t ies_len)
2354 p2p_add_device(p2p, bssid, freq, level, ies, ies_len);
2356 if (p2p->go_neg_peer && p2p->state == P2P_SEARCH &&
2357 os_memcmp(p2p->go_neg_peer->info.p2p_device_addr, bssid, ETH_ALEN)
2359 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2360 "P2P: Found GO Negotiation peer - try to start GO "
2362 p2p_connect_send(p2p, p2p->go_neg_peer);
2370 void p2p_scan_res_handled(struct p2p_data *p2p)
2372 if (!p2p->p2p_scan_running) {
2373 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: p2p_scan was not "
2374 "running, but scan results received");
2376 p2p->p2p_scan_running = 0;
2377 eloop_cancel_timeout(p2p_scan_timeout, p2p, NULL);
2379 if (p2p_run_after_scan(p2p))
2381 if (p2p->state == P2P_SEARCH)
2382 p2p_continue_find(p2p);
2386 void p2p_scan_ie(struct p2p_data *p2p, struct wpabuf *ies)
2388 u8 *len = p2p_buf_add_ie_hdr(ies);
2389 p2p_buf_add_capability(ies, p2p->dev_capab, 0);
2390 if (p2p->cfg->reg_class && p2p->cfg->channel)
2391 p2p_buf_add_listen_channel(ies, p2p->cfg->country,
2392 p2p->cfg->reg_class,
2394 if (p2p->ext_listen_interval)
2395 p2p_buf_add_ext_listen_timing(ies, p2p->ext_listen_period,
2396 p2p->ext_listen_interval);
2397 /* TODO: p2p_buf_add_operating_channel() if GO */
2398 p2p_buf_update_ie_hdr(ies, len);
2402 int p2p_ie_text(struct wpabuf *p2p_ie, char *buf, char *end)
2404 return p2p_attr_text(p2p_ie, buf, end);
2408 static void p2p_go_neg_req_cb(struct p2p_data *p2p, int success)
2410 struct p2p_device *dev = p2p->go_neg_peer;
2412 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2413 "P2P: GO Negotiation Request TX callback: success=%d",
2417 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2418 "P2P: No pending GO Negotiation");
2423 dev->go_neg_req_sent++;
2424 if (dev->flags & P2P_DEV_USER_REJECTED) {
2425 p2p_set_state(p2p, P2P_IDLE);
2431 (dev->info.dev_capab & P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY) &&
2432 !is_zero_ether_addr(dev->member_in_go_dev)) {
2433 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2434 "P2P: Peer " MACSTR " did not acknowledge request - "
2435 "try to use device discoverability through its GO",
2436 MAC2STR(dev->info.p2p_device_addr));
2437 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2438 p2p_send_dev_disc_req(p2p, dev);
2443 * Use P2P find, if needed, to find the other device from its listen
2446 p2p_set_state(p2p, P2P_CONNECT);
2447 p2p_set_timeout(p2p, 0, 100000);
2451 static void p2p_go_neg_resp_cb(struct p2p_data *p2p, int success)
2453 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2454 "P2P: GO Negotiation Response TX callback: success=%d",
2456 if (!p2p->go_neg_peer && p2p->state == P2P_PROVISIONING) {
2457 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2458 "P2P: Ignore TX callback event - GO Negotiation is "
2459 "not running anymore");
2462 p2p_set_state(p2p, P2P_CONNECT);
2463 p2p_set_timeout(p2p, 0, 100000);
2467 static void p2p_go_neg_resp_failure_cb(struct p2p_data *p2p, int success)
2469 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2470 "P2P: GO Negotiation Response (failure) TX callback: "
2471 "success=%d", success);
2472 if (p2p->go_neg_peer && p2p->go_neg_peer->status != P2P_SC_SUCCESS) {
2473 p2p_go_neg_failed(p2p, p2p->go_neg_peer,
2474 p2p->go_neg_peer->status);
2479 static void p2p_go_neg_conf_cb(struct p2p_data *p2p,
2480 enum p2p_send_action_result result)
2482 struct p2p_device *dev;
2484 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2485 "P2P: GO Negotiation Confirm TX callback: result=%d",
2487 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2488 if (result == P2P_SEND_ACTION_FAILED) {
2489 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2492 if (result == P2P_SEND_ACTION_NO_ACK) {
2494 * It looks like the TX status for GO Negotiation Confirm is
2495 * often showing failure even when the peer has actually
2496 * received the frame. Since the peer may change channels
2497 * immediately after having received the frame, we may not see
2498 * an Ack for retries, so just dropping a single frame may
2499 * trigger this. To allow the group formation to succeed if the
2500 * peer did indeed receive the frame, continue regardless of
2503 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2504 "P2P: Assume GO Negotiation Confirm TX was actually "
2505 "received by the peer even though Ack was not "
2509 dev = p2p->go_neg_peer;
2513 p2p_go_complete(p2p, dev);
2517 void p2p_send_action_cb(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
2518 const u8 *src, const u8 *bssid,
2519 enum p2p_send_action_result result)
2521 enum p2p_pending_action_state state;
2524 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2525 "P2P: Action frame TX callback (state=%d freq=%u dst=" MACSTR
2526 " src=" MACSTR " bssid=" MACSTR " result=%d",
2527 p2p->pending_action_state, freq, MAC2STR(dst), MAC2STR(src),
2528 MAC2STR(bssid), result);
2529 success = result == P2P_SEND_ACTION_SUCCESS;
2530 state = p2p->pending_action_state;
2531 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2533 case P2P_NO_PENDING_ACTION:
2535 case P2P_PENDING_GO_NEG_REQUEST:
2536 p2p_go_neg_req_cb(p2p, success);
2538 case P2P_PENDING_GO_NEG_RESPONSE:
2539 p2p_go_neg_resp_cb(p2p, success);
2541 case P2P_PENDING_GO_NEG_RESPONSE_FAILURE:
2542 p2p_go_neg_resp_failure_cb(p2p, success);
2544 case P2P_PENDING_GO_NEG_CONFIRM:
2545 p2p_go_neg_conf_cb(p2p, result);
2547 case P2P_PENDING_SD:
2548 p2p_sd_cb(p2p, success);
2550 case P2P_PENDING_PD:
2551 p2p_prov_disc_cb(p2p, success);
2553 case P2P_PENDING_INVITATION_REQUEST:
2554 p2p_invitation_req_cb(p2p, success);
2556 case P2P_PENDING_INVITATION_RESPONSE:
2557 p2p_invitation_resp_cb(p2p, success);
2559 case P2P_PENDING_DEV_DISC_REQUEST:
2560 p2p_dev_disc_req_cb(p2p, success);
2562 case P2P_PENDING_DEV_DISC_RESPONSE:
2563 p2p_dev_disc_resp_cb(p2p, success);
2565 case P2P_PENDING_GO_DISC_REQ:
2566 p2p_go_disc_req_cb(p2p, success);
2572 void p2p_listen_cb(struct p2p_data *p2p, unsigned int freq,
2573 unsigned int duration)
2575 if (freq == p2p->pending_client_disc_freq) {
2576 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2577 "P2P: Client discoverability remain-awake completed");
2578 p2p->pending_client_disc_freq = 0;
2582 if (freq != p2p->pending_listen_freq) {
2583 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2584 "P2P: Unexpected listen callback for freq=%u "
2585 "duration=%u (pending_listen_freq=%u)",
2586 freq, duration, p2p->pending_listen_freq);
2590 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2591 "P2P: Starting Listen timeout(%u,%u) on freq=%u based on "
2593 p2p->pending_listen_sec, p2p->pending_listen_usec,
2594 p2p->pending_listen_freq);
2596 p2p->drv_in_listen = freq;
2597 if (p2p->pending_listen_sec || p2p->pending_listen_usec) {
2599 * Add 20 msec extra wait to avoid race condition with driver
2600 * remain-on-channel end event, i.e., give driver more time to
2601 * complete the operation before our timeout expires.
2603 p2p_set_timeout(p2p, p2p->pending_listen_sec,
2604 p2p->pending_listen_usec + 20000);
2607 p2p->pending_listen_freq = 0;
2611 int p2p_listen_end(struct p2p_data *p2p, unsigned int freq)
2613 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver ended Listen "
2614 "state (freq=%u)", freq);
2615 p2p->drv_in_listen = 0;
2617 return 0; /* Internal timeout will trigger the next step */
2619 if (p2p->state == P2P_CONNECT_LISTEN && p2p->go_neg_peer) {
2620 if (p2p->go_neg_peer->connect_reqs >= 120) {
2621 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2622 "P2P: Timeout on sending GO Negotiation "
2623 "Request without getting response");
2624 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2628 p2p_set_state(p2p, P2P_CONNECT);
2629 p2p_connect_send(p2p, p2p->go_neg_peer);
2631 } else if (p2p->state == P2P_SEARCH) {
2640 static void p2p_timeout_connect(struct p2p_data *p2p)
2642 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2643 p2p_set_state(p2p, P2P_CONNECT_LISTEN);
2644 p2p_listen_in_find(p2p);
2648 static void p2p_timeout_connect_listen(struct p2p_data *p2p)
2650 if (p2p->go_neg_peer) {
2651 if (p2p->drv_in_listen) {
2652 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Driver is "
2653 "still in Listen state; wait for it to "
2658 if (p2p->go_neg_peer->connect_reqs >= 120) {
2659 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2660 "P2P: Timeout on sending GO Negotiation "
2661 "Request without getting response");
2662 p2p_go_neg_failed(p2p, p2p->go_neg_peer, -1);
2666 p2p_set_state(p2p, P2P_CONNECT);
2667 p2p_connect_send(p2p, p2p->go_neg_peer);
2669 p2p_set_state(p2p, P2P_IDLE);
2673 static void p2p_timeout_wait_peer_connect(struct p2p_data *p2p)
2676 * TODO: could remain constantly in Listen state for some time if there
2677 * are no other concurrent uses for the radio. For now, go to listen
2678 * state once per second to give other uses a chance to use the radio.
2680 p2p_set_state(p2p, P2P_WAIT_PEER_IDLE);
2681 p2p_set_timeout(p2p, 1, 0);
2685 static void p2p_timeout_wait_peer_idle(struct p2p_data *p2p)
2687 struct p2p_device *dev = p2p->go_neg_peer;
2690 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2691 "P2P: Unknown GO Neg peer - stop GO Neg wait");
2696 if (dev->wait_count >= 120) {
2697 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2698 "P2P: Timeout on waiting peer to become ready for GO "
2700 p2p_go_neg_failed(p2p, dev, -1);
2704 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2705 "P2P: Go to Listen state while waiting for the peer to become "
2706 "ready for GO Negotiation");
2707 p2p_set_state(p2p, P2P_WAIT_PEER_CONNECT);
2708 p2p_listen_in_find(p2p);
2712 static void p2p_timeout_sd_during_find(struct p2p_data *p2p)
2714 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2715 "P2P: Service Discovery Query timeout");
2717 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2718 p2p->sd_peer->flags &= ~P2P_DEV_SD_SCHEDULE;
2719 p2p->sd_peer = NULL;
2721 p2p_continue_find(p2p);
2725 static void p2p_timeout_prov_disc_during_find(struct p2p_data *p2p)
2727 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2728 "P2P: Provision Discovery Request timeout");
2729 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2730 p2p_continue_find(p2p);
2734 static void p2p_timeout_prov_disc_req(struct p2p_data *p2p)
2736 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
2739 * For user initiated PD requests that we have not gotten any responses
2740 * for while in IDLE state, we retry them a couple of times before
2743 if (!p2p->user_initiated_pd)
2746 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2747 "P2P: User initiated Provision Discovery Request timeout");
2749 if (p2p->pd_retries) {
2753 if (p2p->cfg->prov_disc_fail)
2754 p2p->cfg->prov_disc_fail(p2p->cfg->cb_ctx,
2755 p2p->pending_pd_devaddr,
2756 P2P_PROV_DISC_TIMEOUT);
2757 p2p_reset_pending_pd(p2p);
2762 static void p2p_timeout_invite(struct p2p_data *p2p)
2764 p2p->cfg->send_action_done(p2p->cfg->cb_ctx);
2765 p2p_set_state(p2p, P2P_INVITE_LISTEN);
2766 if (p2p->inv_role == P2P_INVITE_ROLE_ACTIVE_GO) {
2768 * Better remain on operating channel instead of listen channel
2769 * when running a group.
2771 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Inviting in "
2772 "active GO role - wait on operating channel");
2773 p2p_set_timeout(p2p, 0, 100000);
2776 p2p_listen_in_find(p2p);
2780 static void p2p_timeout_invite_listen(struct p2p_data *p2p)
2782 if (p2p->invite_peer && p2p->invite_peer->invitation_reqs < 100) {
2783 p2p_set_state(p2p, P2P_INVITE);
2784 p2p_invite_send(p2p, p2p->invite_peer,
2785 p2p->invite_go_dev_addr);
2787 if (p2p->invite_peer) {
2788 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2789 "P2P: Invitation Request retry limit reached");
2790 if (p2p->cfg->invitation_result)
2791 p2p->cfg->invitation_result(
2792 p2p->cfg->cb_ctx, -1, NULL);
2794 p2p_set_state(p2p, P2P_IDLE);
2799 static void p2p_state_timeout(void *eloop_ctx, void *timeout_ctx)
2801 struct p2p_data *p2p = eloop_ctx;
2803 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Timeout (state=%s)",
2804 p2p_state_txt(p2p->state));
2808 switch (p2p->state) {
2810 /* Check if we timed out waiting for PD req */
2811 if (p2p->pending_action_state == P2P_PENDING_PD)
2812 p2p_timeout_prov_disc_req(p2p);
2815 /* Check if we timed out waiting for PD req */
2816 if (p2p->pending_action_state == P2P_PENDING_PD)
2817 p2p_timeout_prov_disc_req(p2p);
2821 p2p_timeout_connect(p2p);
2823 case P2P_CONNECT_LISTEN:
2824 p2p_timeout_connect_listen(p2p);
2828 case P2P_LISTEN_ONLY:
2829 /* Check if we timed out waiting for PD req */
2830 if (p2p->pending_action_state == P2P_PENDING_PD)
2831 p2p_timeout_prov_disc_req(p2p);
2833 if (p2p->ext_listen_only) {
2834 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
2835 "P2P: Extended Listen Timing - Listen State "
2837 p2p->ext_listen_only = 0;
2838 p2p_set_state(p2p, P2P_IDLE);
2841 case P2P_WAIT_PEER_CONNECT:
2842 p2p_timeout_wait_peer_connect(p2p);
2844 case P2P_WAIT_PEER_IDLE:
2845 p2p_timeout_wait_peer_idle(p2p);
2847 case P2P_SD_DURING_FIND:
2848 p2p_timeout_sd_during_find(p2p);
2850 case P2P_PROVISIONING:
2852 case P2P_PD_DURING_FIND:
2853 p2p_timeout_prov_disc_during_find(p2p);
2856 p2p_timeout_invite(p2p);
2858 case P2P_INVITE_LISTEN:
2859 p2p_timeout_invite_listen(p2p);
2865 int p2p_reject(struct p2p_data *p2p, const u8 *peer_addr)
2867 struct p2p_device *dev;
2869 dev = p2p_get_device(p2p, peer_addr);
2870 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Local request to reject "
2871 "connection attempts by peer " MACSTR, MAC2STR(peer_addr));
2873 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Peer " MACSTR
2874 " unknown", MAC2STR(peer_addr));
2877 dev->status = P2P_SC_FAIL_REJECTED_BY_USER;
2878 dev->flags |= P2P_DEV_USER_REJECTED;
2883 static const char * p2p_wps_method_text(enum p2p_wps_method method)
2890 case WPS_PIN_DISPLAY:
2892 case WPS_PIN_KEYPAD:
2902 static const char * p2p_go_state_text(enum p2p_go_state go_state)
2917 int p2p_get_peer_info(struct p2p_data *p2p, const u8 *addr, int next,
2918 char *buf, size_t buflen)
2920 struct p2p_device *dev;
2924 char devtype[WPS_DEV_TYPE_BUFSIZE];
2927 dev = p2p_get_device(p2p, addr);
2929 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
2932 dev = dl_list_first(&dev->list, struct p2p_device, list);
2933 if (&dev->list == &p2p->devices)
2943 res = os_snprintf(pos, end - pos, MACSTR "\n",
2944 MAC2STR(dev->info.p2p_device_addr));
2945 if (res < 0 || res >= end - pos)
2950 res = os_snprintf(pos, end - pos,
2955 "interface_addr=" MACSTR "\n"
2956 "member_in_go_dev=" MACSTR "\n"
2957 "member_in_go_iface=" MACSTR "\n"
2963 "serial_number=%s\n"
2964 "config_methods=0x%x\n"
2966 "group_capab=0x%x\n"
2967 "go_neg_req_sent=%d\n"
2970 "intended_addr=" MACSTR "\n"
2973 "req_config_methods=0x%x\n"
2974 "flags=%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s\n"
2977 "invitation_reqs=%u\n",
2978 (int) (now.sec - dev->last_seen.sec),
2981 p2p_wps_method_text(dev->wps_method),
2982 MAC2STR(dev->interface_addr),
2983 MAC2STR(dev->member_in_go_dev),
2984 MAC2STR(dev->member_in_go_iface),
2985 wps_dev_type_bin2str(dev->info.pri_dev_type,
2986 devtype, sizeof(devtype)),
2987 dev->info.device_name,
2988 dev->info.manufacturer,
2989 dev->info.model_name,
2990 dev->info.model_number,
2991 dev->info.serial_number,
2992 dev->info.config_methods,
2993 dev->info.dev_capab,
2994 dev->info.group_capab,
2995 dev->go_neg_req_sent,
2996 p2p_go_state_text(dev->go_state),
2998 MAC2STR(dev->intended_addr),
2999 dev->country[0] ? dev->country[0] : '_',
3000 dev->country[1] ? dev->country[1] : '_',
3002 dev->req_config_methods,
3003 dev->flags & P2P_DEV_PROBE_REQ_ONLY ?
3004 "[PROBE_REQ_ONLY]" : "",
3005 dev->flags & P2P_DEV_REPORTED ? "[REPORTED]" : "",
3006 dev->flags & P2P_DEV_NOT_YET_READY ?
3007 "[NOT_YET_READY]" : "",
3008 dev->flags & P2P_DEV_SD_INFO ? "[SD_INFO]" : "",
3009 dev->flags & P2P_DEV_SD_SCHEDULE ? "[SD_SCHEDULE]" :
3011 dev->flags & P2P_DEV_PD_PEER_DISPLAY ?
3012 "[PD_PEER_DISPLAY]" : "",
3013 dev->flags & P2P_DEV_PD_PEER_KEYPAD ?
3014 "[PD_PEER_KEYPAD]" : "",
3015 dev->flags & P2P_DEV_USER_REJECTED ?
3016 "[USER_REJECTED]" : "",
3017 dev->flags & P2P_DEV_PEER_WAITING_RESPONSE ?
3018 "[PEER_WAITING_RESPONSE]" : "",
3019 dev->flags & P2P_DEV_PREFER_PERSISTENT_GROUP ?
3020 "[PREFER_PERSISTENT_GROUP]" : "",
3021 dev->flags & P2P_DEV_WAIT_GO_NEG_RESPONSE ?
3022 "[WAIT_GO_NEG_RESPONSE]" : "",
3023 dev->flags & P2P_DEV_WAIT_GO_NEG_CONFIRM ?
3024 "[WAIT_GO_NEG_CONFIRM]" : "",
3025 dev->flags & P2P_DEV_GROUP_CLIENT_ONLY ?
3026 "[GROUP_CLIENT_ONLY]" : "",
3027 dev->flags & P2P_DEV_FORCE_FREQ ?
3028 "[FORCE_FREQ]" : "",
3029 dev->flags & P2P_DEV_PD_FOR_JOIN ?
3030 "[PD_FOR_JOIN]" : "",
3033 dev->invitation_reqs);
3034 if (res < 0 || res >= end - pos)
3038 if (dev->ext_listen_period) {
3039 res = os_snprintf(pos, end - pos,
3040 "ext_listen_period=%u\n"
3041 "ext_listen_interval=%u\n",
3042 dev->ext_listen_period,
3043 dev->ext_listen_interval);
3044 if (res < 0 || res >= end - pos)
3049 if (dev->oper_ssid_len) {
3050 res = os_snprintf(pos, end - pos,
3052 wpa_ssid_txt(dev->oper_ssid,
3053 dev->oper_ssid_len));
3054 if (res < 0 || res >= end - pos)
3063 void p2p_set_client_discoverability(struct p2p_data *p2p, int enabled)
3066 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3067 "discoverability enabled");
3068 p2p->dev_capab |= P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3070 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Client "
3071 "discoverability disabled");
3072 p2p->dev_capab &= ~P2P_DEV_CAPAB_CLIENT_DISCOVERABILITY;
3077 static struct wpabuf * p2p_build_presence_req(u32 duration1, u32 interval1,
3078 u32 duration2, u32 interval2)
3081 struct p2p_noa_desc desc1, desc2, *ptr1 = NULL, *ptr2 = NULL;
3084 req = wpabuf_alloc(100);
3088 if (duration1 || interval1) {
3089 os_memset(&desc1, 0, sizeof(desc1));
3090 desc1.count_type = 1;
3091 desc1.duration = duration1;
3092 desc1.interval = interval1;
3095 if (duration2 || interval2) {
3096 os_memset(&desc2, 0, sizeof(desc2));
3097 desc2.count_type = 2;
3098 desc2.duration = duration2;
3099 desc2.interval = interval2;
3104 p2p_buf_add_action_hdr(req, P2P_PRESENCE_REQ, 1);
3105 len = p2p_buf_add_ie_hdr(req);
3106 p2p_buf_add_noa(req, 0, 0, 0, ptr1, ptr2);
3107 p2p_buf_update_ie_hdr(req, len);
3113 int p2p_presence_req(struct p2p_data *p2p, const u8 *go_interface_addr,
3114 const u8 *own_interface_addr, unsigned int freq,
3115 u32 duration1, u32 interval1, u32 duration2,
3120 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Send Presence Request to "
3121 "GO " MACSTR " (own interface " MACSTR ") freq=%u dur1=%u "
3122 "int1=%u dur2=%u int2=%u",
3123 MAC2STR(go_interface_addr), MAC2STR(own_interface_addr),
3124 freq, duration1, interval1, duration2, interval2);
3126 req = p2p_build_presence_req(duration1, interval1, duration2,
3131 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3132 if (p2p_send_action(p2p, freq, go_interface_addr, own_interface_addr,
3134 wpabuf_head(req), wpabuf_len(req), 200) < 0) {
3135 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3136 "P2P: Failed to send Action frame");
3144 static struct wpabuf * p2p_build_presence_resp(u8 status, const u8 *noa,
3145 size_t noa_len, u8 dialog_token)
3147 struct wpabuf *resp;
3150 resp = wpabuf_alloc(100 + noa_len);
3154 p2p_buf_add_action_hdr(resp, P2P_PRESENCE_RESP, dialog_token);
3155 len = p2p_buf_add_ie_hdr(resp);
3156 p2p_buf_add_status(resp, status);
3158 wpabuf_put_u8(resp, P2P_ATTR_NOTICE_OF_ABSENCE);
3159 wpabuf_put_le16(resp, noa_len);
3160 wpabuf_put_data(resp, noa, noa_len);
3162 p2p_buf_add_noa(resp, 0, 0, 0, NULL, NULL);
3163 p2p_buf_update_ie_hdr(resp, len);
3169 static void p2p_process_presence_req(struct p2p_data *p2p, const u8 *da,
3170 const u8 *sa, const u8 *data, size_t len,
3173 struct p2p_message msg;
3175 struct wpabuf *resp;
3177 struct p2p_group *group = NULL;
3182 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3183 "P2P: Received P2P Action - P2P Presence Request");
3185 for (g = 0; g < p2p->num_groups; g++) {
3186 if (os_memcmp(da, p2p_group_get_interface_addr(p2p->groups[g]),
3188 group = p2p->groups[g];
3192 if (group == NULL) {
3193 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3194 "P2P: Ignore P2P Presence Request for unknown group "
3195 MACSTR, MAC2STR(da));
3199 if (p2p_parse(data, len, &msg) < 0) {
3200 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3201 "P2P: Failed to parse P2P Presence Request");
3202 status = P2P_SC_FAIL_INVALID_PARAMS;
3207 if (msg.noa == NULL) {
3208 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3209 "P2P: No NoA attribute in P2P Presence Request");
3210 status = P2P_SC_FAIL_INVALID_PARAMS;
3214 status = p2p_group_presence_req(group, sa, msg.noa, msg.noa_len);
3217 if (p2p->cfg->get_noa)
3218 noa_len = p2p->cfg->get_noa(p2p->cfg->cb_ctx, da, noa,
3222 resp = p2p_build_presence_resp(status, noa_len > 0 ? noa : NULL,
3223 noa_len > 0 ? noa_len : 0,
3226 p2p_parse_free(&msg);
3230 p2p->pending_action_state = P2P_NO_PENDING_ACTION;
3231 if (p2p_send_action(p2p, rx_freq, sa, da, da,
3232 wpabuf_head(resp), wpabuf_len(resp), 200) < 0) {
3233 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3234 "P2P: Failed to send Action frame");
3240 static void p2p_process_presence_resp(struct p2p_data *p2p, const u8 *da,
3241 const u8 *sa, const u8 *data, size_t len)
3243 struct p2p_message msg;
3245 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3246 "P2P: Received P2P Action - P2P Presence Response");
3248 if (p2p_parse(data, len, &msg) < 0) {
3249 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3250 "P2P: Failed to parse P2P Presence Response");
3254 if (msg.status == NULL || msg.noa == NULL) {
3255 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3256 "P2P: No Status or NoA attribute in P2P Presence "
3258 p2p_parse_free(&msg);
3263 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3264 "P2P: P2P Presence Request was rejected: status %u",
3266 p2p_parse_free(&msg);
3270 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3271 "P2P: P2P Presence Request was accepted");
3272 wpa_hexdump(MSG_DEBUG, "P2P: P2P Presence Response - NoA",
3273 msg.noa, msg.noa_len);
3274 /* TODO: process NoA */
3275 p2p_parse_free(&msg);
3279 static void p2p_ext_listen_timeout(void *eloop_ctx, void *timeout_ctx)
3281 struct p2p_data *p2p = eloop_ctx;
3283 if (p2p->ext_listen_interval) {
3284 /* Schedule next extended listen timeout */
3285 eloop_register_timeout(p2p->ext_listen_interval_sec,
3286 p2p->ext_listen_interval_usec,
3287 p2p_ext_listen_timeout, p2p, NULL);
3290 if (p2p->state == P2P_LISTEN_ONLY && p2p->ext_listen_only) {
3292 * This should not really happen, but it looks like the Listen
3293 * command may fail is something else (e.g., a scan) was
3294 * running at an inconvenient time. As a workaround, allow new
3295 * Extended Listen operation to be started.
3297 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Previous "
3298 "Extended Listen operation had not been completed - "
3300 p2p->ext_listen_only = 0;
3301 p2p_set_state(p2p, P2P_IDLE);
3304 if (p2p->state != P2P_IDLE) {
3305 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Skip Extended "
3306 "Listen timeout in active state (%s)",
3307 p2p_state_txt(p2p->state));
3311 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Extended Listen timeout");
3312 p2p->ext_listen_only = 1;
3313 if (p2p_listen(p2p, p2p->ext_listen_period) < 0) {
3314 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Failed to start "
3315 "Listen state for Extended Listen Timing");
3316 p2p->ext_listen_only = 0;
3321 int p2p_ext_listen(struct p2p_data *p2p, unsigned int period,
3322 unsigned int interval)
3324 if (period > 65535 || interval > 65535 || period > interval ||
3325 (period == 0 && interval > 0) || (period > 0 && interval == 0)) {
3326 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3327 "P2P: Invalid Extended Listen Timing request: "
3328 "period=%u interval=%u", period, interval);
3332 eloop_cancel_timeout(p2p_ext_listen_timeout, p2p, NULL);
3334 if (interval == 0) {
3335 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3336 "P2P: Disabling Extended Listen Timing");
3337 p2p->ext_listen_period = 0;
3338 p2p->ext_listen_interval = 0;
3342 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG,
3343 "P2P: Enabling Extended Listen Timing: period %u msec, "
3344 "interval %u msec", period, interval);
3345 p2p->ext_listen_period = period;
3346 p2p->ext_listen_interval = interval;
3347 p2p->ext_listen_interval_sec = interval / 1000;
3348 p2p->ext_listen_interval_usec = (interval % 1000) * 1000;
3350 eloop_register_timeout(p2p->ext_listen_interval_sec,
3351 p2p->ext_listen_interval_usec,
3352 p2p_ext_listen_timeout, p2p, NULL);
3358 void p2p_deauth_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3359 const u8 *ie, size_t ie_len)
3361 struct p2p_message msg;
3363 if (bssid == NULL || ie == NULL)
3366 os_memset(&msg, 0, sizeof(msg));
3367 if (p2p_parse_ies(ie, ie_len, &msg))
3369 if (msg.minor_reason_code == NULL)
3372 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3373 "P2P: Deauthentication notification BSSID " MACSTR
3374 " reason_code=%u minor_reason_code=%u",
3375 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3377 p2p_parse_free(&msg);
3381 void p2p_disassoc_notif(struct p2p_data *p2p, const u8 *bssid, u16 reason_code,
3382 const u8 *ie, size_t ie_len)
3384 struct p2p_message msg;
3386 if (bssid == NULL || ie == NULL)
3389 os_memset(&msg, 0, sizeof(msg));
3390 if (p2p_parse_ies(ie, ie_len, &msg))
3392 if (msg.minor_reason_code == NULL)
3395 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO,
3396 "P2P: Disassociation notification BSSID " MACSTR
3397 " reason_code=%u minor_reason_code=%u",
3398 MAC2STR(bssid), reason_code, *msg.minor_reason_code);
3400 p2p_parse_free(&msg);
3404 void p2p_set_managed_oper(struct p2p_data *p2p, int enabled)
3407 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3408 "Device operations enabled");
3409 p2p->dev_capab |= P2P_DEV_CAPAB_INFRA_MANAGED;
3411 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Managed P2P "
3412 "Device operations disabled");
3413 p2p->dev_capab &= ~P2P_DEV_CAPAB_INFRA_MANAGED;
3418 int p2p_set_listen_channel(struct p2p_data *p2p, u8 reg_class, u8 channel)
3420 if (p2p_channel_to_freq(p2p->cfg->country, reg_class, channel) < 0)
3423 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Set Listen channel: "
3424 "reg_class %u channel %u", reg_class, channel);
3425 p2p->cfg->reg_class = reg_class;
3426 p2p->cfg->channel = channel;
3432 int p2p_set_ssid_postfix(struct p2p_data *p2p, const u8 *postfix, size_t len)
3434 wpa_hexdump_ascii(MSG_DEBUG, "P2P: New SSID postfix", postfix, len);
3435 if (postfix == NULL) {
3436 p2p->cfg->ssid_postfix_len = 0;
3439 if (len > sizeof(p2p->cfg->ssid_postfix))
3441 os_memcpy(p2p->cfg->ssid_postfix, postfix, len);
3442 p2p->cfg->ssid_postfix_len = len;
3447 int p2p_set_oper_channel(struct p2p_data *p2p, u8 op_reg_class, u8 op_channel,
3450 if (p2p_channel_to_freq(p2p->cfg->country, op_reg_class, op_channel)
3454 wpa_msg(p2p->cfg->msg_ctx, MSG_INFO, "P2P: Set Operating channel: "
3455 "reg_class %u channel %u", op_reg_class, op_channel);
3456 p2p->cfg->op_reg_class = op_reg_class;
3457 p2p->cfg->op_channel = op_channel;
3458 p2p->cfg->cfg_op_channel = cfg_op_channel;
3463 int p2p_get_interface_addr(struct p2p_data *p2p, const u8 *dev_addr,
3466 struct p2p_device *dev = p2p_get_device(p2p, dev_addr);
3467 if (dev == NULL || is_zero_ether_addr(dev->interface_addr))
3469 os_memcpy(iface_addr, dev->interface_addr, ETH_ALEN);
3474 int p2p_get_dev_addr(struct p2p_data *p2p, const u8 *iface_addr,
3477 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3480 os_memcpy(dev_addr, dev->info.p2p_device_addr, ETH_ALEN);
3485 void p2p_set_peer_filter(struct p2p_data *p2p, const u8 *addr)
3487 os_memcpy(p2p->peer_filter, addr, ETH_ALEN);
3488 if (is_zero_ether_addr(p2p->peer_filter))
3489 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Disable peer "
3492 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Enable peer "
3493 "filter for " MACSTR, MAC2STR(p2p->peer_filter));
3497 void p2p_set_cross_connect(struct p2p_data *p2p, int enabled)
3499 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Cross connection %s",
3500 enabled ? "enabled" : "disabled");
3501 if (p2p->cross_connect == enabled)
3503 p2p->cross_connect = enabled;
3504 /* TODO: may need to tear down any action group where we are GO(?) */
3508 int p2p_get_oper_freq(struct p2p_data *p2p, const u8 *iface_addr)
3510 struct p2p_device *dev = p2p_get_device_interface(p2p, iface_addr);
3513 if (dev->oper_freq <= 0)
3515 return dev->oper_freq;
3519 void p2p_set_intra_bss_dist(struct p2p_data *p2p, int enabled)
3521 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Intra BSS distribution %s",
3522 enabled ? "enabled" : "disabled");
3523 p2p->cfg->p2p_intra_bss = enabled;
3527 void p2p_update_channel_list(struct p2p_data *p2p, struct p2p_channels *chan)
3529 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Update channel list");
3530 os_memcpy(&p2p->cfg->channels, chan, sizeof(struct p2p_channels));
3534 int p2p_send_action(struct p2p_data *p2p, unsigned int freq, const u8 *dst,
3535 const u8 *src, const u8 *bssid, const u8 *buf,
3536 size_t len, unsigned int wait_time)
3538 if (p2p->p2p_scan_running) {
3539 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Delay Action "
3540 "frame TX until p2p_scan completes");
3541 if (p2p->after_scan_tx) {
3542 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Dropped "
3543 "previous pending Action frame TX");
3544 os_free(p2p->after_scan_tx);
3546 p2p->after_scan_tx = os_malloc(sizeof(*p2p->after_scan_tx) +
3548 if (p2p->after_scan_tx == NULL)
3550 p2p->after_scan_tx->freq = freq;
3551 os_memcpy(p2p->after_scan_tx->dst, dst, ETH_ALEN);
3552 os_memcpy(p2p->after_scan_tx->src, src, ETH_ALEN);
3553 os_memcpy(p2p->after_scan_tx->bssid, bssid, ETH_ALEN);
3554 p2p->after_scan_tx->len = len;
3555 p2p->after_scan_tx->wait_time = wait_time;
3556 os_memcpy(p2p->after_scan_tx + 1, buf, len);
3560 return p2p->cfg->send_action(p2p->cfg->cb_ctx, freq, dst, src, bssid,
3561 buf, len, wait_time);
3565 void p2p_set_best_channels(struct p2p_data *p2p, int freq_24, int freq_5,
3568 wpa_msg(p2p->cfg->msg_ctx, MSG_DEBUG, "P2P: Best channel: 2.4 GHz: %d,"
3569 " 5 GHz: %d, overall: %d", freq_24, freq_5, freq_overall);
3570 p2p->best_freq_24 = freq_24;
3571 p2p->best_freq_5 = freq_5;
3572 p2p->best_freq_overall = freq_overall;
3576 const u8 * p2p_get_go_neg_peer(struct p2p_data *p2p)
3578 if (p2p == NULL || p2p->go_neg_peer == NULL)
3580 return p2p->go_neg_peer->info.p2p_device_addr;
3584 const struct p2p_peer_info *
3585 p2p_get_peer_found(struct p2p_data *p2p, const u8 *addr, int next)
3587 struct p2p_device *dev;
3590 dev = p2p_get_device(p2p, addr);
3595 if (dev->flags & P2P_DEV_PROBE_REQ_ONLY)
3601 dev = dl_list_first(&dev->list,
3604 if (&dev->list == &p2p->devices)
3606 } while (dev->flags & P2P_DEV_PROBE_REQ_ONLY);
3609 dev = dl_list_first(&p2p->devices, struct p2p_device, list);
3612 while (dev->flags & P2P_DEV_PROBE_REQ_ONLY) {
3613 dev = dl_list_first(&dev->list,
3616 if (&dev->list == &p2p->devices)