3 * Copyright (c) 2009-2010, Atheros Communications
4 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
14 #include "common/ieee802_11_common.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/wpa_ctrl.h"
17 #include "wps/wps_i.h"
19 #include "ap/hostapd.h"
20 #include "ap/ap_config.h"
21 #include "ap/sta_info.h"
22 #include "ap/ap_drv_ops.h"
23 #include "ap/wps_hostapd.h"
24 #include "ap/p2p_hostapd.h"
25 #include "eapol_supp/eapol_supp_sm.h"
26 #include "rsn_supp/wpa.h"
27 #include "wpa_supplicant_i.h"
30 #include "config_ssid.h"
35 #include "offchannel.h"
36 #include "wps_supplicant.h"
37 #include "p2p_supplicant.h"
38 #include "wifi_display.h"
42 * How many times to try to scan to find the GO before giving up on join
45 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49 #ifndef P2P_MAX_CLIENT_IDLE
51 * How many seconds to try to reconnect to the GO when connection in P2P client
54 #define P2P_MAX_CLIENT_IDLE 10
55 #endif /* P2P_MAX_CLIENT_IDLE */
57 #ifndef P2P_MAX_INITIAL_CONN_WAIT
59 * How many seconds to wait for initial 4-way handshake to get completed after
60 * WPS provisioning step or after the re-invocation of a persistent group on a
63 #define P2P_MAX_INITIAL_CONN_WAIT 10
64 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
66 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
68 * How many seconds to wait for initial 4-way handshake to get completed after
69 * WPS provisioning step on the GO. This controls the extra time the P2P
70 * operation is considered to be in progress (e.g., to delay other scans) after
71 * WPS provisioning has been completed on the GO during group formation.
73 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
74 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
76 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
78 * How many seconds to wait for initial 4-way handshake to get completed after
79 * re-invocation of a persistent group on the GO when the client is expected
80 * to connect automatically (no user interaction).
82 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
83 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
85 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
87 enum p2p_group_removal_reason {
88 P2P_GROUP_REMOVAL_UNKNOWN,
89 P2P_GROUP_REMOVAL_SILENT,
90 P2P_GROUP_REMOVAL_FORMATION_FAILED,
91 P2P_GROUP_REMOVAL_REQUESTED,
92 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
93 P2P_GROUP_REMOVAL_UNAVAILABLE,
94 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
95 P2P_GROUP_REMOVAL_PSK_FAILURE,
96 P2P_GROUP_REMOVAL_FREQ_CONFLICT
100 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
101 static struct wpa_supplicant *
102 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
104 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
105 const u8 *ssid, size_t ssid_len);
106 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
107 const u8 *ssid, size_t ssid_len);
108 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
109 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
110 const u8 *dev_addr, enum p2p_wps_method wps_method,
111 int auto_join, int freq,
112 const u8 *ssid, size_t ssid_len);
113 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
114 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
115 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
116 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
117 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
119 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
120 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
122 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
123 static void wpas_stop_listen(void *ctx);
124 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
128 * Get the number of concurrent channels that the HW can operate, but that are
129 * currently not in use by any of the wpa_supplicant interfaces.
131 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
136 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
140 num = get_shared_radio_freqs(wpa_s, freqs,
141 wpa_s->num_multichan_concurrent);
144 unused = wpa_s->num_multichan_concurrent - num;
145 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
151 * Get the frequencies that are currently in use by one or more of the virtual
152 * interfaces, and that are also valid for P2P operation.
155 wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
156 struct wpa_used_freq_data *p2p_freqs,
159 struct wpa_used_freq_data *freqs;
160 unsigned int num, i, j;
162 freqs = os_calloc(wpa_s->num_multichan_concurrent,
163 sizeof(struct wpa_used_freq_data));
167 num = get_shared_radio_freqs_data(wpa_s, freqs,
168 wpa_s->num_multichan_concurrent);
170 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
172 for (i = 0, j = 0; i < num && j < len; i++) {
173 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
174 p2p_freqs[j++] = freqs[i];
179 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
185 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
188 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
190 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
191 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
192 wpas_p2p_num_unused_channels(wpa_s) > 0) {
193 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
197 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
201 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
202 struct wpa_scan_results *scan_res)
206 if (wpa_s->p2p_scan_work) {
207 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
208 wpa_s->p2p_scan_work = NULL;
209 radio_work_done(work);
212 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
215 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
216 (int) scan_res->num);
218 for (i = 0; i < scan_res->num; i++) {
219 struct wpa_scan_res *bss = scan_res->res[i];
220 struct os_reltime time_tmp_age, entry_ts;
224 time_tmp_age.sec = bss->age / 1000;
225 time_tmp_age.usec = (bss->age % 1000) * 1000;
226 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
228 ies = (const u8 *) (bss + 1);
229 ies_len = bss->ie_len;
230 if (bss->beacon_ie_len > 0 &&
231 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
232 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
233 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
234 MACSTR, MAC2STR(bss->bssid));
236 ies_len = bss->beacon_ie_len;
240 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
241 bss->freq, &entry_ts, bss->level,
246 p2p_scan_res_handled(wpa_s->global->p2p);
250 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
252 struct wpa_supplicant *wpa_s = work->wpa_s;
253 struct wpa_driver_scan_params *params = work->ctx;
257 if (!work->started) {
258 wpa_scan_free_params(params);
262 wpa_s->p2p_scan_work = NULL;
266 ret = wpa_drv_scan(wpa_s, params);
267 wpa_scan_free_params(params);
270 radio_work_done(work);
274 os_get_reltime(&wpa_s->scan_trigger_time);
275 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
276 wpa_s->own_scan_requested = 1;
277 wpa_s->p2p_scan_work = work;
281 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
282 unsigned int num_req_dev_types,
283 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
285 struct wpa_supplicant *wpa_s = ctx;
286 struct wpa_driver_scan_params *params = NULL;
287 struct wpabuf *wps_ie, *ies;
291 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
294 if (wpa_s->p2p_scan_work) {
295 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
299 params = os_zalloc(sizeof(*params));
303 /* P2P Wildcard SSID */
304 params->num_ssids = 1;
305 n = os_malloc(P2P_WILDCARD_SSID_LEN);
308 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
309 params->ssids[0].ssid = n;
310 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
312 wpa_s->wps->dev.p2p = 1;
313 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
314 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
315 num_req_dev_types, req_dev_types);
319 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
320 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
325 wpabuf_put_buf(ies, wps_ie);
328 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
330 params->p2p_probe = 1;
331 n = os_malloc(wpabuf_len(ies));
336 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
337 params->extra_ies = n;
338 params->extra_ies_len = wpabuf_len(ies);
342 case P2P_SCAN_SOCIAL:
343 params->freqs = os_malloc(4 * sizeof(int));
344 if (params->freqs == NULL)
346 params->freqs[0] = 2412;
347 params->freqs[1] = 2437;
348 params->freqs[2] = 2462;
349 params->freqs[3] = 0;
353 case P2P_SCAN_SOCIAL_PLUS_ONE:
354 params->freqs = os_malloc(5 * sizeof(int));
355 if (params->freqs == NULL)
357 params->freqs[0] = 2412;
358 params->freqs[1] = 2437;
359 params->freqs[2] = 2462;
360 params->freqs[3] = freq;
361 params->freqs[4] = 0;
365 radio_remove_works(wpa_s, "p2p-scan", 0);
366 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
372 wpa_scan_free_params(params);
377 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
379 switch (p2p_group_interface) {
380 case P2P_GROUP_INTERFACE_PENDING:
381 return WPA_IF_P2P_GROUP;
382 case P2P_GROUP_INTERFACE_GO:
383 return WPA_IF_P2P_GO;
384 case P2P_GROUP_INTERFACE_CLIENT:
385 return WPA_IF_P2P_CLIENT;
388 return WPA_IF_P2P_GROUP;
392 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
394 size_t ssid_len, int *go)
398 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
399 for (s = wpa_s->conf->ssid; s; s = s->next) {
400 if (s->disabled != 0 || !s->p2p_group ||
401 s->ssid_len != ssid_len ||
402 os_memcmp(ssid, s->ssid, ssid_len) != 0)
404 if (s->mode == WPAS_MODE_P2P_GO &&
405 s != wpa_s->current_ssid)
408 *go = s->mode == WPAS_MODE_P2P_GO;
417 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
418 enum p2p_group_removal_reason removal_reason)
420 struct wpa_ssid *ssid;
424 ssid = wpa_s->current_ssid;
427 * The current SSID was not known, but there may still be a
428 * pending P2P group interface waiting for provisioning or a
429 * P2P group that is trying to reconnect.
431 ssid = wpa_s->conf->ssid;
433 if (ssid->p2p_group && ssid->disabled != 2)
438 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
440 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
445 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
447 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
448 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
449 wpa_s->reassociate = 0;
450 wpa_s->disconnected = 1;
451 wpa_supplicant_deauthenticate(wpa_s,
452 WLAN_REASON_DEAUTH_LEAVING);
456 if (wpa_s->cross_connect_in_use) {
457 wpa_s->cross_connect_in_use = 0;
458 wpa_msg_global(wpa_s->parent, MSG_INFO,
459 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
460 wpa_s->ifname, wpa_s->cross_connect_uplink);
462 switch (removal_reason) {
463 case P2P_GROUP_REMOVAL_REQUESTED:
464 reason = " reason=REQUESTED";
466 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
467 reason = " reason=FORMATION_FAILED";
469 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
470 reason = " reason=IDLE";
472 case P2P_GROUP_REMOVAL_UNAVAILABLE:
473 reason = " reason=UNAVAILABLE";
475 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
476 reason = " reason=GO_ENDING_SESSION";
478 case P2P_GROUP_REMOVAL_PSK_FAILURE:
479 reason = " reason=PSK_FAILURE";
481 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
482 reason = " reason=FREQ_CONFLICT";
488 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
489 wpa_msg_global(wpa_s->parent, MSG_INFO,
490 P2P_EVENT_GROUP_REMOVED "%s %s%s",
491 wpa_s->ifname, gtype, reason);
494 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
495 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
496 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
497 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
498 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
499 wpa_s->parent, NULL) > 0) {
500 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
502 wpa_s->p2p_in_provisioning = 0;
505 wpa_s->p2p_in_invitation = 0;
508 * Make sure wait for the first client does not remain active after the
509 * group has been removed.
511 wpa_s->global->p2p_go_wait_client.sec = 0;
513 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
514 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
516 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
517 struct wpa_global *global;
519 enum wpa_driver_if_type type;
520 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
522 global = wpa_s->global;
523 ifname = os_strdup(wpa_s->ifname);
524 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
525 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
526 wpa_s = global->ifaces;
528 wpa_drv_if_remove(wpa_s, type, ifname);
533 if (!wpa_s->p2p_go_group_formation_completed) {
534 wpa_s->global->p2p_group_formation = NULL;
535 wpa_s->p2p_in_provisioning = 0;
538 wpa_s->show_group_started = 0;
539 os_free(wpa_s->go_params);
540 wpa_s->go_params = NULL;
542 wpa_s->waiting_presence_resp = 0;
544 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
545 if (ssid && (ssid->p2p_group ||
546 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
547 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
549 if (ssid == wpa_s->current_ssid) {
550 wpa_sm_set_config(wpa_s->wpa, NULL);
551 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
552 wpa_s->current_ssid = NULL;
555 * Networks objects created during any P2P activities are not
556 * exposed out as they might/will confuse certain non-P2P aware
557 * applications since these network objects won't behave like
560 * Likewise, we don't send out network removed signals for such
563 wpa_config_remove_network(wpa_s->conf, id);
564 wpa_supplicant_clear_status(wpa_s);
565 wpa_supplicant_cancel_sched_scan(wpa_s);
567 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
571 wpa_supplicant_ap_deinit(wpa_s);
573 wpa_drv_deinit_p2p_cli(wpa_s);
579 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
581 const u8 *ssid, size_t ssid_len)
589 if (wpa_s->go_params)
590 bssid = wpa_s->go_params->peer_interface_addr;
592 bssid = wpa_s->bssid;
594 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
595 if (bss == NULL && wpa_s->go_params &&
596 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
597 bss = wpa_bss_get_p2p_dev_addr(
598 wpa_s, wpa_s->go_params->peer_device_addr);
600 u8 iface_addr[ETH_ALEN];
601 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
603 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
606 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
607 "group is persistent - BSS " MACSTR " not found",
612 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
614 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
617 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
618 "group is persistent - BSS " MACSTR
619 " did not include P2P IE", MAC2STR(bssid));
620 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
621 (u8 *) (bss + 1), bss->ie_len);
622 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
623 ((u8 *) bss + 1) + bss->ie_len,
628 group_capab = p2p_get_group_capab(p2p);
629 addr = p2p_get_go_dev_addr(p2p);
630 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
631 "group_capab=0x%x", group_capab);
633 os_memcpy(go_dev_addr, addr, ETH_ALEN);
634 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
637 os_memset(go_dev_addr, 0, ETH_ALEN);
640 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
641 "go_dev_addr=" MACSTR,
642 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
644 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
648 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
649 struct wpa_ssid *ssid,
650 const u8 *go_dev_addr)
655 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
656 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
657 for (s = wpa_s->conf->ssid; s; s = s->next) {
658 if (s->disabled == 2 &&
659 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
660 s->ssid_len == ssid->ssid_len &&
661 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
666 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
668 if (ssid->passphrase && !s->passphrase)
670 else if (ssid->passphrase && s->passphrase &&
671 os_strcmp(ssid->passphrase, s->passphrase) != 0)
674 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
677 s = wpa_config_add_network(wpa_s->conf);
682 * Instead of network_added we emit persistent_group_added
683 * notification. Also to keep the defense checks in
684 * persistent_group obj registration method, we set the
685 * relevant flags in s to designate it as a persistent group.
688 s->p2p_persistent_group = 1;
689 wpas_notify_persistent_group_added(wpa_s, s);
690 wpa_config_set_network_defaults(s);
694 s->p2p_persistent_group = 1;
697 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
698 s->mode = ssid->mode;
699 s->auth_alg = WPA_AUTH_ALG_OPEN;
700 s->key_mgmt = WPA_KEY_MGMT_PSK;
701 s->proto = WPA_PROTO_RSN;
702 s->pairwise_cipher = WPA_CIPHER_CCMP;
704 if (ssid->passphrase) {
705 os_free(s->passphrase);
706 s->passphrase = os_strdup(ssid->passphrase);
710 os_memcpy(s->psk, ssid->psk, 32);
712 if (s->passphrase && !s->psk_set)
713 wpa_config_update_psk(s);
714 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
716 s->ssid = os_malloc(ssid->ssid_len);
719 s->ssid_len = ssid->ssid_len;
720 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
722 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
723 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
724 wpa_s->global->add_psk = NULL;
728 if (changed && wpa_s->conf->update_config &&
729 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
730 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
737 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
740 struct wpa_ssid *ssid, *s;
745 ssid = wpa_s->current_ssid;
746 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
747 !ssid->p2p_persistent_group)
750 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
751 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
754 if (s->ssid_len == ssid->ssid_len &&
755 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
762 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
763 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
767 if (i == s->num_p2p_clients - 1)
768 return; /* already the most recent entry */
770 /* move the entry to mark it most recent */
771 os_memmove(s->p2p_client_list + i * ETH_ALEN,
772 s->p2p_client_list + (i + 1) * ETH_ALEN,
773 (s->num_p2p_clients - i - 1) * ETH_ALEN);
774 os_memcpy(s->p2p_client_list +
775 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
780 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
781 n = os_realloc_array(s->p2p_client_list,
782 s->num_p2p_clients + 1, ETH_ALEN);
785 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
786 s->p2p_client_list = n;
787 s->num_p2p_clients++;
789 /* Not enough room for an additional entry - drop the oldest
791 os_memmove(s->p2p_client_list,
792 s->p2p_client_list + ETH_ALEN,
793 (s->num_p2p_clients - 1) * ETH_ALEN);
794 os_memcpy(s->p2p_client_list +
795 (s->num_p2p_clients - 1) * ETH_ALEN,
799 if (wpa_s->parent->conf->update_config &&
800 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
801 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
805 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
808 struct wpa_ssid *ssid;
809 const char *ssid_txt;
812 u8 go_dev_addr[ETH_ALEN];
816 * This callback is likely called for the main interface. Update wpa_s
817 * to use the group interface if a new interface was created for the
820 if (wpa_s->global->p2p_group_formation)
821 wpa_s = wpa_s->global->p2p_group_formation;
822 if (wpa_s->p2p_go_group_formation_completed) {
823 wpa_s->global->p2p_group_formation = NULL;
824 wpa_s->p2p_in_provisioning = 0;
826 wpa_s->p2p_in_invitation = 0;
829 wpa_msg_global(wpa_s->parent, MSG_INFO,
830 P2P_EVENT_GROUP_FORMATION_FAILURE);
831 wpas_p2p_group_delete(wpa_s,
832 P2P_GROUP_REMOVAL_FORMATION_FAILED);
836 wpa_msg_global(wpa_s->parent, MSG_INFO,
837 P2P_EVENT_GROUP_FORMATION_SUCCESS);
839 ssid = wpa_s->current_ssid;
840 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
841 ssid->mode = WPAS_MODE_P2P_GO;
842 p2p_group_notif_formation_done(wpa_s->p2p_group);
843 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
848 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
849 client = ssid->mode == WPAS_MODE_INFRA;
850 if (ssid->mode == WPAS_MODE_P2P_GO) {
851 persistent = ssid->p2p_persistent_group;
852 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
855 persistent = wpas_p2p_persistent_group(wpa_s,
861 client = wpa_s->p2p_group_interface ==
862 P2P_GROUP_INTERFACE_CLIENT;
863 os_memset(go_dev_addr, 0, ETH_ALEN);
866 wpa_s->show_group_started = 0;
869 * Indicate event only after successfully completed 4-way
870 * handshake, i.e., when the interface is ready for data
873 wpa_s->show_group_started = 1;
874 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
876 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
877 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
878 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
880 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
881 MAC2STR(go_dev_addr),
882 persistent ? " [PERSISTENT]" : "");
883 wpas_p2p_cross_connect_setup(wpa_s);
884 wpas_p2p_set_group_idle_timeout(wpa_s);
886 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
887 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
888 "go_dev_addr=" MACSTR "%s",
889 wpa_s->ifname, ssid_txt,
890 ssid ? ssid->frequency : 0,
891 ssid && ssid->passphrase ? ssid->passphrase : "",
892 MAC2STR(go_dev_addr),
893 persistent ? " [PERSISTENT]" : "");
894 wpas_p2p_cross_connect_setup(wpa_s);
895 wpas_p2p_set_group_idle_timeout(wpa_s);
899 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
902 os_free(wpa_s->global->add_psk);
903 wpa_s->global->add_psk = NULL;
905 if (network_id < 0 && ssid)
906 network_id = ssid->id;
908 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
909 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
914 struct send_action_work {
920 unsigned int wait_time;
925 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
928 struct wpa_supplicant *wpa_s = eloop_ctx;
930 if (!wpa_s->p2p_send_action_work)
933 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
934 os_free(wpa_s->p2p_send_action_work->ctx);
935 radio_work_done(wpa_s->p2p_send_action_work);
936 wpa_s->p2p_send_action_work = NULL;
940 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
942 const u8 *dst, const u8 *src,
944 const u8 *data, size_t data_len,
945 enum offchannel_send_action_result
948 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
950 if (wpa_s->p2p_send_action_work) {
951 struct send_action_work *awork;
952 awork = wpa_s->p2p_send_action_work->ctx;
953 if (awork->wait_time == 0) {
955 radio_work_done(wpa_s->p2p_send_action_work);
956 wpa_s->p2p_send_action_work = NULL;
959 * In theory, this should not be needed, but number of
960 * places in the P2P code is still using non-zero wait
961 * time for the last Action frame in the sequence and
962 * some of these do not call send_action_done().
964 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
966 eloop_register_timeout(
967 0, awork->wait_time * 1000,
968 wpas_p2p_send_action_work_timeout,
973 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
977 case OFFCHANNEL_SEND_ACTION_SUCCESS:
978 res = P2P_SEND_ACTION_SUCCESS;
980 case OFFCHANNEL_SEND_ACTION_NO_ACK:
981 res = P2P_SEND_ACTION_NO_ACK;
983 case OFFCHANNEL_SEND_ACTION_FAILED:
984 res = P2P_SEND_ACTION_FAILED;
988 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
990 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
991 wpa_s->pending_pd_before_join &&
992 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
993 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
994 wpa_s->p2p_fallback_to_go_neg) {
995 wpa_s->pending_pd_before_join = 0;
996 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
997 "during p2p_connect-auto");
998 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1004 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1006 struct wpa_supplicant *wpa_s = work->wpa_s;
1007 struct send_action_work *awork = work->ctx;
1010 if (work->started) {
1011 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1013 wpa_s->p2p_send_action_work = NULL;
1014 offchannel_send_action_done(wpa_s);
1020 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1021 awork->bssid, awork->buf, awork->len,
1023 wpas_p2p_send_action_tx_status, 1) < 0) {
1025 radio_work_done(work);
1028 wpa_s->p2p_send_action_work = work;
1032 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1033 unsigned int freq, const u8 *dst,
1034 const u8 *src, const u8 *bssid, const u8 *buf,
1035 size_t len, unsigned int wait_time)
1037 struct send_action_work *awork;
1039 if (wpa_s->p2p_send_action_work) {
1040 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1044 awork = os_zalloc(sizeof(*awork) + len);
1049 os_memcpy(awork->dst, dst, ETH_ALEN);
1050 os_memcpy(awork->src, src, ETH_ALEN);
1051 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1053 awork->wait_time = wait_time;
1054 os_memcpy(awork->buf, buf, len);
1056 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1057 wpas_send_action_cb, awork) < 0) {
1066 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1067 const u8 *src, const u8 *bssid, const u8 *buf,
1068 size_t len, unsigned int wait_time)
1070 struct wpa_supplicant *wpa_s = ctx;
1071 int listen_freq = -1, send_freq = -1;
1073 if (wpa_s->p2p_listen_work)
1074 listen_freq = wpa_s->p2p_listen_work->freq;
1075 if (wpa_s->p2p_send_action_work)
1076 send_freq = wpa_s->p2p_send_action_work->freq;
1077 if (listen_freq != (int) freq && send_freq != (int) freq) {
1078 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1079 listen_freq, send_freq);
1080 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1084 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1085 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1087 wpas_p2p_send_action_tx_status, 1);
1091 static void wpas_send_action_done(void *ctx)
1093 struct wpa_supplicant *wpa_s = ctx;
1095 if (wpa_s->p2p_send_action_work) {
1096 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1098 os_free(wpa_s->p2p_send_action_work->ctx);
1099 radio_work_done(wpa_s->p2p_send_action_work);
1100 wpa_s->p2p_send_action_work = NULL;
1103 offchannel_send_action_done(wpa_s);
1107 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1108 struct p2p_go_neg_results *params)
1110 if (wpa_s->go_params == NULL) {
1111 wpa_s->go_params = os_malloc(sizeof(*params));
1112 if (wpa_s->go_params == NULL)
1115 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1120 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1121 struct p2p_go_neg_results *res)
1123 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1124 " dev_addr " MACSTR " wps_method %d",
1125 MAC2STR(res->peer_interface_addr),
1126 MAC2STR(res->peer_device_addr), res->wps_method);
1127 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1128 res->ssid, res->ssid_len);
1129 wpa_supplicant_ap_deinit(wpa_s);
1130 wpas_copy_go_neg_results(wpa_s, res);
1131 if (res->wps_method == WPS_PBC) {
1132 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1133 #ifdef CONFIG_WPS_NFC
1134 } else if (res->wps_method == WPS_NFC) {
1135 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1136 res->peer_interface_addr,
1137 wpa_s->parent->p2p_oob_dev_pw,
1138 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1139 wpa_s->parent->p2p_oob_dev_pw_id ==
1140 DEV_PW_NFC_CONNECTION_HANDOVER ?
1141 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1144 #endif /* CONFIG_WPS_NFC */
1146 u16 dev_pw_id = DEV_PW_DEFAULT;
1147 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1148 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1149 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1150 wpa_s->p2p_pin, 1, dev_pw_id);
1155 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1156 struct wpa_ssid *ssid)
1158 struct wpa_ssid *persistent;
1159 struct psk_list_entry *psk;
1160 struct hostapd_data *hapd;
1162 if (!wpa_s->ap_iface)
1165 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1167 if (persistent == NULL)
1170 hapd = wpa_s->ap_iface->bss[0];
1172 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1174 struct hostapd_wpa_psk *hpsk;
1176 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1178 MAC2STR(psk->addr), psk->p2p);
1179 hpsk = os_zalloc(sizeof(*hpsk));
1182 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1184 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1186 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1187 hpsk->next = hapd->conf->ssid.wpa_psk;
1188 hapd->conf->ssid.wpa_psk = hpsk;
1193 static void p2p_go_configured(void *ctx, void *data)
1195 struct wpa_supplicant *wpa_s = ctx;
1196 struct p2p_go_neg_results *params = data;
1197 struct wpa_ssid *ssid;
1198 int network_id = -1;
1200 ssid = wpa_s->current_ssid;
1201 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1202 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1203 if (wpa_s->global->p2p_group_formation == wpa_s)
1204 wpa_s->global->p2p_group_formation = NULL;
1205 if (os_strlen(params->passphrase) > 0) {
1206 wpa_msg_global(wpa_s->parent, MSG_INFO,
1207 P2P_EVENT_GROUP_STARTED
1208 "%s GO ssid=\"%s\" freq=%d "
1209 "passphrase=\"%s\" go_dev_addr=" MACSTR
1210 "%s", wpa_s->ifname,
1211 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1212 ssid->frequency, params->passphrase,
1213 MAC2STR(wpa_s->global->p2p_dev_addr),
1214 params->persistent_group ?
1215 " [PERSISTENT]" : "");
1218 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1219 sizeof(params->psk));
1220 wpa_msg_global(wpa_s->parent, MSG_INFO,
1221 P2P_EVENT_GROUP_STARTED
1222 "%s GO ssid=\"%s\" freq=%d psk=%s "
1223 "go_dev_addr=" MACSTR "%s",
1225 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1226 ssid->frequency, psk,
1227 MAC2STR(wpa_s->global->p2p_dev_addr),
1228 params->persistent_group ?
1229 " [PERSISTENT]" : "");
1232 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1233 if (params->persistent_group) {
1234 network_id = wpas_p2p_store_persistent_group(
1235 wpa_s->parent, ssid,
1236 wpa_s->global->p2p_dev_addr);
1237 wpas_p2p_add_psk_list(wpa_s, ssid);
1240 network_id = ssid->id;
1241 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1242 wpas_p2p_cross_connect_setup(wpa_s);
1243 wpas_p2p_set_group_idle_timeout(wpa_s);
1245 if (wpa_s->p2p_first_connection_timeout) {
1246 wpa_dbg(wpa_s, MSG_DEBUG,
1247 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1248 wpa_s->p2p_first_connection_timeout);
1249 wpa_s->p2p_go_group_formation_completed = 0;
1250 wpa_s->global->p2p_group_formation = wpa_s;
1251 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1252 wpa_s->parent, NULL);
1253 eloop_register_timeout(
1254 wpa_s->p2p_first_connection_timeout, 0,
1255 wpas_p2p_group_formation_timeout,
1256 wpa_s->parent, NULL);
1262 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1263 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1264 params->peer_interface_addr)) {
1265 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1269 if (params->wps_method == WPS_PBC) {
1270 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1271 params->peer_device_addr);
1272 #ifdef CONFIG_WPS_NFC
1273 } else if (params->wps_method == WPS_NFC) {
1274 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1275 DEV_PW_NFC_CONNECTION_HANDOVER &&
1276 !wpa_s->parent->p2p_oob_dev_pw) {
1277 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1280 wpas_ap_wps_add_nfc_pw(
1281 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1282 wpa_s->parent->p2p_oob_dev_pw,
1283 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1284 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1285 #endif /* CONFIG_WPS_NFC */
1286 } else if (wpa_s->p2p_pin[0])
1287 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1288 wpa_s->p2p_pin, NULL, 0, 0);
1289 os_free(wpa_s->go_params);
1290 wpa_s->go_params = NULL;
1294 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1295 struct p2p_go_neg_results *params,
1296 int group_formation)
1298 struct wpa_ssid *ssid;
1300 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1301 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1302 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1307 ssid = wpa_config_add_network(wpa_s->conf);
1309 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1313 wpa_s->show_group_started = 0;
1315 wpa_config_set_network_defaults(ssid);
1316 ssid->temporary = 1;
1317 ssid->p2p_group = 1;
1318 ssid->p2p_persistent_group = params->persistent_group;
1319 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1321 ssid->frequency = params->freq;
1322 ssid->ht40 = params->ht40;
1323 ssid->vht = params->vht;
1324 ssid->ssid = os_zalloc(params->ssid_len + 1);
1326 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1327 ssid->ssid_len = params->ssid_len;
1329 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1330 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1331 ssid->proto = WPA_PROTO_RSN;
1332 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1333 if (os_strlen(params->passphrase) > 0) {
1334 ssid->passphrase = os_strdup(params->passphrase);
1335 if (ssid->passphrase == NULL) {
1336 wpa_msg_global(wpa_s, MSG_ERROR,
1337 "P2P: Failed to copy passphrase for GO");
1338 wpa_config_remove_network(wpa_s->conf, ssid->id);
1342 ssid->passphrase = NULL;
1343 ssid->psk_set = params->psk_set;
1345 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1346 else if (ssid->passphrase)
1347 wpa_config_update_psk(ssid);
1348 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1350 wpa_s->ap_configured_cb = p2p_go_configured;
1351 wpa_s->ap_configured_cb_ctx = wpa_s;
1352 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1353 wpa_s->scan_req = NORMAL_SCAN_REQ;
1354 wpa_s->connect_without_scan = ssid;
1355 wpa_s->reassociate = 1;
1356 wpa_s->disconnected = 0;
1357 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1359 wpa_supplicant_req_scan(wpa_s, 0, 0);
1363 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1364 const struct wpa_supplicant *src)
1366 struct wpa_config *d;
1367 const struct wpa_config *s;
1372 #define C(n) if (s->n) d->n = os_strdup(s->n)
1381 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1382 os_memcpy(d->sec_device_type, s->sec_device_type,
1383 sizeof(d->sec_device_type));
1384 d->num_sec_device_types = s->num_sec_device_types;
1386 d->p2p_group_idle = s->p2p_group_idle;
1387 d->p2p_intra_bss = s->p2p_intra_bss;
1388 d->persistent_reconnect = s->persistent_reconnect;
1389 d->max_num_sta = s->max_num_sta;
1390 d->pbc_in_m1 = s->pbc_in_m1;
1391 d->ignore_old_scan_res = s->ignore_old_scan_res;
1392 d->beacon_int = s->beacon_int;
1393 d->dtim_period = s->dtim_period;
1394 d->disassoc_low_ack = s->disassoc_low_ack;
1395 d->disable_scan_offload = s->disable_scan_offload;
1397 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1398 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1399 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1404 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1405 char *ifname, size_t len)
1407 char *ifname_ptr = wpa_s->ifname;
1409 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1410 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1411 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1414 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1415 if (os_strlen(ifname) >= IFNAMSIZ &&
1416 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1417 /* Try to avoid going over the IFNAMSIZ length limit */
1418 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1423 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1424 enum wpa_driver_if_type type)
1426 char ifname[120], force_ifname[120];
1428 if (wpa_s->pending_interface_name[0]) {
1429 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1430 "- skip creation of a new one");
1431 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1432 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1433 "unknown?! ifname='%s'",
1434 wpa_s->pending_interface_name);
1440 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1441 force_ifname[0] = '\0';
1443 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1445 wpa_s->p2p_group_idx++;
1447 wpa_s->pending_interface_type = type;
1448 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1449 wpa_s->pending_interface_addr, NULL) < 0) {
1450 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1455 if (force_ifname[0]) {
1456 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1458 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1459 sizeof(wpa_s->pending_interface_name));
1461 os_strlcpy(wpa_s->pending_interface_name, ifname,
1462 sizeof(wpa_s->pending_interface_name));
1463 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1464 MACSTR, wpa_s->pending_interface_name,
1465 MAC2STR(wpa_s->pending_interface_addr));
1471 static void wpas_p2p_remove_pending_group_interface(
1472 struct wpa_supplicant *wpa_s)
1474 if (!wpa_s->pending_interface_name[0] ||
1475 is_zero_ether_addr(wpa_s->pending_interface_addr))
1476 return; /* No pending virtual interface */
1478 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1479 wpa_s->pending_interface_name);
1480 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1481 wpa_s->pending_interface_name);
1482 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1483 wpa_s->pending_interface_name[0] = '\0';
1487 static struct wpa_supplicant *
1488 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1490 struct wpa_interface iface;
1491 struct wpa_supplicant *group_wpa_s;
1493 if (!wpa_s->pending_interface_name[0]) {
1494 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1495 if (!wpas_p2p_create_iface(wpa_s))
1498 * Something has forced us to remove the pending interface; try
1499 * to create a new one and hope for the best that we will get
1500 * the same local address.
1502 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1503 WPA_IF_P2P_CLIENT) < 0)
1507 os_memset(&iface, 0, sizeof(iface));
1508 iface.ifname = wpa_s->pending_interface_name;
1509 iface.driver = wpa_s->driver->name;
1510 if (wpa_s->conf->ctrl_interface == NULL &&
1511 wpa_s->parent != wpa_s &&
1513 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1514 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1516 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1517 iface.driver_param = wpa_s->conf->driver_param;
1518 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1519 if (group_wpa_s == NULL) {
1520 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1521 "wpa_supplicant interface");
1524 wpa_s->pending_interface_name[0] = '\0';
1525 group_wpa_s->parent = wpa_s;
1526 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1527 P2P_GROUP_INTERFACE_CLIENT;
1528 wpa_s->global->p2p_group_formation = group_wpa_s;
1530 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1536 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1539 struct wpa_supplicant *wpa_s = eloop_ctx;
1540 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1541 wpas_p2p_group_formation_failed(wpa_s);
1545 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1547 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1548 wpa_s->parent, NULL);
1549 if (wpa_s->global->p2p)
1550 p2p_group_formation_failed(wpa_s->global->p2p);
1551 wpas_group_formation_completed(wpa_s, 0);
1555 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1557 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1558 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1559 wpa_s->parent, NULL);
1560 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1561 wpa_s->parent, NULL);
1562 wpa_s->global->p2p_fail_on_wps_complete = 0;
1566 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1568 if (wpa_s->global->p2p_group_formation != wpa_s)
1570 /* Speed up group formation timeout since this cannot succeed */
1571 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1572 wpa_s->parent, NULL);
1573 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1574 wpa_s->parent, NULL);
1578 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1580 struct wpa_supplicant *wpa_s = ctx;
1582 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1583 wpa_drv_cancel_remain_on_channel(wpa_s);
1584 wpa_s->off_channel_freq = 0;
1585 wpa_s->roc_waiting_drv_freq = 0;
1589 wpa_msg_global(wpa_s, MSG_INFO,
1590 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1592 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1593 wpas_p2p_remove_pending_group_interface(wpa_s);
1597 if (wpa_s->p2p_go_ht40)
1599 if (wpa_s->p2p_go_vht)
1602 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1603 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1605 res->role_go ? "GO" : "client", res->freq, res->ht40,
1606 MAC2STR(res->peer_device_addr),
1607 MAC2STR(res->peer_interface_addr),
1608 p2p_wps_method_text(res->wps_method));
1609 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1611 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1612 struct wpa_ssid *ssid;
1613 ssid = wpa_config_get_network(wpa_s->conf,
1614 wpa_s->p2p_persistent_id);
1615 if (ssid && ssid->disabled == 2 &&
1616 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1617 size_t len = os_strlen(ssid->passphrase);
1618 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1619 "on requested persistent group");
1620 os_memcpy(res->passphrase, ssid->passphrase, len);
1621 res->passphrase[len] = '\0';
1625 if (wpa_s->create_p2p_iface) {
1626 struct wpa_supplicant *group_wpa_s =
1627 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1628 if (group_wpa_s == NULL) {
1629 wpas_p2p_remove_pending_group_interface(wpa_s);
1630 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
1632 wpas_p2p_group_formation_failed(wpa_s);
1635 if (group_wpa_s != wpa_s) {
1636 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1637 sizeof(group_wpa_s->p2p_pin));
1638 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1640 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1641 wpa_s->pending_interface_name[0] = '\0';
1642 group_wpa_s->p2p_in_provisioning = 1;
1645 wpas_start_wps_go(group_wpa_s, res, 1);
1647 wpas_start_wps_enrollee(group_wpa_s, res);
1649 wpa_s->p2p_in_provisioning = 1;
1650 wpa_s->global->p2p_group_formation = wpa_s;
1653 wpas_start_wps_go(wpa_s, res, 1);
1655 wpas_start_wps_enrollee(ctx, res);
1658 wpa_s->p2p_long_listen = 0;
1659 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1661 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1662 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1663 (res->peer_config_timeout % 100) * 10000,
1664 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1668 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1670 struct wpa_supplicant *wpa_s = ctx;
1671 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1672 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1674 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1678 static void wpas_dev_found(void *ctx, const u8 *addr,
1679 const struct p2p_peer_info *info,
1682 #ifndef CONFIG_NO_STDOUT_DEBUG
1683 struct wpa_supplicant *wpa_s = ctx;
1684 char devtype[WPS_DEV_TYPE_BUFSIZE];
1685 char *wfd_dev_info_hex = NULL;
1687 #ifdef CONFIG_WIFI_DISPLAY
1688 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1689 WFD_SUBELEM_DEVICE_INFO);
1690 #endif /* CONFIG_WIFI_DISPLAY */
1692 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1693 " p2p_dev_addr=" MACSTR
1694 " pri_dev_type=%s name='%s' config_methods=0x%x "
1695 "dev_capab=0x%x group_capab=0x%x%s%s",
1696 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1697 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1699 info->device_name, info->config_methods,
1700 info->dev_capab, info->group_capab,
1701 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1702 wfd_dev_info_hex ? wfd_dev_info_hex : "");
1704 os_free(wfd_dev_info_hex);
1705 #endif /* CONFIG_NO_STDOUT_DEBUG */
1707 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1711 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1713 struct wpa_supplicant *wpa_s = ctx;
1715 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1716 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1718 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1722 static void wpas_find_stopped(void *ctx)
1724 struct wpa_supplicant *wpa_s = ctx;
1725 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1729 struct wpas_p2p_listen_work {
1731 unsigned int duration;
1732 struct wpabuf *probe_resp_ie;
1736 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1740 wpabuf_free(lwork->probe_resp_ie);
1745 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1747 struct wpas_p2p_listen_work *lwork;
1749 if (!wpa_s->p2p_listen_work)
1752 lwork = wpa_s->p2p_listen_work->ctx;
1753 wpas_p2p_listen_work_free(lwork);
1754 radio_work_done(wpa_s->p2p_listen_work);
1755 wpa_s->p2p_listen_work = NULL;
1759 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1761 struct wpa_supplicant *wpa_s = work->wpa_s;
1762 struct wpas_p2p_listen_work *lwork = work->ctx;
1765 if (work->started) {
1766 wpa_s->p2p_listen_work = NULL;
1767 wpas_stop_listen(wpa_s);
1769 wpas_p2p_listen_work_free(lwork);
1773 wpa_s->p2p_listen_work = work;
1775 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1777 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1778 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1779 "report received Probe Request frames");
1780 wpas_p2p_listen_work_done(wpa_s);
1784 wpa_s->pending_listen_freq = lwork->freq;
1785 wpa_s->pending_listen_duration = lwork->duration;
1787 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1789 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1790 "to remain on channel (%u MHz) for Listen "
1791 "state", lwork->freq);
1792 wpas_p2p_listen_work_done(wpa_s);
1793 wpa_s->pending_listen_freq = 0;
1796 wpa_s->off_channel_freq = 0;
1797 wpa_s->roc_waiting_drv_freq = lwork->freq;
1801 static int wpas_start_listen(void *ctx, unsigned int freq,
1802 unsigned int duration,
1803 const struct wpabuf *probe_resp_ie)
1805 struct wpa_supplicant *wpa_s = ctx;
1806 struct wpas_p2p_listen_work *lwork;
1808 if (wpa_s->p2p_listen_work) {
1809 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
1813 lwork = os_zalloc(sizeof(*lwork));
1817 lwork->duration = duration;
1818 if (probe_resp_ie) {
1819 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1820 if (lwork->probe_resp_ie == NULL) {
1821 wpas_p2p_listen_work_free(lwork);
1826 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1828 wpas_p2p_listen_work_free(lwork);
1836 static void wpas_stop_listen(void *ctx)
1838 struct wpa_supplicant *wpa_s = ctx;
1839 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1840 wpa_drv_cancel_remain_on_channel(wpa_s);
1841 wpa_s->off_channel_freq = 0;
1842 wpa_s->roc_waiting_drv_freq = 0;
1844 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1845 wpa_drv_probe_req_report(wpa_s, 0);
1846 wpas_p2p_listen_work_done(wpa_s);
1850 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1852 struct wpa_supplicant *wpa_s = ctx;
1853 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1858 * DNS Header section is used only to calculate compression pointers, so the
1859 * contents of this data does not matter, but the length needs to be reserved
1860 * in the virtual packet.
1862 #define DNS_HEADER_LEN 12
1865 * 27-octet in-memory packet from P2P specification containing two implied
1866 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1868 #define P2P_SD_IN_MEMORY_LEN 27
1870 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1871 u8 **spos, const u8 *end)
1873 while (*spos < end) {
1874 u8 val = ((*spos)[0] & 0xc0) >> 6;
1877 if (val == 1 || val == 2) {
1878 /* These are reserved values in RFC 1035 */
1879 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1880 "sequence starting with 0x%x", val);
1889 if (*spos + 2 > end) {
1890 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1891 "DNS offset field");
1895 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1896 if (offset >= *spos - start) {
1897 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1898 "pointer offset %u", offset);
1903 spos_tmp = start + offset;
1904 return p2p_sd_dns_uncompress_label(upos, uend, start,
1910 len = (*spos)[0] & 0x3f;
1915 if (*spos + len > end) {
1916 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1917 "sequence - no room for label with length "
1922 if (*upos + len + 2 > uend)
1925 os_memcpy(*upos, *spos, len);
1937 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1938 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1939 * not large enough */
1940 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1941 size_t msg_len, size_t offset)
1943 /* 27-octet in-memory packet from P2P specification */
1944 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1945 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1946 u8 *tmp, *end, *spos;
1952 if (offset > msg_len)
1955 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1958 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1959 end = spos + msg_len;
1962 os_memset(tmp, 0, DNS_HEADER_LEN);
1963 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1964 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1967 uend = buf + buf_len;
1969 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1978 } else if (upos[-1] == '.')
1986 static struct p2p_srv_bonjour *
1987 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1988 const struct wpabuf *query)
1990 struct p2p_srv_bonjour *bsrv;
1993 len = wpabuf_len(query);
1994 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1995 struct p2p_srv_bonjour, list) {
1996 if (len == wpabuf_len(bsrv->query) &&
1997 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
2005 static struct p2p_srv_upnp *
2006 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2007 const char *service)
2009 struct p2p_srv_upnp *usrv;
2011 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2012 struct p2p_srv_upnp, list) {
2013 if (version == usrv->version &&
2014 os_strcmp(service, usrv->service) == 0)
2021 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2026 if (wpabuf_tailroom(resp) < 5)
2029 /* Length (to be filled) */
2030 len_pos = wpabuf_put(resp, 2);
2031 wpabuf_put_u8(resp, srv_proto);
2032 wpabuf_put_u8(resp, srv_trans_id);
2034 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2035 /* Response Data: empty */
2036 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2040 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2041 struct wpabuf *resp, u8 srv_trans_id)
2043 struct p2p_srv_bonjour *bsrv;
2046 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2048 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2049 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2053 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2054 struct p2p_srv_bonjour, list) {
2055 if (wpabuf_tailroom(resp) <
2056 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2058 /* Length (to be filled) */
2059 len_pos = wpabuf_put(resp, 2);
2060 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2061 wpabuf_put_u8(resp, srv_trans_id);
2063 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2064 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2065 wpabuf_head(bsrv->resp),
2066 wpabuf_len(bsrv->resp));
2068 wpabuf_put_buf(resp, bsrv->query); /* Key */
2069 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2070 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2076 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2079 char str_rx[256], str_srv[256];
2081 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2082 return 0; /* Too short to include DNS Type and Version */
2083 if (os_memcmp(query + query_len - 3,
2084 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2086 return 0; /* Mismatch in DNS Type or Version */
2087 if (query_len == wpabuf_len(bsrv->query) &&
2088 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2089 return 1; /* Binary match */
2091 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2093 return 0; /* Failed to uncompress query */
2094 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2095 wpabuf_head(bsrv->query),
2096 wpabuf_len(bsrv->query) - 3, 0))
2097 return 0; /* Failed to uncompress service */
2099 return os_strcmp(str_rx, str_srv) == 0;
2103 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2104 struct wpabuf *resp, u8 srv_trans_id,
2105 const u8 *query, size_t query_len)
2107 struct p2p_srv_bonjour *bsrv;
2111 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2113 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2114 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2115 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2120 if (query_len == 0) {
2121 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2125 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2126 struct p2p_srv_bonjour, list) {
2127 if (!match_bonjour_query(bsrv, query, query_len))
2130 if (wpabuf_tailroom(resp) <
2131 5 + query_len + wpabuf_len(bsrv->resp))
2136 /* Length (to be filled) */
2137 len_pos = wpabuf_put(resp, 2);
2138 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2139 wpabuf_put_u8(resp, srv_trans_id);
2142 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2143 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2144 wpabuf_head(bsrv->resp),
2145 wpabuf_len(bsrv->resp));
2148 wpabuf_put_data(resp, query, query_len); /* Key */
2149 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2151 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2155 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2157 if (wpabuf_tailroom(resp) < 5)
2160 /* Length (to be filled) */
2161 len_pos = wpabuf_put(resp, 2);
2162 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2163 wpabuf_put_u8(resp, srv_trans_id);
2166 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2167 /* Response Data: empty */
2168 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2174 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2175 struct wpabuf *resp, u8 srv_trans_id)
2177 struct p2p_srv_upnp *usrv;
2180 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2182 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2183 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2187 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2188 struct p2p_srv_upnp, list) {
2189 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2192 /* Length (to be filled) */
2193 len_pos = wpabuf_put(resp, 2);
2194 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2195 wpabuf_put_u8(resp, srv_trans_id);
2198 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2200 wpabuf_put_u8(resp, usrv->version);
2201 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2203 wpabuf_put_str(resp, usrv->service);
2204 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2210 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2211 struct wpabuf *resp, u8 srv_trans_id,
2212 const u8 *query, size_t query_len)
2214 struct p2p_srv_upnp *usrv;
2220 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2223 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2224 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2225 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2230 if (query_len == 0) {
2231 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2235 if (wpabuf_tailroom(resp) < 5)
2238 /* Length (to be filled) */
2239 len_pos = wpabuf_put(resp, 2);
2240 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2241 wpabuf_put_u8(resp, srv_trans_id);
2244 str = os_malloc(query_len);
2247 os_memcpy(str, query + 1, query_len - 1);
2248 str[query_len - 1] = '\0';
2250 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2251 struct p2p_srv_upnp, list) {
2252 if (version != usrv->version)
2255 if (os_strcmp(str, "ssdp:all") != 0 &&
2256 os_strstr(usrv->service, str) == NULL)
2259 if (wpabuf_tailroom(resp) < 2)
2263 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2265 wpabuf_put_u8(resp, version);
2267 wpabuf_put_u8(resp, ',');
2271 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2273 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2275 wpabuf_put_str(resp, usrv->service);
2280 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2283 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2284 /* Response Data: empty */
2287 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2291 #ifdef CONFIG_WIFI_DISPLAY
2292 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2293 struct wpabuf *resp, u8 srv_trans_id,
2294 const u8 *query, size_t query_len)
2300 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2302 if (!wpa_s->global->wifi_display) {
2303 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2304 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2309 if (query_len < 1) {
2310 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2315 if (wpabuf_tailroom(resp) < 5)
2320 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2322 /* TODO: role specific handling */
2324 /* Length (to be filled) */
2325 len_pos = wpabuf_put(resp, 2);
2326 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2327 wpabuf_put_u8(resp, srv_trans_id);
2328 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2330 while (pos < query + query_len) {
2331 if (*pos < MAX_WFD_SUBELEMS &&
2332 wpa_s->global->wfd_subelem[*pos] &&
2333 wpabuf_tailroom(resp) >=
2334 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2335 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2336 "subelement %u", *pos);
2337 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2342 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2344 #endif /* CONFIG_WIFI_DISPLAY */
2347 static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2348 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2350 struct wpa_supplicant *wpa_s = ctx;
2351 const u8 *pos = tlvs;
2352 const u8 *end = tlvs + tlvs_len;
2355 struct wpabuf *resp;
2356 u8 srv_proto, srv_trans_id;
2360 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2362 buf_len = 2 * tlvs_len + 1;
2363 buf = os_malloc(buf_len);
2365 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2366 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2368 freq, MAC2STR(sa), dialog_token, update_indic,
2373 if (wpa_s->p2p_sd_over_ctrl_iface) {
2374 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2375 update_indic, tlvs, tlvs_len);
2376 return; /* to be processed by an external program */
2379 resp = wpabuf_alloc(10000);
2383 while (pos + 1 < end) {
2384 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2385 slen = WPA_GET_LE16(pos);
2387 if (pos + slen > end || slen < 2) {
2388 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2393 tlv_end = pos + slen;
2396 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2398 srv_trans_id = *pos++;
2399 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2402 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2403 pos, tlv_end - pos);
2406 if (wpa_s->force_long_sd) {
2407 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2409 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2410 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2414 switch (srv_proto) {
2415 case P2P_SERV_ALL_SERVICES:
2416 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2417 "for all services");
2418 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2419 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2420 wpa_printf(MSG_DEBUG, "P2P: No service "
2421 "discovery protocols available");
2422 wpas_sd_add_proto_not_avail(
2423 resp, P2P_SERV_ALL_SERVICES,
2427 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2428 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2430 case P2P_SERV_BONJOUR:
2431 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2432 pos, tlv_end - pos);
2435 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2436 pos, tlv_end - pos);
2438 #ifdef CONFIG_WIFI_DISPLAY
2439 case P2P_SERV_WIFI_DISPLAY:
2440 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2441 pos, tlv_end - pos);
2443 #endif /* CONFIG_WIFI_DISPLAY */
2445 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2446 "protocol %u", srv_proto);
2447 wpas_sd_add_proto_not_avail(resp, srv_proto,
2456 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2457 update_indic, tlvs, tlvs_len);
2459 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2465 static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2466 const u8 *tlvs, size_t tlvs_len)
2468 struct wpa_supplicant *wpa_s = ctx;
2469 const u8 *pos = tlvs;
2470 const u8 *end = tlvs + tlvs_len;
2476 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2478 if (tlvs_len > 1500) {
2479 /* TODO: better way for handling this */
2480 wpa_msg_ctrl(wpa_s, MSG_INFO,
2481 P2P_EVENT_SERV_DISC_RESP MACSTR
2482 " %u <long response: %u bytes>",
2483 MAC2STR(sa), update_indic,
2484 (unsigned int) tlvs_len);
2486 buf_len = 2 * tlvs_len + 1;
2487 buf = os_malloc(buf_len);
2489 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2490 wpa_msg_ctrl(wpa_s, MSG_INFO,
2491 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2492 MAC2STR(sa), update_indic, buf);
2498 u8 srv_proto, srv_trans_id, status;
2500 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2501 slen = WPA_GET_LE16(pos);
2503 if (pos + slen > end || slen < 3) {
2504 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2508 tlv_end = pos + slen;
2511 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2513 srv_trans_id = *pos++;
2514 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2517 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2520 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2521 pos, tlv_end - pos);
2526 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2530 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2531 const struct wpabuf *tlvs)
2533 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2535 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2539 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2540 u8 version, const char *query)
2542 struct wpabuf *tlvs;
2545 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2548 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2549 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2550 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2551 wpabuf_put_u8(tlvs, version);
2552 wpabuf_put_str(tlvs, query);
2553 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2559 #ifdef CONFIG_WIFI_DISPLAY
2561 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2562 const struct wpabuf *tlvs)
2564 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2566 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2570 #define MAX_WFD_SD_SUBELEMS 20
2572 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2573 const char *subelems)
2580 len = wpabuf_put(tlvs, 2);
2581 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2582 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2584 wpabuf_put_u8(tlvs, role);
2589 if (val >= 0 && val < 256) {
2590 wpabuf_put_u8(tlvs, val);
2592 if (count == MAX_WFD_SD_SUBELEMS)
2595 pos = os_strchr(pos + 1, ',');
2601 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2605 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2606 const u8 *dst, const char *role)
2608 struct wpabuf *tlvs;
2610 const char *subelems;
2613 subelems = os_strchr(role, ' ');
2614 if (subelems == NULL)
2618 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2622 if (os_strstr(role, "[source]"))
2623 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2624 if (os_strstr(role, "[pri-sink]"))
2625 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2626 if (os_strstr(role, "[sec-sink]"))
2627 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2628 if (os_strstr(role, "[source+sink]"))
2629 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2631 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2636 #endif /* CONFIG_WIFI_DISPLAY */
2639 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2641 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2643 return p2p_sd_cancel_request(wpa_s->global->p2p,
2644 (void *) (uintptr_t) req);
2648 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2649 const u8 *dst, u8 dialog_token,
2650 const struct wpabuf *resp_tlvs)
2652 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2654 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2659 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2661 if (wpa_s->global->p2p)
2662 p2p_sd_service_update(wpa_s->global->p2p);
2666 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2668 dl_list_del(&bsrv->list);
2669 wpabuf_free(bsrv->query);
2670 wpabuf_free(bsrv->resp);
2675 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2677 dl_list_del(&usrv->list);
2678 os_free(usrv->service);
2683 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2685 struct p2p_srv_bonjour *bsrv, *bn;
2686 struct p2p_srv_upnp *usrv, *un;
2688 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2689 struct p2p_srv_bonjour, list)
2690 wpas_p2p_srv_bonjour_free(bsrv);
2692 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2693 struct p2p_srv_upnp, list)
2694 wpas_p2p_srv_upnp_free(usrv);
2696 wpas_p2p_sd_service_update(wpa_s);
2700 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2701 struct wpabuf *query, struct wpabuf *resp)
2703 struct p2p_srv_bonjour *bsrv;
2705 bsrv = os_zalloc(sizeof(*bsrv));
2708 bsrv->query = query;
2710 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2712 wpas_p2p_sd_service_update(wpa_s);
2717 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2718 const struct wpabuf *query)
2720 struct p2p_srv_bonjour *bsrv;
2722 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2725 wpas_p2p_srv_bonjour_free(bsrv);
2726 wpas_p2p_sd_service_update(wpa_s);
2731 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2732 const char *service)
2734 struct p2p_srv_upnp *usrv;
2736 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2737 return 0; /* Already listed */
2738 usrv = os_zalloc(sizeof(*usrv));
2741 usrv->version = version;
2742 usrv->service = os_strdup(service);
2743 if (usrv->service == NULL) {
2747 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2749 wpas_p2p_sd_service_update(wpa_s);
2754 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2755 const char *service)
2757 struct p2p_srv_upnp *usrv;
2759 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2762 wpas_p2p_srv_upnp_free(usrv);
2763 wpas_p2p_sd_service_update(wpa_s);
2768 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2769 const u8 *peer, const char *params,
2770 unsigned int generated_pin)
2772 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2773 " %08d%s", MAC2STR(peer), generated_pin, params);
2777 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2778 const u8 *peer, const char *params)
2780 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2781 "%s", MAC2STR(peer), params);
2785 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2786 const u8 *dev_addr, const u8 *pri_dev_type,
2787 const char *dev_name, u16 supp_config_methods,
2788 u8 dev_capab, u8 group_capab, const u8 *group_id,
2789 size_t group_id_len)
2791 struct wpa_supplicant *wpa_s = ctx;
2792 char devtype[WPS_DEV_TYPE_BUFSIZE];
2794 u8 empty_dev_type[8];
2795 unsigned int generated_pin = 0;
2796 struct wpa_supplicant *group = NULL;
2799 for (group = wpa_s->global->ifaces; group; group = group->next)
2801 struct wpa_ssid *s = group->current_ssid;
2803 s->mode == WPAS_MODE_P2P_GO &&
2804 group_id_len - ETH_ALEN == s->ssid_len &&
2805 os_memcmp(group_id + ETH_ALEN, s->ssid,
2811 if (pri_dev_type == NULL) {
2812 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2813 pri_dev_type = empty_dev_type;
2815 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2816 " pri_dev_type=%s name='%s' config_methods=0x%x "
2817 "dev_capab=0x%x group_capab=0x%x%s%s",
2819 wps_dev_type_bin2str(pri_dev_type, devtype,
2821 dev_name, supp_config_methods, dev_capab, group_capab,
2822 group ? " group=" : "",
2823 group ? group->ifname : "");
2824 params[sizeof(params) - 1] = '\0';
2826 if (config_methods & WPS_CONFIG_DISPLAY) {
2827 generated_pin = wps_generate_pin();
2828 wpas_prov_disc_local_display(wpa_s, peer, params,
2830 } else if (config_methods & WPS_CONFIG_KEYPAD)
2831 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2832 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2833 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2834 MACSTR "%s", MAC2STR(peer), params);
2836 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2837 P2P_PROV_DISC_SUCCESS,
2838 config_methods, generated_pin);
2842 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2844 struct wpa_supplicant *wpa_s = ctx;
2845 unsigned int generated_pin = 0;
2848 if (wpa_s->pending_pd_before_join &&
2849 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2850 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2851 wpa_s->pending_pd_before_join = 0;
2852 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2853 "join-existing-group operation");
2854 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2858 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2859 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2860 os_snprintf(params, sizeof(params), " peer_go=%d",
2861 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2865 if (config_methods & WPS_CONFIG_DISPLAY)
2866 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2867 else if (config_methods & WPS_CONFIG_KEYPAD) {
2868 generated_pin = wps_generate_pin();
2869 wpas_prov_disc_local_display(wpa_s, peer, params,
2871 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2872 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2873 MACSTR "%s", MAC2STR(peer), params);
2875 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2876 P2P_PROV_DISC_SUCCESS,
2877 config_methods, generated_pin);
2881 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2882 enum p2p_prov_disc_status status)
2884 struct wpa_supplicant *wpa_s = ctx;
2886 if (wpa_s->p2p_fallback_to_go_neg) {
2887 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2888 "failed - fall back to GO Negotiation");
2889 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2893 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2894 wpa_s->pending_pd_before_join = 0;
2895 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2896 "join-existing-group operation (no ACK for PD "
2898 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2902 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2903 " p2p_dev_addr=" MACSTR " status=%d",
2904 MAC2STR(peer), status);
2906 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2911 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2913 if (channels == NULL)
2914 return 1; /* Assume no restrictions */
2915 return p2p_channels_includes_freq(channels, freq);
2921 * Pick the best frequency to use from all the currently used frequencies.
2923 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2924 struct wpa_used_freq_data *freqs,
2929 /* find a candidate freq that is supported by P2P */
2930 for (c = 0; c < num; c++)
2931 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2937 /* once we have a candidate, try to find a 'better' one */
2938 for (i = c + 1; i < num; i++) {
2939 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2943 * 1. Infrastructure station interfaces have higher preference.
2944 * 2. P2P Clients have higher preference.
2947 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2952 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2955 return freqs[c].freq;
2959 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2960 const u8 *go_dev_addr, const u8 *ssid,
2961 size_t ssid_len, int *go, u8 *group_bssid,
2962 int *force_freq, int persistent_group,
2963 const struct p2p_channels *channels,
2966 struct wpa_supplicant *wpa_s = ctx;
2968 struct wpa_used_freq_data *freqs;
2969 struct wpa_supplicant *grp;
2972 if (!persistent_group) {
2973 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2974 " to join an active group (SSID: %s)",
2975 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
2976 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2977 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2979 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2980 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2981 "authorized invitation");
2985 #ifdef CONFIG_WPS_NFC
2986 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
2987 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
2988 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2989 wpa_s->parent->p2p_wps_method = WPS_NFC;
2990 wpa_s->parent->pending_join_wps_method = WPS_NFC;
2991 os_memcpy(wpa_s->parent->pending_join_dev_addr,
2992 go_dev_addr, ETH_ALEN);
2993 os_memcpy(wpa_s->parent->pending_join_iface_addr,
2997 #endif /* CONFIG_WPS_NFC */
3000 * Do not accept the invitation automatically; notify user and
3003 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3006 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3008 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3009 "running persistent group");
3011 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3015 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3016 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3017 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3018 "invitation to re-invoke a persistent group");
3019 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3020 } else if (!wpa_s->conf->persistent_reconnect)
3021 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3023 for (s = wpa_s->conf->ssid; s; s = s->next) {
3024 if (s->disabled == 2 &&
3025 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3026 s->ssid_len == ssid_len &&
3027 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3032 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3033 " requested reinvocation of an unknown group",
3035 return P2P_SC_FAIL_UNKNOWN_GROUP;
3038 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3040 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3041 wpa_printf(MSG_DEBUG, "P2P: The only available "
3042 "interface is already in use - reject "
3044 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3046 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3047 } else if (s->mode == WPAS_MODE_P2P_GO) {
3049 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3051 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3052 "interface address for the group");
3053 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3055 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3060 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3063 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3064 sizeof(struct wpa_used_freq_data));
3066 int num_channels = wpa_s->num_multichan_concurrent;
3067 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3068 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3072 /* Get one of the frequencies currently in use */
3073 if (best_freq > 0) {
3074 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3075 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
3077 if (wpa_s->num_multichan_concurrent < 2 ||
3078 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3079 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3080 *force_freq = best_freq;
3084 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3085 wpas_p2p_num_unused_channels(wpa_s) > 0) {
3087 /* We are the client */
3088 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3089 "running a GO but we are capable of MCC, "
3090 "figure out the best channel to use");
3092 } else if (!freq_included(channels, *force_freq)) {
3093 /* We are the GO, and *force_freq is not in the
3095 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3096 "in intersection but we are capable of MCC, "
3097 "figure out the best channel to use",
3103 return P2P_SC_SUCCESS;
3107 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3108 const u8 *ssid, size_t ssid_len,
3109 const u8 *go_dev_addr, u8 status,
3112 struct wpa_supplicant *wpa_s = ctx;
3115 for (s = wpa_s->conf->ssid; s; s = s->next) {
3116 if (s->disabled == 2 &&
3117 s->ssid_len == ssid_len &&
3118 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3122 if (status == P2P_SC_SUCCESS) {
3123 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3124 " was accepted; op_freq=%d MHz, SSID=%s",
3125 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3127 int go = s->mode == WPAS_MODE_P2P_GO;
3128 wpas_p2p_group_add_persistent(
3129 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
3130 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3132 wpa_s->user_initiated_pd = 0;
3133 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
3134 wpa_s->p2p_wps_method, 0, op_freq,
3140 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3141 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3142 " was rejected (status %u)", MAC2STR(sa), status);
3148 wpa_msg_global(wpa_s, MSG_INFO,
3149 P2P_EVENT_INVITATION_RECEIVED
3150 "sa=" MACSTR " go_dev_addr=" MACSTR
3151 " bssid=" MACSTR " unknown-network",
3152 MAC2STR(sa), MAC2STR(go_dev_addr),
3155 wpa_msg_global(wpa_s, MSG_INFO,
3156 P2P_EVENT_INVITATION_RECEIVED
3157 "sa=" MACSTR " go_dev_addr=" MACSTR
3159 MAC2STR(sa), MAC2STR(go_dev_addr));
3164 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
3165 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3166 "sa=" MACSTR " persistent=%d freq=%d",
3167 MAC2STR(sa), s->id, op_freq);
3169 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3170 "sa=" MACSTR " persistent=%d",
3171 MAC2STR(sa), s->id);
3176 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3177 struct wpa_ssid *ssid,
3178 const u8 *peer, int inv)
3185 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3186 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3190 if (i >= ssid->num_p2p_clients) {
3191 if (ssid->mode != WPAS_MODE_P2P_GO &&
3192 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3193 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3194 "due to invitation result", ssid->id);
3195 wpas_notify_network_removed(wpa_s, ssid);
3196 wpa_config_remove_network(wpa_s->conf, ssid->id);
3199 return; /* Peer not found in client list */
3202 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
3203 "group %d client list%s",
3204 MAC2STR(peer), ssid->id,
3205 inv ? " due to invitation result" : "");
3206 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3207 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3208 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3209 ssid->num_p2p_clients--;
3210 if (wpa_s->parent->conf->update_config &&
3211 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3212 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3216 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3219 struct wpa_ssid *ssid;
3221 wpa_s = wpa_s->global->p2p_invite_group;
3223 return; /* No known invitation group */
3224 ssid = wpa_s->current_ssid;
3225 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3226 !ssid->p2p_persistent_group)
3227 return; /* Not operating as a GO in persistent group */
3228 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3229 ssid->ssid, ssid->ssid_len);
3230 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3234 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
3235 const struct p2p_channels *channels,
3236 const u8 *peer, int neg_freq,
3239 struct wpa_supplicant *wpa_s = ctx;
3240 struct wpa_ssid *ssid;
3244 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3245 "status=%d " MACSTR,
3246 status, MAC2STR(bssid));
3248 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3249 "status=%d ", status);
3251 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3253 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3254 status, MAC2STR(peer));
3255 if (wpa_s->pending_invite_ssid_id == -1) {
3256 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3257 wpas_remove_persistent_client(wpa_s, peer);
3258 return; /* Invitation to active group */
3261 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3262 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3263 "invitation exchange to indicate readiness for "
3267 if (status != P2P_SC_SUCCESS) {
3268 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3269 ssid = wpa_config_get_network(
3270 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3271 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3273 wpas_p2p_remove_pending_group_interface(wpa_s);
3277 ssid = wpa_config_get_network(wpa_s->conf,
3278 wpa_s->pending_invite_ssid_id);
3280 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3281 "data matching with invitation");
3286 * The peer could have missed our ctrl::ack frame for Invitation
3287 * Response and continue retransmitting the frame. To reduce the
3288 * likelihood of the peer not getting successful TX status for the
3289 * Invitation Response frame, wait a short time here before starting
3290 * the persistent group so that we will remain on the current channel to
3291 * acknowledge any possible retransmission from the peer.
3293 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3294 "starting persistent group");
3297 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3298 freq_included(channels, neg_freq))
3300 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3301 freq_included(channels, peer_oper_freq))
3302 freq = peer_oper_freq;
3306 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3307 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3308 wpas_p2p_group_add_persistent(wpa_s, ssid,
3309 ssid->mode == WPAS_MODE_P2P_GO,
3310 wpa_s->p2p_persistent_go_freq,
3312 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3314 ssid->mode == WPAS_MODE_P2P_GO ?
3315 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3320 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3323 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3325 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3329 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3331 reg->channel[reg->channels] = chan;
3336 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3337 struct p2p_channels *chan,
3338 struct p2p_channels *cli_chan)
3342 os_memset(cli_chan, 0, sizeof(*cli_chan));
3344 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3347 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3348 chan->reg_class[cla].reg_class = 81;
3349 chan->reg_class[cla].channels = 0;
3350 for (i = 0; i < 11; i++) {
3351 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3352 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3354 if (chan->reg_class[cla].channels)
3357 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3360 /* Operating class 115 - 5 GHz, channels 36-48 */
3361 chan->reg_class[cla].reg_class = 115;
3362 chan->reg_class[cla].channels = 0;
3363 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3364 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3365 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3366 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3367 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3368 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3369 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3370 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3371 if (chan->reg_class[cla].channels)
3374 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3377 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3378 chan->reg_class[cla].reg_class = 124;
3379 chan->reg_class[cla].channels = 0;
3380 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3381 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3382 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3383 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3384 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3385 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3386 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3387 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3388 if (chan->reg_class[cla].channels)
3391 chan->reg_classes = cla;
3396 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3398 enum hostapd_hw_mode mode)
3402 for (i = 0; i < num_modes; i++) {
3403 if (modes[i].mode == mode)
3412 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3415 static int has_channel(struct wpa_global *global,
3416 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3421 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3423 if (wpas_p2p_disallowed_freq(global, freq))
3426 for (i = 0; i < mode->num_channels; i++) {
3427 if (mode->channels[i].chan == chan) {
3429 *flags = mode->channels[i].flag;
3430 if (mode->channels[i].flag &
3431 (HOSTAPD_CHAN_DISABLED |
3432 HOSTAPD_CHAN_RADAR))
3434 if (mode->channels[i].flag &
3435 (HOSTAPD_CHAN_PASSIVE_SCAN |
3436 HOSTAPD_CHAN_NO_IBSS))
3437 return PASSIVE_ONLY;
3446 struct p2p_oper_class_map {
3447 enum hostapd_hw_mode mode;
3452 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
3455 static struct p2p_oper_class_map op_class[] = {
3456 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3457 #if 0 /* Do not enable HT40 on 2 GHz for now */
3458 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3459 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3461 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3462 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3463 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3464 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3465 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3466 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3469 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3470 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3471 * 80 MHz, but currently use the following definition for simplicity
3472 * (these center frequencies are not actual channels, which makes
3473 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3474 * removing invalid channels.
3476 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3477 { -1, 0, 0, 0, 0, BW20 }
3481 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3482 struct hostapd_hw_modes *mode,
3485 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3488 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3491 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3493 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3494 * so the center channel is 6 channels away from the start/end.
3496 if (channel >= center_channels[i] - 6 &&
3497 channel <= center_channels[i] + 6)
3498 return center_channels[i];
3504 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3505 struct hostapd_hw_modes *mode,
3510 enum chan_allowed res, ret = ALLOWED;
3512 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3515 if (center_chan >= 58 && center_chan <= 138)
3516 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3518 /* check all the channels are available */
3519 for (i = 0; i < 4; i++) {
3520 int adj_chan = center_chan - 6 + i * 4;
3522 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3523 if (res == NOT_ALLOWED)
3525 if (res == PASSIVE_ONLY)
3528 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3530 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3532 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3534 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3542 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3543 struct hostapd_hw_modes *mode,
3547 enum chan_allowed res, res2;
3549 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3550 if (bw == BW40MINUS) {
3551 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3553 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3554 } else if (bw == BW40PLUS) {
3555 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3557 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3558 } else if (bw == BW80) {
3559 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3562 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3564 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3565 return PASSIVE_ONLY;
3570 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3571 struct p2p_channels *chan,
3572 struct p2p_channels *cli_chan)
3574 struct hostapd_hw_modes *mode;
3575 int cla, op, cli_cla;
3577 if (wpa_s->hw.modes == NULL) {
3578 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3579 "of all supported channels; assume dualband "
3581 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3586 for (op = 0; op_class[op].op_class; op++) {
3587 struct p2p_oper_class_map *o = &op_class[op];
3589 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3591 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3594 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3595 enum chan_allowed res;
3596 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3597 if (res == ALLOWED) {
3599 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3601 reg = &chan->reg_class[cla];
3603 reg->reg_class = o->op_class;
3605 reg->channel[reg->channels] = ch;
3607 } else if (res == PASSIVE_ONLY &&
3608 wpa_s->conf->p2p_add_cli_chan) {
3609 if (cli_reg == NULL) {
3610 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3612 cli_reg = &cli_chan->reg_class[cli_cla];
3614 cli_reg->reg_class = o->op_class;
3616 cli_reg->channel[cli_reg->channels] = ch;
3617 cli_reg->channels++;
3621 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3622 reg->channel, reg->channels);
3625 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3626 cli_reg->channel, cli_reg->channels);
3630 chan->reg_classes = cla;
3631 cli_chan->reg_classes = cli_cla;
3637 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3638 struct hostapd_hw_modes *mode, u8 channel)
3641 enum chan_allowed ret;
3643 for (op = 0; op_class[op].op_class; op++) {
3644 struct p2p_oper_class_map *o = &op_class[op];
3647 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3648 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3649 o->bw == BW20 || ch != channel)
3651 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3653 return (o->bw == BW40MINUS) ? -1 : 1;
3660 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3661 struct hostapd_hw_modes *mode, u8 channel)
3663 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3666 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3670 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3673 struct wpa_supplicant *wpa_s = ctx;
3675 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3676 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3682 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3686 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3688 struct wpa_supplicant *wpa_s = ctx;
3690 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3691 struct wpa_ssid *ssid = wpa_s->current_ssid;
3694 if (ssid->mode != WPAS_MODE_INFRA)
3696 if (wpa_s->wpa_state != WPA_COMPLETED &&
3697 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3699 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3707 static int wpas_is_concurrent_session_active(void *ctx)
3709 struct wpa_supplicant *wpa_s = ctx;
3710 struct wpa_supplicant *ifs;
3712 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3715 if (ifs->wpa_state > WPA_ASSOCIATED)
3722 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3724 struct wpa_supplicant *wpa_s = ctx;
3725 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3729 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3731 struct wpa_interface iface;
3732 struct wpa_supplicant *p2pdev_wpa_s;
3734 char force_name[100];
3737 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3739 force_name[0] = '\0';
3740 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3741 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3742 force_name, wpa_s->pending_interface_addr, NULL);
3744 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3747 os_strlcpy(wpa_s->pending_interface_name, ifname,
3748 sizeof(wpa_s->pending_interface_name));
3750 os_memset(&iface, 0, sizeof(iface));
3752 iface.ifname = wpa_s->pending_interface_name;
3753 iface.driver = wpa_s->driver->name;
3754 iface.driver_param = wpa_s->conf->driver_param;
3757 * If a P2P Device configuration file was given, use it as the interface
3758 * configuration file (instead of using parent's configuration file.
3760 if (wpa_s->conf_p2p_dev) {
3761 iface.confname = wpa_s->conf_p2p_dev;
3762 iface.ctrl_interface = NULL;
3764 iface.confname = wpa_s->confname;
3765 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3767 iface.conf_p2p_dev = NULL;
3769 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3770 if (!p2pdev_wpa_s) {
3771 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3774 p2pdev_wpa_s->parent = wpa_s;
3776 wpa_s->pending_interface_name[0] = '\0';
3781 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3782 const u8 *noa, size_t noa_len)
3784 struct wpa_supplicant *wpa_s, *intf = ctx;
3787 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3788 if (wpa_s->waiting_presence_resp)
3792 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3795 wpa_s->waiting_presence_resp = 0;
3797 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3798 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3799 " status=%u noa=%s", MAC2STR(src), status, hex);
3803 static int _wpas_p2p_in_progress(void *ctx)
3805 struct wpa_supplicant *wpa_s = ctx;
3806 return wpas_p2p_in_progress(wpa_s);
3811 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3812 * @global: Pointer to global data from wpa_supplicant_init()
3813 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3814 * Returns: 0 on success, -1 on failure
3816 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3818 struct p2p_config p2p;
3822 if (wpa_s->conf->p2p_disabled)
3825 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3831 os_memset(&p2p, 0, sizeof(p2p));
3833 p2p.debug_print = wpas_p2p_debug_print;
3834 p2p.p2p_scan = wpas_p2p_scan;
3835 p2p.send_action = wpas_send_action;
3836 p2p.send_action_done = wpas_send_action_done;
3837 p2p.go_neg_completed = wpas_go_neg_completed;
3838 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3839 p2p.dev_found = wpas_dev_found;
3840 p2p.dev_lost = wpas_dev_lost;
3841 p2p.find_stopped = wpas_find_stopped;
3842 p2p.start_listen = wpas_start_listen;
3843 p2p.stop_listen = wpas_stop_listen;
3844 p2p.send_probe_resp = wpas_send_probe_resp;
3845 p2p.sd_request = wpas_sd_request;
3846 p2p.sd_response = wpas_sd_response;
3847 p2p.prov_disc_req = wpas_prov_disc_req;
3848 p2p.prov_disc_resp = wpas_prov_disc_resp;
3849 p2p.prov_disc_fail = wpas_prov_disc_fail;
3850 p2p.invitation_process = wpas_invitation_process;
3851 p2p.invitation_received = wpas_invitation_received;
3852 p2p.invitation_result = wpas_invitation_result;
3853 p2p.get_noa = wpas_get_noa;
3854 p2p.go_connected = wpas_go_connected;
3855 p2p.presence_resp = wpas_presence_resp;
3856 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
3857 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
3859 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
3860 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
3861 p2p.dev_name = wpa_s->conf->device_name;
3862 p2p.manufacturer = wpa_s->conf->manufacturer;
3863 p2p.model_name = wpa_s->conf->model_name;
3864 p2p.model_number = wpa_s->conf->model_number;
3865 p2p.serial_number = wpa_s->conf->serial_number;
3867 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3868 p2p.config_methods = wpa_s->wps->config_methods;
3871 if (wpa_s->conf->p2p_listen_reg_class &&
3872 wpa_s->conf->p2p_listen_channel) {
3873 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3874 p2p.channel = wpa_s->conf->p2p_listen_channel;
3878 * Pick one of the social channels randomly as the listen
3881 os_get_random((u8 *) &r, sizeof(r));
3882 p2p.channel = 1 + (r % 3) * 5;
3884 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3886 if (wpa_s->conf->p2p_oper_reg_class &&
3887 wpa_s->conf->p2p_oper_channel) {
3888 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3889 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3890 p2p.cfg_op_channel = 1;
3891 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3892 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3895 p2p.op_reg_class = 81;
3897 * Use random operation channel from (1, 6, 11) if no other
3898 * preference is indicated.
3900 os_get_random((u8 *) &r, sizeof(r));
3901 p2p.op_channel = 1 + (r % 3) * 5;
3902 p2p.cfg_op_channel = 0;
3903 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3904 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3907 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3908 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3909 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3912 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3913 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3914 p2p.country[2] = 0x04;
3916 os_memcpy(p2p.country, "XX\x04", 3);
3918 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3919 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3924 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3927 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3928 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3929 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3931 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3932 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3934 p2p.max_peers = 100;
3936 if (wpa_s->conf->p2p_ssid_postfix) {
3937 p2p.ssid_postfix_len =
3938 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3939 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3940 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3941 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3942 p2p.ssid_postfix_len);
3945 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3947 p2p.max_listen = wpa_s->max_remain_on_chan;
3949 global->p2p = p2p_init(&p2p);
3950 if (global->p2p == NULL)
3952 global->p2p_init_wpa_s = wpa_s;
3954 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3955 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3957 p2p_add_wps_vendor_extension(
3958 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3961 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3968 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3969 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3971 * This function deinitialize per-interface P2P data.
3973 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3975 if (wpa_s->driver && wpa_s->drv_priv)
3976 wpa_drv_probe_req_report(wpa_s, 0);
3978 if (wpa_s->go_params) {
3979 /* Clear any stored provisioning info */
3980 p2p_clear_provisioning_info(
3982 wpa_s->go_params->peer_device_addr);
3985 os_free(wpa_s->go_params);
3986 wpa_s->go_params = NULL;
3987 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
3988 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3989 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3990 wpa_s->p2p_long_listen = 0;
3991 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3992 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3993 wpas_p2p_remove_pending_group_interface(wpa_s);
3994 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3995 wpas_p2p_listen_work_done(wpa_s);
3996 if (wpa_s->p2p_send_action_work) {
3997 os_free(wpa_s->p2p_send_action_work->ctx);
3998 radio_work_done(wpa_s->p2p_send_action_work);
3999 wpa_s->p2p_send_action_work = NULL;
4001 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4003 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4004 wpa_s->p2p_oob_dev_pw = NULL;
4006 /* TODO: remove group interface from the driver if this wpa_s instance
4007 * is on top of a P2P group interface */
4012 * wpas_p2p_deinit_global - Deinitialize global P2P module
4013 * @global: Pointer to global data from wpa_supplicant_init()
4015 * This function deinitializes the global (per device) P2P module.
4017 void wpas_p2p_deinit_global(struct wpa_global *global)
4019 struct wpa_supplicant *wpa_s, *tmp;
4021 wpa_s = global->ifaces;
4023 wpas_p2p_service_flush(wpa_s);
4025 if (global->p2p == NULL)
4028 /* Remove remaining P2P group interfaces */
4029 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4030 wpa_s = wpa_s->next;
4032 tmp = global->ifaces;
4035 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4040 /* Disconnect from the P2P group and deinit the interface */
4041 wpas_p2p_disconnect(tmp);
4045 * Deinit GO data on any possibly remaining interface (if main
4046 * interface is used as GO).
4048 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4049 if (wpa_s->ap_iface)
4050 wpas_p2p_group_deinit(wpa_s);
4053 p2p_deinit(global->p2p);
4055 global->p2p_init_wpa_s = NULL;
4059 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4061 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4062 wpa_s->conf->p2p_no_group_iface)
4063 return 0; /* separate interface disabled per configuration */
4064 if (wpa_s->drv_flags &
4065 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4066 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4067 return 1; /* P2P group requires a new interface in every case
4069 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4070 return 0; /* driver does not support concurrent operations */
4071 if (wpa_s->global->ifaces->next)
4072 return 1; /* more that one interface already in use */
4073 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4074 return 1; /* this interface is already in use */
4079 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4080 const u8 *peer_addr,
4081 enum p2p_wps_method wps_method,
4082 int go_intent, const u8 *own_interface_addr,
4083 unsigned int force_freq, int persistent_group,
4084 struct wpa_ssid *ssid, unsigned int pref_freq)
4086 if (persistent_group && wpa_s->conf->persistent_reconnect)
4087 persistent_group = 2;
4090 * Increase GO config timeout if HT40 is used since it takes some time
4091 * to scan channels for coex purposes before the BSS can be started.
4093 p2p_set_config_timeout(wpa_s->global->p2p,
4094 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4096 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4097 go_intent, own_interface_addr, force_freq,
4098 persistent_group, ssid ? ssid->ssid : NULL,
4099 ssid ? ssid->ssid_len : 0,
4100 wpa_s->p2p_pd_before_go_neg, pref_freq,
4101 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4106 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4107 const u8 *peer_addr,
4108 enum p2p_wps_method wps_method,
4109 int go_intent, const u8 *own_interface_addr,
4110 unsigned int force_freq, int persistent_group,
4111 struct wpa_ssid *ssid, unsigned int pref_freq)
4113 if (persistent_group && wpa_s->conf->persistent_reconnect)
4114 persistent_group = 2;
4116 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4117 go_intent, own_interface_addr, force_freq,
4118 persistent_group, ssid ? ssid->ssid : NULL,
4119 ssid ? ssid->ssid_len : 0, pref_freq,
4120 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4125 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4127 wpa_s->p2p_join_scan_count++;
4128 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4129 wpa_s->p2p_join_scan_count);
4130 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4131 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4132 " for join operationg - stop join attempt",
4133 MAC2STR(wpa_s->pending_join_iface_addr));
4134 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4135 if (wpa_s->p2p_auto_pd) {
4136 wpa_s->p2p_auto_pd = 0;
4137 wpa_msg_global(wpa_s, MSG_INFO,
4138 P2P_EVENT_PROV_DISC_FAILURE
4139 " p2p_dev_addr=" MACSTR " status=N/A",
4140 MAC2STR(wpa_s->pending_join_dev_addr));
4143 wpa_msg_global(wpa_s->parent, MSG_INFO,
4144 P2P_EVENT_GROUP_FORMATION_FAILURE);
4149 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4152 unsigned int num, i;
4153 struct wpa_used_freq_data *freqs;
4155 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4156 /* Multiple channels are supported and not all are in use */
4160 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4161 sizeof(struct wpa_used_freq_data));
4165 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4166 wpa_s->num_multichan_concurrent);
4168 for (i = 0; i < num; i++) {
4169 if (freqs[i].freq == freq) {
4170 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4177 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4186 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4187 const u8 *peer_dev_addr)
4189 struct wpa_bss *bss;
4192 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4195 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4196 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4201 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4203 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4204 "%ld.%06ld (%supdated in last scan)",
4205 bss->last_update.sec, bss->last_update.usec,
4206 updated ? "": "not ");
4212 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4213 struct wpa_scan_results *scan_res)
4215 struct wpa_bss *bss = NULL;
4217 u8 iface_addr[ETH_ALEN];
4219 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4221 if (wpa_s->global->p2p_disabled)
4224 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4225 scan_res ? (int) scan_res->num : -1,
4226 wpa_s->p2p_auto_join ? "auto_" : "");
4229 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4231 if (wpa_s->p2p_auto_pd) {
4232 int join = wpas_p2p_peer_go(wpa_s,
4233 wpa_s->pending_join_dev_addr);
4235 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4236 wpa_s->auto_pd_scan_retry++;
4237 bss = wpa_bss_get_bssid_latest(
4238 wpa_s, wpa_s->pending_join_dev_addr);
4241 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4242 "the peer " MACSTR " at %d MHz",
4243 wpa_s->auto_pd_scan_retry,
4245 pending_join_dev_addr),
4247 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4255 wpa_s->p2p_auto_pd = 0;
4256 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4257 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4258 MAC2STR(wpa_s->pending_join_dev_addr), join);
4259 if (p2p_prov_disc_req(wpa_s->global->p2p,
4260 wpa_s->pending_join_dev_addr,
4261 wpa_s->pending_pd_config_methods, join,
4262 0, wpa_s->user_initiated_pd) < 0) {
4263 wpa_s->p2p_auto_pd = 0;
4264 wpa_msg_global(wpa_s, MSG_INFO,
4265 P2P_EVENT_PROV_DISC_FAILURE
4266 " p2p_dev_addr=" MACSTR " status=N/A",
4267 MAC2STR(wpa_s->pending_join_dev_addr));
4272 if (wpa_s->p2p_auto_join) {
4273 int join = wpas_p2p_peer_go(wpa_s,
4274 wpa_s->pending_join_dev_addr);
4276 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4277 "running a GO -> use GO Negotiation");
4278 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4279 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4280 wpa_s->p2p_persistent_group, 0, 0, 0,
4281 wpa_s->p2p_go_intent,
4282 wpa_s->p2p_connect_freq,
4283 wpa_s->p2p_persistent_id,
4284 wpa_s->p2p_pd_before_go_neg,
4290 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4291 "try to join the group", join ? "" :
4294 wpa_s->p2p_fallback_to_go_neg = 1;
4297 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4298 wpa_s->pending_join_iface_addr);
4300 p2p_get_interface_addr(wpa_s->global->p2p,
4301 wpa_s->pending_join_dev_addr,
4303 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4305 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4306 "address for join from " MACSTR " to " MACSTR
4307 " based on newly discovered P2P peer entry",
4308 MAC2STR(wpa_s->pending_join_iface_addr),
4309 MAC2STR(iface_addr));
4310 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4313 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4314 wpa_s->pending_join_iface_addr);
4317 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4318 "from P2P peer table: %d MHz", freq);
4320 if (wpa_s->p2p_join_ssid_len) {
4321 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4322 MACSTR " and SSID %s",
4323 MAC2STR(wpa_s->pending_join_iface_addr),
4324 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4325 wpa_s->p2p_join_ssid_len));
4326 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4327 wpa_s->p2p_join_ssid,
4328 wpa_s->p2p_join_ssid_len);
4331 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4332 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4333 bss = wpa_bss_get_bssid_latest(wpa_s,
4334 wpa_s->pending_join_iface_addr);
4338 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4339 "from BSS table: %d MHz (SSID %s)", freq,
4340 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4345 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4346 wpa_msg_global(wpa_s->parent, MSG_INFO,
4347 P2P_EVENT_GROUP_FORMATION_FAILURE
4348 "reason=FREQ_CONFLICT");
4352 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4353 "prior to joining an existing group (GO " MACSTR
4355 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4356 wpa_s->pending_pd_before_join = 1;
4358 switch (wpa_s->pending_join_wps_method) {
4359 case WPS_PIN_DISPLAY:
4360 method = WPS_CONFIG_KEYPAD;
4362 case WPS_PIN_KEYPAD:
4363 method = WPS_CONFIG_DISPLAY;
4366 method = WPS_CONFIG_PUSHBUTTON;
4373 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4374 wpa_s->pending_join_dev_addr) ==
4377 * We have already performed provision discovery for
4378 * joining the group. Proceed directly to join
4379 * operation without duplicated provision discovery. */
4380 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4381 "with " MACSTR " already done - proceed to "
4383 MAC2STR(wpa_s->pending_join_dev_addr));
4384 wpa_s->pending_pd_before_join = 0;
4388 if (p2p_prov_disc_req(wpa_s->global->p2p,
4389 wpa_s->pending_join_dev_addr, method, 1,
4390 freq, wpa_s->user_initiated_pd) < 0) {
4391 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4392 "Discovery Request before joining an "
4394 wpa_s->pending_pd_before_join = 0;
4400 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4401 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4402 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4403 wpas_p2p_check_join_scan_limit(wpa_s);
4407 /* Start join operation immediately */
4408 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4412 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4413 const u8 *ssid, size_t ssid_len)
4416 struct wpa_driver_scan_params params;
4417 struct wpabuf *wps_ie, *ies;
4419 int freqs[2] = { 0, 0 };
4421 os_memset(¶ms, 0, sizeof(params));
4423 /* P2P Wildcard SSID */
4424 params.num_ssids = 1;
4425 if (ssid && ssid_len) {
4426 params.ssids[0].ssid = ssid;
4427 params.ssids[0].ssid_len = ssid_len;
4428 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4429 wpa_s->p2p_join_ssid_len = ssid_len;
4431 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4432 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4433 wpa_s->p2p_join_ssid_len = 0;
4436 wpa_s->wps->dev.p2p = 1;
4437 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4438 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4440 if (wps_ie == NULL) {
4441 wpas_p2p_scan_res_join(wpa_s, NULL);
4445 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4446 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4448 wpabuf_free(wps_ie);
4449 wpas_p2p_scan_res_join(wpa_s, NULL);
4452 wpabuf_put_buf(ies, wps_ie);
4453 wpabuf_free(wps_ie);
4455 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4457 params.p2p_probe = 1;
4458 params.extra_ies = wpabuf_head(ies);
4459 params.extra_ies_len = wpabuf_len(ies);
4464 * If freq is not provided, check the operating freq of the GO
4465 * and use a single channel scan on if possible.
4467 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4468 wpa_s->pending_join_iface_addr);
4474 params.freqs = freqs;
4478 * Run a scan to update BSS table and start Provision Discovery once
4479 * the new scan results become available.
4481 ret = wpa_drv_scan(wpa_s, ¶ms);
4483 os_get_reltime(&wpa_s->scan_trigger_time);
4484 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4485 wpa_s->own_scan_requested = 1;
4491 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4493 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4494 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4495 wpas_p2p_check_join_scan_limit(wpa_s);
4500 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4502 struct wpa_supplicant *wpa_s = eloop_ctx;
4503 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4507 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4508 const u8 *dev_addr, enum p2p_wps_method wps_method,
4509 int auto_join, int op_freq,
4510 const u8 *ssid, size_t ssid_len)
4512 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4513 MACSTR " dev " MACSTR " op_freq=%d)%s",
4514 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4515 auto_join ? " (auto_join)" : "");
4516 if (ssid && ssid_len) {
4517 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4518 wpa_ssid_txt(ssid, ssid_len));
4521 wpa_s->p2p_auto_pd = 0;
4522 wpa_s->p2p_auto_join = !!auto_join;
4523 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4524 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4525 wpa_s->pending_join_wps_method = wps_method;
4527 /* Make sure we are not running find during connection establishment */
4528 wpas_p2p_stop_find(wpa_s);
4530 wpa_s->p2p_join_scan_count = 0;
4531 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4536 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4537 const u8 *ssid, size_t ssid_len)
4539 struct wpa_supplicant *group;
4540 struct p2p_go_neg_results res;
4541 struct wpa_bss *bss;
4543 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4546 if (group != wpa_s) {
4547 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4548 sizeof(group->p2p_pin));
4549 group->p2p_wps_method = wpa_s->p2p_wps_method;
4552 * Need to mark the current interface for p2p_group_formation
4553 * when a separate group interface is not used. This is needed
4554 * to allow p2p_cancel stop a pending p2p_connect-join.
4555 * wpas_p2p_init_group_interface() addresses this for the case
4556 * where a separate group interface is used.
4558 wpa_s->global->p2p_group_formation = wpa_s;
4561 group->p2p_in_provisioning = 1;
4562 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4564 os_memset(&res, 0, sizeof(res));
4565 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4566 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4568 res.wps_method = wpa_s->pending_join_wps_method;
4569 if (freq && ssid && ssid_len) {
4571 res.ssid_len = ssid_len;
4572 os_memcpy(res.ssid, ssid, ssid_len);
4574 bss = wpa_bss_get_bssid_latest(wpa_s,
4575 wpa_s->pending_join_iface_addr);
4577 res.freq = bss->freq;
4578 res.ssid_len = bss->ssid_len;
4579 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4580 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4582 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4586 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4587 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4589 wpa_drv_cancel_remain_on_channel(wpa_s);
4590 wpa_s->off_channel_freq = 0;
4591 wpa_s->roc_waiting_drv_freq = 0;
4593 wpas_start_wps_enrollee(group, &res);
4596 * Allow a longer timeout for join-a-running-group than normal 15
4597 * second group formation timeout since the GO may not have authorized
4598 * our connection yet.
4600 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4601 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4608 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4609 int *force_freq, int *pref_freq, int go)
4611 struct wpa_used_freq_data *freqs;
4612 int res, best_freq, num_unused;
4613 unsigned int freq_in_use = 0, num, i;
4615 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4616 sizeof(struct wpa_used_freq_data));
4620 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4621 wpa_s->num_multichan_concurrent);
4624 * It is possible that the total number of used frequencies is bigger
4625 * than the number of frequencies used for P2P, so get the system wide
4626 * number of unused frequencies.
4628 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4630 wpa_printf(MSG_DEBUG,
4631 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4632 freq, wpa_s->num_multichan_concurrent, num, num_unused);
4637 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4639 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4641 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4642 "(%u MHz) is not supported for P2P uses",
4648 for (i = 0; i < num; i++) {
4649 if (freqs[i].freq == freq)
4653 if (num_unused <= 0 && !freq_in_use) {
4654 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4659 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4660 "requested channel (%u MHz)", freq);
4665 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
4667 /* We have a candidate frequency to use */
4668 if (best_freq > 0) {
4669 if (*pref_freq == 0 && num_unused > 0) {
4670 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4672 *pref_freq = best_freq;
4674 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4676 *force_freq = best_freq;
4678 } else if (num_unused > 0) {
4679 wpa_printf(MSG_DEBUG,
4680 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4683 wpa_printf(MSG_DEBUG,
4684 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4698 * wpas_p2p_connect - Request P2P Group Formation to be started
4699 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4700 * @peer_addr: Address of the peer P2P Device
4701 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4702 * @persistent_group: Whether to create a persistent group
4703 * @auto_join: Whether to select join vs. GO Negotiation automatically
4704 * @join: Whether to join an existing group (as a client) instead of starting
4705 * Group Owner negotiation; @peer_addr is BSSID in that case
4706 * @auth: Whether to only authorize the connection instead of doing that and
4707 * initiating Group Owner negotiation
4708 * @go_intent: GO Intent or -1 to use default
4709 * @freq: Frequency for the group or 0 for auto-selection
4710 * @persistent_id: Persistent group credentials to use for forcing GO
4711 * parameters or -1 to generate new values (SSID/passphrase)
4712 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4713 * interoperability workaround when initiating group formation
4714 * @ht40: Start GO with 40 MHz channel width
4715 * @vht: Start GO with VHT support
4716 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4717 * failure, -2 on failure due to channel not currently available,
4718 * -3 if forced channel is not supported
4720 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4721 const char *pin, enum p2p_wps_method wps_method,
4722 int persistent_group, int auto_join, int join, int auth,
4723 int go_intent, int freq, int persistent_id, int pd,
4726 int force_freq = 0, pref_freq = 0;
4728 enum wpa_driver_if_type iftype;
4730 struct wpa_ssid *ssid = NULL;
4732 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4735 if (persistent_id >= 0) {
4736 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4737 if (ssid == NULL || ssid->disabled != 2 ||
4738 ssid->mode != WPAS_MODE_P2P_GO)
4742 os_free(wpa_s->global->add_psk);
4743 wpa_s->global->add_psk = NULL;
4745 wpa_s->global->p2p_fail_on_wps_complete = 0;
4748 go_intent = wpa_s->conf->p2p_go_intent;
4751 wpa_s->p2p_long_listen = 0;
4753 wpa_s->p2p_wps_method = wps_method;
4754 wpa_s->p2p_persistent_group = !!persistent_group;
4755 wpa_s->p2p_persistent_id = persistent_id;
4756 wpa_s->p2p_go_intent = go_intent;
4757 wpa_s->p2p_connect_freq = freq;
4758 wpa_s->p2p_fallback_to_go_neg = 0;
4759 wpa_s->p2p_pd_before_go_neg = !!pd;
4760 wpa_s->p2p_go_ht40 = !!ht40;
4761 wpa_s->p2p_go_vht = !!vht;
4764 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4765 else if (wps_method == WPS_PIN_DISPLAY) {
4766 ret = wps_generate_pin();
4767 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4769 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4772 wpa_s->p2p_pin[0] = '\0';
4774 if (join || auto_join) {
4775 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4777 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4778 "connect a running group from " MACSTR,
4779 MAC2STR(peer_addr));
4780 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4783 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4784 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4786 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4787 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4791 os_get_reltime(&wpa_s->p2p_auto_started);
4792 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4794 wpa_s->p2p_auto_started.sec,
4795 wpa_s->p2p_auto_started.usec);
4797 wpa_s->user_initiated_pd = 1;
4798 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4799 auto_join, freq, NULL, 0) < 0)
4804 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4808 wpas_p2p_set_own_freq_preference(wpa_s,
4809 force_freq ? force_freq : pref_freq);
4811 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4813 if (wpa_s->create_p2p_iface) {
4814 /* Prepare to add a new interface for the group */
4815 iftype = WPA_IF_P2P_GROUP;
4816 if (go_intent == 15)
4817 iftype = WPA_IF_P2P_GO;
4818 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4819 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4820 "interface for the group");
4824 if_addr = wpa_s->pending_interface_addr;
4826 if_addr = wpa_s->own_addr;
4829 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
4831 force_freq, persistent_group, ssid,
4837 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4838 go_intent, if_addr, force_freq,
4839 persistent_group, ssid, pref_freq) < 0) {
4840 if (wpa_s->create_p2p_iface)
4841 wpas_p2p_remove_pending_group_interface(wpa_s);
4849 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4850 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4851 * @freq: Frequency of the channel in MHz
4852 * @duration: Duration of the stay on the channel in milliseconds
4854 * This callback is called when the driver indicates that it has started the
4855 * requested remain-on-channel duration.
4857 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4858 unsigned int freq, unsigned int duration)
4860 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4862 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4863 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4864 wpa_s->pending_listen_duration);
4865 wpa_s->pending_listen_freq = 0;
4867 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
4868 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4874 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4875 unsigned int timeout)
4877 /* Limit maximum Listen state time based on driver limitation. */
4878 if (timeout > wpa_s->max_remain_on_chan)
4879 timeout = wpa_s->max_remain_on_chan;
4881 return p2p_listen(wpa_s->global->p2p, timeout);
4886 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4887 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4888 * @freq: Frequency of the channel in MHz
4890 * This callback is called when the driver indicates that a remain-on-channel
4891 * operation has been completed, i.e., the duration on the requested channel
4894 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4897 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4898 "(p2p_long_listen=%d ms pending_action_tx=%p)",
4899 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
4900 wpas_p2p_listen_work_done(wpa_s);
4901 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4903 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4904 return; /* P2P module started a new operation */
4905 if (offchannel_pending_action_tx(wpa_s))
4907 if (wpa_s->p2p_long_listen > 0)
4908 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4909 if (wpa_s->p2p_long_listen > 0) {
4910 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4911 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4914 * When listen duration is over, stop listen & update p2p_state
4917 p2p_stop_listen(wpa_s->global->p2p);
4923 * wpas_p2p_group_remove - Remove a P2P group
4924 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4925 * @ifname: Network interface name of the group interface or "*" to remove all
4927 * Returns: 0 on success, -1 on failure
4929 * This function is used to remove a P2P group. This can be used to disconnect
4930 * from a group in which the local end is a P2P Client or to end a P2P Group in
4931 * case the local end is the Group Owner. If a virtual network interface was
4932 * created for this group, that interface will be removed. Otherwise, only the
4933 * configured P2P group network will be removed from the interface.
4935 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4937 struct wpa_global *global = wpa_s->global;
4939 if (os_strcmp(ifname, "*") == 0) {
4940 struct wpa_supplicant *prev;
4941 wpa_s = global->ifaces;
4944 wpa_s = wpa_s->next;
4945 if (prev->p2p_group_interface !=
4946 NOT_P2P_GROUP_INTERFACE ||
4947 (prev->current_ssid &&
4948 prev->current_ssid->p2p_group))
4949 wpas_p2p_disconnect(prev);
4954 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4955 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4959 return wpas_p2p_disconnect(wpa_s);
4963 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4968 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4970 if (wpa_s->best_24_freq > 0 &&
4971 p2p_supported_freq_go(wpa_s->global->p2p,
4972 wpa_s->best_24_freq)) {
4973 freq = wpa_s->best_24_freq;
4974 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4975 "channel: %d MHz", freq);
4977 os_get_random((u8 *) &r, sizeof(r));
4978 freq = 2412 + (r % 3) * 25;
4979 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4980 "channel: %d MHz", freq);
4985 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4987 if (wpa_s->best_5_freq > 0 &&
4988 p2p_supported_freq_go(wpa_s->global->p2p,
4989 wpa_s->best_5_freq)) {
4990 freq = wpa_s->best_5_freq;
4991 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4992 "channel: %d MHz", freq);
4994 os_get_random((u8 *) &r, sizeof(r));
4995 freq = 5180 + (r % 4) * 20;
4996 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4997 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4998 "5 GHz channel for P2P group");
5001 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5002 "channel: %d MHz", freq);
5006 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5007 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5008 "(%u MHz) is not supported for P2P uses",
5017 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5018 struct p2p_go_neg_results *params,
5019 int freq, int ht40, int vht,
5020 const struct p2p_channels *channels)
5022 struct wpa_used_freq_data *freqs;
5023 unsigned int pref_freq, cand_freq;
5024 unsigned int num, i;
5026 os_memset(params, 0, sizeof(*params));
5027 params->role_go = 1;
5028 params->ht40 = ht40;
5031 if (!freq_included(channels, freq)) {
5032 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5036 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5037 "frequency %d MHz", freq);
5038 params->freq = freq;
5039 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5040 wpa_s->conf->p2p_oper_channel >= 1 &&
5041 wpa_s->conf->p2p_oper_channel <= 11 &&
5042 freq_included(channels,
5043 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5044 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5045 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5046 "frequency %d MHz", params->freq);
5047 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5048 wpa_s->conf->p2p_oper_reg_class == 116 ||
5049 wpa_s->conf->p2p_oper_reg_class == 117 ||
5050 wpa_s->conf->p2p_oper_reg_class == 124 ||
5051 wpa_s->conf->p2p_oper_reg_class == 126 ||
5052 wpa_s->conf->p2p_oper_reg_class == 127) &&
5053 freq_included(channels,
5054 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
5055 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5056 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5057 "frequency %d MHz", params->freq);
5058 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5059 wpa_s->best_overall_freq > 0 &&
5060 p2p_supported_freq_go(wpa_s->global->p2p,
5061 wpa_s->best_overall_freq) &&
5062 freq_included(channels, wpa_s->best_overall_freq)) {
5063 params->freq = wpa_s->best_overall_freq;
5064 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5065 "channel %d MHz", params->freq);
5066 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5067 wpa_s->best_24_freq > 0 &&
5068 p2p_supported_freq_go(wpa_s->global->p2p,
5069 wpa_s->best_24_freq) &&
5070 freq_included(channels, wpa_s->best_24_freq)) {
5071 params->freq = wpa_s->best_24_freq;
5072 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5073 "channel %d MHz", params->freq);
5074 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5075 wpa_s->best_5_freq > 0 &&
5076 p2p_supported_freq_go(wpa_s->global->p2p,
5077 wpa_s->best_5_freq) &&
5078 freq_included(channels, wpa_s->best_5_freq)) {
5079 params->freq = wpa_s->best_5_freq;
5080 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5081 "channel %d MHz", params->freq);
5082 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5084 params->freq = pref_freq;
5085 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5086 "channels", params->freq);
5089 for (chan = 0; chan < 11; chan++) {
5090 params->freq = 2412 + chan * 5;
5091 if (!wpas_p2p_disallowed_freq(wpa_s->global,
5093 freq_included(channels, params->freq))
5097 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
5101 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
5102 "known)", params->freq);
5105 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5106 sizeof(struct wpa_used_freq_data));
5110 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5111 wpa_s->num_multichan_concurrent);
5113 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5115 /* First try the best used frequency if possible */
5116 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5117 params->freq = cand_freq;
5119 /* Try any of the used frequencies */
5120 for (i = 0; i < num; i++) {
5121 if (freq_included(channels, freqs[i].freq)) {
5122 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5124 params->freq = freqs[i].freq;
5130 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5131 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5135 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5139 for (i = 0; i < num; i++) {
5140 if (freqs[i].freq == freq)
5145 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5147 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5151 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5161 static struct wpa_supplicant *
5162 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5165 struct wpa_supplicant *group_wpa_s;
5167 if (!wpas_p2p_create_iface(wpa_s)) {
5168 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5170 wpa_s->p2p_first_connection_timeout = 0;
5174 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5175 WPA_IF_P2P_CLIENT) < 0) {
5176 wpa_msg_global(wpa_s, MSG_ERROR,
5177 "P2P: Failed to add group interface");
5180 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5181 if (group_wpa_s == NULL) {
5182 wpa_msg_global(wpa_s, MSG_ERROR,
5183 "P2P: Failed to initialize group interface");
5184 wpas_p2p_remove_pending_group_interface(wpa_s);
5188 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5189 group_wpa_s->ifname);
5190 group_wpa_s->p2p_first_connection_timeout = 0;
5196 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5197 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5198 * @persistent_group: Whether to create a persistent group
5199 * @freq: Frequency for the group or 0 to indicate no hardcoding
5200 * @ht40: Start GO with 40 MHz channel width
5201 * @vht: Start GO with VHT support
5202 * Returns: 0 on success, -1 on failure
5204 * This function creates a new P2P group with the local end as the Group Owner,
5205 * i.e., without using Group Owner Negotiation.
5207 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5208 int freq, int ht40, int vht)
5210 struct p2p_go_neg_results params;
5212 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5215 os_free(wpa_s->global->add_psk);
5216 wpa_s->global->add_psk = NULL;
5218 /* Make sure we are not running find during connection establishment */
5219 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5220 wpas_p2p_stop_find_oper(wpa_s);
5222 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5226 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5229 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5230 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5231 "(%u MHz) is not supported for P2P uses",
5235 p2p_go_params(wpa_s->global->p2p, ¶ms);
5236 params.persistent_group = persistent_group;
5238 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5241 wpas_start_wps_go(wpa_s, ¶ms, 0);
5247 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5248 struct wpa_ssid *params, int addr_allocated,
5251 struct wpa_ssid *ssid;
5253 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5256 wpa_s->p2p_last_4way_hs_fail = NULL;
5258 wpa_supplicant_ap_deinit(wpa_s);
5260 ssid = wpa_config_add_network(wpa_s->conf);
5263 wpa_config_set_network_defaults(ssid);
5264 ssid->temporary = 1;
5265 ssid->proto = WPA_PROTO_RSN;
5266 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5267 ssid->group_cipher = WPA_CIPHER_CCMP;
5268 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5269 ssid->ssid = os_malloc(params->ssid_len);
5270 if (ssid->ssid == NULL) {
5271 wpa_config_remove_network(wpa_s->conf, ssid->id);
5274 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5275 ssid->ssid_len = params->ssid_len;
5276 ssid->p2p_group = 1;
5277 ssid->export_keys = 1;
5278 if (params->psk_set) {
5279 os_memcpy(ssid->psk, params->psk, 32);
5282 if (params->passphrase)
5283 ssid->passphrase = os_strdup(params->passphrase);
5285 wpa_s->show_group_started = 1;
5286 wpa_s->p2p_in_invitation = 1;
5287 wpa_s->p2p_invite_go_freq = freq;
5289 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5291 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5292 wpas_p2p_group_formation_timeout,
5293 wpa_s->parent, NULL);
5294 wpa_supplicant_select_network(wpa_s, ssid);
5300 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5301 struct wpa_ssid *ssid, int addr_allocated,
5302 int force_freq, int neg_freq, int ht40,
5303 int vht, const struct p2p_channels *channels,
5304 int connection_timeout)
5306 struct p2p_go_neg_results params;
5309 if (ssid->disabled != 2 || ssid->ssid == NULL)
5312 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5313 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5314 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5319 os_free(wpa_s->global->add_psk);
5320 wpa_s->global->add_psk = NULL;
5322 /* Make sure we are not running find during connection establishment */
5323 wpas_p2p_stop_find_oper(wpa_s);
5325 wpa_s->p2p_fallback_to_go_neg = 0;
5327 if (force_freq > 0) {
5328 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5332 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5333 if (freq < 0 || (freq > 0 && !freq_included(channels, freq)))
5337 if (ssid->mode == WPAS_MODE_INFRA)
5338 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5340 if (ssid->mode != WPAS_MODE_P2P_GO)
5343 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
5347 params.psk_set = ssid->psk_set;
5349 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5350 if (ssid->passphrase) {
5351 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5352 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5353 "persistent group");
5356 os_strlcpy(params.passphrase, ssid->passphrase,
5357 sizeof(params.passphrase));
5359 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5360 params.ssid_len = ssid->ssid_len;
5361 params.persistent_group = 1;
5363 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5367 wpa_s->p2p_first_connection_timeout = connection_timeout;
5368 wpas_start_wps_go(wpa_s, ¶ms, 0);
5374 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5375 struct wpabuf *proberesp_ies)
5377 struct wpa_supplicant *wpa_s = ctx;
5378 if (wpa_s->ap_iface) {
5379 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5380 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5381 wpabuf_free(beacon_ies);
5382 wpabuf_free(proberesp_ies);
5386 wpabuf_free(hapd->p2p_beacon_ie);
5387 hapd->p2p_beacon_ie = beacon_ies;
5389 wpabuf_free(hapd->p2p_probe_resp_ie);
5390 hapd->p2p_probe_resp_ie = proberesp_ies;
5392 wpabuf_free(beacon_ies);
5393 wpabuf_free(proberesp_ies);
5395 wpa_supplicant_ap_update_beacon(wpa_s);
5399 static void wpas_p2p_idle_update(void *ctx, int idle)
5401 struct wpa_supplicant *wpa_s = ctx;
5402 if (!wpa_s->ap_iface)
5404 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
5406 if (wpa_s->global->p2p_fail_on_wps_complete &&
5407 wpa_s->p2p_in_provisioning) {
5408 wpas_p2p_grpform_fail_after_wps(wpa_s);
5411 wpas_p2p_set_group_idle_timeout(wpa_s);
5413 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5417 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
5418 struct wpa_ssid *ssid)
5420 struct p2p_group *group;
5421 struct p2p_group_config *cfg;
5423 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5426 cfg = os_zalloc(sizeof(*cfg));
5430 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
5431 cfg->persistent_group = 2;
5432 else if (ssid->p2p_persistent_group)
5433 cfg->persistent_group = 1;
5434 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5435 if (wpa_s->max_stations &&
5436 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5437 cfg->max_clients = wpa_s->max_stations;
5439 cfg->max_clients = wpa_s->conf->max_num_sta;
5440 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5441 cfg->ssid_len = ssid->ssid_len;
5442 cfg->freq = ssid->frequency;
5443 cfg->cb_ctx = wpa_s;
5444 cfg->ie_update = wpas_p2p_ie_update;
5445 cfg->idle_update = wpas_p2p_idle_update;
5447 group = p2p_group_init(wpa_s->global->p2p, cfg);
5450 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
5451 p2p_group_notif_formation_done(group);
5452 wpa_s->p2p_group = group;
5457 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5460 struct wpa_ssid *ssid = wpa_s->current_ssid;
5462 if (!wpa_s->p2p_in_provisioning) {
5463 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5464 "provisioning not in progress");
5468 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5469 u8 go_dev_addr[ETH_ALEN];
5470 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5471 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5473 /* Clear any stored provisioning info */
5474 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5477 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5479 wpa_s->p2p_go_group_formation_completed = 1;
5480 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5482 * Use a separate timeout for initial data connection to
5483 * complete to allow the group to be removed automatically if
5484 * something goes wrong in this step before the P2P group idle
5485 * timeout mechanism is taken into use.
5487 wpa_dbg(wpa_s, MSG_DEBUG,
5488 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5489 P2P_MAX_INITIAL_CONN_WAIT);
5490 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5491 wpas_p2p_group_formation_timeout,
5492 wpa_s->parent, NULL);
5495 * Use a separate timeout for initial data connection to
5496 * complete to allow the group to be removed automatically if
5497 * the client does not complete data connection successfully.
5499 wpa_dbg(wpa_s, MSG_DEBUG,
5500 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5501 P2P_MAX_INITIAL_CONN_WAIT_GO);
5502 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5503 wpas_p2p_group_formation_timeout,
5504 wpa_s->parent, NULL);
5506 * Complete group formation on first successful data connection
5508 wpa_s->p2p_go_group_formation_completed = 0;
5510 if (wpa_s->global->p2p)
5511 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5512 wpas_group_formation_completed(wpa_s, 1);
5516 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5517 struct wps_event_fail *fail)
5519 if (!wpa_s->p2p_in_provisioning) {
5520 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5521 "provisioning not in progress");
5525 if (wpa_s->go_params) {
5526 p2p_clear_provisioning_info(
5528 wpa_s->go_params->peer_device_addr);
5531 wpas_notify_p2p_wps_failed(wpa_s, fail);
5533 if (wpa_s == wpa_s->global->p2p_group_formation) {
5535 * Allow some time for the failed WPS negotiation exchange to
5536 * complete, but remove the group since group formation cannot
5537 * succeed after provisioning failure.
5539 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5540 wpa_s->global->p2p_fail_on_wps_complete = 1;
5541 eloop_deplete_timeout(0, 50000,
5542 wpas_p2p_group_formation_timeout,
5543 wpa_s->parent, NULL);
5548 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5550 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5551 !wpa_s->p2p_in_provisioning)
5554 wpas_p2p_grpform_fail_after_wps(wpa_s);
5560 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5561 const char *config_method,
5562 enum wpas_p2p_prov_disc_use use)
5566 wpa_s->p2p_fallback_to_go_neg = 0;
5567 wpa_s->pending_pd_use = NORMAL_PD;
5568 if (os_strncmp(config_method, "display", 7) == 0)
5569 config_methods = WPS_CONFIG_DISPLAY;
5570 else if (os_strncmp(config_method, "keypad", 6) == 0)
5571 config_methods = WPS_CONFIG_KEYPAD;
5572 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5573 os_strncmp(config_method, "pushbutton", 10) == 0)
5574 config_methods = WPS_CONFIG_PUSHBUTTON;
5576 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5580 if (use == WPAS_P2P_PD_AUTO) {
5581 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5582 wpa_s->pending_pd_config_methods = config_methods;
5583 wpa_s->p2p_auto_pd = 1;
5584 wpa_s->p2p_auto_join = 0;
5585 wpa_s->pending_pd_before_join = 0;
5586 wpa_s->auto_pd_scan_retry = 0;
5587 wpas_p2p_stop_find(wpa_s);
5588 wpa_s->p2p_join_scan_count = 0;
5589 os_get_reltime(&wpa_s->p2p_auto_started);
5590 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5591 wpa_s->p2p_auto_started.sec,
5592 wpa_s->p2p_auto_started.usec);
5593 wpas_p2p_join_scan(wpa_s, NULL);
5597 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5600 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
5601 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5606 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5609 return p2p_scan_result_text(ies, ies_len, buf, end);
5613 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5615 if (!offchannel_pending_action_tx(wpa_s))
5618 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5619 "operation request");
5620 offchannel_clear_pending_action_tx(wpa_s);
5624 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5625 enum p2p_discovery_type type,
5626 unsigned int num_req_dev_types, const u8 *req_dev_types,
5627 const u8 *dev_id, unsigned int search_delay)
5629 wpas_p2p_clear_pending_action_tx(wpa_s);
5630 wpa_s->p2p_long_listen = 0;
5632 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5633 wpa_s->p2p_in_provisioning)
5636 wpa_supplicant_cancel_sched_scan(wpa_s);
5638 return p2p_find(wpa_s->global->p2p, timeout, type,
5639 num_req_dev_types, req_dev_types, dev_id,
5644 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
5646 wpas_p2p_clear_pending_action_tx(wpa_s);
5647 wpa_s->p2p_long_listen = 0;
5648 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5649 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5651 if (wpa_s->global->p2p)
5652 p2p_stop_find(wpa_s->global->p2p);
5658 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5660 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5662 wpas_p2p_remove_pending_group_interface(wpa_s);
5666 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5668 struct wpa_supplicant *wpa_s = eloop_ctx;
5669 wpa_s->p2p_long_listen = 0;
5673 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5677 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5680 wpa_supplicant_cancel_sched_scan(wpa_s);
5681 wpas_p2p_clear_pending_action_tx(wpa_s);
5685 * This is a request for unlimited Listen state. However, at
5686 * least for now, this is mapped to a Listen state for one
5691 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5692 wpa_s->p2p_long_listen = 0;
5695 * Stop previous find/listen operation to avoid trying to request a new
5696 * remain-on-channel operation while the driver is still running the
5699 if (wpa_s->global->p2p)
5700 p2p_stop_find(wpa_s->global->p2p);
5702 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5703 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5704 wpa_s->p2p_long_listen = timeout * 1000;
5705 eloop_register_timeout(timeout, 0,
5706 wpas_p2p_long_listen_timeout,
5714 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5715 u8 *buf, size_t len, int p2p_group)
5717 struct wpabuf *p2p_ie;
5720 if (wpa_s->global->p2p_disabled)
5722 if (wpa_s->global->p2p == NULL)
5727 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5728 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5730 wpabuf_free(p2p_ie);
5736 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
5737 const u8 *dst, const u8 *bssid,
5738 const u8 *ie, size_t ie_len, int ssi_signal)
5740 if (wpa_s->global->p2p_disabled)
5742 if (wpa_s->global->p2p == NULL)
5745 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5747 case P2P_PREQ_NOT_P2P:
5748 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5751 case P2P_PREQ_MALFORMED:
5752 case P2P_PREQ_NOT_LISTEN:
5753 case P2P_PREQ_NOT_PROCESSED:
5754 default: /* make gcc happy */
5756 case P2P_PREQ_PROCESSED:
5762 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5763 const u8 *sa, const u8 *bssid,
5764 u8 category, const u8 *data, size_t len, int freq)
5766 if (wpa_s->global->p2p_disabled)
5768 if (wpa_s->global->p2p == NULL)
5771 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5776 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5778 if (wpa_s->global->p2p_disabled)
5780 if (wpa_s->global->p2p == NULL)
5783 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5787 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5789 p2p_group_deinit(wpa_s->p2p_group);
5790 wpa_s->p2p_group = NULL;
5792 wpa_s->ap_configured_cb = NULL;
5793 wpa_s->ap_configured_cb_ctx = NULL;
5794 wpa_s->ap_configured_cb_data = NULL;
5795 wpa_s->connect_without_scan = NULL;
5799 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5801 wpa_s->p2p_long_listen = 0;
5803 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5806 return p2p_reject(wpa_s->global->p2p, addr);
5810 /* Invite to reinvoke a persistent group */
5811 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5812 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
5813 int ht40, int vht, int pref_freq)
5815 enum p2p_invite_role role;
5819 int no_pref_freq_given = pref_freq == 0;
5821 wpa_s->global->p2p_invite_group = NULL;
5823 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5825 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5827 wpa_s->p2p_persistent_go_freq = freq;
5828 wpa_s->p2p_go_ht40 = !!ht40;
5829 if (ssid->mode == WPAS_MODE_P2P_GO) {
5830 role = P2P_INVITE_ROLE_GO;
5831 if (peer_addr == NULL) {
5832 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5833 "address in invitation command");
5836 if (wpas_p2p_create_iface(wpa_s)) {
5837 if (wpas_p2p_add_group_interface(wpa_s,
5838 WPA_IF_P2P_GO) < 0) {
5839 wpa_printf(MSG_ERROR, "P2P: Failed to "
5840 "allocate a new interface for the "
5844 bssid = wpa_s->pending_interface_addr;
5846 bssid = wpa_s->own_addr;
5848 role = P2P_INVITE_ROLE_CLIENT;
5849 peer_addr = ssid->bssid;
5851 wpa_s->pending_invite_ssid_id = ssid->id;
5853 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5854 role == P2P_INVITE_ROLE_GO);
5858 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5861 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5862 no_pref_freq_given && pref_freq > 0 &&
5863 wpa_s->num_multichan_concurrent > 1 &&
5864 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5865 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5870 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5871 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5876 /* Invite to join an active group */
5877 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5878 const u8 *peer_addr, const u8 *go_dev_addr)
5880 struct wpa_global *global = wpa_s->global;
5881 enum p2p_invite_role role;
5883 struct wpa_ssid *ssid;
5885 int freq = 0, force_freq = 0, pref_freq = 0;
5888 wpa_s->p2p_persistent_go_freq = 0;
5889 wpa_s->p2p_go_ht40 = 0;
5890 wpa_s->p2p_go_vht = 0;
5892 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5893 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5896 if (wpa_s == NULL) {
5897 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5901 ssid = wpa_s->current_ssid;
5903 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5908 wpa_s->global->p2p_invite_group = wpa_s;
5909 persistent = ssid->p2p_persistent_group &&
5910 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5911 ssid->ssid, ssid->ssid_len);
5913 if (ssid->mode == WPAS_MODE_P2P_GO) {
5914 role = P2P_INVITE_ROLE_ACTIVE_GO;
5915 bssid = wpa_s->own_addr;
5916 if (go_dev_addr == NULL)
5917 go_dev_addr = wpa_s->global->p2p_dev_addr;
5918 freq = ssid->frequency;
5920 role = P2P_INVITE_ROLE_CLIENT;
5921 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5922 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5923 "invite to current group");
5926 bssid = wpa_s->bssid;
5927 if (go_dev_addr == NULL &&
5928 !is_zero_ether_addr(wpa_s->go_dev_addr))
5929 go_dev_addr = wpa_s->go_dev_addr;
5930 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5931 (int) wpa_s->assoc_freq;
5933 wpa_s->parent->pending_invite_ssid_id = -1;
5935 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5938 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5939 role == P2P_INVITE_ROLE_ACTIVE_GO);
5942 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
5944 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5945 ssid->ssid, ssid->ssid_len, force_freq,
5946 go_dev_addr, persistent, pref_freq, -1);
5950 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5952 struct wpa_ssid *ssid = wpa_s->current_ssid;
5953 const char *ssid_txt;
5954 u8 go_dev_addr[ETH_ALEN];
5955 int network_id = -1;
5961 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5962 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5963 wpa_s->parent, NULL);
5966 if (!wpa_s->show_group_started || !ssid)
5969 wpa_s->show_group_started = 0;
5971 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5972 os_memset(go_dev_addr, 0, ETH_ALEN);
5973 if (ssid->bssid_set)
5974 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5975 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5977 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5979 if (wpa_s->global->p2p_group_formation == wpa_s)
5980 wpa_s->global->p2p_group_formation = NULL;
5982 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5983 (int) wpa_s->assoc_freq;
5986 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
5987 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
5988 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
5989 ip[0], ip[1], ip[2], ip[3],
5990 ip[4], ip[5], ip[6], ip[7],
5991 ip[8], ip[9], ip[10], ip[11]);
5994 if (ssid->passphrase == NULL && ssid->psk_set) {
5996 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5997 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5998 "%s client ssid=\"%s\" freq=%d psk=%s "
5999 "go_dev_addr=" MACSTR "%s%s",
6000 wpa_s->ifname, ssid_txt, freq, psk,
6001 MAC2STR(go_dev_addr),
6002 persistent ? " [PERSISTENT]" : "", ip_addr);
6004 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
6005 "%s client ssid=\"%s\" freq=%d "
6006 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s%s",
6007 wpa_s->ifname, ssid_txt, freq,
6008 ssid->passphrase ? ssid->passphrase : "",
6009 MAC2STR(go_dev_addr),
6010 persistent ? " [PERSISTENT]" : "", ip_addr);
6014 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6017 network_id = ssid->id;
6018 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6022 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6023 u32 interval1, u32 duration2, u32 interval2)
6027 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6030 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6031 wpa_s->current_ssid == NULL ||
6032 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6035 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6036 wpa_s->own_addr, wpa_s->assoc_freq,
6037 duration1, interval1, duration2, interval2);
6039 wpa_s->waiting_presence_resp = 1;
6045 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6046 unsigned int interval)
6048 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6051 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6055 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6057 if (wpa_s->current_ssid == NULL) {
6059 * current_ssid can be cleared when P2P client interface gets
6060 * disconnected, so assume this interface was used as P2P
6065 return wpa_s->current_ssid->p2p_group &&
6066 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6070 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6072 struct wpa_supplicant *wpa_s = eloop_ctx;
6074 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6075 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6080 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6082 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6086 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6090 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6091 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6093 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6096 timeout = wpa_s->conf->p2p_group_idle;
6097 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6098 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6099 timeout = P2P_MAX_CLIENT_IDLE;
6105 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6106 timeout = 0; /* special client mode no-timeout */
6111 if (wpa_s->p2p_in_provisioning) {
6113 * Use the normal group formation timeout during the
6114 * provisioning phase to avoid terminating this process too
6115 * early due to group idle timeout.
6117 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6118 "during provisioning");
6122 if (wpa_s->show_group_started) {
6124 * Use the normal group formation timeout between the end of
6125 * the provisioning phase and completion of 4-way handshake to
6126 * avoid terminating this process too early due to group idle
6129 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6130 "while waiting for initial 4-way handshake to "
6135 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6137 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6142 /* Returns 1 if the interface was removed */
6143 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6144 u16 reason_code, const u8 *ie, size_t ie_len,
6145 int locally_generated)
6147 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6150 if (!locally_generated)
6151 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6154 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6155 wpa_s->current_ssid &&
6156 wpa_s->current_ssid->p2p_group &&
6157 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6158 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6159 "session is ending");
6160 if (wpas_p2p_group_delete(wpa_s,
6161 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6170 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6171 u16 reason_code, const u8 *ie, size_t ie_len,
6172 int locally_generated)
6174 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6177 if (!locally_generated)
6178 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6183 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6185 struct p2p_data *p2p = wpa_s->global->p2p;
6190 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6193 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6194 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6196 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6197 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6200 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6201 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6203 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6204 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6206 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6207 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6208 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6209 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6210 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6213 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6214 p2p_set_sec_dev_types(p2p,
6215 (void *) wpa_s->conf->sec_device_type,
6216 wpa_s->conf->num_sec_device_types);
6218 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6220 p2p_remove_wps_vendor_extensions(p2p);
6221 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6222 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6224 p2p_add_wps_vendor_extension(
6225 p2p, wpa_s->conf->wps_vendor_ext[i]);
6229 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6230 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6232 country[0] = wpa_s->conf->country[0];
6233 country[1] = wpa_s->conf->country[1];
6235 p2p_set_country(p2p, country);
6238 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6239 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6240 wpa_s->conf->p2p_ssid_postfix ?
6241 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6245 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6246 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6248 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6249 u8 reg_class, channel;
6252 if (wpa_s->conf->p2p_listen_reg_class &&
6253 wpa_s->conf->p2p_listen_channel) {
6254 reg_class = wpa_s->conf->p2p_listen_reg_class;
6255 channel = wpa_s->conf->p2p_listen_channel;
6259 * Pick one of the social channels randomly as the
6262 os_get_random((u8 *) &r, sizeof(r));
6263 channel = 1 + (r % 3) * 5;
6265 ret = p2p_set_listen_channel(p2p, reg_class, channel);
6267 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6270 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6271 u8 op_reg_class, op_channel, cfg_op_channel;
6274 if (wpa_s->conf->p2p_oper_reg_class &&
6275 wpa_s->conf->p2p_oper_channel) {
6276 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6277 op_channel = wpa_s->conf->p2p_oper_channel;
6282 * Use random operation channel from (1, 6, 11)
6283 *if no other preference is indicated.
6285 os_get_random((u8 *) &r, sizeof(r));
6286 op_channel = 1 + (r % 3) * 5;
6289 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6292 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6296 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6297 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6298 wpa_s->conf->p2p_pref_chan) < 0) {
6299 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6303 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6304 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6311 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6314 if (!wpa_s->ap_iface)
6316 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6321 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6323 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6326 wpa_s->global->cross_connection = enabled;
6327 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6330 struct wpa_supplicant *iface;
6332 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6334 if (iface->cross_connect_enabled == 0)
6337 iface->cross_connect_enabled = 0;
6338 iface->cross_connect_in_use = 0;
6339 wpa_msg_global(iface->parent, MSG_INFO,
6340 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6342 iface->cross_connect_uplink);
6350 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6352 struct wpa_supplicant *iface;
6354 if (!uplink->global->cross_connection)
6357 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6358 if (!iface->cross_connect_enabled)
6360 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6363 if (iface->ap_iface == NULL)
6365 if (iface->cross_connect_in_use)
6368 iface->cross_connect_in_use = 1;
6369 wpa_msg_global(iface->parent, MSG_INFO,
6370 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6371 iface->ifname, iface->cross_connect_uplink);
6376 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6378 struct wpa_supplicant *iface;
6380 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6381 if (!iface->cross_connect_enabled)
6383 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6386 if (!iface->cross_connect_in_use)
6389 wpa_msg_global(iface->parent, MSG_INFO,
6390 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6391 iface->ifname, iface->cross_connect_uplink);
6392 iface->cross_connect_in_use = 0;
6397 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6399 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6400 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6401 wpa_s->cross_connect_disallowed)
6402 wpas_p2p_disable_cross_connect(wpa_s);
6404 wpas_p2p_enable_cross_connect(wpa_s);
6405 if (!wpa_s->ap_iface &&
6406 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6407 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6411 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6413 wpas_p2p_disable_cross_connect(wpa_s);
6414 if (!wpa_s->ap_iface &&
6415 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6417 wpas_p2p_set_group_idle_timeout(wpa_s);
6421 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6423 struct wpa_supplicant *iface;
6425 if (!wpa_s->global->cross_connection)
6428 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6431 if (iface->drv_flags &
6432 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6434 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6437 wpa_s->cross_connect_enabled = 1;
6438 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6439 sizeof(wpa_s->cross_connect_uplink));
6440 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6441 "%s to %s whenever uplink is available",
6442 wpa_s->ifname, wpa_s->cross_connect_uplink);
6444 if (iface->ap_iface || iface->current_ssid == NULL ||
6445 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6446 iface->cross_connect_disallowed ||
6447 iface->wpa_state != WPA_COMPLETED)
6450 wpa_s->cross_connect_in_use = 1;
6451 wpa_msg_global(wpa_s->parent, MSG_INFO,
6452 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6453 wpa_s->ifname, wpa_s->cross_connect_uplink);
6459 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6461 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6462 !wpa_s->p2p_in_provisioning)
6463 return 0; /* not P2P client operation */
6465 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6467 if (wpa_s != wpa_s->parent)
6468 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
6469 wpas_p2p_group_formation_failed(wpa_s);
6474 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6476 struct wpa_supplicant *wpa_s = eloop_ctx;
6477 wpas_p2p_notif_pbc_overlap(wpa_s);
6481 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6483 struct p2p_channels chan, cli_chan;
6484 struct wpa_supplicant *ifs;
6486 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6489 os_memset(&chan, 0, sizeof(chan));
6490 os_memset(&cli_chan, 0, sizeof(cli_chan));
6491 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
6492 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6497 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
6499 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6501 if (!ifs->current_ssid ||
6502 !ifs->current_ssid->p2p_group ||
6503 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6504 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6506 freq = ifs->current_ssid->frequency;
6507 if (freq_included(&chan, freq)) {
6508 wpa_dbg(ifs, MSG_DEBUG,
6509 "P2P GO operating frequency %d MHz in valid range",
6514 wpa_dbg(ifs, MSG_DEBUG,
6515 "P2P GO operating in invalid frequency %d MHz", freq);
6516 /* TODO: Consider using CSA or removing the group within
6518 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6523 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6524 struct wpa_scan_results *scan_res)
6526 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6530 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6532 struct wpa_global *global = wpa_s->global;
6536 if (global->p2p == NULL)
6539 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6541 if (wpa_s->pending_interface_name[0] &&
6542 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6545 peer = p2p_get_go_neg_peer(global->p2p);
6547 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6548 MACSTR, MAC2STR(peer));
6549 p2p_unauthorize(global->p2p, peer);
6553 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6554 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6555 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6559 if (wpa_s->pending_pd_before_join) {
6560 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6561 wpa_s->pending_pd_before_join = 0;
6565 wpas_p2p_stop_find(wpa_s);
6567 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6568 if (wpa_s == global->p2p_group_formation &&
6569 (wpa_s->p2p_in_provisioning ||
6570 wpa_s->parent->pending_interface_type ==
6571 WPA_IF_P2P_CLIENT)) {
6572 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6573 "formation found - cancelling",
6576 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6577 wpa_s->parent, NULL);
6578 if (wpa_s->p2p_in_provisioning) {
6579 wpas_group_formation_completed(wpa_s, 0);
6582 wpas_p2p_group_delete(wpa_s,
6583 P2P_GROUP_REMOVAL_REQUESTED);
6585 } else if (wpa_s->p2p_in_invitation) {
6586 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6589 wpas_p2p_group_formation_failed(wpa_s);
6594 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6602 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6604 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6607 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6608 "being available anymore");
6609 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
6613 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6614 int freq_24, int freq_5, int freq_overall)
6616 struct p2p_data *p2p = wpa_s->global->p2p;
6619 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6623 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6626 struct p2p_data *p2p = wpa_s->global->p2p;
6631 if (hwaddr_aton(addr, peer))
6634 return p2p_unauthorize(p2p, peer);
6639 * wpas_p2p_disconnect - Disconnect from a P2P Group
6640 * @wpa_s: Pointer to wpa_supplicant data
6641 * Returns: 0 on success, -1 on failure
6643 * This can be used to disconnect from a group in which the local end is a P2P
6644 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6645 * virtual network interface was created for this group, that interface will be
6646 * removed. Otherwise, only the configured P2P group network will be removed
6647 * from the interface.
6649 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6655 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6660 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6664 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6667 ret = p2p_in_progress(wpa_s->global->p2p);
6670 * Check whether there is an ongoing WPS provisioning step (or
6671 * other parts of group formation) on another interface since
6672 * p2p_in_progress() does not report this to avoid issues for
6673 * scans during such provisioning step.
6675 if (wpa_s->global->p2p_group_formation &&
6676 wpa_s->global->p2p_group_formation != wpa_s) {
6677 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6678 "in group formation",
6679 wpa_s->global->p2p_group_formation->ifname);
6684 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6685 struct os_reltime now;
6686 os_get_reltime(&now);
6687 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6688 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
6689 /* Wait for the first client has expired */
6690 wpa_s->global->p2p_go_wait_client.sec = 0;
6692 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6701 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6702 struct wpa_ssid *ssid)
6704 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6705 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6706 wpa_s->parent, NULL) > 0) {
6708 * Remove the network by scheduling the group formation
6709 * timeout to happen immediately. The teardown code
6710 * needs to be scheduled to run asynch later so that we
6711 * don't delete data from under ourselves unexpectedly.
6712 * Calling wpas_p2p_group_formation_timeout directly
6713 * causes a series of crashes in WPS failure scenarios.
6715 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6716 "P2P group network getting removed");
6717 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6718 wpa_s->parent, NULL);
6723 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
6724 const u8 *addr, const u8 *ssid,
6730 for (s = wpa_s->conf->ssid; s; s = s->next) {
6731 if (s->disabled != 2)
6734 (ssid_len != s->ssid_len ||
6735 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6738 if (s->mode == WPAS_MODE_P2P_GO)
6742 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6743 return s; /* peer is GO in the persistent group */
6744 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6746 for (i = 0; i < s->num_p2p_clients; i++) {
6747 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6748 addr, ETH_ALEN) == 0)
6749 return s; /* peer is P2P client in persistent
6758 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6761 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6762 wpa_s->parent, NULL) > 0) {
6764 * This can happen if WPS provisioning step is not terminated
6765 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6766 * peer was able to connect, there is no need to time out group
6767 * formation after this, though. In addition, this is used with
6768 * the initial connection wait on the GO as a separate formation
6769 * timeout and as such, expected to be hit after the initial WPS
6770 * provisioning step.
6772 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6774 if (!wpa_s->p2p_go_group_formation_completed) {
6775 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6776 wpa_s->p2p_go_group_formation_completed = 1;
6777 wpa_s->global->p2p_group_formation = NULL;
6778 wpa_s->p2p_in_provisioning = 0;
6779 wpa_s->p2p_in_invitation = 0;
6781 wpa_s->global->p2p_go_wait_client.sec = 0;
6784 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6788 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6791 struct wpa_supplicant *group = wpa_s;
6792 if (wpa_s->global->p2p_group_formation)
6793 group = wpa_s->global->p2p_group_formation;
6794 wpa_s = wpa_s->parent;
6795 offchannel_send_action_done(wpa_s);
6797 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
6798 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6799 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6800 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6801 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6802 wpa_s->p2p_persistent_id,
6803 wpa_s->p2p_pd_before_go_neg,
6809 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6811 if (!wpa_s->p2p_fallback_to_go_neg ||
6812 wpa_s->p2p_in_provisioning <= 5)
6815 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6816 return 0; /* peer operating as a GO */
6818 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6819 "fallback to GO Negotiation");
6820 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6826 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6828 struct wpa_supplicant *ifs;
6830 if (wpa_s->wpa_state > WPA_SCANNING) {
6831 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6832 "concurrent operation",
6833 wpa_s->conf->p2p_search_delay);
6834 return wpa_s->conf->p2p_search_delay;
6837 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6839 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
6840 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6841 "delay due to concurrent operation on "
6843 wpa_s->conf->p2p_search_delay,
6845 return wpa_s->conf->p2p_search_delay;
6853 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6854 struct wpa_ssid *s, const u8 *addr,
6857 struct psk_list_entry *psk, *tmp;
6860 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6862 if ((iface_addr && !psk->p2p &&
6863 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6864 (!iface_addr && psk->p2p &&
6865 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6866 wpa_dbg(wpa_s, MSG_DEBUG,
6867 "P2P: Remove persistent group PSK list entry for "
6869 MAC2STR(psk->addr), psk->p2p);
6870 dl_list_del(&psk->list);
6880 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6881 const u8 *p2p_dev_addr,
6882 const u8 *psk, size_t psk_len)
6884 struct wpa_ssid *ssid = wpa_s->current_ssid;
6885 struct wpa_ssid *persistent;
6886 struct psk_list_entry *p, *last;
6888 if (psk_len != sizeof(p->psk))
6892 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6893 " p2p_dev_addr=" MACSTR,
6894 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6895 if (is_zero_ether_addr(p2p_dev_addr))
6896 p2p_dev_addr = NULL;
6898 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6902 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6903 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6904 /* To be added to persistent group once created */
6905 if (wpa_s->global->add_psk == NULL) {
6906 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6907 if (wpa_s->global->add_psk == NULL)
6910 p = wpa_s->global->add_psk;
6913 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6916 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6918 os_memcpy(p->psk, psk, psk_len);
6922 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6923 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6927 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6930 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6934 p = os_zalloc(sizeof(*p));
6939 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6942 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6944 os_memcpy(p->psk, psk, psk_len);
6946 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
6947 (last = dl_list_last(&persistent->psk_list,
6948 struct psk_list_entry, list))) {
6949 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6950 MACSTR " (p2p=%u) to make room for a new one",
6951 MAC2STR(last->addr), last->p2p);
6952 dl_list_del(&last->list);
6956 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6957 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6958 p2p_dev_addr == NULL);
6960 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6961 MACSTR, MAC2STR(p2p_dev_addr));
6963 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6966 dl_list_add(&persistent->psk_list, &p->list);
6968 if (wpa_s->parent->conf->update_config &&
6969 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6970 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6974 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6975 struct wpa_ssid *s, const u8 *addr,
6980 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6981 if (res > 0 && wpa_s->conf->update_config &&
6982 wpa_config_write(wpa_s->confname, wpa_s->conf))
6983 wpa_dbg(wpa_s, MSG_DEBUG,
6984 "P2P: Failed to update configuration");
6988 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6989 const u8 *peer, int iface_addr)
6991 struct hostapd_data *hapd;
6992 struct hostapd_wpa_psk *psk, *prev, *rem;
6993 struct sta_info *sta;
6995 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6996 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6999 /* Remove per-station PSK entry */
7000 hapd = wpa_s->ap_iface->bss[0];
7002 psk = hapd->conf->ssid.wpa_psk;
7004 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7006 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7007 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7008 MACSTR " iface_addr=%d",
7009 MAC2STR(peer), iface_addr);
7011 prev->next = psk->next;
7013 hapd->conf->ssid.wpa_psk = psk->next;
7023 /* Disconnect from group */
7025 sta = ap_get_sta(hapd, peer);
7027 sta = ap_get_sta_p2p(hapd, peer);
7029 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7030 " (iface_addr=%d) from group",
7031 MAC2STR(peer), iface_addr);
7032 hostapd_drv_sta_deauth(hapd, sta->addr,
7033 WLAN_REASON_DEAUTH_LEAVING);
7034 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7039 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7043 struct wpa_supplicant *w;
7045 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7047 /* Remove from any persistent group */
7048 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
7049 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7052 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
7053 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
7056 /* Remove from any operating group */
7057 for (w = wpa_s->global->ifaces; w; w = w->next)
7058 wpas_p2p_remove_client_go(w, peer, iface_addr);
7062 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7064 struct wpa_supplicant *wpa_s = eloop_ctx;
7065 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7069 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7071 struct wpa_supplicant *wpa_s = eloop_ctx;
7073 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7074 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7078 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7079 struct wpa_ssid *ssid)
7081 struct wpa_supplicant *iface;
7083 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7084 if (!iface->current_ssid ||
7085 iface->current_ssid->frequency == freq ||
7086 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7087 !iface->current_ssid->p2p_group))
7090 /* Remove the connection with least priority */
7091 if (!wpas_is_p2p_prioritized(iface)) {
7092 /* STA connection has priority over existing
7093 * P2P connection, so remove the interface. */
7094 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7095 eloop_register_timeout(0, 0,
7096 wpas_p2p_group_freq_conflict,
7098 /* If connection in progress is P2P connection, do not
7099 * proceed for the connection. */
7105 /* P2P connection has priority, disable the STA network
7107 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7109 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7110 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7111 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7113 /* If P2P connection is in progress, continue
7126 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7128 struct wpa_ssid *ssid = wpa_s->current_ssid;
7130 if (ssid == NULL || !ssid->p2p_group)
7133 if (wpa_s->p2p_last_4way_hs_fail &&
7134 wpa_s->p2p_last_4way_hs_fail == ssid) {
7135 u8 go_dev_addr[ETH_ALEN];
7136 struct wpa_ssid *persistent;
7138 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7140 ssid->ssid_len) <= 0) {
7141 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7145 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7146 MACSTR, MAC2STR(go_dev_addr));
7147 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7150 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7151 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7154 wpa_msg_global(wpa_s->parent, MSG_INFO,
7155 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7158 wpa_s->p2p_last_4way_hs_fail = NULL;
7160 * Remove the group from a timeout to avoid issues with caller
7161 * continuing to use the interface if this is on a P2P group
7164 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7169 wpa_s->p2p_last_4way_hs_fail = ssid;
7174 #ifdef CONFIG_WPS_NFC
7176 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7184 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7188 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7189 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7196 wpabuf_put_be16(ret, wsc_len);
7198 wpabuf_put_buf(ret, wsc);
7199 wpabuf_put_be16(ret, wpabuf_len(p2p));
7200 wpabuf_put_buf(ret, p2p);
7204 wpa_hexdump_buf(MSG_DEBUG,
7205 "P2P: Generated NFC connection handover message", ret);
7209 tmp = ndef_build_p2p(ret);
7212 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7222 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7223 struct wpa_ssid **ssid, u8 *go_dev_addr)
7225 struct wpa_supplicant *iface;
7228 os_memset(go_dev_addr, 0, ETH_ALEN);
7231 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7232 if (iface->wpa_state < WPA_ASSOCIATING ||
7233 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7234 !iface->current_ssid->p2p_group ||
7235 iface->current_ssid->mode != WPAS_MODE_INFRA)
7238 *ssid = iface->current_ssid;
7240 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7241 return iface->assoc_freq;
7247 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7250 struct wpabuf *wsc, *p2p;
7251 struct wpa_ssid *ssid;
7252 u8 go_dev_addr[ETH_ALEN];
7253 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7255 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7256 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7260 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7261 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7262 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7263 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7267 if (cli_freq == 0) {
7268 wsc = wps_build_nfc_handover_req_p2p(
7269 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7272 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7273 go_dev_addr, ssid ? ssid->ssid : NULL,
7274 ssid ? ssid->ssid_len : 0);
7276 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7280 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7283 struct wpabuf *wsc, *p2p;
7284 struct wpa_ssid *ssid;
7285 u8 go_dev_addr[ETH_ALEN];
7286 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7288 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7291 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7292 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7293 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7296 if (cli_freq == 0) {
7297 wsc = wps_build_nfc_handover_sel_p2p(
7299 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7300 DEV_PW_NFC_CONNECTION_HANDOVER,
7301 wpa_s->conf->wps_nfc_dh_pubkey,
7302 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7305 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7306 go_dev_addr, ssid ? ssid->ssid : NULL,
7307 ssid ? ssid->ssid_len : 0);
7309 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7313 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7314 struct p2p_nfc_params *params)
7316 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7317 "connection handover (freq=%d)",
7320 if (params->go_freq && params->go_ssid_len) {
7321 wpa_s->p2p_wps_method = WPS_NFC;
7322 wpa_s->pending_join_wps_method = WPS_NFC;
7323 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7324 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7326 return wpas_p2p_join_start(wpa_s, params->go_freq,
7328 params->go_ssid_len);
7331 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7332 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7333 params->go_freq, -1, 0, 1, 1);
7337 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7338 struct p2p_nfc_params *params, int tag)
7340 int res, persistent;
7341 struct wpa_ssid *ssid;
7343 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7344 "connection handover");
7345 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7346 ssid = wpa_s->current_ssid;
7349 if (ssid->mode != WPAS_MODE_P2P_GO)
7351 if (wpa_s->ap_iface == NULL)
7355 if (wpa_s == NULL) {
7356 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7360 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7361 DEV_PW_NFC_CONNECTION_HANDOVER &&
7362 !wpa_s->parent->p2p_oob_dev_pw) {
7363 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7366 res = wpas_ap_wps_add_nfc_pw(
7367 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7368 wpa_s->parent->p2p_oob_dev_pw,
7369 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7370 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7375 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7379 if (!params->peer ||
7380 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7383 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7384 " to join", MAC2STR(params->peer->p2p_device_addr));
7386 wpa_s->global->p2p_invite_group = wpa_s;
7387 persistent = ssid->p2p_persistent_group &&
7388 wpas_p2p_get_persistent(wpa_s->parent,
7389 params->peer->p2p_device_addr,
7390 ssid->ssid, ssid->ssid_len);
7391 wpa_s->parent->pending_invite_ssid_id = -1;
7393 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7394 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7395 ssid->ssid, ssid->ssid_len, ssid->frequency,
7396 wpa_s->global->p2p_dev_addr, persistent, 0,
7397 wpa_s->parent->p2p_oob_dev_pw_id);
7401 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7402 struct p2p_nfc_params *params,
7405 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7406 "connection handover");
7407 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7408 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7409 forced_freq, -1, 0, 1, 1);
7413 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7414 struct p2p_nfc_params *params,
7419 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7420 "connection handover");
7421 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7422 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7423 forced_freq, -1, 0, 1, 1);
7427 res = wpas_p2p_listen(wpa_s, 60);
7429 p2p_unauthorize(wpa_s->global->p2p,
7430 params->peer->p2p_device_addr);
7437 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7438 const struct wpabuf *data,
7439 int sel, int tag, int forced_freq)
7441 const u8 *pos, *end;
7443 struct p2p_nfc_params params;
7446 os_memset(¶ms, 0, sizeof(params));
7449 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7451 pos = wpabuf_head(data);
7452 end = pos + wpabuf_len(data);
7454 if (end - pos < 2) {
7455 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7459 len = WPA_GET_BE16(pos);
7461 if (pos + len > end) {
7462 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7466 params.wsc_attr = pos;
7467 params.wsc_len = len;
7470 if (end - pos < 2) {
7471 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7475 len = WPA_GET_BE16(pos);
7477 if (pos + len > end) {
7478 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7482 params.p2p_attr = pos;
7483 params.p2p_len = len;
7486 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7487 params.wsc_attr, params.wsc_len);
7488 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7489 params.p2p_attr, params.p2p_len);
7491 wpa_hexdump(MSG_DEBUG,
7492 "P2P: Ignored extra data after P2P attributes",
7496 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
7500 if (params.next_step == NO_ACTION)
7503 if (params.next_step == BOTH_GO) {
7504 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7505 MAC2STR(params.peer->p2p_device_addr));
7509 if (params.next_step == PEER_CLIENT) {
7510 if (!is_zero_ether_addr(params.go_dev_addr)) {
7511 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7512 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7514 MAC2STR(params.peer->p2p_device_addr),
7516 MAC2STR(params.go_dev_addr),
7517 wpa_ssid_txt(params.go_ssid,
7518 params.go_ssid_len));
7520 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7521 "peer=" MACSTR " freq=%d",
7522 MAC2STR(params.peer->p2p_device_addr),
7528 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7529 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7530 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7534 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7535 wpa_s->p2p_oob_dev_pw = NULL;
7537 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7538 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7543 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7544 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7545 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7546 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7547 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7548 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7549 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7553 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7554 "peer OOB Device Password Id %u", id);
7557 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7558 "Device Password Id %u", id);
7559 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7560 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7561 params.oob_dev_pw_len -
7562 WPS_OOB_PUBKEY_HASH_LEN - 2);
7563 wpa_s->p2p_oob_dev_pw_id = id;
7564 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7565 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7566 params.oob_dev_pw_len -
7567 WPS_OOB_PUBKEY_HASH_LEN - 2);
7568 if (wpa_s->p2p_oob_dev_pw == NULL)
7571 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7572 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7573 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7576 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7577 "without Device Password");
7578 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7581 switch (params.next_step) {
7585 /* already covered above */
7588 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
7590 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
7592 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
7594 /* TODO: use own OOB Dev Pw */
7595 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
7602 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7603 const struct wpabuf *data, int forced_freq)
7605 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7608 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7612 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7613 const struct wpabuf *req,
7614 const struct wpabuf *sel, int forced_freq)
7619 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7622 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7624 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7625 wpabuf_head(req), wpabuf_len(req));
7626 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7627 wpabuf_head(sel), wpabuf_len(sel));
7629 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7630 tmp = ndef_parse_p2p(init ? sel : req);
7632 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7636 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7644 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7647 int go_intent = wpa_s->conf->p2p_go_intent;
7648 struct wpa_supplicant *iface;
7650 if (wpa_s->global->p2p == NULL)
7654 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7655 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7657 if (!iface->ap_iface)
7659 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7661 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7663 if (wpa_s->p2p_nfc_tag_enabled)
7664 wpas_p2p_remove_pending_group_interface(wpa_s);
7665 wpa_s->p2p_nfc_tag_enabled = 0;
7669 if (wpa_s->global->p2p_disabled)
7672 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7673 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7674 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7675 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7676 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7677 "to allow static handover cases");
7681 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7683 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7684 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7685 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7686 if (wpa_s->p2p_oob_dev_pw == NULL)
7688 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7690 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
7691 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
7693 * P2P Group Interface present and the command came on group
7694 * interface, so enable the token for the current interface.
7696 wpa_s->create_p2p_iface = 0;
7698 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7701 if (wpa_s->create_p2p_iface) {
7702 enum wpa_driver_if_type iftype;
7703 /* Prepare to add a new interface for the group */
7704 iftype = WPA_IF_P2P_GROUP;
7705 if (go_intent == 15)
7706 iftype = WPA_IF_P2P_GO;
7707 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7708 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7709 "interface for the group");
7713 if_addr = wpa_s->pending_interface_addr;
7715 if_addr = wpa_s->own_addr;
7717 wpa_s->p2p_nfc_tag_enabled = enabled;
7719 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7720 struct hostapd_data *hapd;
7721 if (iface->ap_iface == NULL)
7723 hapd = iface->ap_iface->bss[0];
7724 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7725 hapd->conf->wps_nfc_dh_pubkey =
7726 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7727 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7728 hapd->conf->wps_nfc_dh_privkey =
7729 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7730 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7731 hapd->conf->wps_nfc_dev_pw =
7732 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7733 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7735 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7736 wpa_dbg(iface, MSG_DEBUG,
7737 "P2P: Failed to enable NFC Tag for GO");
7740 p2p_set_authorized_oob_dev_pw_id(
7741 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
7747 #endif /* CONFIG_WPS_NFC */