3 * Copyright (c) 2009-2010, Atheros Communications
5 * This software may be distributed under the terms of the BSD license.
6 * See README for more details.
13 #include "common/ieee802_11_common.h"
14 #include "common/ieee802_11_defs.h"
15 #include "common/wpa_ctrl.h"
16 #include "wps/wps_i.h"
18 #include "ap/hostapd.h"
19 #include "ap/ap_config.h"
20 #include "ap/sta_info.h"
21 #include "ap/ap_drv_ops.h"
22 #include "ap/p2p_hostapd.h"
23 #include "eapol_supp/eapol_supp_sm.h"
24 #include "rsn_supp/wpa.h"
25 #include "wpa_supplicant_i.h"
28 #include "config_ssid.h"
33 #include "offchannel.h"
34 #include "wps_supplicant.h"
35 #include "p2p_supplicant.h"
39 * How many times to try to scan to find the GO before giving up on join
42 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
44 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
46 #ifndef P2P_MAX_CLIENT_IDLE
48 * How many seconds to try to reconnect to the GO when connection in P2P client
51 #define P2P_MAX_CLIENT_IDLE 10
52 #endif /* P2P_MAX_CLIENT_IDLE */
54 #ifndef P2P_MAX_INITIAL_CONN_WAIT
56 * How many seconds to wait for initial 4-way handshake to get completed after
57 * WPS provisioning step.
59 #define P2P_MAX_INITIAL_CONN_WAIT 10
60 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
62 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
64 * How many seconds to wait for initial 4-way handshake to get completed after
65 * WPS provisioning step on the GO. This controls the extra time the P2P
66 * operation is considered to be in progress (e.g., to delay other scans) after
67 * WPS provisioning has been completed on the GO during group formation.
69 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
70 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
72 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
74 * How many seconds to wait for initial 4-way handshake to get completed after
75 * re-invocation of a persistent group on the GO when the client is expected
76 * to connect automatically (no user interaction).
78 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
79 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
81 #ifndef P2P_CONCURRENT_SEARCH_DELAY
82 #define P2P_CONCURRENT_SEARCH_DELAY 500
83 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
85 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
87 enum p2p_group_removal_reason {
88 P2P_GROUP_REMOVAL_UNKNOWN,
89 P2P_GROUP_REMOVAL_SILENT,
90 P2P_GROUP_REMOVAL_FORMATION_FAILED,
91 P2P_GROUP_REMOVAL_REQUESTED,
92 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
93 P2P_GROUP_REMOVAL_UNAVAILABLE,
94 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
95 P2P_GROUP_REMOVAL_PSK_FAILURE
99 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
100 static struct wpa_supplicant *
101 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
103 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
104 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
105 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
106 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
107 const u8 *dev_addr, enum p2p_wps_method wps_method,
109 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
110 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
111 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
112 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
113 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
115 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
117 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
121 * Get the number of concurrent channels that the HW can operate, but that are
122 * currently not in use by any of the wpa_supplicant interfaces.
124 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
129 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
133 num = get_shared_radio_freqs(wpa_s, freqs,
134 wpa_s->num_multichan_concurrent);
137 return wpa_s->num_multichan_concurrent - num;
142 * Get the frequencies that are currently in use by one or more of the virtual
143 * interfaces, and that are also valid for P2P operation.
145 static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
146 int *p2p_freqs, unsigned int len)
149 unsigned int num, i, j;
151 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
155 num = get_shared_radio_freqs(wpa_s, freqs,
156 wpa_s->num_multichan_concurrent);
158 os_memset(p2p_freqs, 0, sizeof(int) * len);
160 for (i = 0, j = 0; i < num && j < len; i++) {
161 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
162 p2p_freqs[j++] = freqs[i];
171 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
174 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
176 if (freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
177 wpas_p2p_num_unused_channels(wpa_s) > 0 &&
178 wpa_s->parent->conf->p2p_ignore_shared_freq)
180 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
184 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
185 struct wpa_scan_results *scan_res)
189 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
192 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
193 (int) scan_res->num);
195 for (i = 0; i < scan_res->num; i++) {
196 struct wpa_scan_res *bss = scan_res->res[i];
197 struct os_time time_tmp_age, entry_ts;
201 time_tmp_age.sec = bss->age / 1000;
202 time_tmp_age.usec = (bss->age % 1000) * 1000;
203 os_time_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
205 ies = (const u8 *) (bss + 1);
206 ies_len = bss->ie_len;
207 if (bss->beacon_ie_len > 0 &&
208 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
209 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
210 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
211 MACSTR, MAC2STR(bss->bssid));
213 ies_len = bss->beacon_ie_len;
217 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
218 bss->freq, &entry_ts, bss->level,
223 p2p_scan_res_handled(wpa_s->global->p2p);
227 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
228 unsigned int num_req_dev_types,
229 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
231 struct wpa_supplicant *wpa_s = ctx;
232 struct wpa_supplicant *ifs;
233 struct wpa_driver_scan_params params;
235 struct wpabuf *wps_ie, *ies;
236 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
239 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
242 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
243 if (ifs->sta_scan_pending &&
244 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
245 wpas_p2p_in_progress(wpa_s) == 2) {
246 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
247 "pending station mode scan to be "
248 "completed on interface %s", ifs->ifname);
249 wpa_s->global->p2p_cb_on_scan_complete = 1;
250 wpa_supplicant_req_scan(ifs, 0, 0);
255 os_memset(¶ms, 0, sizeof(params));
257 /* P2P Wildcard SSID */
258 params.num_ssids = 1;
259 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
260 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
262 wpa_s->wps->dev.p2p = 1;
263 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
264 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
265 num_req_dev_types, req_dev_types);
269 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
270 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
275 wpabuf_put_buf(ies, wps_ie);
278 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
280 params.p2p_probe = 1;
281 params.extra_ies = wpabuf_head(ies);
282 params.extra_ies_len = wpabuf_len(ies);
285 case P2P_SCAN_SOCIAL:
286 params.freqs = social_channels;
290 case P2P_SCAN_SOCIAL_PLUS_ONE:
291 social_channels[3] = freq;
292 params.freqs = social_channels;
296 ret = wpa_drv_scan(wpa_s, ¶ms);
301 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
303 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
304 wpa_s->global->p2p_cb_on_scan_complete = 1;
310 os_get_time(&wpa_s->scan_trigger_time);
311 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
318 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
320 switch (p2p_group_interface) {
321 case P2P_GROUP_INTERFACE_PENDING:
322 return WPA_IF_P2P_GROUP;
323 case P2P_GROUP_INTERFACE_GO:
324 return WPA_IF_P2P_GO;
325 case P2P_GROUP_INTERFACE_CLIENT:
326 return WPA_IF_P2P_CLIENT;
329 return WPA_IF_P2P_GROUP;
333 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
335 size_t ssid_len, int *go)
339 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
340 for (s = wpa_s->conf->ssid; s; s = s->next) {
341 if (s->disabled != 0 || !s->p2p_group ||
342 s->ssid_len != ssid_len ||
343 os_memcmp(ssid, s->ssid, ssid_len) != 0)
345 if (s->mode == WPAS_MODE_P2P_GO &&
346 s != wpa_s->current_ssid)
349 *go = s->mode == WPAS_MODE_P2P_GO;
358 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
359 enum p2p_group_removal_reason removal_reason)
361 struct wpa_ssid *ssid;
365 ssid = wpa_s->current_ssid;
368 * The current SSID was not known, but there may still be a
369 * pending P2P group interface waiting for provisioning or a
370 * P2P group that is trying to reconnect.
372 ssid = wpa_s->conf->ssid;
374 if (ssid->p2p_group && ssid->disabled != 2)
379 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
381 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
386 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
388 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
389 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
390 wpa_s->reassociate = 0;
391 wpa_s->disconnected = 1;
392 wpa_supplicant_deauthenticate(wpa_s,
393 WLAN_REASON_DEAUTH_LEAVING);
397 if (wpa_s->cross_connect_in_use) {
398 wpa_s->cross_connect_in_use = 0;
399 wpa_msg_global(wpa_s->parent, MSG_INFO,
400 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
401 wpa_s->ifname, wpa_s->cross_connect_uplink);
403 switch (removal_reason) {
404 case P2P_GROUP_REMOVAL_REQUESTED:
405 reason = " reason=REQUESTED";
407 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
408 reason = " reason=FORMATION_FAILED";
410 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
411 reason = " reason=IDLE";
413 case P2P_GROUP_REMOVAL_UNAVAILABLE:
414 reason = " reason=UNAVAILABLE";
416 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
417 reason = " reason=GO_ENDING_SESSION";
419 case P2P_GROUP_REMOVAL_PSK_FAILURE:
420 reason = " reason=PSK_FAILURE";
426 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
427 wpa_msg_global(wpa_s->parent, MSG_INFO,
428 P2P_EVENT_GROUP_REMOVED "%s %s%s",
429 wpa_s->ifname, gtype, reason);
432 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
433 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
434 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
435 wpa_s->parent, NULL) > 0) {
436 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
438 wpa_s->p2p_in_provisioning = 0;
442 * Make sure wait for the first client does not remain active after the
443 * group has been removed.
445 wpa_s->global->p2p_go_wait_client.sec = 0;
447 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
448 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
450 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
451 struct wpa_global *global;
453 enum wpa_driver_if_type type;
454 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
456 global = wpa_s->global;
457 ifname = os_strdup(wpa_s->ifname);
458 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
459 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
460 wpa_s = global->ifaces;
462 wpa_drv_if_remove(wpa_s, type, ifname);
467 if (!wpa_s->p2p_go_group_formation_completed) {
468 wpa_s->global->p2p_group_formation = NULL;
469 wpa_s->p2p_in_provisioning = 0;
472 wpa_s->show_group_started = 0;
473 os_free(wpa_s->go_params);
474 wpa_s->go_params = NULL;
476 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
477 if (ssid && (ssid->p2p_group ||
478 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
479 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
481 if (ssid == wpa_s->current_ssid) {
482 wpa_sm_set_config(wpa_s->wpa, NULL);
483 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
484 wpa_s->current_ssid = NULL;
487 * Networks objects created during any P2P activities are not
488 * exposed out as they might/will confuse certain non-P2P aware
489 * applications since these network objects won't behave like
492 * Likewise, we don't send out network removed signals for such
495 wpa_config_remove_network(wpa_s->conf, id);
496 wpa_supplicant_clear_status(wpa_s);
497 wpa_supplicant_cancel_sched_scan(wpa_s);
498 wpa_s->sta_scan_pending = 0;
500 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
504 wpa_supplicant_ap_deinit(wpa_s);
506 wpa_drv_deinit_p2p_cli(wpa_s);
512 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
514 const u8 *ssid, size_t ssid_len)
522 if (wpa_s->go_params)
523 bssid = wpa_s->go_params->peer_interface_addr;
525 bssid = wpa_s->bssid;
527 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
529 u8 iface_addr[ETH_ALEN];
530 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
532 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
535 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
536 "group is persistent - BSS " MACSTR " not found",
541 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
543 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
546 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
547 "group is persistent - BSS " MACSTR
548 " did not include P2P IE", MAC2STR(bssid));
549 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
550 (u8 *) (bss + 1), bss->ie_len);
551 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
552 ((u8 *) bss + 1) + bss->ie_len,
557 group_capab = p2p_get_group_capab(p2p);
558 addr = p2p_get_go_dev_addr(p2p);
559 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
560 "group_capab=0x%x", group_capab);
562 os_memcpy(go_dev_addr, addr, ETH_ALEN);
563 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
566 os_memset(go_dev_addr, 0, ETH_ALEN);
569 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
570 "go_dev_addr=" MACSTR,
571 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
573 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
577 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
578 struct wpa_ssid *ssid,
579 const u8 *go_dev_addr)
584 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
585 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
586 for (s = wpa_s->conf->ssid; s; s = s->next) {
587 if (s->disabled == 2 &&
588 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
589 s->ssid_len == ssid->ssid_len &&
590 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
595 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
597 if (ssid->passphrase && !s->passphrase)
599 else if (ssid->passphrase && s->passphrase &&
600 os_strcmp(ssid->passphrase, s->passphrase) != 0)
603 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
606 s = wpa_config_add_network(wpa_s->conf);
611 * Instead of network_added we emit persistent_group_added
612 * notification. Also to keep the defense checks in
613 * persistent_group obj registration method, we set the
614 * relevant flags in s to designate it as a persistent group.
617 s->p2p_persistent_group = 1;
618 wpas_notify_persistent_group_added(wpa_s, s);
619 wpa_config_set_network_defaults(s);
623 s->p2p_persistent_group = 1;
626 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
627 s->mode = ssid->mode;
628 s->auth_alg = WPA_AUTH_ALG_OPEN;
629 s->key_mgmt = WPA_KEY_MGMT_PSK;
630 s->proto = WPA_PROTO_RSN;
631 s->pairwise_cipher = WPA_CIPHER_CCMP;
633 if (ssid->passphrase) {
634 os_free(s->passphrase);
635 s->passphrase = os_strdup(ssid->passphrase);
639 os_memcpy(s->psk, ssid->psk, 32);
641 if (s->passphrase && !s->psk_set)
642 wpa_config_update_psk(s);
643 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
645 s->ssid = os_malloc(ssid->ssid_len);
648 s->ssid_len = ssid->ssid_len;
649 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
651 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
652 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
653 wpa_s->global->add_psk = NULL;
657 #ifndef CONFIG_NO_CONFIG_WRITE
658 if (changed && wpa_s->conf->update_config &&
659 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
660 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
662 #endif /* CONFIG_NO_CONFIG_WRITE */
668 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
671 struct wpa_ssid *ssid, *s;
676 ssid = wpa_s->current_ssid;
677 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
678 !ssid->p2p_persistent_group)
681 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
682 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
685 if (s->ssid_len == ssid->ssid_len &&
686 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
693 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
694 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
698 if (i == s->num_p2p_clients - 1)
699 return; /* already the most recent entry */
701 /* move the entry to mark it most recent */
702 os_memmove(s->p2p_client_list + i * ETH_ALEN,
703 s->p2p_client_list + (i + 1) * ETH_ALEN,
704 (s->num_p2p_clients - i - 1) * ETH_ALEN);
705 os_memcpy(s->p2p_client_list +
706 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
711 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
712 n = os_realloc_array(s->p2p_client_list,
713 s->num_p2p_clients + 1, ETH_ALEN);
716 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
717 s->p2p_client_list = n;
718 s->num_p2p_clients++;
720 /* Not enough room for an additional entry - drop the oldest
722 os_memmove(s->p2p_client_list,
723 s->p2p_client_list + ETH_ALEN,
724 (s->num_p2p_clients - 1) * ETH_ALEN);
725 os_memcpy(s->p2p_client_list +
726 (s->num_p2p_clients - 1) * ETH_ALEN,
730 #ifndef CONFIG_NO_CONFIG_WRITE
731 if (wpa_s->parent->conf->update_config &&
732 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
733 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
734 #endif /* CONFIG_NO_CONFIG_WRITE */
738 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
741 struct wpa_ssid *ssid;
742 const char *ssid_txt;
745 u8 go_dev_addr[ETH_ALEN];
749 * This callback is likely called for the main interface. Update wpa_s
750 * to use the group interface if a new interface was created for the
753 if (wpa_s->global->p2p_group_formation)
754 wpa_s = wpa_s->global->p2p_group_formation;
755 if (wpa_s->p2p_go_group_formation_completed) {
756 wpa_s->global->p2p_group_formation = NULL;
757 wpa_s->p2p_in_provisioning = 0;
761 wpa_msg_global(wpa_s->parent, MSG_INFO,
762 P2P_EVENT_GROUP_FORMATION_FAILURE);
763 wpas_p2p_group_delete(wpa_s,
764 P2P_GROUP_REMOVAL_FORMATION_FAILED);
768 wpa_msg_global(wpa_s->parent, MSG_INFO,
769 P2P_EVENT_GROUP_FORMATION_SUCCESS);
771 ssid = wpa_s->current_ssid;
772 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
773 ssid->mode = WPAS_MODE_P2P_GO;
774 p2p_group_notif_formation_done(wpa_s->p2p_group);
775 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
780 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
781 client = ssid->mode == WPAS_MODE_INFRA;
782 if (ssid->mode == WPAS_MODE_P2P_GO) {
783 persistent = ssid->p2p_persistent_group;
784 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
787 persistent = wpas_p2p_persistent_group(wpa_s,
793 client = wpa_s->p2p_group_interface ==
794 P2P_GROUP_INTERFACE_CLIENT;
795 os_memset(go_dev_addr, 0, ETH_ALEN);
798 wpa_s->show_group_started = 0;
801 * Indicate event only after successfully completed 4-way
802 * handshake, i.e., when the interface is ready for data
805 wpa_s->show_group_started = 1;
806 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
808 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
809 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
810 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
812 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
813 MAC2STR(go_dev_addr),
814 persistent ? " [PERSISTENT]" : "");
815 wpas_p2p_cross_connect_setup(wpa_s);
816 wpas_p2p_set_group_idle_timeout(wpa_s);
818 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
819 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
820 "go_dev_addr=" MACSTR "%s",
821 wpa_s->ifname, ssid_txt,
822 ssid ? ssid->frequency : 0,
823 ssid && ssid->passphrase ? ssid->passphrase : "",
824 MAC2STR(go_dev_addr),
825 persistent ? " [PERSISTENT]" : "");
826 wpas_p2p_cross_connect_setup(wpa_s);
827 wpas_p2p_set_group_idle_timeout(wpa_s);
831 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
834 os_free(wpa_s->global->add_psk);
835 wpa_s->global->add_psk = NULL;
837 if (network_id < 0 && ssid)
838 network_id = ssid->id;
840 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
841 os_get_time(&wpa_s->global->p2p_go_wait_client);
846 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
848 const u8 *dst, const u8 *src,
850 const u8 *data, size_t data_len,
851 enum offchannel_send_action_result
854 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
856 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
858 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
862 case OFFCHANNEL_SEND_ACTION_SUCCESS:
863 res = P2P_SEND_ACTION_SUCCESS;
865 case OFFCHANNEL_SEND_ACTION_NO_ACK:
866 res = P2P_SEND_ACTION_NO_ACK;
868 case OFFCHANNEL_SEND_ACTION_FAILED:
869 res = P2P_SEND_ACTION_FAILED;
873 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
875 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
876 wpa_s->pending_pd_before_join &&
877 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
878 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
879 wpa_s->p2p_fallback_to_go_neg) {
880 wpa_s->pending_pd_before_join = 0;
881 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
882 "during p2p_connect-auto");
883 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
889 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
890 const u8 *src, const u8 *bssid, const u8 *buf,
891 size_t len, unsigned int wait_time)
893 struct wpa_supplicant *wpa_s = ctx;
894 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
896 wpas_p2p_send_action_tx_status, 1);
900 static void wpas_send_action_done(void *ctx)
902 struct wpa_supplicant *wpa_s = ctx;
903 offchannel_send_action_done(wpa_s);
907 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
908 struct p2p_go_neg_results *params)
910 if (wpa_s->go_params == NULL) {
911 wpa_s->go_params = os_malloc(sizeof(*params));
912 if (wpa_s->go_params == NULL)
915 os_memcpy(wpa_s->go_params, params, sizeof(*params));
920 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
921 struct p2p_go_neg_results *res)
923 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
924 MAC2STR(res->peer_interface_addr));
925 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
926 res->ssid, res->ssid_len);
927 wpa_supplicant_ap_deinit(wpa_s);
928 wpas_copy_go_neg_results(wpa_s, res);
929 if (res->wps_method == WPS_PBC)
930 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
932 u16 dev_pw_id = DEV_PW_DEFAULT;
933 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
934 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
935 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
936 wpa_s->p2p_pin, 1, dev_pw_id);
941 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
942 struct wpa_ssid *ssid)
944 struct wpa_ssid *persistent;
945 struct psk_list_entry *psk;
946 struct hostapd_data *hapd;
948 if (!wpa_s->ap_iface)
951 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
953 if (persistent == NULL)
956 hapd = wpa_s->ap_iface->bss[0];
958 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
960 struct hostapd_wpa_psk *hpsk;
962 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
964 MAC2STR(psk->addr), psk->p2p);
965 hpsk = os_zalloc(sizeof(*hpsk));
968 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
970 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
972 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
973 hpsk->next = hapd->conf->ssid.wpa_psk;
974 hapd->conf->ssid.wpa_psk = hpsk;
979 static void p2p_go_configured(void *ctx, void *data)
981 struct wpa_supplicant *wpa_s = ctx;
982 struct p2p_go_neg_results *params = data;
983 struct wpa_ssid *ssid;
986 ssid = wpa_s->current_ssid;
987 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
988 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
989 if (wpa_s->global->p2p_group_formation == wpa_s)
990 wpa_s->global->p2p_group_formation = NULL;
991 if (os_strlen(params->passphrase) > 0) {
992 wpa_msg_global(wpa_s->parent, MSG_INFO,
993 P2P_EVENT_GROUP_STARTED
994 "%s GO ssid=\"%s\" freq=%d "
995 "passphrase=\"%s\" go_dev_addr=" MACSTR
997 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
998 ssid->frequency, params->passphrase,
999 MAC2STR(wpa_s->global->p2p_dev_addr),
1000 params->persistent_group ?
1001 " [PERSISTENT]" : "");
1004 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1005 sizeof(params->psk));
1006 wpa_msg_global(wpa_s->parent, MSG_INFO,
1007 P2P_EVENT_GROUP_STARTED
1008 "%s GO ssid=\"%s\" freq=%d psk=%s "
1009 "go_dev_addr=" MACSTR "%s",
1011 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1012 ssid->frequency, psk,
1013 MAC2STR(wpa_s->global->p2p_dev_addr),
1014 params->persistent_group ?
1015 " [PERSISTENT]" : "");
1018 os_get_time(&wpa_s->global->p2p_go_wait_client);
1019 if (params->persistent_group) {
1020 network_id = wpas_p2p_store_persistent_group(
1021 wpa_s->parent, ssid,
1022 wpa_s->global->p2p_dev_addr);
1023 wpas_p2p_add_psk_list(wpa_s, ssid);
1026 network_id = ssid->id;
1027 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1028 wpas_p2p_cross_connect_setup(wpa_s);
1029 wpas_p2p_set_group_idle_timeout(wpa_s);
1031 if (wpa_s->p2p_first_connection_timeout) {
1032 wpa_dbg(wpa_s, MSG_DEBUG,
1033 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1034 wpa_s->p2p_first_connection_timeout);
1035 wpa_s->p2p_go_group_formation_completed = 0;
1036 wpa_s->global->p2p_group_formation = wpa_s;
1037 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1038 wpa_s->parent, NULL);
1039 eloop_register_timeout(
1040 wpa_s->p2p_first_connection_timeout, 0,
1041 wpas_p2p_group_formation_timeout,
1042 wpa_s->parent, NULL);
1048 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1049 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1050 params->peer_interface_addr)) {
1051 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1055 if (params->wps_method == WPS_PBC)
1056 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1057 params->peer_device_addr);
1058 else if (wpa_s->p2p_pin[0])
1059 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1060 wpa_s->p2p_pin, NULL, 0, 0);
1061 os_free(wpa_s->go_params);
1062 wpa_s->go_params = NULL;
1066 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1067 struct p2p_go_neg_results *params,
1068 int group_formation)
1070 struct wpa_ssid *ssid;
1072 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1073 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1074 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1079 ssid = wpa_config_add_network(wpa_s->conf);
1081 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1085 wpa_s->show_group_started = 0;
1087 wpa_config_set_network_defaults(ssid);
1088 ssid->temporary = 1;
1089 ssid->p2p_group = 1;
1090 ssid->p2p_persistent_group = params->persistent_group;
1091 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1093 ssid->frequency = params->freq;
1094 ssid->ht40 = params->ht40;
1095 ssid->vht = params->vht;
1096 ssid->ssid = os_zalloc(params->ssid_len + 1);
1098 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1099 ssid->ssid_len = params->ssid_len;
1101 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1102 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1103 ssid->proto = WPA_PROTO_RSN;
1104 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1105 if (os_strlen(params->passphrase) > 0) {
1106 ssid->passphrase = os_strdup(params->passphrase);
1107 if (ssid->passphrase == NULL) {
1108 wpa_msg_global(wpa_s, MSG_ERROR,
1109 "P2P: Failed to copy passphrase for GO");
1110 wpa_config_remove_network(wpa_s->conf, ssid->id);
1114 ssid->passphrase = NULL;
1115 ssid->psk_set = params->psk_set;
1117 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1118 else if (ssid->passphrase)
1119 wpa_config_update_psk(ssid);
1120 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1122 wpa_s->ap_configured_cb = p2p_go_configured;
1123 wpa_s->ap_configured_cb_ctx = wpa_s;
1124 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1125 wpa_s->connect_without_scan = ssid;
1126 wpa_s->reassociate = 1;
1127 wpa_s->disconnected = 0;
1128 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1130 wpa_supplicant_req_scan(wpa_s, 0, 0);
1134 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1135 const struct wpa_supplicant *src)
1137 struct wpa_config *d;
1138 const struct wpa_config *s;
1143 #define C(n) if (s->n) d->n = os_strdup(s->n)
1152 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1153 os_memcpy(d->sec_device_type, s->sec_device_type,
1154 sizeof(d->sec_device_type));
1155 d->num_sec_device_types = s->num_sec_device_types;
1157 d->p2p_group_idle = s->p2p_group_idle;
1158 d->p2p_intra_bss = s->p2p_intra_bss;
1159 d->persistent_reconnect = s->persistent_reconnect;
1160 d->max_num_sta = s->max_num_sta;
1161 d->pbc_in_m1 = s->pbc_in_m1;
1162 d->ignore_old_scan_res = s->ignore_old_scan_res;
1163 d->beacon_int = s->beacon_int;
1164 d->disassoc_low_ack = s->disassoc_low_ack;
1165 d->disable_scan_offload = s->disable_scan_offload;
1169 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1170 char *ifname, size_t len)
1172 char *ifname_ptr = wpa_s->ifname;
1174 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1175 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1176 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1179 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1180 if (os_strlen(ifname) >= IFNAMSIZ &&
1181 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1182 /* Try to avoid going over the IFNAMSIZ length limit */
1183 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1188 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1189 enum wpa_driver_if_type type)
1191 char ifname[120], force_ifname[120];
1193 if (wpa_s->pending_interface_name[0]) {
1194 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1195 "- skip creation of a new one");
1196 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1197 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1198 "unknown?! ifname='%s'",
1199 wpa_s->pending_interface_name);
1205 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1206 force_ifname[0] = '\0';
1208 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1210 wpa_s->p2p_group_idx++;
1212 wpa_s->pending_interface_type = type;
1213 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1214 wpa_s->pending_interface_addr, NULL) < 0) {
1215 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1220 if (force_ifname[0]) {
1221 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1223 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1224 sizeof(wpa_s->pending_interface_name));
1226 os_strlcpy(wpa_s->pending_interface_name, ifname,
1227 sizeof(wpa_s->pending_interface_name));
1228 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1229 MACSTR, wpa_s->pending_interface_name,
1230 MAC2STR(wpa_s->pending_interface_addr));
1236 static void wpas_p2p_remove_pending_group_interface(
1237 struct wpa_supplicant *wpa_s)
1239 if (!wpa_s->pending_interface_name[0] ||
1240 is_zero_ether_addr(wpa_s->pending_interface_addr))
1241 return; /* No pending virtual interface */
1243 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1244 wpa_s->pending_interface_name);
1245 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1246 wpa_s->pending_interface_name);
1247 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1248 wpa_s->pending_interface_name[0] = '\0';
1252 static struct wpa_supplicant *
1253 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1255 struct wpa_interface iface;
1256 struct wpa_supplicant *group_wpa_s;
1258 if (!wpa_s->pending_interface_name[0]) {
1259 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1260 if (!wpas_p2p_create_iface(wpa_s))
1263 * Something has forced us to remove the pending interface; try
1264 * to create a new one and hope for the best that we will get
1265 * the same local address.
1267 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1268 WPA_IF_P2P_CLIENT) < 0)
1272 os_memset(&iface, 0, sizeof(iface));
1273 iface.ifname = wpa_s->pending_interface_name;
1274 iface.driver = wpa_s->driver->name;
1275 if (wpa_s->conf->ctrl_interface == NULL &&
1276 wpa_s->parent != wpa_s &&
1278 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1279 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1281 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1282 iface.driver_param = wpa_s->conf->driver_param;
1283 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1284 if (group_wpa_s == NULL) {
1285 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1286 "wpa_supplicant interface");
1289 wpa_s->pending_interface_name[0] = '\0';
1290 group_wpa_s->parent = wpa_s;
1291 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1292 P2P_GROUP_INTERFACE_CLIENT;
1293 wpa_s->global->p2p_group_formation = group_wpa_s;
1295 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1301 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1304 struct wpa_supplicant *wpa_s = eloop_ctx;
1305 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1306 wpas_p2p_group_formation_failed(wpa_s);
1310 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1312 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1313 wpa_s->parent, NULL);
1314 if (wpa_s->global->p2p)
1315 p2p_group_formation_failed(wpa_s->global->p2p);
1316 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1317 wpa_drv_p2p_group_formation_failed(wpa_s);
1318 wpas_group_formation_completed(wpa_s, 0);
1322 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1324 if (wpa_s->global->p2p_group_formation != wpa_s)
1326 /* Speed up group formation timeout since this cannot succeed */
1327 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1328 wpa_s->parent, NULL);
1329 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1330 wpa_s->parent, NULL);
1334 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1336 struct wpa_supplicant *wpa_s = ctx;
1338 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1339 wpa_drv_cancel_remain_on_channel(wpa_s);
1340 wpa_s->off_channel_freq = 0;
1341 wpa_s->roc_waiting_drv_freq = 0;
1345 wpa_msg_global(wpa_s, MSG_INFO,
1346 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1348 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1349 wpas_p2p_remove_pending_group_interface(wpa_s);
1353 if (wpa_s->p2p_go_ht40)
1355 if (wpa_s->p2p_go_vht)
1358 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1359 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1361 res->role_go ? "GO" : "client", res->freq, res->ht40,
1362 MAC2STR(res->peer_device_addr),
1363 MAC2STR(res->peer_interface_addr),
1364 p2p_wps_method_text(res->wps_method));
1365 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1367 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1368 struct wpa_ssid *ssid;
1369 ssid = wpa_config_get_network(wpa_s->conf,
1370 wpa_s->p2p_persistent_id);
1371 if (ssid && ssid->disabled == 2 &&
1372 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1373 size_t len = os_strlen(ssid->passphrase);
1374 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1375 "on requested persistent group");
1376 os_memcpy(res->passphrase, ssid->passphrase, len);
1377 res->passphrase[len] = '\0';
1381 if (wpa_s->create_p2p_iface) {
1382 struct wpa_supplicant *group_wpa_s =
1383 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1384 if (group_wpa_s == NULL) {
1385 wpas_p2p_remove_pending_group_interface(wpa_s);
1388 if (group_wpa_s != wpa_s) {
1389 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1390 sizeof(group_wpa_s->p2p_pin));
1391 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1393 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1394 wpa_s->pending_interface_name[0] = '\0';
1395 group_wpa_s->p2p_in_provisioning = 1;
1398 wpas_start_wps_go(group_wpa_s, res, 1);
1400 wpas_start_wps_enrollee(group_wpa_s, res);
1402 wpa_s->p2p_in_provisioning = 1;
1403 wpa_s->global->p2p_group_formation = wpa_s;
1406 wpas_start_wps_go(wpa_s, res, 1);
1408 wpas_start_wps_enrollee(ctx, res);
1411 wpa_s->p2p_long_listen = 0;
1412 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1414 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1415 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1416 (res->peer_config_timeout % 100) * 10000,
1417 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1421 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1423 struct wpa_supplicant *wpa_s = ctx;
1424 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1425 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1427 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1431 void wpas_dev_found(void *ctx, const u8 *addr,
1432 const struct p2p_peer_info *info,
1435 #ifndef CONFIG_NO_STDOUT_DEBUG
1436 struct wpa_supplicant *wpa_s = ctx;
1437 char devtype[WPS_DEV_TYPE_BUFSIZE];
1439 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1440 " p2p_dev_addr=" MACSTR
1441 " pri_dev_type=%s name='%s' config_methods=0x%x "
1442 "dev_capab=0x%x group_capab=0x%x",
1443 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1444 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1446 info->device_name, info->config_methods,
1447 info->dev_capab, info->group_capab);
1448 #endif /* CONFIG_NO_STDOUT_DEBUG */
1450 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1454 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1456 struct wpa_supplicant *wpa_s = ctx;
1458 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1459 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1461 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1465 static void wpas_find_stopped(void *ctx)
1467 struct wpa_supplicant *wpa_s = ctx;
1468 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1472 static int wpas_start_listen(void *ctx, unsigned int freq,
1473 unsigned int duration,
1474 const struct wpabuf *probe_resp_ie)
1476 struct wpa_supplicant *wpa_s = ctx;
1478 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1480 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1481 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1482 "report received Probe Request frames");
1486 wpa_s->pending_listen_freq = freq;
1487 wpa_s->pending_listen_duration = duration;
1489 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1490 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1491 "to remain on channel (%u MHz) for Listen "
1493 wpa_s->pending_listen_freq = 0;
1496 wpa_s->off_channel_freq = 0;
1497 wpa_s->roc_waiting_drv_freq = freq;
1503 static void wpas_stop_listen(void *ctx)
1505 struct wpa_supplicant *wpa_s = ctx;
1506 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1507 wpa_drv_cancel_remain_on_channel(wpa_s);
1508 wpa_s->off_channel_freq = 0;
1509 wpa_s->roc_waiting_drv_freq = 0;
1511 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1512 wpa_drv_probe_req_report(wpa_s, 0);
1516 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1518 struct wpa_supplicant *wpa_s = ctx;
1519 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1524 * DNS Header section is used only to calculate compression pointers, so the
1525 * contents of this data does not matter, but the length needs to be reserved
1526 * in the virtual packet.
1528 #define DNS_HEADER_LEN 12
1531 * 27-octet in-memory packet from P2P specification containing two implied
1532 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1534 #define P2P_SD_IN_MEMORY_LEN 27
1536 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1537 u8 **spos, const u8 *end)
1539 while (*spos < end) {
1540 u8 val = ((*spos)[0] & 0xc0) >> 6;
1543 if (val == 1 || val == 2) {
1544 /* These are reserved values in RFC 1035 */
1545 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1546 "sequence starting with 0x%x", val);
1555 if (*spos + 2 > end) {
1556 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1557 "DNS offset field");
1561 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1562 if (offset >= *spos - start) {
1563 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1564 "pointer offset %u", offset);
1569 spos_tmp = start + offset;
1570 return p2p_sd_dns_uncompress_label(upos, uend, start,
1576 len = (*spos)[0] & 0x3f;
1581 if (*spos + len > end) {
1582 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1583 "sequence - no room for label with length "
1588 if (*upos + len + 2 > uend)
1591 os_memcpy(*upos, *spos, len);
1603 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1604 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1605 * not large enough */
1606 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1607 size_t msg_len, size_t offset)
1609 /* 27-octet in-memory packet from P2P specification */
1610 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1611 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1612 u8 *tmp, *end, *spos;
1618 if (offset > msg_len)
1621 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1624 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1625 end = spos + msg_len;
1628 os_memset(tmp, 0, DNS_HEADER_LEN);
1629 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1630 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1633 uend = buf + buf_len;
1635 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1644 } else if (upos[-1] == '.')
1652 static struct p2p_srv_bonjour *
1653 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1654 const struct wpabuf *query)
1656 struct p2p_srv_bonjour *bsrv;
1659 len = wpabuf_len(query);
1660 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1661 struct p2p_srv_bonjour, list) {
1662 if (len == wpabuf_len(bsrv->query) &&
1663 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1671 static struct p2p_srv_upnp *
1672 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1673 const char *service)
1675 struct p2p_srv_upnp *usrv;
1677 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1678 struct p2p_srv_upnp, list) {
1679 if (version == usrv->version &&
1680 os_strcmp(service, usrv->service) == 0)
1687 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1692 if (wpabuf_tailroom(resp) < 5)
1695 /* Length (to be filled) */
1696 len_pos = wpabuf_put(resp, 2);
1697 wpabuf_put_u8(resp, srv_proto);
1698 wpabuf_put_u8(resp, srv_trans_id);
1700 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1701 /* Response Data: empty */
1702 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1706 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1707 struct wpabuf *resp, u8 srv_trans_id)
1709 struct p2p_srv_bonjour *bsrv;
1712 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1714 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1715 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1719 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1720 struct p2p_srv_bonjour, list) {
1721 if (wpabuf_tailroom(resp) <
1722 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1724 /* Length (to be filled) */
1725 len_pos = wpabuf_put(resp, 2);
1726 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1727 wpabuf_put_u8(resp, srv_trans_id);
1729 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1730 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1731 wpabuf_head(bsrv->resp),
1732 wpabuf_len(bsrv->resp));
1734 wpabuf_put_buf(resp, bsrv->query); /* Key */
1735 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1736 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1742 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1745 char str_rx[256], str_srv[256];
1747 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1748 return 0; /* Too short to include DNS Type and Version */
1749 if (os_memcmp(query + query_len - 3,
1750 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1752 return 0; /* Mismatch in DNS Type or Version */
1753 if (query_len == wpabuf_len(bsrv->query) &&
1754 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1755 return 1; /* Binary match */
1757 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1759 return 0; /* Failed to uncompress query */
1760 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1761 wpabuf_head(bsrv->query),
1762 wpabuf_len(bsrv->query) - 3, 0))
1763 return 0; /* Failed to uncompress service */
1765 return os_strcmp(str_rx, str_srv) == 0;
1769 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1770 struct wpabuf *resp, u8 srv_trans_id,
1771 const u8 *query, size_t query_len)
1773 struct p2p_srv_bonjour *bsrv;
1777 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1779 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1780 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1781 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1786 if (query_len == 0) {
1787 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1791 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1792 struct p2p_srv_bonjour, list) {
1793 if (!match_bonjour_query(bsrv, query, query_len))
1796 if (wpabuf_tailroom(resp) <
1797 5 + query_len + wpabuf_len(bsrv->resp))
1802 /* Length (to be filled) */
1803 len_pos = wpabuf_put(resp, 2);
1804 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1805 wpabuf_put_u8(resp, srv_trans_id);
1808 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1809 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1810 wpabuf_head(bsrv->resp),
1811 wpabuf_len(bsrv->resp));
1814 wpabuf_put_data(resp, query, query_len); /* Key */
1815 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1817 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1821 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1823 if (wpabuf_tailroom(resp) < 5)
1826 /* Length (to be filled) */
1827 len_pos = wpabuf_put(resp, 2);
1828 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1829 wpabuf_put_u8(resp, srv_trans_id);
1832 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1833 /* Response Data: empty */
1834 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1840 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1841 struct wpabuf *resp, u8 srv_trans_id)
1843 struct p2p_srv_upnp *usrv;
1846 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1848 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1849 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1853 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1854 struct p2p_srv_upnp, list) {
1855 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1858 /* Length (to be filled) */
1859 len_pos = wpabuf_put(resp, 2);
1860 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1861 wpabuf_put_u8(resp, srv_trans_id);
1864 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1866 wpabuf_put_u8(resp, usrv->version);
1867 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1869 wpabuf_put_str(resp, usrv->service);
1870 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1876 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1877 struct wpabuf *resp, u8 srv_trans_id,
1878 const u8 *query, size_t query_len)
1880 struct p2p_srv_upnp *usrv;
1886 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1889 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1890 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1891 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1896 if (query_len == 0) {
1897 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1901 if (wpabuf_tailroom(resp) < 5)
1904 /* Length (to be filled) */
1905 len_pos = wpabuf_put(resp, 2);
1906 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1907 wpabuf_put_u8(resp, srv_trans_id);
1910 str = os_malloc(query_len);
1913 os_memcpy(str, query + 1, query_len - 1);
1914 str[query_len - 1] = '\0';
1916 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1917 struct p2p_srv_upnp, list) {
1918 if (version != usrv->version)
1921 if (os_strcmp(str, "ssdp:all") != 0 &&
1922 os_strstr(usrv->service, str) == NULL)
1925 if (wpabuf_tailroom(resp) < 2)
1929 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1931 wpabuf_put_u8(resp, version);
1933 wpabuf_put_u8(resp, ',');
1937 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1939 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1941 wpabuf_put_str(resp, usrv->service);
1946 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1949 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1950 /* Response Data: empty */
1953 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1957 #ifdef CONFIG_WIFI_DISPLAY
1958 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1959 struct wpabuf *resp, u8 srv_trans_id,
1960 const u8 *query, size_t query_len)
1966 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1968 if (!wpa_s->global->wifi_display) {
1969 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1970 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1975 if (query_len < 1) {
1976 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1981 if (wpabuf_tailroom(resp) < 5)
1986 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1988 /* TODO: role specific handling */
1990 /* Length (to be filled) */
1991 len_pos = wpabuf_put(resp, 2);
1992 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1993 wpabuf_put_u8(resp, srv_trans_id);
1994 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1996 while (pos < query + query_len) {
1997 if (*pos < MAX_WFD_SUBELEMS &&
1998 wpa_s->global->wfd_subelem[*pos] &&
1999 wpabuf_tailroom(resp) >=
2000 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2001 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2002 "subelement %u", *pos);
2003 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2008 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2010 #endif /* CONFIG_WIFI_DISPLAY */
2013 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2014 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2016 struct wpa_supplicant *wpa_s = ctx;
2017 const u8 *pos = tlvs;
2018 const u8 *end = tlvs + tlvs_len;
2021 struct wpabuf *resp;
2022 u8 srv_proto, srv_trans_id;
2026 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2028 buf_len = 2 * tlvs_len + 1;
2029 buf = os_malloc(buf_len);
2031 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2032 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2034 freq, MAC2STR(sa), dialog_token, update_indic,
2039 if (wpa_s->p2p_sd_over_ctrl_iface) {
2040 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2041 update_indic, tlvs, tlvs_len);
2042 return; /* to be processed by an external program */
2045 resp = wpabuf_alloc(10000);
2049 while (pos + 1 < end) {
2050 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2051 slen = WPA_GET_LE16(pos);
2053 if (pos + slen > end || slen < 2) {
2054 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2059 tlv_end = pos + slen;
2062 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2064 srv_trans_id = *pos++;
2065 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2068 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2069 pos, tlv_end - pos);
2072 if (wpa_s->force_long_sd) {
2073 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2075 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2076 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2080 switch (srv_proto) {
2081 case P2P_SERV_ALL_SERVICES:
2082 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2083 "for all services");
2084 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2085 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2086 wpa_printf(MSG_DEBUG, "P2P: No service "
2087 "discovery protocols available");
2088 wpas_sd_add_proto_not_avail(
2089 resp, P2P_SERV_ALL_SERVICES,
2093 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2094 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2096 case P2P_SERV_BONJOUR:
2097 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2098 pos, tlv_end - pos);
2101 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2102 pos, tlv_end - pos);
2104 #ifdef CONFIG_WIFI_DISPLAY
2105 case P2P_SERV_WIFI_DISPLAY:
2106 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2107 pos, tlv_end - pos);
2109 #endif /* CONFIG_WIFI_DISPLAY */
2111 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2112 "protocol %u", srv_proto);
2113 wpas_sd_add_proto_not_avail(resp, srv_proto,
2122 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2123 update_indic, tlvs, tlvs_len);
2125 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2131 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2132 const u8 *tlvs, size_t tlvs_len)
2134 struct wpa_supplicant *wpa_s = ctx;
2135 const u8 *pos = tlvs;
2136 const u8 *end = tlvs + tlvs_len;
2142 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2144 if (tlvs_len > 1500) {
2145 /* TODO: better way for handling this */
2146 wpa_msg_ctrl(wpa_s, MSG_INFO,
2147 P2P_EVENT_SERV_DISC_RESP MACSTR
2148 " %u <long response: %u bytes>",
2149 MAC2STR(sa), update_indic,
2150 (unsigned int) tlvs_len);
2152 buf_len = 2 * tlvs_len + 1;
2153 buf = os_malloc(buf_len);
2155 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2156 wpa_msg_ctrl(wpa_s, MSG_INFO,
2157 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2158 MAC2STR(sa), update_indic, buf);
2164 u8 srv_proto, srv_trans_id, status;
2166 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2167 slen = WPA_GET_LE16(pos);
2169 if (pos + slen > end || slen < 3) {
2170 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2174 tlv_end = pos + slen;
2177 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2179 srv_trans_id = *pos++;
2180 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2183 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2186 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2187 pos, tlv_end - pos);
2192 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2196 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2197 const struct wpabuf *tlvs)
2199 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2200 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
2201 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2203 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2207 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2208 u8 version, const char *query)
2210 struct wpabuf *tlvs;
2213 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2216 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2217 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2218 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2219 wpabuf_put_u8(tlvs, version);
2220 wpabuf_put_str(tlvs, query);
2221 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2227 #ifdef CONFIG_WIFI_DISPLAY
2229 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2230 const struct wpabuf *tlvs)
2232 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2234 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2236 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2240 #define MAX_WFD_SD_SUBELEMS 20
2242 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2243 const char *subelems)
2250 len = wpabuf_put(tlvs, 2);
2251 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2252 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2254 wpabuf_put_u8(tlvs, role);
2259 if (val >= 0 && val < 256) {
2260 wpabuf_put_u8(tlvs, val);
2262 if (count == MAX_WFD_SD_SUBELEMS)
2265 pos = os_strchr(pos + 1, ',');
2271 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2275 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2276 const u8 *dst, const char *role)
2278 struct wpabuf *tlvs;
2280 const char *subelems;
2283 subelems = os_strchr(role, ' ');
2284 if (subelems == NULL)
2288 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2292 if (os_strstr(role, "[source]"))
2293 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2294 if (os_strstr(role, "[pri-sink]"))
2295 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2296 if (os_strstr(role, "[sec-sink]"))
2297 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2298 if (os_strstr(role, "[source+sink]"))
2299 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2301 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2306 #endif /* CONFIG_WIFI_DISPLAY */
2309 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2311 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2312 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
2313 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2315 return p2p_sd_cancel_request(wpa_s->global->p2p,
2316 (void *) (uintptr_t) req);
2320 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2321 const u8 *dst, u8 dialog_token,
2322 const struct wpabuf *resp_tlvs)
2324 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2325 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2329 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2331 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2336 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2338 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2339 wpa_drv_p2p_service_update(wpa_s);
2342 if (wpa_s->global->p2p)
2343 p2p_sd_service_update(wpa_s->global->p2p);
2347 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2349 dl_list_del(&bsrv->list);
2350 wpabuf_free(bsrv->query);
2351 wpabuf_free(bsrv->resp);
2356 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2358 dl_list_del(&usrv->list);
2359 os_free(usrv->service);
2364 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2366 struct p2p_srv_bonjour *bsrv, *bn;
2367 struct p2p_srv_upnp *usrv, *un;
2369 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2370 struct p2p_srv_bonjour, list)
2371 wpas_p2p_srv_bonjour_free(bsrv);
2373 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2374 struct p2p_srv_upnp, list)
2375 wpas_p2p_srv_upnp_free(usrv);
2377 wpas_p2p_sd_service_update(wpa_s);
2381 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2382 struct wpabuf *query, struct wpabuf *resp)
2384 struct p2p_srv_bonjour *bsrv;
2386 bsrv = os_zalloc(sizeof(*bsrv));
2389 bsrv->query = query;
2391 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2393 wpas_p2p_sd_service_update(wpa_s);
2398 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2399 const struct wpabuf *query)
2401 struct p2p_srv_bonjour *bsrv;
2403 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2406 wpas_p2p_srv_bonjour_free(bsrv);
2407 wpas_p2p_sd_service_update(wpa_s);
2412 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2413 const char *service)
2415 struct p2p_srv_upnp *usrv;
2417 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2418 return 0; /* Already listed */
2419 usrv = os_zalloc(sizeof(*usrv));
2422 usrv->version = version;
2423 usrv->service = os_strdup(service);
2424 if (usrv->service == NULL) {
2428 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2430 wpas_p2p_sd_service_update(wpa_s);
2435 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2436 const char *service)
2438 struct p2p_srv_upnp *usrv;
2440 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2443 wpas_p2p_srv_upnp_free(usrv);
2444 wpas_p2p_sd_service_update(wpa_s);
2449 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2450 const u8 *peer, const char *params,
2451 unsigned int generated_pin)
2453 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2454 " %08d%s", MAC2STR(peer), generated_pin, params);
2458 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2459 const u8 *peer, const char *params)
2461 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2462 "%s", MAC2STR(peer), params);
2466 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2467 const u8 *dev_addr, const u8 *pri_dev_type,
2468 const char *dev_name, u16 supp_config_methods,
2469 u8 dev_capab, u8 group_capab, const u8 *group_id,
2470 size_t group_id_len)
2472 struct wpa_supplicant *wpa_s = ctx;
2473 char devtype[WPS_DEV_TYPE_BUFSIZE];
2475 u8 empty_dev_type[8];
2476 unsigned int generated_pin = 0;
2477 struct wpa_supplicant *group = NULL;
2480 for (group = wpa_s->global->ifaces; group; group = group->next)
2482 struct wpa_ssid *s = group->current_ssid;
2484 s->mode == WPAS_MODE_P2P_GO &&
2485 group_id_len - ETH_ALEN == s->ssid_len &&
2486 os_memcmp(group_id + ETH_ALEN, s->ssid,
2492 if (pri_dev_type == NULL) {
2493 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2494 pri_dev_type = empty_dev_type;
2496 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2497 " pri_dev_type=%s name='%s' config_methods=0x%x "
2498 "dev_capab=0x%x group_capab=0x%x%s%s",
2500 wps_dev_type_bin2str(pri_dev_type, devtype,
2502 dev_name, supp_config_methods, dev_capab, group_capab,
2503 group ? " group=" : "",
2504 group ? group->ifname : "");
2505 params[sizeof(params) - 1] = '\0';
2507 if (config_methods & WPS_CONFIG_DISPLAY) {
2508 generated_pin = wps_generate_pin();
2509 wpas_prov_disc_local_display(wpa_s, peer, params,
2511 } else if (config_methods & WPS_CONFIG_KEYPAD)
2512 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2513 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2514 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2515 MACSTR "%s", MAC2STR(peer), params);
2517 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2518 P2P_PROV_DISC_SUCCESS,
2519 config_methods, generated_pin);
2523 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2525 struct wpa_supplicant *wpa_s = ctx;
2526 unsigned int generated_pin = 0;
2529 if (wpa_s->pending_pd_before_join &&
2530 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2531 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2532 wpa_s->pending_pd_before_join = 0;
2533 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2534 "join-existing-group operation");
2535 wpas_p2p_join_start(wpa_s);
2539 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2540 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2541 os_snprintf(params, sizeof(params), " peer_go=%d",
2542 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2546 if (config_methods & WPS_CONFIG_DISPLAY)
2547 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2548 else if (config_methods & WPS_CONFIG_KEYPAD) {
2549 generated_pin = wps_generate_pin();
2550 wpas_prov_disc_local_display(wpa_s, peer, params,
2552 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2553 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2554 MACSTR "%s", MAC2STR(peer), params);
2556 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2557 P2P_PROV_DISC_SUCCESS,
2558 config_methods, generated_pin);
2562 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2563 enum p2p_prov_disc_status status)
2565 struct wpa_supplicant *wpa_s = ctx;
2567 if (wpa_s->p2p_fallback_to_go_neg) {
2568 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2569 "failed - fall back to GO Negotiation");
2570 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2574 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2575 wpa_s->pending_pd_before_join = 0;
2576 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2577 "join-existing-group operation (no ACK for PD "
2579 wpas_p2p_join_start(wpa_s);
2583 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2584 " p2p_dev_addr=" MACSTR " status=%d",
2585 MAC2STR(peer), status);
2587 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2592 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2594 if (channels == NULL)
2595 return 1; /* Assume no restrictions */
2596 return p2p_channels_includes_freq(channels, freq);
2601 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2602 const u8 *go_dev_addr, const u8 *ssid,
2603 size_t ssid_len, int *go, u8 *group_bssid,
2604 int *force_freq, int persistent_group,
2605 const struct p2p_channels *channels)
2607 struct wpa_supplicant *wpa_s = ctx;
2610 struct wpa_supplicant *grp;
2612 if (!persistent_group) {
2613 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2614 " to join an active group", MAC2STR(sa));
2615 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2616 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2618 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2619 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2620 "authorized invitation");
2624 * Do not accept the invitation automatically; notify user and
2627 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2630 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2632 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2633 "running persistent group");
2635 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2639 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2640 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2641 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2642 "invitation to re-invoke a persistent group");
2643 } else if (!wpa_s->conf->persistent_reconnect)
2644 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2646 for (s = wpa_s->conf->ssid; s; s = s->next) {
2647 if (s->disabled == 2 &&
2648 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2649 s->ssid_len == ssid_len &&
2650 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2655 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2656 " requested reinvocation of an unknown group",
2658 return P2P_SC_FAIL_UNKNOWN_GROUP;
2661 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2663 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2664 wpa_printf(MSG_DEBUG, "P2P: The only available "
2665 "interface is already in use - reject "
2667 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2669 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2670 } else if (s->mode == WPAS_MODE_P2P_GO) {
2672 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2674 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2675 "interface address for the group");
2676 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2678 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2683 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2685 /* Get one of the frequencies currently in use */
2686 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
2687 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match a channel already used by one of the interfaces");
2689 wpas_p2p_set_own_freq_preference(wpa_s, res);
2692 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2693 wpas_p2p_num_unused_channels(wpa_s) > 0) {
2695 /* We are the client */
2696 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2697 "running a GO but we are capable of MCC, "
2698 "figure out the best channel to use");
2700 } else if (!freq_included(channels, *force_freq)) {
2701 /* We are the GO, and *force_freq is not in the
2703 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2704 "in intersection but we are capable of MCC, "
2705 "figure out the best channel to use",
2711 return P2P_SC_SUCCESS;
2715 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2716 const u8 *ssid, size_t ssid_len,
2717 const u8 *go_dev_addr, u8 status,
2720 struct wpa_supplicant *wpa_s = ctx;
2723 for (s = wpa_s->conf->ssid; s; s = s->next) {
2724 if (s->disabled == 2 &&
2725 s->ssid_len == ssid_len &&
2726 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2730 if (status == P2P_SC_SUCCESS) {
2731 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2732 " was accepted; op_freq=%d MHz",
2733 MAC2STR(sa), op_freq);
2735 int go = s->mode == WPAS_MODE_P2P_GO;
2736 wpas_p2p_group_add_persistent(
2737 wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
2738 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
2740 wpa_s->user_initiated_pd = 0;
2741 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2742 wpa_s->p2p_wps_method, 0);
2747 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2748 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2749 " was rejected (status %u)", MAC2STR(sa), status);
2755 wpa_msg_global(wpa_s, MSG_INFO,
2756 P2P_EVENT_INVITATION_RECEIVED
2757 "sa=" MACSTR " go_dev_addr=" MACSTR
2758 " bssid=" MACSTR " unknown-network",
2759 MAC2STR(sa), MAC2STR(go_dev_addr),
2762 wpa_msg_global(wpa_s, MSG_INFO,
2763 P2P_EVENT_INVITATION_RECEIVED
2764 "sa=" MACSTR " go_dev_addr=" MACSTR
2766 MAC2STR(sa), MAC2STR(go_dev_addr));
2771 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2772 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2773 "sa=" MACSTR " persistent=%d freq=%d",
2774 MAC2STR(sa), s->id, op_freq);
2776 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2777 "sa=" MACSTR " persistent=%d",
2778 MAC2STR(sa), s->id);
2783 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2784 struct wpa_ssid *ssid,
2785 const u8 *peer, int inv)
2792 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2793 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2797 if (i >= ssid->num_p2p_clients) {
2798 if (ssid->mode != WPAS_MODE_P2P_GO &&
2799 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2800 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2801 "due to invitation result", ssid->id);
2802 wpas_notify_network_removed(wpa_s, ssid);
2803 wpa_config_remove_network(wpa_s->conf, ssid->id);
2806 return; /* Peer not found in client list */
2809 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2810 "group %d client list%s",
2811 MAC2STR(peer), ssid->id,
2812 inv ? " due to invitation result" : "");
2813 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2814 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2815 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2816 ssid->num_p2p_clients--;
2817 #ifndef CONFIG_NO_CONFIG_WRITE
2818 if (wpa_s->parent->conf->update_config &&
2819 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2820 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2821 #endif /* CONFIG_NO_CONFIG_WRITE */
2825 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2828 struct wpa_ssid *ssid;
2830 wpa_s = wpa_s->global->p2p_invite_group;
2832 return; /* No known invitation group */
2833 ssid = wpa_s->current_ssid;
2834 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2835 !ssid->p2p_persistent_group)
2836 return; /* Not operating as a GO in persistent group */
2837 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2838 ssid->ssid, ssid->ssid_len);
2839 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2843 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
2844 const struct p2p_channels *channels,
2847 struct wpa_supplicant *wpa_s = ctx;
2848 struct wpa_ssid *ssid;
2851 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2852 "status=%d " MACSTR,
2853 status, MAC2STR(bssid));
2855 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2856 "status=%d ", status);
2858 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2860 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2861 status, MAC2STR(peer));
2862 if (wpa_s->pending_invite_ssid_id == -1) {
2863 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2864 wpas_remove_persistent_client(wpa_s, peer);
2865 return; /* Invitation to active group */
2868 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2869 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2870 "invitation exchange to indicate readiness for "
2874 if (status != P2P_SC_SUCCESS) {
2875 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2876 ssid = wpa_config_get_network(
2877 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2878 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2880 wpas_p2p_remove_pending_group_interface(wpa_s);
2884 ssid = wpa_config_get_network(wpa_s->conf,
2885 wpa_s->pending_invite_ssid_id);
2887 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2888 "data matching with invitation");
2893 * The peer could have missed our ctrl::ack frame for Invitation
2894 * Response and continue retransmitting the frame. To reduce the
2895 * likelihood of the peer not getting successful TX status for the
2896 * Invitation Response frame, wait a short time here before starting
2897 * the persistent group so that we will remain on the current channel to
2898 * acknowledge any possible retransmission from the peer.
2900 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2901 "starting persistent group");
2904 wpas_p2p_group_add_persistent(wpa_s, ssid,
2905 ssid->mode == WPAS_MODE_P2P_GO,
2906 wpa_s->p2p_persistent_go_freq,
2907 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2909 ssid->mode == WPAS_MODE_P2P_GO ?
2910 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2915 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2918 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
2922 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2924 reg->channel[reg->channels] = chan;
2929 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2930 struct p2p_channels *chan,
2931 struct p2p_channels *cli_chan)
2935 os_memset(cli_chan, 0, sizeof(*cli_chan));
2937 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2940 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2941 chan->reg_class[cla].reg_class = 81;
2942 chan->reg_class[cla].channels = 0;
2943 for (i = 0; i < 11; i++) {
2944 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2945 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2947 if (chan->reg_class[cla].channels)
2950 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2953 /* Operating class 115 - 5 GHz, channels 36-48 */
2954 chan->reg_class[cla].reg_class = 115;
2955 chan->reg_class[cla].channels = 0;
2956 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2957 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2958 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2959 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2960 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2961 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2962 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2963 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2964 if (chan->reg_class[cla].channels)
2967 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2970 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2971 chan->reg_class[cla].reg_class = 124;
2972 chan->reg_class[cla].channels = 0;
2973 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2974 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2975 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2976 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2977 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2978 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2979 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2980 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2981 if (chan->reg_class[cla].channels)
2984 chan->reg_classes = cla;
2989 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2991 enum hostapd_hw_mode mode)
2995 for (i = 0; i < num_modes; i++) {
2996 if (modes[i].mode == mode)
3005 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3008 static int has_channel(struct wpa_global *global,
3009 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3014 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3016 if (wpas_p2p_disallowed_freq(global, freq))
3019 for (i = 0; i < mode->num_channels; i++) {
3020 if (mode->channels[i].chan == chan) {
3022 *flags = mode->channels[i].flag;
3023 if (mode->channels[i].flag &
3024 (HOSTAPD_CHAN_DISABLED |
3025 HOSTAPD_CHAN_RADAR))
3027 if (mode->channels[i].flag &
3028 (HOSTAPD_CHAN_PASSIVE_SCAN |
3029 HOSTAPD_CHAN_NO_IBSS))
3030 return PASSIVE_ONLY;
3039 struct p2p_oper_class_map {
3040 enum hostapd_hw_mode mode;
3045 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
3048 static struct p2p_oper_class_map op_class[] = {
3049 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3050 #if 0 /* Do not enable HT40 on 2 GHz for now */
3051 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3052 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3054 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3055 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3056 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3057 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3058 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3059 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3062 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3063 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3064 * 80 MHz, but currently use the following definition for simplicity
3065 * (these center frequencies are not actual channels, which makes
3066 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3067 * removing invalid channels.
3069 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3070 { -1, 0, 0, 0, 0, BW20 }
3074 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3075 struct hostapd_hw_modes *mode,
3078 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3081 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3084 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3086 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3087 * so the center channel is 6 channels away from the start/end.
3089 if (channel >= center_channels[i] - 6 &&
3090 channel <= center_channels[i] + 6)
3091 return center_channels[i];
3097 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3098 struct hostapd_hw_modes *mode,
3103 enum chan_allowed res, ret = ALLOWED;
3105 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3108 if (center_chan >= 58 && center_chan <= 138)
3109 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3111 /* check all the channels are available */
3112 for (i = 0; i < 4; i++) {
3113 int adj_chan = center_chan - 6 + i * 4;
3115 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3116 if (res == NOT_ALLOWED)
3118 if (res == PASSIVE_ONLY)
3121 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3123 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3125 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3127 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3135 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3136 struct hostapd_hw_modes *mode,
3140 enum chan_allowed res, res2;
3142 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3143 if (bw == BW40MINUS) {
3144 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3146 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3147 } else if (bw == BW40PLUS) {
3148 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3150 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3151 } else if (bw == BW80) {
3152 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3155 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3157 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3158 return PASSIVE_ONLY;
3163 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3164 struct p2p_channels *chan,
3165 struct p2p_channels *cli_chan)
3167 struct hostapd_hw_modes *mode;
3168 int cla, op, cli_cla;
3170 if (wpa_s->hw.modes == NULL) {
3171 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3172 "of all supported channels; assume dualband "
3174 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3179 for (op = 0; op_class[op].op_class; op++) {
3180 struct p2p_oper_class_map *o = &op_class[op];
3182 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3184 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3187 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3188 enum chan_allowed res;
3189 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3190 if (res == ALLOWED) {
3192 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3194 reg = &chan->reg_class[cla];
3196 reg->reg_class = o->op_class;
3198 reg->channel[reg->channels] = ch;
3200 } else if (res == PASSIVE_ONLY &&
3201 wpa_s->conf->p2p_add_cli_chan) {
3202 if (cli_reg == NULL) {
3203 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3205 cli_reg = &cli_chan->reg_class[cli_cla];
3207 cli_reg->reg_class = o->op_class;
3209 cli_reg->channel[cli_reg->channels] = ch;
3210 cli_reg->channels++;
3214 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3215 reg->channel, reg->channels);
3218 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3219 cli_reg->channel, cli_reg->channels);
3223 chan->reg_classes = cla;
3224 cli_chan->reg_classes = cli_cla;
3230 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3231 struct hostapd_hw_modes *mode, u8 channel)
3234 enum chan_allowed ret;
3236 for (op = 0; op_class[op].op_class; op++) {
3237 struct p2p_oper_class_map *o = &op_class[op];
3240 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3241 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3242 o->bw == BW20 || ch != channel)
3244 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3246 return (o->bw == BW40MINUS) ? -1 : 1;
3253 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3254 struct hostapd_hw_modes *mode, u8 channel)
3256 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3259 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3263 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3266 struct wpa_supplicant *wpa_s = ctx;
3268 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3269 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3275 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3279 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3281 struct wpa_supplicant *wpa_s = ctx;
3283 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3284 struct wpa_ssid *ssid = wpa_s->current_ssid;
3287 if (ssid->mode != WPAS_MODE_INFRA)
3289 if (wpa_s->wpa_state != WPA_COMPLETED &&
3290 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3292 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3300 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3302 struct wpa_supplicant *wpa_s = ctx;
3303 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3307 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3309 struct wpa_interface iface;
3310 struct wpa_supplicant *p2pdev_wpa_s;
3312 char force_name[100];
3315 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3317 force_name[0] = '\0';
3318 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3319 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3320 force_name, wpa_s->pending_interface_addr, NULL);
3322 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3325 os_strlcpy(wpa_s->pending_interface_name, ifname,
3326 sizeof(wpa_s->pending_interface_name));
3328 os_memset(&iface, 0, sizeof(iface));
3330 iface.ifname = wpa_s->pending_interface_name;
3331 iface.driver = wpa_s->driver->name;
3332 iface.driver_param = wpa_s->conf->driver_param;
3333 iface.confname = wpa_s->confname;
3334 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3335 if (!p2pdev_wpa_s) {
3336 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3339 p2pdev_wpa_s->parent = wpa_s;
3341 wpa_s->pending_interface_name[0] = '\0';
3347 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3348 * @global: Pointer to global data from wpa_supplicant_init()
3349 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3350 * Returns: 0 on success, -1 on failure
3352 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3354 struct p2p_config p2p;
3358 if (wpa_s->conf->p2p_disabled)
3361 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3367 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3368 struct p2p_params params;
3370 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3371 os_memset(¶ms, 0, sizeof(params));
3372 params.dev_name = wpa_s->conf->device_name;
3373 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3375 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3376 os_memcpy(params.sec_dev_type,
3377 wpa_s->conf->sec_device_type,
3378 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3380 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
3386 os_memset(&p2p, 0, sizeof(p2p));
3388 p2p.debug_print = wpas_p2p_debug_print;
3389 p2p.p2p_scan = wpas_p2p_scan;
3390 p2p.send_action = wpas_send_action;
3391 p2p.send_action_done = wpas_send_action_done;
3392 p2p.go_neg_completed = wpas_go_neg_completed;
3393 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3394 p2p.dev_found = wpas_dev_found;
3395 p2p.dev_lost = wpas_dev_lost;
3396 p2p.find_stopped = wpas_find_stopped;
3397 p2p.start_listen = wpas_start_listen;
3398 p2p.stop_listen = wpas_stop_listen;
3399 p2p.send_probe_resp = wpas_send_probe_resp;
3400 p2p.sd_request = wpas_sd_request;
3401 p2p.sd_response = wpas_sd_response;
3402 p2p.prov_disc_req = wpas_prov_disc_req;
3403 p2p.prov_disc_resp = wpas_prov_disc_resp;
3404 p2p.prov_disc_fail = wpas_prov_disc_fail;
3405 p2p.invitation_process = wpas_invitation_process;
3406 p2p.invitation_received = wpas_invitation_received;
3407 p2p.invitation_result = wpas_invitation_result;
3408 p2p.get_noa = wpas_get_noa;
3409 p2p.go_connected = wpas_go_connected;
3411 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
3412 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
3413 p2p.dev_name = wpa_s->conf->device_name;
3414 p2p.manufacturer = wpa_s->conf->manufacturer;
3415 p2p.model_name = wpa_s->conf->model_name;
3416 p2p.model_number = wpa_s->conf->model_number;
3417 p2p.serial_number = wpa_s->conf->serial_number;
3419 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3420 p2p.config_methods = wpa_s->wps->config_methods;
3423 if (wpa_s->conf->p2p_listen_reg_class &&
3424 wpa_s->conf->p2p_listen_channel) {
3425 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3426 p2p.channel = wpa_s->conf->p2p_listen_channel;
3430 * Pick one of the social channels randomly as the listen
3433 os_get_random((u8 *) &r, sizeof(r));
3434 p2p.channel = 1 + (r % 3) * 5;
3436 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3438 if (wpa_s->conf->p2p_oper_reg_class &&
3439 wpa_s->conf->p2p_oper_channel) {
3440 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3441 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3442 p2p.cfg_op_channel = 1;
3443 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3444 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3447 p2p.op_reg_class = 81;
3449 * Use random operation channel from (1, 6, 11) if no other
3450 * preference is indicated.
3452 os_get_random((u8 *) &r, sizeof(r));
3453 p2p.op_channel = 1 + (r % 3) * 5;
3454 p2p.cfg_op_channel = 0;
3455 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3456 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3459 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3460 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3461 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3464 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3465 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3466 p2p.country[2] = 0x04;
3468 os_memcpy(p2p.country, "XX\x04", 3);
3470 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3471 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3476 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3479 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3480 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3481 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3483 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3484 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3486 p2p.max_peers = 100;
3488 if (wpa_s->conf->p2p_ssid_postfix) {
3489 p2p.ssid_postfix_len =
3490 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3491 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3492 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3493 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3494 p2p.ssid_postfix_len);
3497 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3499 p2p.max_listen = wpa_s->max_remain_on_chan;
3501 global->p2p = p2p_init(&p2p);
3502 if (global->p2p == NULL)
3504 global->p2p_init_wpa_s = wpa_s;
3506 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3507 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3509 p2p_add_wps_vendor_extension(
3510 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3513 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3520 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3521 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3523 * This function deinitialize per-interface P2P data.
3525 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3527 if (wpa_s->driver && wpa_s->drv_priv)
3528 wpa_drv_probe_req_report(wpa_s, 0);
3530 if (wpa_s->go_params) {
3531 /* Clear any stored provisioning info */
3532 p2p_clear_provisioning_info(
3534 wpa_s->go_params->peer_device_addr);
3537 os_free(wpa_s->go_params);
3538 wpa_s->go_params = NULL;
3539 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3540 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3541 wpa_s->p2p_long_listen = 0;
3542 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3543 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3544 wpas_p2p_remove_pending_group_interface(wpa_s);
3546 /* TODO: remove group interface from the driver if this wpa_s instance
3547 * is on top of a P2P group interface */
3552 * wpas_p2p_deinit_global - Deinitialize global P2P module
3553 * @global: Pointer to global data from wpa_supplicant_init()
3555 * This function deinitializes the global (per device) P2P module.
3557 void wpas_p2p_deinit_global(struct wpa_global *global)
3559 struct wpa_supplicant *wpa_s, *tmp;
3561 wpa_s = global->ifaces;
3563 wpas_p2p_service_flush(wpa_s);
3565 if (global->p2p == NULL)
3568 /* Remove remaining P2P group interfaces */
3569 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3570 wpa_s = wpa_s->next;
3572 tmp = global->ifaces;
3575 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3580 /* Disconnect from the P2P group and deinit the interface */
3581 wpas_p2p_disconnect(tmp);
3585 * Deinit GO data on any possibly remaining interface (if main
3586 * interface is used as GO).
3588 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3589 if (wpa_s->ap_iface)
3590 wpas_p2p_group_deinit(wpa_s);
3593 p2p_deinit(global->p2p);
3595 global->p2p_init_wpa_s = NULL;
3599 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3601 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3602 wpa_s->conf->p2p_no_group_iface)
3603 return 0; /* separate interface disabled per configuration */
3604 if (wpa_s->drv_flags &
3605 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3606 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3607 return 1; /* P2P group requires a new interface in every case
3609 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3610 return 0; /* driver does not support concurrent operations */
3611 if (wpa_s->global->ifaces->next)
3612 return 1; /* more that one interface already in use */
3613 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3614 return 1; /* this interface is already in use */
3619 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3620 const u8 *peer_addr,
3621 enum p2p_wps_method wps_method,
3622 int go_intent, const u8 *own_interface_addr,
3623 unsigned int force_freq, int persistent_group,
3624 struct wpa_ssid *ssid, unsigned int pref_freq)
3626 if (persistent_group && wpa_s->conf->persistent_reconnect)
3627 persistent_group = 2;
3629 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3630 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3631 go_intent, own_interface_addr,
3632 force_freq, persistent_group);
3636 * Increase GO config timeout if HT40 is used since it takes some time
3637 * to scan channels for coex purposes before the BSS can be started.
3639 p2p_set_config_timeout(wpa_s->global->p2p,
3640 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3642 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3643 go_intent, own_interface_addr, force_freq,
3644 persistent_group, ssid ? ssid->ssid : NULL,
3645 ssid ? ssid->ssid_len : 0,
3646 wpa_s->p2p_pd_before_go_neg, pref_freq);
3650 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3651 const u8 *peer_addr,
3652 enum p2p_wps_method wps_method,
3653 int go_intent, const u8 *own_interface_addr,
3654 unsigned int force_freq, int persistent_group,
3655 struct wpa_ssid *ssid, unsigned int pref_freq)
3657 if (persistent_group && wpa_s->conf->persistent_reconnect)
3658 persistent_group = 2;
3660 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3663 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3664 go_intent, own_interface_addr, force_freq,
3665 persistent_group, ssid ? ssid->ssid : NULL,
3666 ssid ? ssid->ssid_len : 0, pref_freq);
3670 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3672 wpa_s->p2p_join_scan_count++;
3673 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3674 wpa_s->p2p_join_scan_count);
3675 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3676 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3677 " for join operationg - stop join attempt",
3678 MAC2STR(wpa_s->pending_join_iface_addr));
3679 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3680 if (wpa_s->p2p_auto_pd) {
3681 wpa_s->p2p_auto_pd = 0;
3682 wpa_msg_global(wpa_s, MSG_INFO,
3683 P2P_EVENT_PROV_DISC_FAILURE
3684 " p2p_dev_addr=" MACSTR " status=N/A",
3685 MAC2STR(wpa_s->pending_join_dev_addr));
3688 wpa_msg_global(wpa_s->parent, MSG_INFO,
3689 P2P_EVENT_GROUP_FORMATION_FAILURE);
3694 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3696 int *freqs, res, num, i;
3698 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3699 /* Multiple channels are supported and not all are in use */
3703 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3707 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3708 wpa_s->num_multichan_concurrent);
3714 for (i = 0; i < num; i++) {
3715 if (freqs[i] == freq) {
3716 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3731 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3732 const u8 *peer_dev_addr)
3734 struct wpa_bss *bss;
3737 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3740 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3741 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3746 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3747 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3748 "%ld.%06ld (%supdated in last scan)",
3749 bss->last_update.sec, bss->last_update.usec,
3750 updated ? "": "not ");
3756 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3757 struct wpa_scan_results *scan_res)
3759 struct wpa_bss *bss;
3761 u8 iface_addr[ETH_ALEN];
3763 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3765 if (wpa_s->global->p2p_disabled)
3768 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3769 scan_res ? (int) scan_res->num : -1,
3770 wpa_s->p2p_auto_join ? "auto_" : "");
3773 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3775 if (wpa_s->p2p_auto_pd) {
3776 int join = wpas_p2p_peer_go(wpa_s,
3777 wpa_s->pending_join_dev_addr);
3779 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3780 wpa_s->auto_pd_scan_retry++;
3781 bss = wpa_bss_get_bssid_latest(
3782 wpa_s, wpa_s->pending_join_dev_addr);
3785 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3786 "the peer " MACSTR " at %d MHz",
3787 wpa_s->auto_pd_scan_retry,
3789 pending_join_dev_addr),
3791 wpas_p2p_join_scan_req(wpa_s, freq);
3799 wpa_s->p2p_auto_pd = 0;
3800 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3801 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3802 MAC2STR(wpa_s->pending_join_dev_addr), join);
3803 if (p2p_prov_disc_req(wpa_s->global->p2p,
3804 wpa_s->pending_join_dev_addr,
3805 wpa_s->pending_pd_config_methods, join,
3806 0, wpa_s->user_initiated_pd) < 0) {
3807 wpa_s->p2p_auto_pd = 0;
3808 wpa_msg_global(wpa_s, MSG_INFO,
3809 P2P_EVENT_PROV_DISC_FAILURE
3810 " p2p_dev_addr=" MACSTR " status=N/A",
3811 MAC2STR(wpa_s->pending_join_dev_addr));
3816 if (wpa_s->p2p_auto_join) {
3817 int join = wpas_p2p_peer_go(wpa_s,
3818 wpa_s->pending_join_dev_addr);
3820 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3821 "running a GO -> use GO Negotiation");
3822 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3823 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3824 wpa_s->p2p_persistent_group, 0, 0, 0,
3825 wpa_s->p2p_go_intent,
3826 wpa_s->p2p_connect_freq,
3827 wpa_s->p2p_persistent_id,
3828 wpa_s->p2p_pd_before_go_neg,
3834 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3835 "try to join the group", join ? "" :
3838 wpa_s->p2p_fallback_to_go_neg = 1;
3841 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3842 wpa_s->pending_join_iface_addr);
3844 p2p_get_interface_addr(wpa_s->global->p2p,
3845 wpa_s->pending_join_dev_addr,
3847 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3849 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3850 "address for join from " MACSTR " to " MACSTR
3851 " based on newly discovered P2P peer entry",
3852 MAC2STR(wpa_s->pending_join_iface_addr),
3853 MAC2STR(iface_addr));
3854 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3857 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3858 wpa_s->pending_join_iface_addr);
3861 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3862 "from P2P peer table: %d MHz", freq);
3864 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
3867 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3868 "from BSS table: %d MHz (SSID %s)", freq,
3869 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3874 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3875 wpa_msg_global(wpa_s->parent, MSG_INFO,
3876 P2P_EVENT_GROUP_FORMATION_FAILURE
3877 "reason=FREQ_CONFLICT");
3881 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3882 "prior to joining an existing group (GO " MACSTR
3884 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3885 wpa_s->pending_pd_before_join = 1;
3887 switch (wpa_s->pending_join_wps_method) {
3888 case WPS_PIN_DISPLAY:
3889 method = WPS_CONFIG_KEYPAD;
3891 case WPS_PIN_KEYPAD:
3892 method = WPS_CONFIG_DISPLAY;
3895 method = WPS_CONFIG_PUSHBUTTON;
3902 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3903 wpa_s->pending_join_dev_addr) ==
3906 * We have already performed provision discovery for
3907 * joining the group. Proceed directly to join
3908 * operation without duplicated provision discovery. */
3909 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3910 "with " MACSTR " already done - proceed to "
3912 MAC2STR(wpa_s->pending_join_dev_addr));
3913 wpa_s->pending_pd_before_join = 0;
3917 if (p2p_prov_disc_req(wpa_s->global->p2p,
3918 wpa_s->pending_join_dev_addr, method, 1,
3919 freq, wpa_s->user_initiated_pd) < 0) {
3920 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3921 "Discovery Request before joining an "
3923 wpa_s->pending_pd_before_join = 0;
3929 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3930 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3931 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3932 wpas_p2p_check_join_scan_limit(wpa_s);
3936 /* Start join operation immediately */
3937 wpas_p2p_join_start(wpa_s);
3941 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3944 struct wpa_driver_scan_params params;
3945 struct wpabuf *wps_ie, *ies;
3947 int freqs[2] = { 0, 0 };
3949 os_memset(¶ms, 0, sizeof(params));
3951 /* P2P Wildcard SSID */
3952 params.num_ssids = 1;
3953 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3954 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3956 wpa_s->wps->dev.p2p = 1;
3957 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3958 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3960 if (wps_ie == NULL) {
3961 wpas_p2p_scan_res_join(wpa_s, NULL);
3965 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3966 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
3968 wpabuf_free(wps_ie);
3969 wpas_p2p_scan_res_join(wpa_s, NULL);
3972 wpabuf_put_buf(ies, wps_ie);
3973 wpabuf_free(wps_ie);
3975 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3977 params.p2p_probe = 1;
3978 params.extra_ies = wpabuf_head(ies);
3979 params.extra_ies_len = wpabuf_len(ies);
3982 params.freqs = freqs;
3986 * Run a scan to update BSS table and start Provision Discovery once
3987 * the new scan results become available.
3989 ret = wpa_drv_scan(wpa_s, ¶ms);
3991 os_get_time(&wpa_s->scan_trigger_time);
3992 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
3998 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4000 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4001 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4002 wpas_p2p_check_join_scan_limit(wpa_s);
4007 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4009 struct wpa_supplicant *wpa_s = eloop_ctx;
4010 wpas_p2p_join_scan_req(wpa_s, 0);
4014 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4015 const u8 *dev_addr, enum p2p_wps_method wps_method,
4018 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4019 MACSTR " dev " MACSTR ")%s",
4020 MAC2STR(iface_addr), MAC2STR(dev_addr),
4021 auto_join ? " (auto_join)" : "");
4023 wpa_s->p2p_auto_pd = 0;
4024 wpa_s->p2p_auto_join = !!auto_join;
4025 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4026 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4027 wpa_s->pending_join_wps_method = wps_method;
4029 /* Make sure we are not running find during connection establishment */
4030 wpas_p2p_stop_find(wpa_s);
4032 wpa_s->p2p_join_scan_count = 0;
4033 wpas_p2p_join_scan(wpa_s, NULL);
4038 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
4040 struct wpa_supplicant *group;
4041 struct p2p_go_neg_results res;
4042 struct wpa_bss *bss;
4044 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4047 if (group != wpa_s) {
4048 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4049 sizeof(group->p2p_pin));
4050 group->p2p_wps_method = wpa_s->p2p_wps_method;
4053 * Need to mark the current interface for p2p_group_formation
4054 * when a separate group interface is not used. This is needed
4055 * to allow p2p_cancel stop a pending p2p_connect-join.
4056 * wpas_p2p_init_group_interface() addresses this for the case
4057 * where a separate group interface is used.
4059 wpa_s->global->p2p_group_formation = wpa_s;
4062 group->p2p_in_provisioning = 1;
4063 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4065 os_memset(&res, 0, sizeof(res));
4066 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4068 res.wps_method = wpa_s->pending_join_wps_method;
4069 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
4071 res.freq = bss->freq;
4072 res.ssid_len = bss->ssid_len;
4073 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4074 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
4075 "from BSS table: %d MHz (SSID %s)", bss->freq,
4076 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4079 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4080 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4082 wpa_drv_cancel_remain_on_channel(wpa_s);
4083 wpa_s->off_channel_freq = 0;
4084 wpa_s->roc_waiting_drv_freq = 0;
4086 wpas_start_wps_enrollee(group, &res);
4089 * Allow a longer timeout for join-a-running-group than normal 15
4090 * second group formation timeout since the GO may not have authorized
4091 * our connection yet.
4093 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4094 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4101 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4102 int *force_freq, int *pref_freq, int go)
4105 unsigned int freq_in_use = 0, num, i;
4107 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4111 num = get_shared_radio_freqs(wpa_s, freqs,
4112 wpa_s->num_multichan_concurrent);
4113 wpa_printf(MSG_DEBUG,
4114 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4115 freq, wpa_s->num_multichan_concurrent, num);
4120 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4122 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4124 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4125 "(%u MHz) is not supported for P2P uses",
4131 for (i = 0; i < num; i++) {
4132 if (freqs[i] == freq)
4136 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4137 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4142 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4143 "requested channel (%u MHz)", freq);
4148 for (i = 0; i < num; i++) {
4149 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4152 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4154 *force_freq = freqs[i];
4156 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
4157 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
4158 *pref_freq = *force_freq;
4164 if (num < wpa_s->num_multichan_concurrent) {
4165 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4168 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4183 * wpas_p2p_connect - Request P2P Group Formation to be started
4184 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4185 * @peer_addr: Address of the peer P2P Device
4186 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4187 * @persistent_group: Whether to create a persistent group
4188 * @auto_join: Whether to select join vs. GO Negotiation automatically
4189 * @join: Whether to join an existing group (as a client) instead of starting
4190 * Group Owner negotiation; @peer_addr is BSSID in that case
4191 * @auth: Whether to only authorize the connection instead of doing that and
4192 * initiating Group Owner negotiation
4193 * @go_intent: GO Intent or -1 to use default
4194 * @freq: Frequency for the group or 0 for auto-selection
4195 * @persistent_id: Persistent group credentials to use for forcing GO
4196 * parameters or -1 to generate new values (SSID/passphrase)
4197 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4198 * interoperability workaround when initiating group formation
4199 * @ht40: Start GO with 40 MHz channel width
4200 * @vht: Start GO with VHT support
4201 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4202 * failure, -2 on failure due to channel not currently available,
4203 * -3 if forced channel is not supported
4205 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4206 const char *pin, enum p2p_wps_method wps_method,
4207 int persistent_group, int auto_join, int join, int auth,
4208 int go_intent, int freq, int persistent_id, int pd,
4211 int force_freq = 0, pref_freq = 0;
4213 enum wpa_driver_if_type iftype;
4215 struct wpa_ssid *ssid = NULL;
4217 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4220 if (persistent_id >= 0) {
4221 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4222 if (ssid == NULL || ssid->disabled != 2 ||
4223 ssid->mode != WPAS_MODE_P2P_GO)
4227 os_free(wpa_s->global->add_psk);
4228 wpa_s->global->add_psk = NULL;
4231 go_intent = wpa_s->conf->p2p_go_intent;
4234 wpa_s->p2p_long_listen = 0;
4236 wpa_s->p2p_wps_method = wps_method;
4237 wpa_s->p2p_persistent_group = !!persistent_group;
4238 wpa_s->p2p_persistent_id = persistent_id;
4239 wpa_s->p2p_go_intent = go_intent;
4240 wpa_s->p2p_connect_freq = freq;
4241 wpa_s->p2p_fallback_to_go_neg = 0;
4242 wpa_s->p2p_pd_before_go_neg = !!pd;
4243 wpa_s->p2p_go_ht40 = !!ht40;
4244 wpa_s->p2p_go_vht = !!vht;
4247 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4248 else if (wps_method == WPS_PIN_DISPLAY) {
4249 ret = wps_generate_pin();
4250 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4252 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4255 wpa_s->p2p_pin[0] = '\0';
4257 if (join || auto_join) {
4258 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4260 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4261 "connect a running group from " MACSTR,
4262 MAC2STR(peer_addr));
4263 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4266 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4267 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4269 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4270 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4274 os_get_time(&wpa_s->p2p_auto_started);
4275 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4277 wpa_s->p2p_auto_started.sec,
4278 wpa_s->p2p_auto_started.usec);
4280 wpa_s->user_initiated_pd = 1;
4281 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4287 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4291 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
4293 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4295 if (wpa_s->create_p2p_iface) {
4296 /* Prepare to add a new interface for the group */
4297 iftype = WPA_IF_P2P_GROUP;
4298 if (go_intent == 15)
4299 iftype = WPA_IF_P2P_GO;
4300 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4301 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4302 "interface for the group");
4306 if_addr = wpa_s->pending_interface_addr;
4308 if_addr = wpa_s->own_addr;
4311 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
4313 force_freq, persistent_group, ssid,
4319 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4320 go_intent, if_addr, force_freq,
4321 persistent_group, ssid, pref_freq) < 0) {
4322 if (wpa_s->create_p2p_iface)
4323 wpas_p2p_remove_pending_group_interface(wpa_s);
4331 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4332 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4333 * @freq: Frequency of the channel in MHz
4334 * @duration: Duration of the stay on the channel in milliseconds
4336 * This callback is called when the driver indicates that it has started the
4337 * requested remain-on-channel duration.
4339 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4340 unsigned int freq, unsigned int duration)
4342 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4344 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4345 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4346 wpa_s->pending_listen_duration);
4347 wpa_s->pending_listen_freq = 0;
4352 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4353 unsigned int timeout)
4355 /* Limit maximum Listen state time based on driver limitation. */
4356 if (timeout > wpa_s->max_remain_on_chan)
4357 timeout = wpa_s->max_remain_on_chan;
4359 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4360 return wpa_drv_p2p_listen(wpa_s, timeout);
4362 return p2p_listen(wpa_s->global->p2p, timeout);
4367 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4368 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4369 * @freq: Frequency of the channel in MHz
4371 * This callback is called when the driver indicates that a remain-on-channel
4372 * operation has been completed, i.e., the duration on the requested channel
4375 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4378 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4379 "(p2p_long_listen=%d ms pending_action_tx=%p)",
4380 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
4381 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4383 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4384 return; /* P2P module started a new operation */
4385 if (offchannel_pending_action_tx(wpa_s))
4387 if (wpa_s->p2p_long_listen > 0)
4388 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4389 if (wpa_s->p2p_long_listen > 0) {
4390 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4391 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4394 * When listen duration is over, stop listen & update p2p_state
4397 p2p_stop_listen(wpa_s->global->p2p);
4403 * wpas_p2p_group_remove - Remove a P2P group
4404 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4405 * @ifname: Network interface name of the group interface or "*" to remove all
4407 * Returns: 0 on success, -1 on failure
4409 * This function is used to remove a P2P group. This can be used to disconnect
4410 * from a group in which the local end is a P2P Client or to end a P2P Group in
4411 * case the local end is the Group Owner. If a virtual network interface was
4412 * created for this group, that interface will be removed. Otherwise, only the
4413 * configured P2P group network will be removed from the interface.
4415 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4417 struct wpa_global *global = wpa_s->global;
4419 if (os_strcmp(ifname, "*") == 0) {
4420 struct wpa_supplicant *prev;
4421 wpa_s = global->ifaces;
4424 wpa_s = wpa_s->next;
4425 if (prev->p2p_group_interface !=
4426 NOT_P2P_GROUP_INTERFACE ||
4427 (prev->current_ssid &&
4428 prev->current_ssid->p2p_group))
4429 wpas_p2p_disconnect(prev);
4434 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4435 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4439 return wpas_p2p_disconnect(wpa_s);
4443 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4448 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4450 if (wpa_s->best_24_freq > 0 &&
4451 p2p_supported_freq_go(wpa_s->global->p2p,
4452 wpa_s->best_24_freq)) {
4453 freq = wpa_s->best_24_freq;
4454 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4455 "channel: %d MHz", freq);
4457 os_get_random((u8 *) &r, sizeof(r));
4458 freq = 2412 + (r % 3) * 25;
4459 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4460 "channel: %d MHz", freq);
4465 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4467 if (wpa_s->best_5_freq > 0 &&
4468 p2p_supported_freq_go(wpa_s->global->p2p,
4469 wpa_s->best_5_freq)) {
4470 freq = wpa_s->best_5_freq;
4471 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4472 "channel: %d MHz", freq);
4474 os_get_random((u8 *) &r, sizeof(r));
4475 freq = 5180 + (r % 4) * 20;
4476 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4477 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4478 "5 GHz channel for P2P group");
4481 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4482 "channel: %d MHz", freq);
4486 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4487 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4488 "(%u MHz) is not supported for P2P uses",
4497 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4498 struct p2p_go_neg_results *params,
4499 int freq, int ht40, int vht,
4500 const struct p2p_channels *channels)
4503 unsigned int pref_freq;
4504 unsigned int num, i;
4506 os_memset(params, 0, sizeof(*params));
4507 params->role_go = 1;
4508 params->ht40 = ht40;
4511 if (!freq_included(channels, freq)) {
4512 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4516 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4517 "frequency %d MHz", freq);
4518 params->freq = freq;
4519 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4520 wpa_s->conf->p2p_oper_channel >= 1 &&
4521 wpa_s->conf->p2p_oper_channel <= 11 &&
4522 freq_included(channels,
4523 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
4524 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4525 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4526 "frequency %d MHz", params->freq);
4527 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4528 wpa_s->conf->p2p_oper_reg_class == 116 ||
4529 wpa_s->conf->p2p_oper_reg_class == 117 ||
4530 wpa_s->conf->p2p_oper_reg_class == 124 ||
4531 wpa_s->conf->p2p_oper_reg_class == 126 ||
4532 wpa_s->conf->p2p_oper_reg_class == 127) &&
4533 freq_included(channels,
4534 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
4535 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4536 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4537 "frequency %d MHz", params->freq);
4538 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4539 wpa_s->best_overall_freq > 0 &&
4540 p2p_supported_freq_go(wpa_s->global->p2p,
4541 wpa_s->best_overall_freq) &&
4542 freq_included(channels, wpa_s->best_overall_freq)) {
4543 params->freq = wpa_s->best_overall_freq;
4544 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4545 "channel %d MHz", params->freq);
4546 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4547 wpa_s->best_24_freq > 0 &&
4548 p2p_supported_freq_go(wpa_s->global->p2p,
4549 wpa_s->best_24_freq) &&
4550 freq_included(channels, wpa_s->best_24_freq)) {
4551 params->freq = wpa_s->best_24_freq;
4552 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4553 "channel %d MHz", params->freq);
4554 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4555 wpa_s->best_5_freq > 0 &&
4556 p2p_supported_freq_go(wpa_s->global->p2p,
4557 wpa_s->best_5_freq) &&
4558 freq_included(channels, wpa_s->best_5_freq)) {
4559 params->freq = wpa_s->best_5_freq;
4560 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4561 "channel %d MHz", params->freq);
4562 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4564 params->freq = pref_freq;
4565 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4566 "channels", params->freq);
4569 for (chan = 0; chan < 11; chan++) {
4570 params->freq = 2412 + chan * 5;
4571 if (!wpas_p2p_disallowed_freq(wpa_s->global,
4573 freq_included(channels, params->freq))
4577 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4581 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4582 "known)", params->freq);
4585 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4589 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4590 wpa_s->num_multichan_concurrent);
4598 for (i = 0; i < num; i++) {
4599 if (freq_included(channels, freqs[i])) {
4600 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4602 params->freq = freqs[i];
4608 if (num == wpa_s->num_multichan_concurrent) {
4609 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4613 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4617 for (i = 0; i < num; i++) {
4618 if (freqs[i] == freq)
4623 if (num == wpa_s->num_multichan_concurrent) {
4624 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4628 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4637 static struct wpa_supplicant *
4638 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4641 struct wpa_supplicant *group_wpa_s;
4643 if (!wpas_p2p_create_iface(wpa_s)) {
4644 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4646 wpa_s->p2p_first_connection_timeout = 0;
4650 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
4651 WPA_IF_P2P_CLIENT) < 0) {
4652 wpa_msg_global(wpa_s, MSG_ERROR,
4653 "P2P: Failed to add group interface");
4656 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4657 if (group_wpa_s == NULL) {
4658 wpa_msg_global(wpa_s, MSG_ERROR,
4659 "P2P: Failed to initialize group interface");
4660 wpas_p2p_remove_pending_group_interface(wpa_s);
4664 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4665 group_wpa_s->ifname);
4666 group_wpa_s->p2p_first_connection_timeout = 0;
4672 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4673 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4674 * @persistent_group: Whether to create a persistent group
4675 * @freq: Frequency for the group or 0 to indicate no hardcoding
4676 * @ht40: Start GO with 40 MHz channel width
4677 * @vht: Start GO with VHT support
4678 * Returns: 0 on success, -1 on failure
4680 * This function creates a new P2P group with the local end as the Group Owner,
4681 * i.e., without using Group Owner Negotiation.
4683 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
4684 int freq, int ht40, int vht)
4686 struct p2p_go_neg_results params;
4688 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4691 os_free(wpa_s->global->add_psk);
4692 wpa_s->global->add_psk = NULL;
4694 /* Make sure we are not running find during connection establishment */
4695 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
4696 wpas_p2p_stop_find_oper(wpa_s);
4698 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4702 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
4705 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
4706 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4707 "(%u MHz) is not supported for P2P uses",
4711 p2p_go_params(wpa_s->global->p2p, ¶ms);
4712 params.persistent_group = persistent_group;
4714 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4717 wpas_start_wps_go(wpa_s, ¶ms, 0);
4723 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4724 struct wpa_ssid *params, int addr_allocated)
4726 struct wpa_ssid *ssid;
4728 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4731 wpa_s->p2p_last_4way_hs_fail = NULL;
4733 wpa_supplicant_ap_deinit(wpa_s);
4735 ssid = wpa_config_add_network(wpa_s->conf);
4738 wpa_config_set_network_defaults(ssid);
4739 ssid->temporary = 1;
4740 ssid->proto = WPA_PROTO_RSN;
4741 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4742 ssid->group_cipher = WPA_CIPHER_CCMP;
4743 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4744 ssid->ssid = os_malloc(params->ssid_len);
4745 if (ssid->ssid == NULL) {
4746 wpa_config_remove_network(wpa_s->conf, ssid->id);
4749 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4750 ssid->ssid_len = params->ssid_len;
4751 ssid->p2p_group = 1;
4752 ssid->export_keys = 1;
4753 if (params->psk_set) {
4754 os_memcpy(ssid->psk, params->psk, 32);
4757 if (params->passphrase)
4758 ssid->passphrase = os_strdup(params->passphrase);
4760 wpa_supplicant_select_network(wpa_s, ssid);
4762 wpa_s->show_group_started = 1;
4768 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4769 struct wpa_ssid *ssid, int addr_allocated,
4770 int freq, int ht40, int vht,
4771 const struct p2p_channels *channels,
4772 int connection_timeout)
4774 struct p2p_go_neg_results params;
4777 if (ssid->disabled != 2 || ssid->ssid == NULL)
4780 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4781 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4782 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4787 os_free(wpa_s->global->add_psk);
4788 wpa_s->global->add_psk = NULL;
4790 /* Make sure we are not running find during connection establishment */
4791 wpas_p2p_stop_find_oper(wpa_s);
4793 wpa_s->p2p_fallback_to_go_neg = 0;
4795 if (ssid->mode == WPAS_MODE_INFRA)
4796 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4798 if (ssid->mode != WPAS_MODE_P2P_GO)
4801 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4805 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
4809 params.psk_set = ssid->psk_set;
4811 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
4812 if (ssid->passphrase) {
4813 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4814 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4815 "persistent group");
4818 os_strlcpy(params.passphrase, ssid->passphrase,
4819 sizeof(params.passphrase));
4821 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4822 params.ssid_len = ssid->ssid_len;
4823 params.persistent_group = 1;
4825 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4829 wpa_s->p2p_first_connection_timeout = connection_timeout;
4830 wpas_start_wps_go(wpa_s, ¶ms, 0);
4836 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4837 struct wpabuf *proberesp_ies)
4839 struct wpa_supplicant *wpa_s = ctx;
4840 if (wpa_s->ap_iface) {
4841 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
4842 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4843 wpabuf_free(beacon_ies);
4844 wpabuf_free(proberesp_ies);
4848 wpabuf_free(hapd->p2p_beacon_ie);
4849 hapd->p2p_beacon_ie = beacon_ies;
4851 wpabuf_free(hapd->p2p_probe_resp_ie);
4852 hapd->p2p_probe_resp_ie = proberesp_ies;
4854 wpabuf_free(beacon_ies);
4855 wpabuf_free(proberesp_ies);
4857 wpa_supplicant_ap_update_beacon(wpa_s);
4861 static void wpas_p2p_idle_update(void *ctx, int idle)
4863 struct wpa_supplicant *wpa_s = ctx;
4864 if (!wpa_s->ap_iface)
4866 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4868 wpas_p2p_set_group_idle_timeout(wpa_s);
4870 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4874 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
4875 struct wpa_ssid *ssid)
4877 struct p2p_group *group;
4878 struct p2p_group_config *cfg;
4880 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4882 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4885 cfg = os_zalloc(sizeof(*cfg));
4889 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4890 cfg->persistent_group = 2;
4891 else if (ssid->p2p_persistent_group)
4892 cfg->persistent_group = 1;
4893 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4894 if (wpa_s->max_stations &&
4895 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4896 cfg->max_clients = wpa_s->max_stations;
4898 cfg->max_clients = wpa_s->conf->max_num_sta;
4899 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4900 cfg->ssid_len = ssid->ssid_len;
4901 cfg->cb_ctx = wpa_s;
4902 cfg->ie_update = wpas_p2p_ie_update;
4903 cfg->idle_update = wpas_p2p_idle_update;
4905 group = p2p_group_init(wpa_s->global->p2p, cfg);
4908 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4909 p2p_group_notif_formation_done(group);
4910 wpa_s->p2p_group = group;
4915 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4918 struct wpa_ssid *ssid = wpa_s->current_ssid;
4920 if (!wpa_s->p2p_in_provisioning) {
4921 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4922 "provisioning not in progress");
4926 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4927 u8 go_dev_addr[ETH_ALEN];
4928 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4929 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4931 /* Clear any stored provisioning info */
4932 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4935 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4937 wpa_s->p2p_go_group_formation_completed = 1;
4938 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4940 * Use a separate timeout for initial data connection to
4941 * complete to allow the group to be removed automatically if
4942 * something goes wrong in this step before the P2P group idle
4943 * timeout mechanism is taken into use.
4945 wpa_dbg(wpa_s, MSG_DEBUG,
4946 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
4947 P2P_MAX_INITIAL_CONN_WAIT);
4948 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4949 wpas_p2p_group_formation_timeout,
4950 wpa_s->parent, NULL);
4953 * Use a separate timeout for initial data connection to
4954 * complete to allow the group to be removed automatically if
4955 * the client does not complete data connection successfully.
4957 wpa_dbg(wpa_s, MSG_DEBUG,
4958 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
4959 P2P_MAX_INITIAL_CONN_WAIT_GO);
4960 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
4961 wpas_p2p_group_formation_timeout,
4962 wpa_s->parent, NULL);
4964 * Complete group formation on first successful data connection
4966 wpa_s->p2p_go_group_formation_completed = 0;
4968 if (wpa_s->global->p2p)
4969 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4970 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4971 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4972 wpas_group_formation_completed(wpa_s, 1);
4976 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4977 struct wps_event_fail *fail)
4979 if (!wpa_s->p2p_in_provisioning) {
4980 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4981 "provisioning not in progress");
4985 if (wpa_s->go_params) {
4986 p2p_clear_provisioning_info(
4988 wpa_s->go_params->peer_device_addr);
4991 wpas_notify_p2p_wps_failed(wpa_s, fail);
4995 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4996 const char *config_method,
4997 enum wpas_p2p_prov_disc_use use)
5001 wpa_s->p2p_fallback_to_go_neg = 0;
5002 wpa_s->pending_pd_use = NORMAL_PD;
5003 if (os_strncmp(config_method, "display", 7) == 0)
5004 config_methods = WPS_CONFIG_DISPLAY;
5005 else if (os_strncmp(config_method, "keypad", 6) == 0)
5006 config_methods = WPS_CONFIG_KEYPAD;
5007 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5008 os_strncmp(config_method, "pushbutton", 10) == 0)
5009 config_methods = WPS_CONFIG_PUSHBUTTON;
5011 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5015 if (use == WPAS_P2P_PD_AUTO) {
5016 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5017 wpa_s->pending_pd_config_methods = config_methods;
5018 wpa_s->p2p_auto_pd = 1;
5019 wpa_s->p2p_auto_join = 0;
5020 wpa_s->pending_pd_before_join = 0;
5021 wpa_s->auto_pd_scan_retry = 0;
5022 wpas_p2p_stop_find(wpa_s);
5023 wpa_s->p2p_join_scan_count = 0;
5024 os_get_time(&wpa_s->p2p_auto_started);
5025 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5026 wpa_s->p2p_auto_started.sec,
5027 wpa_s->p2p_auto_started.usec);
5028 wpas_p2p_join_scan(wpa_s, NULL);
5032 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5033 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
5035 use == WPAS_P2P_PD_FOR_JOIN);
5038 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5041 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
5042 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5047 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5050 return p2p_scan_result_text(ies, ies_len, buf, end);
5054 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5056 if (!offchannel_pending_action_tx(wpa_s))
5059 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5060 "operation request");
5061 offchannel_clear_pending_action_tx(wpa_s);
5065 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5066 enum p2p_discovery_type type,
5067 unsigned int num_req_dev_types, const u8 *req_dev_types,
5068 const u8 *dev_id, unsigned int search_delay)
5070 wpas_p2p_clear_pending_action_tx(wpa_s);
5071 wpa_s->p2p_long_listen = 0;
5073 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5074 return wpa_drv_p2p_find(wpa_s, timeout, type);
5076 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5077 wpa_s->p2p_in_provisioning)
5080 wpa_supplicant_cancel_sched_scan(wpa_s);
5082 return p2p_find(wpa_s->global->p2p, timeout, type,
5083 num_req_dev_types, req_dev_types, dev_id,
5088 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
5090 wpas_p2p_clear_pending_action_tx(wpa_s);
5091 wpa_s->p2p_long_listen = 0;
5092 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5093 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5094 wpa_s->global->p2p_cb_on_scan_complete = 0;
5096 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5097 wpa_drv_p2p_stop_find(wpa_s);
5101 if (wpa_s->global->p2p)
5102 p2p_stop_find(wpa_s->global->p2p);
5108 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5110 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5112 wpas_p2p_remove_pending_group_interface(wpa_s);
5116 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5118 struct wpa_supplicant *wpa_s = eloop_ctx;
5119 wpa_s->p2p_long_listen = 0;
5123 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5127 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5130 wpa_supplicant_cancel_sched_scan(wpa_s);
5131 wpas_p2p_clear_pending_action_tx(wpa_s);
5135 * This is a request for unlimited Listen state. However, at
5136 * least for now, this is mapped to a Listen state for one
5141 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5142 wpa_s->p2p_long_listen = 0;
5145 * Stop previous find/listen operation to avoid trying to request a new
5146 * remain-on-channel operation while the driver is still running the
5149 if (wpa_s->global->p2p)
5150 p2p_stop_find(wpa_s->global->p2p);
5152 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5153 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5154 wpa_s->p2p_long_listen = timeout * 1000;
5155 eloop_register_timeout(timeout, 0,
5156 wpas_p2p_long_listen_timeout,
5164 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5165 u8 *buf, size_t len, int p2p_group)
5167 struct wpabuf *p2p_ie;
5170 if (wpa_s->global->p2p_disabled)
5172 if (wpa_s->global->p2p == NULL)
5177 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5178 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5180 wpabuf_free(p2p_ie);
5186 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
5187 const u8 *dst, const u8 *bssid,
5188 const u8 *ie, size_t ie_len, int ssi_signal)
5190 if (wpa_s->global->p2p_disabled)
5192 if (wpa_s->global->p2p == NULL)
5195 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5197 case P2P_PREQ_NOT_P2P:
5198 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5201 case P2P_PREQ_MALFORMED:
5202 case P2P_PREQ_NOT_LISTEN:
5203 case P2P_PREQ_NOT_PROCESSED:
5204 default: /* make gcc happy */
5206 case P2P_PREQ_PROCESSED:
5212 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5213 const u8 *sa, const u8 *bssid,
5214 u8 category, const u8 *data, size_t len, int freq)
5216 if (wpa_s->global->p2p_disabled)
5218 if (wpa_s->global->p2p == NULL)
5221 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5226 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5228 if (wpa_s->global->p2p_disabled)
5230 if (wpa_s->global->p2p == NULL)
5233 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5237 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5239 p2p_group_deinit(wpa_s->p2p_group);
5240 wpa_s->p2p_group = NULL;
5242 wpa_s->ap_configured_cb = NULL;
5243 wpa_s->ap_configured_cb_ctx = NULL;
5244 wpa_s->ap_configured_cb_data = NULL;
5245 wpa_s->connect_without_scan = NULL;
5249 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5251 wpa_s->p2p_long_listen = 0;
5253 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5254 return wpa_drv_p2p_reject(wpa_s, addr);
5256 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5259 return p2p_reject(wpa_s->global->p2p, addr);
5263 /* Invite to reinvoke a persistent group */
5264 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5265 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
5266 int ht40, int vht, int pref_freq)
5268 enum p2p_invite_role role;
5273 wpa_s->global->p2p_invite_group = NULL;
5275 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5277 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5279 wpa_s->p2p_persistent_go_freq = freq;
5280 wpa_s->p2p_go_ht40 = !!ht40;
5281 if (ssid->mode == WPAS_MODE_P2P_GO) {
5282 role = P2P_INVITE_ROLE_GO;
5283 if (peer_addr == NULL) {
5284 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5285 "address in invitation command");
5288 if (wpas_p2p_create_iface(wpa_s)) {
5289 if (wpas_p2p_add_group_interface(wpa_s,
5290 WPA_IF_P2P_GO) < 0) {
5291 wpa_printf(MSG_ERROR, "P2P: Failed to "
5292 "allocate a new interface for the "
5296 bssid = wpa_s->pending_interface_addr;
5298 bssid = wpa_s->own_addr;
5300 role = P2P_INVITE_ROLE_CLIENT;
5301 peer_addr = ssid->bssid;
5303 wpa_s->pending_invite_ssid_id = ssid->id;
5305 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5306 role == P2P_INVITE_ROLE_GO);
5310 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5311 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5312 ssid->ssid, ssid->ssid_len,
5315 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5318 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5319 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5324 /* Invite to join an active group */
5325 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5326 const u8 *peer_addr, const u8 *go_dev_addr)
5328 struct wpa_global *global = wpa_s->global;
5329 enum p2p_invite_role role;
5331 struct wpa_ssid *ssid;
5333 int freq = 0, force_freq = 0, pref_freq = 0;
5336 wpa_s->p2p_persistent_go_freq = 0;
5337 wpa_s->p2p_go_ht40 = 0;
5338 wpa_s->p2p_go_vht = 0;
5340 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5341 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5344 if (wpa_s == NULL) {
5345 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5349 ssid = wpa_s->current_ssid;
5351 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5356 wpa_s->global->p2p_invite_group = wpa_s;
5357 persistent = ssid->p2p_persistent_group &&
5358 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5359 ssid->ssid, ssid->ssid_len);
5361 if (ssid->mode == WPAS_MODE_P2P_GO) {
5362 role = P2P_INVITE_ROLE_ACTIVE_GO;
5363 bssid = wpa_s->own_addr;
5364 if (go_dev_addr == NULL)
5365 go_dev_addr = wpa_s->global->p2p_dev_addr;
5366 freq = ssid->frequency;
5368 role = P2P_INVITE_ROLE_CLIENT;
5369 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5370 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5371 "invite to current group");
5374 bssid = wpa_s->bssid;
5375 if (go_dev_addr == NULL &&
5376 !is_zero_ether_addr(wpa_s->go_dev_addr))
5377 go_dev_addr = wpa_s->go_dev_addr;
5378 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5379 (int) wpa_s->assoc_freq;
5381 wpa_s->parent->pending_invite_ssid_id = -1;
5383 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5384 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5385 ssid->ssid, ssid->ssid_len,
5386 go_dev_addr, persistent);
5388 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5391 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5392 role == P2P_INVITE_ROLE_ACTIVE_GO);
5395 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
5397 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5398 ssid->ssid, ssid->ssid_len, force_freq,
5399 go_dev_addr, persistent, pref_freq);
5403 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5405 struct wpa_ssid *ssid = wpa_s->current_ssid;
5406 const char *ssid_txt;
5407 u8 go_dev_addr[ETH_ALEN];
5408 int network_id = -1;
5412 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5413 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5414 wpa_s->parent, NULL);
5417 if (!wpa_s->show_group_started || !ssid)
5420 wpa_s->show_group_started = 0;
5422 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5423 os_memset(go_dev_addr, 0, ETH_ALEN);
5424 if (ssid->bssid_set)
5425 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5426 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5428 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5430 if (wpa_s->global->p2p_group_formation == wpa_s)
5431 wpa_s->global->p2p_group_formation = NULL;
5433 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5434 (int) wpa_s->assoc_freq;
5435 if (ssid->passphrase == NULL && ssid->psk_set) {
5437 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5438 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5439 "%s client ssid=\"%s\" freq=%d psk=%s "
5440 "go_dev_addr=" MACSTR "%s",
5441 wpa_s->ifname, ssid_txt, freq, psk,
5442 MAC2STR(go_dev_addr),
5443 persistent ? " [PERSISTENT]" : "");
5445 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5446 "%s client ssid=\"%s\" freq=%d "
5447 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5448 wpa_s->ifname, ssid_txt, freq,
5449 ssid->passphrase ? ssid->passphrase : "",
5450 MAC2STR(go_dev_addr),
5451 persistent ? " [PERSISTENT]" : "");
5455 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5458 network_id = ssid->id;
5459 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
5462 wpas_p2p_continue_after_scan(wpa_s);
5466 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5467 u32 interval1, u32 duration2, u32 interval2)
5469 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5471 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5474 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5475 wpa_s->current_ssid == NULL ||
5476 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5479 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5480 wpa_s->own_addr, wpa_s->assoc_freq,
5481 duration1, interval1, duration2, interval2);
5485 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5486 unsigned int interval)
5488 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5491 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5494 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5498 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5500 if (wpa_s->current_ssid == NULL) {
5502 * current_ssid can be cleared when P2P client interface gets
5503 * disconnected, so assume this interface was used as P2P
5508 return wpa_s->current_ssid->p2p_group &&
5509 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5513 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5515 struct wpa_supplicant *wpa_s = eloop_ctx;
5517 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
5518 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5523 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5525 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
5529 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5533 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5534 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5536 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5539 timeout = wpa_s->conf->p2p_group_idle;
5540 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5541 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5542 timeout = P2P_MAX_CLIENT_IDLE;
5548 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5549 timeout = 0; /* special client mode no-timeout */
5554 if (wpa_s->p2p_in_provisioning) {
5556 * Use the normal group formation timeout during the
5557 * provisioning phase to avoid terminating this process too
5558 * early due to group idle timeout.
5560 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5561 "during provisioning");
5565 if (wpa_s->show_group_started) {
5567 * Use the normal group formation timeout between the end of
5568 * the provisioning phase and completion of 4-way handshake to
5569 * avoid terminating this process too early due to group idle
5572 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5573 "while waiting for initial 4-way handshake to "
5578 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
5580 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5585 /* Returns 1 if the interface was removed */
5586 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5587 u16 reason_code, const u8 *ie, size_t ie_len,
5588 int locally_generated)
5590 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5592 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5595 if (!locally_generated)
5596 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5599 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5600 wpa_s->current_ssid &&
5601 wpa_s->current_ssid->p2p_group &&
5602 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5603 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5604 "session is ending");
5605 if (wpas_p2p_group_delete(wpa_s,
5606 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5615 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5616 u16 reason_code, const u8 *ie, size_t ie_len,
5617 int locally_generated)
5619 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5621 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5624 if (!locally_generated)
5625 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5630 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5632 struct p2p_data *p2p = wpa_s->global->p2p;
5637 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5640 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5641 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5643 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5644 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5647 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5648 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5650 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5651 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5653 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5654 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5655 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5656 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5657 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5660 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5661 p2p_set_sec_dev_types(p2p,
5662 (void *) wpa_s->conf->sec_device_type,
5663 wpa_s->conf->num_sec_device_types);
5665 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5667 p2p_remove_wps_vendor_extensions(p2p);
5668 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5669 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5671 p2p_add_wps_vendor_extension(
5672 p2p, wpa_s->conf->wps_vendor_ext[i]);
5676 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5677 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5679 country[0] = wpa_s->conf->country[0];
5680 country[1] = wpa_s->conf->country[1];
5682 p2p_set_country(p2p, country);
5685 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5686 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5687 wpa_s->conf->p2p_ssid_postfix ?
5688 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5692 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5693 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
5695 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5696 u8 reg_class, channel;
5699 if (wpa_s->conf->p2p_listen_reg_class &&
5700 wpa_s->conf->p2p_listen_channel) {
5701 reg_class = wpa_s->conf->p2p_listen_reg_class;
5702 channel = wpa_s->conf->p2p_listen_channel;
5706 * Pick one of the social channels randomly as the
5709 os_get_random((u8 *) &r, sizeof(r));
5710 channel = 1 + (r % 3) * 5;
5712 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5714 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5717 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5718 u8 op_reg_class, op_channel, cfg_op_channel;
5721 if (wpa_s->conf->p2p_oper_reg_class &&
5722 wpa_s->conf->p2p_oper_channel) {
5723 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5724 op_channel = wpa_s->conf->p2p_oper_channel;
5729 * Use random operation channel from (1, 6, 11)
5730 *if no other preference is indicated.
5732 os_get_random((u8 *) &r, sizeof(r));
5733 op_channel = 1 + (r % 3) * 5;
5736 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5739 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5743 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5744 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5745 wpa_s->conf->p2p_pref_chan) < 0) {
5746 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5750 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
5751 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
5758 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5761 if (!wpa_s->ap_iface)
5763 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5768 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5770 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5772 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5775 wpa_s->global->cross_connection = enabled;
5776 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5779 struct wpa_supplicant *iface;
5781 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5783 if (iface->cross_connect_enabled == 0)
5786 iface->cross_connect_enabled = 0;
5787 iface->cross_connect_in_use = 0;
5788 wpa_msg_global(iface->parent, MSG_INFO,
5789 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5791 iface->cross_connect_uplink);
5799 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5801 struct wpa_supplicant *iface;
5803 if (!uplink->global->cross_connection)
5806 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5807 if (!iface->cross_connect_enabled)
5809 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5812 if (iface->ap_iface == NULL)
5814 if (iface->cross_connect_in_use)
5817 iface->cross_connect_in_use = 1;
5818 wpa_msg_global(iface->parent, MSG_INFO,
5819 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5820 iface->ifname, iface->cross_connect_uplink);
5825 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5827 struct wpa_supplicant *iface;
5829 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5830 if (!iface->cross_connect_enabled)
5832 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5835 if (!iface->cross_connect_in_use)
5838 wpa_msg_global(iface->parent, MSG_INFO,
5839 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5840 iface->ifname, iface->cross_connect_uplink);
5841 iface->cross_connect_in_use = 0;
5846 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5848 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5849 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5850 wpa_s->cross_connect_disallowed)
5851 wpas_p2p_disable_cross_connect(wpa_s);
5853 wpas_p2p_enable_cross_connect(wpa_s);
5854 if (!wpa_s->ap_iface &&
5855 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5856 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5860 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5862 wpas_p2p_disable_cross_connect(wpa_s);
5863 if (!wpa_s->ap_iface &&
5864 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5866 wpas_p2p_set_group_idle_timeout(wpa_s);
5870 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5872 struct wpa_supplicant *iface;
5874 if (!wpa_s->global->cross_connection)
5877 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5880 if (iface->drv_flags &
5881 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5883 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5886 wpa_s->cross_connect_enabled = 1;
5887 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5888 sizeof(wpa_s->cross_connect_uplink));
5889 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5890 "%s to %s whenever uplink is available",
5891 wpa_s->ifname, wpa_s->cross_connect_uplink);
5893 if (iface->ap_iface || iface->current_ssid == NULL ||
5894 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5895 iface->cross_connect_disallowed ||
5896 iface->wpa_state != WPA_COMPLETED)
5899 wpa_s->cross_connect_in_use = 1;
5900 wpa_msg_global(wpa_s->parent, MSG_INFO,
5901 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5902 wpa_s->ifname, wpa_s->cross_connect_uplink);
5908 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5910 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5911 !wpa_s->p2p_in_provisioning)
5912 return 0; /* not P2P client operation */
5914 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5916 if (wpa_s != wpa_s->parent)
5917 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5918 wpas_p2p_group_formation_failed(wpa_s);
5923 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5925 struct p2p_channels chan, cli_chan;
5927 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5930 os_memset(&chan, 0, sizeof(chan));
5931 os_memset(&cli_chan, 0, sizeof(cli_chan));
5932 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
5933 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5938 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
5942 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5943 struct wpa_scan_results *scan_res)
5945 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5949 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5951 struct wpa_global *global = wpa_s->global;
5955 if (global->p2p == NULL)
5958 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5960 if (wpa_s->pending_interface_name[0] &&
5961 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5964 peer = p2p_get_go_neg_peer(global->p2p);
5966 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5967 MACSTR, MAC2STR(peer));
5968 p2p_unauthorize(global->p2p, peer);
5972 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5973 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5974 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5978 if (wpa_s->pending_pd_before_join) {
5979 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5980 wpa_s->pending_pd_before_join = 0;
5984 wpas_p2p_stop_find(wpa_s);
5986 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5987 if (wpa_s == global->p2p_group_formation &&
5988 (wpa_s->p2p_in_provisioning ||
5989 wpa_s->parent->pending_interface_type ==
5990 WPA_IF_P2P_CLIENT)) {
5991 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5992 "formation found - cancelling",
5995 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5996 wpa_s->parent, NULL);
5997 if (wpa_s->p2p_in_provisioning) {
5998 wpas_group_formation_completed(wpa_s, 0);
6001 wpas_p2p_group_delete(wpa_s,
6002 P2P_GROUP_REMOVAL_REQUESTED);
6008 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6016 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6018 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6021 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6022 "being available anymore");
6023 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
6027 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6028 int freq_24, int freq_5, int freq_overall)
6030 struct p2p_data *p2p = wpa_s->global->p2p;
6031 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6033 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6037 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6040 struct p2p_data *p2p = wpa_s->global->p2p;
6042 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6045 if (hwaddr_aton(addr, peer))
6048 return p2p_unauthorize(p2p, peer);
6053 * wpas_p2p_disconnect - Disconnect from a P2P Group
6054 * @wpa_s: Pointer to wpa_supplicant data
6055 * Returns: 0 on success, -1 on failure
6057 * This can be used to disconnect from a group in which the local end is a P2P
6058 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6059 * virtual network interface was created for this group, that interface will be
6060 * removed. Otherwise, only the configured P2P group network will be removed
6061 * from the interface.
6063 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6069 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6074 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6078 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6081 ret = p2p_in_progress(wpa_s->global->p2p);
6084 * Check whether there is an ongoing WPS provisioning step (or
6085 * other parts of group formation) on another interface since
6086 * p2p_in_progress() does not report this to avoid issues for
6087 * scans during such provisioning step.
6089 if (wpa_s->global->p2p_group_formation &&
6090 wpa_s->global->p2p_group_formation != wpa_s) {
6091 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6092 "in group formation",
6093 wpa_s->global->p2p_group_formation->ifname);
6098 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6101 if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
6102 P2P_MAX_INITIAL_CONN_WAIT_GO) {
6103 /* Wait for the first client has expired */
6104 wpa_s->global->p2p_go_wait_client.sec = 0;
6106 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6115 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6116 struct wpa_ssid *ssid)
6118 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6119 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6120 wpa_s->parent, NULL) > 0) {
6122 * Remove the network by scheduling the group formation
6123 * timeout to happen immediately. The teardown code
6124 * needs to be scheduled to run asynch later so that we
6125 * don't delete data from under ourselves unexpectedly.
6126 * Calling wpas_p2p_group_formation_timeout directly
6127 * causes a series of crashes in WPS failure scenarios.
6129 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6130 "P2P group network getting removed");
6131 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6132 wpa_s->parent, NULL);
6137 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
6138 const u8 *addr, const u8 *ssid,
6144 for (s = wpa_s->conf->ssid; s; s = s->next) {
6145 if (s->disabled != 2)
6148 (ssid_len != s->ssid_len ||
6149 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6152 if (s->mode == WPAS_MODE_P2P_GO)
6156 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6157 return s; /* peer is GO in the persistent group */
6158 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6160 for (i = 0; i < s->num_p2p_clients; i++) {
6161 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6162 addr, ETH_ALEN) == 0)
6163 return s; /* peer is P2P client in persistent
6172 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6175 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6176 wpa_s->parent, NULL) > 0) {
6178 * This can happen if WPS provisioning step is not terminated
6179 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6180 * peer was able to connect, there is no need to time out group
6181 * formation after this, though. In addition, this is used with
6182 * the initial connection wait on the GO as a separate formation
6183 * timeout and as such, expected to be hit after the initial WPS
6184 * provisioning step.
6186 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6188 if (!wpa_s->p2p_go_group_formation_completed) {
6189 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6190 wpa_s->p2p_go_group_formation_completed = 1;
6191 wpa_s->global->p2p_group_formation = NULL;
6192 wpa_s->p2p_in_provisioning = 0;
6194 wpa_s->global->p2p_go_wait_client.sec = 0;
6197 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6201 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6204 struct wpa_supplicant *group = wpa_s;
6205 if (wpa_s->global->p2p_group_formation)
6206 group = wpa_s->global->p2p_group_formation;
6207 wpa_s = wpa_s->parent;
6208 offchannel_send_action_done(wpa_s);
6210 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
6211 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6212 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6213 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6214 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6215 wpa_s->p2p_persistent_id,
6216 wpa_s->p2p_pd_before_go_neg,
6222 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6224 if (!wpa_s->p2p_fallback_to_go_neg ||
6225 wpa_s->p2p_in_provisioning <= 5)
6228 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6229 return 0; /* peer operating as a GO */
6231 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6232 "fallback to GO Negotiation");
6233 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6239 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6241 const char *rn, *rn2;
6242 struct wpa_supplicant *ifs;
6244 if (wpa_s->wpa_state > WPA_SCANNING) {
6245 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6246 "concurrent operation",
6247 P2P_CONCURRENT_SEARCH_DELAY);
6248 return P2P_CONCURRENT_SEARCH_DELAY;
6251 if (!wpa_s->driver->get_radio_name)
6253 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
6254 if (rn == NULL || rn[0] == '\0')
6257 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6258 if (ifs == wpa_s || !ifs->driver->get_radio_name)
6261 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
6262 if (!rn2 || os_strcmp(rn, rn2) != 0)
6264 if (ifs->wpa_state > WPA_SCANNING) {
6265 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6266 "delay due to concurrent operation on "
6268 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6269 return P2P_CONCURRENT_SEARCH_DELAY;
6277 void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
6279 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
6280 "pending anymore (sta_scan_pending=%d "
6281 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
6282 wpa_s->global->p2p_cb_on_scan_complete);
6283 wpa_s->sta_scan_pending = 0;
6285 if (!wpa_s->global->p2p_cb_on_scan_complete)
6287 wpa_s->global->p2p_cb_on_scan_complete = 0;
6289 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6292 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6293 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6294 "continued after successful connection");
6295 p2p_increase_search_delay(wpa_s->global->p2p,
6296 wpas_p2p_search_delay(wpa_s));
6301 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6302 struct wpa_ssid *s, const u8 *addr,
6305 struct psk_list_entry *psk, *tmp;
6308 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6310 if ((iface_addr && !psk->p2p &&
6311 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6312 (!iface_addr && psk->p2p &&
6313 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6314 wpa_dbg(wpa_s, MSG_DEBUG,
6315 "P2P: Remove persistent group PSK list entry for "
6317 MAC2STR(psk->addr), psk->p2p);
6318 dl_list_del(&psk->list);
6328 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6329 const u8 *p2p_dev_addr,
6330 const u8 *psk, size_t psk_len)
6332 struct wpa_ssid *ssid = wpa_s->current_ssid;
6333 struct wpa_ssid *persistent;
6334 struct psk_list_entry *p;
6336 if (psk_len != sizeof(p->psk))
6340 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6341 " p2p_dev_addr=" MACSTR,
6342 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6343 if (is_zero_ether_addr(p2p_dev_addr))
6344 p2p_dev_addr = NULL;
6346 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6350 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6351 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6352 /* To be added to persistent group once created */
6353 if (wpa_s->global->add_psk == NULL) {
6354 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6355 if (wpa_s->global->add_psk == NULL)
6358 p = wpa_s->global->add_psk;
6361 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6364 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6366 os_memcpy(p->psk, psk, psk_len);
6370 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6371 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6375 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6378 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6382 p = os_zalloc(sizeof(*p));
6387 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6390 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6392 os_memcpy(p->psk, psk, psk_len);
6394 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6395 struct psk_list_entry *last;
6396 last = dl_list_last(&persistent->psk_list,
6397 struct psk_list_entry, list);
6398 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6399 MACSTR " (p2p=%u) to make room for a new one",
6400 MAC2STR(last->addr), last->p2p);
6401 dl_list_del(&last->list);
6405 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6406 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6407 p2p_dev_addr == NULL);
6409 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6410 MACSTR, MAC2STR(p2p_dev_addr));
6412 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6415 dl_list_add(&persistent->psk_list, &p->list);
6417 #ifndef CONFIG_NO_CONFIG_WRITE
6418 if (wpa_s->parent->conf->update_config &&
6419 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6420 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6421 #endif /* CONFIG_NO_CONFIG_WRITE */
6425 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6426 struct wpa_ssid *s, const u8 *addr,
6431 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6433 #ifndef CONFIG_NO_CONFIG_WRITE
6434 if (wpa_s->conf->update_config &&
6435 wpa_config_write(wpa_s->confname, wpa_s->conf))
6436 wpa_dbg(wpa_s, MSG_DEBUG,
6437 "P2P: Failed to update configuration");
6438 #endif /* CONFIG_NO_CONFIG_WRITE */
6443 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6444 const u8 *peer, int iface_addr)
6446 struct hostapd_data *hapd;
6447 struct hostapd_wpa_psk *psk, *prev, *rem;
6448 struct sta_info *sta;
6450 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6451 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6454 /* Remove per-station PSK entry */
6455 hapd = wpa_s->ap_iface->bss[0];
6457 psk = hapd->conf->ssid.wpa_psk;
6459 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6461 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6462 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6463 MACSTR " iface_addr=%d",
6464 MAC2STR(peer), iface_addr);
6466 prev->next = psk->next;
6468 hapd->conf->ssid.wpa_psk = psk->next;
6478 /* Disconnect from group */
6480 sta = ap_get_sta(hapd, peer);
6482 sta = ap_get_sta_p2p(hapd, peer);
6484 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6485 " (iface_addr=%d) from group",
6486 MAC2STR(peer), iface_addr);
6487 hostapd_drv_sta_deauth(hapd, sta->addr,
6488 WLAN_REASON_DEAUTH_LEAVING);
6489 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6494 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6498 struct wpa_supplicant *w;
6500 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6502 /* Remove from any persistent group */
6503 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6504 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6507 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6508 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6511 /* Remove from any operating group */
6512 for (w = wpa_s->global->ifaces; w; w = w->next)
6513 wpas_p2p_remove_client_go(w, peer, iface_addr);
6517 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6519 struct wpa_supplicant *wpa_s = eloop_ctx;
6520 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6524 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6526 struct wpa_ssid *ssid = wpa_s->current_ssid;
6528 if (ssid == NULL || !ssid->p2p_group)
6531 if (wpa_s->p2p_last_4way_hs_fail &&
6532 wpa_s->p2p_last_4way_hs_fail == ssid) {
6533 u8 go_dev_addr[ETH_ALEN];
6534 struct wpa_ssid *persistent;
6536 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6538 ssid->ssid_len) <= 0) {
6539 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6543 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6544 MACSTR, MAC2STR(go_dev_addr));
6545 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6548 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6549 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6552 wpa_msg_global(wpa_s->parent, MSG_INFO,
6553 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6556 wpa_s->p2p_last_4way_hs_fail = NULL;
6558 * Remove the group from a timeout to avoid issues with caller
6559 * continuing to use the interface if this is on a P2P group
6562 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6567 wpa_s->p2p_last_4way_hs_fail = ssid;