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.
62 #define P2P_MAX_INITIAL_CONN_WAIT 10
63 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
65 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
67 * How many seconds to wait for initial 4-way handshake to get completed after
68 * WPS provisioning step on the GO. This controls the extra time the P2P
69 * operation is considered to be in progress (e.g., to delay other scans) after
70 * WPS provisioning has been completed on the GO during group formation.
72 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
73 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
75 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
77 * How many seconds to wait for initial 4-way handshake to get completed after
78 * re-invocation of a persistent group on the GO when the client is expected
79 * to connect automatically (no user interaction).
81 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
82 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
84 #ifndef P2P_CONCURRENT_SEARCH_DELAY
85 #define P2P_CONCURRENT_SEARCH_DELAY 500
86 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
88 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
90 enum p2p_group_removal_reason {
91 P2P_GROUP_REMOVAL_UNKNOWN,
92 P2P_GROUP_REMOVAL_SILENT,
93 P2P_GROUP_REMOVAL_FORMATION_FAILED,
94 P2P_GROUP_REMOVAL_REQUESTED,
95 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
96 P2P_GROUP_REMOVAL_UNAVAILABLE,
97 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
98 P2P_GROUP_REMOVAL_PSK_FAILURE,
99 P2P_GROUP_REMOVAL_FREQ_CONFLICT
103 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
104 static struct wpa_supplicant *
105 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
107 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
108 const u8 *ssid, size_t ssid_len);
109 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
110 const u8 *ssid, size_t ssid_len);
111 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
112 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
113 const u8 *dev_addr, enum p2p_wps_method wps_method,
114 int auto_join, int freq,
115 const u8 *ssid, size_t ssid_len);
116 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
117 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
118 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
119 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
120 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
122 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
123 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
125 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
126 static void wpas_stop_listen(void *ctx);
130 * Get the number of concurrent channels that the HW can operate, but that are
131 * currently not in use by any of the wpa_supplicant interfaces.
133 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
138 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
142 num = get_shared_radio_freqs(wpa_s, freqs,
143 wpa_s->num_multichan_concurrent);
146 unused = wpa_s->num_multichan_concurrent - num;
147 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
153 * Get the frequencies that are currently in use by one or more of the virtual
154 * interfaces, and that are also valid for P2P operation.
156 static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
157 int *p2p_freqs, unsigned int len)
160 unsigned int num, i, j;
162 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
166 num = get_shared_radio_freqs(wpa_s, freqs,
167 wpa_s->num_multichan_concurrent);
169 os_memset(p2p_freqs, 0, sizeof(int) * len);
171 for (i = 0, j = 0; i < num && j < len; i++) {
172 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
173 p2p_freqs[j++] = freqs[i];
178 dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
184 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
187 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
189 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
190 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
191 wpas_p2p_num_unused_channels(wpa_s) > 0) {
192 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
196 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
200 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
201 struct wpa_scan_results *scan_res)
205 if (wpa_s->p2p_scan_work) {
206 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
207 wpa_s->p2p_scan_work = NULL;
208 radio_work_done(work);
211 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
214 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
215 (int) scan_res->num);
217 for (i = 0; i < scan_res->num; i++) {
218 struct wpa_scan_res *bss = scan_res->res[i];
219 struct os_reltime time_tmp_age, entry_ts;
223 time_tmp_age.sec = bss->age / 1000;
224 time_tmp_age.usec = (bss->age % 1000) * 1000;
225 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
227 ies = (const u8 *) (bss + 1);
228 ies_len = bss->ie_len;
229 if (bss->beacon_ie_len > 0 &&
230 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
231 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
232 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
233 MACSTR, MAC2STR(bss->bssid));
235 ies_len = bss->beacon_ie_len;
239 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
240 bss->freq, &entry_ts, bss->level,
245 p2p_scan_res_handled(wpa_s->global->p2p);
249 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
251 struct wpa_supplicant *wpa_s = work->wpa_s;
252 struct wpa_driver_scan_params *params = work->ctx;
256 if (!work->started) {
257 wpa_scan_free_params(params);
261 wpa_s->p2p_scan_work = NULL;
265 ret = wpa_drv_scan(wpa_s, params);
266 wpa_scan_free_params(params);
269 radio_work_done(work);
273 os_get_reltime(&wpa_s->scan_trigger_time);
274 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
275 wpa_s->own_scan_requested = 1;
276 wpa_s->p2p_scan_work = work;
280 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
281 unsigned int num_req_dev_types,
282 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
284 struct wpa_supplicant *wpa_s = ctx;
285 struct wpa_driver_scan_params *params = NULL;
286 struct wpabuf *wps_ie, *ies;
290 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
293 if (wpa_s->p2p_scan_work) {
294 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
298 params = os_zalloc(sizeof(*params));
302 /* P2P Wildcard SSID */
303 params->num_ssids = 1;
304 n = os_malloc(P2P_WILDCARD_SSID_LEN);
307 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
308 params->ssids[0].ssid = n;
309 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
311 wpa_s->wps->dev.p2p = 1;
312 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
313 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
314 num_req_dev_types, req_dev_types);
318 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
319 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
324 wpabuf_put_buf(ies, wps_ie);
327 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
329 params->p2p_probe = 1;
330 n = os_malloc(wpabuf_len(ies));
335 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
336 params->extra_ies = n;
337 params->extra_ies_len = wpabuf_len(ies);
341 case P2P_SCAN_SOCIAL:
342 params->freqs = os_malloc(4 * sizeof(int));
343 if (params->freqs == NULL)
345 params->freqs[0] = 2412;
346 params->freqs[1] = 2437;
347 params->freqs[2] = 2462;
348 params->freqs[3] = 0;
352 case P2P_SCAN_SOCIAL_PLUS_ONE:
353 params->freqs = os_malloc(5 * sizeof(int));
354 if (params->freqs == NULL)
356 params->freqs[0] = 2412;
357 params->freqs[1] = 2437;
358 params->freqs[2] = 2462;
359 params->freqs[3] = freq;
360 params->freqs[4] = 0;
364 radio_remove_works(wpa_s, "p2p-scan", 0);
365 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
371 wpa_scan_free_params(params);
376 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
378 switch (p2p_group_interface) {
379 case P2P_GROUP_INTERFACE_PENDING:
380 return WPA_IF_P2P_GROUP;
381 case P2P_GROUP_INTERFACE_GO:
382 return WPA_IF_P2P_GO;
383 case P2P_GROUP_INTERFACE_CLIENT:
384 return WPA_IF_P2P_CLIENT;
387 return WPA_IF_P2P_GROUP;
391 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
393 size_t ssid_len, int *go)
397 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
398 for (s = wpa_s->conf->ssid; s; s = s->next) {
399 if (s->disabled != 0 || !s->p2p_group ||
400 s->ssid_len != ssid_len ||
401 os_memcmp(ssid, s->ssid, ssid_len) != 0)
403 if (s->mode == WPAS_MODE_P2P_GO &&
404 s != wpa_s->current_ssid)
407 *go = s->mode == WPAS_MODE_P2P_GO;
416 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
417 enum p2p_group_removal_reason removal_reason)
419 struct wpa_ssid *ssid;
423 ssid = wpa_s->current_ssid;
426 * The current SSID was not known, but there may still be a
427 * pending P2P group interface waiting for provisioning or a
428 * P2P group that is trying to reconnect.
430 ssid = wpa_s->conf->ssid;
432 if (ssid->p2p_group && ssid->disabled != 2)
437 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
439 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
444 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
446 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
447 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
448 wpa_s->reassociate = 0;
449 wpa_s->disconnected = 1;
450 wpa_supplicant_deauthenticate(wpa_s,
451 WLAN_REASON_DEAUTH_LEAVING);
455 if (wpa_s->cross_connect_in_use) {
456 wpa_s->cross_connect_in_use = 0;
457 wpa_msg_global(wpa_s->parent, MSG_INFO,
458 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
459 wpa_s->ifname, wpa_s->cross_connect_uplink);
461 switch (removal_reason) {
462 case P2P_GROUP_REMOVAL_REQUESTED:
463 reason = " reason=REQUESTED";
465 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
466 reason = " reason=FORMATION_FAILED";
468 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
469 reason = " reason=IDLE";
471 case P2P_GROUP_REMOVAL_UNAVAILABLE:
472 reason = " reason=UNAVAILABLE";
474 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
475 reason = " reason=GO_ENDING_SESSION";
477 case P2P_GROUP_REMOVAL_PSK_FAILURE:
478 reason = " reason=PSK_FAILURE";
480 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
481 reason = " reason=FREQ_CONFLICT";
487 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
488 wpa_msg_global(wpa_s->parent, MSG_INFO,
489 P2P_EVENT_GROUP_REMOVED "%s %s%s",
490 wpa_s->ifname, gtype, reason);
493 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
494 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
495 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
496 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
497 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
498 wpa_s->parent, NULL) > 0) {
499 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
501 wpa_s->p2p_in_provisioning = 0;
505 * Make sure wait for the first client does not remain active after the
506 * group has been removed.
508 wpa_s->global->p2p_go_wait_client.sec = 0;
510 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
511 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
513 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
514 struct wpa_global *global;
516 enum wpa_driver_if_type type;
517 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
519 global = wpa_s->global;
520 ifname = os_strdup(wpa_s->ifname);
521 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
522 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
523 wpa_s = global->ifaces;
525 wpa_drv_if_remove(wpa_s, type, ifname);
530 if (!wpa_s->p2p_go_group_formation_completed) {
531 wpa_s->global->p2p_group_formation = NULL;
532 wpa_s->p2p_in_provisioning = 0;
535 wpa_s->show_group_started = 0;
536 os_free(wpa_s->go_params);
537 wpa_s->go_params = NULL;
539 wpa_s->waiting_presence_resp = 0;
541 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
542 if (ssid && (ssid->p2p_group ||
543 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
544 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
546 if (ssid == wpa_s->current_ssid) {
547 wpa_sm_set_config(wpa_s->wpa, NULL);
548 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
549 wpa_s->current_ssid = NULL;
552 * Networks objects created during any P2P activities are not
553 * exposed out as they might/will confuse certain non-P2P aware
554 * applications since these network objects won't behave like
557 * Likewise, we don't send out network removed signals for such
560 wpa_config_remove_network(wpa_s->conf, id);
561 wpa_supplicant_clear_status(wpa_s);
562 wpa_supplicant_cancel_sched_scan(wpa_s);
564 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
568 wpa_supplicant_ap_deinit(wpa_s);
570 wpa_drv_deinit_p2p_cli(wpa_s);
576 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
578 const u8 *ssid, size_t ssid_len)
586 if (wpa_s->go_params)
587 bssid = wpa_s->go_params->peer_interface_addr;
589 bssid = wpa_s->bssid;
591 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
592 if (bss == NULL && wpa_s->go_params &&
593 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
594 bss = wpa_bss_get_p2p_dev_addr(
595 wpa_s, wpa_s->go_params->peer_device_addr);
597 u8 iface_addr[ETH_ALEN];
598 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
600 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
603 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
604 "group is persistent - BSS " MACSTR " not found",
609 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
611 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
614 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
615 "group is persistent - BSS " MACSTR
616 " did not include P2P IE", MAC2STR(bssid));
617 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
618 (u8 *) (bss + 1), bss->ie_len);
619 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
620 ((u8 *) bss + 1) + bss->ie_len,
625 group_capab = p2p_get_group_capab(p2p);
626 addr = p2p_get_go_dev_addr(p2p);
627 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
628 "group_capab=0x%x", group_capab);
630 os_memcpy(go_dev_addr, addr, ETH_ALEN);
631 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
634 os_memset(go_dev_addr, 0, ETH_ALEN);
637 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
638 "go_dev_addr=" MACSTR,
639 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
641 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
645 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
646 struct wpa_ssid *ssid,
647 const u8 *go_dev_addr)
652 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
653 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
654 for (s = wpa_s->conf->ssid; s; s = s->next) {
655 if (s->disabled == 2 &&
656 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
657 s->ssid_len == ssid->ssid_len &&
658 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
663 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
665 if (ssid->passphrase && !s->passphrase)
667 else if (ssid->passphrase && s->passphrase &&
668 os_strcmp(ssid->passphrase, s->passphrase) != 0)
671 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
674 s = wpa_config_add_network(wpa_s->conf);
679 * Instead of network_added we emit persistent_group_added
680 * notification. Also to keep the defense checks in
681 * persistent_group obj registration method, we set the
682 * relevant flags in s to designate it as a persistent group.
685 s->p2p_persistent_group = 1;
686 wpas_notify_persistent_group_added(wpa_s, s);
687 wpa_config_set_network_defaults(s);
691 s->p2p_persistent_group = 1;
694 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
695 s->mode = ssid->mode;
696 s->auth_alg = WPA_AUTH_ALG_OPEN;
697 s->key_mgmt = WPA_KEY_MGMT_PSK;
698 s->proto = WPA_PROTO_RSN;
699 s->pairwise_cipher = WPA_CIPHER_CCMP;
701 if (ssid->passphrase) {
702 os_free(s->passphrase);
703 s->passphrase = os_strdup(ssid->passphrase);
707 os_memcpy(s->psk, ssid->psk, 32);
709 if (s->passphrase && !s->psk_set)
710 wpa_config_update_psk(s);
711 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
713 s->ssid = os_malloc(ssid->ssid_len);
716 s->ssid_len = ssid->ssid_len;
717 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
719 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
720 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
721 wpa_s->global->add_psk = NULL;
725 if (changed && wpa_s->conf->update_config &&
726 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
727 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
734 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
737 struct wpa_ssid *ssid, *s;
742 ssid = wpa_s->current_ssid;
743 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
744 !ssid->p2p_persistent_group)
747 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
748 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
751 if (s->ssid_len == ssid->ssid_len &&
752 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
759 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
760 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
764 if (i == s->num_p2p_clients - 1)
765 return; /* already the most recent entry */
767 /* move the entry to mark it most recent */
768 os_memmove(s->p2p_client_list + i * ETH_ALEN,
769 s->p2p_client_list + (i + 1) * ETH_ALEN,
770 (s->num_p2p_clients - i - 1) * ETH_ALEN);
771 os_memcpy(s->p2p_client_list +
772 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
777 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
778 n = os_realloc_array(s->p2p_client_list,
779 s->num_p2p_clients + 1, ETH_ALEN);
782 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
783 s->p2p_client_list = n;
784 s->num_p2p_clients++;
786 /* Not enough room for an additional entry - drop the oldest
788 os_memmove(s->p2p_client_list,
789 s->p2p_client_list + ETH_ALEN,
790 (s->num_p2p_clients - 1) * ETH_ALEN);
791 os_memcpy(s->p2p_client_list +
792 (s->num_p2p_clients - 1) * ETH_ALEN,
796 if (wpa_s->parent->conf->update_config &&
797 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
798 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
802 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
805 struct wpa_ssid *ssid;
806 const char *ssid_txt;
809 u8 go_dev_addr[ETH_ALEN];
813 * This callback is likely called for the main interface. Update wpa_s
814 * to use the group interface if a new interface was created for the
817 if (wpa_s->global->p2p_group_formation)
818 wpa_s = wpa_s->global->p2p_group_formation;
819 if (wpa_s->p2p_go_group_formation_completed) {
820 wpa_s->global->p2p_group_formation = NULL;
821 wpa_s->p2p_in_provisioning = 0;
825 wpa_msg_global(wpa_s->parent, MSG_INFO,
826 P2P_EVENT_GROUP_FORMATION_FAILURE);
827 wpas_p2p_group_delete(wpa_s,
828 P2P_GROUP_REMOVAL_FORMATION_FAILED);
832 wpa_msg_global(wpa_s->parent, MSG_INFO,
833 P2P_EVENT_GROUP_FORMATION_SUCCESS);
835 ssid = wpa_s->current_ssid;
836 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
837 ssid->mode = WPAS_MODE_P2P_GO;
838 p2p_group_notif_formation_done(wpa_s->p2p_group);
839 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
844 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
845 client = ssid->mode == WPAS_MODE_INFRA;
846 if (ssid->mode == WPAS_MODE_P2P_GO) {
847 persistent = ssid->p2p_persistent_group;
848 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
851 persistent = wpas_p2p_persistent_group(wpa_s,
857 client = wpa_s->p2p_group_interface ==
858 P2P_GROUP_INTERFACE_CLIENT;
859 os_memset(go_dev_addr, 0, ETH_ALEN);
862 wpa_s->show_group_started = 0;
865 * Indicate event only after successfully completed 4-way
866 * handshake, i.e., when the interface is ready for data
869 wpa_s->show_group_started = 1;
870 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
872 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
873 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
874 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
876 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
877 MAC2STR(go_dev_addr),
878 persistent ? " [PERSISTENT]" : "");
879 wpas_p2p_cross_connect_setup(wpa_s);
880 wpas_p2p_set_group_idle_timeout(wpa_s);
882 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
883 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
884 "go_dev_addr=" MACSTR "%s",
885 wpa_s->ifname, ssid_txt,
886 ssid ? ssid->frequency : 0,
887 ssid && ssid->passphrase ? ssid->passphrase : "",
888 MAC2STR(go_dev_addr),
889 persistent ? " [PERSISTENT]" : "");
890 wpas_p2p_cross_connect_setup(wpa_s);
891 wpas_p2p_set_group_idle_timeout(wpa_s);
895 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
898 os_free(wpa_s->global->add_psk);
899 wpa_s->global->add_psk = NULL;
901 if (network_id < 0 && ssid)
902 network_id = ssid->id;
904 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
905 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
910 struct send_action_work {
916 unsigned int wait_time;
921 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
924 struct wpa_supplicant *wpa_s = eloop_ctx;
926 if (!wpa_s->p2p_send_action_work)
929 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
930 os_free(wpa_s->p2p_send_action_work->ctx);
931 radio_work_done(wpa_s->p2p_send_action_work);
932 wpa_s->p2p_send_action_work = NULL;
936 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
938 const u8 *dst, const u8 *src,
940 const u8 *data, size_t data_len,
941 enum offchannel_send_action_result
944 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
946 if (wpa_s->p2p_send_action_work) {
947 struct send_action_work *awork;
948 awork = wpa_s->p2p_send_action_work->ctx;
949 if (awork->wait_time == 0) {
951 radio_work_done(wpa_s->p2p_send_action_work);
952 wpa_s->p2p_send_action_work = NULL;
955 * In theory, this should not be needed, but number of
956 * places in the P2P code is still using non-zero wait
957 * time for the last Action frame in the sequence and
958 * some of these do not call send_action_done().
960 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
962 eloop_register_timeout(
963 0, awork->wait_time * 1000,
964 wpas_p2p_send_action_work_timeout,
969 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
973 case OFFCHANNEL_SEND_ACTION_SUCCESS:
974 res = P2P_SEND_ACTION_SUCCESS;
976 case OFFCHANNEL_SEND_ACTION_NO_ACK:
977 res = P2P_SEND_ACTION_NO_ACK;
979 case OFFCHANNEL_SEND_ACTION_FAILED:
980 res = P2P_SEND_ACTION_FAILED;
984 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
986 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
987 wpa_s->pending_pd_before_join &&
988 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
989 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
990 wpa_s->p2p_fallback_to_go_neg) {
991 wpa_s->pending_pd_before_join = 0;
992 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
993 "during p2p_connect-auto");
994 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1000 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1002 struct wpa_supplicant *wpa_s = work->wpa_s;
1003 struct send_action_work *awork = work->ctx;
1006 if (work->started) {
1007 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1009 wpa_s->p2p_send_action_work = NULL;
1010 offchannel_send_action_done(wpa_s);
1016 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1017 awork->bssid, awork->buf, awork->len,
1019 wpas_p2p_send_action_tx_status, 1) < 0) {
1021 radio_work_done(work);
1024 wpa_s->p2p_send_action_work = work;
1028 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1029 unsigned int freq, const u8 *dst,
1030 const u8 *src, const u8 *bssid, const u8 *buf,
1031 size_t len, unsigned int wait_time)
1033 struct send_action_work *awork;
1035 if (wpa_s->p2p_send_action_work) {
1036 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1040 awork = os_zalloc(sizeof(*awork) + len);
1045 os_memcpy(awork->dst, dst, ETH_ALEN);
1046 os_memcpy(awork->src, src, ETH_ALEN);
1047 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1049 awork->wait_time = wait_time;
1050 os_memcpy(awork->buf, buf, len);
1052 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1053 wpas_send_action_cb, awork) < 0) {
1062 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1063 const u8 *src, const u8 *bssid, const u8 *buf,
1064 size_t len, unsigned int wait_time)
1066 struct wpa_supplicant *wpa_s = ctx;
1067 int listen_freq = -1, send_freq = -1;
1069 if (wpa_s->p2p_listen_work)
1070 listen_freq = wpa_s->p2p_listen_work->freq;
1071 if (wpa_s->p2p_send_action_work)
1072 send_freq = wpa_s->p2p_send_action_work->freq;
1073 if (listen_freq != (int) freq && send_freq != (int) freq) {
1074 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1075 listen_freq, send_freq);
1076 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1080 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1081 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1083 wpas_p2p_send_action_tx_status, 1);
1087 static void wpas_send_action_done(void *ctx)
1089 struct wpa_supplicant *wpa_s = ctx;
1091 if (wpa_s->p2p_send_action_work) {
1092 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1094 os_free(wpa_s->p2p_send_action_work->ctx);
1095 radio_work_done(wpa_s->p2p_send_action_work);
1096 wpa_s->p2p_send_action_work = NULL;
1099 offchannel_send_action_done(wpa_s);
1103 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1104 struct p2p_go_neg_results *params)
1106 if (wpa_s->go_params == NULL) {
1107 wpa_s->go_params = os_malloc(sizeof(*params));
1108 if (wpa_s->go_params == NULL)
1111 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1116 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1117 struct p2p_go_neg_results *res)
1119 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1120 " dev_addr " MACSTR " wps_method %d",
1121 MAC2STR(res->peer_interface_addr),
1122 MAC2STR(res->peer_device_addr), res->wps_method);
1123 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1124 res->ssid, res->ssid_len);
1125 wpa_supplicant_ap_deinit(wpa_s);
1126 wpas_copy_go_neg_results(wpa_s, res);
1127 if (res->wps_method == WPS_PBC) {
1128 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1129 #ifdef CONFIG_WPS_NFC
1130 } else if (res->wps_method == WPS_NFC) {
1131 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1132 res->peer_interface_addr,
1133 wpa_s->parent->p2p_oob_dev_pw,
1134 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1135 wpa_s->parent->p2p_oob_dev_pw_id ==
1136 DEV_PW_NFC_CONNECTION_HANDOVER ?
1137 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1140 #endif /* CONFIG_WPS_NFC */
1142 u16 dev_pw_id = DEV_PW_DEFAULT;
1143 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1144 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1145 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1146 wpa_s->p2p_pin, 1, dev_pw_id);
1151 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1152 struct wpa_ssid *ssid)
1154 struct wpa_ssid *persistent;
1155 struct psk_list_entry *psk;
1156 struct hostapd_data *hapd;
1158 if (!wpa_s->ap_iface)
1161 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1163 if (persistent == NULL)
1166 hapd = wpa_s->ap_iface->bss[0];
1168 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1170 struct hostapd_wpa_psk *hpsk;
1172 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1174 MAC2STR(psk->addr), psk->p2p);
1175 hpsk = os_zalloc(sizeof(*hpsk));
1178 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1180 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1182 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1183 hpsk->next = hapd->conf->ssid.wpa_psk;
1184 hapd->conf->ssid.wpa_psk = hpsk;
1189 static void p2p_go_configured(void *ctx, void *data)
1191 struct wpa_supplicant *wpa_s = ctx;
1192 struct p2p_go_neg_results *params = data;
1193 struct wpa_ssid *ssid;
1194 int network_id = -1;
1196 ssid = wpa_s->current_ssid;
1197 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1198 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1199 if (wpa_s->global->p2p_group_formation == wpa_s)
1200 wpa_s->global->p2p_group_formation = NULL;
1201 if (os_strlen(params->passphrase) > 0) {
1202 wpa_msg_global(wpa_s->parent, MSG_INFO,
1203 P2P_EVENT_GROUP_STARTED
1204 "%s GO ssid=\"%s\" freq=%d "
1205 "passphrase=\"%s\" go_dev_addr=" MACSTR
1206 "%s", wpa_s->ifname,
1207 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1208 ssid->frequency, params->passphrase,
1209 MAC2STR(wpa_s->global->p2p_dev_addr),
1210 params->persistent_group ?
1211 " [PERSISTENT]" : "");
1214 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1215 sizeof(params->psk));
1216 wpa_msg_global(wpa_s->parent, MSG_INFO,
1217 P2P_EVENT_GROUP_STARTED
1218 "%s GO ssid=\"%s\" freq=%d psk=%s "
1219 "go_dev_addr=" MACSTR "%s",
1221 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1222 ssid->frequency, psk,
1223 MAC2STR(wpa_s->global->p2p_dev_addr),
1224 params->persistent_group ?
1225 " [PERSISTENT]" : "");
1228 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1229 if (params->persistent_group) {
1230 network_id = wpas_p2p_store_persistent_group(
1231 wpa_s->parent, ssid,
1232 wpa_s->global->p2p_dev_addr);
1233 wpas_p2p_add_psk_list(wpa_s, ssid);
1236 network_id = ssid->id;
1237 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1238 wpas_p2p_cross_connect_setup(wpa_s);
1239 wpas_p2p_set_group_idle_timeout(wpa_s);
1241 if (wpa_s->p2p_first_connection_timeout) {
1242 wpa_dbg(wpa_s, MSG_DEBUG,
1243 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1244 wpa_s->p2p_first_connection_timeout);
1245 wpa_s->p2p_go_group_formation_completed = 0;
1246 wpa_s->global->p2p_group_formation = wpa_s;
1247 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1248 wpa_s->parent, NULL);
1249 eloop_register_timeout(
1250 wpa_s->p2p_first_connection_timeout, 0,
1251 wpas_p2p_group_formation_timeout,
1252 wpa_s->parent, NULL);
1258 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1259 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1260 params->peer_interface_addr)) {
1261 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1265 if (params->wps_method == WPS_PBC) {
1266 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1267 params->peer_device_addr);
1268 #ifdef CONFIG_WPS_NFC
1269 } else if (params->wps_method == WPS_NFC) {
1270 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1271 DEV_PW_NFC_CONNECTION_HANDOVER &&
1272 !wpa_s->parent->p2p_oob_dev_pw) {
1273 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1276 wpas_ap_wps_add_nfc_pw(
1277 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1278 wpa_s->parent->p2p_oob_dev_pw,
1279 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1280 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1281 #endif /* CONFIG_WPS_NFC */
1282 } else if (wpa_s->p2p_pin[0])
1283 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1284 wpa_s->p2p_pin, NULL, 0, 0);
1285 os_free(wpa_s->go_params);
1286 wpa_s->go_params = NULL;
1290 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1291 struct p2p_go_neg_results *params,
1292 int group_formation)
1294 struct wpa_ssid *ssid;
1296 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1297 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1298 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1303 ssid = wpa_config_add_network(wpa_s->conf);
1305 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1309 wpa_s->show_group_started = 0;
1311 wpa_config_set_network_defaults(ssid);
1312 ssid->temporary = 1;
1313 ssid->p2p_group = 1;
1314 ssid->p2p_persistent_group = params->persistent_group;
1315 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1317 ssid->frequency = params->freq;
1318 ssid->ht40 = params->ht40;
1319 ssid->vht = params->vht;
1320 ssid->ssid = os_zalloc(params->ssid_len + 1);
1322 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1323 ssid->ssid_len = params->ssid_len;
1325 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1326 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1327 ssid->proto = WPA_PROTO_RSN;
1328 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1329 if (os_strlen(params->passphrase) > 0) {
1330 ssid->passphrase = os_strdup(params->passphrase);
1331 if (ssid->passphrase == NULL) {
1332 wpa_msg_global(wpa_s, MSG_ERROR,
1333 "P2P: Failed to copy passphrase for GO");
1334 wpa_config_remove_network(wpa_s->conf, ssid->id);
1338 ssid->passphrase = NULL;
1339 ssid->psk_set = params->psk_set;
1341 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1342 else if (ssid->passphrase)
1343 wpa_config_update_psk(ssid);
1344 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1346 wpa_s->ap_configured_cb = p2p_go_configured;
1347 wpa_s->ap_configured_cb_ctx = wpa_s;
1348 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1349 wpa_s->connect_without_scan = ssid;
1350 wpa_s->reassociate = 1;
1351 wpa_s->disconnected = 0;
1352 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1354 wpa_supplicant_req_scan(wpa_s, 0, 0);
1358 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1359 const struct wpa_supplicant *src)
1361 struct wpa_config *d;
1362 const struct wpa_config *s;
1367 #define C(n) if (s->n) d->n = os_strdup(s->n)
1376 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1377 os_memcpy(d->sec_device_type, s->sec_device_type,
1378 sizeof(d->sec_device_type));
1379 d->num_sec_device_types = s->num_sec_device_types;
1381 d->p2p_group_idle = s->p2p_group_idle;
1382 d->p2p_intra_bss = s->p2p_intra_bss;
1383 d->persistent_reconnect = s->persistent_reconnect;
1384 d->max_num_sta = s->max_num_sta;
1385 d->pbc_in_m1 = s->pbc_in_m1;
1386 d->ignore_old_scan_res = s->ignore_old_scan_res;
1387 d->beacon_int = s->beacon_int;
1388 d->dtim_period = s->dtim_period;
1389 d->disassoc_low_ack = s->disassoc_low_ack;
1390 d->disable_scan_offload = s->disable_scan_offload;
1392 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1393 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1394 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1399 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1400 char *ifname, size_t len)
1402 char *ifname_ptr = wpa_s->ifname;
1404 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1405 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1406 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1409 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1410 if (os_strlen(ifname) >= IFNAMSIZ &&
1411 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1412 /* Try to avoid going over the IFNAMSIZ length limit */
1413 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1418 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1419 enum wpa_driver_if_type type)
1421 char ifname[120], force_ifname[120];
1423 if (wpa_s->pending_interface_name[0]) {
1424 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1425 "- skip creation of a new one");
1426 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1427 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1428 "unknown?! ifname='%s'",
1429 wpa_s->pending_interface_name);
1435 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1436 force_ifname[0] = '\0';
1438 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1440 wpa_s->p2p_group_idx++;
1442 wpa_s->pending_interface_type = type;
1443 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1444 wpa_s->pending_interface_addr, NULL) < 0) {
1445 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1450 if (force_ifname[0]) {
1451 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1453 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1454 sizeof(wpa_s->pending_interface_name));
1456 os_strlcpy(wpa_s->pending_interface_name, ifname,
1457 sizeof(wpa_s->pending_interface_name));
1458 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1459 MACSTR, wpa_s->pending_interface_name,
1460 MAC2STR(wpa_s->pending_interface_addr));
1466 static void wpas_p2p_remove_pending_group_interface(
1467 struct wpa_supplicant *wpa_s)
1469 if (!wpa_s->pending_interface_name[0] ||
1470 is_zero_ether_addr(wpa_s->pending_interface_addr))
1471 return; /* No pending virtual interface */
1473 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1474 wpa_s->pending_interface_name);
1475 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1476 wpa_s->pending_interface_name);
1477 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1478 wpa_s->pending_interface_name[0] = '\0';
1482 static struct wpa_supplicant *
1483 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1485 struct wpa_interface iface;
1486 struct wpa_supplicant *group_wpa_s;
1488 if (!wpa_s->pending_interface_name[0]) {
1489 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1490 if (!wpas_p2p_create_iface(wpa_s))
1493 * Something has forced us to remove the pending interface; try
1494 * to create a new one and hope for the best that we will get
1495 * the same local address.
1497 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1498 WPA_IF_P2P_CLIENT) < 0)
1502 os_memset(&iface, 0, sizeof(iface));
1503 iface.ifname = wpa_s->pending_interface_name;
1504 iface.driver = wpa_s->driver->name;
1505 if (wpa_s->conf->ctrl_interface == NULL &&
1506 wpa_s->parent != wpa_s &&
1508 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1509 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1511 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1512 iface.driver_param = wpa_s->conf->driver_param;
1513 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1514 if (group_wpa_s == NULL) {
1515 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1516 "wpa_supplicant interface");
1519 wpa_s->pending_interface_name[0] = '\0';
1520 group_wpa_s->parent = wpa_s;
1521 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1522 P2P_GROUP_INTERFACE_CLIENT;
1523 wpa_s->global->p2p_group_formation = group_wpa_s;
1525 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1531 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1534 struct wpa_supplicant *wpa_s = eloop_ctx;
1535 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1536 wpas_p2p_group_formation_failed(wpa_s);
1540 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1542 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1543 wpa_s->parent, NULL);
1544 if (wpa_s->global->p2p)
1545 p2p_group_formation_failed(wpa_s->global->p2p);
1546 wpas_group_formation_completed(wpa_s, 0);
1550 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1552 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1553 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1554 wpa_s->parent, NULL);
1555 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1556 wpa_s->parent, NULL);
1557 wpa_s->global->p2p_fail_on_wps_complete = 0;
1561 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1563 if (wpa_s->global->p2p_group_formation != wpa_s)
1565 /* Speed up group formation timeout since this cannot succeed */
1566 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1567 wpa_s->parent, NULL);
1568 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1569 wpa_s->parent, NULL);
1573 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1575 struct wpa_supplicant *wpa_s = ctx;
1577 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1578 wpa_drv_cancel_remain_on_channel(wpa_s);
1579 wpa_s->off_channel_freq = 0;
1580 wpa_s->roc_waiting_drv_freq = 0;
1584 wpa_msg_global(wpa_s, MSG_INFO,
1585 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1587 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1588 wpas_p2p_remove_pending_group_interface(wpa_s);
1592 if (wpa_s->p2p_go_ht40)
1594 if (wpa_s->p2p_go_vht)
1597 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1598 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1600 res->role_go ? "GO" : "client", res->freq, res->ht40,
1601 MAC2STR(res->peer_device_addr),
1602 MAC2STR(res->peer_interface_addr),
1603 p2p_wps_method_text(res->wps_method));
1604 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1606 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1607 struct wpa_ssid *ssid;
1608 ssid = wpa_config_get_network(wpa_s->conf,
1609 wpa_s->p2p_persistent_id);
1610 if (ssid && ssid->disabled == 2 &&
1611 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1612 size_t len = os_strlen(ssid->passphrase);
1613 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1614 "on requested persistent group");
1615 os_memcpy(res->passphrase, ssid->passphrase, len);
1616 res->passphrase[len] = '\0';
1620 if (wpa_s->create_p2p_iface) {
1621 struct wpa_supplicant *group_wpa_s =
1622 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1623 if (group_wpa_s == NULL) {
1624 wpas_p2p_remove_pending_group_interface(wpa_s);
1627 if (group_wpa_s != wpa_s) {
1628 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1629 sizeof(group_wpa_s->p2p_pin));
1630 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1632 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1633 wpa_s->pending_interface_name[0] = '\0';
1634 group_wpa_s->p2p_in_provisioning = 1;
1637 wpas_start_wps_go(group_wpa_s, res, 1);
1639 wpas_start_wps_enrollee(group_wpa_s, res);
1641 wpa_s->p2p_in_provisioning = 1;
1642 wpa_s->global->p2p_group_formation = wpa_s;
1645 wpas_start_wps_go(wpa_s, res, 1);
1647 wpas_start_wps_enrollee(ctx, res);
1650 wpa_s->p2p_long_listen = 0;
1651 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1653 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1654 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1655 (res->peer_config_timeout % 100) * 10000,
1656 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1660 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1662 struct wpa_supplicant *wpa_s = ctx;
1663 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1664 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1666 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1670 static void wpas_dev_found(void *ctx, const u8 *addr,
1671 const struct p2p_peer_info *info,
1674 #ifndef CONFIG_NO_STDOUT_DEBUG
1675 struct wpa_supplicant *wpa_s = ctx;
1676 char devtype[WPS_DEV_TYPE_BUFSIZE];
1677 char *wfd_dev_info_hex = NULL;
1679 #ifdef CONFIG_WIFI_DISPLAY
1680 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1681 WFD_SUBELEM_DEVICE_INFO);
1682 #endif /* CONFIG_WIFI_DISPLAY */
1684 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1685 " p2p_dev_addr=" MACSTR
1686 " pri_dev_type=%s name='%s' config_methods=0x%x "
1687 "dev_capab=0x%x group_capab=0x%x%s%s",
1688 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1689 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1691 info->device_name, info->config_methods,
1692 info->dev_capab, info->group_capab,
1693 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1694 wfd_dev_info_hex ? wfd_dev_info_hex : "");
1696 os_free(wfd_dev_info_hex);
1697 #endif /* CONFIG_NO_STDOUT_DEBUG */
1699 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1703 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1705 struct wpa_supplicant *wpa_s = ctx;
1707 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1708 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1710 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1714 static void wpas_find_stopped(void *ctx)
1716 struct wpa_supplicant *wpa_s = ctx;
1717 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1721 struct wpas_p2p_listen_work {
1723 unsigned int duration;
1724 struct wpabuf *probe_resp_ie;
1728 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1732 wpabuf_free(lwork->probe_resp_ie);
1737 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1739 struct wpas_p2p_listen_work *lwork;
1741 if (!wpa_s->p2p_listen_work)
1744 lwork = wpa_s->p2p_listen_work->ctx;
1745 wpas_p2p_listen_work_free(lwork);
1746 radio_work_done(wpa_s->p2p_listen_work);
1747 wpa_s->p2p_listen_work = NULL;
1751 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1753 struct wpa_supplicant *wpa_s = work->wpa_s;
1754 struct wpas_p2p_listen_work *lwork = work->ctx;
1757 if (work->started) {
1758 wpa_s->p2p_listen_work = NULL;
1759 wpas_stop_listen(wpa_s);
1761 wpas_p2p_listen_work_free(lwork);
1765 wpa_s->p2p_listen_work = work;
1767 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1769 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1770 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1771 "report received Probe Request frames");
1772 wpas_p2p_listen_work_done(wpa_s);
1776 wpa_s->pending_listen_freq = lwork->freq;
1777 wpa_s->pending_listen_duration = lwork->duration;
1779 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1781 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1782 "to remain on channel (%u MHz) for Listen "
1783 "state", lwork->freq);
1784 wpas_p2p_listen_work_done(wpa_s);
1785 wpa_s->pending_listen_freq = 0;
1788 wpa_s->off_channel_freq = 0;
1789 wpa_s->roc_waiting_drv_freq = lwork->freq;
1793 static int wpas_start_listen(void *ctx, unsigned int freq,
1794 unsigned int duration,
1795 const struct wpabuf *probe_resp_ie)
1797 struct wpa_supplicant *wpa_s = ctx;
1798 struct wpas_p2p_listen_work *lwork;
1800 if (wpa_s->p2p_listen_work) {
1801 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
1805 lwork = os_zalloc(sizeof(*lwork));
1809 lwork->duration = duration;
1810 if (probe_resp_ie) {
1811 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1812 if (lwork->probe_resp_ie == NULL) {
1813 wpas_p2p_listen_work_free(lwork);
1818 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1820 wpas_p2p_listen_work_free(lwork);
1828 static void wpas_stop_listen(void *ctx)
1830 struct wpa_supplicant *wpa_s = ctx;
1831 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1832 wpa_drv_cancel_remain_on_channel(wpa_s);
1833 wpa_s->off_channel_freq = 0;
1834 wpa_s->roc_waiting_drv_freq = 0;
1836 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1837 wpa_drv_probe_req_report(wpa_s, 0);
1838 wpas_p2p_listen_work_done(wpa_s);
1842 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1844 struct wpa_supplicant *wpa_s = ctx;
1845 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1850 * DNS Header section is used only to calculate compression pointers, so the
1851 * contents of this data does not matter, but the length needs to be reserved
1852 * in the virtual packet.
1854 #define DNS_HEADER_LEN 12
1857 * 27-octet in-memory packet from P2P specification containing two implied
1858 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1860 #define P2P_SD_IN_MEMORY_LEN 27
1862 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1863 u8 **spos, const u8 *end)
1865 while (*spos < end) {
1866 u8 val = ((*spos)[0] & 0xc0) >> 6;
1869 if (val == 1 || val == 2) {
1870 /* These are reserved values in RFC 1035 */
1871 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1872 "sequence starting with 0x%x", val);
1881 if (*spos + 2 > end) {
1882 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1883 "DNS offset field");
1887 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1888 if (offset >= *spos - start) {
1889 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1890 "pointer offset %u", offset);
1895 spos_tmp = start + offset;
1896 return p2p_sd_dns_uncompress_label(upos, uend, start,
1902 len = (*spos)[0] & 0x3f;
1907 if (*spos + len > end) {
1908 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1909 "sequence - no room for label with length "
1914 if (*upos + len + 2 > uend)
1917 os_memcpy(*upos, *spos, len);
1929 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1930 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1931 * not large enough */
1932 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1933 size_t msg_len, size_t offset)
1935 /* 27-octet in-memory packet from P2P specification */
1936 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1937 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1938 u8 *tmp, *end, *spos;
1944 if (offset > msg_len)
1947 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1950 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1951 end = spos + msg_len;
1954 os_memset(tmp, 0, DNS_HEADER_LEN);
1955 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1956 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1959 uend = buf + buf_len;
1961 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1970 } else if (upos[-1] == '.')
1978 static struct p2p_srv_bonjour *
1979 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1980 const struct wpabuf *query)
1982 struct p2p_srv_bonjour *bsrv;
1985 len = wpabuf_len(query);
1986 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1987 struct p2p_srv_bonjour, list) {
1988 if (len == wpabuf_len(bsrv->query) &&
1989 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1997 static struct p2p_srv_upnp *
1998 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1999 const char *service)
2001 struct p2p_srv_upnp *usrv;
2003 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2004 struct p2p_srv_upnp, list) {
2005 if (version == usrv->version &&
2006 os_strcmp(service, usrv->service) == 0)
2013 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2018 if (wpabuf_tailroom(resp) < 5)
2021 /* Length (to be filled) */
2022 len_pos = wpabuf_put(resp, 2);
2023 wpabuf_put_u8(resp, srv_proto);
2024 wpabuf_put_u8(resp, srv_trans_id);
2026 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2027 /* Response Data: empty */
2028 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2032 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2033 struct wpabuf *resp, u8 srv_trans_id)
2035 struct p2p_srv_bonjour *bsrv;
2038 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2040 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2041 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2045 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2046 struct p2p_srv_bonjour, list) {
2047 if (wpabuf_tailroom(resp) <
2048 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2050 /* Length (to be filled) */
2051 len_pos = wpabuf_put(resp, 2);
2052 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2053 wpabuf_put_u8(resp, srv_trans_id);
2055 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2056 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2057 wpabuf_head(bsrv->resp),
2058 wpabuf_len(bsrv->resp));
2060 wpabuf_put_buf(resp, bsrv->query); /* Key */
2061 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2062 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2068 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2071 char str_rx[256], str_srv[256];
2073 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2074 return 0; /* Too short to include DNS Type and Version */
2075 if (os_memcmp(query + query_len - 3,
2076 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2078 return 0; /* Mismatch in DNS Type or Version */
2079 if (query_len == wpabuf_len(bsrv->query) &&
2080 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2081 return 1; /* Binary match */
2083 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2085 return 0; /* Failed to uncompress query */
2086 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2087 wpabuf_head(bsrv->query),
2088 wpabuf_len(bsrv->query) - 3, 0))
2089 return 0; /* Failed to uncompress service */
2091 return os_strcmp(str_rx, str_srv) == 0;
2095 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2096 struct wpabuf *resp, u8 srv_trans_id,
2097 const u8 *query, size_t query_len)
2099 struct p2p_srv_bonjour *bsrv;
2103 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2105 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2106 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2107 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2112 if (query_len == 0) {
2113 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2117 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2118 struct p2p_srv_bonjour, list) {
2119 if (!match_bonjour_query(bsrv, query, query_len))
2122 if (wpabuf_tailroom(resp) <
2123 5 + query_len + wpabuf_len(bsrv->resp))
2128 /* Length (to be filled) */
2129 len_pos = wpabuf_put(resp, 2);
2130 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2131 wpabuf_put_u8(resp, srv_trans_id);
2134 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2135 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2136 wpabuf_head(bsrv->resp),
2137 wpabuf_len(bsrv->resp));
2140 wpabuf_put_data(resp, query, query_len); /* Key */
2141 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2143 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2147 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2149 if (wpabuf_tailroom(resp) < 5)
2152 /* Length (to be filled) */
2153 len_pos = wpabuf_put(resp, 2);
2154 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2155 wpabuf_put_u8(resp, srv_trans_id);
2158 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2159 /* Response Data: empty */
2160 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2166 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2167 struct wpabuf *resp, u8 srv_trans_id)
2169 struct p2p_srv_upnp *usrv;
2172 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2174 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2175 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2179 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2180 struct p2p_srv_upnp, list) {
2181 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2184 /* Length (to be filled) */
2185 len_pos = wpabuf_put(resp, 2);
2186 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2187 wpabuf_put_u8(resp, srv_trans_id);
2190 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2192 wpabuf_put_u8(resp, usrv->version);
2193 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2195 wpabuf_put_str(resp, usrv->service);
2196 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2202 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2203 struct wpabuf *resp, u8 srv_trans_id,
2204 const u8 *query, size_t query_len)
2206 struct p2p_srv_upnp *usrv;
2212 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2215 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2216 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2217 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2222 if (query_len == 0) {
2223 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2227 if (wpabuf_tailroom(resp) < 5)
2230 /* Length (to be filled) */
2231 len_pos = wpabuf_put(resp, 2);
2232 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2233 wpabuf_put_u8(resp, srv_trans_id);
2236 str = os_malloc(query_len);
2239 os_memcpy(str, query + 1, query_len - 1);
2240 str[query_len - 1] = '\0';
2242 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2243 struct p2p_srv_upnp, list) {
2244 if (version != usrv->version)
2247 if (os_strcmp(str, "ssdp:all") != 0 &&
2248 os_strstr(usrv->service, str) == NULL)
2251 if (wpabuf_tailroom(resp) < 2)
2255 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2257 wpabuf_put_u8(resp, version);
2259 wpabuf_put_u8(resp, ',');
2263 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2265 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2267 wpabuf_put_str(resp, usrv->service);
2272 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2275 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2276 /* Response Data: empty */
2279 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2283 #ifdef CONFIG_WIFI_DISPLAY
2284 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2285 struct wpabuf *resp, u8 srv_trans_id,
2286 const u8 *query, size_t query_len)
2292 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2294 if (!wpa_s->global->wifi_display) {
2295 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2296 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2301 if (query_len < 1) {
2302 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2307 if (wpabuf_tailroom(resp) < 5)
2312 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2314 /* TODO: role specific handling */
2316 /* Length (to be filled) */
2317 len_pos = wpabuf_put(resp, 2);
2318 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2319 wpabuf_put_u8(resp, srv_trans_id);
2320 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2322 while (pos < query + query_len) {
2323 if (*pos < MAX_WFD_SUBELEMS &&
2324 wpa_s->global->wfd_subelem[*pos] &&
2325 wpabuf_tailroom(resp) >=
2326 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2327 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2328 "subelement %u", *pos);
2329 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2334 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2336 #endif /* CONFIG_WIFI_DISPLAY */
2339 static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2340 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2342 struct wpa_supplicant *wpa_s = ctx;
2343 const u8 *pos = tlvs;
2344 const u8 *end = tlvs + tlvs_len;
2347 struct wpabuf *resp;
2348 u8 srv_proto, srv_trans_id;
2352 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2354 buf_len = 2 * tlvs_len + 1;
2355 buf = os_malloc(buf_len);
2357 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2358 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2360 freq, MAC2STR(sa), dialog_token, update_indic,
2365 if (wpa_s->p2p_sd_over_ctrl_iface) {
2366 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2367 update_indic, tlvs, tlvs_len);
2368 return; /* to be processed by an external program */
2371 resp = wpabuf_alloc(10000);
2375 while (pos + 1 < end) {
2376 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2377 slen = WPA_GET_LE16(pos);
2379 if (pos + slen > end || slen < 2) {
2380 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2385 tlv_end = pos + slen;
2388 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2390 srv_trans_id = *pos++;
2391 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2394 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2395 pos, tlv_end - pos);
2398 if (wpa_s->force_long_sd) {
2399 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2401 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2402 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2406 switch (srv_proto) {
2407 case P2P_SERV_ALL_SERVICES:
2408 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2409 "for all services");
2410 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2411 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2412 wpa_printf(MSG_DEBUG, "P2P: No service "
2413 "discovery protocols available");
2414 wpas_sd_add_proto_not_avail(
2415 resp, P2P_SERV_ALL_SERVICES,
2419 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2420 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2422 case P2P_SERV_BONJOUR:
2423 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2424 pos, tlv_end - pos);
2427 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2428 pos, tlv_end - pos);
2430 #ifdef CONFIG_WIFI_DISPLAY
2431 case P2P_SERV_WIFI_DISPLAY:
2432 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2433 pos, tlv_end - pos);
2435 #endif /* CONFIG_WIFI_DISPLAY */
2437 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2438 "protocol %u", srv_proto);
2439 wpas_sd_add_proto_not_avail(resp, srv_proto,
2448 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2449 update_indic, tlvs, tlvs_len);
2451 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2457 static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2458 const u8 *tlvs, size_t tlvs_len)
2460 struct wpa_supplicant *wpa_s = ctx;
2461 const u8 *pos = tlvs;
2462 const u8 *end = tlvs + tlvs_len;
2468 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2470 if (tlvs_len > 1500) {
2471 /* TODO: better way for handling this */
2472 wpa_msg_ctrl(wpa_s, MSG_INFO,
2473 P2P_EVENT_SERV_DISC_RESP MACSTR
2474 " %u <long response: %u bytes>",
2475 MAC2STR(sa), update_indic,
2476 (unsigned int) tlvs_len);
2478 buf_len = 2 * tlvs_len + 1;
2479 buf = os_malloc(buf_len);
2481 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2482 wpa_msg_ctrl(wpa_s, MSG_INFO,
2483 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2484 MAC2STR(sa), update_indic, buf);
2490 u8 srv_proto, srv_trans_id, status;
2492 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2493 slen = WPA_GET_LE16(pos);
2495 if (pos + slen > end || slen < 3) {
2496 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2500 tlv_end = pos + slen;
2503 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2505 srv_trans_id = *pos++;
2506 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2509 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2512 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2513 pos, tlv_end - pos);
2518 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2522 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2523 const struct wpabuf *tlvs)
2525 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2527 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2531 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2532 u8 version, const char *query)
2534 struct wpabuf *tlvs;
2537 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2540 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2541 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2542 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2543 wpabuf_put_u8(tlvs, version);
2544 wpabuf_put_str(tlvs, query);
2545 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2551 #ifdef CONFIG_WIFI_DISPLAY
2553 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2554 const struct wpabuf *tlvs)
2556 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2558 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2562 #define MAX_WFD_SD_SUBELEMS 20
2564 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2565 const char *subelems)
2572 len = wpabuf_put(tlvs, 2);
2573 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2574 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2576 wpabuf_put_u8(tlvs, role);
2581 if (val >= 0 && val < 256) {
2582 wpabuf_put_u8(tlvs, val);
2584 if (count == MAX_WFD_SD_SUBELEMS)
2587 pos = os_strchr(pos + 1, ',');
2593 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2597 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2598 const u8 *dst, const char *role)
2600 struct wpabuf *tlvs;
2602 const char *subelems;
2605 subelems = os_strchr(role, ' ');
2606 if (subelems == NULL)
2610 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2614 if (os_strstr(role, "[source]"))
2615 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2616 if (os_strstr(role, "[pri-sink]"))
2617 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2618 if (os_strstr(role, "[sec-sink]"))
2619 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2620 if (os_strstr(role, "[source+sink]"))
2621 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2623 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2628 #endif /* CONFIG_WIFI_DISPLAY */
2631 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2633 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2635 return p2p_sd_cancel_request(wpa_s->global->p2p,
2636 (void *) (uintptr_t) req);
2640 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2641 const u8 *dst, u8 dialog_token,
2642 const struct wpabuf *resp_tlvs)
2644 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2646 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2651 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2653 if (wpa_s->global->p2p)
2654 p2p_sd_service_update(wpa_s->global->p2p);
2658 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2660 dl_list_del(&bsrv->list);
2661 wpabuf_free(bsrv->query);
2662 wpabuf_free(bsrv->resp);
2667 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2669 dl_list_del(&usrv->list);
2670 os_free(usrv->service);
2675 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2677 struct p2p_srv_bonjour *bsrv, *bn;
2678 struct p2p_srv_upnp *usrv, *un;
2680 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2681 struct p2p_srv_bonjour, list)
2682 wpas_p2p_srv_bonjour_free(bsrv);
2684 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2685 struct p2p_srv_upnp, list)
2686 wpas_p2p_srv_upnp_free(usrv);
2688 wpas_p2p_sd_service_update(wpa_s);
2692 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2693 struct wpabuf *query, struct wpabuf *resp)
2695 struct p2p_srv_bonjour *bsrv;
2697 bsrv = os_zalloc(sizeof(*bsrv));
2700 bsrv->query = query;
2702 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2704 wpas_p2p_sd_service_update(wpa_s);
2709 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2710 const struct wpabuf *query)
2712 struct p2p_srv_bonjour *bsrv;
2714 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2717 wpas_p2p_srv_bonjour_free(bsrv);
2718 wpas_p2p_sd_service_update(wpa_s);
2723 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2724 const char *service)
2726 struct p2p_srv_upnp *usrv;
2728 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2729 return 0; /* Already listed */
2730 usrv = os_zalloc(sizeof(*usrv));
2733 usrv->version = version;
2734 usrv->service = os_strdup(service);
2735 if (usrv->service == NULL) {
2739 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2741 wpas_p2p_sd_service_update(wpa_s);
2746 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2747 const char *service)
2749 struct p2p_srv_upnp *usrv;
2751 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2754 wpas_p2p_srv_upnp_free(usrv);
2755 wpas_p2p_sd_service_update(wpa_s);
2760 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2761 const u8 *peer, const char *params,
2762 unsigned int generated_pin)
2764 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2765 " %08d%s", MAC2STR(peer), generated_pin, params);
2769 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2770 const u8 *peer, const char *params)
2772 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2773 "%s", MAC2STR(peer), params);
2777 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2778 const u8 *dev_addr, const u8 *pri_dev_type,
2779 const char *dev_name, u16 supp_config_methods,
2780 u8 dev_capab, u8 group_capab, const u8 *group_id,
2781 size_t group_id_len)
2783 struct wpa_supplicant *wpa_s = ctx;
2784 char devtype[WPS_DEV_TYPE_BUFSIZE];
2786 u8 empty_dev_type[8];
2787 unsigned int generated_pin = 0;
2788 struct wpa_supplicant *group = NULL;
2791 for (group = wpa_s->global->ifaces; group; group = group->next)
2793 struct wpa_ssid *s = group->current_ssid;
2795 s->mode == WPAS_MODE_P2P_GO &&
2796 group_id_len - ETH_ALEN == s->ssid_len &&
2797 os_memcmp(group_id + ETH_ALEN, s->ssid,
2803 if (pri_dev_type == NULL) {
2804 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2805 pri_dev_type = empty_dev_type;
2807 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2808 " pri_dev_type=%s name='%s' config_methods=0x%x "
2809 "dev_capab=0x%x group_capab=0x%x%s%s",
2811 wps_dev_type_bin2str(pri_dev_type, devtype,
2813 dev_name, supp_config_methods, dev_capab, group_capab,
2814 group ? " group=" : "",
2815 group ? group->ifname : "");
2816 params[sizeof(params) - 1] = '\0';
2818 if (config_methods & WPS_CONFIG_DISPLAY) {
2819 generated_pin = wps_generate_pin();
2820 wpas_prov_disc_local_display(wpa_s, peer, params,
2822 } else if (config_methods & WPS_CONFIG_KEYPAD)
2823 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2824 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2825 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2826 MACSTR "%s", MAC2STR(peer), params);
2828 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2829 P2P_PROV_DISC_SUCCESS,
2830 config_methods, generated_pin);
2834 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2836 struct wpa_supplicant *wpa_s = ctx;
2837 unsigned int generated_pin = 0;
2840 if (wpa_s->pending_pd_before_join &&
2841 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2842 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2843 wpa_s->pending_pd_before_join = 0;
2844 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2845 "join-existing-group operation");
2846 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2850 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2851 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2852 os_snprintf(params, sizeof(params), " peer_go=%d",
2853 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2857 if (config_methods & WPS_CONFIG_DISPLAY)
2858 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2859 else if (config_methods & WPS_CONFIG_KEYPAD) {
2860 generated_pin = wps_generate_pin();
2861 wpas_prov_disc_local_display(wpa_s, peer, params,
2863 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2864 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2865 MACSTR "%s", MAC2STR(peer), params);
2867 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2868 P2P_PROV_DISC_SUCCESS,
2869 config_methods, generated_pin);
2873 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2874 enum p2p_prov_disc_status status)
2876 struct wpa_supplicant *wpa_s = ctx;
2878 if (wpa_s->p2p_fallback_to_go_neg) {
2879 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2880 "failed - fall back to GO Negotiation");
2881 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2885 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2886 wpa_s->pending_pd_before_join = 0;
2887 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2888 "join-existing-group operation (no ACK for PD "
2890 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2894 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2895 " p2p_dev_addr=" MACSTR " status=%d",
2896 MAC2STR(peer), status);
2898 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2903 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2905 if (channels == NULL)
2906 return 1; /* Assume no restrictions */
2907 return p2p_channels_includes_freq(channels, freq);
2912 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2913 const u8 *go_dev_addr, const u8 *ssid,
2914 size_t ssid_len, int *go, u8 *group_bssid,
2915 int *force_freq, int persistent_group,
2916 const struct p2p_channels *channels,
2919 struct wpa_supplicant *wpa_s = ctx;
2922 struct wpa_supplicant *grp;
2924 if (!persistent_group) {
2925 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2926 " to join an active group (SSID: %s)",
2927 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
2928 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2929 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2931 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2932 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2933 "authorized invitation");
2937 #ifdef CONFIG_WPS_NFC
2938 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
2939 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
2940 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2941 wpa_s->parent->p2p_wps_method = WPS_NFC;
2942 wpa_s->parent->pending_join_wps_method = WPS_NFC;
2943 os_memcpy(wpa_s->parent->pending_join_dev_addr,
2944 go_dev_addr, ETH_ALEN);
2945 os_memcpy(wpa_s->parent->pending_join_iface_addr,
2949 #endif /* CONFIG_WPS_NFC */
2952 * Do not accept the invitation automatically; notify user and
2955 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2958 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2960 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2961 "running persistent group");
2963 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2967 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2968 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2969 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2970 "invitation to re-invoke a persistent group");
2971 } else if (!wpa_s->conf->persistent_reconnect)
2972 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2974 for (s = wpa_s->conf->ssid; s; s = s->next) {
2975 if (s->disabled == 2 &&
2976 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2977 s->ssid_len == ssid_len &&
2978 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2983 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2984 " requested reinvocation of an unknown group",
2986 return P2P_SC_FAIL_UNKNOWN_GROUP;
2989 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2991 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2992 wpa_printf(MSG_DEBUG, "P2P: The only available "
2993 "interface is already in use - reject "
2995 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2997 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2998 } else if (s->mode == WPAS_MODE_P2P_GO) {
3000 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3002 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3003 "interface address for the group");
3004 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3006 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3011 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3013 /* Get one of the frequencies currently in use */
3014 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
3015 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3016 wpas_p2p_set_own_freq_preference(wpa_s, res);
3018 if (wpa_s->num_multichan_concurrent < 2 ||
3019 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3020 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3025 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3026 wpas_p2p_num_unused_channels(wpa_s) > 0) {
3028 /* We are the client */
3029 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3030 "running a GO but we are capable of MCC, "
3031 "figure out the best channel to use");
3033 } else if (!freq_included(channels, *force_freq)) {
3034 /* We are the GO, and *force_freq is not in the
3036 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3037 "in intersection but we are capable of MCC, "
3038 "figure out the best channel to use",
3044 return P2P_SC_SUCCESS;
3048 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3049 const u8 *ssid, size_t ssid_len,
3050 const u8 *go_dev_addr, u8 status,
3053 struct wpa_supplicant *wpa_s = ctx;
3056 for (s = wpa_s->conf->ssid; s; s = s->next) {
3057 if (s->disabled == 2 &&
3058 s->ssid_len == ssid_len &&
3059 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3063 if (status == P2P_SC_SUCCESS) {
3064 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3065 " was accepted; op_freq=%d MHz, SSID=%s",
3066 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3068 int go = s->mode == WPAS_MODE_P2P_GO;
3069 wpas_p2p_group_add_persistent(
3070 wpa_s, s, go, 0, go ? op_freq : 0, 0, 0, NULL,
3071 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3073 wpa_s->user_initiated_pd = 0;
3074 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
3075 wpa_s->p2p_wps_method, 0, op_freq,
3081 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3082 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3083 " was rejected (status %u)", MAC2STR(sa), status);
3089 wpa_msg_global(wpa_s, MSG_INFO,
3090 P2P_EVENT_INVITATION_RECEIVED
3091 "sa=" MACSTR " go_dev_addr=" MACSTR
3092 " bssid=" MACSTR " unknown-network",
3093 MAC2STR(sa), MAC2STR(go_dev_addr),
3096 wpa_msg_global(wpa_s, MSG_INFO,
3097 P2P_EVENT_INVITATION_RECEIVED
3098 "sa=" MACSTR " go_dev_addr=" MACSTR
3100 MAC2STR(sa), MAC2STR(go_dev_addr));
3105 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
3106 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3107 "sa=" MACSTR " persistent=%d freq=%d",
3108 MAC2STR(sa), s->id, op_freq);
3110 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3111 "sa=" MACSTR " persistent=%d",
3112 MAC2STR(sa), s->id);
3117 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3118 struct wpa_ssid *ssid,
3119 const u8 *peer, int inv)
3126 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3127 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3131 if (i >= ssid->num_p2p_clients) {
3132 if (ssid->mode != WPAS_MODE_P2P_GO &&
3133 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3134 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3135 "due to invitation result", ssid->id);
3136 wpas_notify_network_removed(wpa_s, ssid);
3137 wpa_config_remove_network(wpa_s->conf, ssid->id);
3140 return; /* Peer not found in client list */
3143 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
3144 "group %d client list%s",
3145 MAC2STR(peer), ssid->id,
3146 inv ? " due to invitation result" : "");
3147 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3148 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3149 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3150 ssid->num_p2p_clients--;
3151 if (wpa_s->parent->conf->update_config &&
3152 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3153 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3157 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3160 struct wpa_ssid *ssid;
3162 wpa_s = wpa_s->global->p2p_invite_group;
3164 return; /* No known invitation group */
3165 ssid = wpa_s->current_ssid;
3166 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3167 !ssid->p2p_persistent_group)
3168 return; /* Not operating as a GO in persistent group */
3169 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3170 ssid->ssid, ssid->ssid_len);
3171 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3175 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
3176 const struct p2p_channels *channels,
3177 const u8 *peer, int neg_freq)
3179 struct wpa_supplicant *wpa_s = ctx;
3180 struct wpa_ssid *ssid;
3183 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3184 "status=%d " MACSTR,
3185 status, MAC2STR(bssid));
3187 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3188 "status=%d ", status);
3190 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3192 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3193 status, MAC2STR(peer));
3194 if (wpa_s->pending_invite_ssid_id == -1) {
3195 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3196 wpas_remove_persistent_client(wpa_s, peer);
3197 return; /* Invitation to active group */
3200 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3201 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3202 "invitation exchange to indicate readiness for "
3206 if (status != P2P_SC_SUCCESS) {
3207 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3208 ssid = wpa_config_get_network(
3209 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3210 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3212 wpas_p2p_remove_pending_group_interface(wpa_s);
3216 ssid = wpa_config_get_network(wpa_s->conf,
3217 wpa_s->pending_invite_ssid_id);
3219 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3220 "data matching with invitation");
3225 * The peer could have missed our ctrl::ack frame for Invitation
3226 * Response and continue retransmitting the frame. To reduce the
3227 * likelihood of the peer not getting successful TX status for the
3228 * Invitation Response frame, wait a short time here before starting
3229 * the persistent group so that we will remain on the current channel to
3230 * acknowledge any possible retransmission from the peer.
3232 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3233 "starting persistent group");
3236 wpas_p2p_group_add_persistent(wpa_s, ssid,
3237 ssid->mode == WPAS_MODE_P2P_GO,
3238 wpa_s->p2p_persistent_go_freq,
3240 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3242 ssid->mode == WPAS_MODE_P2P_GO ?
3243 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3248 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3251 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3253 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3257 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3259 reg->channel[reg->channels] = chan;
3264 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3265 struct p2p_channels *chan,
3266 struct p2p_channels *cli_chan)
3270 os_memset(cli_chan, 0, sizeof(*cli_chan));
3272 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3275 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3276 chan->reg_class[cla].reg_class = 81;
3277 chan->reg_class[cla].channels = 0;
3278 for (i = 0; i < 11; i++) {
3279 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3280 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3282 if (chan->reg_class[cla].channels)
3285 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3288 /* Operating class 115 - 5 GHz, channels 36-48 */
3289 chan->reg_class[cla].reg_class = 115;
3290 chan->reg_class[cla].channels = 0;
3291 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3292 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3293 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3294 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3295 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3296 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3297 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3298 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3299 if (chan->reg_class[cla].channels)
3302 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3305 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3306 chan->reg_class[cla].reg_class = 124;
3307 chan->reg_class[cla].channels = 0;
3308 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3309 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3310 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3311 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3312 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3313 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3314 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3315 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3316 if (chan->reg_class[cla].channels)
3319 chan->reg_classes = cla;
3324 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3326 enum hostapd_hw_mode mode)
3330 for (i = 0; i < num_modes; i++) {
3331 if (modes[i].mode == mode)
3340 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3343 static int has_channel(struct wpa_global *global,
3344 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3349 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3351 if (wpas_p2p_disallowed_freq(global, freq))
3354 for (i = 0; i < mode->num_channels; i++) {
3355 if (mode->channels[i].chan == chan) {
3357 *flags = mode->channels[i].flag;
3358 if (mode->channels[i].flag &
3359 (HOSTAPD_CHAN_DISABLED |
3360 HOSTAPD_CHAN_RADAR))
3362 if (mode->channels[i].flag &
3363 (HOSTAPD_CHAN_PASSIVE_SCAN |
3364 HOSTAPD_CHAN_NO_IBSS))
3365 return PASSIVE_ONLY;
3374 struct p2p_oper_class_map {
3375 enum hostapd_hw_mode mode;
3380 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
3383 static struct p2p_oper_class_map op_class[] = {
3384 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3385 #if 0 /* Do not enable HT40 on 2 GHz for now */
3386 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3387 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3389 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3390 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3391 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3392 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3393 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3394 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3397 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3398 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3399 * 80 MHz, but currently use the following definition for simplicity
3400 * (these center frequencies are not actual channels, which makes
3401 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3402 * removing invalid channels.
3404 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3405 { -1, 0, 0, 0, 0, BW20 }
3409 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3410 struct hostapd_hw_modes *mode,
3413 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3416 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3419 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3421 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3422 * so the center channel is 6 channels away from the start/end.
3424 if (channel >= center_channels[i] - 6 &&
3425 channel <= center_channels[i] + 6)
3426 return center_channels[i];
3432 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3433 struct hostapd_hw_modes *mode,
3438 enum chan_allowed res, ret = ALLOWED;
3440 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3443 if (center_chan >= 58 && center_chan <= 138)
3444 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3446 /* check all the channels are available */
3447 for (i = 0; i < 4; i++) {
3448 int adj_chan = center_chan - 6 + i * 4;
3450 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3451 if (res == NOT_ALLOWED)
3453 if (res == PASSIVE_ONLY)
3456 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3458 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3460 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3462 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3470 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3471 struct hostapd_hw_modes *mode,
3475 enum chan_allowed res, res2;
3477 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3478 if (bw == BW40MINUS) {
3479 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3481 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3482 } else if (bw == BW40PLUS) {
3483 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3485 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3486 } else if (bw == BW80) {
3487 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3490 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3492 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3493 return PASSIVE_ONLY;
3498 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3499 struct p2p_channels *chan,
3500 struct p2p_channels *cli_chan)
3502 struct hostapd_hw_modes *mode;
3503 int cla, op, cli_cla;
3505 if (wpa_s->hw.modes == NULL) {
3506 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3507 "of all supported channels; assume dualband "
3509 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3514 for (op = 0; op_class[op].op_class; op++) {
3515 struct p2p_oper_class_map *o = &op_class[op];
3517 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3519 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3522 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3523 enum chan_allowed res;
3524 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3525 if (res == ALLOWED) {
3527 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3529 reg = &chan->reg_class[cla];
3531 reg->reg_class = o->op_class;
3533 reg->channel[reg->channels] = ch;
3535 } else if (res == PASSIVE_ONLY &&
3536 wpa_s->conf->p2p_add_cli_chan) {
3537 if (cli_reg == NULL) {
3538 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3540 cli_reg = &cli_chan->reg_class[cli_cla];
3542 cli_reg->reg_class = o->op_class;
3544 cli_reg->channel[cli_reg->channels] = ch;
3545 cli_reg->channels++;
3549 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3550 reg->channel, reg->channels);
3553 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3554 cli_reg->channel, cli_reg->channels);
3558 chan->reg_classes = cla;
3559 cli_chan->reg_classes = cli_cla;
3565 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3566 struct hostapd_hw_modes *mode, u8 channel)
3569 enum chan_allowed ret;
3571 for (op = 0; op_class[op].op_class; op++) {
3572 struct p2p_oper_class_map *o = &op_class[op];
3575 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3576 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3577 o->bw == BW20 || ch != channel)
3579 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3581 return (o->bw == BW40MINUS) ? -1 : 1;
3588 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3589 struct hostapd_hw_modes *mode, u8 channel)
3591 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3594 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3598 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3601 struct wpa_supplicant *wpa_s = ctx;
3603 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3604 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3610 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3614 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3616 struct wpa_supplicant *wpa_s = ctx;
3618 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3619 struct wpa_ssid *ssid = wpa_s->current_ssid;
3622 if (ssid->mode != WPAS_MODE_INFRA)
3624 if (wpa_s->wpa_state != WPA_COMPLETED &&
3625 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3627 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3635 static int wpas_is_concurrent_session_active(void *ctx)
3637 struct wpa_supplicant *wpa_s = ctx;
3638 struct wpa_supplicant *ifs;
3640 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3643 if (ifs->wpa_state > WPA_ASSOCIATED)
3650 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3652 struct wpa_supplicant *wpa_s = ctx;
3653 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3657 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3659 struct wpa_interface iface;
3660 struct wpa_supplicant *p2pdev_wpa_s;
3662 char force_name[100];
3665 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3667 force_name[0] = '\0';
3668 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3669 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3670 force_name, wpa_s->pending_interface_addr, NULL);
3672 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3675 os_strlcpy(wpa_s->pending_interface_name, ifname,
3676 sizeof(wpa_s->pending_interface_name));
3678 os_memset(&iface, 0, sizeof(iface));
3680 iface.ifname = wpa_s->pending_interface_name;
3681 iface.driver = wpa_s->driver->name;
3682 iface.driver_param = wpa_s->conf->driver_param;
3683 iface.confname = wpa_s->confname;
3684 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3685 if (!p2pdev_wpa_s) {
3686 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3689 p2pdev_wpa_s->parent = wpa_s;
3691 wpa_s->pending_interface_name[0] = '\0';
3696 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3697 const u8 *noa, size_t noa_len)
3699 struct wpa_supplicant *wpa_s, *intf = ctx;
3702 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3703 if (wpa_s->waiting_presence_resp)
3707 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3710 wpa_s->waiting_presence_resp = 0;
3712 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3713 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3714 " status=%u noa=%s", MAC2STR(src), status, hex);
3719 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3720 * @global: Pointer to global data from wpa_supplicant_init()
3721 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3722 * Returns: 0 on success, -1 on failure
3724 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3726 struct p2p_config p2p;
3730 if (wpa_s->conf->p2p_disabled)
3733 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3739 os_memset(&p2p, 0, sizeof(p2p));
3741 p2p.debug_print = wpas_p2p_debug_print;
3742 p2p.p2p_scan = wpas_p2p_scan;
3743 p2p.send_action = wpas_send_action;
3744 p2p.send_action_done = wpas_send_action_done;
3745 p2p.go_neg_completed = wpas_go_neg_completed;
3746 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3747 p2p.dev_found = wpas_dev_found;
3748 p2p.dev_lost = wpas_dev_lost;
3749 p2p.find_stopped = wpas_find_stopped;
3750 p2p.start_listen = wpas_start_listen;
3751 p2p.stop_listen = wpas_stop_listen;
3752 p2p.send_probe_resp = wpas_send_probe_resp;
3753 p2p.sd_request = wpas_sd_request;
3754 p2p.sd_response = wpas_sd_response;
3755 p2p.prov_disc_req = wpas_prov_disc_req;
3756 p2p.prov_disc_resp = wpas_prov_disc_resp;
3757 p2p.prov_disc_fail = wpas_prov_disc_fail;
3758 p2p.invitation_process = wpas_invitation_process;
3759 p2p.invitation_received = wpas_invitation_received;
3760 p2p.invitation_result = wpas_invitation_result;
3761 p2p.get_noa = wpas_get_noa;
3762 p2p.go_connected = wpas_go_connected;
3763 p2p.presence_resp = wpas_presence_resp;
3764 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
3766 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
3767 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
3768 p2p.dev_name = wpa_s->conf->device_name;
3769 p2p.manufacturer = wpa_s->conf->manufacturer;
3770 p2p.model_name = wpa_s->conf->model_name;
3771 p2p.model_number = wpa_s->conf->model_number;
3772 p2p.serial_number = wpa_s->conf->serial_number;
3774 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3775 p2p.config_methods = wpa_s->wps->config_methods;
3778 if (wpa_s->conf->p2p_listen_reg_class &&
3779 wpa_s->conf->p2p_listen_channel) {
3780 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3781 p2p.channel = wpa_s->conf->p2p_listen_channel;
3785 * Pick one of the social channels randomly as the listen
3788 os_get_random((u8 *) &r, sizeof(r));
3789 p2p.channel = 1 + (r % 3) * 5;
3791 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3793 if (wpa_s->conf->p2p_oper_reg_class &&
3794 wpa_s->conf->p2p_oper_channel) {
3795 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3796 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3797 p2p.cfg_op_channel = 1;
3798 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3799 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3802 p2p.op_reg_class = 81;
3804 * Use random operation channel from (1, 6, 11) if no other
3805 * preference is indicated.
3807 os_get_random((u8 *) &r, sizeof(r));
3808 p2p.op_channel = 1 + (r % 3) * 5;
3809 p2p.cfg_op_channel = 0;
3810 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3811 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3814 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3815 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3816 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3819 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3820 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3821 p2p.country[2] = 0x04;
3823 os_memcpy(p2p.country, "XX\x04", 3);
3825 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3826 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3831 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3834 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3835 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3836 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3838 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3839 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3841 p2p.max_peers = 100;
3843 if (wpa_s->conf->p2p_ssid_postfix) {
3844 p2p.ssid_postfix_len =
3845 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3846 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3847 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3848 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3849 p2p.ssid_postfix_len);
3852 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3854 p2p.max_listen = wpa_s->max_remain_on_chan;
3856 global->p2p = p2p_init(&p2p);
3857 if (global->p2p == NULL)
3859 global->p2p_init_wpa_s = wpa_s;
3861 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3862 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3864 p2p_add_wps_vendor_extension(
3865 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3868 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3875 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3876 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3878 * This function deinitialize per-interface P2P data.
3880 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3882 if (wpa_s->driver && wpa_s->drv_priv)
3883 wpa_drv_probe_req_report(wpa_s, 0);
3885 if (wpa_s->go_params) {
3886 /* Clear any stored provisioning info */
3887 p2p_clear_provisioning_info(
3889 wpa_s->go_params->peer_device_addr);
3892 os_free(wpa_s->go_params);
3893 wpa_s->go_params = NULL;
3894 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3895 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3896 wpa_s->p2p_long_listen = 0;
3897 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3898 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3899 wpas_p2p_remove_pending_group_interface(wpa_s);
3900 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3901 wpas_p2p_listen_work_done(wpa_s);
3902 if (wpa_s->p2p_send_action_work) {
3903 os_free(wpa_s->p2p_send_action_work->ctx);
3904 radio_work_done(wpa_s->p2p_send_action_work);
3905 wpa_s->p2p_send_action_work = NULL;
3907 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
3909 wpabuf_free(wpa_s->p2p_oob_dev_pw);
3910 wpa_s->p2p_oob_dev_pw = NULL;
3912 /* TODO: remove group interface from the driver if this wpa_s instance
3913 * is on top of a P2P group interface */
3918 * wpas_p2p_deinit_global - Deinitialize global P2P module
3919 * @global: Pointer to global data from wpa_supplicant_init()
3921 * This function deinitializes the global (per device) P2P module.
3923 void wpas_p2p_deinit_global(struct wpa_global *global)
3925 struct wpa_supplicant *wpa_s, *tmp;
3927 wpa_s = global->ifaces;
3929 wpas_p2p_service_flush(wpa_s);
3931 if (global->p2p == NULL)
3934 /* Remove remaining P2P group interfaces */
3935 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3936 wpa_s = wpa_s->next;
3938 tmp = global->ifaces;
3941 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3946 /* Disconnect from the P2P group and deinit the interface */
3947 wpas_p2p_disconnect(tmp);
3951 * Deinit GO data on any possibly remaining interface (if main
3952 * interface is used as GO).
3954 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3955 if (wpa_s->ap_iface)
3956 wpas_p2p_group_deinit(wpa_s);
3959 p2p_deinit(global->p2p);
3961 global->p2p_init_wpa_s = NULL;
3965 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3967 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3968 wpa_s->conf->p2p_no_group_iface)
3969 return 0; /* separate interface disabled per configuration */
3970 if (wpa_s->drv_flags &
3971 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3972 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3973 return 1; /* P2P group requires a new interface in every case
3975 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3976 return 0; /* driver does not support concurrent operations */
3977 if (wpa_s->global->ifaces->next)
3978 return 1; /* more that one interface already in use */
3979 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3980 return 1; /* this interface is already in use */
3985 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3986 const u8 *peer_addr,
3987 enum p2p_wps_method wps_method,
3988 int go_intent, const u8 *own_interface_addr,
3989 unsigned int force_freq, int persistent_group,
3990 struct wpa_ssid *ssid, unsigned int pref_freq)
3992 if (persistent_group && wpa_s->conf->persistent_reconnect)
3993 persistent_group = 2;
3996 * Increase GO config timeout if HT40 is used since it takes some time
3997 * to scan channels for coex purposes before the BSS can be started.
3999 p2p_set_config_timeout(wpa_s->global->p2p,
4000 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4002 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4003 go_intent, own_interface_addr, force_freq,
4004 persistent_group, ssid ? ssid->ssid : NULL,
4005 ssid ? ssid->ssid_len : 0,
4006 wpa_s->p2p_pd_before_go_neg, pref_freq,
4007 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4012 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4013 const u8 *peer_addr,
4014 enum p2p_wps_method wps_method,
4015 int go_intent, const u8 *own_interface_addr,
4016 unsigned int force_freq, int persistent_group,
4017 struct wpa_ssid *ssid, unsigned int pref_freq)
4019 if (persistent_group && wpa_s->conf->persistent_reconnect)
4020 persistent_group = 2;
4022 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4023 go_intent, own_interface_addr, force_freq,
4024 persistent_group, ssid ? ssid->ssid : NULL,
4025 ssid ? ssid->ssid_len : 0, pref_freq,
4026 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4031 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4033 wpa_s->p2p_join_scan_count++;
4034 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4035 wpa_s->p2p_join_scan_count);
4036 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4037 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4038 " for join operationg - stop join attempt",
4039 MAC2STR(wpa_s->pending_join_iface_addr));
4040 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4041 if (wpa_s->p2p_auto_pd) {
4042 wpa_s->p2p_auto_pd = 0;
4043 wpa_msg_global(wpa_s, MSG_INFO,
4044 P2P_EVENT_PROV_DISC_FAILURE
4045 " p2p_dev_addr=" MACSTR " status=N/A",
4046 MAC2STR(wpa_s->pending_join_dev_addr));
4049 wpa_msg_global(wpa_s->parent, MSG_INFO,
4050 P2P_EVENT_GROUP_FORMATION_FAILURE);
4055 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4057 int *freqs, res, num, i;
4059 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4060 /* Multiple channels are supported and not all are in use */
4064 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4068 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4069 wpa_s->num_multichan_concurrent);
4075 for (i = 0; i < num; i++) {
4076 if (freqs[i] == freq) {
4077 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4092 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4093 const u8 *peer_dev_addr)
4095 struct wpa_bss *bss;
4098 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4101 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4102 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4107 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4109 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4110 "%ld.%06ld (%supdated in last scan)",
4111 bss->last_update.sec, bss->last_update.usec,
4112 updated ? "": "not ");
4118 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4119 struct wpa_scan_results *scan_res)
4121 struct wpa_bss *bss = NULL;
4123 u8 iface_addr[ETH_ALEN];
4125 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4127 if (wpa_s->global->p2p_disabled)
4130 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4131 scan_res ? (int) scan_res->num : -1,
4132 wpa_s->p2p_auto_join ? "auto_" : "");
4135 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4137 if (wpa_s->p2p_auto_pd) {
4138 int join = wpas_p2p_peer_go(wpa_s,
4139 wpa_s->pending_join_dev_addr);
4141 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4142 wpa_s->auto_pd_scan_retry++;
4143 bss = wpa_bss_get_bssid_latest(
4144 wpa_s, wpa_s->pending_join_dev_addr);
4147 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4148 "the peer " MACSTR " at %d MHz",
4149 wpa_s->auto_pd_scan_retry,
4151 pending_join_dev_addr),
4153 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4161 wpa_s->p2p_auto_pd = 0;
4162 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4163 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4164 MAC2STR(wpa_s->pending_join_dev_addr), join);
4165 if (p2p_prov_disc_req(wpa_s->global->p2p,
4166 wpa_s->pending_join_dev_addr,
4167 wpa_s->pending_pd_config_methods, join,
4168 0, wpa_s->user_initiated_pd) < 0) {
4169 wpa_s->p2p_auto_pd = 0;
4170 wpa_msg_global(wpa_s, MSG_INFO,
4171 P2P_EVENT_PROV_DISC_FAILURE
4172 " p2p_dev_addr=" MACSTR " status=N/A",
4173 MAC2STR(wpa_s->pending_join_dev_addr));
4178 if (wpa_s->p2p_auto_join) {
4179 int join = wpas_p2p_peer_go(wpa_s,
4180 wpa_s->pending_join_dev_addr);
4182 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4183 "running a GO -> use GO Negotiation");
4184 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4185 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4186 wpa_s->p2p_persistent_group, 0, 0, 0,
4187 wpa_s->p2p_go_intent,
4188 wpa_s->p2p_connect_freq,
4189 wpa_s->p2p_persistent_id,
4190 wpa_s->p2p_pd_before_go_neg,
4196 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4197 "try to join the group", join ? "" :
4200 wpa_s->p2p_fallback_to_go_neg = 1;
4203 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4204 wpa_s->pending_join_iface_addr);
4206 p2p_get_interface_addr(wpa_s->global->p2p,
4207 wpa_s->pending_join_dev_addr,
4209 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4211 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4212 "address for join from " MACSTR " to " MACSTR
4213 " based on newly discovered P2P peer entry",
4214 MAC2STR(wpa_s->pending_join_iface_addr),
4215 MAC2STR(iface_addr));
4216 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4219 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4220 wpa_s->pending_join_iface_addr);
4223 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4224 "from P2P peer table: %d MHz", freq);
4226 if (wpa_s->p2p_join_ssid_len) {
4227 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4228 MACSTR " and SSID %s",
4229 MAC2STR(wpa_s->pending_join_iface_addr),
4230 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4231 wpa_s->p2p_join_ssid_len));
4232 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4233 wpa_s->p2p_join_ssid,
4234 wpa_s->p2p_join_ssid_len);
4237 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4238 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4239 bss = wpa_bss_get_bssid_latest(wpa_s,
4240 wpa_s->pending_join_iface_addr);
4244 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4245 "from BSS table: %d MHz (SSID %s)", freq,
4246 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4251 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4252 wpa_msg_global(wpa_s->parent, MSG_INFO,
4253 P2P_EVENT_GROUP_FORMATION_FAILURE
4254 "reason=FREQ_CONFLICT");
4258 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4259 "prior to joining an existing group (GO " MACSTR
4261 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4262 wpa_s->pending_pd_before_join = 1;
4264 switch (wpa_s->pending_join_wps_method) {
4265 case WPS_PIN_DISPLAY:
4266 method = WPS_CONFIG_KEYPAD;
4268 case WPS_PIN_KEYPAD:
4269 method = WPS_CONFIG_DISPLAY;
4272 method = WPS_CONFIG_PUSHBUTTON;
4279 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4280 wpa_s->pending_join_dev_addr) ==
4283 * We have already performed provision discovery for
4284 * joining the group. Proceed directly to join
4285 * operation without duplicated provision discovery. */
4286 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4287 "with " MACSTR " already done - proceed to "
4289 MAC2STR(wpa_s->pending_join_dev_addr));
4290 wpa_s->pending_pd_before_join = 0;
4294 if (p2p_prov_disc_req(wpa_s->global->p2p,
4295 wpa_s->pending_join_dev_addr, method, 1,
4296 freq, wpa_s->user_initiated_pd) < 0) {
4297 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4298 "Discovery Request before joining an "
4300 wpa_s->pending_pd_before_join = 0;
4306 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4307 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4308 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4309 wpas_p2p_check_join_scan_limit(wpa_s);
4313 /* Start join operation immediately */
4314 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4318 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4319 const u8 *ssid, size_t ssid_len)
4322 struct wpa_driver_scan_params params;
4323 struct wpabuf *wps_ie, *ies;
4325 int freqs[2] = { 0, 0 };
4327 os_memset(¶ms, 0, sizeof(params));
4329 /* P2P Wildcard SSID */
4330 params.num_ssids = 1;
4331 if (ssid && ssid_len) {
4332 params.ssids[0].ssid = ssid;
4333 params.ssids[0].ssid_len = ssid_len;
4334 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4335 wpa_s->p2p_join_ssid_len = ssid_len;
4337 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4338 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4339 wpa_s->p2p_join_ssid_len = 0;
4342 wpa_s->wps->dev.p2p = 1;
4343 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4344 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4346 if (wps_ie == NULL) {
4347 wpas_p2p_scan_res_join(wpa_s, NULL);
4351 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4352 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4354 wpabuf_free(wps_ie);
4355 wpas_p2p_scan_res_join(wpa_s, NULL);
4358 wpabuf_put_buf(ies, wps_ie);
4359 wpabuf_free(wps_ie);
4361 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4363 params.p2p_probe = 1;
4364 params.extra_ies = wpabuf_head(ies);
4365 params.extra_ies_len = wpabuf_len(ies);
4370 * If freq is not provided, check the operating freq of the GO
4371 * and use a single channel scan on if possible.
4373 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4374 wpa_s->pending_join_iface_addr);
4380 params.freqs = freqs;
4384 * Run a scan to update BSS table and start Provision Discovery once
4385 * the new scan results become available.
4387 ret = wpa_drv_scan(wpa_s, ¶ms);
4389 os_get_reltime(&wpa_s->scan_trigger_time);
4390 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4391 wpa_s->own_scan_requested = 1;
4397 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4399 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4400 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4401 wpas_p2p_check_join_scan_limit(wpa_s);
4406 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4408 struct wpa_supplicant *wpa_s = eloop_ctx;
4409 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4413 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4414 const u8 *dev_addr, enum p2p_wps_method wps_method,
4415 int auto_join, int op_freq,
4416 const u8 *ssid, size_t ssid_len)
4418 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4419 MACSTR " dev " MACSTR " op_freq=%d)%s",
4420 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4421 auto_join ? " (auto_join)" : "");
4422 if (ssid && ssid_len) {
4423 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4424 wpa_ssid_txt(ssid, ssid_len));
4427 wpa_s->p2p_auto_pd = 0;
4428 wpa_s->p2p_auto_join = !!auto_join;
4429 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4430 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4431 wpa_s->pending_join_wps_method = wps_method;
4433 /* Make sure we are not running find during connection establishment */
4434 wpas_p2p_stop_find(wpa_s);
4436 wpa_s->p2p_join_scan_count = 0;
4437 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4442 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4443 const u8 *ssid, size_t ssid_len)
4445 struct wpa_supplicant *group;
4446 struct p2p_go_neg_results res;
4447 struct wpa_bss *bss;
4449 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4452 if (group != wpa_s) {
4453 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4454 sizeof(group->p2p_pin));
4455 group->p2p_wps_method = wpa_s->p2p_wps_method;
4458 * Need to mark the current interface for p2p_group_formation
4459 * when a separate group interface is not used. This is needed
4460 * to allow p2p_cancel stop a pending p2p_connect-join.
4461 * wpas_p2p_init_group_interface() addresses this for the case
4462 * where a separate group interface is used.
4464 wpa_s->global->p2p_group_formation = wpa_s;
4467 group->p2p_in_provisioning = 1;
4468 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4470 os_memset(&res, 0, sizeof(res));
4471 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4472 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4474 res.wps_method = wpa_s->pending_join_wps_method;
4475 if (freq && ssid && ssid_len) {
4477 res.ssid_len = ssid_len;
4478 os_memcpy(res.ssid, ssid, ssid_len);
4480 bss = wpa_bss_get_bssid_latest(wpa_s,
4481 wpa_s->pending_join_iface_addr);
4483 res.freq = bss->freq;
4484 res.ssid_len = bss->ssid_len;
4485 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4486 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4488 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4492 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4493 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4495 wpa_drv_cancel_remain_on_channel(wpa_s);
4496 wpa_s->off_channel_freq = 0;
4497 wpa_s->roc_waiting_drv_freq = 0;
4499 wpas_start_wps_enrollee(group, &res);
4502 * Allow a longer timeout for join-a-running-group than normal 15
4503 * second group formation timeout since the GO may not have authorized
4504 * our connection yet.
4506 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4507 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4514 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4515 int *force_freq, int *pref_freq, int go)
4518 unsigned int freq_in_use = 0, num, i;
4520 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4524 num = get_shared_radio_freqs(wpa_s, freqs,
4525 wpa_s->num_multichan_concurrent);
4526 wpa_printf(MSG_DEBUG,
4527 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4528 freq, wpa_s->num_multichan_concurrent, num);
4533 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4535 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4537 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4538 "(%u MHz) is not supported for P2P uses",
4544 for (i = 0; i < num; i++) {
4545 if (freqs[i] == freq)
4549 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4550 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4555 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4556 "requested channel (%u MHz)", freq);
4561 for (i = 0; i < num; i++) {
4562 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4565 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
4566 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4568 *pref_freq = freqs[i];
4570 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4572 *force_freq = freqs[i];
4578 if (num < wpa_s->num_multichan_concurrent && num > 0) {
4579 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4581 } else if (num < wpa_s->num_multichan_concurrent) {
4582 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4585 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4600 * wpas_p2p_connect - Request P2P Group Formation to be started
4601 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4602 * @peer_addr: Address of the peer P2P Device
4603 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4604 * @persistent_group: Whether to create a persistent group
4605 * @auto_join: Whether to select join vs. GO Negotiation automatically
4606 * @join: Whether to join an existing group (as a client) instead of starting
4607 * Group Owner negotiation; @peer_addr is BSSID in that case
4608 * @auth: Whether to only authorize the connection instead of doing that and
4609 * initiating Group Owner negotiation
4610 * @go_intent: GO Intent or -1 to use default
4611 * @freq: Frequency for the group or 0 for auto-selection
4612 * @persistent_id: Persistent group credentials to use for forcing GO
4613 * parameters or -1 to generate new values (SSID/passphrase)
4614 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4615 * interoperability workaround when initiating group formation
4616 * @ht40: Start GO with 40 MHz channel width
4617 * @vht: Start GO with VHT support
4618 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4619 * failure, -2 on failure due to channel not currently available,
4620 * -3 if forced channel is not supported
4622 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4623 const char *pin, enum p2p_wps_method wps_method,
4624 int persistent_group, int auto_join, int join, int auth,
4625 int go_intent, int freq, int persistent_id, int pd,
4628 int force_freq = 0, pref_freq = 0;
4630 enum wpa_driver_if_type iftype;
4632 struct wpa_ssid *ssid = NULL;
4634 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4637 if (persistent_id >= 0) {
4638 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4639 if (ssid == NULL || ssid->disabled != 2 ||
4640 ssid->mode != WPAS_MODE_P2P_GO)
4644 os_free(wpa_s->global->add_psk);
4645 wpa_s->global->add_psk = NULL;
4647 wpa_s->global->p2p_fail_on_wps_complete = 0;
4650 go_intent = wpa_s->conf->p2p_go_intent;
4653 wpa_s->p2p_long_listen = 0;
4655 wpa_s->p2p_wps_method = wps_method;
4656 wpa_s->p2p_persistent_group = !!persistent_group;
4657 wpa_s->p2p_persistent_id = persistent_id;
4658 wpa_s->p2p_go_intent = go_intent;
4659 wpa_s->p2p_connect_freq = freq;
4660 wpa_s->p2p_fallback_to_go_neg = 0;
4661 wpa_s->p2p_pd_before_go_neg = !!pd;
4662 wpa_s->p2p_go_ht40 = !!ht40;
4663 wpa_s->p2p_go_vht = !!vht;
4666 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4667 else if (wps_method == WPS_PIN_DISPLAY) {
4668 ret = wps_generate_pin();
4669 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4671 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4674 wpa_s->p2p_pin[0] = '\0';
4676 if (join || auto_join) {
4677 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4679 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4680 "connect a running group from " MACSTR,
4681 MAC2STR(peer_addr));
4682 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4685 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4686 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4688 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4689 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4693 os_get_reltime(&wpa_s->p2p_auto_started);
4694 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4696 wpa_s->p2p_auto_started.sec,
4697 wpa_s->p2p_auto_started.usec);
4699 wpa_s->user_initiated_pd = 1;
4700 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4701 auto_join, freq, NULL, 0) < 0)
4706 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4710 wpas_p2p_set_own_freq_preference(wpa_s,
4711 force_freq ? force_freq : pref_freq);
4713 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4715 if (wpa_s->create_p2p_iface) {
4716 /* Prepare to add a new interface for the group */
4717 iftype = WPA_IF_P2P_GROUP;
4718 if (go_intent == 15)
4719 iftype = WPA_IF_P2P_GO;
4720 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4721 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4722 "interface for the group");
4726 if_addr = wpa_s->pending_interface_addr;
4728 if_addr = wpa_s->own_addr;
4731 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
4733 force_freq, persistent_group, ssid,
4739 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4740 go_intent, if_addr, force_freq,
4741 persistent_group, ssid, pref_freq) < 0) {
4742 if (wpa_s->create_p2p_iface)
4743 wpas_p2p_remove_pending_group_interface(wpa_s);
4751 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4752 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4753 * @freq: Frequency of the channel in MHz
4754 * @duration: Duration of the stay on the channel in milliseconds
4756 * This callback is called when the driver indicates that it has started the
4757 * requested remain-on-channel duration.
4759 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4760 unsigned int freq, unsigned int duration)
4762 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4764 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4765 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4766 wpa_s->pending_listen_duration);
4767 wpa_s->pending_listen_freq = 0;
4769 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
4770 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4776 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4777 unsigned int timeout)
4779 /* Limit maximum Listen state time based on driver limitation. */
4780 if (timeout > wpa_s->max_remain_on_chan)
4781 timeout = wpa_s->max_remain_on_chan;
4783 return p2p_listen(wpa_s->global->p2p, timeout);
4788 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4789 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4790 * @freq: Frequency of the channel in MHz
4792 * This callback is called when the driver indicates that a remain-on-channel
4793 * operation has been completed, i.e., the duration on the requested channel
4796 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4799 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4800 "(p2p_long_listen=%d ms pending_action_tx=%p)",
4801 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
4802 wpas_p2p_listen_work_done(wpa_s);
4803 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4805 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4806 return; /* P2P module started a new operation */
4807 if (offchannel_pending_action_tx(wpa_s))
4809 if (wpa_s->p2p_long_listen > 0)
4810 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4811 if (wpa_s->p2p_long_listen > 0) {
4812 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4813 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4816 * When listen duration is over, stop listen & update p2p_state
4819 p2p_stop_listen(wpa_s->global->p2p);
4825 * wpas_p2p_group_remove - Remove a P2P group
4826 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4827 * @ifname: Network interface name of the group interface or "*" to remove all
4829 * Returns: 0 on success, -1 on failure
4831 * This function is used to remove a P2P group. This can be used to disconnect
4832 * from a group in which the local end is a P2P Client or to end a P2P Group in
4833 * case the local end is the Group Owner. If a virtual network interface was
4834 * created for this group, that interface will be removed. Otherwise, only the
4835 * configured P2P group network will be removed from the interface.
4837 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4839 struct wpa_global *global = wpa_s->global;
4841 if (os_strcmp(ifname, "*") == 0) {
4842 struct wpa_supplicant *prev;
4843 wpa_s = global->ifaces;
4846 wpa_s = wpa_s->next;
4847 if (prev->p2p_group_interface !=
4848 NOT_P2P_GROUP_INTERFACE ||
4849 (prev->current_ssid &&
4850 prev->current_ssid->p2p_group))
4851 wpas_p2p_disconnect(prev);
4856 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4857 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4861 return wpas_p2p_disconnect(wpa_s);
4865 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4870 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4872 if (wpa_s->best_24_freq > 0 &&
4873 p2p_supported_freq_go(wpa_s->global->p2p,
4874 wpa_s->best_24_freq)) {
4875 freq = wpa_s->best_24_freq;
4876 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4877 "channel: %d MHz", freq);
4879 os_get_random((u8 *) &r, sizeof(r));
4880 freq = 2412 + (r % 3) * 25;
4881 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4882 "channel: %d MHz", freq);
4887 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4889 if (wpa_s->best_5_freq > 0 &&
4890 p2p_supported_freq_go(wpa_s->global->p2p,
4891 wpa_s->best_5_freq)) {
4892 freq = wpa_s->best_5_freq;
4893 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4894 "channel: %d MHz", freq);
4896 os_get_random((u8 *) &r, sizeof(r));
4897 freq = 5180 + (r % 4) * 20;
4898 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4899 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4900 "5 GHz channel for P2P group");
4903 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4904 "channel: %d MHz", freq);
4908 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4909 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4910 "(%u MHz) is not supported for P2P uses",
4919 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4920 struct p2p_go_neg_results *params,
4921 int freq, int ht40, int vht,
4922 const struct p2p_channels *channels)
4925 unsigned int pref_freq;
4926 unsigned int num, i;
4928 os_memset(params, 0, sizeof(*params));
4929 params->role_go = 1;
4930 params->ht40 = ht40;
4933 if (!freq_included(channels, freq)) {
4934 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4938 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4939 "frequency %d MHz", freq);
4940 params->freq = freq;
4941 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4942 wpa_s->conf->p2p_oper_channel >= 1 &&
4943 wpa_s->conf->p2p_oper_channel <= 11 &&
4944 freq_included(channels,
4945 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
4946 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4947 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4948 "frequency %d MHz", params->freq);
4949 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4950 wpa_s->conf->p2p_oper_reg_class == 116 ||
4951 wpa_s->conf->p2p_oper_reg_class == 117 ||
4952 wpa_s->conf->p2p_oper_reg_class == 124 ||
4953 wpa_s->conf->p2p_oper_reg_class == 126 ||
4954 wpa_s->conf->p2p_oper_reg_class == 127) &&
4955 freq_included(channels,
4956 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
4957 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4958 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4959 "frequency %d MHz", params->freq);
4960 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4961 wpa_s->best_overall_freq > 0 &&
4962 p2p_supported_freq_go(wpa_s->global->p2p,
4963 wpa_s->best_overall_freq) &&
4964 freq_included(channels, wpa_s->best_overall_freq)) {
4965 params->freq = wpa_s->best_overall_freq;
4966 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4967 "channel %d MHz", params->freq);
4968 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4969 wpa_s->best_24_freq > 0 &&
4970 p2p_supported_freq_go(wpa_s->global->p2p,
4971 wpa_s->best_24_freq) &&
4972 freq_included(channels, wpa_s->best_24_freq)) {
4973 params->freq = wpa_s->best_24_freq;
4974 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4975 "channel %d MHz", params->freq);
4976 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4977 wpa_s->best_5_freq > 0 &&
4978 p2p_supported_freq_go(wpa_s->global->p2p,
4979 wpa_s->best_5_freq) &&
4980 freq_included(channels, wpa_s->best_5_freq)) {
4981 params->freq = wpa_s->best_5_freq;
4982 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4983 "channel %d MHz", params->freq);
4984 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4986 params->freq = pref_freq;
4987 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4988 "channels", params->freq);
4991 for (chan = 0; chan < 11; chan++) {
4992 params->freq = 2412 + chan * 5;
4993 if (!wpas_p2p_disallowed_freq(wpa_s->global,
4995 freq_included(channels, params->freq))
4999 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
5003 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
5004 "known)", params->freq);
5007 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
5011 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5012 wpa_s->num_multichan_concurrent);
5019 for (i = 0; i < num; i++) {
5020 if (freq && freqs[i] == freq)
5022 if (!freq && freq_included(channels, freqs[i])) {
5023 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5025 params->freq = freqs[i];
5031 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5033 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5035 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5038 } else if (num == 0) {
5039 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5041 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5050 static struct wpa_supplicant *
5051 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5054 struct wpa_supplicant *group_wpa_s;
5056 if (!wpas_p2p_create_iface(wpa_s)) {
5057 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5059 wpa_s->p2p_first_connection_timeout = 0;
5063 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5064 WPA_IF_P2P_CLIENT) < 0) {
5065 wpa_msg_global(wpa_s, MSG_ERROR,
5066 "P2P: Failed to add group interface");
5069 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5070 if (group_wpa_s == NULL) {
5071 wpa_msg_global(wpa_s, MSG_ERROR,
5072 "P2P: Failed to initialize group interface");
5073 wpas_p2p_remove_pending_group_interface(wpa_s);
5077 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5078 group_wpa_s->ifname);
5079 group_wpa_s->p2p_first_connection_timeout = 0;
5085 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5086 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5087 * @persistent_group: Whether to create a persistent group
5088 * @freq: Frequency for the group or 0 to indicate no hardcoding
5089 * @ht40: Start GO with 40 MHz channel width
5090 * @vht: Start GO with VHT support
5091 * Returns: 0 on success, -1 on failure
5093 * This function creates a new P2P group with the local end as the Group Owner,
5094 * i.e., without using Group Owner Negotiation.
5096 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5097 int freq, int ht40, int vht)
5099 struct p2p_go_neg_results params;
5101 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5104 os_free(wpa_s->global->add_psk);
5105 wpa_s->global->add_psk = NULL;
5107 /* Make sure we are not running find during connection establishment */
5108 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5109 wpas_p2p_stop_find_oper(wpa_s);
5111 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5115 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5118 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5119 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5120 "(%u MHz) is not supported for P2P uses",
5124 p2p_go_params(wpa_s->global->p2p, ¶ms);
5125 params.persistent_group = persistent_group;
5127 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5130 wpas_start_wps_go(wpa_s, ¶ms, 0);
5136 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5137 struct wpa_ssid *params, int addr_allocated)
5139 struct wpa_ssid *ssid;
5141 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5144 wpa_s->p2p_last_4way_hs_fail = NULL;
5146 wpa_supplicant_ap_deinit(wpa_s);
5148 ssid = wpa_config_add_network(wpa_s->conf);
5151 wpa_config_set_network_defaults(ssid);
5152 ssid->temporary = 1;
5153 ssid->proto = WPA_PROTO_RSN;
5154 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5155 ssid->group_cipher = WPA_CIPHER_CCMP;
5156 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5157 ssid->ssid = os_malloc(params->ssid_len);
5158 if (ssid->ssid == NULL) {
5159 wpa_config_remove_network(wpa_s->conf, ssid->id);
5162 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5163 ssid->ssid_len = params->ssid_len;
5164 ssid->p2p_group = 1;
5165 ssid->export_keys = 1;
5166 if (params->psk_set) {
5167 os_memcpy(ssid->psk, params->psk, 32);
5170 if (params->passphrase)
5171 ssid->passphrase = os_strdup(params->passphrase);
5173 wpa_s->show_group_started = 1;
5175 wpa_supplicant_select_network(wpa_s, ssid);
5181 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5182 struct wpa_ssid *ssid, int addr_allocated,
5183 int force_freq, int neg_freq, int ht40,
5184 int vht, const struct p2p_channels *channels,
5185 int connection_timeout)
5187 struct p2p_go_neg_results params;
5190 if (ssid->disabled != 2 || ssid->ssid == NULL)
5193 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5194 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5195 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5200 os_free(wpa_s->global->add_psk);
5201 wpa_s->global->add_psk = NULL;
5203 /* Make sure we are not running find during connection establishment */
5204 wpas_p2p_stop_find_oper(wpa_s);
5206 wpa_s->p2p_fallback_to_go_neg = 0;
5208 if (ssid->mode == WPAS_MODE_INFRA)
5209 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
5211 if (ssid->mode != WPAS_MODE_P2P_GO)
5214 if (force_freq > 0) {
5215 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5219 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5220 if (freq < 0 || (freq > 0 && !freq_included(channels, freq)))
5224 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
5228 params.psk_set = ssid->psk_set;
5230 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5231 if (ssid->passphrase) {
5232 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5233 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5234 "persistent group");
5237 os_strlcpy(params.passphrase, ssid->passphrase,
5238 sizeof(params.passphrase));
5240 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5241 params.ssid_len = ssid->ssid_len;
5242 params.persistent_group = 1;
5244 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5248 wpa_s->p2p_first_connection_timeout = connection_timeout;
5249 wpas_start_wps_go(wpa_s, ¶ms, 0);
5255 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5256 struct wpabuf *proberesp_ies)
5258 struct wpa_supplicant *wpa_s = ctx;
5259 if (wpa_s->ap_iface) {
5260 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5261 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5262 wpabuf_free(beacon_ies);
5263 wpabuf_free(proberesp_ies);
5267 wpabuf_free(hapd->p2p_beacon_ie);
5268 hapd->p2p_beacon_ie = beacon_ies;
5270 wpabuf_free(hapd->p2p_probe_resp_ie);
5271 hapd->p2p_probe_resp_ie = proberesp_ies;
5273 wpabuf_free(beacon_ies);
5274 wpabuf_free(proberesp_ies);
5276 wpa_supplicant_ap_update_beacon(wpa_s);
5280 static void wpas_p2p_idle_update(void *ctx, int idle)
5282 struct wpa_supplicant *wpa_s = ctx;
5283 if (!wpa_s->ap_iface)
5285 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
5287 if (wpa_s->global->p2p_fail_on_wps_complete &&
5288 wpa_s->p2p_in_provisioning) {
5289 wpas_p2p_grpform_fail_after_wps(wpa_s);
5292 wpas_p2p_set_group_idle_timeout(wpa_s);
5294 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5298 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
5299 struct wpa_ssid *ssid)
5301 struct p2p_group *group;
5302 struct p2p_group_config *cfg;
5304 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5307 cfg = os_zalloc(sizeof(*cfg));
5311 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
5312 cfg->persistent_group = 2;
5313 else if (ssid->p2p_persistent_group)
5314 cfg->persistent_group = 1;
5315 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5316 if (wpa_s->max_stations &&
5317 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5318 cfg->max_clients = wpa_s->max_stations;
5320 cfg->max_clients = wpa_s->conf->max_num_sta;
5321 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5322 cfg->ssid_len = ssid->ssid_len;
5323 cfg->freq = ssid->frequency;
5324 cfg->cb_ctx = wpa_s;
5325 cfg->ie_update = wpas_p2p_ie_update;
5326 cfg->idle_update = wpas_p2p_idle_update;
5328 group = p2p_group_init(wpa_s->global->p2p, cfg);
5331 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
5332 p2p_group_notif_formation_done(group);
5333 wpa_s->p2p_group = group;
5338 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5341 struct wpa_ssid *ssid = wpa_s->current_ssid;
5343 if (!wpa_s->p2p_in_provisioning) {
5344 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5345 "provisioning not in progress");
5349 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5350 u8 go_dev_addr[ETH_ALEN];
5351 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5352 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5354 /* Clear any stored provisioning info */
5355 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5358 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5360 wpa_s->p2p_go_group_formation_completed = 1;
5361 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5363 * Use a separate timeout for initial data connection to
5364 * complete to allow the group to be removed automatically if
5365 * something goes wrong in this step before the P2P group idle
5366 * timeout mechanism is taken into use.
5368 wpa_dbg(wpa_s, MSG_DEBUG,
5369 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5370 P2P_MAX_INITIAL_CONN_WAIT);
5371 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5372 wpas_p2p_group_formation_timeout,
5373 wpa_s->parent, NULL);
5376 * Use a separate timeout for initial data connection to
5377 * complete to allow the group to be removed automatically if
5378 * the client does not complete data connection successfully.
5380 wpa_dbg(wpa_s, MSG_DEBUG,
5381 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5382 P2P_MAX_INITIAL_CONN_WAIT_GO);
5383 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5384 wpas_p2p_group_formation_timeout,
5385 wpa_s->parent, NULL);
5387 * Complete group formation on first successful data connection
5389 wpa_s->p2p_go_group_formation_completed = 0;
5391 if (wpa_s->global->p2p)
5392 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5393 wpas_group_formation_completed(wpa_s, 1);
5397 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5398 struct wps_event_fail *fail)
5400 if (!wpa_s->p2p_in_provisioning) {
5401 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5402 "provisioning not in progress");
5406 if (wpa_s->go_params) {
5407 p2p_clear_provisioning_info(
5409 wpa_s->go_params->peer_device_addr);
5412 wpas_notify_p2p_wps_failed(wpa_s, fail);
5414 if (wpa_s == wpa_s->global->p2p_group_formation) {
5416 * Allow some time for the failed WPS negotiation exchange to
5417 * complete, but remove the group since group formation cannot
5418 * succeed after provisioning failure.
5420 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5421 wpa_s->global->p2p_fail_on_wps_complete = 1;
5422 eloop_deplete_timeout(0, 50000,
5423 wpas_p2p_group_formation_timeout,
5424 wpa_s->parent, NULL);
5429 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5431 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5432 !wpa_s->p2p_in_provisioning)
5435 wpas_p2p_grpform_fail_after_wps(wpa_s);
5441 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5442 const char *config_method,
5443 enum wpas_p2p_prov_disc_use use)
5447 wpa_s->p2p_fallback_to_go_neg = 0;
5448 wpa_s->pending_pd_use = NORMAL_PD;
5449 if (os_strncmp(config_method, "display", 7) == 0)
5450 config_methods = WPS_CONFIG_DISPLAY;
5451 else if (os_strncmp(config_method, "keypad", 6) == 0)
5452 config_methods = WPS_CONFIG_KEYPAD;
5453 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5454 os_strncmp(config_method, "pushbutton", 10) == 0)
5455 config_methods = WPS_CONFIG_PUSHBUTTON;
5457 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5461 if (use == WPAS_P2P_PD_AUTO) {
5462 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5463 wpa_s->pending_pd_config_methods = config_methods;
5464 wpa_s->p2p_auto_pd = 1;
5465 wpa_s->p2p_auto_join = 0;
5466 wpa_s->pending_pd_before_join = 0;
5467 wpa_s->auto_pd_scan_retry = 0;
5468 wpas_p2p_stop_find(wpa_s);
5469 wpa_s->p2p_join_scan_count = 0;
5470 os_get_reltime(&wpa_s->p2p_auto_started);
5471 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5472 wpa_s->p2p_auto_started.sec,
5473 wpa_s->p2p_auto_started.usec);
5474 wpas_p2p_join_scan(wpa_s, NULL);
5478 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5481 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
5482 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5487 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5490 return p2p_scan_result_text(ies, ies_len, buf, end);
5494 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5496 if (!offchannel_pending_action_tx(wpa_s))
5499 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5500 "operation request");
5501 offchannel_clear_pending_action_tx(wpa_s);
5505 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5506 enum p2p_discovery_type type,
5507 unsigned int num_req_dev_types, const u8 *req_dev_types,
5508 const u8 *dev_id, unsigned int search_delay)
5510 wpas_p2p_clear_pending_action_tx(wpa_s);
5511 wpa_s->p2p_long_listen = 0;
5513 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5514 wpa_s->p2p_in_provisioning)
5517 wpa_supplicant_cancel_sched_scan(wpa_s);
5519 return p2p_find(wpa_s->global->p2p, timeout, type,
5520 num_req_dev_types, req_dev_types, dev_id,
5525 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
5527 wpas_p2p_clear_pending_action_tx(wpa_s);
5528 wpa_s->p2p_long_listen = 0;
5529 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5530 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5532 if (wpa_s->global->p2p)
5533 p2p_stop_find(wpa_s->global->p2p);
5539 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5541 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5543 wpas_p2p_remove_pending_group_interface(wpa_s);
5547 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5549 struct wpa_supplicant *wpa_s = eloop_ctx;
5550 wpa_s->p2p_long_listen = 0;
5554 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5558 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5561 wpa_supplicant_cancel_sched_scan(wpa_s);
5562 wpas_p2p_clear_pending_action_tx(wpa_s);
5566 * This is a request for unlimited Listen state. However, at
5567 * least for now, this is mapped to a Listen state for one
5572 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5573 wpa_s->p2p_long_listen = 0;
5576 * Stop previous find/listen operation to avoid trying to request a new
5577 * remain-on-channel operation while the driver is still running the
5580 if (wpa_s->global->p2p)
5581 p2p_stop_find(wpa_s->global->p2p);
5583 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5584 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5585 wpa_s->p2p_long_listen = timeout * 1000;
5586 eloop_register_timeout(timeout, 0,
5587 wpas_p2p_long_listen_timeout,
5595 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5596 u8 *buf, size_t len, int p2p_group)
5598 struct wpabuf *p2p_ie;
5601 if (wpa_s->global->p2p_disabled)
5603 if (wpa_s->global->p2p == NULL)
5608 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5609 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5611 wpabuf_free(p2p_ie);
5617 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
5618 const u8 *dst, const u8 *bssid,
5619 const u8 *ie, size_t ie_len, int ssi_signal)
5621 if (wpa_s->global->p2p_disabled)
5623 if (wpa_s->global->p2p == NULL)
5626 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5628 case P2P_PREQ_NOT_P2P:
5629 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5632 case P2P_PREQ_MALFORMED:
5633 case P2P_PREQ_NOT_LISTEN:
5634 case P2P_PREQ_NOT_PROCESSED:
5635 default: /* make gcc happy */
5637 case P2P_PREQ_PROCESSED:
5643 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5644 const u8 *sa, const u8 *bssid,
5645 u8 category, const u8 *data, size_t len, int freq)
5647 if (wpa_s->global->p2p_disabled)
5649 if (wpa_s->global->p2p == NULL)
5652 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5657 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5659 if (wpa_s->global->p2p_disabled)
5661 if (wpa_s->global->p2p == NULL)
5664 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5668 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5670 p2p_group_deinit(wpa_s->p2p_group);
5671 wpa_s->p2p_group = NULL;
5673 wpa_s->ap_configured_cb = NULL;
5674 wpa_s->ap_configured_cb_ctx = NULL;
5675 wpa_s->ap_configured_cb_data = NULL;
5676 wpa_s->connect_without_scan = NULL;
5680 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5682 wpa_s->p2p_long_listen = 0;
5684 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5687 return p2p_reject(wpa_s->global->p2p, addr);
5691 /* Invite to reinvoke a persistent group */
5692 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5693 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
5694 int ht40, int vht, int pref_freq)
5696 enum p2p_invite_role role;
5700 int no_pref_freq_given = pref_freq == 0;
5702 wpa_s->global->p2p_invite_group = NULL;
5704 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5706 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5708 wpa_s->p2p_persistent_go_freq = freq;
5709 wpa_s->p2p_go_ht40 = !!ht40;
5710 if (ssid->mode == WPAS_MODE_P2P_GO) {
5711 role = P2P_INVITE_ROLE_GO;
5712 if (peer_addr == NULL) {
5713 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5714 "address in invitation command");
5717 if (wpas_p2p_create_iface(wpa_s)) {
5718 if (wpas_p2p_add_group_interface(wpa_s,
5719 WPA_IF_P2P_GO) < 0) {
5720 wpa_printf(MSG_ERROR, "P2P: Failed to "
5721 "allocate a new interface for the "
5725 bssid = wpa_s->pending_interface_addr;
5727 bssid = wpa_s->own_addr;
5729 role = P2P_INVITE_ROLE_CLIENT;
5730 peer_addr = ssid->bssid;
5732 wpa_s->pending_invite_ssid_id = ssid->id;
5734 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5735 role == P2P_INVITE_ROLE_GO);
5739 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5742 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5743 no_pref_freq_given && pref_freq > 0 &&
5744 wpa_s->num_multichan_concurrent > 1 &&
5745 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5746 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5751 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5752 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5757 /* Invite to join an active group */
5758 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5759 const u8 *peer_addr, const u8 *go_dev_addr)
5761 struct wpa_global *global = wpa_s->global;
5762 enum p2p_invite_role role;
5764 struct wpa_ssid *ssid;
5766 int freq = 0, force_freq = 0, pref_freq = 0;
5769 wpa_s->p2p_persistent_go_freq = 0;
5770 wpa_s->p2p_go_ht40 = 0;
5771 wpa_s->p2p_go_vht = 0;
5773 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5774 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5777 if (wpa_s == NULL) {
5778 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5782 ssid = wpa_s->current_ssid;
5784 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5789 wpa_s->global->p2p_invite_group = wpa_s;
5790 persistent = ssid->p2p_persistent_group &&
5791 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5792 ssid->ssid, ssid->ssid_len);
5794 if (ssid->mode == WPAS_MODE_P2P_GO) {
5795 role = P2P_INVITE_ROLE_ACTIVE_GO;
5796 bssid = wpa_s->own_addr;
5797 if (go_dev_addr == NULL)
5798 go_dev_addr = wpa_s->global->p2p_dev_addr;
5799 freq = ssid->frequency;
5801 role = P2P_INVITE_ROLE_CLIENT;
5802 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5803 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5804 "invite to current group");
5807 bssid = wpa_s->bssid;
5808 if (go_dev_addr == NULL &&
5809 !is_zero_ether_addr(wpa_s->go_dev_addr))
5810 go_dev_addr = wpa_s->go_dev_addr;
5811 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5812 (int) wpa_s->assoc_freq;
5814 wpa_s->parent->pending_invite_ssid_id = -1;
5816 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5819 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5820 role == P2P_INVITE_ROLE_ACTIVE_GO);
5823 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
5825 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5826 ssid->ssid, ssid->ssid_len, force_freq,
5827 go_dev_addr, persistent, pref_freq, -1);
5831 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5833 struct wpa_ssid *ssid = wpa_s->current_ssid;
5834 const char *ssid_txt;
5835 u8 go_dev_addr[ETH_ALEN];
5836 int network_id = -1;
5842 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5843 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5844 wpa_s->parent, NULL);
5847 if (!wpa_s->show_group_started || !ssid)
5850 wpa_s->show_group_started = 0;
5852 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5853 os_memset(go_dev_addr, 0, ETH_ALEN);
5854 if (ssid->bssid_set)
5855 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5856 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5858 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5860 if (wpa_s->global->p2p_group_formation == wpa_s)
5861 wpa_s->global->p2p_group_formation = NULL;
5863 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5864 (int) wpa_s->assoc_freq;
5867 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
5868 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
5869 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
5870 ip[0], ip[1], ip[2], ip[3],
5871 ip[4], ip[5], ip[6], ip[7],
5872 ip[8], ip[9], ip[10], ip[11]);
5875 if (ssid->passphrase == NULL && ssid->psk_set) {
5877 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5878 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5879 "%s client ssid=\"%s\" freq=%d psk=%s "
5880 "go_dev_addr=" MACSTR "%s%s",
5881 wpa_s->ifname, ssid_txt, freq, psk,
5882 MAC2STR(go_dev_addr),
5883 persistent ? " [PERSISTENT]" : "", ip_addr);
5885 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5886 "%s client ssid=\"%s\" freq=%d "
5887 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s%s",
5888 wpa_s->ifname, ssid_txt, freq,
5889 ssid->passphrase ? ssid->passphrase : "",
5890 MAC2STR(go_dev_addr),
5891 persistent ? " [PERSISTENT]" : "", ip_addr);
5895 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5898 network_id = ssid->id;
5899 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
5903 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5904 u32 interval1, u32 duration2, u32 interval2)
5908 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5911 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5912 wpa_s->current_ssid == NULL ||
5913 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5916 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5917 wpa_s->own_addr, wpa_s->assoc_freq,
5918 duration1, interval1, duration2, interval2);
5920 wpa_s->waiting_presence_resp = 1;
5926 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5927 unsigned int interval)
5929 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5932 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5936 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5938 if (wpa_s->current_ssid == NULL) {
5940 * current_ssid can be cleared when P2P client interface gets
5941 * disconnected, so assume this interface was used as P2P
5946 return wpa_s->current_ssid->p2p_group &&
5947 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5951 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5953 struct wpa_supplicant *wpa_s = eloop_ctx;
5955 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
5956 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5961 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5963 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
5967 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5971 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5972 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5974 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5977 timeout = wpa_s->conf->p2p_group_idle;
5978 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5979 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5980 timeout = P2P_MAX_CLIENT_IDLE;
5986 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5987 timeout = 0; /* special client mode no-timeout */
5992 if (wpa_s->p2p_in_provisioning) {
5994 * Use the normal group formation timeout during the
5995 * provisioning phase to avoid terminating this process too
5996 * early due to group idle timeout.
5998 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5999 "during provisioning");
6003 if (wpa_s->show_group_started) {
6005 * Use the normal group formation timeout between the end of
6006 * the provisioning phase and completion of 4-way handshake to
6007 * avoid terminating this process too early due to group idle
6010 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6011 "while waiting for initial 4-way handshake to "
6016 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6018 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6023 /* Returns 1 if the interface was removed */
6024 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6025 u16 reason_code, const u8 *ie, size_t ie_len,
6026 int locally_generated)
6028 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6031 if (!locally_generated)
6032 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6035 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6036 wpa_s->current_ssid &&
6037 wpa_s->current_ssid->p2p_group &&
6038 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6039 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6040 "session is ending");
6041 if (wpas_p2p_group_delete(wpa_s,
6042 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6051 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6052 u16 reason_code, const u8 *ie, size_t ie_len,
6053 int locally_generated)
6055 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6058 if (!locally_generated)
6059 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6064 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6066 struct p2p_data *p2p = wpa_s->global->p2p;
6071 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6074 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6075 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6077 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6078 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6081 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6082 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6084 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6085 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6087 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6088 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6089 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6090 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6091 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6094 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6095 p2p_set_sec_dev_types(p2p,
6096 (void *) wpa_s->conf->sec_device_type,
6097 wpa_s->conf->num_sec_device_types);
6099 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6101 p2p_remove_wps_vendor_extensions(p2p);
6102 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6103 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6105 p2p_add_wps_vendor_extension(
6106 p2p, wpa_s->conf->wps_vendor_ext[i]);
6110 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6111 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6113 country[0] = wpa_s->conf->country[0];
6114 country[1] = wpa_s->conf->country[1];
6116 p2p_set_country(p2p, country);
6119 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6120 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6121 wpa_s->conf->p2p_ssid_postfix ?
6122 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6126 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6127 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6129 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6130 u8 reg_class, channel;
6133 if (wpa_s->conf->p2p_listen_reg_class &&
6134 wpa_s->conf->p2p_listen_channel) {
6135 reg_class = wpa_s->conf->p2p_listen_reg_class;
6136 channel = wpa_s->conf->p2p_listen_channel;
6140 * Pick one of the social channels randomly as the
6143 os_get_random((u8 *) &r, sizeof(r));
6144 channel = 1 + (r % 3) * 5;
6146 ret = p2p_set_listen_channel(p2p, reg_class, channel);
6148 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6151 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6152 u8 op_reg_class, op_channel, cfg_op_channel;
6155 if (wpa_s->conf->p2p_oper_reg_class &&
6156 wpa_s->conf->p2p_oper_channel) {
6157 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6158 op_channel = wpa_s->conf->p2p_oper_channel;
6163 * Use random operation channel from (1, 6, 11)
6164 *if no other preference is indicated.
6166 os_get_random((u8 *) &r, sizeof(r));
6167 op_channel = 1 + (r % 3) * 5;
6170 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6173 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6177 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6178 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6179 wpa_s->conf->p2p_pref_chan) < 0) {
6180 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6184 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6185 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6192 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6195 if (!wpa_s->ap_iface)
6197 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6202 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6204 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6207 wpa_s->global->cross_connection = enabled;
6208 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6211 struct wpa_supplicant *iface;
6213 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6215 if (iface->cross_connect_enabled == 0)
6218 iface->cross_connect_enabled = 0;
6219 iface->cross_connect_in_use = 0;
6220 wpa_msg_global(iface->parent, MSG_INFO,
6221 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6223 iface->cross_connect_uplink);
6231 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6233 struct wpa_supplicant *iface;
6235 if (!uplink->global->cross_connection)
6238 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6239 if (!iface->cross_connect_enabled)
6241 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6244 if (iface->ap_iface == NULL)
6246 if (iface->cross_connect_in_use)
6249 iface->cross_connect_in_use = 1;
6250 wpa_msg_global(iface->parent, MSG_INFO,
6251 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6252 iface->ifname, iface->cross_connect_uplink);
6257 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6259 struct wpa_supplicant *iface;
6261 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6262 if (!iface->cross_connect_enabled)
6264 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6267 if (!iface->cross_connect_in_use)
6270 wpa_msg_global(iface->parent, MSG_INFO,
6271 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6272 iface->ifname, iface->cross_connect_uplink);
6273 iface->cross_connect_in_use = 0;
6278 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6280 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6281 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6282 wpa_s->cross_connect_disallowed)
6283 wpas_p2p_disable_cross_connect(wpa_s);
6285 wpas_p2p_enable_cross_connect(wpa_s);
6286 if (!wpa_s->ap_iface &&
6287 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6288 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6292 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6294 wpas_p2p_disable_cross_connect(wpa_s);
6295 if (!wpa_s->ap_iface &&
6296 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6298 wpas_p2p_set_group_idle_timeout(wpa_s);
6302 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6304 struct wpa_supplicant *iface;
6306 if (!wpa_s->global->cross_connection)
6309 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6312 if (iface->drv_flags &
6313 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6315 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6318 wpa_s->cross_connect_enabled = 1;
6319 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6320 sizeof(wpa_s->cross_connect_uplink));
6321 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6322 "%s to %s whenever uplink is available",
6323 wpa_s->ifname, wpa_s->cross_connect_uplink);
6325 if (iface->ap_iface || iface->current_ssid == NULL ||
6326 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6327 iface->cross_connect_disallowed ||
6328 iface->wpa_state != WPA_COMPLETED)
6331 wpa_s->cross_connect_in_use = 1;
6332 wpa_msg_global(wpa_s->parent, MSG_INFO,
6333 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6334 wpa_s->ifname, wpa_s->cross_connect_uplink);
6340 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6342 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6343 !wpa_s->p2p_in_provisioning)
6344 return 0; /* not P2P client operation */
6346 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6348 if (wpa_s != wpa_s->parent)
6349 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
6350 wpas_p2p_group_formation_failed(wpa_s);
6355 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6357 struct p2p_channels chan, cli_chan;
6359 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6362 os_memset(&chan, 0, sizeof(chan));
6363 os_memset(&cli_chan, 0, sizeof(cli_chan));
6364 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
6365 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6370 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
6374 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6375 struct wpa_scan_results *scan_res)
6377 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6381 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6383 struct wpa_global *global = wpa_s->global;
6387 if (global->p2p == NULL)
6390 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6392 if (wpa_s->pending_interface_name[0] &&
6393 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6396 peer = p2p_get_go_neg_peer(global->p2p);
6398 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6399 MACSTR, MAC2STR(peer));
6400 p2p_unauthorize(global->p2p, peer);
6404 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6405 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6406 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6410 if (wpa_s->pending_pd_before_join) {
6411 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6412 wpa_s->pending_pd_before_join = 0;
6416 wpas_p2p_stop_find(wpa_s);
6418 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6419 if (wpa_s == global->p2p_group_formation &&
6420 (wpa_s->p2p_in_provisioning ||
6421 wpa_s->parent->pending_interface_type ==
6422 WPA_IF_P2P_CLIENT)) {
6423 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6424 "formation found - cancelling",
6427 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6428 wpa_s->parent, NULL);
6429 if (wpa_s->p2p_in_provisioning) {
6430 wpas_group_formation_completed(wpa_s, 0);
6433 wpas_p2p_group_delete(wpa_s,
6434 P2P_GROUP_REMOVAL_REQUESTED);
6440 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6448 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6450 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6453 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6454 "being available anymore");
6455 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
6459 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6460 int freq_24, int freq_5, int freq_overall)
6462 struct p2p_data *p2p = wpa_s->global->p2p;
6465 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6469 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6472 struct p2p_data *p2p = wpa_s->global->p2p;
6477 if (hwaddr_aton(addr, peer))
6480 return p2p_unauthorize(p2p, peer);
6485 * wpas_p2p_disconnect - Disconnect from a P2P Group
6486 * @wpa_s: Pointer to wpa_supplicant data
6487 * Returns: 0 on success, -1 on failure
6489 * This can be used to disconnect from a group in which the local end is a P2P
6490 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6491 * virtual network interface was created for this group, that interface will be
6492 * removed. Otherwise, only the configured P2P group network will be removed
6493 * from the interface.
6495 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6501 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6506 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6510 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6513 ret = p2p_in_progress(wpa_s->global->p2p);
6516 * Check whether there is an ongoing WPS provisioning step (or
6517 * other parts of group formation) on another interface since
6518 * p2p_in_progress() does not report this to avoid issues for
6519 * scans during such provisioning step.
6521 if (wpa_s->global->p2p_group_formation &&
6522 wpa_s->global->p2p_group_formation != wpa_s) {
6523 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6524 "in group formation",
6525 wpa_s->global->p2p_group_formation->ifname);
6530 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6531 struct os_reltime now;
6532 os_get_reltime(&now);
6533 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6534 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
6535 /* Wait for the first client has expired */
6536 wpa_s->global->p2p_go_wait_client.sec = 0;
6538 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6547 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6548 struct wpa_ssid *ssid)
6550 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6551 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6552 wpa_s->parent, NULL) > 0) {
6554 * Remove the network by scheduling the group formation
6555 * timeout to happen immediately. The teardown code
6556 * needs to be scheduled to run asynch later so that we
6557 * don't delete data from under ourselves unexpectedly.
6558 * Calling wpas_p2p_group_formation_timeout directly
6559 * causes a series of crashes in WPS failure scenarios.
6561 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6562 "P2P group network getting removed");
6563 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6564 wpa_s->parent, NULL);
6569 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
6570 const u8 *addr, const u8 *ssid,
6576 for (s = wpa_s->conf->ssid; s; s = s->next) {
6577 if (s->disabled != 2)
6580 (ssid_len != s->ssid_len ||
6581 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6584 if (s->mode == WPAS_MODE_P2P_GO)
6588 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6589 return s; /* peer is GO in the persistent group */
6590 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6592 for (i = 0; i < s->num_p2p_clients; i++) {
6593 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6594 addr, ETH_ALEN) == 0)
6595 return s; /* peer is P2P client in persistent
6604 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6607 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6608 wpa_s->parent, NULL) > 0) {
6610 * This can happen if WPS provisioning step is not terminated
6611 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6612 * peer was able to connect, there is no need to time out group
6613 * formation after this, though. In addition, this is used with
6614 * the initial connection wait on the GO as a separate formation
6615 * timeout and as such, expected to be hit after the initial WPS
6616 * provisioning step.
6618 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6620 if (!wpa_s->p2p_go_group_formation_completed) {
6621 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6622 wpa_s->p2p_go_group_formation_completed = 1;
6623 wpa_s->global->p2p_group_formation = NULL;
6624 wpa_s->p2p_in_provisioning = 0;
6626 wpa_s->global->p2p_go_wait_client.sec = 0;
6629 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6633 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6636 struct wpa_supplicant *group = wpa_s;
6637 if (wpa_s->global->p2p_group_formation)
6638 group = wpa_s->global->p2p_group_formation;
6639 wpa_s = wpa_s->parent;
6640 offchannel_send_action_done(wpa_s);
6642 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
6643 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6644 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6645 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6646 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6647 wpa_s->p2p_persistent_id,
6648 wpa_s->p2p_pd_before_go_neg,
6654 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6656 if (!wpa_s->p2p_fallback_to_go_neg ||
6657 wpa_s->p2p_in_provisioning <= 5)
6660 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6661 return 0; /* peer operating as a GO */
6663 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6664 "fallback to GO Negotiation");
6665 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6671 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6673 struct wpa_supplicant *ifs;
6675 if (wpa_s->wpa_state > WPA_SCANNING) {
6676 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6677 "concurrent operation",
6678 P2P_CONCURRENT_SEARCH_DELAY);
6679 return P2P_CONCURRENT_SEARCH_DELAY;
6682 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6684 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
6685 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6686 "delay due to concurrent operation on "
6688 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6689 return P2P_CONCURRENT_SEARCH_DELAY;
6697 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6698 struct wpa_ssid *s, const u8 *addr,
6701 struct psk_list_entry *psk, *tmp;
6704 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6706 if ((iface_addr && !psk->p2p &&
6707 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6708 (!iface_addr && psk->p2p &&
6709 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6710 wpa_dbg(wpa_s, MSG_DEBUG,
6711 "P2P: Remove persistent group PSK list entry for "
6713 MAC2STR(psk->addr), psk->p2p);
6714 dl_list_del(&psk->list);
6724 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6725 const u8 *p2p_dev_addr,
6726 const u8 *psk, size_t psk_len)
6728 struct wpa_ssid *ssid = wpa_s->current_ssid;
6729 struct wpa_ssid *persistent;
6730 struct psk_list_entry *p;
6732 if (psk_len != sizeof(p->psk))
6736 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6737 " p2p_dev_addr=" MACSTR,
6738 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6739 if (is_zero_ether_addr(p2p_dev_addr))
6740 p2p_dev_addr = NULL;
6742 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6746 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6747 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6748 /* To be added to persistent group once created */
6749 if (wpa_s->global->add_psk == NULL) {
6750 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6751 if (wpa_s->global->add_psk == NULL)
6754 p = wpa_s->global->add_psk;
6757 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6760 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6762 os_memcpy(p->psk, psk, psk_len);
6766 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6767 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6771 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6774 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6778 p = os_zalloc(sizeof(*p));
6783 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6786 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6788 os_memcpy(p->psk, psk, psk_len);
6790 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6791 struct psk_list_entry *last;
6792 last = dl_list_last(&persistent->psk_list,
6793 struct psk_list_entry, list);
6794 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6795 MACSTR " (p2p=%u) to make room for a new one",
6796 MAC2STR(last->addr), last->p2p);
6797 dl_list_del(&last->list);
6801 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6802 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6803 p2p_dev_addr == NULL);
6805 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6806 MACSTR, MAC2STR(p2p_dev_addr));
6808 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6811 dl_list_add(&persistent->psk_list, &p->list);
6813 if (wpa_s->parent->conf->update_config &&
6814 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6815 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6819 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6820 struct wpa_ssid *s, const u8 *addr,
6825 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6826 if (res > 0 && wpa_s->conf->update_config &&
6827 wpa_config_write(wpa_s->confname, wpa_s->conf))
6828 wpa_dbg(wpa_s, MSG_DEBUG,
6829 "P2P: Failed to update configuration");
6833 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6834 const u8 *peer, int iface_addr)
6836 struct hostapd_data *hapd;
6837 struct hostapd_wpa_psk *psk, *prev, *rem;
6838 struct sta_info *sta;
6840 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6841 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6844 /* Remove per-station PSK entry */
6845 hapd = wpa_s->ap_iface->bss[0];
6847 psk = hapd->conf->ssid.wpa_psk;
6849 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6851 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6852 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6853 MACSTR " iface_addr=%d",
6854 MAC2STR(peer), iface_addr);
6856 prev->next = psk->next;
6858 hapd->conf->ssid.wpa_psk = psk->next;
6868 /* Disconnect from group */
6870 sta = ap_get_sta(hapd, peer);
6872 sta = ap_get_sta_p2p(hapd, peer);
6874 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6875 " (iface_addr=%d) from group",
6876 MAC2STR(peer), iface_addr);
6877 hostapd_drv_sta_deauth(hapd, sta->addr,
6878 WLAN_REASON_DEAUTH_LEAVING);
6879 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6884 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6888 struct wpa_supplicant *w;
6890 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6892 /* Remove from any persistent group */
6893 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6894 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6897 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6898 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6901 /* Remove from any operating group */
6902 for (w = wpa_s->global->ifaces; w; w = w->next)
6903 wpas_p2p_remove_client_go(w, peer, iface_addr);
6907 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6909 struct wpa_supplicant *wpa_s = eloop_ctx;
6910 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6914 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6916 struct wpa_supplicant *wpa_s = eloop_ctx;
6918 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6919 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6923 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6924 struct wpa_ssid *ssid)
6926 struct wpa_supplicant *iface;
6928 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6929 if (!iface->current_ssid ||
6930 iface->current_ssid->frequency == freq ||
6931 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
6932 !iface->current_ssid->p2p_group))
6935 /* Remove the connection with least priority */
6936 if (!wpas_is_p2p_prioritized(iface)) {
6937 /* STA connection has priority over existing
6938 * P2P connection, so remove the interface. */
6939 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6940 eloop_register_timeout(0, 0,
6941 wpas_p2p_group_freq_conflict,
6943 /* If connection in progress is P2P connection, do not
6944 * proceed for the connection. */
6950 /* P2P connection has priority, disable the STA network
6952 wpa_supplicant_disable_network(wpa_s->global->ifaces,
6954 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
6955 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
6956 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
6958 /* If P2P connection is in progress, continue
6971 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6973 struct wpa_ssid *ssid = wpa_s->current_ssid;
6975 if (ssid == NULL || !ssid->p2p_group)
6978 if (wpa_s->p2p_last_4way_hs_fail &&
6979 wpa_s->p2p_last_4way_hs_fail == ssid) {
6980 u8 go_dev_addr[ETH_ALEN];
6981 struct wpa_ssid *persistent;
6983 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6985 ssid->ssid_len) <= 0) {
6986 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6990 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6991 MACSTR, MAC2STR(go_dev_addr));
6992 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6995 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6996 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6999 wpa_msg_global(wpa_s->parent, MSG_INFO,
7000 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7003 wpa_s->p2p_last_4way_hs_fail = NULL;
7005 * Remove the group from a timeout to avoid issues with caller
7006 * continuing to use the interface if this is on a P2P group
7009 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7014 wpa_s->p2p_last_4way_hs_fail = ssid;
7019 #ifdef CONFIG_WPS_NFC
7021 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7029 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7033 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7034 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7041 wpabuf_put_be16(ret, wsc_len);
7043 wpabuf_put_buf(ret, wsc);
7044 wpabuf_put_be16(ret, wpabuf_len(p2p));
7045 wpabuf_put_buf(ret, p2p);
7049 wpa_hexdump_buf(MSG_DEBUG,
7050 "P2P: Generated NFC connection handover message", ret);
7054 tmp = ndef_build_p2p(ret);
7057 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7067 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7068 struct wpa_ssid **ssid, u8 *go_dev_addr)
7070 struct wpa_supplicant *iface;
7073 os_memset(go_dev_addr, 0, ETH_ALEN);
7076 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7077 if (iface->wpa_state < WPA_ASSOCIATING ||
7078 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7079 !iface->current_ssid->p2p_group ||
7080 iface->current_ssid->mode != WPAS_MODE_INFRA)
7083 *ssid = iface->current_ssid;
7085 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7086 return iface->assoc_freq;
7092 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7095 struct wpabuf *wsc, *p2p;
7096 struct wpa_ssid *ssid;
7097 u8 go_dev_addr[ETH_ALEN];
7098 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7100 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7101 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7105 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7106 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7107 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7108 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7112 if (cli_freq == 0) {
7113 wsc = wps_build_nfc_handover_req_p2p(
7114 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7117 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7118 go_dev_addr, ssid ? ssid->ssid : NULL,
7119 ssid ? ssid->ssid_len : 0);
7121 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7125 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7128 struct wpabuf *wsc, *p2p;
7129 struct wpa_ssid *ssid;
7130 u8 go_dev_addr[ETH_ALEN];
7131 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7133 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7136 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7137 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7138 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7141 if (cli_freq == 0) {
7142 wsc = wps_build_nfc_handover_sel_p2p(
7144 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7145 DEV_PW_NFC_CONNECTION_HANDOVER,
7146 wpa_s->conf->wps_nfc_dh_pubkey,
7147 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7150 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7151 go_dev_addr, ssid ? ssid->ssid : NULL,
7152 ssid ? ssid->ssid_len : 0);
7154 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7158 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7159 struct p2p_nfc_params *params)
7161 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7162 "connection handover (freq=%d)",
7165 if (params->go_freq && params->go_ssid_len) {
7166 wpa_s->p2p_wps_method = WPS_NFC;
7167 wpa_s->pending_join_wps_method = WPS_NFC;
7168 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7169 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7171 return wpas_p2p_join_start(wpa_s, params->go_freq,
7173 params->go_ssid_len);
7176 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7177 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7178 params->go_freq, -1, 0, 1, 1);
7182 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7183 struct p2p_nfc_params *params, int tag)
7185 int res, persistent;
7186 struct wpa_ssid *ssid;
7188 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7189 "connection handover");
7190 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7191 ssid = wpa_s->current_ssid;
7194 if (ssid->mode != WPAS_MODE_P2P_GO)
7196 if (wpa_s->ap_iface == NULL)
7200 if (wpa_s == NULL) {
7201 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7205 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7206 DEV_PW_NFC_CONNECTION_HANDOVER &&
7207 !wpa_s->parent->p2p_oob_dev_pw) {
7208 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7211 res = wpas_ap_wps_add_nfc_pw(
7212 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7213 wpa_s->parent->p2p_oob_dev_pw,
7214 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7215 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7220 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7224 if (!params->peer ||
7225 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7228 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7229 " to join", MAC2STR(params->peer->p2p_device_addr));
7231 wpa_s->global->p2p_invite_group = wpa_s;
7232 persistent = ssid->p2p_persistent_group &&
7233 wpas_p2p_get_persistent(wpa_s->parent,
7234 params->peer->p2p_device_addr,
7235 ssid->ssid, ssid->ssid_len);
7236 wpa_s->parent->pending_invite_ssid_id = -1;
7238 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7239 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7240 ssid->ssid, ssid->ssid_len, ssid->frequency,
7241 wpa_s->global->p2p_dev_addr, persistent, 0,
7242 wpa_s->parent->p2p_oob_dev_pw_id);
7246 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7247 struct p2p_nfc_params *params,
7250 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7251 "connection handover");
7252 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7253 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7254 forced_freq, -1, 0, 1, 1);
7258 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7259 struct p2p_nfc_params *params,
7264 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7265 "connection handover");
7266 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7267 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7268 forced_freq, -1, 0, 1, 1);
7272 res = wpas_p2p_listen(wpa_s, 60);
7274 p2p_unauthorize(wpa_s->global->p2p,
7275 params->peer->p2p_device_addr);
7282 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7283 const struct wpabuf *data,
7284 int sel, int tag, int forced_freq)
7286 const u8 *pos, *end;
7288 struct p2p_nfc_params params;
7291 os_memset(¶ms, 0, sizeof(params));
7294 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7296 pos = wpabuf_head(data);
7297 end = pos + wpabuf_len(data);
7299 if (end - pos < 2) {
7300 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7304 len = WPA_GET_BE16(pos);
7306 if (pos + len > end) {
7307 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7311 params.wsc_attr = pos;
7312 params.wsc_len = len;
7315 if (end - pos < 2) {
7316 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7320 len = WPA_GET_BE16(pos);
7322 if (pos + len > end) {
7323 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7327 params.p2p_attr = pos;
7328 params.p2p_len = len;
7331 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7332 params.wsc_attr, params.wsc_len);
7333 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7334 params.p2p_attr, params.p2p_len);
7336 wpa_hexdump(MSG_DEBUG,
7337 "P2P: Ignored extra data after P2P attributes",
7341 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
7345 if (params.next_step == NO_ACTION)
7348 if (params.next_step == BOTH_GO) {
7349 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7350 MAC2STR(params.peer->p2p_device_addr));
7354 if (params.next_step == PEER_CLIENT) {
7355 if (!is_zero_ether_addr(params.go_dev_addr)) {
7356 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7357 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7359 MAC2STR(params.peer->p2p_device_addr),
7361 MAC2STR(params.go_dev_addr),
7362 wpa_ssid_txt(params.go_ssid,
7363 params.go_ssid_len));
7365 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7366 "peer=" MACSTR " freq=%d",
7367 MAC2STR(params.peer->p2p_device_addr),
7373 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7374 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7375 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7379 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7380 wpa_s->p2p_oob_dev_pw = NULL;
7382 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7383 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7388 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7389 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7390 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7391 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7392 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7393 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7394 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7398 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7399 "peer OOB Device Password Id %u", id);
7402 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7403 "Device Password Id %u", id);
7404 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7405 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7406 params.oob_dev_pw_len -
7407 WPS_OOB_PUBKEY_HASH_LEN - 2);
7408 wpa_s->p2p_oob_dev_pw_id = id;
7409 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7410 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7411 params.oob_dev_pw_len -
7412 WPS_OOB_PUBKEY_HASH_LEN - 2);
7413 if (wpa_s->p2p_oob_dev_pw == NULL)
7416 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7417 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7418 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7421 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7422 "without Device Password");
7423 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7426 switch (params.next_step) {
7430 /* already covered above */
7433 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
7435 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
7437 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
7439 /* TODO: use own OOB Dev Pw */
7440 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
7447 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7448 const struct wpabuf *data, int forced_freq)
7450 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7453 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7457 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7458 const struct wpabuf *req,
7459 const struct wpabuf *sel, int forced_freq)
7464 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7467 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7469 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7470 wpabuf_head(req), wpabuf_len(req));
7471 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7472 wpabuf_head(sel), wpabuf_len(sel));
7474 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7475 tmp = ndef_parse_p2p(init ? sel : req);
7477 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7481 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7489 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7492 int go_intent = wpa_s->conf->p2p_go_intent;
7493 struct wpa_supplicant *iface;
7495 if (wpa_s->global->p2p == NULL)
7499 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7500 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7502 if (!iface->ap_iface)
7504 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7506 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7508 if (wpa_s->p2p_nfc_tag_enabled)
7509 wpas_p2p_remove_pending_group_interface(wpa_s);
7510 wpa_s->p2p_nfc_tag_enabled = 0;
7514 if (wpa_s->global->p2p_disabled)
7517 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7518 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7519 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7520 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7521 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7522 "to allow static handover cases");
7526 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7528 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7529 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7530 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7531 if (wpa_s->p2p_oob_dev_pw == NULL)
7533 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7535 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7537 if (wpa_s->create_p2p_iface) {
7538 enum wpa_driver_if_type iftype;
7539 /* Prepare to add a new interface for the group */
7540 iftype = WPA_IF_P2P_GROUP;
7541 if (go_intent == 15)
7542 iftype = WPA_IF_P2P_GO;
7543 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7544 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7545 "interface for the group");
7549 if_addr = wpa_s->pending_interface_addr;
7551 if_addr = wpa_s->own_addr;
7553 wpa_s->p2p_nfc_tag_enabled = enabled;
7555 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7556 struct hostapd_data *hapd;
7557 if (iface->ap_iface == NULL)
7559 hapd = iface->ap_iface->bss[0];
7560 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7561 hapd->conf->wps_nfc_dh_pubkey =
7562 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7563 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7564 hapd->conf->wps_nfc_dh_privkey =
7565 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7566 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7567 hapd->conf->wps_nfc_dev_pw =
7568 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7569 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7571 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7572 wpa_dbg(iface, MSG_DEBUG,
7573 "P2P: Failed to enable NFC Tag for GO");
7576 p2p_set_authorized_oob_dev_pw_id(
7577 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
7583 #endif /* CONFIG_WPS_NFC */