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_printf(MSG_DEBUG, "P2P: Remove temporary group network");
473 if (ssid && (ssid->p2p_group ||
474 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
475 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
477 if (ssid == wpa_s->current_ssid) {
478 wpa_sm_set_config(wpa_s->wpa, NULL);
479 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
480 wpa_s->current_ssid = NULL;
483 * Networks objects created during any P2P activities are not
484 * exposed out as they might/will confuse certain non-P2P aware
485 * applications since these network objects won't behave like
488 * Likewise, we don't send out network removed signals for such
491 wpa_config_remove_network(wpa_s->conf, id);
492 wpa_supplicant_clear_status(wpa_s);
493 wpa_supplicant_cancel_sched_scan(wpa_s);
494 wpa_s->sta_scan_pending = 0;
496 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
500 wpa_supplicant_ap_deinit(wpa_s);
502 wpa_drv_deinit_p2p_cli(wpa_s);
508 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
510 const u8 *ssid, size_t ssid_len)
518 if (wpa_s->go_params)
519 bssid = wpa_s->go_params->peer_interface_addr;
521 bssid = wpa_s->bssid;
523 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
525 u8 iface_addr[ETH_ALEN];
526 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
528 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
531 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
532 "group is persistent - BSS " MACSTR " not found",
537 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
539 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
542 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
543 "group is persistent - BSS " MACSTR
544 " did not include P2P IE", MAC2STR(bssid));
545 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
546 (u8 *) (bss + 1), bss->ie_len);
547 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
548 ((u8 *) bss + 1) + bss->ie_len,
553 group_capab = p2p_get_group_capab(p2p);
554 addr = p2p_get_go_dev_addr(p2p);
555 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
556 "group_capab=0x%x", group_capab);
558 os_memcpy(go_dev_addr, addr, ETH_ALEN);
559 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
562 os_memset(go_dev_addr, 0, ETH_ALEN);
565 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
566 "go_dev_addr=" MACSTR,
567 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
569 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
573 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
574 struct wpa_ssid *ssid,
575 const u8 *go_dev_addr)
580 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
581 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
582 for (s = wpa_s->conf->ssid; s; s = s->next) {
583 if (s->disabled == 2 &&
584 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
585 s->ssid_len == ssid->ssid_len &&
586 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
591 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
593 if (ssid->passphrase && !s->passphrase)
595 else if (ssid->passphrase && s->passphrase &&
596 os_strcmp(ssid->passphrase, s->passphrase) != 0)
599 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
602 s = wpa_config_add_network(wpa_s->conf);
607 * Instead of network_added we emit persistent_group_added
608 * notification. Also to keep the defense checks in
609 * persistent_group obj registration method, we set the
610 * relevant flags in s to designate it as a persistent group.
613 s->p2p_persistent_group = 1;
614 wpas_notify_persistent_group_added(wpa_s, s);
615 wpa_config_set_network_defaults(s);
619 s->p2p_persistent_group = 1;
622 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
623 s->mode = ssid->mode;
624 s->auth_alg = WPA_AUTH_ALG_OPEN;
625 s->key_mgmt = WPA_KEY_MGMT_PSK;
626 s->proto = WPA_PROTO_RSN;
627 s->pairwise_cipher = WPA_CIPHER_CCMP;
629 if (ssid->passphrase) {
630 os_free(s->passphrase);
631 s->passphrase = os_strdup(ssid->passphrase);
635 os_memcpy(s->psk, ssid->psk, 32);
637 if (s->passphrase && !s->psk_set)
638 wpa_config_update_psk(s);
639 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
641 s->ssid = os_malloc(ssid->ssid_len);
644 s->ssid_len = ssid->ssid_len;
645 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
647 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
648 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
649 wpa_s->global->add_psk = NULL;
653 #ifndef CONFIG_NO_CONFIG_WRITE
654 if (changed && wpa_s->conf->update_config &&
655 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
656 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
658 #endif /* CONFIG_NO_CONFIG_WRITE */
664 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
667 struct wpa_ssid *ssid, *s;
672 ssid = wpa_s->current_ssid;
673 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
674 !ssid->p2p_persistent_group)
677 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
678 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
681 if (s->ssid_len == ssid->ssid_len &&
682 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
689 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
690 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
694 if (i == s->num_p2p_clients - 1)
695 return; /* already the most recent entry */
697 /* move the entry to mark it most recent */
698 os_memmove(s->p2p_client_list + i * ETH_ALEN,
699 s->p2p_client_list + (i + 1) * ETH_ALEN,
700 (s->num_p2p_clients - i - 1) * ETH_ALEN);
701 os_memcpy(s->p2p_client_list +
702 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
707 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
708 n = os_realloc_array(s->p2p_client_list,
709 s->num_p2p_clients + 1, ETH_ALEN);
712 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
713 s->p2p_client_list = n;
714 s->num_p2p_clients++;
716 /* Not enough room for an additional entry - drop the oldest
718 os_memmove(s->p2p_client_list,
719 s->p2p_client_list + ETH_ALEN,
720 (s->num_p2p_clients - 1) * ETH_ALEN);
721 os_memcpy(s->p2p_client_list +
722 (s->num_p2p_clients - 1) * ETH_ALEN,
726 #ifndef CONFIG_NO_CONFIG_WRITE
727 if (wpa_s->parent->conf->update_config &&
728 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
729 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
730 #endif /* CONFIG_NO_CONFIG_WRITE */
734 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
737 struct wpa_ssid *ssid;
738 const char *ssid_txt;
741 u8 go_dev_addr[ETH_ALEN];
745 * This callback is likely called for the main interface. Update wpa_s
746 * to use the group interface if a new interface was created for the
749 if (wpa_s->global->p2p_group_formation)
750 wpa_s = wpa_s->global->p2p_group_formation;
751 if (wpa_s->p2p_go_group_formation_completed) {
752 wpa_s->global->p2p_group_formation = NULL;
753 wpa_s->p2p_in_provisioning = 0;
757 wpa_msg_global(wpa_s->parent, MSG_INFO,
758 P2P_EVENT_GROUP_FORMATION_FAILURE);
759 wpas_p2p_group_delete(wpa_s,
760 P2P_GROUP_REMOVAL_FORMATION_FAILED);
764 wpa_msg_global(wpa_s->parent, MSG_INFO,
765 P2P_EVENT_GROUP_FORMATION_SUCCESS);
767 ssid = wpa_s->current_ssid;
768 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
769 ssid->mode = WPAS_MODE_P2P_GO;
770 p2p_group_notif_formation_done(wpa_s->p2p_group);
771 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
776 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
777 client = ssid->mode == WPAS_MODE_INFRA;
778 if (ssid->mode == WPAS_MODE_P2P_GO) {
779 persistent = ssid->p2p_persistent_group;
780 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
783 persistent = wpas_p2p_persistent_group(wpa_s,
789 client = wpa_s->p2p_group_interface ==
790 P2P_GROUP_INTERFACE_CLIENT;
791 os_memset(go_dev_addr, 0, ETH_ALEN);
794 wpa_s->show_group_started = 0;
797 * Indicate event only after successfully completed 4-way
798 * handshake, i.e., when the interface is ready for data
801 wpa_s->show_group_started = 1;
802 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
804 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
805 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
806 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
808 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
809 MAC2STR(go_dev_addr),
810 persistent ? " [PERSISTENT]" : "");
811 wpas_p2p_cross_connect_setup(wpa_s);
812 wpas_p2p_set_group_idle_timeout(wpa_s);
814 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
815 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
816 "go_dev_addr=" MACSTR "%s",
817 wpa_s->ifname, ssid_txt,
818 ssid ? ssid->frequency : 0,
819 ssid && ssid->passphrase ? ssid->passphrase : "",
820 MAC2STR(go_dev_addr),
821 persistent ? " [PERSISTENT]" : "");
822 wpas_p2p_cross_connect_setup(wpa_s);
823 wpas_p2p_set_group_idle_timeout(wpa_s);
827 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
830 os_free(wpa_s->global->add_psk);
831 wpa_s->global->add_psk = NULL;
833 if (network_id < 0 && ssid)
834 network_id = ssid->id;
836 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
837 os_get_time(&wpa_s->global->p2p_go_wait_client);
842 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
844 const u8 *dst, const u8 *src,
846 const u8 *data, size_t data_len,
847 enum offchannel_send_action_result
850 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
852 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
854 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
858 case OFFCHANNEL_SEND_ACTION_SUCCESS:
859 res = P2P_SEND_ACTION_SUCCESS;
861 case OFFCHANNEL_SEND_ACTION_NO_ACK:
862 res = P2P_SEND_ACTION_NO_ACK;
864 case OFFCHANNEL_SEND_ACTION_FAILED:
865 res = P2P_SEND_ACTION_FAILED;
869 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
871 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
872 wpa_s->pending_pd_before_join &&
873 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
874 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
875 wpa_s->p2p_fallback_to_go_neg) {
876 wpa_s->pending_pd_before_join = 0;
877 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
878 "during p2p_connect-auto");
879 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
885 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
886 const u8 *src, const u8 *bssid, const u8 *buf,
887 size_t len, unsigned int wait_time)
889 struct wpa_supplicant *wpa_s = ctx;
890 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
892 wpas_p2p_send_action_tx_status, 1);
896 static void wpas_send_action_done(void *ctx)
898 struct wpa_supplicant *wpa_s = ctx;
899 offchannel_send_action_done(wpa_s);
903 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
904 struct p2p_go_neg_results *params)
906 if (wpa_s->go_params == NULL) {
907 wpa_s->go_params = os_malloc(sizeof(*params));
908 if (wpa_s->go_params == NULL)
911 os_memcpy(wpa_s->go_params, params, sizeof(*params));
916 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
917 struct p2p_go_neg_results *res)
919 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
920 MAC2STR(res->peer_interface_addr));
921 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
922 res->ssid, res->ssid_len);
923 wpa_supplicant_ap_deinit(wpa_s);
924 wpas_copy_go_neg_results(wpa_s, res);
925 if (res->wps_method == WPS_PBC)
926 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
928 u16 dev_pw_id = DEV_PW_DEFAULT;
929 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
930 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
931 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
932 wpa_s->p2p_pin, 1, dev_pw_id);
937 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
938 struct wpa_ssid *ssid)
940 struct wpa_ssid *persistent;
941 struct psk_list_entry *psk;
942 struct hostapd_data *hapd;
944 if (!wpa_s->ap_iface)
947 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
949 if (persistent == NULL)
952 hapd = wpa_s->ap_iface->bss[0];
954 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
956 struct hostapd_wpa_psk *hpsk;
958 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
960 MAC2STR(psk->addr), psk->p2p);
961 hpsk = os_zalloc(sizeof(*hpsk));
964 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
966 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
968 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
969 hpsk->next = hapd->conf->ssid.wpa_psk;
970 hapd->conf->ssid.wpa_psk = hpsk;
975 static void p2p_go_configured(void *ctx, void *data)
977 struct wpa_supplicant *wpa_s = ctx;
978 struct p2p_go_neg_results *params = data;
979 struct wpa_ssid *ssid;
982 ssid = wpa_s->current_ssid;
983 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
984 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
985 if (wpa_s->global->p2p_group_formation == wpa_s)
986 wpa_s->global->p2p_group_formation = NULL;
987 if (os_strlen(params->passphrase) > 0) {
988 wpa_msg_global(wpa_s->parent, MSG_INFO,
989 P2P_EVENT_GROUP_STARTED
990 "%s GO ssid=\"%s\" freq=%d "
991 "passphrase=\"%s\" go_dev_addr=" MACSTR
993 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
994 ssid->frequency, params->passphrase,
995 MAC2STR(wpa_s->global->p2p_dev_addr),
996 params->persistent_group ?
997 " [PERSISTENT]" : "");
1000 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1001 sizeof(params->psk));
1002 wpa_msg_global(wpa_s->parent, MSG_INFO,
1003 P2P_EVENT_GROUP_STARTED
1004 "%s GO ssid=\"%s\" freq=%d psk=%s "
1005 "go_dev_addr=" MACSTR "%s",
1007 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1008 ssid->frequency, psk,
1009 MAC2STR(wpa_s->global->p2p_dev_addr),
1010 params->persistent_group ?
1011 " [PERSISTENT]" : "");
1014 os_get_time(&wpa_s->global->p2p_go_wait_client);
1015 if (params->persistent_group) {
1016 network_id = wpas_p2p_store_persistent_group(
1017 wpa_s->parent, ssid,
1018 wpa_s->global->p2p_dev_addr);
1019 wpas_p2p_add_psk_list(wpa_s, ssid);
1022 network_id = ssid->id;
1023 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1024 wpas_p2p_cross_connect_setup(wpa_s);
1025 wpas_p2p_set_group_idle_timeout(wpa_s);
1027 if (wpa_s->p2p_first_connection_timeout) {
1028 wpa_dbg(wpa_s, MSG_DEBUG,
1029 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1030 wpa_s->p2p_first_connection_timeout);
1031 wpa_s->p2p_go_group_formation_completed = 0;
1032 wpa_s->global->p2p_group_formation = wpa_s;
1033 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1034 wpa_s->parent, NULL);
1035 eloop_register_timeout(
1036 wpa_s->p2p_first_connection_timeout, 0,
1037 wpas_p2p_group_formation_timeout,
1038 wpa_s->parent, NULL);
1044 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1045 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1046 params->peer_interface_addr)) {
1047 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1051 if (params->wps_method == WPS_PBC)
1052 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1053 params->peer_device_addr);
1054 else if (wpa_s->p2p_pin[0])
1055 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1056 wpa_s->p2p_pin, NULL, 0, 0);
1057 os_free(wpa_s->go_params);
1058 wpa_s->go_params = NULL;
1062 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1063 struct p2p_go_neg_results *params,
1064 int group_formation)
1066 struct wpa_ssid *ssid;
1068 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1069 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1070 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1075 ssid = wpa_config_add_network(wpa_s->conf);
1077 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1081 wpa_s->show_group_started = 0;
1083 wpa_config_set_network_defaults(ssid);
1084 ssid->temporary = 1;
1085 ssid->p2p_group = 1;
1086 ssid->p2p_persistent_group = params->persistent_group;
1087 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1089 ssid->frequency = params->freq;
1090 ssid->ht40 = params->ht40;
1091 ssid->ssid = os_zalloc(params->ssid_len + 1);
1093 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1094 ssid->ssid_len = params->ssid_len;
1096 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1097 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1098 ssid->proto = WPA_PROTO_RSN;
1099 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1100 if (os_strlen(params->passphrase) > 0) {
1101 ssid->passphrase = os_strdup(params->passphrase);
1102 if (ssid->passphrase == NULL) {
1103 wpa_msg_global(wpa_s, MSG_ERROR,
1104 "P2P: Failed to copy passphrase for GO");
1105 wpa_config_remove_network(wpa_s->conf, ssid->id);
1109 ssid->passphrase = NULL;
1110 ssid->psk_set = params->psk_set;
1112 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1113 else if (ssid->passphrase)
1114 wpa_config_update_psk(ssid);
1115 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1117 wpa_s->ap_configured_cb = p2p_go_configured;
1118 wpa_s->ap_configured_cb_ctx = wpa_s;
1119 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1120 wpa_s->connect_without_scan = ssid;
1121 wpa_s->reassociate = 1;
1122 wpa_s->disconnected = 0;
1123 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1125 wpa_supplicant_req_scan(wpa_s, 0, 0);
1129 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1130 const struct wpa_supplicant *src)
1132 struct wpa_config *d;
1133 const struct wpa_config *s;
1138 #define C(n) if (s->n) d->n = os_strdup(s->n)
1147 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1148 os_memcpy(d->sec_device_type, s->sec_device_type,
1149 sizeof(d->sec_device_type));
1150 d->num_sec_device_types = s->num_sec_device_types;
1152 d->p2p_group_idle = s->p2p_group_idle;
1153 d->p2p_intra_bss = s->p2p_intra_bss;
1154 d->persistent_reconnect = s->persistent_reconnect;
1155 d->max_num_sta = s->max_num_sta;
1156 d->pbc_in_m1 = s->pbc_in_m1;
1157 d->ignore_old_scan_res = s->ignore_old_scan_res;
1158 d->beacon_int = s->beacon_int;
1159 d->disassoc_low_ack = s->disassoc_low_ack;
1160 d->disable_scan_offload = s->disable_scan_offload;
1164 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1165 char *ifname, size_t len)
1167 char *ifname_ptr = wpa_s->ifname;
1169 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1170 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1171 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1174 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1175 if (os_strlen(ifname) >= IFNAMSIZ &&
1176 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1177 /* Try to avoid going over the IFNAMSIZ length limit */
1178 os_snprintf(ifname, sizeof(ifname), "p2p-%d",
1179 wpa_s->p2p_group_idx);
1184 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1185 enum wpa_driver_if_type type)
1187 char ifname[120], force_ifname[120];
1189 if (wpa_s->pending_interface_name[0]) {
1190 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1191 "- skip creation of a new one");
1192 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1193 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1194 "unknown?! ifname='%s'",
1195 wpa_s->pending_interface_name);
1201 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1202 force_ifname[0] = '\0';
1204 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1206 wpa_s->p2p_group_idx++;
1208 wpa_s->pending_interface_type = type;
1209 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1210 wpa_s->pending_interface_addr, NULL) < 0) {
1211 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1216 if (force_ifname[0]) {
1217 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1219 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1220 sizeof(wpa_s->pending_interface_name));
1222 os_strlcpy(wpa_s->pending_interface_name, ifname,
1223 sizeof(wpa_s->pending_interface_name));
1224 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1225 MACSTR, wpa_s->pending_interface_name,
1226 MAC2STR(wpa_s->pending_interface_addr));
1232 static void wpas_p2p_remove_pending_group_interface(
1233 struct wpa_supplicant *wpa_s)
1235 if (!wpa_s->pending_interface_name[0] ||
1236 is_zero_ether_addr(wpa_s->pending_interface_addr))
1237 return; /* No pending virtual interface */
1239 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1240 wpa_s->pending_interface_name);
1241 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1242 wpa_s->pending_interface_name);
1243 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1244 wpa_s->pending_interface_name[0] = '\0';
1248 static struct wpa_supplicant *
1249 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1251 struct wpa_interface iface;
1252 struct wpa_supplicant *group_wpa_s;
1254 if (!wpa_s->pending_interface_name[0]) {
1255 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1256 if (!wpas_p2p_create_iface(wpa_s))
1259 * Something has forced us to remove the pending interface; try
1260 * to create a new one and hope for the best that we will get
1261 * the same local address.
1263 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1264 WPA_IF_P2P_CLIENT) < 0)
1268 os_memset(&iface, 0, sizeof(iface));
1269 iface.ifname = wpa_s->pending_interface_name;
1270 iface.driver = wpa_s->driver->name;
1271 if (wpa_s->conf->ctrl_interface == NULL &&
1272 wpa_s->parent != wpa_s &&
1274 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1275 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1277 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1278 iface.driver_param = wpa_s->conf->driver_param;
1279 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1280 if (group_wpa_s == NULL) {
1281 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1282 "wpa_supplicant interface");
1285 wpa_s->pending_interface_name[0] = '\0';
1286 group_wpa_s->parent = wpa_s;
1287 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1288 P2P_GROUP_INTERFACE_CLIENT;
1289 wpa_s->global->p2p_group_formation = group_wpa_s;
1291 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1297 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1300 struct wpa_supplicant *wpa_s = eloop_ctx;
1301 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1302 wpas_p2p_group_formation_failed(wpa_s);
1306 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1308 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1309 wpa_s->parent, NULL);
1310 if (wpa_s->global->p2p)
1311 p2p_group_formation_failed(wpa_s->global->p2p);
1312 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1313 wpa_drv_p2p_group_formation_failed(wpa_s);
1314 wpas_group_formation_completed(wpa_s, 0);
1318 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1320 if (wpa_s->global->p2p_group_formation != wpa_s)
1322 /* Speed up group formation timeout since this cannot succeed */
1323 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1324 wpa_s->parent, NULL);
1325 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1326 wpa_s->parent, NULL);
1330 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1332 struct wpa_supplicant *wpa_s = ctx;
1334 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1335 wpa_drv_cancel_remain_on_channel(wpa_s);
1336 wpa_s->off_channel_freq = 0;
1337 wpa_s->roc_waiting_drv_freq = 0;
1341 wpa_msg_global(wpa_s, MSG_INFO,
1342 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1344 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1345 wpas_p2p_remove_pending_group_interface(wpa_s);
1349 if (wpa_s->p2p_go_ht40)
1352 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1353 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1355 res->role_go ? "GO" : "client", res->freq, res->ht40,
1356 MAC2STR(res->peer_device_addr),
1357 MAC2STR(res->peer_interface_addr),
1358 p2p_wps_method_text(res->wps_method));
1359 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1361 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1362 struct wpa_ssid *ssid;
1363 ssid = wpa_config_get_network(wpa_s->conf,
1364 wpa_s->p2p_persistent_id);
1365 if (ssid && ssid->disabled == 2 &&
1366 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1367 size_t len = os_strlen(ssid->passphrase);
1368 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1369 "on requested persistent group");
1370 os_memcpy(res->passphrase, ssid->passphrase, len);
1371 res->passphrase[len] = '\0';
1375 if (wpa_s->create_p2p_iface) {
1376 struct wpa_supplicant *group_wpa_s =
1377 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1378 if (group_wpa_s == NULL) {
1379 wpas_p2p_remove_pending_group_interface(wpa_s);
1382 if (group_wpa_s != wpa_s) {
1383 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1384 sizeof(group_wpa_s->p2p_pin));
1385 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1387 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1388 wpa_s->pending_interface_name[0] = '\0';
1389 group_wpa_s->p2p_in_provisioning = 1;
1392 wpas_start_wps_go(group_wpa_s, res, 1);
1394 wpas_start_wps_enrollee(group_wpa_s, res);
1396 wpa_s->p2p_in_provisioning = 1;
1397 wpa_s->global->p2p_group_formation = wpa_s;
1400 wpas_start_wps_go(wpa_s, res, 1);
1402 wpas_start_wps_enrollee(ctx, res);
1405 wpa_s->p2p_long_listen = 0;
1406 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1408 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1409 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1410 (res->peer_config_timeout % 100) * 10000,
1411 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1415 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1417 struct wpa_supplicant *wpa_s = ctx;
1418 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1419 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1421 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1425 void wpas_dev_found(void *ctx, const u8 *addr,
1426 const struct p2p_peer_info *info,
1429 #ifndef CONFIG_NO_STDOUT_DEBUG
1430 struct wpa_supplicant *wpa_s = ctx;
1431 char devtype[WPS_DEV_TYPE_BUFSIZE];
1433 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1434 " p2p_dev_addr=" MACSTR
1435 " pri_dev_type=%s name='%s' config_methods=0x%x "
1436 "dev_capab=0x%x group_capab=0x%x",
1437 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1438 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1440 info->device_name, info->config_methods,
1441 info->dev_capab, info->group_capab);
1442 #endif /* CONFIG_NO_STDOUT_DEBUG */
1444 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1448 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1450 struct wpa_supplicant *wpa_s = ctx;
1452 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1453 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1455 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1459 static void wpas_find_stopped(void *ctx)
1461 struct wpa_supplicant *wpa_s = ctx;
1462 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1466 static int wpas_start_listen(void *ctx, unsigned int freq,
1467 unsigned int duration,
1468 const struct wpabuf *probe_resp_ie)
1470 struct wpa_supplicant *wpa_s = ctx;
1472 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1474 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1475 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1476 "report received Probe Request frames");
1480 wpa_s->pending_listen_freq = freq;
1481 wpa_s->pending_listen_duration = duration;
1483 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1484 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1485 "to remain on channel (%u MHz) for Listen "
1487 wpa_s->pending_listen_freq = 0;
1490 wpa_s->off_channel_freq = 0;
1491 wpa_s->roc_waiting_drv_freq = freq;
1497 static void wpas_stop_listen(void *ctx)
1499 struct wpa_supplicant *wpa_s = ctx;
1500 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1501 wpa_drv_cancel_remain_on_channel(wpa_s);
1502 wpa_s->off_channel_freq = 0;
1503 wpa_s->roc_waiting_drv_freq = 0;
1505 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1506 wpa_drv_probe_req_report(wpa_s, 0);
1510 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1512 struct wpa_supplicant *wpa_s = ctx;
1513 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1518 * DNS Header section is used only to calculate compression pointers, so the
1519 * contents of this data does not matter, but the length needs to be reserved
1520 * in the virtual packet.
1522 #define DNS_HEADER_LEN 12
1525 * 27-octet in-memory packet from P2P specification containing two implied
1526 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1528 #define P2P_SD_IN_MEMORY_LEN 27
1530 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1531 u8 **spos, const u8 *end)
1533 while (*spos < end) {
1534 u8 val = ((*spos)[0] & 0xc0) >> 6;
1537 if (val == 1 || val == 2) {
1538 /* These are reserved values in RFC 1035 */
1539 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1540 "sequence starting with 0x%x", val);
1549 if (*spos + 2 > end) {
1550 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1551 "DNS offset field");
1555 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1556 if (offset >= *spos - start) {
1557 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1558 "pointer offset %u", offset);
1563 spos_tmp = start + offset;
1564 return p2p_sd_dns_uncompress_label(upos, uend, start,
1570 len = (*spos)[0] & 0x3f;
1575 if (*spos + len > end) {
1576 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1577 "sequence - no room for label with length "
1582 if (*upos + len + 2 > uend)
1585 os_memcpy(*upos, *spos, len);
1597 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1598 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1599 * not large enough */
1600 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1601 size_t msg_len, size_t offset)
1603 /* 27-octet in-memory packet from P2P specification */
1604 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1605 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1606 u8 *tmp, *end, *spos;
1612 if (offset > msg_len)
1615 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1618 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1619 end = spos + msg_len;
1622 os_memset(tmp, 0, DNS_HEADER_LEN);
1623 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1624 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1627 uend = buf + buf_len;
1629 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1638 } else if (upos[-1] == '.')
1646 static struct p2p_srv_bonjour *
1647 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1648 const struct wpabuf *query)
1650 struct p2p_srv_bonjour *bsrv;
1653 len = wpabuf_len(query);
1654 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1655 struct p2p_srv_bonjour, list) {
1656 if (len == wpabuf_len(bsrv->query) &&
1657 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1665 static struct p2p_srv_upnp *
1666 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1667 const char *service)
1669 struct p2p_srv_upnp *usrv;
1671 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1672 struct p2p_srv_upnp, list) {
1673 if (version == usrv->version &&
1674 os_strcmp(service, usrv->service) == 0)
1681 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1686 if (wpabuf_tailroom(resp) < 5)
1689 /* Length (to be filled) */
1690 len_pos = wpabuf_put(resp, 2);
1691 wpabuf_put_u8(resp, srv_proto);
1692 wpabuf_put_u8(resp, srv_trans_id);
1694 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1695 /* Response Data: empty */
1696 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1700 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1701 struct wpabuf *resp, u8 srv_trans_id)
1703 struct p2p_srv_bonjour *bsrv;
1706 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1708 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1709 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1713 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1714 struct p2p_srv_bonjour, list) {
1715 if (wpabuf_tailroom(resp) <
1716 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1718 /* Length (to be filled) */
1719 len_pos = wpabuf_put(resp, 2);
1720 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1721 wpabuf_put_u8(resp, srv_trans_id);
1723 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1724 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1725 wpabuf_head(bsrv->resp),
1726 wpabuf_len(bsrv->resp));
1728 wpabuf_put_buf(resp, bsrv->query); /* Key */
1729 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1730 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1736 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1739 char str_rx[256], str_srv[256];
1741 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1742 return 0; /* Too short to include DNS Type and Version */
1743 if (os_memcmp(query + query_len - 3,
1744 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1746 return 0; /* Mismatch in DNS Type or Version */
1747 if (query_len == wpabuf_len(bsrv->query) &&
1748 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1749 return 1; /* Binary match */
1751 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1753 return 0; /* Failed to uncompress query */
1754 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1755 wpabuf_head(bsrv->query),
1756 wpabuf_len(bsrv->query) - 3, 0))
1757 return 0; /* Failed to uncompress service */
1759 return os_strcmp(str_rx, str_srv) == 0;
1763 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1764 struct wpabuf *resp, u8 srv_trans_id,
1765 const u8 *query, size_t query_len)
1767 struct p2p_srv_bonjour *bsrv;
1771 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1773 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1774 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1775 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1780 if (query_len == 0) {
1781 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1785 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1786 struct p2p_srv_bonjour, list) {
1787 if (!match_bonjour_query(bsrv, query, query_len))
1790 if (wpabuf_tailroom(resp) <
1791 5 + query_len + wpabuf_len(bsrv->resp))
1796 /* Length (to be filled) */
1797 len_pos = wpabuf_put(resp, 2);
1798 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1799 wpabuf_put_u8(resp, srv_trans_id);
1802 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1803 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1804 wpabuf_head(bsrv->resp),
1805 wpabuf_len(bsrv->resp));
1808 wpabuf_put_data(resp, query, query_len); /* Key */
1809 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1811 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1815 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1817 if (wpabuf_tailroom(resp) < 5)
1820 /* Length (to be filled) */
1821 len_pos = wpabuf_put(resp, 2);
1822 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1823 wpabuf_put_u8(resp, srv_trans_id);
1826 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1827 /* Response Data: empty */
1828 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1834 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1835 struct wpabuf *resp, u8 srv_trans_id)
1837 struct p2p_srv_upnp *usrv;
1840 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1842 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1843 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1847 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1848 struct p2p_srv_upnp, list) {
1849 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1852 /* Length (to be filled) */
1853 len_pos = wpabuf_put(resp, 2);
1854 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1855 wpabuf_put_u8(resp, srv_trans_id);
1858 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1860 wpabuf_put_u8(resp, usrv->version);
1861 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1863 wpabuf_put_str(resp, usrv->service);
1864 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1870 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1871 struct wpabuf *resp, u8 srv_trans_id,
1872 const u8 *query, size_t query_len)
1874 struct p2p_srv_upnp *usrv;
1880 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1883 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1884 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1885 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1890 if (query_len == 0) {
1891 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1895 if (wpabuf_tailroom(resp) < 5)
1898 /* Length (to be filled) */
1899 len_pos = wpabuf_put(resp, 2);
1900 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1901 wpabuf_put_u8(resp, srv_trans_id);
1904 str = os_malloc(query_len);
1907 os_memcpy(str, query + 1, query_len - 1);
1908 str[query_len - 1] = '\0';
1910 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1911 struct p2p_srv_upnp, list) {
1912 if (version != usrv->version)
1915 if (os_strcmp(str, "ssdp:all") != 0 &&
1916 os_strstr(usrv->service, str) == NULL)
1919 if (wpabuf_tailroom(resp) < 2)
1923 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1925 wpabuf_put_u8(resp, version);
1927 wpabuf_put_u8(resp, ',');
1931 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1933 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1935 wpabuf_put_str(resp, usrv->service);
1940 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1943 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1944 /* Response Data: empty */
1947 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1951 #ifdef CONFIG_WIFI_DISPLAY
1952 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1953 struct wpabuf *resp, u8 srv_trans_id,
1954 const u8 *query, size_t query_len)
1960 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1962 if (!wpa_s->global->wifi_display) {
1963 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1964 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
1969 if (query_len < 1) {
1970 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
1975 if (wpabuf_tailroom(resp) < 5)
1980 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
1982 /* TODO: role specific handling */
1984 /* Length (to be filled) */
1985 len_pos = wpabuf_put(resp, 2);
1986 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
1987 wpabuf_put_u8(resp, srv_trans_id);
1988 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
1990 while (pos < query + query_len) {
1991 if (*pos < MAX_WFD_SUBELEMS &&
1992 wpa_s->global->wfd_subelem[*pos] &&
1993 wpabuf_tailroom(resp) >=
1994 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
1995 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
1996 "subelement %u", *pos);
1997 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2002 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2004 #endif /* CONFIG_WIFI_DISPLAY */
2007 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2008 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2010 struct wpa_supplicant *wpa_s = ctx;
2011 const u8 *pos = tlvs;
2012 const u8 *end = tlvs + tlvs_len;
2015 struct wpabuf *resp;
2016 u8 srv_proto, srv_trans_id;
2020 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2022 buf_len = 2 * tlvs_len + 1;
2023 buf = os_malloc(buf_len);
2025 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2026 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2028 freq, MAC2STR(sa), dialog_token, update_indic,
2033 if (wpa_s->p2p_sd_over_ctrl_iface) {
2034 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2035 update_indic, tlvs, tlvs_len);
2036 return; /* to be processed by an external program */
2039 resp = wpabuf_alloc(10000);
2043 while (pos + 1 < end) {
2044 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2045 slen = WPA_GET_LE16(pos);
2047 if (pos + slen > end || slen < 2) {
2048 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2053 tlv_end = pos + slen;
2056 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2058 srv_trans_id = *pos++;
2059 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2062 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2063 pos, tlv_end - pos);
2066 if (wpa_s->force_long_sd) {
2067 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2069 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2070 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2074 switch (srv_proto) {
2075 case P2P_SERV_ALL_SERVICES:
2076 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2077 "for all services");
2078 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2079 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2080 wpa_printf(MSG_DEBUG, "P2P: No service "
2081 "discovery protocols available");
2082 wpas_sd_add_proto_not_avail(
2083 resp, P2P_SERV_ALL_SERVICES,
2087 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2088 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2090 case P2P_SERV_BONJOUR:
2091 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2092 pos, tlv_end - pos);
2095 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2096 pos, tlv_end - pos);
2098 #ifdef CONFIG_WIFI_DISPLAY
2099 case P2P_SERV_WIFI_DISPLAY:
2100 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2101 pos, tlv_end - pos);
2103 #endif /* CONFIG_WIFI_DISPLAY */
2105 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2106 "protocol %u", srv_proto);
2107 wpas_sd_add_proto_not_avail(resp, srv_proto,
2116 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2117 update_indic, tlvs, tlvs_len);
2119 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2125 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2126 const u8 *tlvs, size_t tlvs_len)
2128 struct wpa_supplicant *wpa_s = ctx;
2129 const u8 *pos = tlvs;
2130 const u8 *end = tlvs + tlvs_len;
2136 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2138 if (tlvs_len > 1500) {
2139 /* TODO: better way for handling this */
2140 wpa_msg_ctrl(wpa_s, MSG_INFO,
2141 P2P_EVENT_SERV_DISC_RESP MACSTR
2142 " %u <long response: %u bytes>",
2143 MAC2STR(sa), update_indic,
2144 (unsigned int) tlvs_len);
2146 buf_len = 2 * tlvs_len + 1;
2147 buf = os_malloc(buf_len);
2149 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2150 wpa_msg_ctrl(wpa_s, MSG_INFO,
2151 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2152 MAC2STR(sa), update_indic, buf);
2158 u8 srv_proto, srv_trans_id, status;
2160 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2161 slen = WPA_GET_LE16(pos);
2163 if (pos + slen > end || slen < 3) {
2164 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2168 tlv_end = pos + slen;
2171 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2173 srv_trans_id = *pos++;
2174 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2177 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2180 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2181 pos, tlv_end - pos);
2186 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2190 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2191 const struct wpabuf *tlvs)
2193 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2194 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
2195 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2197 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2201 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2202 u8 version, const char *query)
2204 struct wpabuf *tlvs;
2207 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2210 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2211 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2212 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2213 wpabuf_put_u8(tlvs, version);
2214 wpabuf_put_str(tlvs, query);
2215 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2221 #ifdef CONFIG_WIFI_DISPLAY
2223 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2224 const struct wpabuf *tlvs)
2226 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2228 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2230 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2234 #define MAX_WFD_SD_SUBELEMS 20
2236 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2237 const char *subelems)
2244 len = wpabuf_put(tlvs, 2);
2245 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2246 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2248 wpabuf_put_u8(tlvs, role);
2253 if (val >= 0 && val < 256) {
2254 wpabuf_put_u8(tlvs, val);
2256 if (count == MAX_WFD_SD_SUBELEMS)
2259 pos = os_strchr(pos + 1, ',');
2265 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2269 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2270 const u8 *dst, const char *role)
2272 struct wpabuf *tlvs;
2274 const char *subelems;
2277 subelems = os_strchr(role, ' ');
2278 if (subelems == NULL)
2282 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2286 if (os_strstr(role, "[source]"))
2287 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2288 if (os_strstr(role, "[pri-sink]"))
2289 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2290 if (os_strstr(role, "[sec-sink]"))
2291 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2292 if (os_strstr(role, "[source+sink]"))
2293 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2295 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2300 #endif /* CONFIG_WIFI_DISPLAY */
2303 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2305 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2306 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
2307 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2309 return p2p_sd_cancel_request(wpa_s->global->p2p,
2310 (void *) (uintptr_t) req);
2314 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2315 const u8 *dst, u8 dialog_token,
2316 const struct wpabuf *resp_tlvs)
2318 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2319 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2323 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2325 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2330 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2332 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2333 wpa_drv_p2p_service_update(wpa_s);
2336 if (wpa_s->global->p2p)
2337 p2p_sd_service_update(wpa_s->global->p2p);
2341 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2343 dl_list_del(&bsrv->list);
2344 wpabuf_free(bsrv->query);
2345 wpabuf_free(bsrv->resp);
2350 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2352 dl_list_del(&usrv->list);
2353 os_free(usrv->service);
2358 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2360 struct p2p_srv_bonjour *bsrv, *bn;
2361 struct p2p_srv_upnp *usrv, *un;
2363 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2364 struct p2p_srv_bonjour, list)
2365 wpas_p2p_srv_bonjour_free(bsrv);
2367 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2368 struct p2p_srv_upnp, list)
2369 wpas_p2p_srv_upnp_free(usrv);
2371 wpas_p2p_sd_service_update(wpa_s);
2375 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2376 struct wpabuf *query, struct wpabuf *resp)
2378 struct p2p_srv_bonjour *bsrv;
2380 bsrv = os_zalloc(sizeof(*bsrv));
2383 bsrv->query = query;
2385 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2387 wpas_p2p_sd_service_update(wpa_s);
2392 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2393 const struct wpabuf *query)
2395 struct p2p_srv_bonjour *bsrv;
2397 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2400 wpas_p2p_srv_bonjour_free(bsrv);
2401 wpas_p2p_sd_service_update(wpa_s);
2406 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2407 const char *service)
2409 struct p2p_srv_upnp *usrv;
2411 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2412 return 0; /* Already listed */
2413 usrv = os_zalloc(sizeof(*usrv));
2416 usrv->version = version;
2417 usrv->service = os_strdup(service);
2418 if (usrv->service == NULL) {
2422 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2424 wpas_p2p_sd_service_update(wpa_s);
2429 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2430 const char *service)
2432 struct p2p_srv_upnp *usrv;
2434 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2437 wpas_p2p_srv_upnp_free(usrv);
2438 wpas_p2p_sd_service_update(wpa_s);
2443 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2444 const u8 *peer, const char *params,
2445 unsigned int generated_pin)
2447 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2448 " %08d%s", MAC2STR(peer), generated_pin, params);
2452 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2453 const u8 *peer, const char *params)
2455 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2456 "%s", MAC2STR(peer), params);
2460 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2461 const u8 *dev_addr, const u8 *pri_dev_type,
2462 const char *dev_name, u16 supp_config_methods,
2463 u8 dev_capab, u8 group_capab, const u8 *group_id,
2464 size_t group_id_len)
2466 struct wpa_supplicant *wpa_s = ctx;
2467 char devtype[WPS_DEV_TYPE_BUFSIZE];
2469 u8 empty_dev_type[8];
2470 unsigned int generated_pin = 0;
2471 struct wpa_supplicant *group = NULL;
2474 for (group = wpa_s->global->ifaces; group; group = group->next)
2476 struct wpa_ssid *s = group->current_ssid;
2478 s->mode == WPAS_MODE_P2P_GO &&
2479 group_id_len - ETH_ALEN == s->ssid_len &&
2480 os_memcmp(group_id + ETH_ALEN, s->ssid,
2486 if (pri_dev_type == NULL) {
2487 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2488 pri_dev_type = empty_dev_type;
2490 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2491 " pri_dev_type=%s name='%s' config_methods=0x%x "
2492 "dev_capab=0x%x group_capab=0x%x%s%s",
2494 wps_dev_type_bin2str(pri_dev_type, devtype,
2496 dev_name, supp_config_methods, dev_capab, group_capab,
2497 group ? " group=" : "",
2498 group ? group->ifname : "");
2499 params[sizeof(params) - 1] = '\0';
2501 if (config_methods & WPS_CONFIG_DISPLAY) {
2502 generated_pin = wps_generate_pin();
2503 wpas_prov_disc_local_display(wpa_s, peer, params,
2505 } else if (config_methods & WPS_CONFIG_KEYPAD)
2506 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2507 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2508 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2509 MACSTR "%s", MAC2STR(peer), params);
2511 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2512 P2P_PROV_DISC_SUCCESS,
2513 config_methods, generated_pin);
2517 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2519 struct wpa_supplicant *wpa_s = ctx;
2520 unsigned int generated_pin = 0;
2523 if (wpa_s->pending_pd_before_join &&
2524 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2525 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2526 wpa_s->pending_pd_before_join = 0;
2527 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2528 "join-existing-group operation");
2529 wpas_p2p_join_start(wpa_s);
2533 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2534 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2535 os_snprintf(params, sizeof(params), " peer_go=%d",
2536 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2540 if (config_methods & WPS_CONFIG_DISPLAY)
2541 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2542 else if (config_methods & WPS_CONFIG_KEYPAD) {
2543 generated_pin = wps_generate_pin();
2544 wpas_prov_disc_local_display(wpa_s, peer, params,
2546 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2547 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2548 MACSTR "%s", MAC2STR(peer), params);
2550 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2551 P2P_PROV_DISC_SUCCESS,
2552 config_methods, generated_pin);
2556 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2557 enum p2p_prov_disc_status status)
2559 struct wpa_supplicant *wpa_s = ctx;
2561 if (wpa_s->p2p_fallback_to_go_neg) {
2562 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2563 "failed - fall back to GO Negotiation");
2564 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2568 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2569 wpa_s->pending_pd_before_join = 0;
2570 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2571 "join-existing-group operation (no ACK for PD "
2573 wpas_p2p_join_start(wpa_s);
2577 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2578 " p2p_dev_addr=" MACSTR " status=%d",
2579 MAC2STR(peer), status);
2581 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2586 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2588 if (channels == NULL)
2589 return 1; /* Assume no restrictions */
2590 return p2p_channels_includes_freq(channels, freq);
2595 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2596 const u8 *go_dev_addr, const u8 *ssid,
2597 size_t ssid_len, int *go, u8 *group_bssid,
2598 int *force_freq, int persistent_group,
2599 const struct p2p_channels *channels)
2601 struct wpa_supplicant *wpa_s = ctx;
2604 struct wpa_supplicant *grp;
2606 if (!persistent_group) {
2607 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2608 " to join an active group", MAC2STR(sa));
2609 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2610 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2612 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2613 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2614 "authorized invitation");
2618 * Do not accept the invitation automatically; notify user and
2621 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2624 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2626 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2627 "running persistent group");
2629 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2633 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2634 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2635 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2636 "invitation to re-invoke a persistent group");
2637 } else if (!wpa_s->conf->persistent_reconnect)
2638 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2640 for (s = wpa_s->conf->ssid; s; s = s->next) {
2641 if (s->disabled == 2 &&
2642 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2643 s->ssid_len == ssid_len &&
2644 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2649 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2650 " requested reinvocation of an unknown group",
2652 return P2P_SC_FAIL_UNKNOWN_GROUP;
2655 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2657 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2658 wpa_printf(MSG_DEBUG, "P2P: The only available "
2659 "interface is already in use - reject "
2661 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2663 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2664 } else if (s->mode == WPAS_MODE_P2P_GO) {
2666 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2668 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2669 "interface address for the group");
2670 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2672 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2677 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2679 /* Get one of the frequencies currently in use */
2680 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
2681 wpa_printf(MSG_DEBUG, "P2P: Trying to force channel to match a channel already used by one of the interfaces");
2683 wpas_p2p_set_own_freq_preference(wpa_s, res);
2686 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2687 wpas_p2p_num_unused_channels(wpa_s) > 0) {
2689 /* We are the client */
2690 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2691 "running a GO but we are capable of MCC, "
2692 "figure out the best channel to use");
2694 } else if (!freq_included(channels, *force_freq)) {
2695 /* We are the GO, and *force_freq is not in the
2697 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2698 "in intersection but we are capable of MCC, "
2699 "figure out the best channel to use",
2705 return P2P_SC_SUCCESS;
2709 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2710 const u8 *ssid, size_t ssid_len,
2711 const u8 *go_dev_addr, u8 status,
2714 struct wpa_supplicant *wpa_s = ctx;
2717 for (s = wpa_s->conf->ssid; s; s = s->next) {
2718 if (s->disabled == 2 &&
2719 s->ssid_len == ssid_len &&
2720 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2724 if (status == P2P_SC_SUCCESS) {
2725 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2726 " was accepted; op_freq=%d MHz",
2727 MAC2STR(sa), op_freq);
2729 int go = s->mode == WPAS_MODE_P2P_GO;
2730 wpas_p2p_group_add_persistent(
2731 wpa_s, s, go, go ? op_freq : 0, 0, NULL,
2732 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
2734 wpa_s->user_initiated_pd = 0;
2735 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2736 wpa_s->p2p_wps_method, 0);
2741 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2742 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2743 " was rejected (status %u)", MAC2STR(sa), status);
2749 wpa_msg_global(wpa_s, MSG_INFO,
2750 P2P_EVENT_INVITATION_RECEIVED
2751 "sa=" MACSTR " go_dev_addr=" MACSTR
2752 " bssid=" MACSTR " unknown-network",
2753 MAC2STR(sa), MAC2STR(go_dev_addr),
2756 wpa_msg_global(wpa_s, MSG_INFO,
2757 P2P_EVENT_INVITATION_RECEIVED
2758 "sa=" MACSTR " go_dev_addr=" MACSTR
2760 MAC2STR(sa), MAC2STR(go_dev_addr));
2765 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2766 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2767 "sa=" MACSTR " persistent=%d freq=%d",
2768 MAC2STR(sa), s->id, op_freq);
2770 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2771 "sa=" MACSTR " persistent=%d",
2772 MAC2STR(sa), s->id);
2777 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2778 struct wpa_ssid *ssid,
2779 const u8 *peer, int inv)
2786 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2787 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2791 if (i >= ssid->num_p2p_clients) {
2792 if (ssid->mode != WPAS_MODE_P2P_GO &&
2793 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2794 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2795 "due to invitation result", ssid->id);
2796 wpas_notify_network_removed(wpa_s, ssid);
2797 wpa_config_remove_network(wpa_s->conf, ssid->id);
2800 return; /* Peer not found in client list */
2803 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2804 "group %d client list%s",
2805 MAC2STR(peer), ssid->id,
2806 inv ? " due to invitation result" : "");
2807 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2808 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2809 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2810 ssid->num_p2p_clients--;
2811 #ifndef CONFIG_NO_CONFIG_WRITE
2812 if (wpa_s->parent->conf->update_config &&
2813 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2814 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2815 #endif /* CONFIG_NO_CONFIG_WRITE */
2819 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2822 struct wpa_ssid *ssid;
2824 wpa_s = wpa_s->global->p2p_invite_group;
2826 return; /* No known invitation group */
2827 ssid = wpa_s->current_ssid;
2828 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2829 !ssid->p2p_persistent_group)
2830 return; /* Not operating as a GO in persistent group */
2831 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2832 ssid->ssid, ssid->ssid_len);
2833 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2837 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
2838 const struct p2p_channels *channels,
2841 struct wpa_supplicant *wpa_s = ctx;
2842 struct wpa_ssid *ssid;
2845 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2846 "status=%d " MACSTR,
2847 status, MAC2STR(bssid));
2849 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2850 "status=%d ", status);
2852 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2854 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2855 status, MAC2STR(peer));
2856 if (wpa_s->pending_invite_ssid_id == -1) {
2857 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2858 wpas_remove_persistent_client(wpa_s, peer);
2859 return; /* Invitation to active group */
2862 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2863 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2864 "invitation exchange to indicate readiness for "
2868 if (status != P2P_SC_SUCCESS) {
2869 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2870 ssid = wpa_config_get_network(
2871 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2872 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2874 wpas_p2p_remove_pending_group_interface(wpa_s);
2878 ssid = wpa_config_get_network(wpa_s->conf,
2879 wpa_s->pending_invite_ssid_id);
2881 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2882 "data matching with invitation");
2887 * The peer could have missed our ctrl::ack frame for Invitation
2888 * Response and continue retransmitting the frame. To reduce the
2889 * likelihood of the peer not getting successful TX status for the
2890 * Invitation Response frame, wait a short time here before starting
2891 * the persistent group so that we will remain on the current channel to
2892 * acknowledge any possible retransmission from the peer.
2894 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2895 "starting persistent group");
2898 wpas_p2p_group_add_persistent(wpa_s, ssid,
2899 ssid->mode == WPAS_MODE_P2P_GO,
2900 wpa_s->p2p_persistent_go_freq,
2901 wpa_s->p2p_go_ht40, channels,
2902 ssid->mode == WPAS_MODE_P2P_GO ?
2903 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2908 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2913 if (global->p2p_disallow_freq == NULL)
2916 for (i = 0; i < global->num_p2p_disallow_freq; i++) {
2917 if (freq >= global->p2p_disallow_freq[i].min &&
2918 freq <= global->p2p_disallow_freq[i].max)
2926 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2928 reg->channel[reg->channels] = chan;
2933 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2934 struct p2p_channels *chan)
2938 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2941 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2942 chan->reg_class[cla].reg_class = 81;
2943 chan->reg_class[cla].channels = 0;
2944 for (i = 0; i < 11; i++) {
2945 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2946 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2948 if (chan->reg_class[cla].channels)
2951 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2954 /* Operating class 115 - 5 GHz, channels 36-48 */
2955 chan->reg_class[cla].reg_class = 115;
2956 chan->reg_class[cla].channels = 0;
2957 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2958 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2959 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2960 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2961 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
2962 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
2963 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
2964 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
2965 if (chan->reg_class[cla].channels)
2968 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
2971 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
2972 chan->reg_class[cla].reg_class = 124;
2973 chan->reg_class[cla].channels = 0;
2974 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
2975 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
2976 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
2977 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
2978 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
2979 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
2980 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
2981 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
2982 if (chan->reg_class[cla].channels)
2985 chan->reg_classes = cla;
2990 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
2992 enum hostapd_hw_mode mode)
2996 for (i = 0; i < num_modes; i++) {
2997 if (modes[i].mode == mode)
3005 static int has_channel(struct wpa_global *global,
3006 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3011 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3013 if (wpas_p2p_disallowed_freq(global, freq))
3016 for (i = 0; i < mode->num_channels; i++) {
3017 if (mode->channels[i].chan == chan) {
3019 *flags = mode->channels[i].flag;
3020 return !(mode->channels[i].flag &
3021 (HOSTAPD_CHAN_DISABLED |
3022 HOSTAPD_CHAN_PASSIVE_SCAN |
3023 HOSTAPD_CHAN_NO_IBSS |
3024 HOSTAPD_CHAN_RADAR));
3032 struct p2p_oper_class_map {
3033 enum hostapd_hw_mode mode;
3038 enum { BW20, BW40PLUS, BW40MINUS } bw;
3041 static struct p2p_oper_class_map op_class[] = {
3042 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3043 #if 0 /* Do not enable HT40 on 2 GHz for now */
3044 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3045 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3047 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3048 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3049 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3050 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3051 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3052 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3053 { -1, 0, 0, 0, 0, BW20 }
3057 static int wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3058 struct hostapd_hw_modes *mode,
3063 if (!has_channel(wpa_s->global, mode, channel, &flag))
3065 if (bw == BW40MINUS &&
3066 (!(flag & HOSTAPD_CHAN_HT40MINUS) ||
3067 !has_channel(wpa_s->global, mode, channel - 4, NULL)))
3069 if (bw == BW40PLUS &&
3070 (!(flag & HOSTAPD_CHAN_HT40PLUS) ||
3071 !has_channel(wpa_s->global, mode, channel + 4, NULL)))
3077 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3078 struct p2p_channels *chan)
3080 struct hostapd_hw_modes *mode;
3083 if (wpa_s->hw.modes == NULL) {
3084 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3085 "of all supported channels; assume dualband "
3087 return wpas_p2p_default_channels(wpa_s, chan);
3092 for (op = 0; op_class[op].op_class; op++) {
3093 struct p2p_oper_class_map *o = &op_class[op];
3095 struct p2p_reg_class *reg = NULL;
3097 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3100 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3101 if (wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw) < 1)
3104 wpa_printf(MSG_DEBUG, "P2P: Add operating "
3105 "class %u", o->op_class);
3106 reg = &chan->reg_class[cla];
3108 reg->reg_class = o->op_class;
3110 reg->channel[reg->channels] = ch;
3114 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3115 reg->channel, reg->channels);
3119 chan->reg_classes = cla;
3125 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3126 struct hostapd_hw_modes *mode, u8 channel)
3130 for (op = 0; op_class[op].op_class; op++) {
3131 struct p2p_oper_class_map *o = &op_class[op];
3134 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3135 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3136 o->bw == BW20 || ch != channel)
3138 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3142 return (o->bw == BW40MINUS) ? -1 : 1;
3151 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3154 struct wpa_supplicant *wpa_s = ctx;
3156 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3157 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3163 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3167 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3169 struct wpa_supplicant *wpa_s = ctx;
3171 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3172 struct wpa_ssid *ssid = wpa_s->current_ssid;
3175 if (ssid->mode != WPAS_MODE_INFRA)
3177 if (wpa_s->wpa_state != WPA_COMPLETED &&
3178 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3180 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3188 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3190 struct wpa_supplicant *wpa_s = ctx;
3191 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3195 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3197 struct wpa_interface iface;
3198 struct wpa_supplicant *p2pdev_wpa_s;
3200 char force_name[100];
3203 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3205 force_name[0] = '\0';
3206 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3207 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3208 force_name, wpa_s->pending_interface_addr, NULL);
3210 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3213 os_strlcpy(wpa_s->pending_interface_name, ifname,
3214 sizeof(wpa_s->pending_interface_name));
3216 os_memset(&iface, 0, sizeof(iface));
3218 iface.ifname = wpa_s->pending_interface_name;
3219 iface.driver = wpa_s->driver->name;
3220 iface.driver_param = wpa_s->conf->driver_param;
3221 iface.confname = wpa_s->confname;
3222 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3223 if (!p2pdev_wpa_s) {
3224 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3227 p2pdev_wpa_s->parent = wpa_s;
3229 wpa_s->pending_interface_name[0] = '\0';
3235 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3236 * @global: Pointer to global data from wpa_supplicant_init()
3237 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3238 * Returns: 0 on success, -1 on failure
3240 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3242 struct p2p_config p2p;
3246 if (wpa_s->conf->p2p_disabled)
3249 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3255 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3256 struct p2p_params params;
3258 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3259 os_memset(¶ms, 0, sizeof(params));
3260 params.dev_name = wpa_s->conf->device_name;
3261 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3263 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3264 os_memcpy(params.sec_dev_type,
3265 wpa_s->conf->sec_device_type,
3266 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3268 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
3274 os_memset(&p2p, 0, sizeof(p2p));
3276 p2p.debug_print = wpas_p2p_debug_print;
3277 p2p.p2p_scan = wpas_p2p_scan;
3278 p2p.send_action = wpas_send_action;
3279 p2p.send_action_done = wpas_send_action_done;
3280 p2p.go_neg_completed = wpas_go_neg_completed;
3281 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3282 p2p.dev_found = wpas_dev_found;
3283 p2p.dev_lost = wpas_dev_lost;
3284 p2p.find_stopped = wpas_find_stopped;
3285 p2p.start_listen = wpas_start_listen;
3286 p2p.stop_listen = wpas_stop_listen;
3287 p2p.send_probe_resp = wpas_send_probe_resp;
3288 p2p.sd_request = wpas_sd_request;
3289 p2p.sd_response = wpas_sd_response;
3290 p2p.prov_disc_req = wpas_prov_disc_req;
3291 p2p.prov_disc_resp = wpas_prov_disc_resp;
3292 p2p.prov_disc_fail = wpas_prov_disc_fail;
3293 p2p.invitation_process = wpas_invitation_process;
3294 p2p.invitation_received = wpas_invitation_received;
3295 p2p.invitation_result = wpas_invitation_result;
3296 p2p.get_noa = wpas_get_noa;
3297 p2p.go_connected = wpas_go_connected;
3299 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
3300 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
3301 p2p.dev_name = wpa_s->conf->device_name;
3302 p2p.manufacturer = wpa_s->conf->manufacturer;
3303 p2p.model_name = wpa_s->conf->model_name;
3304 p2p.model_number = wpa_s->conf->model_number;
3305 p2p.serial_number = wpa_s->conf->serial_number;
3307 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3308 p2p.config_methods = wpa_s->wps->config_methods;
3311 if (wpa_s->conf->p2p_listen_reg_class &&
3312 wpa_s->conf->p2p_listen_channel) {
3313 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3314 p2p.channel = wpa_s->conf->p2p_listen_channel;
3318 * Pick one of the social channels randomly as the listen
3321 os_get_random((u8 *) &r, sizeof(r));
3322 p2p.channel = 1 + (r % 3) * 5;
3324 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3326 if (wpa_s->conf->p2p_oper_reg_class &&
3327 wpa_s->conf->p2p_oper_channel) {
3328 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3329 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3330 p2p.cfg_op_channel = 1;
3331 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3332 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3335 p2p.op_reg_class = 81;
3337 * Use random operation channel from (1, 6, 11) if no other
3338 * preference is indicated.
3340 os_get_random((u8 *) &r, sizeof(r));
3341 p2p.op_channel = 1 + (r % 3) * 5;
3342 p2p.cfg_op_channel = 0;
3343 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3344 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3347 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3348 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3349 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3352 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3353 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3354 p2p.country[2] = 0x04;
3356 os_memcpy(p2p.country, "XX\x04", 3);
3358 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels)) {
3359 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3364 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3367 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3368 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3369 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3371 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3372 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3374 p2p.max_peers = 100;
3376 if (wpa_s->conf->p2p_ssid_postfix) {
3377 p2p.ssid_postfix_len =
3378 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3379 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3380 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3381 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3382 p2p.ssid_postfix_len);
3385 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3387 p2p.max_listen = wpa_s->max_remain_on_chan;
3389 global->p2p = p2p_init(&p2p);
3390 if (global->p2p == NULL)
3392 global->p2p_init_wpa_s = wpa_s;
3394 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3395 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3397 p2p_add_wps_vendor_extension(
3398 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3406 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3407 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3409 * This function deinitialize per-interface P2P data.
3411 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3413 if (wpa_s->driver && wpa_s->drv_priv)
3414 wpa_drv_probe_req_report(wpa_s, 0);
3416 if (wpa_s->go_params) {
3417 /* Clear any stored provisioning info */
3418 p2p_clear_provisioning_info(
3420 wpa_s->go_params->peer_device_addr);
3423 os_free(wpa_s->go_params);
3424 wpa_s->go_params = NULL;
3425 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3426 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3427 wpa_s->p2p_long_listen = 0;
3428 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3429 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3430 wpas_p2p_remove_pending_group_interface(wpa_s);
3432 /* TODO: remove group interface from the driver if this wpa_s instance
3433 * is on top of a P2P group interface */
3438 * wpas_p2p_deinit_global - Deinitialize global P2P module
3439 * @global: Pointer to global data from wpa_supplicant_init()
3441 * This function deinitializes the global (per device) P2P module.
3443 void wpas_p2p_deinit_global(struct wpa_global *global)
3445 struct wpa_supplicant *wpa_s, *tmp;
3447 wpa_s = global->ifaces;
3449 wpas_p2p_service_flush(wpa_s);
3451 if (global->p2p == NULL)
3454 /* Remove remaining P2P group interfaces */
3455 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3456 wpa_s = wpa_s->next;
3458 tmp = global->ifaces;
3461 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3466 /* Disconnect from the P2P group and deinit the interface */
3467 wpas_p2p_disconnect(tmp);
3471 * Deinit GO data on any possibly remaining interface (if main
3472 * interface is used as GO).
3474 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3475 if (wpa_s->ap_iface)
3476 wpas_p2p_group_deinit(wpa_s);
3479 p2p_deinit(global->p2p);
3481 global->p2p_init_wpa_s = NULL;
3485 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3487 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3488 wpa_s->conf->p2p_no_group_iface)
3489 return 0; /* separate interface disabled per configuration */
3490 if (wpa_s->drv_flags &
3491 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3492 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3493 return 1; /* P2P group requires a new interface in every case
3495 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3496 return 0; /* driver does not support concurrent operations */
3497 if (wpa_s->global->ifaces->next)
3498 return 1; /* more that one interface already in use */
3499 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3500 return 1; /* this interface is already in use */
3505 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3506 const u8 *peer_addr,
3507 enum p2p_wps_method wps_method,
3508 int go_intent, const u8 *own_interface_addr,
3509 unsigned int force_freq, int persistent_group,
3510 struct wpa_ssid *ssid, unsigned int pref_freq)
3512 if (persistent_group && wpa_s->conf->persistent_reconnect)
3513 persistent_group = 2;
3515 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3516 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3517 go_intent, own_interface_addr,
3518 force_freq, persistent_group);
3522 * Increase GO config timeout if HT40 is used since it takes some time
3523 * to scan channels for coex purposes before the BSS can be started.
3525 p2p_set_config_timeout(wpa_s->global->p2p,
3526 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3528 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3529 go_intent, own_interface_addr, force_freq,
3530 persistent_group, ssid ? ssid->ssid : NULL,
3531 ssid ? ssid->ssid_len : 0,
3532 wpa_s->p2p_pd_before_go_neg, pref_freq);
3536 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3537 const u8 *peer_addr,
3538 enum p2p_wps_method wps_method,
3539 int go_intent, const u8 *own_interface_addr,
3540 unsigned int force_freq, int persistent_group,
3541 struct wpa_ssid *ssid, unsigned int pref_freq)
3543 if (persistent_group && wpa_s->conf->persistent_reconnect)
3544 persistent_group = 2;
3546 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3549 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3550 go_intent, own_interface_addr, force_freq,
3551 persistent_group, ssid ? ssid->ssid : NULL,
3552 ssid ? ssid->ssid_len : 0, pref_freq);
3556 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3558 wpa_s->p2p_join_scan_count++;
3559 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3560 wpa_s->p2p_join_scan_count);
3561 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3562 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3563 " for join operationg - stop join attempt",
3564 MAC2STR(wpa_s->pending_join_iface_addr));
3565 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3566 if (wpa_s->p2p_auto_pd) {
3567 wpa_s->p2p_auto_pd = 0;
3568 wpa_msg_global(wpa_s, MSG_INFO,
3569 P2P_EVENT_PROV_DISC_FAILURE
3570 " p2p_dev_addr=" MACSTR " status=N/A",
3571 MAC2STR(wpa_s->pending_join_dev_addr));
3574 wpa_msg_global(wpa_s->parent, MSG_INFO,
3575 P2P_EVENT_GROUP_FORMATION_FAILURE);
3580 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3582 int *freqs, res, num, i;
3584 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3585 /* Multiple channels are supported and not all are in use */
3589 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3593 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3594 wpa_s->num_multichan_concurrent);
3600 for (i = 0; i < num; i++) {
3601 if (freqs[i] == freq) {
3602 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3617 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3618 const u8 *peer_dev_addr)
3620 struct wpa_bss *bss;
3623 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3626 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3627 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3632 updated = os_time_before(&wpa_s->p2p_auto_started, &bss->last_update);
3633 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3634 "%ld.%06ld (%supdated in last scan)",
3635 bss->last_update.sec, bss->last_update.usec,
3636 updated ? "": "not ");
3642 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3643 struct wpa_scan_results *scan_res)
3645 struct wpa_bss *bss;
3647 u8 iface_addr[ETH_ALEN];
3649 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3651 if (wpa_s->global->p2p_disabled)
3654 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3655 scan_res ? (int) scan_res->num : -1,
3656 wpa_s->p2p_auto_join ? "auto_" : "");
3659 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3661 if (wpa_s->p2p_auto_pd) {
3662 int join = wpas_p2p_peer_go(wpa_s,
3663 wpa_s->pending_join_dev_addr);
3665 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3666 wpa_s->auto_pd_scan_retry++;
3667 bss = wpa_bss_get_bssid_latest(
3668 wpa_s, wpa_s->pending_join_dev_addr);
3671 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3672 "the peer " MACSTR " at %d MHz",
3673 wpa_s->auto_pd_scan_retry,
3675 pending_join_dev_addr),
3677 wpas_p2p_join_scan_req(wpa_s, freq);
3685 wpa_s->p2p_auto_pd = 0;
3686 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3687 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3688 MAC2STR(wpa_s->pending_join_dev_addr), join);
3689 if (p2p_prov_disc_req(wpa_s->global->p2p,
3690 wpa_s->pending_join_dev_addr,
3691 wpa_s->pending_pd_config_methods, join,
3692 0, wpa_s->user_initiated_pd) < 0) {
3693 wpa_s->p2p_auto_pd = 0;
3694 wpa_msg_global(wpa_s, MSG_INFO,
3695 P2P_EVENT_PROV_DISC_FAILURE
3696 " p2p_dev_addr=" MACSTR " status=N/A",
3697 MAC2STR(wpa_s->pending_join_dev_addr));
3702 if (wpa_s->p2p_auto_join) {
3703 int join = wpas_p2p_peer_go(wpa_s,
3704 wpa_s->pending_join_dev_addr);
3706 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3707 "running a GO -> use GO Negotiation");
3708 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3709 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3710 wpa_s->p2p_persistent_group, 0, 0, 0,
3711 wpa_s->p2p_go_intent,
3712 wpa_s->p2p_connect_freq,
3713 wpa_s->p2p_persistent_id,
3714 wpa_s->p2p_pd_before_go_neg,
3715 wpa_s->p2p_go_ht40);
3719 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3720 "try to join the group", join ? "" :
3723 wpa_s->p2p_fallback_to_go_neg = 1;
3726 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3727 wpa_s->pending_join_iface_addr);
3729 p2p_get_interface_addr(wpa_s->global->p2p,
3730 wpa_s->pending_join_dev_addr,
3732 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3734 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3735 "address for join from " MACSTR " to " MACSTR
3736 " based on newly discovered P2P peer entry",
3737 MAC2STR(wpa_s->pending_join_iface_addr),
3738 MAC2STR(iface_addr));
3739 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3742 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3743 wpa_s->pending_join_iface_addr);
3746 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3747 "from P2P peer table: %d MHz", freq);
3749 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
3752 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3753 "from BSS table: %d MHz (SSID %s)", freq,
3754 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3759 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3760 wpa_msg_global(wpa_s->parent, MSG_INFO,
3761 P2P_EVENT_GROUP_FORMATION_FAILURE
3762 "reason=FREQ_CONFLICT");
3766 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3767 "prior to joining an existing group (GO " MACSTR
3769 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3770 wpa_s->pending_pd_before_join = 1;
3772 switch (wpa_s->pending_join_wps_method) {
3773 case WPS_PIN_DISPLAY:
3774 method = WPS_CONFIG_KEYPAD;
3776 case WPS_PIN_KEYPAD:
3777 method = WPS_CONFIG_DISPLAY;
3780 method = WPS_CONFIG_PUSHBUTTON;
3787 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3788 wpa_s->pending_join_dev_addr) ==
3791 * We have already performed provision discovery for
3792 * joining the group. Proceed directly to join
3793 * operation without duplicated provision discovery. */
3794 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3795 "with " MACSTR " already done - proceed to "
3797 MAC2STR(wpa_s->pending_join_dev_addr));
3798 wpa_s->pending_pd_before_join = 0;
3802 if (p2p_prov_disc_req(wpa_s->global->p2p,
3803 wpa_s->pending_join_dev_addr, method, 1,
3804 freq, wpa_s->user_initiated_pd) < 0) {
3805 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3806 "Discovery Request before joining an "
3808 wpa_s->pending_pd_before_join = 0;
3814 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3815 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3816 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3817 wpas_p2p_check_join_scan_limit(wpa_s);
3821 /* Start join operation immediately */
3822 wpas_p2p_join_start(wpa_s);
3826 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3829 struct wpa_driver_scan_params params;
3830 struct wpabuf *wps_ie, *ies;
3832 int freqs[2] = { 0, 0 };
3834 os_memset(¶ms, 0, sizeof(params));
3836 /* P2P Wildcard SSID */
3837 params.num_ssids = 1;
3838 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3839 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3841 wpa_s->wps->dev.p2p = 1;
3842 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3843 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
3845 if (wps_ie == NULL) {
3846 wpas_p2p_scan_res_join(wpa_s, NULL);
3850 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
3851 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
3853 wpabuf_free(wps_ie);
3854 wpas_p2p_scan_res_join(wpa_s, NULL);
3857 wpabuf_put_buf(ies, wps_ie);
3858 wpabuf_free(wps_ie);
3860 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
3862 params.p2p_probe = 1;
3863 params.extra_ies = wpabuf_head(ies);
3864 params.extra_ies_len = wpabuf_len(ies);
3867 params.freqs = freqs;
3871 * Run a scan to update BSS table and start Provision Discovery once
3872 * the new scan results become available.
3874 ret = wpa_drv_scan(wpa_s, ¶ms);
3876 os_get_time(&wpa_s->scan_trigger_time);
3877 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
3883 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
3885 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3886 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3887 wpas_p2p_check_join_scan_limit(wpa_s);
3892 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
3894 struct wpa_supplicant *wpa_s = eloop_ctx;
3895 wpas_p2p_join_scan_req(wpa_s, 0);
3899 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
3900 const u8 *dev_addr, enum p2p_wps_method wps_method,
3903 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
3904 MACSTR " dev " MACSTR ")%s",
3905 MAC2STR(iface_addr), MAC2STR(dev_addr),
3906 auto_join ? " (auto_join)" : "");
3908 wpa_s->p2p_auto_pd = 0;
3909 wpa_s->p2p_auto_join = !!auto_join;
3910 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
3911 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
3912 wpa_s->pending_join_wps_method = wps_method;
3914 /* Make sure we are not running find during connection establishment */
3915 wpas_p2p_stop_find(wpa_s);
3917 wpa_s->p2p_join_scan_count = 0;
3918 wpas_p2p_join_scan(wpa_s, NULL);
3923 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
3925 struct wpa_supplicant *group;
3926 struct p2p_go_neg_results res;
3927 struct wpa_bss *bss;
3929 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
3932 if (group != wpa_s) {
3933 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
3934 sizeof(group->p2p_pin));
3935 group->p2p_wps_method = wpa_s->p2p_wps_method;
3938 * Need to mark the current interface for p2p_group_formation
3939 * when a separate group interface is not used. This is needed
3940 * to allow p2p_cancel stop a pending p2p_connect-join.
3941 * wpas_p2p_init_group_interface() addresses this for the case
3942 * where a separate group interface is used.
3944 wpa_s->global->p2p_group_formation = wpa_s;
3947 group->p2p_in_provisioning = 1;
3948 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
3950 os_memset(&res, 0, sizeof(res));
3951 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
3953 res.wps_method = wpa_s->pending_join_wps_method;
3954 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
3956 res.freq = bss->freq;
3957 res.ssid_len = bss->ssid_len;
3958 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
3959 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
3960 "from BSS table: %d MHz (SSID %s)", bss->freq,
3961 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3964 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
3965 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
3967 wpa_drv_cancel_remain_on_channel(wpa_s);
3968 wpa_s->off_channel_freq = 0;
3969 wpa_s->roc_waiting_drv_freq = 0;
3971 wpas_start_wps_enrollee(group, &res);
3974 * Allow a longer timeout for join-a-running-group than normal 15
3975 * second group formation timeout since the GO may not have authorized
3976 * our connection yet.
3978 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3979 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
3986 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
3987 int *force_freq, int *pref_freq)
3990 unsigned int freq_in_use = 0, num, i;
3992 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3996 num = get_shared_radio_freqs(wpa_s, freqs,
3997 wpa_s->num_multichan_concurrent);
3998 wpa_printf(MSG_DEBUG,
3999 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4000 freq, wpa_s->num_multichan_concurrent, num);
4003 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4004 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4005 "(%u MHz) is not supported for P2P uses",
4011 for (i = 0; i < num; i++) {
4012 if (freqs[i] == freq)
4016 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4017 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4022 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4023 "requested channel (%u MHz)", freq);
4028 for (i = 0; i < num; i++) {
4029 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4032 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4034 *force_freq = freqs[i];
4036 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
4037 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency we are already using");
4038 *pref_freq = *force_freq;
4044 if (num < wpa_s->num_multichan_concurrent) {
4045 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4048 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4063 * wpas_p2p_connect - Request P2P Group Formation to be started
4064 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4065 * @peer_addr: Address of the peer P2P Device
4066 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4067 * @persistent_group: Whether to create a persistent group
4068 * @auto_join: Whether to select join vs. GO Negotiation automatically
4069 * @join: Whether to join an existing group (as a client) instead of starting
4070 * Group Owner negotiation; @peer_addr is BSSID in that case
4071 * @auth: Whether to only authorize the connection instead of doing that and
4072 * initiating Group Owner negotiation
4073 * @go_intent: GO Intent or -1 to use default
4074 * @freq: Frequency for the group or 0 for auto-selection
4075 * @persistent_id: Persistent group credentials to use for forcing GO
4076 * parameters or -1 to generate new values (SSID/passphrase)
4077 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4078 * interoperability workaround when initiating group formation
4079 * @ht40: Start GO with 40 MHz channel width
4080 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4081 * failure, -2 on failure due to channel not currently available,
4082 * -3 if forced channel is not supported
4084 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4085 const char *pin, enum p2p_wps_method wps_method,
4086 int persistent_group, int auto_join, int join, int auth,
4087 int go_intent, int freq, int persistent_id, int pd,
4090 int force_freq = 0, pref_freq = 0;
4092 enum wpa_driver_if_type iftype;
4094 struct wpa_ssid *ssid = NULL;
4096 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4099 if (persistent_id >= 0) {
4100 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4101 if (ssid == NULL || ssid->disabled != 2 ||
4102 ssid->mode != WPAS_MODE_P2P_GO)
4106 os_free(wpa_s->global->add_psk);
4107 wpa_s->global->add_psk = NULL;
4110 go_intent = wpa_s->conf->p2p_go_intent;
4113 wpa_s->p2p_long_listen = 0;
4115 wpa_s->p2p_wps_method = wps_method;
4116 wpa_s->p2p_persistent_group = !!persistent_group;
4117 wpa_s->p2p_persistent_id = persistent_id;
4118 wpa_s->p2p_go_intent = go_intent;
4119 wpa_s->p2p_connect_freq = freq;
4120 wpa_s->p2p_fallback_to_go_neg = 0;
4121 wpa_s->p2p_pd_before_go_neg = !!pd;
4122 wpa_s->p2p_go_ht40 = !!ht40;
4125 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4126 else if (wps_method == WPS_PIN_DISPLAY) {
4127 ret = wps_generate_pin();
4128 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4130 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4133 wpa_s->p2p_pin[0] = '\0';
4135 if (join || auto_join) {
4136 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4138 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4139 "connect a running group from " MACSTR,
4140 MAC2STR(peer_addr));
4141 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4144 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4145 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4147 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4148 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4152 os_get_time(&wpa_s->p2p_auto_started);
4153 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4155 wpa_s->p2p_auto_started.sec,
4156 wpa_s->p2p_auto_started.usec);
4158 wpa_s->user_initiated_pd = 1;
4159 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4165 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
4168 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
4170 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4172 if (wpa_s->create_p2p_iface) {
4173 /* Prepare to add a new interface for the group */
4174 iftype = WPA_IF_P2P_GROUP;
4175 if (go_intent == 15)
4176 iftype = WPA_IF_P2P_GO;
4177 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4178 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4179 "interface for the group");
4183 if_addr = wpa_s->pending_interface_addr;
4185 if_addr = wpa_s->own_addr;
4188 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
4190 force_freq, persistent_group, ssid,
4196 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4197 go_intent, if_addr, force_freq,
4198 persistent_group, ssid, pref_freq) < 0) {
4199 if (wpa_s->create_p2p_iface)
4200 wpas_p2p_remove_pending_group_interface(wpa_s);
4208 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4209 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4210 * @freq: Frequency of the channel in MHz
4211 * @duration: Duration of the stay on the channel in milliseconds
4213 * This callback is called when the driver indicates that it has started the
4214 * requested remain-on-channel duration.
4216 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4217 unsigned int freq, unsigned int duration)
4219 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4221 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4222 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4223 wpa_s->pending_listen_duration);
4224 wpa_s->pending_listen_freq = 0;
4229 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4230 unsigned int timeout)
4232 /* Limit maximum Listen state time based on driver limitation. */
4233 if (timeout > wpa_s->max_remain_on_chan)
4234 timeout = wpa_s->max_remain_on_chan;
4236 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4237 return wpa_drv_p2p_listen(wpa_s, timeout);
4239 return p2p_listen(wpa_s->global->p2p, timeout);
4244 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4245 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4246 * @freq: Frequency of the channel in MHz
4248 * This callback is called when the driver indicates that a remain-on-channel
4249 * operation has been completed, i.e., the duration on the requested channel
4252 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4255 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4256 "(p2p_long_listen=%d ms pending_action_tx=%p)",
4257 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
4258 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4260 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4261 return; /* P2P module started a new operation */
4262 if (offchannel_pending_action_tx(wpa_s))
4264 if (wpa_s->p2p_long_listen > 0)
4265 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4266 if (wpa_s->p2p_long_listen > 0) {
4267 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4268 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4271 * When listen duration is over, stop listen & update p2p_state
4274 p2p_stop_listen(wpa_s->global->p2p);
4280 * wpas_p2p_group_remove - Remove a P2P group
4281 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4282 * @ifname: Network interface name of the group interface or "*" to remove all
4284 * Returns: 0 on success, -1 on failure
4286 * This function is used to remove a P2P group. This can be used to disconnect
4287 * from a group in which the local end is a P2P Client or to end a P2P Group in
4288 * case the local end is the Group Owner. If a virtual network interface was
4289 * created for this group, that interface will be removed. Otherwise, only the
4290 * configured P2P group network will be removed from the interface.
4292 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4294 struct wpa_global *global = wpa_s->global;
4296 if (os_strcmp(ifname, "*") == 0) {
4297 struct wpa_supplicant *prev;
4298 wpa_s = global->ifaces;
4301 wpa_s = wpa_s->next;
4302 if (prev->p2p_group_interface !=
4303 NOT_P2P_GROUP_INTERFACE ||
4304 (prev->current_ssid &&
4305 prev->current_ssid->p2p_group))
4306 wpas_p2p_disconnect(prev);
4311 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4312 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4316 return wpas_p2p_disconnect(wpa_s);
4320 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4325 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4327 if (wpa_s->best_24_freq > 0 &&
4328 p2p_supported_freq(wpa_s->global->p2p,
4329 wpa_s->best_24_freq)) {
4330 freq = wpa_s->best_24_freq;
4331 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4332 "channel: %d MHz", freq);
4334 os_get_random((u8 *) &r, sizeof(r));
4335 freq = 2412 + (r % 3) * 25;
4336 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4337 "channel: %d MHz", freq);
4342 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4344 if (wpa_s->best_5_freq > 0 &&
4345 p2p_supported_freq(wpa_s->global->p2p,
4346 wpa_s->best_5_freq)) {
4347 freq = wpa_s->best_5_freq;
4348 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4349 "channel: %d MHz", freq);
4351 os_get_random((u8 *) &r, sizeof(r));
4352 freq = 5180 + (r % 4) * 20;
4353 if (!p2p_supported_freq(wpa_s->global->p2p, freq)) {
4354 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4355 "5 GHz channel for P2P group");
4358 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4359 "channel: %d MHz", freq);
4363 if (freq > 0 && !p2p_supported_freq(wpa_s->global->p2p, freq)) {
4364 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4365 "(%u MHz) is not supported for P2P uses",
4374 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4375 struct p2p_go_neg_results *params,
4377 const struct p2p_channels *channels)
4380 unsigned int pref_freq;
4381 unsigned int num, i;
4383 os_memset(params, 0, sizeof(*params));
4384 params->role_go = 1;
4385 params->ht40 = ht40;
4387 if (!freq_included(channels, freq)) {
4388 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4392 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4393 "frequency %d MHz", freq);
4394 params->freq = freq;
4395 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4396 wpa_s->conf->p2p_oper_channel >= 1 &&
4397 wpa_s->conf->p2p_oper_channel <= 11 &&
4398 freq_included(channels,
4399 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
4400 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4401 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4402 "frequency %d MHz", params->freq);
4403 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4404 wpa_s->conf->p2p_oper_reg_class == 116 ||
4405 wpa_s->conf->p2p_oper_reg_class == 117 ||
4406 wpa_s->conf->p2p_oper_reg_class == 124 ||
4407 wpa_s->conf->p2p_oper_reg_class == 126 ||
4408 wpa_s->conf->p2p_oper_reg_class == 127) &&
4409 freq_included(channels,
4410 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
4411 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4412 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4413 "frequency %d MHz", params->freq);
4414 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4415 wpa_s->best_overall_freq > 0 &&
4416 p2p_supported_freq(wpa_s->global->p2p,
4417 wpa_s->best_overall_freq) &&
4418 freq_included(channels, wpa_s->best_overall_freq)) {
4419 params->freq = wpa_s->best_overall_freq;
4420 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4421 "channel %d MHz", params->freq);
4422 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4423 wpa_s->best_24_freq > 0 &&
4424 p2p_supported_freq(wpa_s->global->p2p,
4425 wpa_s->best_24_freq) &&
4426 freq_included(channels, wpa_s->best_24_freq)) {
4427 params->freq = wpa_s->best_24_freq;
4428 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4429 "channel %d MHz", params->freq);
4430 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4431 wpa_s->best_5_freq > 0 &&
4432 p2p_supported_freq(wpa_s->global->p2p,
4433 wpa_s->best_5_freq) &&
4434 freq_included(channels, wpa_s->best_5_freq)) {
4435 params->freq = wpa_s->best_5_freq;
4436 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4437 "channel %d MHz", params->freq);
4438 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4440 params->freq = pref_freq;
4441 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4442 "channels", params->freq);
4445 for (chan = 0; chan < 11; chan++) {
4446 params->freq = 2412 + chan * 5;
4447 if (!wpas_p2p_disallowed_freq(wpa_s->global,
4449 freq_included(channels, params->freq))
4453 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4457 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4458 "known)", params->freq);
4461 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4465 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4466 wpa_s->num_multichan_concurrent);
4474 for (i = 0; i < num; i++) {
4475 if (freq_included(channels, freqs[i])) {
4476 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4478 params->freq = freqs[i];
4484 if (num == wpa_s->num_multichan_concurrent) {
4485 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4489 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4493 for (i = 0; i < num; i++) {
4494 if (freqs[i] == freq)
4499 if (num == wpa_s->num_multichan_concurrent) {
4500 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4504 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4513 static struct wpa_supplicant *
4514 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4517 struct wpa_supplicant *group_wpa_s;
4519 if (!wpas_p2p_create_iface(wpa_s)) {
4520 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4522 wpa_s->p2p_first_connection_timeout = 0;
4526 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
4527 WPA_IF_P2P_CLIENT) < 0) {
4528 wpa_msg_global(wpa_s, MSG_ERROR,
4529 "P2P: Failed to add group interface");
4532 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4533 if (group_wpa_s == NULL) {
4534 wpa_msg_global(wpa_s, MSG_ERROR,
4535 "P2P: Failed to initialize group interface");
4536 wpas_p2p_remove_pending_group_interface(wpa_s);
4540 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4541 group_wpa_s->ifname);
4542 group_wpa_s->p2p_first_connection_timeout = 0;
4548 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4549 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4550 * @persistent_group: Whether to create a persistent group
4551 * @freq: Frequency for the group or 0 to indicate no hardcoding
4552 * Returns: 0 on success, -1 on failure
4554 * This function creates a new P2P group with the local end as the Group Owner,
4555 * i.e., without using Group Owner Negotiation.
4557 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
4560 struct p2p_go_neg_results params;
4562 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4565 os_free(wpa_s->global->add_psk);
4566 wpa_s->global->add_psk = NULL;
4568 /* Make sure we are not running find during connection establishment */
4569 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
4570 wpas_p2p_stop_find_oper(wpa_s);
4572 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4576 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, NULL))
4579 !p2p_supported_freq(wpa_s->global->p2p, params.freq)) {
4580 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4581 "(%u MHz) is not supported for P2P uses",
4585 p2p_go_params(wpa_s->global->p2p, ¶ms);
4586 params.persistent_group = persistent_group;
4588 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4591 wpas_start_wps_go(wpa_s, ¶ms, 0);
4597 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4598 struct wpa_ssid *params, int addr_allocated)
4600 struct wpa_ssid *ssid;
4602 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4605 wpa_s->p2p_last_4way_hs_fail = NULL;
4607 wpa_supplicant_ap_deinit(wpa_s);
4609 ssid = wpa_config_add_network(wpa_s->conf);
4612 wpa_config_set_network_defaults(ssid);
4613 ssid->temporary = 1;
4614 ssid->proto = WPA_PROTO_RSN;
4615 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4616 ssid->group_cipher = WPA_CIPHER_CCMP;
4617 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4618 ssid->ssid = os_malloc(params->ssid_len);
4619 if (ssid->ssid == NULL) {
4620 wpa_config_remove_network(wpa_s->conf, ssid->id);
4623 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4624 ssid->ssid_len = params->ssid_len;
4625 ssid->p2p_group = 1;
4626 ssid->export_keys = 1;
4627 if (params->psk_set) {
4628 os_memcpy(ssid->psk, params->psk, 32);
4631 if (params->passphrase)
4632 ssid->passphrase = os_strdup(params->passphrase);
4634 wpa_supplicant_select_network(wpa_s, ssid);
4636 wpa_s->show_group_started = 1;
4642 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4643 struct wpa_ssid *ssid, int addr_allocated,
4645 const struct p2p_channels *channels,
4646 int connection_timeout)
4648 struct p2p_go_neg_results params;
4651 if (ssid->disabled != 2 || ssid->ssid == NULL)
4654 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4655 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4656 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4661 os_free(wpa_s->global->add_psk);
4662 wpa_s->global->add_psk = NULL;
4664 /* Make sure we are not running find during connection establishment */
4665 wpas_p2p_stop_find_oper(wpa_s);
4667 wpa_s->p2p_fallback_to_go_neg = 0;
4669 if (ssid->mode == WPAS_MODE_INFRA)
4670 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4672 if (ssid->mode != WPAS_MODE_P2P_GO)
4675 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4679 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, channels))
4683 params.psk_set = ssid->psk_set;
4685 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
4686 if (ssid->passphrase) {
4687 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4688 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4689 "persistent group");
4692 os_strlcpy(params.passphrase, ssid->passphrase,
4693 sizeof(params.passphrase));
4695 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4696 params.ssid_len = ssid->ssid_len;
4697 params.persistent_group = 1;
4699 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4703 wpa_s->p2p_first_connection_timeout = connection_timeout;
4704 wpas_start_wps_go(wpa_s, ¶ms, 0);
4710 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4711 struct wpabuf *proberesp_ies)
4713 struct wpa_supplicant *wpa_s = ctx;
4714 if (wpa_s->ap_iface) {
4715 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
4716 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4717 wpabuf_free(beacon_ies);
4718 wpabuf_free(proberesp_ies);
4722 wpabuf_free(hapd->p2p_beacon_ie);
4723 hapd->p2p_beacon_ie = beacon_ies;
4725 wpabuf_free(hapd->p2p_probe_resp_ie);
4726 hapd->p2p_probe_resp_ie = proberesp_ies;
4728 wpabuf_free(beacon_ies);
4729 wpabuf_free(proberesp_ies);
4731 wpa_supplicant_ap_update_beacon(wpa_s);
4735 static void wpas_p2p_idle_update(void *ctx, int idle)
4737 struct wpa_supplicant *wpa_s = ctx;
4738 if (!wpa_s->ap_iface)
4740 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4742 wpas_p2p_set_group_idle_timeout(wpa_s);
4744 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4748 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
4749 struct wpa_ssid *ssid)
4751 struct p2p_group *group;
4752 struct p2p_group_config *cfg;
4754 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4756 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4759 cfg = os_zalloc(sizeof(*cfg));
4763 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4764 cfg->persistent_group = 2;
4765 else if (ssid->p2p_persistent_group)
4766 cfg->persistent_group = 1;
4767 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4768 if (wpa_s->max_stations &&
4769 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4770 cfg->max_clients = wpa_s->max_stations;
4772 cfg->max_clients = wpa_s->conf->max_num_sta;
4773 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4774 cfg->ssid_len = ssid->ssid_len;
4775 cfg->cb_ctx = wpa_s;
4776 cfg->ie_update = wpas_p2p_ie_update;
4777 cfg->idle_update = wpas_p2p_idle_update;
4779 group = p2p_group_init(wpa_s->global->p2p, cfg);
4782 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4783 p2p_group_notif_formation_done(group);
4784 wpa_s->p2p_group = group;
4789 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4792 struct wpa_ssid *ssid = wpa_s->current_ssid;
4794 if (!wpa_s->p2p_in_provisioning) {
4795 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4796 "provisioning not in progress");
4800 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4801 u8 go_dev_addr[ETH_ALEN];
4802 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4803 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4805 /* Clear any stored provisioning info */
4806 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4809 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4811 wpa_s->p2p_go_group_formation_completed = 1;
4812 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4814 * Use a separate timeout for initial data connection to
4815 * complete to allow the group to be removed automatically if
4816 * something goes wrong in this step before the P2P group idle
4817 * timeout mechanism is taken into use.
4819 wpa_dbg(wpa_s, MSG_DEBUG,
4820 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
4821 P2P_MAX_INITIAL_CONN_WAIT);
4822 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4823 wpas_p2p_group_formation_timeout,
4824 wpa_s->parent, NULL);
4827 * Use a separate timeout for initial data connection to
4828 * complete to allow the group to be removed automatically if
4829 * the client does not complete data connection successfully.
4831 wpa_dbg(wpa_s, MSG_DEBUG,
4832 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
4833 P2P_MAX_INITIAL_CONN_WAIT_GO);
4834 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
4835 wpas_p2p_group_formation_timeout,
4836 wpa_s->parent, NULL);
4838 * Complete group formation on first successful data connection
4840 wpa_s->p2p_go_group_formation_completed = 0;
4842 if (wpa_s->global->p2p)
4843 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
4844 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4845 wpa_drv_wps_success_cb(wpa_s, peer_addr);
4846 wpas_group_formation_completed(wpa_s, 1);
4850 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
4851 struct wps_event_fail *fail)
4853 if (!wpa_s->p2p_in_provisioning) {
4854 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
4855 "provisioning not in progress");
4859 if (wpa_s->go_params) {
4860 p2p_clear_provisioning_info(
4862 wpa_s->go_params->peer_device_addr);
4865 wpas_notify_p2p_wps_failed(wpa_s, fail);
4869 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4870 const char *config_method,
4871 enum wpas_p2p_prov_disc_use use)
4875 wpa_s->p2p_fallback_to_go_neg = 0;
4876 wpa_s->pending_pd_use = NORMAL_PD;
4877 if (os_strncmp(config_method, "display", 7) == 0)
4878 config_methods = WPS_CONFIG_DISPLAY;
4879 else if (os_strncmp(config_method, "keypad", 6) == 0)
4880 config_methods = WPS_CONFIG_KEYPAD;
4881 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
4882 os_strncmp(config_method, "pushbutton", 10) == 0)
4883 config_methods = WPS_CONFIG_PUSHBUTTON;
4885 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
4889 if (use == WPAS_P2P_PD_AUTO) {
4890 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
4891 wpa_s->pending_pd_config_methods = config_methods;
4892 wpa_s->p2p_auto_pd = 1;
4893 wpa_s->p2p_auto_join = 0;
4894 wpa_s->pending_pd_before_join = 0;
4895 wpa_s->auto_pd_scan_retry = 0;
4896 wpas_p2p_stop_find(wpa_s);
4897 wpa_s->p2p_join_scan_count = 0;
4898 os_get_time(&wpa_s->p2p_auto_started);
4899 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
4900 wpa_s->p2p_auto_started.sec,
4901 wpa_s->p2p_auto_started.usec);
4902 wpas_p2p_join_scan(wpa_s, NULL);
4906 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4907 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
4909 use == WPAS_P2P_PD_FOR_JOIN);
4912 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
4915 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
4916 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
4921 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
4924 return p2p_scan_result_text(ies, ies_len, buf, end);
4928 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
4930 if (!offchannel_pending_action_tx(wpa_s))
4933 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
4934 "operation request");
4935 offchannel_clear_pending_action_tx(wpa_s);
4939 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
4940 enum p2p_discovery_type type,
4941 unsigned int num_req_dev_types, const u8 *req_dev_types,
4942 const u8 *dev_id, unsigned int search_delay)
4944 wpas_p2p_clear_pending_action_tx(wpa_s);
4945 wpa_s->p2p_long_listen = 0;
4947 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4948 return wpa_drv_p2p_find(wpa_s, timeout, type);
4950 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
4951 wpa_s->p2p_in_provisioning)
4954 wpa_supplicant_cancel_sched_scan(wpa_s);
4956 return p2p_find(wpa_s->global->p2p, timeout, type,
4957 num_req_dev_types, req_dev_types, dev_id,
4962 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
4964 wpas_p2p_clear_pending_action_tx(wpa_s);
4965 wpa_s->p2p_long_listen = 0;
4966 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4967 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4968 wpa_s->global->p2p_cb_on_scan_complete = 0;
4970 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
4971 wpa_drv_p2p_stop_find(wpa_s);
4975 if (wpa_s->global->p2p)
4976 p2p_stop_find(wpa_s->global->p2p);
4982 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
4984 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
4986 wpas_p2p_remove_pending_group_interface(wpa_s);
4990 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
4992 struct wpa_supplicant *wpa_s = eloop_ctx;
4993 wpa_s->p2p_long_listen = 0;
4997 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5001 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5004 wpa_supplicant_cancel_sched_scan(wpa_s);
5005 wpas_p2p_clear_pending_action_tx(wpa_s);
5009 * This is a request for unlimited Listen state. However, at
5010 * least for now, this is mapped to a Listen state for one
5015 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5016 wpa_s->p2p_long_listen = 0;
5019 * Stop previous find/listen operation to avoid trying to request a new
5020 * remain-on-channel operation while the driver is still running the
5023 if (wpa_s->global->p2p)
5024 p2p_stop_find(wpa_s->global->p2p);
5026 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5027 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5028 wpa_s->p2p_long_listen = timeout * 1000;
5029 eloop_register_timeout(timeout, 0,
5030 wpas_p2p_long_listen_timeout,
5038 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5039 u8 *buf, size_t len, int p2p_group)
5041 struct wpabuf *p2p_ie;
5044 if (wpa_s->global->p2p_disabled)
5046 if (wpa_s->global->p2p == NULL)
5051 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5052 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5054 wpabuf_free(p2p_ie);
5060 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
5061 const u8 *dst, const u8 *bssid,
5062 const u8 *ie, size_t ie_len, int ssi_signal)
5064 if (wpa_s->global->p2p_disabled)
5066 if (wpa_s->global->p2p == NULL)
5069 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5071 case P2P_PREQ_NOT_P2P:
5072 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5075 case P2P_PREQ_MALFORMED:
5076 case P2P_PREQ_NOT_LISTEN:
5077 case P2P_PREQ_NOT_PROCESSED:
5078 default: /* make gcc happy */
5080 case P2P_PREQ_PROCESSED:
5086 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5087 const u8 *sa, const u8 *bssid,
5088 u8 category, const u8 *data, size_t len, int freq)
5090 if (wpa_s->global->p2p_disabled)
5092 if (wpa_s->global->p2p == NULL)
5095 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5100 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5102 if (wpa_s->global->p2p_disabled)
5104 if (wpa_s->global->p2p == NULL)
5107 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5111 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5113 p2p_group_deinit(wpa_s->p2p_group);
5114 wpa_s->p2p_group = NULL;
5116 wpa_s->ap_configured_cb = NULL;
5117 wpa_s->ap_configured_cb_ctx = NULL;
5118 wpa_s->ap_configured_cb_data = NULL;
5119 wpa_s->connect_without_scan = NULL;
5123 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5125 wpa_s->p2p_long_listen = 0;
5127 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5128 return wpa_drv_p2p_reject(wpa_s, addr);
5130 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5133 return p2p_reject(wpa_s->global->p2p, addr);
5137 /* Invite to reinvoke a persistent group */
5138 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5139 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
5140 int ht40, int pref_freq)
5142 enum p2p_invite_role role;
5147 wpa_s->global->p2p_invite_group = NULL;
5149 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5151 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5153 wpa_s->p2p_persistent_go_freq = freq;
5154 wpa_s->p2p_go_ht40 = !!ht40;
5155 if (ssid->mode == WPAS_MODE_P2P_GO) {
5156 role = P2P_INVITE_ROLE_GO;
5157 if (peer_addr == NULL) {
5158 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5159 "address in invitation command");
5162 if (wpas_p2p_create_iface(wpa_s)) {
5163 if (wpas_p2p_add_group_interface(wpa_s,
5164 WPA_IF_P2P_GO) < 0) {
5165 wpa_printf(MSG_ERROR, "P2P: Failed to "
5166 "allocate a new interface for the "
5170 bssid = wpa_s->pending_interface_addr;
5172 bssid = wpa_s->own_addr;
5174 role = P2P_INVITE_ROLE_CLIENT;
5175 peer_addr = ssid->bssid;
5177 wpa_s->pending_invite_ssid_id = ssid->id;
5179 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
5183 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5184 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5185 ssid->ssid, ssid->ssid_len,
5188 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5191 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5192 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5197 /* Invite to join an active group */
5198 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5199 const u8 *peer_addr, const u8 *go_dev_addr)
5201 struct wpa_global *global = wpa_s->global;
5202 enum p2p_invite_role role;
5204 struct wpa_ssid *ssid;
5206 int freq = 0, force_freq = 0, pref_freq = 0;
5209 wpa_s->p2p_persistent_go_freq = 0;
5210 wpa_s->p2p_go_ht40 = 0;
5212 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5213 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5216 if (wpa_s == NULL) {
5217 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5221 ssid = wpa_s->current_ssid;
5223 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5228 wpa_s->global->p2p_invite_group = wpa_s;
5229 persistent = ssid->p2p_persistent_group &&
5230 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5231 ssid->ssid, ssid->ssid_len);
5233 if (ssid->mode == WPAS_MODE_P2P_GO) {
5234 role = P2P_INVITE_ROLE_ACTIVE_GO;
5235 bssid = wpa_s->own_addr;
5236 if (go_dev_addr == NULL)
5237 go_dev_addr = wpa_s->global->p2p_dev_addr;
5238 freq = ssid->frequency;
5240 role = P2P_INVITE_ROLE_CLIENT;
5241 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5242 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5243 "invite to current group");
5246 bssid = wpa_s->bssid;
5247 if (go_dev_addr == NULL &&
5248 !is_zero_ether_addr(wpa_s->go_dev_addr))
5249 go_dev_addr = wpa_s->go_dev_addr;
5250 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5251 (int) wpa_s->assoc_freq;
5253 wpa_s->parent->pending_invite_ssid_id = -1;
5255 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5256 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5257 ssid->ssid, ssid->ssid_len,
5258 go_dev_addr, persistent);
5260 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5263 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq);
5266 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
5268 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5269 ssid->ssid, ssid->ssid_len, force_freq,
5270 go_dev_addr, persistent, pref_freq);
5274 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5276 struct wpa_ssid *ssid = wpa_s->current_ssid;
5277 const char *ssid_txt;
5278 u8 go_dev_addr[ETH_ALEN];
5279 int network_id = -1;
5283 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5284 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5285 wpa_s->parent, NULL);
5288 if (!wpa_s->show_group_started || !ssid)
5291 wpa_s->show_group_started = 0;
5293 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5294 os_memset(go_dev_addr, 0, ETH_ALEN);
5295 if (ssid->bssid_set)
5296 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5297 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5299 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5301 if (wpa_s->global->p2p_group_formation == wpa_s)
5302 wpa_s->global->p2p_group_formation = NULL;
5304 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5305 (int) wpa_s->assoc_freq;
5306 if (ssid->passphrase == NULL && ssid->psk_set) {
5308 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5309 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5310 "%s client ssid=\"%s\" freq=%d psk=%s "
5311 "go_dev_addr=" MACSTR "%s",
5312 wpa_s->ifname, ssid_txt, freq, psk,
5313 MAC2STR(go_dev_addr),
5314 persistent ? " [PERSISTENT]" : "");
5316 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5317 "%s client ssid=\"%s\" freq=%d "
5318 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5319 wpa_s->ifname, ssid_txt, freq,
5320 ssid->passphrase ? ssid->passphrase : "",
5321 MAC2STR(go_dev_addr),
5322 persistent ? " [PERSISTENT]" : "");
5326 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5329 network_id = ssid->id;
5330 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
5333 wpas_p2p_continue_after_scan(wpa_s);
5337 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5338 u32 interval1, u32 duration2, u32 interval2)
5340 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5342 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5345 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5346 wpa_s->current_ssid == NULL ||
5347 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5350 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5351 wpa_s->own_addr, wpa_s->assoc_freq,
5352 duration1, interval1, duration2, interval2);
5356 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5357 unsigned int interval)
5359 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5362 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5365 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5369 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5371 if (wpa_s->current_ssid == NULL) {
5373 * current_ssid can be cleared when P2P client interface gets
5374 * disconnected, so assume this interface was used as P2P
5379 return wpa_s->current_ssid->p2p_group &&
5380 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5384 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5386 struct wpa_supplicant *wpa_s = eloop_ctx;
5388 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
5389 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5394 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5396 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
5400 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5404 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5405 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5407 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5410 timeout = wpa_s->conf->p2p_group_idle;
5411 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5412 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5413 timeout = P2P_MAX_CLIENT_IDLE;
5419 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5420 timeout = 0; /* special client mode no-timeout */
5425 if (wpa_s->p2p_in_provisioning) {
5427 * Use the normal group formation timeout during the
5428 * provisioning phase to avoid terminating this process too
5429 * early due to group idle timeout.
5431 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5432 "during provisioning");
5436 if (wpa_s->show_group_started) {
5438 * Use the normal group formation timeout between the end of
5439 * the provisioning phase and completion of 4-way handshake to
5440 * avoid terminating this process too early due to group idle
5443 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5444 "while waiting for initial 4-way handshake to "
5449 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
5451 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5456 /* Returns 1 if the interface was removed */
5457 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5458 u16 reason_code, const u8 *ie, size_t ie_len,
5459 int locally_generated)
5461 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5463 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5466 if (!locally_generated)
5467 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5470 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5471 wpa_s->current_ssid &&
5472 wpa_s->current_ssid->p2p_group &&
5473 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5474 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5475 "session is ending");
5476 if (wpas_p2p_group_delete(wpa_s,
5477 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5486 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5487 u16 reason_code, const u8 *ie, size_t ie_len,
5488 int locally_generated)
5490 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5492 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5495 if (!locally_generated)
5496 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5501 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5503 struct p2p_data *p2p = wpa_s->global->p2p;
5508 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5511 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5512 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5514 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5515 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5518 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5519 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5521 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5522 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5524 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5525 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5526 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5527 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5528 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5531 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5532 p2p_set_sec_dev_types(p2p,
5533 (void *) wpa_s->conf->sec_device_type,
5534 wpa_s->conf->num_sec_device_types);
5536 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5538 p2p_remove_wps_vendor_extensions(p2p);
5539 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5540 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5542 p2p_add_wps_vendor_extension(
5543 p2p, wpa_s->conf->wps_vendor_ext[i]);
5547 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5548 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5550 country[0] = wpa_s->conf->country[0];
5551 country[1] = wpa_s->conf->country[1];
5553 p2p_set_country(p2p, country);
5556 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5557 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5558 wpa_s->conf->p2p_ssid_postfix ?
5559 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5563 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5564 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
5566 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5567 u8 reg_class, channel;
5570 if (wpa_s->conf->p2p_listen_reg_class &&
5571 wpa_s->conf->p2p_listen_channel) {
5572 reg_class = wpa_s->conf->p2p_listen_reg_class;
5573 channel = wpa_s->conf->p2p_listen_channel;
5577 * Pick one of the social channels randomly as the
5580 os_get_random((u8 *) &r, sizeof(r));
5581 channel = 1 + (r % 3) * 5;
5583 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5585 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5588 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5589 u8 op_reg_class, op_channel, cfg_op_channel;
5592 if (wpa_s->conf->p2p_oper_reg_class &&
5593 wpa_s->conf->p2p_oper_channel) {
5594 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5595 op_channel = wpa_s->conf->p2p_oper_channel;
5600 * Use random operation channel from (1, 6, 11)
5601 *if no other preference is indicated.
5603 os_get_random((u8 *) &r, sizeof(r));
5604 op_channel = 1 + (r % 3) * 5;
5607 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5610 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5614 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5615 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5616 wpa_s->conf->p2p_pref_chan) < 0) {
5617 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5624 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5627 if (!wpa_s->ap_iface)
5629 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5634 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5636 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5638 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5641 wpa_s->global->cross_connection = enabled;
5642 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5645 struct wpa_supplicant *iface;
5647 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5649 if (iface->cross_connect_enabled == 0)
5652 iface->cross_connect_enabled = 0;
5653 iface->cross_connect_in_use = 0;
5654 wpa_msg_global(iface->parent, MSG_INFO,
5655 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5657 iface->cross_connect_uplink);
5665 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5667 struct wpa_supplicant *iface;
5669 if (!uplink->global->cross_connection)
5672 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5673 if (!iface->cross_connect_enabled)
5675 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5678 if (iface->ap_iface == NULL)
5680 if (iface->cross_connect_in_use)
5683 iface->cross_connect_in_use = 1;
5684 wpa_msg_global(iface->parent, MSG_INFO,
5685 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5686 iface->ifname, iface->cross_connect_uplink);
5691 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5693 struct wpa_supplicant *iface;
5695 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5696 if (!iface->cross_connect_enabled)
5698 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5701 if (!iface->cross_connect_in_use)
5704 wpa_msg_global(iface->parent, MSG_INFO,
5705 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5706 iface->ifname, iface->cross_connect_uplink);
5707 iface->cross_connect_in_use = 0;
5712 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5714 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5715 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5716 wpa_s->cross_connect_disallowed)
5717 wpas_p2p_disable_cross_connect(wpa_s);
5719 wpas_p2p_enable_cross_connect(wpa_s);
5720 if (!wpa_s->ap_iface &&
5721 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5722 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5726 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5728 wpas_p2p_disable_cross_connect(wpa_s);
5729 if (!wpa_s->ap_iface &&
5730 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5732 wpas_p2p_set_group_idle_timeout(wpa_s);
5736 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5738 struct wpa_supplicant *iface;
5740 if (!wpa_s->global->cross_connection)
5743 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5746 if (iface->drv_flags &
5747 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5749 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5752 wpa_s->cross_connect_enabled = 1;
5753 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5754 sizeof(wpa_s->cross_connect_uplink));
5755 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5756 "%s to %s whenever uplink is available",
5757 wpa_s->ifname, wpa_s->cross_connect_uplink);
5759 if (iface->ap_iface || iface->current_ssid == NULL ||
5760 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5761 iface->cross_connect_disallowed ||
5762 iface->wpa_state != WPA_COMPLETED)
5765 wpa_s->cross_connect_in_use = 1;
5766 wpa_msg_global(wpa_s->parent, MSG_INFO,
5767 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5768 wpa_s->ifname, wpa_s->cross_connect_uplink);
5774 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5776 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5777 !wpa_s->p2p_in_provisioning)
5778 return 0; /* not P2P client operation */
5780 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5782 if (wpa_s != wpa_s->parent)
5783 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5784 wpas_p2p_group_formation_failed(wpa_s);
5789 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5791 struct p2p_channels chan;
5793 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5796 os_memset(&chan, 0, sizeof(chan));
5797 if (wpas_p2p_setup_channels(wpa_s, &chan)) {
5798 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5803 p2p_update_channel_list(wpa_s->global->p2p, &chan);
5807 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5808 struct wpa_scan_results *scan_res)
5810 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5814 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5816 struct wpa_global *global = wpa_s->global;
5820 if (global->p2p == NULL)
5823 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
5825 if (wpa_s->pending_interface_name[0] &&
5826 !is_zero_ether_addr(wpa_s->pending_interface_addr))
5829 peer = p2p_get_go_neg_peer(global->p2p);
5831 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
5832 MACSTR, MAC2STR(peer));
5833 p2p_unauthorize(global->p2p, peer);
5837 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
5838 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
5839 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
5843 if (wpa_s->pending_pd_before_join) {
5844 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
5845 wpa_s->pending_pd_before_join = 0;
5849 wpas_p2p_stop_find(wpa_s);
5851 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5852 if (wpa_s == global->p2p_group_formation &&
5853 (wpa_s->p2p_in_provisioning ||
5854 wpa_s->parent->pending_interface_type ==
5855 WPA_IF_P2P_CLIENT)) {
5856 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
5857 "formation found - cancelling",
5860 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5861 wpa_s->parent, NULL);
5862 if (wpa_s->p2p_in_provisioning) {
5863 wpas_group_formation_completed(wpa_s, 0);
5866 wpas_p2p_group_delete(wpa_s,
5867 P2P_GROUP_REMOVAL_REQUESTED);
5873 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
5881 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
5883 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5886 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
5887 "being available anymore");
5888 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
5892 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
5893 int freq_24, int freq_5, int freq_overall)
5895 struct p2p_data *p2p = wpa_s->global->p2p;
5896 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5898 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
5902 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
5905 struct p2p_data *p2p = wpa_s->global->p2p;
5907 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
5910 if (hwaddr_aton(addr, peer))
5913 return p2p_unauthorize(p2p, peer);
5918 * wpas_p2p_disconnect - Disconnect from a P2P Group
5919 * @wpa_s: Pointer to wpa_supplicant data
5920 * Returns: 0 on success, -1 on failure
5922 * This can be used to disconnect from a group in which the local end is a P2P
5923 * Client or to end a P2P Group in case the local end is the Group Owner. If a
5924 * virtual network interface was created for this group, that interface will be
5925 * removed. Otherwise, only the configured P2P group network will be removed
5926 * from the interface.
5928 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
5934 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
5939 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
5943 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5946 ret = p2p_in_progress(wpa_s->global->p2p);
5949 * Check whether there is an ongoing WPS provisioning step (or
5950 * other parts of group formation) on another interface since
5951 * p2p_in_progress() does not report this to avoid issues for
5952 * scans during such provisioning step.
5954 if (wpa_s->global->p2p_group_formation &&
5955 wpa_s->global->p2p_group_formation != wpa_s) {
5956 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
5957 "in group formation",
5958 wpa_s->global->p2p_group_formation->ifname);
5963 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
5966 if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
5967 P2P_MAX_INITIAL_CONN_WAIT_GO) {
5968 /* Wait for the first client has expired */
5969 wpa_s->global->p2p_go_wait_client.sec = 0;
5971 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
5980 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
5981 struct wpa_ssid *ssid)
5983 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
5984 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5985 wpa_s->parent, NULL) > 0) {
5987 * Remove the network by scheduling the group formation
5988 * timeout to happen immediately. The teardown code
5989 * needs to be scheduled to run asynch later so that we
5990 * don't delete data from under ourselves unexpectedly.
5991 * Calling wpas_p2p_group_formation_timeout directly
5992 * causes a series of crashes in WPS failure scenarios.
5994 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
5995 "P2P group network getting removed");
5996 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
5997 wpa_s->parent, NULL);
6002 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
6003 const u8 *addr, const u8 *ssid,
6009 for (s = wpa_s->conf->ssid; s; s = s->next) {
6010 if (s->disabled != 2)
6013 (ssid_len != s->ssid_len ||
6014 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6017 if (s->mode == WPAS_MODE_P2P_GO)
6021 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6022 return s; /* peer is GO in the persistent group */
6023 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6025 for (i = 0; i < s->num_p2p_clients; i++) {
6026 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6027 addr, ETH_ALEN) == 0)
6028 return s; /* peer is P2P client in persistent
6037 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6040 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6041 wpa_s->parent, NULL) > 0) {
6043 * This can happen if WPS provisioning step is not terminated
6044 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6045 * peer was able to connect, there is no need to time out group
6046 * formation after this, though. In addition, this is used with
6047 * the initial connection wait on the GO as a separate formation
6048 * timeout and as such, expected to be hit after the initial WPS
6049 * provisioning step.
6051 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6053 if (!wpa_s->p2p_go_group_formation_completed) {
6054 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6055 wpa_s->p2p_go_group_formation_completed = 1;
6056 wpa_s->global->p2p_group_formation = NULL;
6057 wpa_s->p2p_in_provisioning = 0;
6059 wpa_s->global->p2p_go_wait_client.sec = 0;
6062 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6066 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6069 struct wpa_supplicant *group = wpa_s;
6070 if (wpa_s->global->p2p_group_formation)
6071 group = wpa_s->global->p2p_group_formation;
6072 wpa_s = wpa_s->parent;
6073 offchannel_send_action_done(wpa_s);
6075 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
6076 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6077 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6078 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6079 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6080 wpa_s->p2p_persistent_id,
6081 wpa_s->p2p_pd_before_go_neg,
6082 wpa_s->p2p_go_ht40);
6086 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6088 if (!wpa_s->p2p_fallback_to_go_neg ||
6089 wpa_s->p2p_in_provisioning <= 5)
6092 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6093 return 0; /* peer operating as a GO */
6095 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6096 "fallback to GO Negotiation");
6097 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6103 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6105 const char *rn, *rn2;
6106 struct wpa_supplicant *ifs;
6108 if (wpa_s->wpa_state > WPA_SCANNING) {
6109 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6110 "concurrent operation",
6111 P2P_CONCURRENT_SEARCH_DELAY);
6112 return P2P_CONCURRENT_SEARCH_DELAY;
6115 if (!wpa_s->driver->get_radio_name)
6117 rn = wpa_s->driver->get_radio_name(wpa_s->drv_priv);
6118 if (rn == NULL || rn[0] == '\0')
6121 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6122 if (ifs == wpa_s || !ifs->driver->get_radio_name)
6125 rn2 = ifs->driver->get_radio_name(ifs->drv_priv);
6126 if (!rn2 || os_strcmp(rn, rn2) != 0)
6128 if (ifs->wpa_state > WPA_SCANNING) {
6129 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6130 "delay due to concurrent operation on "
6132 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6133 return P2P_CONCURRENT_SEARCH_DELAY;
6141 void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
6143 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
6144 "pending anymore (sta_scan_pending=%d "
6145 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
6146 wpa_s->global->p2p_cb_on_scan_complete);
6147 wpa_s->sta_scan_pending = 0;
6149 if (!wpa_s->global->p2p_cb_on_scan_complete)
6151 wpa_s->global->p2p_cb_on_scan_complete = 0;
6153 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6156 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6157 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6158 "continued after successful connection");
6159 p2p_increase_search_delay(wpa_s->global->p2p,
6160 wpas_p2p_search_delay(wpa_s));
6165 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6166 struct wpa_ssid *s, const u8 *addr,
6169 struct psk_list_entry *psk, *tmp;
6172 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6174 if ((iface_addr && !psk->p2p &&
6175 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6176 (!iface_addr && psk->p2p &&
6177 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6178 wpa_dbg(wpa_s, MSG_DEBUG,
6179 "P2P: Remove persistent group PSK list entry for "
6181 MAC2STR(psk->addr), psk->p2p);
6182 dl_list_del(&psk->list);
6192 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6193 const u8 *p2p_dev_addr,
6194 const u8 *psk, size_t psk_len)
6196 struct wpa_ssid *ssid = wpa_s->current_ssid;
6197 struct wpa_ssid *persistent;
6198 struct psk_list_entry *p;
6200 if (psk_len != sizeof(p->psk))
6204 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6205 " p2p_dev_addr=" MACSTR,
6206 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6207 if (is_zero_ether_addr(p2p_dev_addr))
6208 p2p_dev_addr = NULL;
6210 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6214 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6215 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6216 /* To be added to persistent group once created */
6217 if (wpa_s->global->add_psk == NULL) {
6218 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6219 if (wpa_s->global->add_psk == NULL)
6222 p = wpa_s->global->add_psk;
6225 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6228 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6230 os_memcpy(p->psk, psk, psk_len);
6234 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6235 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6239 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6242 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6246 p = os_zalloc(sizeof(*p));
6251 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6254 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6256 os_memcpy(p->psk, psk, psk_len);
6258 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6259 struct psk_list_entry *last;
6260 last = dl_list_last(&persistent->psk_list,
6261 struct psk_list_entry, list);
6262 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6263 MACSTR " (p2p=%u) to make room for a new one",
6264 MAC2STR(last->addr), last->p2p);
6265 dl_list_del(&last->list);
6269 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6270 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6271 p2p_dev_addr == NULL);
6273 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6274 MACSTR, MAC2STR(p2p_dev_addr));
6276 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6279 dl_list_add(&persistent->psk_list, &p->list);
6281 #ifndef CONFIG_NO_CONFIG_WRITE
6282 if (wpa_s->parent->conf->update_config &&
6283 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6284 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6285 #endif /* CONFIG_NO_CONFIG_WRITE */
6289 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6290 struct wpa_ssid *s, const u8 *addr,
6295 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6297 #ifndef CONFIG_NO_CONFIG_WRITE
6298 if (wpa_s->conf->update_config &&
6299 wpa_config_write(wpa_s->confname, wpa_s->conf))
6300 wpa_dbg(wpa_s, MSG_DEBUG,
6301 "P2P: Failed to update configuration");
6302 #endif /* CONFIG_NO_CONFIG_WRITE */
6307 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6308 const u8 *peer, int iface_addr)
6310 struct hostapd_data *hapd;
6311 struct hostapd_wpa_psk *psk, *prev, *rem;
6312 struct sta_info *sta;
6314 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6315 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6318 /* Remove per-station PSK entry */
6319 hapd = wpa_s->ap_iface->bss[0];
6321 psk = hapd->conf->ssid.wpa_psk;
6323 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6325 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6326 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6327 MACSTR " iface_addr=%d",
6328 MAC2STR(peer), iface_addr);
6330 prev->next = psk->next;
6332 hapd->conf->ssid.wpa_psk = psk->next;
6342 /* Disconnect from group */
6344 sta = ap_get_sta(hapd, peer);
6346 sta = ap_get_sta_p2p(hapd, peer);
6348 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6349 " (iface_addr=%d) from group",
6350 MAC2STR(peer), iface_addr);
6351 hostapd_drv_sta_deauth(hapd, sta->addr,
6352 WLAN_REASON_DEAUTH_LEAVING);
6353 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6358 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6362 struct wpa_supplicant *w;
6364 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6366 /* Remove from any persistent group */
6367 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6368 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6371 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6372 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6375 /* Remove from any operating group */
6376 for (w = wpa_s->global->ifaces; w; w = w->next)
6377 wpas_p2p_remove_client_go(w, peer, iface_addr);
6381 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6383 struct wpa_supplicant *wpa_s = eloop_ctx;
6384 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6388 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6390 struct wpa_ssid *ssid = wpa_s->current_ssid;
6392 if (ssid == NULL || !ssid->p2p_group)
6395 if (wpa_s->p2p_last_4way_hs_fail &&
6396 wpa_s->p2p_last_4way_hs_fail == ssid) {
6397 u8 go_dev_addr[ETH_ALEN];
6398 struct wpa_ssid *persistent;
6400 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6402 ssid->ssid_len) <= 0) {
6403 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6407 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6408 MACSTR, MAC2STR(go_dev_addr));
6409 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6412 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6413 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6416 wpa_msg_global(wpa_s->parent, MSG_INFO,
6417 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6420 wpa_s->p2p_last_4way_hs_fail = NULL;
6422 * Remove the group from a timeout to avoid issues with caller
6423 * continuing to use the interface if this is on a P2P group
6426 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6431 wpa_s->p2p_last_4way_hs_fail = ssid;