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);
129 * Get the number of concurrent channels that the HW can operate, but that are
130 * currently not in use by any of the wpa_supplicant interfaces.
132 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
137 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
141 num = get_shared_radio_freqs(wpa_s, freqs,
142 wpa_s->num_multichan_concurrent);
145 unused = wpa_s->num_multichan_concurrent - num;
146 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
152 * Get the frequencies that are currently in use by one or more of the virtual
153 * interfaces, and that are also valid for P2P operation.
155 static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
156 int *p2p_freqs, unsigned int len)
159 unsigned int num, i, j;
161 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
165 num = get_shared_radio_freqs(wpa_s, freqs,
166 wpa_s->num_multichan_concurrent);
168 os_memset(p2p_freqs, 0, sizeof(int) * len);
170 for (i = 0, j = 0; i < num && j < len; i++) {
171 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
172 p2p_freqs[j++] = freqs[i];
177 dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
183 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
186 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
188 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
189 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
190 wpas_p2p_num_unused_channels(wpa_s) > 0) {
191 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
195 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
199 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
200 struct wpa_scan_results *scan_res)
204 if (wpa_s->p2p_scan_work) {
205 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
206 wpa_s->p2p_scan_work = NULL;
207 radio_work_done(work);
210 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
213 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
214 (int) scan_res->num);
216 for (i = 0; i < scan_res->num; i++) {
217 struct wpa_scan_res *bss = scan_res->res[i];
218 struct os_reltime time_tmp_age, entry_ts;
222 time_tmp_age.sec = bss->age / 1000;
223 time_tmp_age.usec = (bss->age % 1000) * 1000;
224 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
226 ies = (const u8 *) (bss + 1);
227 ies_len = bss->ie_len;
228 if (bss->beacon_ie_len > 0 &&
229 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
230 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
231 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
232 MACSTR, MAC2STR(bss->bssid));
234 ies_len = bss->beacon_ie_len;
238 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
239 bss->freq, &entry_ts, bss->level,
244 p2p_scan_res_handled(wpa_s->global->p2p);
248 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
250 struct wpa_supplicant *wpa_s = work->wpa_s;
251 struct wpa_driver_scan_params *params = work->ctx;
255 wpa_scan_free_params(params);
259 ret = wpa_drv_scan(wpa_s, params);
260 wpa_scan_free_params(params);
263 radio_work_done(work);
267 os_get_reltime(&wpa_s->scan_trigger_time);
268 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
269 wpa_s->own_scan_requested = 1;
270 wpa_s->p2p_scan_work = work;
274 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
275 unsigned int num_req_dev_types,
276 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
278 struct wpa_supplicant *wpa_s = ctx;
279 struct wpa_driver_scan_params *params = NULL;
280 struct wpabuf *wps_ie, *ies;
284 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
287 if (wpa_s->p2p_scan_work) {
288 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
292 params = os_zalloc(sizeof(*params));
296 /* P2P Wildcard SSID */
297 params->num_ssids = 1;
298 n = os_malloc(P2P_WILDCARD_SSID_LEN);
301 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
302 params->ssids[0].ssid = n;
303 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
305 wpa_s->wps->dev.p2p = 1;
306 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
307 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
308 num_req_dev_types, req_dev_types);
312 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
313 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
318 wpabuf_put_buf(ies, wps_ie);
321 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
323 params->p2p_probe = 1;
324 n = os_malloc(wpabuf_len(ies));
329 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
330 params->extra_ies = n;
331 params->extra_ies_len = wpabuf_len(ies);
335 case P2P_SCAN_SOCIAL:
336 params->freqs = os_malloc(4 * sizeof(int));
337 if (params->freqs == NULL)
339 params->freqs[0] = 2412;
340 params->freqs[1] = 2437;
341 params->freqs[2] = 2462;
342 params->freqs[3] = 0;
346 case P2P_SCAN_SOCIAL_PLUS_ONE:
347 params->freqs = os_malloc(5 * sizeof(int));
348 if (params->freqs == NULL)
350 params->freqs[0] = 2412;
351 params->freqs[1] = 2437;
352 params->freqs[2] = 2462;
353 params->freqs[3] = freq;
354 params->freqs[4] = 0;
358 radio_remove_unstarted_work(wpa_s, "p2p-scan");
359 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
365 wpa_scan_free_params(params);
370 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
372 switch (p2p_group_interface) {
373 case P2P_GROUP_INTERFACE_PENDING:
374 return WPA_IF_P2P_GROUP;
375 case P2P_GROUP_INTERFACE_GO:
376 return WPA_IF_P2P_GO;
377 case P2P_GROUP_INTERFACE_CLIENT:
378 return WPA_IF_P2P_CLIENT;
381 return WPA_IF_P2P_GROUP;
385 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
387 size_t ssid_len, int *go)
391 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
392 for (s = wpa_s->conf->ssid; s; s = s->next) {
393 if (s->disabled != 0 || !s->p2p_group ||
394 s->ssid_len != ssid_len ||
395 os_memcmp(ssid, s->ssid, ssid_len) != 0)
397 if (s->mode == WPAS_MODE_P2P_GO &&
398 s != wpa_s->current_ssid)
401 *go = s->mode == WPAS_MODE_P2P_GO;
410 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
411 enum p2p_group_removal_reason removal_reason)
413 struct wpa_ssid *ssid;
417 ssid = wpa_s->current_ssid;
420 * The current SSID was not known, but there may still be a
421 * pending P2P group interface waiting for provisioning or a
422 * P2P group that is trying to reconnect.
424 ssid = wpa_s->conf->ssid;
426 if (ssid->p2p_group && ssid->disabled != 2)
431 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
433 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
438 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
440 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
441 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
442 wpa_s->reassociate = 0;
443 wpa_s->disconnected = 1;
444 wpa_supplicant_deauthenticate(wpa_s,
445 WLAN_REASON_DEAUTH_LEAVING);
449 if (wpa_s->cross_connect_in_use) {
450 wpa_s->cross_connect_in_use = 0;
451 wpa_msg_global(wpa_s->parent, MSG_INFO,
452 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
453 wpa_s->ifname, wpa_s->cross_connect_uplink);
455 switch (removal_reason) {
456 case P2P_GROUP_REMOVAL_REQUESTED:
457 reason = " reason=REQUESTED";
459 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
460 reason = " reason=FORMATION_FAILED";
462 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
463 reason = " reason=IDLE";
465 case P2P_GROUP_REMOVAL_UNAVAILABLE:
466 reason = " reason=UNAVAILABLE";
468 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
469 reason = " reason=GO_ENDING_SESSION";
471 case P2P_GROUP_REMOVAL_PSK_FAILURE:
472 reason = " reason=PSK_FAILURE";
474 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
475 reason = " reason=FREQ_CONFLICT";
481 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
482 wpa_msg_global(wpa_s->parent, MSG_INFO,
483 P2P_EVENT_GROUP_REMOVED "%s %s%s",
484 wpa_s->ifname, gtype, reason);
487 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
488 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
489 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
490 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
491 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
492 wpa_s->parent, NULL) > 0) {
493 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
495 wpa_s->p2p_in_provisioning = 0;
499 * Make sure wait for the first client does not remain active after the
500 * group has been removed.
502 wpa_s->global->p2p_go_wait_client.sec = 0;
504 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
505 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
507 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
508 struct wpa_global *global;
510 enum wpa_driver_if_type type;
511 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
513 global = wpa_s->global;
514 ifname = os_strdup(wpa_s->ifname);
515 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
516 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
517 wpa_s = global->ifaces;
519 wpa_drv_if_remove(wpa_s, type, ifname);
524 if (!wpa_s->p2p_go_group_formation_completed) {
525 wpa_s->global->p2p_group_formation = NULL;
526 wpa_s->p2p_in_provisioning = 0;
529 wpa_s->show_group_started = 0;
530 os_free(wpa_s->go_params);
531 wpa_s->go_params = NULL;
533 wpa_s->waiting_presence_resp = 0;
535 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
536 if (ssid && (ssid->p2p_group ||
537 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
538 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
540 if (ssid == wpa_s->current_ssid) {
541 wpa_sm_set_config(wpa_s->wpa, NULL);
542 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
543 wpa_s->current_ssid = NULL;
546 * Networks objects created during any P2P activities are not
547 * exposed out as they might/will confuse certain non-P2P aware
548 * applications since these network objects won't behave like
551 * Likewise, we don't send out network removed signals for such
554 wpa_config_remove_network(wpa_s->conf, id);
555 wpa_supplicant_clear_status(wpa_s);
556 wpa_supplicant_cancel_sched_scan(wpa_s);
558 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
562 wpa_supplicant_ap_deinit(wpa_s);
564 wpa_drv_deinit_p2p_cli(wpa_s);
570 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
572 const u8 *ssid, size_t ssid_len)
580 if (wpa_s->go_params)
581 bssid = wpa_s->go_params->peer_interface_addr;
583 bssid = wpa_s->bssid;
585 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
586 if (bss == NULL && wpa_s->go_params &&
587 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
588 bss = wpa_bss_get_p2p_dev_addr(
589 wpa_s, wpa_s->go_params->peer_device_addr);
591 u8 iface_addr[ETH_ALEN];
592 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
594 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
597 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
598 "group is persistent - BSS " MACSTR " not found",
603 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
605 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
608 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
609 "group is persistent - BSS " MACSTR
610 " did not include P2P IE", MAC2STR(bssid));
611 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
612 (u8 *) (bss + 1), bss->ie_len);
613 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
614 ((u8 *) bss + 1) + bss->ie_len,
619 group_capab = p2p_get_group_capab(p2p);
620 addr = p2p_get_go_dev_addr(p2p);
621 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
622 "group_capab=0x%x", group_capab);
624 os_memcpy(go_dev_addr, addr, ETH_ALEN);
625 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
628 os_memset(go_dev_addr, 0, ETH_ALEN);
631 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
632 "go_dev_addr=" MACSTR,
633 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
635 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
639 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
640 struct wpa_ssid *ssid,
641 const u8 *go_dev_addr)
646 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
647 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
648 for (s = wpa_s->conf->ssid; s; s = s->next) {
649 if (s->disabled == 2 &&
650 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
651 s->ssid_len == ssid->ssid_len &&
652 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
657 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
659 if (ssid->passphrase && !s->passphrase)
661 else if (ssid->passphrase && s->passphrase &&
662 os_strcmp(ssid->passphrase, s->passphrase) != 0)
665 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
668 s = wpa_config_add_network(wpa_s->conf);
673 * Instead of network_added we emit persistent_group_added
674 * notification. Also to keep the defense checks in
675 * persistent_group obj registration method, we set the
676 * relevant flags in s to designate it as a persistent group.
679 s->p2p_persistent_group = 1;
680 wpas_notify_persistent_group_added(wpa_s, s);
681 wpa_config_set_network_defaults(s);
685 s->p2p_persistent_group = 1;
688 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
689 s->mode = ssid->mode;
690 s->auth_alg = WPA_AUTH_ALG_OPEN;
691 s->key_mgmt = WPA_KEY_MGMT_PSK;
692 s->proto = WPA_PROTO_RSN;
693 s->pairwise_cipher = WPA_CIPHER_CCMP;
695 if (ssid->passphrase) {
696 os_free(s->passphrase);
697 s->passphrase = os_strdup(ssid->passphrase);
701 os_memcpy(s->psk, ssid->psk, 32);
703 if (s->passphrase && !s->psk_set)
704 wpa_config_update_psk(s);
705 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
707 s->ssid = os_malloc(ssid->ssid_len);
710 s->ssid_len = ssid->ssid_len;
711 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
713 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
714 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
715 wpa_s->global->add_psk = NULL;
719 #ifndef CONFIG_NO_CONFIG_WRITE
720 if (changed && wpa_s->conf->update_config &&
721 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
722 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
724 #endif /* CONFIG_NO_CONFIG_WRITE */
730 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
733 struct wpa_ssid *ssid, *s;
738 ssid = wpa_s->current_ssid;
739 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
740 !ssid->p2p_persistent_group)
743 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
744 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
747 if (s->ssid_len == ssid->ssid_len &&
748 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
755 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
756 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
760 if (i == s->num_p2p_clients - 1)
761 return; /* already the most recent entry */
763 /* move the entry to mark it most recent */
764 os_memmove(s->p2p_client_list + i * ETH_ALEN,
765 s->p2p_client_list + (i + 1) * ETH_ALEN,
766 (s->num_p2p_clients - i - 1) * ETH_ALEN);
767 os_memcpy(s->p2p_client_list +
768 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
773 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
774 n = os_realloc_array(s->p2p_client_list,
775 s->num_p2p_clients + 1, ETH_ALEN);
778 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
779 s->p2p_client_list = n;
780 s->num_p2p_clients++;
782 /* Not enough room for an additional entry - drop the oldest
784 os_memmove(s->p2p_client_list,
785 s->p2p_client_list + ETH_ALEN,
786 (s->num_p2p_clients - 1) * ETH_ALEN);
787 os_memcpy(s->p2p_client_list +
788 (s->num_p2p_clients - 1) * ETH_ALEN,
792 #ifndef CONFIG_NO_CONFIG_WRITE
793 if (wpa_s->parent->conf->update_config &&
794 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
795 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
796 #endif /* CONFIG_NO_CONFIG_WRITE */
800 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
803 struct wpa_ssid *ssid;
804 const char *ssid_txt;
807 u8 go_dev_addr[ETH_ALEN];
811 * This callback is likely called for the main interface. Update wpa_s
812 * to use the group interface if a new interface was created for the
815 if (wpa_s->global->p2p_group_formation)
816 wpa_s = wpa_s->global->p2p_group_formation;
817 if (wpa_s->p2p_go_group_formation_completed) {
818 wpa_s->global->p2p_group_formation = NULL;
819 wpa_s->p2p_in_provisioning = 0;
823 wpa_msg_global(wpa_s->parent, MSG_INFO,
824 P2P_EVENT_GROUP_FORMATION_FAILURE);
825 wpas_p2p_group_delete(wpa_s,
826 P2P_GROUP_REMOVAL_FORMATION_FAILED);
830 wpa_msg_global(wpa_s->parent, MSG_INFO,
831 P2P_EVENT_GROUP_FORMATION_SUCCESS);
833 ssid = wpa_s->current_ssid;
834 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
835 ssid->mode = WPAS_MODE_P2P_GO;
836 p2p_group_notif_formation_done(wpa_s->p2p_group);
837 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
842 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
843 client = ssid->mode == WPAS_MODE_INFRA;
844 if (ssid->mode == WPAS_MODE_P2P_GO) {
845 persistent = ssid->p2p_persistent_group;
846 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
849 persistent = wpas_p2p_persistent_group(wpa_s,
855 client = wpa_s->p2p_group_interface ==
856 P2P_GROUP_INTERFACE_CLIENT;
857 os_memset(go_dev_addr, 0, ETH_ALEN);
860 wpa_s->show_group_started = 0;
863 * Indicate event only after successfully completed 4-way
864 * handshake, i.e., when the interface is ready for data
867 wpa_s->show_group_started = 1;
868 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
870 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
871 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
872 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
874 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
875 MAC2STR(go_dev_addr),
876 persistent ? " [PERSISTENT]" : "");
877 wpas_p2p_cross_connect_setup(wpa_s);
878 wpas_p2p_set_group_idle_timeout(wpa_s);
880 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
881 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
882 "go_dev_addr=" MACSTR "%s",
883 wpa_s->ifname, ssid_txt,
884 ssid ? ssid->frequency : 0,
885 ssid && ssid->passphrase ? ssid->passphrase : "",
886 MAC2STR(go_dev_addr),
887 persistent ? " [PERSISTENT]" : "");
888 wpas_p2p_cross_connect_setup(wpa_s);
889 wpas_p2p_set_group_idle_timeout(wpa_s);
893 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
896 os_free(wpa_s->global->add_psk);
897 wpa_s->global->add_psk = NULL;
899 if (network_id < 0 && ssid)
900 network_id = ssid->id;
902 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
903 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
908 struct send_action_work {
914 unsigned int wait_time;
919 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
922 struct wpa_supplicant *wpa_s = eloop_ctx;
924 if (!wpa_s->p2p_send_action_work)
927 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
928 os_free(wpa_s->p2p_send_action_work->ctx);
929 radio_work_done(wpa_s->p2p_send_action_work);
930 wpa_s->p2p_send_action_work = NULL;
934 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
936 const u8 *dst, const u8 *src,
938 const u8 *data, size_t data_len,
939 enum offchannel_send_action_result
942 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
944 if (wpa_s->p2p_send_action_work) {
945 struct send_action_work *awork;
946 awork = wpa_s->p2p_send_action_work->ctx;
947 if (awork->wait_time == 0) {
949 radio_work_done(wpa_s->p2p_send_action_work);
950 wpa_s->p2p_send_action_work = NULL;
953 * In theory, this should not be needed, but number of
954 * places in the P2P code is still using non-zero wait
955 * time for the last Action frame in the sequence and
956 * some of these do not call send_action_done().
958 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
960 eloop_register_timeout(
961 0, awork->wait_time * 1000,
962 wpas_p2p_send_action_work_timeout,
967 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
971 case OFFCHANNEL_SEND_ACTION_SUCCESS:
972 res = P2P_SEND_ACTION_SUCCESS;
974 case OFFCHANNEL_SEND_ACTION_NO_ACK:
975 res = P2P_SEND_ACTION_NO_ACK;
977 case OFFCHANNEL_SEND_ACTION_FAILED:
978 res = P2P_SEND_ACTION_FAILED;
982 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
984 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
985 wpa_s->pending_pd_before_join &&
986 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
987 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
988 wpa_s->p2p_fallback_to_go_neg) {
989 wpa_s->pending_pd_before_join = 0;
990 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
991 "during p2p_connect-auto");
992 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
998 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1000 struct wpa_supplicant *wpa_s = work->wpa_s;
1001 struct send_action_work *awork = work->ctx;
1008 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1009 awork->bssid, awork->buf, awork->len,
1011 wpas_p2p_send_action_tx_status, 1) < 0) {
1013 radio_work_done(work);
1016 wpa_s->p2p_send_action_work = work;
1020 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1021 unsigned int freq, const u8 *dst,
1022 const u8 *src, const u8 *bssid, const u8 *buf,
1023 size_t len, unsigned int wait_time)
1025 struct send_action_work *awork;
1027 if (wpa_s->p2p_send_action_work) {
1028 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1032 awork = os_zalloc(sizeof(*awork) + len);
1037 os_memcpy(awork->dst, dst, ETH_ALEN);
1038 os_memcpy(awork->src, src, ETH_ALEN);
1039 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1041 awork->wait_time = wait_time;
1042 os_memcpy(awork->buf, buf, len);
1044 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1045 wpas_send_action_cb, awork) < 0) {
1054 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1055 const u8 *src, const u8 *bssid, const u8 *buf,
1056 size_t len, unsigned int wait_time)
1058 struct wpa_supplicant *wpa_s = ctx;
1059 int listen_freq = -1, send_freq = -1;
1061 if (wpa_s->p2p_listen_work)
1062 listen_freq = wpa_s->p2p_listen_work->freq;
1063 if (wpa_s->p2p_send_action_work)
1064 send_freq = wpa_s->p2p_send_action_work->freq;
1065 if (listen_freq != (int) freq && send_freq != (int) freq) {
1066 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1067 listen_freq, send_freq);
1068 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1072 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1073 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1075 wpas_p2p_send_action_tx_status, 1);
1079 static void wpas_send_action_done(void *ctx)
1081 struct wpa_supplicant *wpa_s = ctx;
1083 if (wpa_s->p2p_send_action_work) {
1084 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1086 os_free(wpa_s->p2p_send_action_work->ctx);
1087 radio_work_done(wpa_s->p2p_send_action_work);
1088 wpa_s->p2p_send_action_work = NULL;
1091 offchannel_send_action_done(wpa_s);
1095 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1096 struct p2p_go_neg_results *params)
1098 if (wpa_s->go_params == NULL) {
1099 wpa_s->go_params = os_malloc(sizeof(*params));
1100 if (wpa_s->go_params == NULL)
1103 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1108 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1109 struct p2p_go_neg_results *res)
1111 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1112 " dev_addr " MACSTR " wps_method %d",
1113 MAC2STR(res->peer_interface_addr),
1114 MAC2STR(res->peer_device_addr), res->wps_method);
1115 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1116 res->ssid, res->ssid_len);
1117 wpa_supplicant_ap_deinit(wpa_s);
1118 wpas_copy_go_neg_results(wpa_s, res);
1119 if (res->wps_method == WPS_PBC) {
1120 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1121 #ifdef CONFIG_WPS_NFC
1122 } else if (res->wps_method == WPS_NFC) {
1123 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1124 res->peer_interface_addr,
1125 wpa_s->parent->p2p_oob_dev_pw,
1126 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1127 wpa_s->parent->p2p_oob_dev_pw_id ==
1128 DEV_PW_NFC_CONNECTION_HANDOVER ?
1129 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1132 #endif /* CONFIG_WPS_NFC */
1134 u16 dev_pw_id = DEV_PW_DEFAULT;
1135 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1136 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1137 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1138 wpa_s->p2p_pin, 1, dev_pw_id);
1143 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1144 struct wpa_ssid *ssid)
1146 struct wpa_ssid *persistent;
1147 struct psk_list_entry *psk;
1148 struct hostapd_data *hapd;
1150 if (!wpa_s->ap_iface)
1153 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1155 if (persistent == NULL)
1158 hapd = wpa_s->ap_iface->bss[0];
1160 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1162 struct hostapd_wpa_psk *hpsk;
1164 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1166 MAC2STR(psk->addr), psk->p2p);
1167 hpsk = os_zalloc(sizeof(*hpsk));
1170 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1172 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1174 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1175 hpsk->next = hapd->conf->ssid.wpa_psk;
1176 hapd->conf->ssid.wpa_psk = hpsk;
1181 static void p2p_go_configured(void *ctx, void *data)
1183 struct wpa_supplicant *wpa_s = ctx;
1184 struct p2p_go_neg_results *params = data;
1185 struct wpa_ssid *ssid;
1186 int network_id = -1;
1188 ssid = wpa_s->current_ssid;
1189 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1190 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1191 if (wpa_s->global->p2p_group_formation == wpa_s)
1192 wpa_s->global->p2p_group_formation = NULL;
1193 if (os_strlen(params->passphrase) > 0) {
1194 wpa_msg_global(wpa_s->parent, MSG_INFO,
1195 P2P_EVENT_GROUP_STARTED
1196 "%s GO ssid=\"%s\" freq=%d "
1197 "passphrase=\"%s\" go_dev_addr=" MACSTR
1198 "%s", wpa_s->ifname,
1199 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1200 ssid->frequency, params->passphrase,
1201 MAC2STR(wpa_s->global->p2p_dev_addr),
1202 params->persistent_group ?
1203 " [PERSISTENT]" : "");
1206 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1207 sizeof(params->psk));
1208 wpa_msg_global(wpa_s->parent, MSG_INFO,
1209 P2P_EVENT_GROUP_STARTED
1210 "%s GO ssid=\"%s\" freq=%d psk=%s "
1211 "go_dev_addr=" MACSTR "%s",
1213 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1214 ssid->frequency, psk,
1215 MAC2STR(wpa_s->global->p2p_dev_addr),
1216 params->persistent_group ?
1217 " [PERSISTENT]" : "");
1220 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1221 if (params->persistent_group) {
1222 network_id = wpas_p2p_store_persistent_group(
1223 wpa_s->parent, ssid,
1224 wpa_s->global->p2p_dev_addr);
1225 wpas_p2p_add_psk_list(wpa_s, ssid);
1228 network_id = ssid->id;
1229 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1230 wpas_p2p_cross_connect_setup(wpa_s);
1231 wpas_p2p_set_group_idle_timeout(wpa_s);
1233 if (wpa_s->p2p_first_connection_timeout) {
1234 wpa_dbg(wpa_s, MSG_DEBUG,
1235 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1236 wpa_s->p2p_first_connection_timeout);
1237 wpa_s->p2p_go_group_formation_completed = 0;
1238 wpa_s->global->p2p_group_formation = wpa_s;
1239 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1240 wpa_s->parent, NULL);
1241 eloop_register_timeout(
1242 wpa_s->p2p_first_connection_timeout, 0,
1243 wpas_p2p_group_formation_timeout,
1244 wpa_s->parent, NULL);
1250 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1251 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1252 params->peer_interface_addr)) {
1253 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1257 if (params->wps_method == WPS_PBC) {
1258 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1259 params->peer_device_addr);
1260 #ifdef CONFIG_WPS_NFC
1261 } else if (params->wps_method == WPS_NFC) {
1262 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1263 DEV_PW_NFC_CONNECTION_HANDOVER &&
1264 !wpa_s->parent->p2p_oob_dev_pw) {
1265 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1268 wpas_ap_wps_add_nfc_pw(
1269 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1270 wpa_s->parent->p2p_oob_dev_pw,
1271 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1272 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1273 #endif /* CONFIG_WPS_NFC */
1274 } else if (wpa_s->p2p_pin[0])
1275 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1276 wpa_s->p2p_pin, NULL, 0, 0);
1277 os_free(wpa_s->go_params);
1278 wpa_s->go_params = NULL;
1282 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1283 struct p2p_go_neg_results *params,
1284 int group_formation)
1286 struct wpa_ssid *ssid;
1288 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1289 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1290 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1295 ssid = wpa_config_add_network(wpa_s->conf);
1297 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1301 wpa_s->show_group_started = 0;
1303 wpa_config_set_network_defaults(ssid);
1304 ssid->temporary = 1;
1305 ssid->p2p_group = 1;
1306 ssid->p2p_persistent_group = params->persistent_group;
1307 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1309 ssid->frequency = params->freq;
1310 ssid->ht40 = params->ht40;
1311 ssid->vht = params->vht;
1312 ssid->ssid = os_zalloc(params->ssid_len + 1);
1314 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1315 ssid->ssid_len = params->ssid_len;
1317 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1318 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1319 ssid->proto = WPA_PROTO_RSN;
1320 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1321 if (os_strlen(params->passphrase) > 0) {
1322 ssid->passphrase = os_strdup(params->passphrase);
1323 if (ssid->passphrase == NULL) {
1324 wpa_msg_global(wpa_s, MSG_ERROR,
1325 "P2P: Failed to copy passphrase for GO");
1326 wpa_config_remove_network(wpa_s->conf, ssid->id);
1330 ssid->passphrase = NULL;
1331 ssid->psk_set = params->psk_set;
1333 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1334 else if (ssid->passphrase)
1335 wpa_config_update_psk(ssid);
1336 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1338 wpa_s->ap_configured_cb = p2p_go_configured;
1339 wpa_s->ap_configured_cb_ctx = wpa_s;
1340 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1341 wpa_s->connect_without_scan = ssid;
1342 wpa_s->reassociate = 1;
1343 wpa_s->disconnected = 0;
1344 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1346 wpa_supplicant_req_scan(wpa_s, 0, 0);
1350 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1351 const struct wpa_supplicant *src)
1353 struct wpa_config *d;
1354 const struct wpa_config *s;
1359 #define C(n) if (s->n) d->n = os_strdup(s->n)
1368 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1369 os_memcpy(d->sec_device_type, s->sec_device_type,
1370 sizeof(d->sec_device_type));
1371 d->num_sec_device_types = s->num_sec_device_types;
1373 d->p2p_group_idle = s->p2p_group_idle;
1374 d->p2p_intra_bss = s->p2p_intra_bss;
1375 d->persistent_reconnect = s->persistent_reconnect;
1376 d->max_num_sta = s->max_num_sta;
1377 d->pbc_in_m1 = s->pbc_in_m1;
1378 d->ignore_old_scan_res = s->ignore_old_scan_res;
1379 d->beacon_int = s->beacon_int;
1380 d->dtim_period = s->dtim_period;
1381 d->disassoc_low_ack = s->disassoc_low_ack;
1382 d->disable_scan_offload = s->disable_scan_offload;
1384 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1385 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1386 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1391 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1392 char *ifname, size_t len)
1394 char *ifname_ptr = wpa_s->ifname;
1396 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1397 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1398 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1401 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1402 if (os_strlen(ifname) >= IFNAMSIZ &&
1403 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1404 /* Try to avoid going over the IFNAMSIZ length limit */
1405 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1410 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1411 enum wpa_driver_if_type type)
1413 char ifname[120], force_ifname[120];
1415 if (wpa_s->pending_interface_name[0]) {
1416 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1417 "- skip creation of a new one");
1418 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1419 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1420 "unknown?! ifname='%s'",
1421 wpa_s->pending_interface_name);
1427 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1428 force_ifname[0] = '\0';
1430 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1432 wpa_s->p2p_group_idx++;
1434 wpa_s->pending_interface_type = type;
1435 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1436 wpa_s->pending_interface_addr, NULL) < 0) {
1437 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1442 if (force_ifname[0]) {
1443 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1445 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1446 sizeof(wpa_s->pending_interface_name));
1448 os_strlcpy(wpa_s->pending_interface_name, ifname,
1449 sizeof(wpa_s->pending_interface_name));
1450 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1451 MACSTR, wpa_s->pending_interface_name,
1452 MAC2STR(wpa_s->pending_interface_addr));
1458 static void wpas_p2p_remove_pending_group_interface(
1459 struct wpa_supplicant *wpa_s)
1461 if (!wpa_s->pending_interface_name[0] ||
1462 is_zero_ether_addr(wpa_s->pending_interface_addr))
1463 return; /* No pending virtual interface */
1465 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1466 wpa_s->pending_interface_name);
1467 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1468 wpa_s->pending_interface_name);
1469 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1470 wpa_s->pending_interface_name[0] = '\0';
1474 static struct wpa_supplicant *
1475 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1477 struct wpa_interface iface;
1478 struct wpa_supplicant *group_wpa_s;
1480 if (!wpa_s->pending_interface_name[0]) {
1481 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1482 if (!wpas_p2p_create_iface(wpa_s))
1485 * Something has forced us to remove the pending interface; try
1486 * to create a new one and hope for the best that we will get
1487 * the same local address.
1489 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1490 WPA_IF_P2P_CLIENT) < 0)
1494 os_memset(&iface, 0, sizeof(iface));
1495 iface.ifname = wpa_s->pending_interface_name;
1496 iface.driver = wpa_s->driver->name;
1497 if (wpa_s->conf->ctrl_interface == NULL &&
1498 wpa_s->parent != wpa_s &&
1500 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1501 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1503 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1504 iface.driver_param = wpa_s->conf->driver_param;
1505 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1506 if (group_wpa_s == NULL) {
1507 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1508 "wpa_supplicant interface");
1511 wpa_s->pending_interface_name[0] = '\0';
1512 group_wpa_s->parent = wpa_s;
1513 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1514 P2P_GROUP_INTERFACE_CLIENT;
1515 wpa_s->global->p2p_group_formation = group_wpa_s;
1517 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1523 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1526 struct wpa_supplicant *wpa_s = eloop_ctx;
1527 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1528 wpas_p2p_group_formation_failed(wpa_s);
1532 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1534 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1535 wpa_s->parent, NULL);
1536 if (wpa_s->global->p2p)
1537 p2p_group_formation_failed(wpa_s->global->p2p);
1538 wpas_group_formation_completed(wpa_s, 0);
1542 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1544 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1545 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1546 wpa_s->parent, NULL);
1547 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1548 wpa_s->parent, NULL);
1549 wpa_s->global->p2p_fail_on_wps_complete = 0;
1553 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1555 if (wpa_s->global->p2p_group_formation != wpa_s)
1557 /* Speed up group formation timeout since this cannot succeed */
1558 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1559 wpa_s->parent, NULL);
1560 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1561 wpa_s->parent, NULL);
1565 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1567 struct wpa_supplicant *wpa_s = ctx;
1569 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1570 wpa_drv_cancel_remain_on_channel(wpa_s);
1571 wpa_s->off_channel_freq = 0;
1572 wpa_s->roc_waiting_drv_freq = 0;
1576 wpa_msg_global(wpa_s, MSG_INFO,
1577 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1579 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1580 wpas_p2p_remove_pending_group_interface(wpa_s);
1584 if (wpa_s->p2p_go_ht40)
1586 if (wpa_s->p2p_go_vht)
1589 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1590 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1592 res->role_go ? "GO" : "client", res->freq, res->ht40,
1593 MAC2STR(res->peer_device_addr),
1594 MAC2STR(res->peer_interface_addr),
1595 p2p_wps_method_text(res->wps_method));
1596 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1598 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1599 struct wpa_ssid *ssid;
1600 ssid = wpa_config_get_network(wpa_s->conf,
1601 wpa_s->p2p_persistent_id);
1602 if (ssid && ssid->disabled == 2 &&
1603 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1604 size_t len = os_strlen(ssid->passphrase);
1605 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1606 "on requested persistent group");
1607 os_memcpy(res->passphrase, ssid->passphrase, len);
1608 res->passphrase[len] = '\0';
1612 if (wpa_s->create_p2p_iface) {
1613 struct wpa_supplicant *group_wpa_s =
1614 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1615 if (group_wpa_s == NULL) {
1616 wpas_p2p_remove_pending_group_interface(wpa_s);
1619 if (group_wpa_s != wpa_s) {
1620 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1621 sizeof(group_wpa_s->p2p_pin));
1622 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1624 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1625 wpa_s->pending_interface_name[0] = '\0';
1626 group_wpa_s->p2p_in_provisioning = 1;
1629 wpas_start_wps_go(group_wpa_s, res, 1);
1631 wpas_start_wps_enrollee(group_wpa_s, res);
1633 wpa_s->p2p_in_provisioning = 1;
1634 wpa_s->global->p2p_group_formation = wpa_s;
1637 wpas_start_wps_go(wpa_s, res, 1);
1639 wpas_start_wps_enrollee(ctx, res);
1642 wpa_s->p2p_long_listen = 0;
1643 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1645 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1646 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1647 (res->peer_config_timeout % 100) * 10000,
1648 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1652 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1654 struct wpa_supplicant *wpa_s = ctx;
1655 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1656 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1658 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1662 static void wpas_dev_found(void *ctx, const u8 *addr,
1663 const struct p2p_peer_info *info,
1666 #ifndef CONFIG_NO_STDOUT_DEBUG
1667 struct wpa_supplicant *wpa_s = ctx;
1668 char devtype[WPS_DEV_TYPE_BUFSIZE];
1669 char *wfd_dev_info_hex = NULL;
1671 #ifdef CONFIG_WIFI_DISPLAY
1672 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1673 WFD_SUBELEM_DEVICE_INFO);
1674 #endif /* CONFIG_WIFI_DISPLAY */
1676 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1677 " p2p_dev_addr=" MACSTR
1678 " pri_dev_type=%s name='%s' config_methods=0x%x "
1679 "dev_capab=0x%x group_capab=0x%x%s%s",
1680 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1681 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1683 info->device_name, info->config_methods,
1684 info->dev_capab, info->group_capab,
1685 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1686 wfd_dev_info_hex ? wfd_dev_info_hex : "");
1688 os_free(wfd_dev_info_hex);
1689 #endif /* CONFIG_NO_STDOUT_DEBUG */
1691 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1695 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1697 struct wpa_supplicant *wpa_s = ctx;
1699 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1700 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1702 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1706 static void wpas_find_stopped(void *ctx)
1708 struct wpa_supplicant *wpa_s = ctx;
1709 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1713 struct wpas_p2p_listen_work {
1715 unsigned int duration;
1716 struct wpabuf *probe_resp_ie;
1720 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1724 wpabuf_free(lwork->probe_resp_ie);
1729 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1731 struct wpas_p2p_listen_work *lwork;
1733 if (!wpa_s->p2p_listen_work)
1736 lwork = wpa_s->p2p_listen_work->ctx;
1737 wpas_p2p_listen_work_free(lwork);
1738 radio_work_done(wpa_s->p2p_listen_work);
1739 wpa_s->p2p_listen_work = NULL;
1743 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1745 struct wpa_supplicant *wpa_s = work->wpa_s;
1746 struct wpas_p2p_listen_work *lwork = work->ctx;
1749 wpas_p2p_listen_work_free(lwork);
1753 wpa_s->p2p_listen_work = work;
1755 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1757 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1758 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1759 "report received Probe Request frames");
1760 wpas_p2p_listen_work_done(wpa_s);
1764 wpa_s->pending_listen_freq = lwork->freq;
1765 wpa_s->pending_listen_duration = lwork->duration;
1767 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1769 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1770 "to remain on channel (%u MHz) for Listen "
1771 "state", lwork->freq);
1772 wpas_p2p_listen_work_done(wpa_s);
1773 wpa_s->pending_listen_freq = 0;
1776 wpa_s->off_channel_freq = 0;
1777 wpa_s->roc_waiting_drv_freq = lwork->freq;
1781 static int wpas_start_listen(void *ctx, unsigned int freq,
1782 unsigned int duration,
1783 const struct wpabuf *probe_resp_ie)
1785 struct wpa_supplicant *wpa_s = ctx;
1786 struct wpas_p2p_listen_work *lwork;
1788 if (wpa_s->p2p_listen_work) {
1789 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
1793 lwork = os_zalloc(sizeof(*lwork));
1797 lwork->duration = duration;
1798 if (probe_resp_ie) {
1799 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1800 if (lwork->probe_resp_ie == NULL) {
1801 wpas_p2p_listen_work_free(lwork);
1806 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1808 wpas_p2p_listen_work_free(lwork);
1816 static void wpas_stop_listen(void *ctx)
1818 struct wpa_supplicant *wpa_s = ctx;
1819 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1820 wpa_drv_cancel_remain_on_channel(wpa_s);
1821 wpa_s->off_channel_freq = 0;
1822 wpa_s->roc_waiting_drv_freq = 0;
1824 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1825 wpa_drv_probe_req_report(wpa_s, 0);
1826 wpas_p2p_listen_work_done(wpa_s);
1830 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1832 struct wpa_supplicant *wpa_s = ctx;
1833 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1838 * DNS Header section is used only to calculate compression pointers, so the
1839 * contents of this data does not matter, but the length needs to be reserved
1840 * in the virtual packet.
1842 #define DNS_HEADER_LEN 12
1845 * 27-octet in-memory packet from P2P specification containing two implied
1846 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1848 #define P2P_SD_IN_MEMORY_LEN 27
1850 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1851 u8 **spos, const u8 *end)
1853 while (*spos < end) {
1854 u8 val = ((*spos)[0] & 0xc0) >> 6;
1857 if (val == 1 || val == 2) {
1858 /* These are reserved values in RFC 1035 */
1859 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1860 "sequence starting with 0x%x", val);
1869 if (*spos + 2 > end) {
1870 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1871 "DNS offset field");
1875 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1876 if (offset >= *spos - start) {
1877 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1878 "pointer offset %u", offset);
1883 spos_tmp = start + offset;
1884 return p2p_sd_dns_uncompress_label(upos, uend, start,
1890 len = (*spos)[0] & 0x3f;
1895 if (*spos + len > end) {
1896 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1897 "sequence - no room for label with length "
1902 if (*upos + len + 2 > uend)
1905 os_memcpy(*upos, *spos, len);
1917 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1918 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1919 * not large enough */
1920 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1921 size_t msg_len, size_t offset)
1923 /* 27-octet in-memory packet from P2P specification */
1924 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1925 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1926 u8 *tmp, *end, *spos;
1932 if (offset > msg_len)
1935 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1938 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1939 end = spos + msg_len;
1942 os_memset(tmp, 0, DNS_HEADER_LEN);
1943 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1944 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1947 uend = buf + buf_len;
1949 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1958 } else if (upos[-1] == '.')
1966 static struct p2p_srv_bonjour *
1967 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1968 const struct wpabuf *query)
1970 struct p2p_srv_bonjour *bsrv;
1973 len = wpabuf_len(query);
1974 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1975 struct p2p_srv_bonjour, list) {
1976 if (len == wpabuf_len(bsrv->query) &&
1977 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1985 static struct p2p_srv_upnp *
1986 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1987 const char *service)
1989 struct p2p_srv_upnp *usrv;
1991 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1992 struct p2p_srv_upnp, list) {
1993 if (version == usrv->version &&
1994 os_strcmp(service, usrv->service) == 0)
2001 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2006 if (wpabuf_tailroom(resp) < 5)
2009 /* Length (to be filled) */
2010 len_pos = wpabuf_put(resp, 2);
2011 wpabuf_put_u8(resp, srv_proto);
2012 wpabuf_put_u8(resp, srv_trans_id);
2014 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2015 /* Response Data: empty */
2016 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2020 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2021 struct wpabuf *resp, u8 srv_trans_id)
2023 struct p2p_srv_bonjour *bsrv;
2026 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2028 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2029 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2033 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2034 struct p2p_srv_bonjour, list) {
2035 if (wpabuf_tailroom(resp) <
2036 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2038 /* Length (to be filled) */
2039 len_pos = wpabuf_put(resp, 2);
2040 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2041 wpabuf_put_u8(resp, srv_trans_id);
2043 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2044 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2045 wpabuf_head(bsrv->resp),
2046 wpabuf_len(bsrv->resp));
2048 wpabuf_put_buf(resp, bsrv->query); /* Key */
2049 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2050 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2056 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2059 char str_rx[256], str_srv[256];
2061 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2062 return 0; /* Too short to include DNS Type and Version */
2063 if (os_memcmp(query + query_len - 3,
2064 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2066 return 0; /* Mismatch in DNS Type or Version */
2067 if (query_len == wpabuf_len(bsrv->query) &&
2068 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2069 return 1; /* Binary match */
2071 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2073 return 0; /* Failed to uncompress query */
2074 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2075 wpabuf_head(bsrv->query),
2076 wpabuf_len(bsrv->query) - 3, 0))
2077 return 0; /* Failed to uncompress service */
2079 return os_strcmp(str_rx, str_srv) == 0;
2083 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2084 struct wpabuf *resp, u8 srv_trans_id,
2085 const u8 *query, size_t query_len)
2087 struct p2p_srv_bonjour *bsrv;
2091 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2093 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2094 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2095 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2100 if (query_len == 0) {
2101 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2105 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2106 struct p2p_srv_bonjour, list) {
2107 if (!match_bonjour_query(bsrv, query, query_len))
2110 if (wpabuf_tailroom(resp) <
2111 5 + query_len + wpabuf_len(bsrv->resp))
2116 /* Length (to be filled) */
2117 len_pos = wpabuf_put(resp, 2);
2118 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2119 wpabuf_put_u8(resp, srv_trans_id);
2122 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2123 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2124 wpabuf_head(bsrv->resp),
2125 wpabuf_len(bsrv->resp));
2128 wpabuf_put_data(resp, query, query_len); /* Key */
2129 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2131 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2135 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2137 if (wpabuf_tailroom(resp) < 5)
2140 /* Length (to be filled) */
2141 len_pos = wpabuf_put(resp, 2);
2142 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2143 wpabuf_put_u8(resp, srv_trans_id);
2146 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2147 /* Response Data: empty */
2148 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2154 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2155 struct wpabuf *resp, u8 srv_trans_id)
2157 struct p2p_srv_upnp *usrv;
2160 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2162 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2163 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2167 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2168 struct p2p_srv_upnp, list) {
2169 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2172 /* Length (to be filled) */
2173 len_pos = wpabuf_put(resp, 2);
2174 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2175 wpabuf_put_u8(resp, srv_trans_id);
2178 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2180 wpabuf_put_u8(resp, usrv->version);
2181 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2183 wpabuf_put_str(resp, usrv->service);
2184 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2190 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2191 struct wpabuf *resp, u8 srv_trans_id,
2192 const u8 *query, size_t query_len)
2194 struct p2p_srv_upnp *usrv;
2200 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2203 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2204 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2205 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2210 if (query_len == 0) {
2211 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2215 if (wpabuf_tailroom(resp) < 5)
2218 /* Length (to be filled) */
2219 len_pos = wpabuf_put(resp, 2);
2220 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2221 wpabuf_put_u8(resp, srv_trans_id);
2224 str = os_malloc(query_len);
2227 os_memcpy(str, query + 1, query_len - 1);
2228 str[query_len - 1] = '\0';
2230 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2231 struct p2p_srv_upnp, list) {
2232 if (version != usrv->version)
2235 if (os_strcmp(str, "ssdp:all") != 0 &&
2236 os_strstr(usrv->service, str) == NULL)
2239 if (wpabuf_tailroom(resp) < 2)
2243 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2245 wpabuf_put_u8(resp, version);
2247 wpabuf_put_u8(resp, ',');
2251 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2253 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2255 wpabuf_put_str(resp, usrv->service);
2260 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2263 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2264 /* Response Data: empty */
2267 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2271 #ifdef CONFIG_WIFI_DISPLAY
2272 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2273 struct wpabuf *resp, u8 srv_trans_id,
2274 const u8 *query, size_t query_len)
2280 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2282 if (!wpa_s->global->wifi_display) {
2283 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2284 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2289 if (query_len < 1) {
2290 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2295 if (wpabuf_tailroom(resp) < 5)
2300 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2302 /* TODO: role specific handling */
2304 /* Length (to be filled) */
2305 len_pos = wpabuf_put(resp, 2);
2306 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2307 wpabuf_put_u8(resp, srv_trans_id);
2308 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2310 while (pos < query + query_len) {
2311 if (*pos < MAX_WFD_SUBELEMS &&
2312 wpa_s->global->wfd_subelem[*pos] &&
2313 wpabuf_tailroom(resp) >=
2314 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2315 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2316 "subelement %u", *pos);
2317 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2322 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2324 #endif /* CONFIG_WIFI_DISPLAY */
2327 static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2328 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2330 struct wpa_supplicant *wpa_s = ctx;
2331 const u8 *pos = tlvs;
2332 const u8 *end = tlvs + tlvs_len;
2335 struct wpabuf *resp;
2336 u8 srv_proto, srv_trans_id;
2340 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2342 buf_len = 2 * tlvs_len + 1;
2343 buf = os_malloc(buf_len);
2345 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2346 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2348 freq, MAC2STR(sa), dialog_token, update_indic,
2353 if (wpa_s->p2p_sd_over_ctrl_iface) {
2354 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2355 update_indic, tlvs, tlvs_len);
2356 return; /* to be processed by an external program */
2359 resp = wpabuf_alloc(10000);
2363 while (pos + 1 < end) {
2364 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2365 slen = WPA_GET_LE16(pos);
2367 if (pos + slen > end || slen < 2) {
2368 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2373 tlv_end = pos + slen;
2376 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2378 srv_trans_id = *pos++;
2379 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2382 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2383 pos, tlv_end - pos);
2386 if (wpa_s->force_long_sd) {
2387 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2389 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2390 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2394 switch (srv_proto) {
2395 case P2P_SERV_ALL_SERVICES:
2396 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2397 "for all services");
2398 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2399 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2400 wpa_printf(MSG_DEBUG, "P2P: No service "
2401 "discovery protocols available");
2402 wpas_sd_add_proto_not_avail(
2403 resp, P2P_SERV_ALL_SERVICES,
2407 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2408 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2410 case P2P_SERV_BONJOUR:
2411 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2412 pos, tlv_end - pos);
2415 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2416 pos, tlv_end - pos);
2418 #ifdef CONFIG_WIFI_DISPLAY
2419 case P2P_SERV_WIFI_DISPLAY:
2420 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2421 pos, tlv_end - pos);
2423 #endif /* CONFIG_WIFI_DISPLAY */
2425 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2426 "protocol %u", srv_proto);
2427 wpas_sd_add_proto_not_avail(resp, srv_proto,
2436 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2437 update_indic, tlvs, tlvs_len);
2439 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2445 static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2446 const u8 *tlvs, size_t tlvs_len)
2448 struct wpa_supplicant *wpa_s = ctx;
2449 const u8 *pos = tlvs;
2450 const u8 *end = tlvs + tlvs_len;
2456 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2458 if (tlvs_len > 1500) {
2459 /* TODO: better way for handling this */
2460 wpa_msg_ctrl(wpa_s, MSG_INFO,
2461 P2P_EVENT_SERV_DISC_RESP MACSTR
2462 " %u <long response: %u bytes>",
2463 MAC2STR(sa), update_indic,
2464 (unsigned int) tlvs_len);
2466 buf_len = 2 * tlvs_len + 1;
2467 buf = os_malloc(buf_len);
2469 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2470 wpa_msg_ctrl(wpa_s, MSG_INFO,
2471 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2472 MAC2STR(sa), update_indic, buf);
2478 u8 srv_proto, srv_trans_id, status;
2480 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2481 slen = WPA_GET_LE16(pos);
2483 if (pos + slen > end || slen < 3) {
2484 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2488 tlv_end = pos + slen;
2491 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2493 srv_trans_id = *pos++;
2494 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2497 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2500 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2501 pos, tlv_end - pos);
2506 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2510 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2511 const struct wpabuf *tlvs)
2513 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2515 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2519 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2520 u8 version, const char *query)
2522 struct wpabuf *tlvs;
2525 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2528 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2529 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2530 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2531 wpabuf_put_u8(tlvs, version);
2532 wpabuf_put_str(tlvs, query);
2533 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2539 #ifdef CONFIG_WIFI_DISPLAY
2541 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2542 const struct wpabuf *tlvs)
2544 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2546 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2550 #define MAX_WFD_SD_SUBELEMS 20
2552 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2553 const char *subelems)
2560 len = wpabuf_put(tlvs, 2);
2561 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2562 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2564 wpabuf_put_u8(tlvs, role);
2569 if (val >= 0 && val < 256) {
2570 wpabuf_put_u8(tlvs, val);
2572 if (count == MAX_WFD_SD_SUBELEMS)
2575 pos = os_strchr(pos + 1, ',');
2581 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2585 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2586 const u8 *dst, const char *role)
2588 struct wpabuf *tlvs;
2590 const char *subelems;
2593 subelems = os_strchr(role, ' ');
2594 if (subelems == NULL)
2598 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2602 if (os_strstr(role, "[source]"))
2603 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2604 if (os_strstr(role, "[pri-sink]"))
2605 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2606 if (os_strstr(role, "[sec-sink]"))
2607 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2608 if (os_strstr(role, "[source+sink]"))
2609 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2611 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2616 #endif /* CONFIG_WIFI_DISPLAY */
2619 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2621 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2623 return p2p_sd_cancel_request(wpa_s->global->p2p,
2624 (void *) (uintptr_t) req);
2628 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2629 const u8 *dst, u8 dialog_token,
2630 const struct wpabuf *resp_tlvs)
2632 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2634 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2639 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2641 if (wpa_s->global->p2p)
2642 p2p_sd_service_update(wpa_s->global->p2p);
2646 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2648 dl_list_del(&bsrv->list);
2649 wpabuf_free(bsrv->query);
2650 wpabuf_free(bsrv->resp);
2655 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2657 dl_list_del(&usrv->list);
2658 os_free(usrv->service);
2663 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2665 struct p2p_srv_bonjour *bsrv, *bn;
2666 struct p2p_srv_upnp *usrv, *un;
2668 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2669 struct p2p_srv_bonjour, list)
2670 wpas_p2p_srv_bonjour_free(bsrv);
2672 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2673 struct p2p_srv_upnp, list)
2674 wpas_p2p_srv_upnp_free(usrv);
2676 wpas_p2p_sd_service_update(wpa_s);
2680 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2681 struct wpabuf *query, struct wpabuf *resp)
2683 struct p2p_srv_bonjour *bsrv;
2685 bsrv = os_zalloc(sizeof(*bsrv));
2688 bsrv->query = query;
2690 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2692 wpas_p2p_sd_service_update(wpa_s);
2697 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2698 const struct wpabuf *query)
2700 struct p2p_srv_bonjour *bsrv;
2702 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2705 wpas_p2p_srv_bonjour_free(bsrv);
2706 wpas_p2p_sd_service_update(wpa_s);
2711 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2712 const char *service)
2714 struct p2p_srv_upnp *usrv;
2716 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2717 return 0; /* Already listed */
2718 usrv = os_zalloc(sizeof(*usrv));
2721 usrv->version = version;
2722 usrv->service = os_strdup(service);
2723 if (usrv->service == NULL) {
2727 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2729 wpas_p2p_sd_service_update(wpa_s);
2734 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2735 const char *service)
2737 struct p2p_srv_upnp *usrv;
2739 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2742 wpas_p2p_srv_upnp_free(usrv);
2743 wpas_p2p_sd_service_update(wpa_s);
2748 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2749 const u8 *peer, const char *params,
2750 unsigned int generated_pin)
2752 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2753 " %08d%s", MAC2STR(peer), generated_pin, params);
2757 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2758 const u8 *peer, const char *params)
2760 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2761 "%s", MAC2STR(peer), params);
2765 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2766 const u8 *dev_addr, const u8 *pri_dev_type,
2767 const char *dev_name, u16 supp_config_methods,
2768 u8 dev_capab, u8 group_capab, const u8 *group_id,
2769 size_t group_id_len)
2771 struct wpa_supplicant *wpa_s = ctx;
2772 char devtype[WPS_DEV_TYPE_BUFSIZE];
2774 u8 empty_dev_type[8];
2775 unsigned int generated_pin = 0;
2776 struct wpa_supplicant *group = NULL;
2779 for (group = wpa_s->global->ifaces; group; group = group->next)
2781 struct wpa_ssid *s = group->current_ssid;
2783 s->mode == WPAS_MODE_P2P_GO &&
2784 group_id_len - ETH_ALEN == s->ssid_len &&
2785 os_memcmp(group_id + ETH_ALEN, s->ssid,
2791 if (pri_dev_type == NULL) {
2792 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2793 pri_dev_type = empty_dev_type;
2795 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2796 " pri_dev_type=%s name='%s' config_methods=0x%x "
2797 "dev_capab=0x%x group_capab=0x%x%s%s",
2799 wps_dev_type_bin2str(pri_dev_type, devtype,
2801 dev_name, supp_config_methods, dev_capab, group_capab,
2802 group ? " group=" : "",
2803 group ? group->ifname : "");
2804 params[sizeof(params) - 1] = '\0';
2806 if (config_methods & WPS_CONFIG_DISPLAY) {
2807 generated_pin = wps_generate_pin();
2808 wpas_prov_disc_local_display(wpa_s, peer, params,
2810 } else if (config_methods & WPS_CONFIG_KEYPAD)
2811 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2812 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2813 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2814 MACSTR "%s", MAC2STR(peer), params);
2816 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2817 P2P_PROV_DISC_SUCCESS,
2818 config_methods, generated_pin);
2822 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2824 struct wpa_supplicant *wpa_s = ctx;
2825 unsigned int generated_pin = 0;
2828 if (wpa_s->pending_pd_before_join &&
2829 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2830 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2831 wpa_s->pending_pd_before_join = 0;
2832 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2833 "join-existing-group operation");
2834 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2838 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2839 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2840 os_snprintf(params, sizeof(params), " peer_go=%d",
2841 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2845 if (config_methods & WPS_CONFIG_DISPLAY)
2846 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2847 else if (config_methods & WPS_CONFIG_KEYPAD) {
2848 generated_pin = wps_generate_pin();
2849 wpas_prov_disc_local_display(wpa_s, peer, params,
2851 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2852 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2853 MACSTR "%s", MAC2STR(peer), params);
2855 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2856 P2P_PROV_DISC_SUCCESS,
2857 config_methods, generated_pin);
2861 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2862 enum p2p_prov_disc_status status)
2864 struct wpa_supplicant *wpa_s = ctx;
2866 if (wpa_s->p2p_fallback_to_go_neg) {
2867 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2868 "failed - fall back to GO Negotiation");
2869 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2873 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2874 wpa_s->pending_pd_before_join = 0;
2875 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2876 "join-existing-group operation (no ACK for PD "
2878 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2882 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2883 " p2p_dev_addr=" MACSTR " status=%d",
2884 MAC2STR(peer), status);
2886 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2891 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2893 if (channels == NULL)
2894 return 1; /* Assume no restrictions */
2895 return p2p_channels_includes_freq(channels, freq);
2900 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2901 const u8 *go_dev_addr, const u8 *ssid,
2902 size_t ssid_len, int *go, u8 *group_bssid,
2903 int *force_freq, int persistent_group,
2904 const struct p2p_channels *channels,
2907 struct wpa_supplicant *wpa_s = ctx;
2910 struct wpa_supplicant *grp;
2912 if (!persistent_group) {
2913 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2914 " to join an active group (SSID: %s)",
2915 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
2916 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2917 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2919 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2920 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2921 "authorized invitation");
2925 #ifdef CONFIG_WPS_NFC
2926 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
2927 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
2928 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2929 wpa_s->parent->p2p_wps_method = WPS_NFC;
2930 wpa_s->parent->pending_join_wps_method = WPS_NFC;
2931 os_memcpy(wpa_s->parent->pending_join_dev_addr,
2932 go_dev_addr, ETH_ALEN);
2933 os_memcpy(wpa_s->parent->pending_join_iface_addr,
2937 #endif /* CONFIG_WPS_NFC */
2940 * Do not accept the invitation automatically; notify user and
2943 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2946 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2948 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2949 "running persistent group");
2951 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2955 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2956 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2957 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2958 "invitation to re-invoke a persistent group");
2959 } else if (!wpa_s->conf->persistent_reconnect)
2960 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2962 for (s = wpa_s->conf->ssid; s; s = s->next) {
2963 if (s->disabled == 2 &&
2964 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2965 s->ssid_len == ssid_len &&
2966 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2971 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2972 " requested reinvocation of an unknown group",
2974 return P2P_SC_FAIL_UNKNOWN_GROUP;
2977 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2979 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2980 wpa_printf(MSG_DEBUG, "P2P: The only available "
2981 "interface is already in use - reject "
2983 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2985 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2986 } else if (s->mode == WPAS_MODE_P2P_GO) {
2988 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2990 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2991 "interface address for the group");
2992 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2994 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2999 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3001 /* Get one of the frequencies currently in use */
3002 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
3003 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3004 wpas_p2p_set_own_freq_preference(wpa_s, res);
3006 if (wpa_s->num_multichan_concurrent < 2 ||
3007 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3008 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3013 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3014 wpas_p2p_num_unused_channels(wpa_s) > 0) {
3016 /* We are the client */
3017 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3018 "running a GO but we are capable of MCC, "
3019 "figure out the best channel to use");
3021 } else if (!freq_included(channels, *force_freq)) {
3022 /* We are the GO, and *force_freq is not in the
3024 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3025 "in intersection but we are capable of MCC, "
3026 "figure out the best channel to use",
3032 return P2P_SC_SUCCESS;
3036 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3037 const u8 *ssid, size_t ssid_len,
3038 const u8 *go_dev_addr, u8 status,
3041 struct wpa_supplicant *wpa_s = ctx;
3044 for (s = wpa_s->conf->ssid; s; s = s->next) {
3045 if (s->disabled == 2 &&
3046 s->ssid_len == ssid_len &&
3047 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3051 if (status == P2P_SC_SUCCESS) {
3052 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3053 " was accepted; op_freq=%d MHz, SSID=%s",
3054 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3056 int go = s->mode == WPAS_MODE_P2P_GO;
3057 wpas_p2p_group_add_persistent(
3058 wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
3059 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3061 wpa_s->user_initiated_pd = 0;
3062 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
3063 wpa_s->p2p_wps_method, 0, op_freq,
3069 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3070 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3071 " was rejected (status %u)", MAC2STR(sa), status);
3077 wpa_msg_global(wpa_s, MSG_INFO,
3078 P2P_EVENT_INVITATION_RECEIVED
3079 "sa=" MACSTR " go_dev_addr=" MACSTR
3080 " bssid=" MACSTR " unknown-network",
3081 MAC2STR(sa), MAC2STR(go_dev_addr),
3084 wpa_msg_global(wpa_s, MSG_INFO,
3085 P2P_EVENT_INVITATION_RECEIVED
3086 "sa=" MACSTR " go_dev_addr=" MACSTR
3088 MAC2STR(sa), MAC2STR(go_dev_addr));
3093 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
3094 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3095 "sa=" MACSTR " persistent=%d freq=%d",
3096 MAC2STR(sa), s->id, op_freq);
3098 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3099 "sa=" MACSTR " persistent=%d",
3100 MAC2STR(sa), s->id);
3105 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3106 struct wpa_ssid *ssid,
3107 const u8 *peer, int inv)
3114 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3115 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3119 if (i >= ssid->num_p2p_clients) {
3120 if (ssid->mode != WPAS_MODE_P2P_GO &&
3121 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3122 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3123 "due to invitation result", ssid->id);
3124 wpas_notify_network_removed(wpa_s, ssid);
3125 wpa_config_remove_network(wpa_s->conf, ssid->id);
3128 return; /* Peer not found in client list */
3131 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
3132 "group %d client list%s",
3133 MAC2STR(peer), ssid->id,
3134 inv ? " due to invitation result" : "");
3135 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3136 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3137 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3138 ssid->num_p2p_clients--;
3139 #ifndef CONFIG_NO_CONFIG_WRITE
3140 if (wpa_s->parent->conf->update_config &&
3141 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3142 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3143 #endif /* CONFIG_NO_CONFIG_WRITE */
3147 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3150 struct wpa_ssid *ssid;
3152 wpa_s = wpa_s->global->p2p_invite_group;
3154 return; /* No known invitation group */
3155 ssid = wpa_s->current_ssid;
3156 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3157 !ssid->p2p_persistent_group)
3158 return; /* Not operating as a GO in persistent group */
3159 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3160 ssid->ssid, ssid->ssid_len);
3161 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3165 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
3166 const struct p2p_channels *channels,
3167 const u8 *peer, int neg_freq)
3169 struct wpa_supplicant *wpa_s = ctx;
3170 struct wpa_ssid *ssid;
3174 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3175 "status=%d " MACSTR,
3176 status, MAC2STR(bssid));
3178 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3179 "status=%d ", status);
3181 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3183 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3184 status, MAC2STR(peer));
3185 if (wpa_s->pending_invite_ssid_id == -1) {
3186 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3187 wpas_remove_persistent_client(wpa_s, peer);
3188 return; /* Invitation to active group */
3191 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3192 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3193 "invitation exchange to indicate readiness for "
3197 if (status != P2P_SC_SUCCESS) {
3198 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3199 ssid = wpa_config_get_network(
3200 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3201 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3203 wpas_p2p_remove_pending_group_interface(wpa_s);
3207 ssid = wpa_config_get_network(wpa_s->conf,
3208 wpa_s->pending_invite_ssid_id);
3210 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3211 "data matching with invitation");
3216 * The peer could have missed our ctrl::ack frame for Invitation
3217 * Response and continue retransmitting the frame. To reduce the
3218 * likelihood of the peer not getting successful TX status for the
3219 * Invitation Response frame, wait a short time here before starting
3220 * the persistent group so that we will remain on the current channel to
3221 * acknowledge any possible retransmission from the peer.
3223 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3224 "starting persistent group");
3227 freq = wpa_s->p2p_persistent_go_freq;
3228 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3229 freq_included(channels, neg_freq)) {
3230 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode",
3235 wpas_p2p_group_add_persistent(wpa_s, ssid,
3236 ssid->mode == WPAS_MODE_P2P_GO,
3238 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3240 ssid->mode == WPAS_MODE_P2P_GO ?
3241 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3246 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3249 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3251 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3255 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3257 reg->channel[reg->channels] = chan;
3262 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3263 struct p2p_channels *chan,
3264 struct p2p_channels *cli_chan)
3268 os_memset(cli_chan, 0, sizeof(*cli_chan));
3270 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3273 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3274 chan->reg_class[cla].reg_class = 81;
3275 chan->reg_class[cla].channels = 0;
3276 for (i = 0; i < 11; i++) {
3277 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3278 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3280 if (chan->reg_class[cla].channels)
3283 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3286 /* Operating class 115 - 5 GHz, channels 36-48 */
3287 chan->reg_class[cla].reg_class = 115;
3288 chan->reg_class[cla].channels = 0;
3289 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3290 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3291 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3292 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3293 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3294 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3295 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3296 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3297 if (chan->reg_class[cla].channels)
3300 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3303 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3304 chan->reg_class[cla].reg_class = 124;
3305 chan->reg_class[cla].channels = 0;
3306 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3307 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3308 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3309 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3310 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3311 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3312 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3313 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3314 if (chan->reg_class[cla].channels)
3317 chan->reg_classes = cla;
3322 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3324 enum hostapd_hw_mode mode)
3328 for (i = 0; i < num_modes; i++) {
3329 if (modes[i].mode == mode)
3338 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3341 static int has_channel(struct wpa_global *global,
3342 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3347 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3349 if (wpas_p2p_disallowed_freq(global, freq))
3352 for (i = 0; i < mode->num_channels; i++) {
3353 if (mode->channels[i].chan == chan) {
3355 *flags = mode->channels[i].flag;
3356 if (mode->channels[i].flag &
3357 (HOSTAPD_CHAN_DISABLED |
3358 HOSTAPD_CHAN_RADAR))
3360 if (mode->channels[i].flag &
3361 (HOSTAPD_CHAN_PASSIVE_SCAN |
3362 HOSTAPD_CHAN_NO_IBSS))
3363 return PASSIVE_ONLY;
3372 struct p2p_oper_class_map {
3373 enum hostapd_hw_mode mode;
3378 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
3381 static struct p2p_oper_class_map op_class[] = {
3382 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3383 #if 0 /* Do not enable HT40 on 2 GHz for now */
3384 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3385 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3387 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3388 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3389 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3390 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3391 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3392 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3395 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3396 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3397 * 80 MHz, but currently use the following definition for simplicity
3398 * (these center frequencies are not actual channels, which makes
3399 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3400 * removing invalid channels.
3402 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3403 { -1, 0, 0, 0, 0, BW20 }
3407 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3408 struct hostapd_hw_modes *mode,
3411 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3414 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3417 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3419 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3420 * so the center channel is 6 channels away from the start/end.
3422 if (channel >= center_channels[i] - 6 &&
3423 channel <= center_channels[i] + 6)
3424 return center_channels[i];
3430 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3431 struct hostapd_hw_modes *mode,
3436 enum chan_allowed res, ret = ALLOWED;
3438 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3441 if (center_chan >= 58 && center_chan <= 138)
3442 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3444 /* check all the channels are available */
3445 for (i = 0; i < 4; i++) {
3446 int adj_chan = center_chan - 6 + i * 4;
3448 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3449 if (res == NOT_ALLOWED)
3451 if (res == PASSIVE_ONLY)
3454 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3456 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3458 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3460 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3468 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3469 struct hostapd_hw_modes *mode,
3473 enum chan_allowed res, res2;
3475 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3476 if (bw == BW40MINUS) {
3477 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3479 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3480 } else if (bw == BW40PLUS) {
3481 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3483 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3484 } else if (bw == BW80) {
3485 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3488 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3490 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3491 return PASSIVE_ONLY;
3496 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3497 struct p2p_channels *chan,
3498 struct p2p_channels *cli_chan)
3500 struct hostapd_hw_modes *mode;
3501 int cla, op, cli_cla;
3503 if (wpa_s->hw.modes == NULL) {
3504 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3505 "of all supported channels; assume dualband "
3507 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3512 for (op = 0; op_class[op].op_class; op++) {
3513 struct p2p_oper_class_map *o = &op_class[op];
3515 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3517 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3520 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3521 enum chan_allowed res;
3522 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3523 if (res == ALLOWED) {
3525 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3527 reg = &chan->reg_class[cla];
3529 reg->reg_class = o->op_class;
3531 reg->channel[reg->channels] = ch;
3533 } else if (res == PASSIVE_ONLY &&
3534 wpa_s->conf->p2p_add_cli_chan) {
3535 if (cli_reg == NULL) {
3536 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3538 cli_reg = &cli_chan->reg_class[cli_cla];
3540 cli_reg->reg_class = o->op_class;
3542 cli_reg->channel[cli_reg->channels] = ch;
3543 cli_reg->channels++;
3547 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3548 reg->channel, reg->channels);
3551 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3552 cli_reg->channel, cli_reg->channels);
3556 chan->reg_classes = cla;
3557 cli_chan->reg_classes = cli_cla;
3563 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3564 struct hostapd_hw_modes *mode, u8 channel)
3567 enum chan_allowed ret;
3569 for (op = 0; op_class[op].op_class; op++) {
3570 struct p2p_oper_class_map *o = &op_class[op];
3573 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3574 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3575 o->bw == BW20 || ch != channel)
3577 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3579 return (o->bw == BW40MINUS) ? -1 : 1;
3586 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3587 struct hostapd_hw_modes *mode, u8 channel)
3589 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3592 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3596 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3599 struct wpa_supplicant *wpa_s = ctx;
3601 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3602 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3608 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3612 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3614 struct wpa_supplicant *wpa_s = ctx;
3616 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3617 struct wpa_ssid *ssid = wpa_s->current_ssid;
3620 if (ssid->mode != WPAS_MODE_INFRA)
3622 if (wpa_s->wpa_state != WPA_COMPLETED &&
3623 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3625 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3633 static int wpas_is_concurrent_session_active(void *ctx)
3635 struct wpa_supplicant *wpa_s = ctx;
3636 struct wpa_supplicant *ifs;
3638 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3641 if (ifs->wpa_state > WPA_ASSOCIATED)
3648 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3650 struct wpa_supplicant *wpa_s = ctx;
3651 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3655 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3657 struct wpa_interface iface;
3658 struct wpa_supplicant *p2pdev_wpa_s;
3660 char force_name[100];
3663 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3665 force_name[0] = '\0';
3666 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3667 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3668 force_name, wpa_s->pending_interface_addr, NULL);
3670 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3673 os_strlcpy(wpa_s->pending_interface_name, ifname,
3674 sizeof(wpa_s->pending_interface_name));
3676 os_memset(&iface, 0, sizeof(iface));
3678 iface.ifname = wpa_s->pending_interface_name;
3679 iface.driver = wpa_s->driver->name;
3680 iface.driver_param = wpa_s->conf->driver_param;
3681 iface.confname = wpa_s->confname;
3682 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3683 if (!p2pdev_wpa_s) {
3684 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3687 p2pdev_wpa_s->parent = wpa_s;
3689 wpa_s->pending_interface_name[0] = '\0';
3694 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3695 const u8 *noa, size_t noa_len)
3697 struct wpa_supplicant *wpa_s, *intf = ctx;
3700 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3701 if (wpa_s->waiting_presence_resp)
3705 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3708 wpa_s->waiting_presence_resp = 0;
3710 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3711 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3712 " status=%u noa=%s", MAC2STR(src), status, hex);
3717 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3718 * @global: Pointer to global data from wpa_supplicant_init()
3719 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3720 * Returns: 0 on success, -1 on failure
3722 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3724 struct p2p_config p2p;
3728 if (wpa_s->conf->p2p_disabled)
3731 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3737 os_memset(&p2p, 0, sizeof(p2p));
3739 p2p.debug_print = wpas_p2p_debug_print;
3740 p2p.p2p_scan = wpas_p2p_scan;
3741 p2p.send_action = wpas_send_action;
3742 p2p.send_action_done = wpas_send_action_done;
3743 p2p.go_neg_completed = wpas_go_neg_completed;
3744 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3745 p2p.dev_found = wpas_dev_found;
3746 p2p.dev_lost = wpas_dev_lost;
3747 p2p.find_stopped = wpas_find_stopped;
3748 p2p.start_listen = wpas_start_listen;
3749 p2p.stop_listen = wpas_stop_listen;
3750 p2p.send_probe_resp = wpas_send_probe_resp;
3751 p2p.sd_request = wpas_sd_request;
3752 p2p.sd_response = wpas_sd_response;
3753 p2p.prov_disc_req = wpas_prov_disc_req;
3754 p2p.prov_disc_resp = wpas_prov_disc_resp;
3755 p2p.prov_disc_fail = wpas_prov_disc_fail;
3756 p2p.invitation_process = wpas_invitation_process;
3757 p2p.invitation_received = wpas_invitation_received;
3758 p2p.invitation_result = wpas_invitation_result;
3759 p2p.get_noa = wpas_get_noa;
3760 p2p.go_connected = wpas_go_connected;
3761 p2p.presence_resp = wpas_presence_resp;
3762 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
3764 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
3765 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
3766 p2p.dev_name = wpa_s->conf->device_name;
3767 p2p.manufacturer = wpa_s->conf->manufacturer;
3768 p2p.model_name = wpa_s->conf->model_name;
3769 p2p.model_number = wpa_s->conf->model_number;
3770 p2p.serial_number = wpa_s->conf->serial_number;
3772 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3773 p2p.config_methods = wpa_s->wps->config_methods;
3776 if (wpa_s->conf->p2p_listen_reg_class &&
3777 wpa_s->conf->p2p_listen_channel) {
3778 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3779 p2p.channel = wpa_s->conf->p2p_listen_channel;
3783 * Pick one of the social channels randomly as the listen
3786 os_get_random((u8 *) &r, sizeof(r));
3787 p2p.channel = 1 + (r % 3) * 5;
3789 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3791 if (wpa_s->conf->p2p_oper_reg_class &&
3792 wpa_s->conf->p2p_oper_channel) {
3793 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3794 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3795 p2p.cfg_op_channel = 1;
3796 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3797 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3800 p2p.op_reg_class = 81;
3802 * Use random operation channel from (1, 6, 11) if no other
3803 * preference is indicated.
3805 os_get_random((u8 *) &r, sizeof(r));
3806 p2p.op_channel = 1 + (r % 3) * 5;
3807 p2p.cfg_op_channel = 0;
3808 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3809 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3812 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3813 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3814 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3817 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3818 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3819 p2p.country[2] = 0x04;
3821 os_memcpy(p2p.country, "XX\x04", 3);
3823 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3824 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3829 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3832 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3833 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3834 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3836 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3837 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3839 p2p.max_peers = 100;
3841 if (wpa_s->conf->p2p_ssid_postfix) {
3842 p2p.ssid_postfix_len =
3843 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3844 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3845 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3846 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3847 p2p.ssid_postfix_len);
3850 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3852 p2p.max_listen = wpa_s->max_remain_on_chan;
3854 global->p2p = p2p_init(&p2p);
3855 if (global->p2p == NULL)
3857 global->p2p_init_wpa_s = wpa_s;
3859 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3860 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3862 p2p_add_wps_vendor_extension(
3863 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3866 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3873 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3874 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3876 * This function deinitialize per-interface P2P data.
3878 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3880 if (wpa_s->driver && wpa_s->drv_priv)
3881 wpa_drv_probe_req_report(wpa_s, 0);
3883 if (wpa_s->go_params) {
3884 /* Clear any stored provisioning info */
3885 p2p_clear_provisioning_info(
3887 wpa_s->go_params->peer_device_addr);
3890 os_free(wpa_s->go_params);
3891 wpa_s->go_params = NULL;
3892 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3893 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3894 wpa_s->p2p_long_listen = 0;
3895 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3896 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3897 wpas_p2p_remove_pending_group_interface(wpa_s);
3898 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3899 wpas_p2p_listen_work_done(wpa_s);
3900 if (wpa_s->p2p_send_action_work) {
3901 os_free(wpa_s->p2p_send_action_work->ctx);
3902 radio_work_done(wpa_s->p2p_send_action_work);
3903 wpa_s->p2p_send_action_work = NULL;
3905 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
3907 wpabuf_free(wpa_s->p2p_oob_dev_pw);
3908 wpa_s->p2p_oob_dev_pw = NULL;
3910 /* TODO: remove group interface from the driver if this wpa_s instance
3911 * is on top of a P2P group interface */
3916 * wpas_p2p_deinit_global - Deinitialize global P2P module
3917 * @global: Pointer to global data from wpa_supplicant_init()
3919 * This function deinitializes the global (per device) P2P module.
3921 void wpas_p2p_deinit_global(struct wpa_global *global)
3923 struct wpa_supplicant *wpa_s, *tmp;
3925 wpa_s = global->ifaces;
3927 wpas_p2p_service_flush(wpa_s);
3929 if (global->p2p == NULL)
3932 /* Remove remaining P2P group interfaces */
3933 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3934 wpa_s = wpa_s->next;
3936 tmp = global->ifaces;
3939 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3944 /* Disconnect from the P2P group and deinit the interface */
3945 wpas_p2p_disconnect(tmp);
3949 * Deinit GO data on any possibly remaining interface (if main
3950 * interface is used as GO).
3952 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3953 if (wpa_s->ap_iface)
3954 wpas_p2p_group_deinit(wpa_s);
3957 p2p_deinit(global->p2p);
3959 global->p2p_init_wpa_s = NULL;
3963 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3965 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3966 wpa_s->conf->p2p_no_group_iface)
3967 return 0; /* separate interface disabled per configuration */
3968 if (wpa_s->drv_flags &
3969 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3970 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3971 return 1; /* P2P group requires a new interface in every case
3973 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3974 return 0; /* driver does not support concurrent operations */
3975 if (wpa_s->global->ifaces->next)
3976 return 1; /* more that one interface already in use */
3977 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3978 return 1; /* this interface is already in use */
3983 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3984 const u8 *peer_addr,
3985 enum p2p_wps_method wps_method,
3986 int go_intent, const u8 *own_interface_addr,
3987 unsigned int force_freq, int persistent_group,
3988 struct wpa_ssid *ssid, unsigned int pref_freq)
3990 if (persistent_group && wpa_s->conf->persistent_reconnect)
3991 persistent_group = 2;
3994 * Increase GO config timeout if HT40 is used since it takes some time
3995 * to scan channels for coex purposes before the BSS can be started.
3997 p2p_set_config_timeout(wpa_s->global->p2p,
3998 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4000 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4001 go_intent, own_interface_addr, force_freq,
4002 persistent_group, ssid ? ssid->ssid : NULL,
4003 ssid ? ssid->ssid_len : 0,
4004 wpa_s->p2p_pd_before_go_neg, pref_freq,
4005 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4010 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4011 const u8 *peer_addr,
4012 enum p2p_wps_method wps_method,
4013 int go_intent, const u8 *own_interface_addr,
4014 unsigned int force_freq, int persistent_group,
4015 struct wpa_ssid *ssid, unsigned int pref_freq)
4017 if (persistent_group && wpa_s->conf->persistent_reconnect)
4018 persistent_group = 2;
4020 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4021 go_intent, own_interface_addr, force_freq,
4022 persistent_group, ssid ? ssid->ssid : NULL,
4023 ssid ? ssid->ssid_len : 0, pref_freq,
4024 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4029 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4031 wpa_s->p2p_join_scan_count++;
4032 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4033 wpa_s->p2p_join_scan_count);
4034 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4035 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4036 " for join operationg - stop join attempt",
4037 MAC2STR(wpa_s->pending_join_iface_addr));
4038 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4039 if (wpa_s->p2p_auto_pd) {
4040 wpa_s->p2p_auto_pd = 0;
4041 wpa_msg_global(wpa_s, MSG_INFO,
4042 P2P_EVENT_PROV_DISC_FAILURE
4043 " p2p_dev_addr=" MACSTR " status=N/A",
4044 MAC2STR(wpa_s->pending_join_dev_addr));
4047 wpa_msg_global(wpa_s->parent, MSG_INFO,
4048 P2P_EVENT_GROUP_FORMATION_FAILURE);
4053 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4055 int *freqs, res, num, i;
4057 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4058 /* Multiple channels are supported and not all are in use */
4062 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4066 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4067 wpa_s->num_multichan_concurrent);
4073 for (i = 0; i < num; i++) {
4074 if (freqs[i] == freq) {
4075 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4090 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4091 const u8 *peer_dev_addr)
4093 struct wpa_bss *bss;
4096 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4099 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4100 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4105 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4107 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4108 "%ld.%06ld (%supdated in last scan)",
4109 bss->last_update.sec, bss->last_update.usec,
4110 updated ? "": "not ");
4116 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4117 struct wpa_scan_results *scan_res)
4119 struct wpa_bss *bss = NULL;
4121 u8 iface_addr[ETH_ALEN];
4123 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4125 if (wpa_s->global->p2p_disabled)
4128 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4129 scan_res ? (int) scan_res->num : -1,
4130 wpa_s->p2p_auto_join ? "auto_" : "");
4133 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4135 if (wpa_s->p2p_auto_pd) {
4136 int join = wpas_p2p_peer_go(wpa_s,
4137 wpa_s->pending_join_dev_addr);
4139 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4140 wpa_s->auto_pd_scan_retry++;
4141 bss = wpa_bss_get_bssid_latest(
4142 wpa_s, wpa_s->pending_join_dev_addr);
4145 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4146 "the peer " MACSTR " at %d MHz",
4147 wpa_s->auto_pd_scan_retry,
4149 pending_join_dev_addr),
4151 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4159 wpa_s->p2p_auto_pd = 0;
4160 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4161 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4162 MAC2STR(wpa_s->pending_join_dev_addr), join);
4163 if (p2p_prov_disc_req(wpa_s->global->p2p,
4164 wpa_s->pending_join_dev_addr,
4165 wpa_s->pending_pd_config_methods, join,
4166 0, wpa_s->user_initiated_pd) < 0) {
4167 wpa_s->p2p_auto_pd = 0;
4168 wpa_msg_global(wpa_s, MSG_INFO,
4169 P2P_EVENT_PROV_DISC_FAILURE
4170 " p2p_dev_addr=" MACSTR " status=N/A",
4171 MAC2STR(wpa_s->pending_join_dev_addr));
4176 if (wpa_s->p2p_auto_join) {
4177 int join = wpas_p2p_peer_go(wpa_s,
4178 wpa_s->pending_join_dev_addr);
4180 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4181 "running a GO -> use GO Negotiation");
4182 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4183 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4184 wpa_s->p2p_persistent_group, 0, 0, 0,
4185 wpa_s->p2p_go_intent,
4186 wpa_s->p2p_connect_freq,
4187 wpa_s->p2p_persistent_id,
4188 wpa_s->p2p_pd_before_go_neg,
4194 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4195 "try to join the group", join ? "" :
4198 wpa_s->p2p_fallback_to_go_neg = 1;
4201 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4202 wpa_s->pending_join_iface_addr);
4204 p2p_get_interface_addr(wpa_s->global->p2p,
4205 wpa_s->pending_join_dev_addr,
4207 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4209 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4210 "address for join from " MACSTR " to " MACSTR
4211 " based on newly discovered P2P peer entry",
4212 MAC2STR(wpa_s->pending_join_iface_addr),
4213 MAC2STR(iface_addr));
4214 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4217 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4218 wpa_s->pending_join_iface_addr);
4221 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4222 "from P2P peer table: %d MHz", freq);
4224 if (wpa_s->p2p_join_ssid_len) {
4225 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4226 MACSTR " and SSID %s",
4227 MAC2STR(wpa_s->pending_join_iface_addr),
4228 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4229 wpa_s->p2p_join_ssid_len));
4230 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4231 wpa_s->p2p_join_ssid,
4232 wpa_s->p2p_join_ssid_len);
4235 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4236 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4237 bss = wpa_bss_get_bssid_latest(wpa_s,
4238 wpa_s->pending_join_iface_addr);
4242 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4243 "from BSS table: %d MHz (SSID %s)", freq,
4244 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4249 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4250 wpa_msg_global(wpa_s->parent, MSG_INFO,
4251 P2P_EVENT_GROUP_FORMATION_FAILURE
4252 "reason=FREQ_CONFLICT");
4256 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4257 "prior to joining an existing group (GO " MACSTR
4259 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4260 wpa_s->pending_pd_before_join = 1;
4262 switch (wpa_s->pending_join_wps_method) {
4263 case WPS_PIN_DISPLAY:
4264 method = WPS_CONFIG_KEYPAD;
4266 case WPS_PIN_KEYPAD:
4267 method = WPS_CONFIG_DISPLAY;
4270 method = WPS_CONFIG_PUSHBUTTON;
4277 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4278 wpa_s->pending_join_dev_addr) ==
4281 * We have already performed provision discovery for
4282 * joining the group. Proceed directly to join
4283 * operation without duplicated provision discovery. */
4284 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4285 "with " MACSTR " already done - proceed to "
4287 MAC2STR(wpa_s->pending_join_dev_addr));
4288 wpa_s->pending_pd_before_join = 0;
4292 if (p2p_prov_disc_req(wpa_s->global->p2p,
4293 wpa_s->pending_join_dev_addr, method, 1,
4294 freq, wpa_s->user_initiated_pd) < 0) {
4295 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4296 "Discovery Request before joining an "
4298 wpa_s->pending_pd_before_join = 0;
4304 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4305 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4306 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4307 wpas_p2p_check_join_scan_limit(wpa_s);
4311 /* Start join operation immediately */
4312 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4316 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4317 const u8 *ssid, size_t ssid_len)
4320 struct wpa_driver_scan_params params;
4321 struct wpabuf *wps_ie, *ies;
4323 int freqs[2] = { 0, 0 };
4325 os_memset(¶ms, 0, sizeof(params));
4327 /* P2P Wildcard SSID */
4328 params.num_ssids = 1;
4329 if (ssid && ssid_len) {
4330 params.ssids[0].ssid = ssid;
4331 params.ssids[0].ssid_len = ssid_len;
4332 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4333 wpa_s->p2p_join_ssid_len = ssid_len;
4335 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4336 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4337 wpa_s->p2p_join_ssid_len = 0;
4340 wpa_s->wps->dev.p2p = 1;
4341 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4342 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4344 if (wps_ie == NULL) {
4345 wpas_p2p_scan_res_join(wpa_s, NULL);
4349 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4350 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4352 wpabuf_free(wps_ie);
4353 wpas_p2p_scan_res_join(wpa_s, NULL);
4356 wpabuf_put_buf(ies, wps_ie);
4357 wpabuf_free(wps_ie);
4359 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4361 params.p2p_probe = 1;
4362 params.extra_ies = wpabuf_head(ies);
4363 params.extra_ies_len = wpabuf_len(ies);
4368 * If freq is not provided, check the operating freq of the GO
4369 * and use a single channel scan on if possible.
4371 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4372 wpa_s->pending_join_iface_addr);
4378 params.freqs = freqs;
4382 * Run a scan to update BSS table and start Provision Discovery once
4383 * the new scan results become available.
4385 ret = wpa_drv_scan(wpa_s, ¶ms);
4387 os_get_reltime(&wpa_s->scan_trigger_time);
4388 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4389 wpa_s->own_scan_requested = 1;
4395 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4397 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4398 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4399 wpas_p2p_check_join_scan_limit(wpa_s);
4404 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4406 struct wpa_supplicant *wpa_s = eloop_ctx;
4407 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4411 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4412 const u8 *dev_addr, enum p2p_wps_method wps_method,
4413 int auto_join, int op_freq,
4414 const u8 *ssid, size_t ssid_len)
4416 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4417 MACSTR " dev " MACSTR " op_freq=%d)%s",
4418 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4419 auto_join ? " (auto_join)" : "");
4420 if (ssid && ssid_len) {
4421 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4422 wpa_ssid_txt(ssid, ssid_len));
4425 wpa_s->p2p_auto_pd = 0;
4426 wpa_s->p2p_auto_join = !!auto_join;
4427 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4428 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4429 wpa_s->pending_join_wps_method = wps_method;
4431 /* Make sure we are not running find during connection establishment */
4432 wpas_p2p_stop_find(wpa_s);
4434 wpa_s->p2p_join_scan_count = 0;
4435 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4440 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4441 const u8 *ssid, size_t ssid_len)
4443 struct wpa_supplicant *group;
4444 struct p2p_go_neg_results res;
4445 struct wpa_bss *bss;
4447 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4450 if (group != wpa_s) {
4451 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4452 sizeof(group->p2p_pin));
4453 group->p2p_wps_method = wpa_s->p2p_wps_method;
4456 * Need to mark the current interface for p2p_group_formation
4457 * when a separate group interface is not used. This is needed
4458 * to allow p2p_cancel stop a pending p2p_connect-join.
4459 * wpas_p2p_init_group_interface() addresses this for the case
4460 * where a separate group interface is used.
4462 wpa_s->global->p2p_group_formation = wpa_s;
4465 group->p2p_in_provisioning = 1;
4466 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4468 os_memset(&res, 0, sizeof(res));
4469 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4470 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4472 res.wps_method = wpa_s->pending_join_wps_method;
4473 if (freq && ssid && ssid_len) {
4475 res.ssid_len = ssid_len;
4476 os_memcpy(res.ssid, ssid, ssid_len);
4478 bss = wpa_bss_get_bssid_latest(wpa_s,
4479 wpa_s->pending_join_iface_addr);
4481 res.freq = bss->freq;
4482 res.ssid_len = bss->ssid_len;
4483 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4484 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4486 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4490 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4491 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4493 wpa_drv_cancel_remain_on_channel(wpa_s);
4494 wpa_s->off_channel_freq = 0;
4495 wpa_s->roc_waiting_drv_freq = 0;
4497 wpas_start_wps_enrollee(group, &res);
4500 * Allow a longer timeout for join-a-running-group than normal 15
4501 * second group formation timeout since the GO may not have authorized
4502 * our connection yet.
4504 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4505 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4512 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4513 int *force_freq, int *pref_freq, int go)
4516 unsigned int freq_in_use = 0, num, i;
4518 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4522 num = get_shared_radio_freqs(wpa_s, freqs,
4523 wpa_s->num_multichan_concurrent);
4524 wpa_printf(MSG_DEBUG,
4525 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4526 freq, wpa_s->num_multichan_concurrent, num);
4531 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4533 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4535 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4536 "(%u MHz) is not supported for P2P uses",
4542 for (i = 0; i < num; i++) {
4543 if (freqs[i] == freq)
4547 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4548 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4553 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4554 "requested channel (%u MHz)", freq);
4559 for (i = 0; i < num; i++) {
4560 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4563 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
4564 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4566 *pref_freq = freqs[i];
4568 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4570 *force_freq = freqs[i];
4576 if (num < wpa_s->num_multichan_concurrent && num > 0) {
4577 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4579 } else if (num < wpa_s->num_multichan_concurrent) {
4580 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4583 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4598 * wpas_p2p_connect - Request P2P Group Formation to be started
4599 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4600 * @peer_addr: Address of the peer P2P Device
4601 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4602 * @persistent_group: Whether to create a persistent group
4603 * @auto_join: Whether to select join vs. GO Negotiation automatically
4604 * @join: Whether to join an existing group (as a client) instead of starting
4605 * Group Owner negotiation; @peer_addr is BSSID in that case
4606 * @auth: Whether to only authorize the connection instead of doing that and
4607 * initiating Group Owner negotiation
4608 * @go_intent: GO Intent or -1 to use default
4609 * @freq: Frequency for the group or 0 for auto-selection
4610 * @persistent_id: Persistent group credentials to use for forcing GO
4611 * parameters or -1 to generate new values (SSID/passphrase)
4612 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4613 * interoperability workaround when initiating group formation
4614 * @ht40: Start GO with 40 MHz channel width
4615 * @vht: Start GO with VHT support
4616 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4617 * failure, -2 on failure due to channel not currently available,
4618 * -3 if forced channel is not supported
4620 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4621 const char *pin, enum p2p_wps_method wps_method,
4622 int persistent_group, int auto_join, int join, int auth,
4623 int go_intent, int freq, int persistent_id, int pd,
4626 int force_freq = 0, pref_freq = 0;
4628 enum wpa_driver_if_type iftype;
4630 struct wpa_ssid *ssid = NULL;
4632 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4635 if (persistent_id >= 0) {
4636 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4637 if (ssid == NULL || ssid->disabled != 2 ||
4638 ssid->mode != WPAS_MODE_P2P_GO)
4642 os_free(wpa_s->global->add_psk);
4643 wpa_s->global->add_psk = NULL;
4645 wpa_s->global->p2p_fail_on_wps_complete = 0;
4648 go_intent = wpa_s->conf->p2p_go_intent;
4651 wpa_s->p2p_long_listen = 0;
4653 wpa_s->p2p_wps_method = wps_method;
4654 wpa_s->p2p_persistent_group = !!persistent_group;
4655 wpa_s->p2p_persistent_id = persistent_id;
4656 wpa_s->p2p_go_intent = go_intent;
4657 wpa_s->p2p_connect_freq = freq;
4658 wpa_s->p2p_fallback_to_go_neg = 0;
4659 wpa_s->p2p_pd_before_go_neg = !!pd;
4660 wpa_s->p2p_go_ht40 = !!ht40;
4661 wpa_s->p2p_go_vht = !!vht;
4664 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4665 else if (wps_method == WPS_PIN_DISPLAY) {
4666 ret = wps_generate_pin();
4667 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4669 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4672 wpa_s->p2p_pin[0] = '\0';
4674 if (join || auto_join) {
4675 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4677 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4678 "connect a running group from " MACSTR,
4679 MAC2STR(peer_addr));
4680 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4683 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4684 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4686 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4687 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4691 os_get_reltime(&wpa_s->p2p_auto_started);
4692 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4694 wpa_s->p2p_auto_started.sec,
4695 wpa_s->p2p_auto_started.usec);
4697 wpa_s->user_initiated_pd = 1;
4698 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4699 auto_join, freq, NULL, 0) < 0)
4704 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4708 wpas_p2p_set_own_freq_preference(wpa_s,
4709 force_freq ? force_freq : pref_freq);
4711 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4713 if (wpa_s->create_p2p_iface) {
4714 /* Prepare to add a new interface for the group */
4715 iftype = WPA_IF_P2P_GROUP;
4716 if (go_intent == 15)
4717 iftype = WPA_IF_P2P_GO;
4718 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4719 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4720 "interface for the group");
4724 if_addr = wpa_s->pending_interface_addr;
4726 if_addr = wpa_s->own_addr;
4729 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
4731 force_freq, persistent_group, ssid,
4737 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4738 go_intent, if_addr, force_freq,
4739 persistent_group, ssid, pref_freq) < 0) {
4740 if (wpa_s->create_p2p_iface)
4741 wpas_p2p_remove_pending_group_interface(wpa_s);
4749 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4750 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4751 * @freq: Frequency of the channel in MHz
4752 * @duration: Duration of the stay on the channel in milliseconds
4754 * This callback is called when the driver indicates that it has started the
4755 * requested remain-on-channel duration.
4757 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4758 unsigned int freq, unsigned int duration)
4760 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4762 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4763 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4764 wpa_s->pending_listen_duration);
4765 wpa_s->pending_listen_freq = 0;
4770 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4771 unsigned int timeout)
4773 /* Limit maximum Listen state time based on driver limitation. */
4774 if (timeout > wpa_s->max_remain_on_chan)
4775 timeout = wpa_s->max_remain_on_chan;
4777 return p2p_listen(wpa_s->global->p2p, timeout);
4782 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4783 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4784 * @freq: Frequency of the channel in MHz
4786 * This callback is called when the driver indicates that a remain-on-channel
4787 * operation has been completed, i.e., the duration on the requested channel
4790 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4793 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4794 "(p2p_long_listen=%d ms pending_action_tx=%p)",
4795 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
4796 wpas_p2p_listen_work_done(wpa_s);
4797 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4799 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4800 return; /* P2P module started a new operation */
4801 if (offchannel_pending_action_tx(wpa_s))
4803 if (wpa_s->p2p_long_listen > 0)
4804 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4805 if (wpa_s->p2p_long_listen > 0) {
4806 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4807 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4810 * When listen duration is over, stop listen & update p2p_state
4813 p2p_stop_listen(wpa_s->global->p2p);
4819 * wpas_p2p_group_remove - Remove a P2P group
4820 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4821 * @ifname: Network interface name of the group interface or "*" to remove all
4823 * Returns: 0 on success, -1 on failure
4825 * This function is used to remove a P2P group. This can be used to disconnect
4826 * from a group in which the local end is a P2P Client or to end a P2P Group in
4827 * case the local end is the Group Owner. If a virtual network interface was
4828 * created for this group, that interface will be removed. Otherwise, only the
4829 * configured P2P group network will be removed from the interface.
4831 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4833 struct wpa_global *global = wpa_s->global;
4835 if (os_strcmp(ifname, "*") == 0) {
4836 struct wpa_supplicant *prev;
4837 wpa_s = global->ifaces;
4840 wpa_s = wpa_s->next;
4841 if (prev->p2p_group_interface !=
4842 NOT_P2P_GROUP_INTERFACE ||
4843 (prev->current_ssid &&
4844 prev->current_ssid->p2p_group))
4845 wpas_p2p_disconnect(prev);
4850 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4851 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4855 return wpas_p2p_disconnect(wpa_s);
4859 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4864 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4866 if (wpa_s->best_24_freq > 0 &&
4867 p2p_supported_freq_go(wpa_s->global->p2p,
4868 wpa_s->best_24_freq)) {
4869 freq = wpa_s->best_24_freq;
4870 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4871 "channel: %d MHz", freq);
4873 os_get_random((u8 *) &r, sizeof(r));
4874 freq = 2412 + (r % 3) * 25;
4875 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4876 "channel: %d MHz", freq);
4881 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4883 if (wpa_s->best_5_freq > 0 &&
4884 p2p_supported_freq_go(wpa_s->global->p2p,
4885 wpa_s->best_5_freq)) {
4886 freq = wpa_s->best_5_freq;
4887 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4888 "channel: %d MHz", freq);
4890 os_get_random((u8 *) &r, sizeof(r));
4891 freq = 5180 + (r % 4) * 20;
4892 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4893 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4894 "5 GHz channel for P2P group");
4897 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4898 "channel: %d MHz", freq);
4902 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4903 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4904 "(%u MHz) is not supported for P2P uses",
4913 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4914 struct p2p_go_neg_results *params,
4915 int freq, int ht40, int vht,
4916 const struct p2p_channels *channels)
4919 unsigned int pref_freq;
4920 unsigned int num, i;
4922 os_memset(params, 0, sizeof(*params));
4923 params->role_go = 1;
4924 params->ht40 = ht40;
4927 if (!freq_included(channels, freq)) {
4928 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4932 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4933 "frequency %d MHz", freq);
4934 params->freq = freq;
4935 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4936 wpa_s->conf->p2p_oper_channel >= 1 &&
4937 wpa_s->conf->p2p_oper_channel <= 11 &&
4938 freq_included(channels,
4939 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
4940 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4941 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4942 "frequency %d MHz", params->freq);
4943 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4944 wpa_s->conf->p2p_oper_reg_class == 116 ||
4945 wpa_s->conf->p2p_oper_reg_class == 117 ||
4946 wpa_s->conf->p2p_oper_reg_class == 124 ||
4947 wpa_s->conf->p2p_oper_reg_class == 126 ||
4948 wpa_s->conf->p2p_oper_reg_class == 127) &&
4949 freq_included(channels,
4950 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
4951 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4952 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4953 "frequency %d MHz", params->freq);
4954 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4955 wpa_s->best_overall_freq > 0 &&
4956 p2p_supported_freq_go(wpa_s->global->p2p,
4957 wpa_s->best_overall_freq) &&
4958 freq_included(channels, wpa_s->best_overall_freq)) {
4959 params->freq = wpa_s->best_overall_freq;
4960 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4961 "channel %d MHz", params->freq);
4962 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4963 wpa_s->best_24_freq > 0 &&
4964 p2p_supported_freq_go(wpa_s->global->p2p,
4965 wpa_s->best_24_freq) &&
4966 freq_included(channels, wpa_s->best_24_freq)) {
4967 params->freq = wpa_s->best_24_freq;
4968 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4969 "channel %d MHz", params->freq);
4970 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4971 wpa_s->best_5_freq > 0 &&
4972 p2p_supported_freq_go(wpa_s->global->p2p,
4973 wpa_s->best_5_freq) &&
4974 freq_included(channels, wpa_s->best_5_freq)) {
4975 params->freq = wpa_s->best_5_freq;
4976 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4977 "channel %d MHz", params->freq);
4978 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4980 params->freq = pref_freq;
4981 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4982 "channels", params->freq);
4985 for (chan = 0; chan < 11; chan++) {
4986 params->freq = 2412 + chan * 5;
4987 if (!wpas_p2p_disallowed_freq(wpa_s->global,
4989 freq_included(channels, params->freq))
4993 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4997 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4998 "known)", params->freq);
5001 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
5005 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5006 wpa_s->num_multichan_concurrent);
5013 for (i = 0; i < num; i++) {
5014 if (freq && freqs[i] == freq)
5016 if (!freq && freq_included(channels, freqs[i])) {
5017 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5019 params->freq = freqs[i];
5025 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5027 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5029 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5032 } else if (num == 0) {
5033 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5035 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5044 static struct wpa_supplicant *
5045 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5048 struct wpa_supplicant *group_wpa_s;
5050 if (!wpas_p2p_create_iface(wpa_s)) {
5051 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5053 wpa_s->p2p_first_connection_timeout = 0;
5057 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5058 WPA_IF_P2P_CLIENT) < 0) {
5059 wpa_msg_global(wpa_s, MSG_ERROR,
5060 "P2P: Failed to add group interface");
5063 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5064 if (group_wpa_s == NULL) {
5065 wpa_msg_global(wpa_s, MSG_ERROR,
5066 "P2P: Failed to initialize group interface");
5067 wpas_p2p_remove_pending_group_interface(wpa_s);
5071 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5072 group_wpa_s->ifname);
5073 group_wpa_s->p2p_first_connection_timeout = 0;
5079 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5080 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5081 * @persistent_group: Whether to create a persistent group
5082 * @freq: Frequency for the group or 0 to indicate no hardcoding
5083 * @ht40: Start GO with 40 MHz channel width
5084 * @vht: Start GO with VHT support
5085 * Returns: 0 on success, -1 on failure
5087 * This function creates a new P2P group with the local end as the Group Owner,
5088 * i.e., without using Group Owner Negotiation.
5090 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5091 int freq, int ht40, int vht)
5093 struct p2p_go_neg_results params;
5095 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5098 os_free(wpa_s->global->add_psk);
5099 wpa_s->global->add_psk = NULL;
5101 /* Make sure we are not running find during connection establishment */
5102 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5103 wpas_p2p_stop_find_oper(wpa_s);
5105 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5109 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5112 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5113 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5114 "(%u MHz) is not supported for P2P uses",
5118 p2p_go_params(wpa_s->global->p2p, ¶ms);
5119 params.persistent_group = persistent_group;
5121 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5124 wpas_start_wps_go(wpa_s, ¶ms, 0);
5130 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5131 struct wpa_ssid *params, int addr_allocated)
5133 struct wpa_ssid *ssid;
5135 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5138 wpa_s->p2p_last_4way_hs_fail = NULL;
5140 wpa_supplicant_ap_deinit(wpa_s);
5142 ssid = wpa_config_add_network(wpa_s->conf);
5145 wpa_config_set_network_defaults(ssid);
5146 ssid->temporary = 1;
5147 ssid->proto = WPA_PROTO_RSN;
5148 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5149 ssid->group_cipher = WPA_CIPHER_CCMP;
5150 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5151 ssid->ssid = os_malloc(params->ssid_len);
5152 if (ssid->ssid == NULL) {
5153 wpa_config_remove_network(wpa_s->conf, ssid->id);
5156 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5157 ssid->ssid_len = params->ssid_len;
5158 ssid->p2p_group = 1;
5159 ssid->export_keys = 1;
5160 if (params->psk_set) {
5161 os_memcpy(ssid->psk, params->psk, 32);
5164 if (params->passphrase)
5165 ssid->passphrase = os_strdup(params->passphrase);
5167 wpa_s->show_group_started = 1;
5169 wpa_supplicant_select_network(wpa_s, ssid);
5175 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5176 struct wpa_ssid *ssid, int addr_allocated,
5177 int freq, int ht40, int vht,
5178 const struct p2p_channels *channels,
5179 int connection_timeout)
5181 struct p2p_go_neg_results params;
5184 if (ssid->disabled != 2 || ssid->ssid == NULL)
5187 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5188 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5189 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5194 os_free(wpa_s->global->add_psk);
5195 wpa_s->global->add_psk = NULL;
5197 /* Make sure we are not running find during connection establishment */
5198 wpas_p2p_stop_find_oper(wpa_s);
5200 wpa_s->p2p_fallback_to_go_neg = 0;
5202 if (ssid->mode == WPAS_MODE_INFRA)
5203 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
5205 if (ssid->mode != WPAS_MODE_P2P_GO)
5208 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5212 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
5216 params.psk_set = ssid->psk_set;
5218 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5219 if (ssid->passphrase) {
5220 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5221 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5222 "persistent group");
5225 os_strlcpy(params.passphrase, ssid->passphrase,
5226 sizeof(params.passphrase));
5228 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5229 params.ssid_len = ssid->ssid_len;
5230 params.persistent_group = 1;
5232 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5236 wpa_s->p2p_first_connection_timeout = connection_timeout;
5237 wpas_start_wps_go(wpa_s, ¶ms, 0);
5243 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5244 struct wpabuf *proberesp_ies)
5246 struct wpa_supplicant *wpa_s = ctx;
5247 if (wpa_s->ap_iface) {
5248 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5249 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5250 wpabuf_free(beacon_ies);
5251 wpabuf_free(proberesp_ies);
5255 wpabuf_free(hapd->p2p_beacon_ie);
5256 hapd->p2p_beacon_ie = beacon_ies;
5258 wpabuf_free(hapd->p2p_probe_resp_ie);
5259 hapd->p2p_probe_resp_ie = proberesp_ies;
5261 wpabuf_free(beacon_ies);
5262 wpabuf_free(proberesp_ies);
5264 wpa_supplicant_ap_update_beacon(wpa_s);
5268 static void wpas_p2p_idle_update(void *ctx, int idle)
5270 struct wpa_supplicant *wpa_s = ctx;
5271 if (!wpa_s->ap_iface)
5273 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
5275 if (wpa_s->global->p2p_fail_on_wps_complete &&
5276 wpa_s->p2p_in_provisioning) {
5277 wpas_p2p_grpform_fail_after_wps(wpa_s);
5280 wpas_p2p_set_group_idle_timeout(wpa_s);
5282 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5286 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
5287 struct wpa_ssid *ssid)
5289 struct p2p_group *group;
5290 struct p2p_group_config *cfg;
5292 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5295 cfg = os_zalloc(sizeof(*cfg));
5299 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
5300 cfg->persistent_group = 2;
5301 else if (ssid->p2p_persistent_group)
5302 cfg->persistent_group = 1;
5303 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5304 if (wpa_s->max_stations &&
5305 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5306 cfg->max_clients = wpa_s->max_stations;
5308 cfg->max_clients = wpa_s->conf->max_num_sta;
5309 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5310 cfg->ssid_len = ssid->ssid_len;
5311 cfg->freq = ssid->frequency;
5312 cfg->cb_ctx = wpa_s;
5313 cfg->ie_update = wpas_p2p_ie_update;
5314 cfg->idle_update = wpas_p2p_idle_update;
5316 group = p2p_group_init(wpa_s->global->p2p, cfg);
5319 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
5320 p2p_group_notif_formation_done(group);
5321 wpa_s->p2p_group = group;
5326 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5329 struct wpa_ssid *ssid = wpa_s->current_ssid;
5331 if (!wpa_s->p2p_in_provisioning) {
5332 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5333 "provisioning not in progress");
5337 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5338 u8 go_dev_addr[ETH_ALEN];
5339 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5340 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5342 /* Clear any stored provisioning info */
5343 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5346 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5348 wpa_s->p2p_go_group_formation_completed = 1;
5349 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5351 * Use a separate timeout for initial data connection to
5352 * complete to allow the group to be removed automatically if
5353 * something goes wrong in this step before the P2P group idle
5354 * timeout mechanism is taken into use.
5356 wpa_dbg(wpa_s, MSG_DEBUG,
5357 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5358 P2P_MAX_INITIAL_CONN_WAIT);
5359 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5360 wpas_p2p_group_formation_timeout,
5361 wpa_s->parent, NULL);
5364 * Use a separate timeout for initial data connection to
5365 * complete to allow the group to be removed automatically if
5366 * the client does not complete data connection successfully.
5368 wpa_dbg(wpa_s, MSG_DEBUG,
5369 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5370 P2P_MAX_INITIAL_CONN_WAIT_GO);
5371 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5372 wpas_p2p_group_formation_timeout,
5373 wpa_s->parent, NULL);
5375 * Complete group formation on first successful data connection
5377 wpa_s->p2p_go_group_formation_completed = 0;
5379 if (wpa_s->global->p2p)
5380 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5381 wpas_group_formation_completed(wpa_s, 1);
5385 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5386 struct wps_event_fail *fail)
5388 if (!wpa_s->p2p_in_provisioning) {
5389 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5390 "provisioning not in progress");
5394 if (wpa_s->go_params) {
5395 p2p_clear_provisioning_info(
5397 wpa_s->go_params->peer_device_addr);
5400 wpas_notify_p2p_wps_failed(wpa_s, fail);
5402 if (wpa_s == wpa_s->global->p2p_group_formation) {
5404 * Allow some time for the failed WPS negotiation exchange to
5405 * complete, but remove the group since group formation cannot
5406 * succeed after provisioning failure.
5408 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5409 wpa_s->global->p2p_fail_on_wps_complete = 1;
5410 eloop_deplete_timeout(0, 50000,
5411 wpas_p2p_group_formation_timeout,
5412 wpa_s->parent, NULL);
5417 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5419 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5420 !wpa_s->p2p_in_provisioning)
5423 wpas_p2p_grpform_fail_after_wps(wpa_s);
5429 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5430 const char *config_method,
5431 enum wpas_p2p_prov_disc_use use)
5435 wpa_s->p2p_fallback_to_go_neg = 0;
5436 wpa_s->pending_pd_use = NORMAL_PD;
5437 if (os_strncmp(config_method, "display", 7) == 0)
5438 config_methods = WPS_CONFIG_DISPLAY;
5439 else if (os_strncmp(config_method, "keypad", 6) == 0)
5440 config_methods = WPS_CONFIG_KEYPAD;
5441 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5442 os_strncmp(config_method, "pushbutton", 10) == 0)
5443 config_methods = WPS_CONFIG_PUSHBUTTON;
5445 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5449 if (use == WPAS_P2P_PD_AUTO) {
5450 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5451 wpa_s->pending_pd_config_methods = config_methods;
5452 wpa_s->p2p_auto_pd = 1;
5453 wpa_s->p2p_auto_join = 0;
5454 wpa_s->pending_pd_before_join = 0;
5455 wpa_s->auto_pd_scan_retry = 0;
5456 wpas_p2p_stop_find(wpa_s);
5457 wpa_s->p2p_join_scan_count = 0;
5458 os_get_reltime(&wpa_s->p2p_auto_started);
5459 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5460 wpa_s->p2p_auto_started.sec,
5461 wpa_s->p2p_auto_started.usec);
5462 wpas_p2p_join_scan(wpa_s, NULL);
5466 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5469 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
5470 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5475 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5478 return p2p_scan_result_text(ies, ies_len, buf, end);
5482 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5484 if (!offchannel_pending_action_tx(wpa_s))
5487 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5488 "operation request");
5489 offchannel_clear_pending_action_tx(wpa_s);
5493 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5494 enum p2p_discovery_type type,
5495 unsigned int num_req_dev_types, const u8 *req_dev_types,
5496 const u8 *dev_id, unsigned int search_delay)
5498 wpas_p2p_clear_pending_action_tx(wpa_s);
5499 wpa_s->p2p_long_listen = 0;
5501 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5502 wpa_s->p2p_in_provisioning)
5505 wpa_supplicant_cancel_sched_scan(wpa_s);
5507 return p2p_find(wpa_s->global->p2p, timeout, type,
5508 num_req_dev_types, req_dev_types, dev_id,
5513 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
5515 wpas_p2p_clear_pending_action_tx(wpa_s);
5516 wpa_s->p2p_long_listen = 0;
5517 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5518 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5520 if (wpa_s->global->p2p)
5521 p2p_stop_find(wpa_s->global->p2p);
5527 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5529 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5531 wpas_p2p_remove_pending_group_interface(wpa_s);
5535 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5537 struct wpa_supplicant *wpa_s = eloop_ctx;
5538 wpa_s->p2p_long_listen = 0;
5542 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5546 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5549 wpa_supplicant_cancel_sched_scan(wpa_s);
5550 wpas_p2p_clear_pending_action_tx(wpa_s);
5554 * This is a request for unlimited Listen state. However, at
5555 * least for now, this is mapped to a Listen state for one
5560 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5561 wpa_s->p2p_long_listen = 0;
5564 * Stop previous find/listen operation to avoid trying to request a new
5565 * remain-on-channel operation while the driver is still running the
5568 if (wpa_s->global->p2p)
5569 p2p_stop_find(wpa_s->global->p2p);
5571 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5572 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5573 wpa_s->p2p_long_listen = timeout * 1000;
5574 eloop_register_timeout(timeout, 0,
5575 wpas_p2p_long_listen_timeout,
5583 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5584 u8 *buf, size_t len, int p2p_group)
5586 struct wpabuf *p2p_ie;
5589 if (wpa_s->global->p2p_disabled)
5591 if (wpa_s->global->p2p == NULL)
5596 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5597 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5599 wpabuf_free(p2p_ie);
5605 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
5606 const u8 *dst, const u8 *bssid,
5607 const u8 *ie, size_t ie_len, int ssi_signal)
5609 if (wpa_s->global->p2p_disabled)
5611 if (wpa_s->global->p2p == NULL)
5614 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5616 case P2P_PREQ_NOT_P2P:
5617 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5620 case P2P_PREQ_MALFORMED:
5621 case P2P_PREQ_NOT_LISTEN:
5622 case P2P_PREQ_NOT_PROCESSED:
5623 default: /* make gcc happy */
5625 case P2P_PREQ_PROCESSED:
5631 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5632 const u8 *sa, const u8 *bssid,
5633 u8 category, const u8 *data, size_t len, int freq)
5635 if (wpa_s->global->p2p_disabled)
5637 if (wpa_s->global->p2p == NULL)
5640 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5645 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5647 if (wpa_s->global->p2p_disabled)
5649 if (wpa_s->global->p2p == NULL)
5652 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5656 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5658 p2p_group_deinit(wpa_s->p2p_group);
5659 wpa_s->p2p_group = NULL;
5661 wpa_s->ap_configured_cb = NULL;
5662 wpa_s->ap_configured_cb_ctx = NULL;
5663 wpa_s->ap_configured_cb_data = NULL;
5664 wpa_s->connect_without_scan = NULL;
5668 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5670 wpa_s->p2p_long_listen = 0;
5672 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5675 return p2p_reject(wpa_s->global->p2p, addr);
5679 /* Invite to reinvoke a persistent group */
5680 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5681 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
5682 int ht40, int vht, int pref_freq)
5684 enum p2p_invite_role role;
5688 int no_pref_freq_given = pref_freq == 0;
5690 wpa_s->global->p2p_invite_group = NULL;
5692 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5694 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5696 wpa_s->p2p_persistent_go_freq = freq;
5697 wpa_s->p2p_go_ht40 = !!ht40;
5698 if (ssid->mode == WPAS_MODE_P2P_GO) {
5699 role = P2P_INVITE_ROLE_GO;
5700 if (peer_addr == NULL) {
5701 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5702 "address in invitation command");
5705 if (wpas_p2p_create_iface(wpa_s)) {
5706 if (wpas_p2p_add_group_interface(wpa_s,
5707 WPA_IF_P2P_GO) < 0) {
5708 wpa_printf(MSG_ERROR, "P2P: Failed to "
5709 "allocate a new interface for the "
5713 bssid = wpa_s->pending_interface_addr;
5715 bssid = wpa_s->own_addr;
5717 role = P2P_INVITE_ROLE_CLIENT;
5718 peer_addr = ssid->bssid;
5720 wpa_s->pending_invite_ssid_id = ssid->id;
5722 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5723 role == P2P_INVITE_ROLE_GO);
5727 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5730 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5731 no_pref_freq_given && pref_freq > 0 &&
5732 wpa_s->num_multichan_concurrent > 1 &&
5733 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5734 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5739 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5740 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5745 /* Invite to join an active group */
5746 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5747 const u8 *peer_addr, const u8 *go_dev_addr)
5749 struct wpa_global *global = wpa_s->global;
5750 enum p2p_invite_role role;
5752 struct wpa_ssid *ssid;
5754 int freq = 0, force_freq = 0, pref_freq = 0;
5757 wpa_s->p2p_persistent_go_freq = 0;
5758 wpa_s->p2p_go_ht40 = 0;
5759 wpa_s->p2p_go_vht = 0;
5761 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5762 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5765 if (wpa_s == NULL) {
5766 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5770 ssid = wpa_s->current_ssid;
5772 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5777 wpa_s->global->p2p_invite_group = wpa_s;
5778 persistent = ssid->p2p_persistent_group &&
5779 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5780 ssid->ssid, ssid->ssid_len);
5782 if (ssid->mode == WPAS_MODE_P2P_GO) {
5783 role = P2P_INVITE_ROLE_ACTIVE_GO;
5784 bssid = wpa_s->own_addr;
5785 if (go_dev_addr == NULL)
5786 go_dev_addr = wpa_s->global->p2p_dev_addr;
5787 freq = ssid->frequency;
5789 role = P2P_INVITE_ROLE_CLIENT;
5790 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5791 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5792 "invite to current group");
5795 bssid = wpa_s->bssid;
5796 if (go_dev_addr == NULL &&
5797 !is_zero_ether_addr(wpa_s->go_dev_addr))
5798 go_dev_addr = wpa_s->go_dev_addr;
5799 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5800 (int) wpa_s->assoc_freq;
5802 wpa_s->parent->pending_invite_ssid_id = -1;
5804 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5807 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5808 role == P2P_INVITE_ROLE_ACTIVE_GO);
5811 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
5813 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5814 ssid->ssid, ssid->ssid_len, force_freq,
5815 go_dev_addr, persistent, pref_freq, -1);
5819 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5821 struct wpa_ssid *ssid = wpa_s->current_ssid;
5822 const char *ssid_txt;
5823 u8 go_dev_addr[ETH_ALEN];
5824 int network_id = -1;
5830 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5831 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5832 wpa_s->parent, NULL);
5835 if (!wpa_s->show_group_started || !ssid)
5838 wpa_s->show_group_started = 0;
5840 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5841 os_memset(go_dev_addr, 0, ETH_ALEN);
5842 if (ssid->bssid_set)
5843 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5844 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5846 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5848 if (wpa_s->global->p2p_group_formation == wpa_s)
5849 wpa_s->global->p2p_group_formation = NULL;
5851 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5852 (int) wpa_s->assoc_freq;
5855 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
5856 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
5857 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
5858 ip[0], ip[1], ip[2], ip[3],
5859 ip[4], ip[5], ip[6], ip[7],
5860 ip[8], ip[9], ip[10], ip[11]);
5863 if (ssid->passphrase == NULL && ssid->psk_set) {
5865 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5866 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5867 "%s client ssid=\"%s\" freq=%d psk=%s "
5868 "go_dev_addr=" MACSTR "%s%s",
5869 wpa_s->ifname, ssid_txt, freq, psk,
5870 MAC2STR(go_dev_addr),
5871 persistent ? " [PERSISTENT]" : "", ip_addr);
5873 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5874 "%s client ssid=\"%s\" freq=%d "
5875 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s%s",
5876 wpa_s->ifname, ssid_txt, freq,
5877 ssid->passphrase ? ssid->passphrase : "",
5878 MAC2STR(go_dev_addr),
5879 persistent ? " [PERSISTENT]" : "", ip_addr);
5883 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5886 network_id = ssid->id;
5887 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
5891 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5892 u32 interval1, u32 duration2, u32 interval2)
5896 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5899 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5900 wpa_s->current_ssid == NULL ||
5901 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5904 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5905 wpa_s->own_addr, wpa_s->assoc_freq,
5906 duration1, interval1, duration2, interval2);
5908 wpa_s->waiting_presence_resp = 1;
5914 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5915 unsigned int interval)
5917 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5920 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5924 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5926 if (wpa_s->current_ssid == NULL) {
5928 * current_ssid can be cleared when P2P client interface gets
5929 * disconnected, so assume this interface was used as P2P
5934 return wpa_s->current_ssid->p2p_group &&
5935 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5939 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5941 struct wpa_supplicant *wpa_s = eloop_ctx;
5943 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
5944 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5949 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5951 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
5955 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5959 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5960 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5962 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5965 timeout = wpa_s->conf->p2p_group_idle;
5966 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5967 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5968 timeout = P2P_MAX_CLIENT_IDLE;
5974 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5975 timeout = 0; /* special client mode no-timeout */
5980 if (wpa_s->p2p_in_provisioning) {
5982 * Use the normal group formation timeout during the
5983 * provisioning phase to avoid terminating this process too
5984 * early due to group idle timeout.
5986 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5987 "during provisioning");
5991 if (wpa_s->show_group_started) {
5993 * Use the normal group formation timeout between the end of
5994 * the provisioning phase and completion of 4-way handshake to
5995 * avoid terminating this process too early due to group idle
5998 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5999 "while waiting for initial 4-way handshake to "
6004 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6006 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6011 /* Returns 1 if the interface was removed */
6012 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6013 u16 reason_code, const u8 *ie, size_t ie_len,
6014 int locally_generated)
6016 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6019 if (!locally_generated)
6020 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6023 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6024 wpa_s->current_ssid &&
6025 wpa_s->current_ssid->p2p_group &&
6026 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6027 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6028 "session is ending");
6029 if (wpas_p2p_group_delete(wpa_s,
6030 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6039 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6040 u16 reason_code, const u8 *ie, size_t ie_len,
6041 int locally_generated)
6043 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6046 if (!locally_generated)
6047 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6052 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6054 struct p2p_data *p2p = wpa_s->global->p2p;
6059 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6062 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6063 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6065 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6066 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6069 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6070 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6072 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6073 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6075 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6076 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6077 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6078 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6079 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6082 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6083 p2p_set_sec_dev_types(p2p,
6084 (void *) wpa_s->conf->sec_device_type,
6085 wpa_s->conf->num_sec_device_types);
6087 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6089 p2p_remove_wps_vendor_extensions(p2p);
6090 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6091 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6093 p2p_add_wps_vendor_extension(
6094 p2p, wpa_s->conf->wps_vendor_ext[i]);
6098 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6099 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6101 country[0] = wpa_s->conf->country[0];
6102 country[1] = wpa_s->conf->country[1];
6104 p2p_set_country(p2p, country);
6107 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6108 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6109 wpa_s->conf->p2p_ssid_postfix ?
6110 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6114 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6115 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6117 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6118 u8 reg_class, channel;
6121 if (wpa_s->conf->p2p_listen_reg_class &&
6122 wpa_s->conf->p2p_listen_channel) {
6123 reg_class = wpa_s->conf->p2p_listen_reg_class;
6124 channel = wpa_s->conf->p2p_listen_channel;
6128 * Pick one of the social channels randomly as the
6131 os_get_random((u8 *) &r, sizeof(r));
6132 channel = 1 + (r % 3) * 5;
6134 ret = p2p_set_listen_channel(p2p, reg_class, channel);
6136 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6139 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6140 u8 op_reg_class, op_channel, cfg_op_channel;
6143 if (wpa_s->conf->p2p_oper_reg_class &&
6144 wpa_s->conf->p2p_oper_channel) {
6145 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6146 op_channel = wpa_s->conf->p2p_oper_channel;
6151 * Use random operation channel from (1, 6, 11)
6152 *if no other preference is indicated.
6154 os_get_random((u8 *) &r, sizeof(r));
6155 op_channel = 1 + (r % 3) * 5;
6158 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6161 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6165 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6166 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6167 wpa_s->conf->p2p_pref_chan) < 0) {
6168 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6172 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6173 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6180 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6183 if (!wpa_s->ap_iface)
6185 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6190 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6192 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6195 wpa_s->global->cross_connection = enabled;
6196 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6199 struct wpa_supplicant *iface;
6201 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6203 if (iface->cross_connect_enabled == 0)
6206 iface->cross_connect_enabled = 0;
6207 iface->cross_connect_in_use = 0;
6208 wpa_msg_global(iface->parent, MSG_INFO,
6209 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6211 iface->cross_connect_uplink);
6219 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6221 struct wpa_supplicant *iface;
6223 if (!uplink->global->cross_connection)
6226 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6227 if (!iface->cross_connect_enabled)
6229 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6232 if (iface->ap_iface == NULL)
6234 if (iface->cross_connect_in_use)
6237 iface->cross_connect_in_use = 1;
6238 wpa_msg_global(iface->parent, MSG_INFO,
6239 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6240 iface->ifname, iface->cross_connect_uplink);
6245 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6247 struct wpa_supplicant *iface;
6249 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6250 if (!iface->cross_connect_enabled)
6252 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6255 if (!iface->cross_connect_in_use)
6258 wpa_msg_global(iface->parent, MSG_INFO,
6259 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6260 iface->ifname, iface->cross_connect_uplink);
6261 iface->cross_connect_in_use = 0;
6266 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6268 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6269 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6270 wpa_s->cross_connect_disallowed)
6271 wpas_p2p_disable_cross_connect(wpa_s);
6273 wpas_p2p_enable_cross_connect(wpa_s);
6274 if (!wpa_s->ap_iface &&
6275 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6276 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6280 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6282 wpas_p2p_disable_cross_connect(wpa_s);
6283 if (!wpa_s->ap_iface &&
6284 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6286 wpas_p2p_set_group_idle_timeout(wpa_s);
6290 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6292 struct wpa_supplicant *iface;
6294 if (!wpa_s->global->cross_connection)
6297 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6300 if (iface->drv_flags &
6301 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6303 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6306 wpa_s->cross_connect_enabled = 1;
6307 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6308 sizeof(wpa_s->cross_connect_uplink));
6309 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6310 "%s to %s whenever uplink is available",
6311 wpa_s->ifname, wpa_s->cross_connect_uplink);
6313 if (iface->ap_iface || iface->current_ssid == NULL ||
6314 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6315 iface->cross_connect_disallowed ||
6316 iface->wpa_state != WPA_COMPLETED)
6319 wpa_s->cross_connect_in_use = 1;
6320 wpa_msg_global(wpa_s->parent, MSG_INFO,
6321 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6322 wpa_s->ifname, wpa_s->cross_connect_uplink);
6328 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6330 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6331 !wpa_s->p2p_in_provisioning)
6332 return 0; /* not P2P client operation */
6334 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6336 if (wpa_s != wpa_s->parent)
6337 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
6338 wpas_p2p_group_formation_failed(wpa_s);
6343 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6345 struct p2p_channels chan, cli_chan;
6347 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6350 os_memset(&chan, 0, sizeof(chan));
6351 os_memset(&cli_chan, 0, sizeof(cli_chan));
6352 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
6353 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6358 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
6362 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6363 struct wpa_scan_results *scan_res)
6365 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6369 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6371 struct wpa_global *global = wpa_s->global;
6375 if (global->p2p == NULL)
6378 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6380 if (wpa_s->pending_interface_name[0] &&
6381 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6384 peer = p2p_get_go_neg_peer(global->p2p);
6386 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6387 MACSTR, MAC2STR(peer));
6388 p2p_unauthorize(global->p2p, peer);
6392 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6393 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6394 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6398 if (wpa_s->pending_pd_before_join) {
6399 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6400 wpa_s->pending_pd_before_join = 0;
6404 wpas_p2p_stop_find(wpa_s);
6406 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6407 if (wpa_s == global->p2p_group_formation &&
6408 (wpa_s->p2p_in_provisioning ||
6409 wpa_s->parent->pending_interface_type ==
6410 WPA_IF_P2P_CLIENT)) {
6411 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6412 "formation found - cancelling",
6415 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6416 wpa_s->parent, NULL);
6417 if (wpa_s->p2p_in_provisioning) {
6418 wpas_group_formation_completed(wpa_s, 0);
6421 wpas_p2p_group_delete(wpa_s,
6422 P2P_GROUP_REMOVAL_REQUESTED);
6428 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6436 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6438 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6441 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6442 "being available anymore");
6443 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
6447 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6448 int freq_24, int freq_5, int freq_overall)
6450 struct p2p_data *p2p = wpa_s->global->p2p;
6453 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6457 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6460 struct p2p_data *p2p = wpa_s->global->p2p;
6465 if (hwaddr_aton(addr, peer))
6468 return p2p_unauthorize(p2p, peer);
6473 * wpas_p2p_disconnect - Disconnect from a P2P Group
6474 * @wpa_s: Pointer to wpa_supplicant data
6475 * Returns: 0 on success, -1 on failure
6477 * This can be used to disconnect from a group in which the local end is a P2P
6478 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6479 * virtual network interface was created for this group, that interface will be
6480 * removed. Otherwise, only the configured P2P group network will be removed
6481 * from the interface.
6483 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6489 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6494 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6498 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6501 ret = p2p_in_progress(wpa_s->global->p2p);
6504 * Check whether there is an ongoing WPS provisioning step (or
6505 * other parts of group formation) on another interface since
6506 * p2p_in_progress() does not report this to avoid issues for
6507 * scans during such provisioning step.
6509 if (wpa_s->global->p2p_group_formation &&
6510 wpa_s->global->p2p_group_formation != wpa_s) {
6511 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6512 "in group formation",
6513 wpa_s->global->p2p_group_formation->ifname);
6518 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6519 struct os_reltime now;
6520 os_get_reltime(&now);
6521 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6522 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
6523 /* Wait for the first client has expired */
6524 wpa_s->global->p2p_go_wait_client.sec = 0;
6526 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6535 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6536 struct wpa_ssid *ssid)
6538 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6539 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6540 wpa_s->parent, NULL) > 0) {
6542 * Remove the network by scheduling the group formation
6543 * timeout to happen immediately. The teardown code
6544 * needs to be scheduled to run asynch later so that we
6545 * don't delete data from under ourselves unexpectedly.
6546 * Calling wpas_p2p_group_formation_timeout directly
6547 * causes a series of crashes in WPS failure scenarios.
6549 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6550 "P2P group network getting removed");
6551 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6552 wpa_s->parent, NULL);
6557 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
6558 const u8 *addr, const u8 *ssid,
6564 for (s = wpa_s->conf->ssid; s; s = s->next) {
6565 if (s->disabled != 2)
6568 (ssid_len != s->ssid_len ||
6569 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6572 if (s->mode == WPAS_MODE_P2P_GO)
6576 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6577 return s; /* peer is GO in the persistent group */
6578 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6580 for (i = 0; i < s->num_p2p_clients; i++) {
6581 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6582 addr, ETH_ALEN) == 0)
6583 return s; /* peer is P2P client in persistent
6592 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6595 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6596 wpa_s->parent, NULL) > 0) {
6598 * This can happen if WPS provisioning step is not terminated
6599 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6600 * peer was able to connect, there is no need to time out group
6601 * formation after this, though. In addition, this is used with
6602 * the initial connection wait on the GO as a separate formation
6603 * timeout and as such, expected to be hit after the initial WPS
6604 * provisioning step.
6606 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6608 if (!wpa_s->p2p_go_group_formation_completed) {
6609 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6610 wpa_s->p2p_go_group_formation_completed = 1;
6611 wpa_s->global->p2p_group_formation = NULL;
6612 wpa_s->p2p_in_provisioning = 0;
6614 wpa_s->global->p2p_go_wait_client.sec = 0;
6617 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6621 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6624 struct wpa_supplicant *group = wpa_s;
6625 if (wpa_s->global->p2p_group_formation)
6626 group = wpa_s->global->p2p_group_formation;
6627 wpa_s = wpa_s->parent;
6628 offchannel_send_action_done(wpa_s);
6630 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
6631 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6632 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6633 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6634 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6635 wpa_s->p2p_persistent_id,
6636 wpa_s->p2p_pd_before_go_neg,
6642 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6644 if (!wpa_s->p2p_fallback_to_go_neg ||
6645 wpa_s->p2p_in_provisioning <= 5)
6648 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6649 return 0; /* peer operating as a GO */
6651 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6652 "fallback to GO Negotiation");
6653 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6659 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6661 struct wpa_supplicant *ifs;
6663 if (wpa_s->wpa_state > WPA_SCANNING) {
6664 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6665 "concurrent operation",
6666 P2P_CONCURRENT_SEARCH_DELAY);
6667 return P2P_CONCURRENT_SEARCH_DELAY;
6670 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6672 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
6673 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6674 "delay due to concurrent operation on "
6676 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6677 return P2P_CONCURRENT_SEARCH_DELAY;
6685 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6686 struct wpa_ssid *s, const u8 *addr,
6689 struct psk_list_entry *psk, *tmp;
6692 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6694 if ((iface_addr && !psk->p2p &&
6695 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6696 (!iface_addr && psk->p2p &&
6697 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6698 wpa_dbg(wpa_s, MSG_DEBUG,
6699 "P2P: Remove persistent group PSK list entry for "
6701 MAC2STR(psk->addr), psk->p2p);
6702 dl_list_del(&psk->list);
6712 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6713 const u8 *p2p_dev_addr,
6714 const u8 *psk, size_t psk_len)
6716 struct wpa_ssid *ssid = wpa_s->current_ssid;
6717 struct wpa_ssid *persistent;
6718 struct psk_list_entry *p;
6720 if (psk_len != sizeof(p->psk))
6724 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6725 " p2p_dev_addr=" MACSTR,
6726 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6727 if (is_zero_ether_addr(p2p_dev_addr))
6728 p2p_dev_addr = NULL;
6730 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6734 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6735 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6736 /* To be added to persistent group once created */
6737 if (wpa_s->global->add_psk == NULL) {
6738 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6739 if (wpa_s->global->add_psk == NULL)
6742 p = wpa_s->global->add_psk;
6745 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6748 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6750 os_memcpy(p->psk, psk, psk_len);
6754 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6755 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6759 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6762 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6766 p = os_zalloc(sizeof(*p));
6771 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6774 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6776 os_memcpy(p->psk, psk, psk_len);
6778 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6779 struct psk_list_entry *last;
6780 last = dl_list_last(&persistent->psk_list,
6781 struct psk_list_entry, list);
6782 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6783 MACSTR " (p2p=%u) to make room for a new one",
6784 MAC2STR(last->addr), last->p2p);
6785 dl_list_del(&last->list);
6789 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6790 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6791 p2p_dev_addr == NULL);
6793 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6794 MACSTR, MAC2STR(p2p_dev_addr));
6796 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6799 dl_list_add(&persistent->psk_list, &p->list);
6801 #ifndef CONFIG_NO_CONFIG_WRITE
6802 if (wpa_s->parent->conf->update_config &&
6803 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6804 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6805 #endif /* CONFIG_NO_CONFIG_WRITE */
6809 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6810 struct wpa_ssid *s, const u8 *addr,
6815 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6817 #ifndef CONFIG_NO_CONFIG_WRITE
6818 if (wpa_s->conf->update_config &&
6819 wpa_config_write(wpa_s->confname, wpa_s->conf))
6820 wpa_dbg(wpa_s, MSG_DEBUG,
6821 "P2P: Failed to update configuration");
6822 #endif /* CONFIG_NO_CONFIG_WRITE */
6827 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6828 const u8 *peer, int iface_addr)
6830 struct hostapd_data *hapd;
6831 struct hostapd_wpa_psk *psk, *prev, *rem;
6832 struct sta_info *sta;
6834 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6835 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6838 /* Remove per-station PSK entry */
6839 hapd = wpa_s->ap_iface->bss[0];
6841 psk = hapd->conf->ssid.wpa_psk;
6843 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6845 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6846 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6847 MACSTR " iface_addr=%d",
6848 MAC2STR(peer), iface_addr);
6850 prev->next = psk->next;
6852 hapd->conf->ssid.wpa_psk = psk->next;
6862 /* Disconnect from group */
6864 sta = ap_get_sta(hapd, peer);
6866 sta = ap_get_sta_p2p(hapd, peer);
6868 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6869 " (iface_addr=%d) from group",
6870 MAC2STR(peer), iface_addr);
6871 hostapd_drv_sta_deauth(hapd, sta->addr,
6872 WLAN_REASON_DEAUTH_LEAVING);
6873 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6878 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6882 struct wpa_supplicant *w;
6884 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6886 /* Remove from any persistent group */
6887 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6888 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6891 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6892 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6895 /* Remove from any operating group */
6896 for (w = wpa_s->global->ifaces; w; w = w->next)
6897 wpas_p2p_remove_client_go(w, peer, iface_addr);
6901 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6903 struct wpa_supplicant *wpa_s = eloop_ctx;
6904 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6908 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6910 struct wpa_supplicant *wpa_s = eloop_ctx;
6912 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6913 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6917 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6918 struct wpa_ssid *ssid)
6920 struct wpa_supplicant *iface;
6922 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6923 if (!iface->current_ssid ||
6924 iface->current_ssid->frequency == freq ||
6925 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
6926 !iface->current_ssid->p2p_group))
6929 /* Remove the connection with least priority */
6930 if (!wpas_is_p2p_prioritized(iface)) {
6931 /* STA connection has priority over existing
6932 * P2P connection, so remove the interface. */
6933 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6934 eloop_register_timeout(0, 0,
6935 wpas_p2p_group_freq_conflict,
6937 /* If connection in progress is P2P connection, do not
6938 * proceed for the connection. */
6944 /* P2P connection has priority, disable the STA network
6946 wpa_supplicant_disable_network(wpa_s->global->ifaces,
6948 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
6949 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
6950 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
6952 /* If P2P connection is in progress, continue
6965 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6967 struct wpa_ssid *ssid = wpa_s->current_ssid;
6969 if (ssid == NULL || !ssid->p2p_group)
6972 if (wpa_s->p2p_last_4way_hs_fail &&
6973 wpa_s->p2p_last_4way_hs_fail == ssid) {
6974 u8 go_dev_addr[ETH_ALEN];
6975 struct wpa_ssid *persistent;
6977 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6979 ssid->ssid_len) <= 0) {
6980 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6984 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6985 MACSTR, MAC2STR(go_dev_addr));
6986 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6989 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6990 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6993 wpa_msg_global(wpa_s->parent, MSG_INFO,
6994 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6997 wpa_s->p2p_last_4way_hs_fail = NULL;
6999 * Remove the group from a timeout to avoid issues with caller
7000 * continuing to use the interface if this is on a P2P group
7003 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7008 wpa_s->p2p_last_4way_hs_fail = ssid;
7013 #ifdef CONFIG_WPS_NFC
7015 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7023 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7027 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7028 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7035 wpabuf_put_be16(ret, wsc_len);
7037 wpabuf_put_buf(ret, wsc);
7038 wpabuf_put_be16(ret, wpabuf_len(p2p));
7039 wpabuf_put_buf(ret, p2p);
7043 wpa_hexdump_buf(MSG_DEBUG,
7044 "P2P: Generated NFC connection handover message", ret);
7048 tmp = ndef_build_p2p(ret);
7051 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7061 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7062 struct wpa_ssid **ssid, u8 *go_dev_addr)
7064 struct wpa_supplicant *iface;
7067 os_memset(go_dev_addr, 0, ETH_ALEN);
7070 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7071 if (iface->wpa_state < WPA_ASSOCIATING ||
7072 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7073 !iface->current_ssid->p2p_group ||
7074 iface->current_ssid->mode != WPAS_MODE_INFRA)
7077 *ssid = iface->current_ssid;
7079 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7080 return iface->assoc_freq;
7086 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7089 struct wpabuf *wsc, *p2p;
7090 struct wpa_ssid *ssid;
7091 u8 go_dev_addr[ETH_ALEN];
7092 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7094 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7095 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7099 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7100 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7101 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7102 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7106 if (cli_freq == 0) {
7107 wsc = wps_build_nfc_handover_req_p2p(
7108 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7111 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7112 go_dev_addr, ssid ? ssid->ssid : NULL,
7113 ssid ? ssid->ssid_len : 0);
7115 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7119 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7122 struct wpabuf *wsc, *p2p;
7123 struct wpa_ssid *ssid;
7124 u8 go_dev_addr[ETH_ALEN];
7125 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7127 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7130 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7131 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7132 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7135 if (cli_freq == 0) {
7136 wsc = wps_build_nfc_handover_sel_p2p(
7138 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7139 DEV_PW_NFC_CONNECTION_HANDOVER,
7140 wpa_s->conf->wps_nfc_dh_pubkey,
7141 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7144 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7145 go_dev_addr, ssid ? ssid->ssid : NULL,
7146 ssid ? ssid->ssid_len : 0);
7148 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7152 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7153 struct p2p_nfc_params *params)
7155 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7156 "connection handover (freq=%d)",
7159 if (params->go_freq && params->go_ssid_len) {
7160 wpa_s->p2p_wps_method = WPS_NFC;
7161 wpa_s->pending_join_wps_method = WPS_NFC;
7162 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7163 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7165 return wpas_p2p_join_start(wpa_s, params->go_freq,
7167 params->go_ssid_len);
7170 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7171 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7172 params->go_freq, -1, 0, 1, 1);
7176 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7177 struct p2p_nfc_params *params, int tag)
7179 int res, persistent;
7180 struct wpa_ssid *ssid;
7182 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7183 "connection handover");
7184 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7185 ssid = wpa_s->current_ssid;
7188 if (ssid->mode != WPAS_MODE_P2P_GO)
7190 if (wpa_s->ap_iface == NULL)
7194 if (wpa_s == NULL) {
7195 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7199 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7200 DEV_PW_NFC_CONNECTION_HANDOVER &&
7201 !wpa_s->parent->p2p_oob_dev_pw) {
7202 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7205 res = wpas_ap_wps_add_nfc_pw(
7206 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7207 wpa_s->parent->p2p_oob_dev_pw,
7208 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7209 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7214 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7218 if (!params->peer ||
7219 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7222 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7223 " to join", MAC2STR(params->peer->p2p_device_addr));
7225 wpa_s->global->p2p_invite_group = wpa_s;
7226 persistent = ssid->p2p_persistent_group &&
7227 wpas_p2p_get_persistent(wpa_s->parent,
7228 params->peer->p2p_device_addr,
7229 ssid->ssid, ssid->ssid_len);
7230 wpa_s->parent->pending_invite_ssid_id = -1;
7232 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7233 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7234 ssid->ssid, ssid->ssid_len, ssid->frequency,
7235 wpa_s->global->p2p_dev_addr, persistent, 0,
7236 wpa_s->parent->p2p_oob_dev_pw_id);
7240 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7241 struct p2p_nfc_params *params,
7244 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7245 "connection handover");
7246 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7247 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7248 forced_freq, -1, 0, 1, 1);
7252 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7253 struct p2p_nfc_params *params,
7258 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7259 "connection handover");
7260 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7261 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7262 forced_freq, -1, 0, 1, 1);
7266 res = wpas_p2p_listen(wpa_s, 60);
7268 p2p_unauthorize(wpa_s->global->p2p,
7269 params->peer->p2p_device_addr);
7276 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7277 const struct wpabuf *data,
7278 int sel, int tag, int forced_freq)
7280 const u8 *pos, *end;
7282 struct p2p_nfc_params params;
7285 os_memset(¶ms, 0, sizeof(params));
7288 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7290 pos = wpabuf_head(data);
7291 end = pos + wpabuf_len(data);
7293 if (end - pos < 2) {
7294 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7298 len = WPA_GET_BE16(pos);
7300 if (pos + len > end) {
7301 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7305 params.wsc_attr = pos;
7306 params.wsc_len = len;
7309 if (end - pos < 2) {
7310 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7314 len = WPA_GET_BE16(pos);
7316 if (pos + len > end) {
7317 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7321 params.p2p_attr = pos;
7322 params.p2p_len = len;
7325 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7326 params.wsc_attr, params.wsc_len);
7327 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7328 params.p2p_attr, params.p2p_len);
7330 wpa_hexdump(MSG_DEBUG,
7331 "P2P: Ignored extra data after P2P attributes",
7335 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
7339 if (params.next_step == NO_ACTION)
7342 if (params.next_step == BOTH_GO) {
7343 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7344 MAC2STR(params.peer->p2p_device_addr));
7348 if (params.next_step == PEER_CLIENT) {
7349 if (!is_zero_ether_addr(params.go_dev_addr)) {
7350 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7351 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7353 MAC2STR(params.peer->p2p_device_addr),
7355 MAC2STR(params.go_dev_addr),
7356 wpa_ssid_txt(params.go_ssid,
7357 params.go_ssid_len));
7359 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7360 "peer=" MACSTR " freq=%d",
7361 MAC2STR(params.peer->p2p_device_addr),
7367 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7368 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7369 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7373 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7374 wpa_s->p2p_oob_dev_pw = NULL;
7376 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7377 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7382 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7383 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7384 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7385 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7386 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7387 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7388 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7392 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7393 "peer OOB Device Password Id %u", id);
7396 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7397 "Device Password Id %u", id);
7398 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7399 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7400 params.oob_dev_pw_len -
7401 WPS_OOB_PUBKEY_HASH_LEN - 2);
7402 wpa_s->p2p_oob_dev_pw_id = id;
7403 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7404 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7405 params.oob_dev_pw_len -
7406 WPS_OOB_PUBKEY_HASH_LEN - 2);
7407 if (wpa_s->p2p_oob_dev_pw == NULL)
7410 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7411 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7412 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7415 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7416 "without Device Password");
7417 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7420 switch (params.next_step) {
7424 /* already covered above */
7427 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
7429 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
7431 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
7433 /* TODO: use own OOB Dev Pw */
7434 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
7441 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7442 const struct wpabuf *data, int forced_freq)
7444 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7447 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7451 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7452 const struct wpabuf *req,
7453 const struct wpabuf *sel, int forced_freq)
7458 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7461 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7463 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7464 wpabuf_head(req), wpabuf_len(req));
7465 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7466 wpabuf_head(sel), wpabuf_len(sel));
7468 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7469 tmp = ndef_parse_p2p(init ? sel : req);
7471 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7475 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7483 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7486 int go_intent = wpa_s->conf->p2p_go_intent;
7487 struct wpa_supplicant *iface;
7489 if (wpa_s->global->p2p == NULL)
7493 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7494 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7496 if (!iface->ap_iface)
7498 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7500 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7502 if (wpa_s->p2p_nfc_tag_enabled)
7503 wpas_p2p_remove_pending_group_interface(wpa_s);
7504 wpa_s->p2p_nfc_tag_enabled = 0;
7508 if (wpa_s->global->p2p_disabled)
7511 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7512 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7513 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7514 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7515 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7516 "to allow static handover cases");
7520 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7522 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7523 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7524 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7525 if (wpa_s->p2p_oob_dev_pw == NULL)
7527 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7529 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7531 if (wpa_s->create_p2p_iface) {
7532 enum wpa_driver_if_type iftype;
7533 /* Prepare to add a new interface for the group */
7534 iftype = WPA_IF_P2P_GROUP;
7535 if (go_intent == 15)
7536 iftype = WPA_IF_P2P_GO;
7537 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7538 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7539 "interface for the group");
7543 if_addr = wpa_s->pending_interface_addr;
7545 if_addr = wpa_s->own_addr;
7547 wpa_s->p2p_nfc_tag_enabled = enabled;
7549 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7550 struct hostapd_data *hapd;
7551 if (iface->ap_iface == NULL)
7553 hapd = iface->ap_iface->bss[0];
7554 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7555 hapd->conf->wps_nfc_dh_pubkey =
7556 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7557 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7558 hapd->conf->wps_nfc_dh_privkey =
7559 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7560 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7561 hapd->conf->wps_nfc_dev_pw =
7562 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7563 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7565 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7566 wpa_dbg(iface, MSG_DEBUG,
7567 "P2P: Failed to enable NFC Tag for GO");
7570 p2p_set_authorized_oob_dev_pw_id(
7571 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
7577 #endif /* CONFIG_WPS_NFC */