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"
36 #include "wifi_display.h"
40 * How many times to try to scan to find the GO before giving up on join
43 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
45 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
47 #ifndef P2P_MAX_CLIENT_IDLE
49 * How many seconds to try to reconnect to the GO when connection in P2P client
52 #define P2P_MAX_CLIENT_IDLE 10
53 #endif /* P2P_MAX_CLIENT_IDLE */
55 #ifndef P2P_MAX_INITIAL_CONN_WAIT
57 * How many seconds to wait for initial 4-way handshake to get completed after
58 * WPS provisioning step.
60 #define P2P_MAX_INITIAL_CONN_WAIT 10
61 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
63 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
65 * How many seconds to wait for initial 4-way handshake to get completed after
66 * WPS provisioning step on the GO. This controls the extra time the P2P
67 * operation is considered to be in progress (e.g., to delay other scans) after
68 * WPS provisioning has been completed on the GO during group formation.
70 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
71 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
73 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
75 * How many seconds to wait for initial 4-way handshake to get completed after
76 * re-invocation of a persistent group on the GO when the client is expected
77 * to connect automatically (no user interaction).
79 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
80 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
82 #ifndef P2P_CONCURRENT_SEARCH_DELAY
83 #define P2P_CONCURRENT_SEARCH_DELAY 500
84 #endif /* P2P_CONCURRENT_SEARCH_DELAY */
86 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
88 enum p2p_group_removal_reason {
89 P2P_GROUP_REMOVAL_UNKNOWN,
90 P2P_GROUP_REMOVAL_SILENT,
91 P2P_GROUP_REMOVAL_FORMATION_FAILED,
92 P2P_GROUP_REMOVAL_REQUESTED,
93 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
94 P2P_GROUP_REMOVAL_UNAVAILABLE,
95 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
96 P2P_GROUP_REMOVAL_PSK_FAILURE,
97 P2P_GROUP_REMOVAL_FREQ_CONFLICT
101 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
102 static struct wpa_supplicant *
103 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
105 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s);
106 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq);
107 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
108 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
109 const u8 *dev_addr, enum p2p_wps_method wps_method,
111 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
112 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
113 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
114 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
115 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
117 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
118 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
120 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
124 * Get the number of concurrent channels that the HW can operate, but that are
125 * currently not in use by any of the wpa_supplicant interfaces.
127 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
132 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
136 num = get_shared_radio_freqs(wpa_s, freqs,
137 wpa_s->num_multichan_concurrent);
140 unused = wpa_s->num_multichan_concurrent - num;
141 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
147 * Get the frequencies that are currently in use by one or more of the virtual
148 * interfaces, and that are also valid for P2P operation.
150 static int wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
151 int *p2p_freqs, unsigned int len)
154 unsigned int num, i, j;
156 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
160 num = get_shared_radio_freqs(wpa_s, freqs,
161 wpa_s->num_multichan_concurrent);
163 os_memset(p2p_freqs, 0, sizeof(int) * len);
165 for (i = 0, j = 0; i < num && j < len; i++) {
166 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
167 p2p_freqs[j++] = freqs[i];
172 dump_freq_array(wpa_s, "valid for P2P", p2p_freqs, j);
178 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
181 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
183 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
184 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
185 wpas_p2p_num_unused_channels(wpa_s) > 0) {
186 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
190 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
194 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
195 struct wpa_scan_results *scan_res)
199 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
202 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
203 (int) scan_res->num);
205 for (i = 0; i < scan_res->num; i++) {
206 struct wpa_scan_res *bss = scan_res->res[i];
207 struct os_reltime time_tmp_age, entry_ts;
211 time_tmp_age.sec = bss->age / 1000;
212 time_tmp_age.usec = (bss->age % 1000) * 1000;
213 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
215 ies = (const u8 *) (bss + 1);
216 ies_len = bss->ie_len;
217 if (bss->beacon_ie_len > 0 &&
218 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
219 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
220 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
221 MACSTR, MAC2STR(bss->bssid));
223 ies_len = bss->beacon_ie_len;
227 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
228 bss->freq, &entry_ts, bss->level,
233 p2p_scan_res_handled(wpa_s->global->p2p);
237 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
238 unsigned int num_req_dev_types,
239 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
241 struct wpa_supplicant *wpa_s = ctx;
242 struct wpa_supplicant *ifs;
243 struct wpa_driver_scan_params params;
245 struct wpabuf *wps_ie, *ies;
246 int social_channels[] = { 2412, 2437, 2462, 0, 0 };
249 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
252 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
253 if (ifs->sta_scan_pending &&
254 (wpas_scan_scheduled(ifs) || ifs->scanning) &&
255 wpas_p2p_in_progress(wpa_s) == 2) {
256 wpa_printf(MSG_DEBUG, "Delaying P2P scan to allow "
257 "pending station mode scan to be "
258 "completed on interface %s", ifs->ifname);
259 wpa_s->global->p2p_cb_on_scan_complete = 1;
260 wpa_supplicant_req_scan(ifs, 0, 0);
265 os_memset(¶ms, 0, sizeof(params));
267 /* P2P Wildcard SSID */
268 params.num_ssids = 1;
269 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
270 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
272 wpa_s->wps->dev.p2p = 1;
273 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
274 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
275 num_req_dev_types, req_dev_types);
279 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
280 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
285 wpabuf_put_buf(ies, wps_ie);
288 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
290 params.p2p_probe = 1;
291 params.extra_ies = wpabuf_head(ies);
292 params.extra_ies_len = wpabuf_len(ies);
295 case P2P_SCAN_SOCIAL:
296 params.freqs = social_channels;
300 case P2P_SCAN_SOCIAL_PLUS_ONE:
301 social_channels[3] = freq;
302 params.freqs = social_channels;
306 ret = wpa_drv_scan(wpa_s, ¶ms);
311 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
313 ifs->scan_res_handler == wpas_p2p_scan_res_handler) {
314 wpa_s->global->p2p_cb_on_scan_complete = 1;
320 os_get_reltime(&wpa_s->scan_trigger_time);
321 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
322 wpa_s->own_scan_requested = 1;
329 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
331 switch (p2p_group_interface) {
332 case P2P_GROUP_INTERFACE_PENDING:
333 return WPA_IF_P2P_GROUP;
334 case P2P_GROUP_INTERFACE_GO:
335 return WPA_IF_P2P_GO;
336 case P2P_GROUP_INTERFACE_CLIENT:
337 return WPA_IF_P2P_CLIENT;
340 return WPA_IF_P2P_GROUP;
344 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
346 size_t ssid_len, int *go)
350 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
351 for (s = wpa_s->conf->ssid; s; s = s->next) {
352 if (s->disabled != 0 || !s->p2p_group ||
353 s->ssid_len != ssid_len ||
354 os_memcmp(ssid, s->ssid, ssid_len) != 0)
356 if (s->mode == WPAS_MODE_P2P_GO &&
357 s != wpa_s->current_ssid)
360 *go = s->mode == WPAS_MODE_P2P_GO;
369 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
370 enum p2p_group_removal_reason removal_reason)
372 struct wpa_ssid *ssid;
376 ssid = wpa_s->current_ssid;
379 * The current SSID was not known, but there may still be a
380 * pending P2P group interface waiting for provisioning or a
381 * P2P group that is trying to reconnect.
383 ssid = wpa_s->conf->ssid;
385 if (ssid->p2p_group && ssid->disabled != 2)
390 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
392 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
397 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
399 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
400 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
401 wpa_s->reassociate = 0;
402 wpa_s->disconnected = 1;
403 wpa_supplicant_deauthenticate(wpa_s,
404 WLAN_REASON_DEAUTH_LEAVING);
408 if (wpa_s->cross_connect_in_use) {
409 wpa_s->cross_connect_in_use = 0;
410 wpa_msg_global(wpa_s->parent, MSG_INFO,
411 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
412 wpa_s->ifname, wpa_s->cross_connect_uplink);
414 switch (removal_reason) {
415 case P2P_GROUP_REMOVAL_REQUESTED:
416 reason = " reason=REQUESTED";
418 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
419 reason = " reason=FORMATION_FAILED";
421 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
422 reason = " reason=IDLE";
424 case P2P_GROUP_REMOVAL_UNAVAILABLE:
425 reason = " reason=UNAVAILABLE";
427 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
428 reason = " reason=GO_ENDING_SESSION";
430 case P2P_GROUP_REMOVAL_PSK_FAILURE:
431 reason = " reason=PSK_FAILURE";
433 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
434 reason = " reason=FREQ_CONFLICT";
440 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
441 wpa_msg_global(wpa_s->parent, MSG_INFO,
442 P2P_EVENT_GROUP_REMOVED "%s %s%s",
443 wpa_s->ifname, gtype, reason);
446 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
447 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
448 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
449 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
450 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
451 wpa_s->parent, NULL) > 0) {
452 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
454 wpa_s->p2p_in_provisioning = 0;
458 * Make sure wait for the first client does not remain active after the
459 * group has been removed.
461 wpa_s->global->p2p_go_wait_client.sec = 0;
463 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
464 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
466 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
467 struct wpa_global *global;
469 enum wpa_driver_if_type type;
470 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
472 global = wpa_s->global;
473 ifname = os_strdup(wpa_s->ifname);
474 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
475 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
476 wpa_s = global->ifaces;
478 wpa_drv_if_remove(wpa_s, type, ifname);
483 if (!wpa_s->p2p_go_group_formation_completed) {
484 wpa_s->global->p2p_group_formation = NULL;
485 wpa_s->p2p_in_provisioning = 0;
488 wpa_s->show_group_started = 0;
489 os_free(wpa_s->go_params);
490 wpa_s->go_params = NULL;
492 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
493 if (ssid && (ssid->p2p_group ||
494 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
495 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
497 if (ssid == wpa_s->current_ssid) {
498 wpa_sm_set_config(wpa_s->wpa, NULL);
499 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
500 wpa_s->current_ssid = NULL;
503 * Networks objects created during any P2P activities are not
504 * exposed out as they might/will confuse certain non-P2P aware
505 * applications since these network objects won't behave like
508 * Likewise, we don't send out network removed signals for such
511 wpa_config_remove_network(wpa_s->conf, id);
512 wpa_supplicant_clear_status(wpa_s);
513 wpa_supplicant_cancel_sched_scan(wpa_s);
514 wpa_s->sta_scan_pending = 0;
516 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
520 wpa_supplicant_ap_deinit(wpa_s);
522 wpa_drv_deinit_p2p_cli(wpa_s);
528 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
530 const u8 *ssid, size_t ssid_len)
538 if (wpa_s->go_params)
539 bssid = wpa_s->go_params->peer_interface_addr;
541 bssid = wpa_s->bssid;
543 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
545 u8 iface_addr[ETH_ALEN];
546 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
548 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
551 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
552 "group is persistent - BSS " MACSTR " not found",
557 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
559 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
562 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
563 "group is persistent - BSS " MACSTR
564 " did not include P2P IE", MAC2STR(bssid));
565 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
566 (u8 *) (bss + 1), bss->ie_len);
567 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
568 ((u8 *) bss + 1) + bss->ie_len,
573 group_capab = p2p_get_group_capab(p2p);
574 addr = p2p_get_go_dev_addr(p2p);
575 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
576 "group_capab=0x%x", group_capab);
578 os_memcpy(go_dev_addr, addr, ETH_ALEN);
579 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
582 os_memset(go_dev_addr, 0, ETH_ALEN);
585 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
586 "go_dev_addr=" MACSTR,
587 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
589 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
593 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
594 struct wpa_ssid *ssid,
595 const u8 *go_dev_addr)
600 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
601 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
602 for (s = wpa_s->conf->ssid; s; s = s->next) {
603 if (s->disabled == 2 &&
604 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
605 s->ssid_len == ssid->ssid_len &&
606 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
611 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
613 if (ssid->passphrase && !s->passphrase)
615 else if (ssid->passphrase && s->passphrase &&
616 os_strcmp(ssid->passphrase, s->passphrase) != 0)
619 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
622 s = wpa_config_add_network(wpa_s->conf);
627 * Instead of network_added we emit persistent_group_added
628 * notification. Also to keep the defense checks in
629 * persistent_group obj registration method, we set the
630 * relevant flags in s to designate it as a persistent group.
633 s->p2p_persistent_group = 1;
634 wpas_notify_persistent_group_added(wpa_s, s);
635 wpa_config_set_network_defaults(s);
639 s->p2p_persistent_group = 1;
642 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
643 s->mode = ssid->mode;
644 s->auth_alg = WPA_AUTH_ALG_OPEN;
645 s->key_mgmt = WPA_KEY_MGMT_PSK;
646 s->proto = WPA_PROTO_RSN;
647 s->pairwise_cipher = WPA_CIPHER_CCMP;
649 if (ssid->passphrase) {
650 os_free(s->passphrase);
651 s->passphrase = os_strdup(ssid->passphrase);
655 os_memcpy(s->psk, ssid->psk, 32);
657 if (s->passphrase && !s->psk_set)
658 wpa_config_update_psk(s);
659 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
661 s->ssid = os_malloc(ssid->ssid_len);
664 s->ssid_len = ssid->ssid_len;
665 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
667 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
668 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
669 wpa_s->global->add_psk = NULL;
673 #ifndef CONFIG_NO_CONFIG_WRITE
674 if (changed && wpa_s->conf->update_config &&
675 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
676 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
678 #endif /* CONFIG_NO_CONFIG_WRITE */
684 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
687 struct wpa_ssid *ssid, *s;
692 ssid = wpa_s->current_ssid;
693 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
694 !ssid->p2p_persistent_group)
697 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
698 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
701 if (s->ssid_len == ssid->ssid_len &&
702 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
709 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
710 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
714 if (i == s->num_p2p_clients - 1)
715 return; /* already the most recent entry */
717 /* move the entry to mark it most recent */
718 os_memmove(s->p2p_client_list + i * ETH_ALEN,
719 s->p2p_client_list + (i + 1) * ETH_ALEN,
720 (s->num_p2p_clients - i - 1) * ETH_ALEN);
721 os_memcpy(s->p2p_client_list +
722 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
727 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
728 n = os_realloc_array(s->p2p_client_list,
729 s->num_p2p_clients + 1, ETH_ALEN);
732 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
733 s->p2p_client_list = n;
734 s->num_p2p_clients++;
736 /* Not enough room for an additional entry - drop the oldest
738 os_memmove(s->p2p_client_list,
739 s->p2p_client_list + ETH_ALEN,
740 (s->num_p2p_clients - 1) * ETH_ALEN);
741 os_memcpy(s->p2p_client_list +
742 (s->num_p2p_clients - 1) * ETH_ALEN,
746 #ifndef CONFIG_NO_CONFIG_WRITE
747 if (wpa_s->parent->conf->update_config &&
748 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
749 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
750 #endif /* CONFIG_NO_CONFIG_WRITE */
754 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
757 struct wpa_ssid *ssid;
758 const char *ssid_txt;
761 u8 go_dev_addr[ETH_ALEN];
765 * This callback is likely called for the main interface. Update wpa_s
766 * to use the group interface if a new interface was created for the
769 if (wpa_s->global->p2p_group_formation)
770 wpa_s = wpa_s->global->p2p_group_formation;
771 if (wpa_s->p2p_go_group_formation_completed) {
772 wpa_s->global->p2p_group_formation = NULL;
773 wpa_s->p2p_in_provisioning = 0;
777 wpa_msg_global(wpa_s->parent, MSG_INFO,
778 P2P_EVENT_GROUP_FORMATION_FAILURE);
779 wpas_p2p_group_delete(wpa_s,
780 P2P_GROUP_REMOVAL_FORMATION_FAILED);
784 wpa_msg_global(wpa_s->parent, MSG_INFO,
785 P2P_EVENT_GROUP_FORMATION_SUCCESS);
787 ssid = wpa_s->current_ssid;
788 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
789 ssid->mode = WPAS_MODE_P2P_GO;
790 p2p_group_notif_formation_done(wpa_s->p2p_group);
791 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
796 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
797 client = ssid->mode == WPAS_MODE_INFRA;
798 if (ssid->mode == WPAS_MODE_P2P_GO) {
799 persistent = ssid->p2p_persistent_group;
800 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
803 persistent = wpas_p2p_persistent_group(wpa_s,
809 client = wpa_s->p2p_group_interface ==
810 P2P_GROUP_INTERFACE_CLIENT;
811 os_memset(go_dev_addr, 0, ETH_ALEN);
814 wpa_s->show_group_started = 0;
817 * Indicate event only after successfully completed 4-way
818 * handshake, i.e., when the interface is ready for data
821 wpa_s->show_group_started = 1;
822 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
824 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
825 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
826 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
828 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
829 MAC2STR(go_dev_addr),
830 persistent ? " [PERSISTENT]" : "");
831 wpas_p2p_cross_connect_setup(wpa_s);
832 wpas_p2p_set_group_idle_timeout(wpa_s);
834 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
835 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
836 "go_dev_addr=" MACSTR "%s",
837 wpa_s->ifname, ssid_txt,
838 ssid ? ssid->frequency : 0,
839 ssid && ssid->passphrase ? ssid->passphrase : "",
840 MAC2STR(go_dev_addr),
841 persistent ? " [PERSISTENT]" : "");
842 wpas_p2p_cross_connect_setup(wpa_s);
843 wpas_p2p_set_group_idle_timeout(wpa_s);
847 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
850 os_free(wpa_s->global->add_psk);
851 wpa_s->global->add_psk = NULL;
853 if (network_id < 0 && ssid)
854 network_id = ssid->id;
856 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
857 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
862 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
864 const u8 *dst, const u8 *src,
866 const u8 *data, size_t data_len,
867 enum offchannel_send_action_result
870 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
872 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
874 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
878 case OFFCHANNEL_SEND_ACTION_SUCCESS:
879 res = P2P_SEND_ACTION_SUCCESS;
881 case OFFCHANNEL_SEND_ACTION_NO_ACK:
882 res = P2P_SEND_ACTION_NO_ACK;
884 case OFFCHANNEL_SEND_ACTION_FAILED:
885 res = P2P_SEND_ACTION_FAILED;
889 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
891 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
892 wpa_s->pending_pd_before_join &&
893 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
894 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
895 wpa_s->p2p_fallback_to_go_neg) {
896 wpa_s->pending_pd_before_join = 0;
897 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
898 "during p2p_connect-auto");
899 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
905 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
906 const u8 *src, const u8 *bssid, const u8 *buf,
907 size_t len, unsigned int wait_time)
909 struct wpa_supplicant *wpa_s = ctx;
910 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
912 wpas_p2p_send_action_tx_status, 1);
916 static void wpas_send_action_done(void *ctx)
918 struct wpa_supplicant *wpa_s = ctx;
919 offchannel_send_action_done(wpa_s);
923 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
924 struct p2p_go_neg_results *params)
926 if (wpa_s->go_params == NULL) {
927 wpa_s->go_params = os_malloc(sizeof(*params));
928 if (wpa_s->go_params == NULL)
931 os_memcpy(wpa_s->go_params, params, sizeof(*params));
936 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
937 struct p2p_go_neg_results *res)
939 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
940 MAC2STR(res->peer_interface_addr));
941 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
942 res->ssid, res->ssid_len);
943 wpa_supplicant_ap_deinit(wpa_s);
944 wpas_copy_go_neg_results(wpa_s, res);
945 if (res->wps_method == WPS_PBC)
946 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
948 u16 dev_pw_id = DEV_PW_DEFAULT;
949 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
950 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
951 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
952 wpa_s->p2p_pin, 1, dev_pw_id);
957 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
958 struct wpa_ssid *ssid)
960 struct wpa_ssid *persistent;
961 struct psk_list_entry *psk;
962 struct hostapd_data *hapd;
964 if (!wpa_s->ap_iface)
967 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
969 if (persistent == NULL)
972 hapd = wpa_s->ap_iface->bss[0];
974 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
976 struct hostapd_wpa_psk *hpsk;
978 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
980 MAC2STR(psk->addr), psk->p2p);
981 hpsk = os_zalloc(sizeof(*hpsk));
984 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
986 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
988 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
989 hpsk->next = hapd->conf->ssid.wpa_psk;
990 hapd->conf->ssid.wpa_psk = hpsk;
995 static void p2p_go_configured(void *ctx, void *data)
997 struct wpa_supplicant *wpa_s = ctx;
998 struct p2p_go_neg_results *params = data;
999 struct wpa_ssid *ssid;
1000 int network_id = -1;
1002 ssid = wpa_s->current_ssid;
1003 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1004 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1005 if (wpa_s->global->p2p_group_formation == wpa_s)
1006 wpa_s->global->p2p_group_formation = NULL;
1007 if (os_strlen(params->passphrase) > 0) {
1008 wpa_msg_global(wpa_s->parent, MSG_INFO,
1009 P2P_EVENT_GROUP_STARTED
1010 "%s GO ssid=\"%s\" freq=%d "
1011 "passphrase=\"%s\" go_dev_addr=" MACSTR
1012 "%s", wpa_s->ifname,
1013 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1014 ssid->frequency, params->passphrase,
1015 MAC2STR(wpa_s->global->p2p_dev_addr),
1016 params->persistent_group ?
1017 " [PERSISTENT]" : "");
1020 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1021 sizeof(params->psk));
1022 wpa_msg_global(wpa_s->parent, MSG_INFO,
1023 P2P_EVENT_GROUP_STARTED
1024 "%s GO ssid=\"%s\" freq=%d psk=%s "
1025 "go_dev_addr=" MACSTR "%s",
1027 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1028 ssid->frequency, psk,
1029 MAC2STR(wpa_s->global->p2p_dev_addr),
1030 params->persistent_group ?
1031 " [PERSISTENT]" : "");
1034 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1035 if (params->persistent_group) {
1036 network_id = wpas_p2p_store_persistent_group(
1037 wpa_s->parent, ssid,
1038 wpa_s->global->p2p_dev_addr);
1039 wpas_p2p_add_psk_list(wpa_s, ssid);
1042 network_id = ssid->id;
1043 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1044 wpas_p2p_cross_connect_setup(wpa_s);
1045 wpas_p2p_set_group_idle_timeout(wpa_s);
1047 if (wpa_s->p2p_first_connection_timeout) {
1048 wpa_dbg(wpa_s, MSG_DEBUG,
1049 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1050 wpa_s->p2p_first_connection_timeout);
1051 wpa_s->p2p_go_group_formation_completed = 0;
1052 wpa_s->global->p2p_group_formation = wpa_s;
1053 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1054 wpa_s->parent, NULL);
1055 eloop_register_timeout(
1056 wpa_s->p2p_first_connection_timeout, 0,
1057 wpas_p2p_group_formation_timeout,
1058 wpa_s->parent, NULL);
1064 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1065 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1066 params->peer_interface_addr)) {
1067 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1071 if (params->wps_method == WPS_PBC)
1072 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1073 params->peer_device_addr);
1074 else if (wpa_s->p2p_pin[0])
1075 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1076 wpa_s->p2p_pin, NULL, 0, 0);
1077 os_free(wpa_s->go_params);
1078 wpa_s->go_params = NULL;
1082 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1083 struct p2p_go_neg_results *params,
1084 int group_formation)
1086 struct wpa_ssid *ssid;
1088 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1089 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1090 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1095 ssid = wpa_config_add_network(wpa_s->conf);
1097 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1101 wpa_s->show_group_started = 0;
1103 wpa_config_set_network_defaults(ssid);
1104 ssid->temporary = 1;
1105 ssid->p2p_group = 1;
1106 ssid->p2p_persistent_group = params->persistent_group;
1107 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1109 ssid->frequency = params->freq;
1110 ssid->ht40 = params->ht40;
1111 ssid->vht = params->vht;
1112 ssid->ssid = os_zalloc(params->ssid_len + 1);
1114 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1115 ssid->ssid_len = params->ssid_len;
1117 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1118 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1119 ssid->proto = WPA_PROTO_RSN;
1120 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1121 if (os_strlen(params->passphrase) > 0) {
1122 ssid->passphrase = os_strdup(params->passphrase);
1123 if (ssid->passphrase == NULL) {
1124 wpa_msg_global(wpa_s, MSG_ERROR,
1125 "P2P: Failed to copy passphrase for GO");
1126 wpa_config_remove_network(wpa_s->conf, ssid->id);
1130 ssid->passphrase = NULL;
1131 ssid->psk_set = params->psk_set;
1133 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1134 else if (ssid->passphrase)
1135 wpa_config_update_psk(ssid);
1136 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1138 wpa_s->ap_configured_cb = p2p_go_configured;
1139 wpa_s->ap_configured_cb_ctx = wpa_s;
1140 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1141 wpa_s->connect_without_scan = ssid;
1142 wpa_s->reassociate = 1;
1143 wpa_s->disconnected = 0;
1144 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1146 wpa_supplicant_req_scan(wpa_s, 0, 0);
1150 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1151 const struct wpa_supplicant *src)
1153 struct wpa_config *d;
1154 const struct wpa_config *s;
1159 #define C(n) if (s->n) d->n = os_strdup(s->n)
1168 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1169 os_memcpy(d->sec_device_type, s->sec_device_type,
1170 sizeof(d->sec_device_type));
1171 d->num_sec_device_types = s->num_sec_device_types;
1173 d->p2p_group_idle = s->p2p_group_idle;
1174 d->p2p_intra_bss = s->p2p_intra_bss;
1175 d->persistent_reconnect = s->persistent_reconnect;
1176 d->max_num_sta = s->max_num_sta;
1177 d->pbc_in_m1 = s->pbc_in_m1;
1178 d->ignore_old_scan_res = s->ignore_old_scan_res;
1179 d->beacon_int = s->beacon_int;
1180 d->disassoc_low_ack = s->disassoc_low_ack;
1181 d->disable_scan_offload = s->disable_scan_offload;
1185 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1186 char *ifname, size_t len)
1188 char *ifname_ptr = wpa_s->ifname;
1190 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1191 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1192 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1195 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1196 if (os_strlen(ifname) >= IFNAMSIZ &&
1197 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1198 /* Try to avoid going over the IFNAMSIZ length limit */
1199 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1204 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1205 enum wpa_driver_if_type type)
1207 char ifname[120], force_ifname[120];
1209 if (wpa_s->pending_interface_name[0]) {
1210 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1211 "- skip creation of a new one");
1212 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1213 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1214 "unknown?! ifname='%s'",
1215 wpa_s->pending_interface_name);
1221 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1222 force_ifname[0] = '\0';
1224 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1226 wpa_s->p2p_group_idx++;
1228 wpa_s->pending_interface_type = type;
1229 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1230 wpa_s->pending_interface_addr, NULL) < 0) {
1231 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1236 if (force_ifname[0]) {
1237 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1239 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1240 sizeof(wpa_s->pending_interface_name));
1242 os_strlcpy(wpa_s->pending_interface_name, ifname,
1243 sizeof(wpa_s->pending_interface_name));
1244 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1245 MACSTR, wpa_s->pending_interface_name,
1246 MAC2STR(wpa_s->pending_interface_addr));
1252 static void wpas_p2p_remove_pending_group_interface(
1253 struct wpa_supplicant *wpa_s)
1255 if (!wpa_s->pending_interface_name[0] ||
1256 is_zero_ether_addr(wpa_s->pending_interface_addr))
1257 return; /* No pending virtual interface */
1259 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1260 wpa_s->pending_interface_name);
1261 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1262 wpa_s->pending_interface_name);
1263 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1264 wpa_s->pending_interface_name[0] = '\0';
1268 static struct wpa_supplicant *
1269 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1271 struct wpa_interface iface;
1272 struct wpa_supplicant *group_wpa_s;
1274 if (!wpa_s->pending_interface_name[0]) {
1275 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1276 if (!wpas_p2p_create_iface(wpa_s))
1279 * Something has forced us to remove the pending interface; try
1280 * to create a new one and hope for the best that we will get
1281 * the same local address.
1283 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1284 WPA_IF_P2P_CLIENT) < 0)
1288 os_memset(&iface, 0, sizeof(iface));
1289 iface.ifname = wpa_s->pending_interface_name;
1290 iface.driver = wpa_s->driver->name;
1291 if (wpa_s->conf->ctrl_interface == NULL &&
1292 wpa_s->parent != wpa_s &&
1294 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1295 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1297 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1298 iface.driver_param = wpa_s->conf->driver_param;
1299 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1300 if (group_wpa_s == NULL) {
1301 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1302 "wpa_supplicant interface");
1305 wpa_s->pending_interface_name[0] = '\0';
1306 group_wpa_s->parent = wpa_s;
1307 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1308 P2P_GROUP_INTERFACE_CLIENT;
1309 wpa_s->global->p2p_group_formation = group_wpa_s;
1311 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1317 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1320 struct wpa_supplicant *wpa_s = eloop_ctx;
1321 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1322 wpas_p2p_group_formation_failed(wpa_s);
1326 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1328 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1329 wpa_s->parent, NULL);
1330 if (wpa_s->global->p2p)
1331 p2p_group_formation_failed(wpa_s->global->p2p);
1332 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1333 wpa_drv_p2p_group_formation_failed(wpa_s);
1334 wpas_group_formation_completed(wpa_s, 0);
1338 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1340 if (wpa_s->global->p2p_group_formation != wpa_s)
1342 /* Speed up group formation timeout since this cannot succeed */
1343 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1344 wpa_s->parent, NULL);
1345 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1346 wpa_s->parent, NULL);
1350 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1352 struct wpa_supplicant *wpa_s = ctx;
1354 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1355 wpa_drv_cancel_remain_on_channel(wpa_s);
1356 wpa_s->off_channel_freq = 0;
1357 wpa_s->roc_waiting_drv_freq = 0;
1361 wpa_msg_global(wpa_s, MSG_INFO,
1362 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1364 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1365 wpas_p2p_remove_pending_group_interface(wpa_s);
1369 if (wpa_s->p2p_go_ht40)
1371 if (wpa_s->p2p_go_vht)
1374 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1375 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1377 res->role_go ? "GO" : "client", res->freq, res->ht40,
1378 MAC2STR(res->peer_device_addr),
1379 MAC2STR(res->peer_interface_addr),
1380 p2p_wps_method_text(res->wps_method));
1381 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1383 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1384 struct wpa_ssid *ssid;
1385 ssid = wpa_config_get_network(wpa_s->conf,
1386 wpa_s->p2p_persistent_id);
1387 if (ssid && ssid->disabled == 2 &&
1388 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1389 size_t len = os_strlen(ssid->passphrase);
1390 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1391 "on requested persistent group");
1392 os_memcpy(res->passphrase, ssid->passphrase, len);
1393 res->passphrase[len] = '\0';
1397 if (wpa_s->create_p2p_iface) {
1398 struct wpa_supplicant *group_wpa_s =
1399 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1400 if (group_wpa_s == NULL) {
1401 wpas_p2p_remove_pending_group_interface(wpa_s);
1404 if (group_wpa_s != wpa_s) {
1405 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1406 sizeof(group_wpa_s->p2p_pin));
1407 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1409 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1410 wpa_s->pending_interface_name[0] = '\0';
1411 group_wpa_s->p2p_in_provisioning = 1;
1414 wpas_start_wps_go(group_wpa_s, res, 1);
1416 wpas_start_wps_enrollee(group_wpa_s, res);
1418 wpa_s->p2p_in_provisioning = 1;
1419 wpa_s->global->p2p_group_formation = wpa_s;
1422 wpas_start_wps_go(wpa_s, res, 1);
1424 wpas_start_wps_enrollee(ctx, res);
1427 wpa_s->p2p_long_listen = 0;
1428 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1430 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1431 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1432 (res->peer_config_timeout % 100) * 10000,
1433 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1437 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1439 struct wpa_supplicant *wpa_s = ctx;
1440 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1441 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1443 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1447 void wpas_dev_found(void *ctx, const u8 *addr,
1448 const struct p2p_peer_info *info,
1451 #ifndef CONFIG_NO_STDOUT_DEBUG
1452 struct wpa_supplicant *wpa_s = ctx;
1453 char devtype[WPS_DEV_TYPE_BUFSIZE];
1454 char *wfd_dev_info_hex = NULL;
1456 #ifdef CONFIG_WIFI_DISPLAY
1457 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1458 WFD_SUBELEM_DEVICE_INFO);
1459 #endif /* CONFIG_WIFI_DISPLAY */
1461 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1462 " p2p_dev_addr=" MACSTR
1463 " pri_dev_type=%s name='%s' config_methods=0x%x "
1464 "dev_capab=0x%x group_capab=0x%x%s%s",
1465 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1466 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1468 info->device_name, info->config_methods,
1469 info->dev_capab, info->group_capab,
1470 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1471 wfd_dev_info_hex ? wfd_dev_info_hex : "");
1472 #endif /* CONFIG_NO_STDOUT_DEBUG */
1474 os_free(wfd_dev_info_hex);
1476 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1480 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1482 struct wpa_supplicant *wpa_s = ctx;
1484 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1485 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1487 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1491 static void wpas_find_stopped(void *ctx)
1493 struct wpa_supplicant *wpa_s = ctx;
1494 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1498 static int wpas_start_listen(void *ctx, unsigned int freq,
1499 unsigned int duration,
1500 const struct wpabuf *probe_resp_ie)
1502 struct wpa_supplicant *wpa_s = ctx;
1504 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1506 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1507 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1508 "report received Probe Request frames");
1512 wpa_s->pending_listen_freq = freq;
1513 wpa_s->pending_listen_duration = duration;
1515 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1516 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1517 "to remain on channel (%u MHz) for Listen "
1519 wpa_s->pending_listen_freq = 0;
1522 wpa_s->off_channel_freq = 0;
1523 wpa_s->roc_waiting_drv_freq = freq;
1529 static void wpas_stop_listen(void *ctx)
1531 struct wpa_supplicant *wpa_s = ctx;
1532 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1533 wpa_drv_cancel_remain_on_channel(wpa_s);
1534 wpa_s->off_channel_freq = 0;
1535 wpa_s->roc_waiting_drv_freq = 0;
1537 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1538 wpa_drv_probe_req_report(wpa_s, 0);
1542 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1544 struct wpa_supplicant *wpa_s = ctx;
1545 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1550 * DNS Header section is used only to calculate compression pointers, so the
1551 * contents of this data does not matter, but the length needs to be reserved
1552 * in the virtual packet.
1554 #define DNS_HEADER_LEN 12
1557 * 27-octet in-memory packet from P2P specification containing two implied
1558 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1560 #define P2P_SD_IN_MEMORY_LEN 27
1562 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1563 u8 **spos, const u8 *end)
1565 while (*spos < end) {
1566 u8 val = ((*spos)[0] & 0xc0) >> 6;
1569 if (val == 1 || val == 2) {
1570 /* These are reserved values in RFC 1035 */
1571 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1572 "sequence starting with 0x%x", val);
1581 if (*spos + 2 > end) {
1582 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1583 "DNS offset field");
1587 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1588 if (offset >= *spos - start) {
1589 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1590 "pointer offset %u", offset);
1595 spos_tmp = start + offset;
1596 return p2p_sd_dns_uncompress_label(upos, uend, start,
1602 len = (*spos)[0] & 0x3f;
1607 if (*spos + len > end) {
1608 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1609 "sequence - no room for label with length "
1614 if (*upos + len + 2 > uend)
1617 os_memcpy(*upos, *spos, len);
1629 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1630 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1631 * not large enough */
1632 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1633 size_t msg_len, size_t offset)
1635 /* 27-octet in-memory packet from P2P specification */
1636 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1637 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1638 u8 *tmp, *end, *spos;
1644 if (offset > msg_len)
1647 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1650 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1651 end = spos + msg_len;
1654 os_memset(tmp, 0, DNS_HEADER_LEN);
1655 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1656 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1659 uend = buf + buf_len;
1661 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1670 } else if (upos[-1] == '.')
1678 static struct p2p_srv_bonjour *
1679 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1680 const struct wpabuf *query)
1682 struct p2p_srv_bonjour *bsrv;
1685 len = wpabuf_len(query);
1686 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1687 struct p2p_srv_bonjour, list) {
1688 if (len == wpabuf_len(bsrv->query) &&
1689 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1697 static struct p2p_srv_upnp *
1698 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1699 const char *service)
1701 struct p2p_srv_upnp *usrv;
1703 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1704 struct p2p_srv_upnp, list) {
1705 if (version == usrv->version &&
1706 os_strcmp(service, usrv->service) == 0)
1713 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1718 if (wpabuf_tailroom(resp) < 5)
1721 /* Length (to be filled) */
1722 len_pos = wpabuf_put(resp, 2);
1723 wpabuf_put_u8(resp, srv_proto);
1724 wpabuf_put_u8(resp, srv_trans_id);
1726 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1727 /* Response Data: empty */
1728 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1732 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1733 struct wpabuf *resp, u8 srv_trans_id)
1735 struct p2p_srv_bonjour *bsrv;
1738 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1740 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1741 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1745 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1746 struct p2p_srv_bonjour, list) {
1747 if (wpabuf_tailroom(resp) <
1748 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1750 /* Length (to be filled) */
1751 len_pos = wpabuf_put(resp, 2);
1752 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1753 wpabuf_put_u8(resp, srv_trans_id);
1755 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1756 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1757 wpabuf_head(bsrv->resp),
1758 wpabuf_len(bsrv->resp));
1760 wpabuf_put_buf(resp, bsrv->query); /* Key */
1761 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1762 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1768 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1771 char str_rx[256], str_srv[256];
1773 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1774 return 0; /* Too short to include DNS Type and Version */
1775 if (os_memcmp(query + query_len - 3,
1776 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1778 return 0; /* Mismatch in DNS Type or Version */
1779 if (query_len == wpabuf_len(bsrv->query) &&
1780 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1781 return 1; /* Binary match */
1783 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1785 return 0; /* Failed to uncompress query */
1786 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1787 wpabuf_head(bsrv->query),
1788 wpabuf_len(bsrv->query) - 3, 0))
1789 return 0; /* Failed to uncompress service */
1791 return os_strcmp(str_rx, str_srv) == 0;
1795 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1796 struct wpabuf *resp, u8 srv_trans_id,
1797 const u8 *query, size_t query_len)
1799 struct p2p_srv_bonjour *bsrv;
1803 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1805 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1806 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1807 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1812 if (query_len == 0) {
1813 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1817 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1818 struct p2p_srv_bonjour, list) {
1819 if (!match_bonjour_query(bsrv, query, query_len))
1822 if (wpabuf_tailroom(resp) <
1823 5 + query_len + wpabuf_len(bsrv->resp))
1828 /* Length (to be filled) */
1829 len_pos = wpabuf_put(resp, 2);
1830 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1831 wpabuf_put_u8(resp, srv_trans_id);
1834 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1835 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1836 wpabuf_head(bsrv->resp),
1837 wpabuf_len(bsrv->resp));
1840 wpabuf_put_data(resp, query, query_len); /* Key */
1841 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1843 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1847 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1849 if (wpabuf_tailroom(resp) < 5)
1852 /* Length (to be filled) */
1853 len_pos = wpabuf_put(resp, 2);
1854 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1855 wpabuf_put_u8(resp, srv_trans_id);
1858 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1859 /* Response Data: empty */
1860 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1866 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1867 struct wpabuf *resp, u8 srv_trans_id)
1869 struct p2p_srv_upnp *usrv;
1872 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1874 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1875 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1879 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1880 struct p2p_srv_upnp, list) {
1881 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1884 /* Length (to be filled) */
1885 len_pos = wpabuf_put(resp, 2);
1886 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1887 wpabuf_put_u8(resp, srv_trans_id);
1890 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1892 wpabuf_put_u8(resp, usrv->version);
1893 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1895 wpabuf_put_str(resp, usrv->service);
1896 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1902 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1903 struct wpabuf *resp, u8 srv_trans_id,
1904 const u8 *query, size_t query_len)
1906 struct p2p_srv_upnp *usrv;
1912 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1915 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1916 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1917 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1922 if (query_len == 0) {
1923 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1927 if (wpabuf_tailroom(resp) < 5)
1930 /* Length (to be filled) */
1931 len_pos = wpabuf_put(resp, 2);
1932 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1933 wpabuf_put_u8(resp, srv_trans_id);
1936 str = os_malloc(query_len);
1939 os_memcpy(str, query + 1, query_len - 1);
1940 str[query_len - 1] = '\0';
1942 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1943 struct p2p_srv_upnp, list) {
1944 if (version != usrv->version)
1947 if (os_strcmp(str, "ssdp:all") != 0 &&
1948 os_strstr(usrv->service, str) == NULL)
1951 if (wpabuf_tailroom(resp) < 2)
1955 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1957 wpabuf_put_u8(resp, version);
1959 wpabuf_put_u8(resp, ',');
1963 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1965 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1967 wpabuf_put_str(resp, usrv->service);
1972 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1975 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1976 /* Response Data: empty */
1979 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1983 #ifdef CONFIG_WIFI_DISPLAY
1984 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1985 struct wpabuf *resp, u8 srv_trans_id,
1986 const u8 *query, size_t query_len)
1992 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1994 if (!wpa_s->global->wifi_display) {
1995 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1996 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2001 if (query_len < 1) {
2002 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2007 if (wpabuf_tailroom(resp) < 5)
2012 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2014 /* TODO: role specific handling */
2016 /* Length (to be filled) */
2017 len_pos = wpabuf_put(resp, 2);
2018 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2019 wpabuf_put_u8(resp, srv_trans_id);
2020 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2022 while (pos < query + query_len) {
2023 if (*pos < MAX_WFD_SUBELEMS &&
2024 wpa_s->global->wfd_subelem[*pos] &&
2025 wpabuf_tailroom(resp) >=
2026 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2027 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2028 "subelement %u", *pos);
2029 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2034 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2036 #endif /* CONFIG_WIFI_DISPLAY */
2039 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2040 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2042 struct wpa_supplicant *wpa_s = ctx;
2043 const u8 *pos = tlvs;
2044 const u8 *end = tlvs + tlvs_len;
2047 struct wpabuf *resp;
2048 u8 srv_proto, srv_trans_id;
2052 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2054 buf_len = 2 * tlvs_len + 1;
2055 buf = os_malloc(buf_len);
2057 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2058 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2060 freq, MAC2STR(sa), dialog_token, update_indic,
2065 if (wpa_s->p2p_sd_over_ctrl_iface) {
2066 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2067 update_indic, tlvs, tlvs_len);
2068 return; /* to be processed by an external program */
2071 resp = wpabuf_alloc(10000);
2075 while (pos + 1 < end) {
2076 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2077 slen = WPA_GET_LE16(pos);
2079 if (pos + slen > end || slen < 2) {
2080 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2085 tlv_end = pos + slen;
2088 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2090 srv_trans_id = *pos++;
2091 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2094 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2095 pos, tlv_end - pos);
2098 if (wpa_s->force_long_sd) {
2099 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2101 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2102 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2106 switch (srv_proto) {
2107 case P2P_SERV_ALL_SERVICES:
2108 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2109 "for all services");
2110 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2111 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2112 wpa_printf(MSG_DEBUG, "P2P: No service "
2113 "discovery protocols available");
2114 wpas_sd_add_proto_not_avail(
2115 resp, P2P_SERV_ALL_SERVICES,
2119 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2120 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2122 case P2P_SERV_BONJOUR:
2123 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2124 pos, tlv_end - pos);
2127 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2128 pos, tlv_end - pos);
2130 #ifdef CONFIG_WIFI_DISPLAY
2131 case P2P_SERV_WIFI_DISPLAY:
2132 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2133 pos, tlv_end - pos);
2135 #endif /* CONFIG_WIFI_DISPLAY */
2137 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2138 "protocol %u", srv_proto);
2139 wpas_sd_add_proto_not_avail(resp, srv_proto,
2148 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2149 update_indic, tlvs, tlvs_len);
2151 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2157 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2158 const u8 *tlvs, size_t tlvs_len)
2160 struct wpa_supplicant *wpa_s = ctx;
2161 const u8 *pos = tlvs;
2162 const u8 *end = tlvs + tlvs_len;
2168 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2170 if (tlvs_len > 1500) {
2171 /* TODO: better way for handling this */
2172 wpa_msg_ctrl(wpa_s, MSG_INFO,
2173 P2P_EVENT_SERV_DISC_RESP MACSTR
2174 " %u <long response: %u bytes>",
2175 MAC2STR(sa), update_indic,
2176 (unsigned int) tlvs_len);
2178 buf_len = 2 * tlvs_len + 1;
2179 buf = os_malloc(buf_len);
2181 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2182 wpa_msg_ctrl(wpa_s, MSG_INFO,
2183 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2184 MAC2STR(sa), update_indic, buf);
2190 u8 srv_proto, srv_trans_id, status;
2192 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2193 slen = WPA_GET_LE16(pos);
2195 if (pos + slen > end || slen < 3) {
2196 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2200 tlv_end = pos + slen;
2203 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2205 srv_trans_id = *pos++;
2206 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2209 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2212 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2213 pos, tlv_end - pos);
2218 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2222 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2223 const struct wpabuf *tlvs)
2225 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2226 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
2227 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2229 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2233 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2234 u8 version, const char *query)
2236 struct wpabuf *tlvs;
2239 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2242 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2243 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2244 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2245 wpabuf_put_u8(tlvs, version);
2246 wpabuf_put_str(tlvs, query);
2247 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2253 #ifdef CONFIG_WIFI_DISPLAY
2255 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2256 const struct wpabuf *tlvs)
2258 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2260 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2262 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2266 #define MAX_WFD_SD_SUBELEMS 20
2268 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2269 const char *subelems)
2276 len = wpabuf_put(tlvs, 2);
2277 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2278 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2280 wpabuf_put_u8(tlvs, role);
2285 if (val >= 0 && val < 256) {
2286 wpabuf_put_u8(tlvs, val);
2288 if (count == MAX_WFD_SD_SUBELEMS)
2291 pos = os_strchr(pos + 1, ',');
2297 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2301 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2302 const u8 *dst, const char *role)
2304 struct wpabuf *tlvs;
2306 const char *subelems;
2309 subelems = os_strchr(role, ' ');
2310 if (subelems == NULL)
2314 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2318 if (os_strstr(role, "[source]"))
2319 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2320 if (os_strstr(role, "[pri-sink]"))
2321 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2322 if (os_strstr(role, "[sec-sink]"))
2323 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2324 if (os_strstr(role, "[source+sink]"))
2325 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2327 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2332 #endif /* CONFIG_WIFI_DISPLAY */
2335 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2337 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2338 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
2339 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2341 return p2p_sd_cancel_request(wpa_s->global->p2p,
2342 (void *) (uintptr_t) req);
2346 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2347 const u8 *dst, u8 dialog_token,
2348 const struct wpabuf *resp_tlvs)
2350 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2351 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2355 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2357 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2362 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2364 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2365 wpa_drv_p2p_service_update(wpa_s);
2368 if (wpa_s->global->p2p)
2369 p2p_sd_service_update(wpa_s->global->p2p);
2373 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2375 dl_list_del(&bsrv->list);
2376 wpabuf_free(bsrv->query);
2377 wpabuf_free(bsrv->resp);
2382 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2384 dl_list_del(&usrv->list);
2385 os_free(usrv->service);
2390 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2392 struct p2p_srv_bonjour *bsrv, *bn;
2393 struct p2p_srv_upnp *usrv, *un;
2395 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2396 struct p2p_srv_bonjour, list)
2397 wpas_p2p_srv_bonjour_free(bsrv);
2399 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2400 struct p2p_srv_upnp, list)
2401 wpas_p2p_srv_upnp_free(usrv);
2403 wpas_p2p_sd_service_update(wpa_s);
2407 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2408 struct wpabuf *query, struct wpabuf *resp)
2410 struct p2p_srv_bonjour *bsrv;
2412 bsrv = os_zalloc(sizeof(*bsrv));
2415 bsrv->query = query;
2417 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2419 wpas_p2p_sd_service_update(wpa_s);
2424 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2425 const struct wpabuf *query)
2427 struct p2p_srv_bonjour *bsrv;
2429 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2432 wpas_p2p_srv_bonjour_free(bsrv);
2433 wpas_p2p_sd_service_update(wpa_s);
2438 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2439 const char *service)
2441 struct p2p_srv_upnp *usrv;
2443 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2444 return 0; /* Already listed */
2445 usrv = os_zalloc(sizeof(*usrv));
2448 usrv->version = version;
2449 usrv->service = os_strdup(service);
2450 if (usrv->service == NULL) {
2454 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2456 wpas_p2p_sd_service_update(wpa_s);
2461 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2462 const char *service)
2464 struct p2p_srv_upnp *usrv;
2466 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2469 wpas_p2p_srv_upnp_free(usrv);
2470 wpas_p2p_sd_service_update(wpa_s);
2475 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2476 const u8 *peer, const char *params,
2477 unsigned int generated_pin)
2479 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2480 " %08d%s", MAC2STR(peer), generated_pin, params);
2484 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2485 const u8 *peer, const char *params)
2487 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2488 "%s", MAC2STR(peer), params);
2492 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2493 const u8 *dev_addr, const u8 *pri_dev_type,
2494 const char *dev_name, u16 supp_config_methods,
2495 u8 dev_capab, u8 group_capab, const u8 *group_id,
2496 size_t group_id_len)
2498 struct wpa_supplicant *wpa_s = ctx;
2499 char devtype[WPS_DEV_TYPE_BUFSIZE];
2501 u8 empty_dev_type[8];
2502 unsigned int generated_pin = 0;
2503 struct wpa_supplicant *group = NULL;
2506 for (group = wpa_s->global->ifaces; group; group = group->next)
2508 struct wpa_ssid *s = group->current_ssid;
2510 s->mode == WPAS_MODE_P2P_GO &&
2511 group_id_len - ETH_ALEN == s->ssid_len &&
2512 os_memcmp(group_id + ETH_ALEN, s->ssid,
2518 if (pri_dev_type == NULL) {
2519 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2520 pri_dev_type = empty_dev_type;
2522 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2523 " pri_dev_type=%s name='%s' config_methods=0x%x "
2524 "dev_capab=0x%x group_capab=0x%x%s%s",
2526 wps_dev_type_bin2str(pri_dev_type, devtype,
2528 dev_name, supp_config_methods, dev_capab, group_capab,
2529 group ? " group=" : "",
2530 group ? group->ifname : "");
2531 params[sizeof(params) - 1] = '\0';
2533 if (config_methods & WPS_CONFIG_DISPLAY) {
2534 generated_pin = wps_generate_pin();
2535 wpas_prov_disc_local_display(wpa_s, peer, params,
2537 } else if (config_methods & WPS_CONFIG_KEYPAD)
2538 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2539 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2540 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2541 MACSTR "%s", MAC2STR(peer), params);
2543 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2544 P2P_PROV_DISC_SUCCESS,
2545 config_methods, generated_pin);
2549 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2551 struct wpa_supplicant *wpa_s = ctx;
2552 unsigned int generated_pin = 0;
2555 if (wpa_s->pending_pd_before_join &&
2556 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2557 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2558 wpa_s->pending_pd_before_join = 0;
2559 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2560 "join-existing-group operation");
2561 wpas_p2p_join_start(wpa_s);
2565 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2566 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2567 os_snprintf(params, sizeof(params), " peer_go=%d",
2568 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2572 if (config_methods & WPS_CONFIG_DISPLAY)
2573 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2574 else if (config_methods & WPS_CONFIG_KEYPAD) {
2575 generated_pin = wps_generate_pin();
2576 wpas_prov_disc_local_display(wpa_s, peer, params,
2578 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2579 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2580 MACSTR "%s", MAC2STR(peer), params);
2582 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2583 P2P_PROV_DISC_SUCCESS,
2584 config_methods, generated_pin);
2588 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2589 enum p2p_prov_disc_status status)
2591 struct wpa_supplicant *wpa_s = ctx;
2593 if (wpa_s->p2p_fallback_to_go_neg) {
2594 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2595 "failed - fall back to GO Negotiation");
2596 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2600 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2601 wpa_s->pending_pd_before_join = 0;
2602 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2603 "join-existing-group operation (no ACK for PD "
2605 wpas_p2p_join_start(wpa_s);
2609 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2610 " p2p_dev_addr=" MACSTR " status=%d",
2611 MAC2STR(peer), status);
2613 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2618 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2620 if (channels == NULL)
2621 return 1; /* Assume no restrictions */
2622 return p2p_channels_includes_freq(channels, freq);
2627 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2628 const u8 *go_dev_addr, const u8 *ssid,
2629 size_t ssid_len, int *go, u8 *group_bssid,
2630 int *force_freq, int persistent_group,
2631 const struct p2p_channels *channels)
2633 struct wpa_supplicant *wpa_s = ctx;
2636 struct wpa_supplicant *grp;
2638 if (!persistent_group) {
2639 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2640 " to join an active group", MAC2STR(sa));
2641 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2642 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2644 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2645 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2646 "authorized invitation");
2650 * Do not accept the invitation automatically; notify user and
2653 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2656 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2658 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2659 "running persistent group");
2661 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2665 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2666 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2667 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2668 "invitation to re-invoke a persistent group");
2669 } else if (!wpa_s->conf->persistent_reconnect)
2670 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2672 for (s = wpa_s->conf->ssid; s; s = s->next) {
2673 if (s->disabled == 2 &&
2674 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2675 s->ssid_len == ssid_len &&
2676 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2681 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2682 " requested reinvocation of an unknown group",
2684 return P2P_SC_FAIL_UNKNOWN_GROUP;
2687 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2689 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2690 wpa_printf(MSG_DEBUG, "P2P: The only available "
2691 "interface is already in use - reject "
2693 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2695 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2696 } else if (s->mode == WPAS_MODE_P2P_GO) {
2698 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2700 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2701 "interface address for the group");
2702 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2704 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2709 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2711 /* Get one of the frequencies currently in use */
2712 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
2713 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
2714 wpas_p2p_set_own_freq_preference(wpa_s, res);
2716 if (wpa_s->num_multichan_concurrent < 2 ||
2717 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2718 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2723 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2724 wpas_p2p_num_unused_channels(wpa_s) > 0) {
2726 /* We are the client */
2727 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2728 "running a GO but we are capable of MCC, "
2729 "figure out the best channel to use");
2731 } else if (!freq_included(channels, *force_freq)) {
2732 /* We are the GO, and *force_freq is not in the
2734 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2735 "in intersection but we are capable of MCC, "
2736 "figure out the best channel to use",
2742 return P2P_SC_SUCCESS;
2746 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2747 const u8 *ssid, size_t ssid_len,
2748 const u8 *go_dev_addr, u8 status,
2751 struct wpa_supplicant *wpa_s = ctx;
2754 for (s = wpa_s->conf->ssid; s; s = s->next) {
2755 if (s->disabled == 2 &&
2756 s->ssid_len == ssid_len &&
2757 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2761 if (status == P2P_SC_SUCCESS) {
2762 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2763 " was accepted; op_freq=%d MHz",
2764 MAC2STR(sa), op_freq);
2766 int go = s->mode == WPAS_MODE_P2P_GO;
2767 wpas_p2p_group_add_persistent(
2768 wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
2769 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
2771 wpa_s->user_initiated_pd = 0;
2772 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2773 wpa_s->p2p_wps_method, 0);
2778 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2779 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2780 " was rejected (status %u)", MAC2STR(sa), status);
2786 wpa_msg_global(wpa_s, MSG_INFO,
2787 P2P_EVENT_INVITATION_RECEIVED
2788 "sa=" MACSTR " go_dev_addr=" MACSTR
2789 " bssid=" MACSTR " unknown-network",
2790 MAC2STR(sa), MAC2STR(go_dev_addr),
2793 wpa_msg_global(wpa_s, MSG_INFO,
2794 P2P_EVENT_INVITATION_RECEIVED
2795 "sa=" MACSTR " go_dev_addr=" MACSTR
2797 MAC2STR(sa), MAC2STR(go_dev_addr));
2802 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2803 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2804 "sa=" MACSTR " persistent=%d freq=%d",
2805 MAC2STR(sa), s->id, op_freq);
2807 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2808 "sa=" MACSTR " persistent=%d",
2809 MAC2STR(sa), s->id);
2814 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2815 struct wpa_ssid *ssid,
2816 const u8 *peer, int inv)
2823 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2824 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2828 if (i >= ssid->num_p2p_clients) {
2829 if (ssid->mode != WPAS_MODE_P2P_GO &&
2830 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2831 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2832 "due to invitation result", ssid->id);
2833 wpas_notify_network_removed(wpa_s, ssid);
2834 wpa_config_remove_network(wpa_s->conf, ssid->id);
2837 return; /* Peer not found in client list */
2840 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2841 "group %d client list%s",
2842 MAC2STR(peer), ssid->id,
2843 inv ? " due to invitation result" : "");
2844 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2845 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2846 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2847 ssid->num_p2p_clients--;
2848 #ifndef CONFIG_NO_CONFIG_WRITE
2849 if (wpa_s->parent->conf->update_config &&
2850 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2851 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2852 #endif /* CONFIG_NO_CONFIG_WRITE */
2856 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2859 struct wpa_ssid *ssid;
2861 wpa_s = wpa_s->global->p2p_invite_group;
2863 return; /* No known invitation group */
2864 ssid = wpa_s->current_ssid;
2865 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2866 !ssid->p2p_persistent_group)
2867 return; /* Not operating as a GO in persistent group */
2868 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2869 ssid->ssid, ssid->ssid_len);
2870 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2874 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
2875 const struct p2p_channels *channels,
2876 const u8 *peer, int neg_freq)
2878 struct wpa_supplicant *wpa_s = ctx;
2879 struct wpa_ssid *ssid;
2883 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2884 "status=%d " MACSTR,
2885 status, MAC2STR(bssid));
2887 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2888 "status=%d ", status);
2890 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2892 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2893 status, MAC2STR(peer));
2894 if (wpa_s->pending_invite_ssid_id == -1) {
2895 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2896 wpas_remove_persistent_client(wpa_s, peer);
2897 return; /* Invitation to active group */
2900 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2901 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2902 "invitation exchange to indicate readiness for "
2906 if (status != P2P_SC_SUCCESS) {
2907 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2908 ssid = wpa_config_get_network(
2909 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2910 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2912 wpas_p2p_remove_pending_group_interface(wpa_s);
2916 ssid = wpa_config_get_network(wpa_s->conf,
2917 wpa_s->pending_invite_ssid_id);
2919 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2920 "data matching with invitation");
2925 * The peer could have missed our ctrl::ack frame for Invitation
2926 * Response and continue retransmitting the frame. To reduce the
2927 * likelihood of the peer not getting successful TX status for the
2928 * Invitation Response frame, wait a short time here before starting
2929 * the persistent group so that we will remain on the current channel to
2930 * acknowledge any possible retransmission from the peer.
2932 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2933 "starting persistent group");
2936 freq = wpa_s->p2p_persistent_go_freq;
2937 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
2938 freq_included(channels, neg_freq)) {
2939 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode",
2944 wpas_p2p_group_add_persistent(wpa_s, ssid,
2945 ssid->mode == WPAS_MODE_P2P_GO,
2947 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2949 ssid->mode == WPAS_MODE_P2P_GO ?
2950 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2955 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2958 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
2962 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2964 reg->channel[reg->channels] = chan;
2969 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2970 struct p2p_channels *chan,
2971 struct p2p_channels *cli_chan)
2975 os_memset(cli_chan, 0, sizeof(*cli_chan));
2977 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2980 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2981 chan->reg_class[cla].reg_class = 81;
2982 chan->reg_class[cla].channels = 0;
2983 for (i = 0; i < 11; i++) {
2984 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2985 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2987 if (chan->reg_class[cla].channels)
2990 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2993 /* Operating class 115 - 5 GHz, channels 36-48 */
2994 chan->reg_class[cla].reg_class = 115;
2995 chan->reg_class[cla].channels = 0;
2996 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2997 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2998 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2999 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3000 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3001 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3002 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3003 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3004 if (chan->reg_class[cla].channels)
3007 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3010 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3011 chan->reg_class[cla].reg_class = 124;
3012 chan->reg_class[cla].channels = 0;
3013 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3014 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3015 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3016 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3017 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3018 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3019 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3020 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3021 if (chan->reg_class[cla].channels)
3024 chan->reg_classes = cla;
3029 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3031 enum hostapd_hw_mode mode)
3035 for (i = 0; i < num_modes; i++) {
3036 if (modes[i].mode == mode)
3045 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3048 static int has_channel(struct wpa_global *global,
3049 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3054 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3056 if (wpas_p2p_disallowed_freq(global, freq))
3059 for (i = 0; i < mode->num_channels; i++) {
3060 if (mode->channels[i].chan == chan) {
3062 *flags = mode->channels[i].flag;
3063 if (mode->channels[i].flag &
3064 (HOSTAPD_CHAN_DISABLED |
3065 HOSTAPD_CHAN_RADAR))
3067 if (mode->channels[i].flag &
3068 (HOSTAPD_CHAN_PASSIVE_SCAN |
3069 HOSTAPD_CHAN_NO_IBSS))
3070 return PASSIVE_ONLY;
3079 struct p2p_oper_class_map {
3080 enum hostapd_hw_mode mode;
3085 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
3088 static struct p2p_oper_class_map op_class[] = {
3089 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3090 #if 0 /* Do not enable HT40 on 2 GHz for now */
3091 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3092 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3094 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3095 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3096 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3097 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3098 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3099 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3102 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3103 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3104 * 80 MHz, but currently use the following definition for simplicity
3105 * (these center frequencies are not actual channels, which makes
3106 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3107 * removing invalid channels.
3109 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3110 { -1, 0, 0, 0, 0, BW20 }
3114 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3115 struct hostapd_hw_modes *mode,
3118 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3121 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3124 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3126 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3127 * so the center channel is 6 channels away from the start/end.
3129 if (channel >= center_channels[i] - 6 &&
3130 channel <= center_channels[i] + 6)
3131 return center_channels[i];
3137 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3138 struct hostapd_hw_modes *mode,
3143 enum chan_allowed res, ret = ALLOWED;
3145 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3148 if (center_chan >= 58 && center_chan <= 138)
3149 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3151 /* check all the channels are available */
3152 for (i = 0; i < 4; i++) {
3153 int adj_chan = center_chan - 6 + i * 4;
3155 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3156 if (res == NOT_ALLOWED)
3158 if (res == PASSIVE_ONLY)
3161 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3163 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3165 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3167 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3175 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3176 struct hostapd_hw_modes *mode,
3180 enum chan_allowed res, res2;
3182 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3183 if (bw == BW40MINUS) {
3184 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3186 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3187 } else if (bw == BW40PLUS) {
3188 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3190 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3191 } else if (bw == BW80) {
3192 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3195 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3197 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3198 return PASSIVE_ONLY;
3203 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3204 struct p2p_channels *chan,
3205 struct p2p_channels *cli_chan)
3207 struct hostapd_hw_modes *mode;
3208 int cla, op, cli_cla;
3210 if (wpa_s->hw.modes == NULL) {
3211 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3212 "of all supported channels; assume dualband "
3214 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3219 for (op = 0; op_class[op].op_class; op++) {
3220 struct p2p_oper_class_map *o = &op_class[op];
3222 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3224 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3227 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3228 enum chan_allowed res;
3229 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3230 if (res == ALLOWED) {
3232 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3234 reg = &chan->reg_class[cla];
3236 reg->reg_class = o->op_class;
3238 reg->channel[reg->channels] = ch;
3240 } else if (res == PASSIVE_ONLY &&
3241 wpa_s->conf->p2p_add_cli_chan) {
3242 if (cli_reg == NULL) {
3243 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3245 cli_reg = &cli_chan->reg_class[cli_cla];
3247 cli_reg->reg_class = o->op_class;
3249 cli_reg->channel[cli_reg->channels] = ch;
3250 cli_reg->channels++;
3254 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3255 reg->channel, reg->channels);
3258 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3259 cli_reg->channel, cli_reg->channels);
3263 chan->reg_classes = cla;
3264 cli_chan->reg_classes = cli_cla;
3270 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3271 struct hostapd_hw_modes *mode, u8 channel)
3274 enum chan_allowed ret;
3276 for (op = 0; op_class[op].op_class; op++) {
3277 struct p2p_oper_class_map *o = &op_class[op];
3280 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3281 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3282 o->bw == BW20 || ch != channel)
3284 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3286 return (o->bw == BW40MINUS) ? -1 : 1;
3293 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3294 struct hostapd_hw_modes *mode, u8 channel)
3296 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3299 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3303 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3306 struct wpa_supplicant *wpa_s = ctx;
3308 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3309 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3315 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3319 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3321 struct wpa_supplicant *wpa_s = ctx;
3323 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3324 struct wpa_ssid *ssid = wpa_s->current_ssid;
3327 if (ssid->mode != WPAS_MODE_INFRA)
3329 if (wpa_s->wpa_state != WPA_COMPLETED &&
3330 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3332 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3340 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3342 struct wpa_supplicant *wpa_s = ctx;
3343 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3347 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3349 struct wpa_interface iface;
3350 struct wpa_supplicant *p2pdev_wpa_s;
3352 char force_name[100];
3355 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3357 force_name[0] = '\0';
3358 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3359 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3360 force_name, wpa_s->pending_interface_addr, NULL);
3362 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3365 os_strlcpy(wpa_s->pending_interface_name, ifname,
3366 sizeof(wpa_s->pending_interface_name));
3368 os_memset(&iface, 0, sizeof(iface));
3370 iface.ifname = wpa_s->pending_interface_name;
3371 iface.driver = wpa_s->driver->name;
3372 iface.driver_param = wpa_s->conf->driver_param;
3373 iface.confname = wpa_s->confname;
3374 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3375 if (!p2pdev_wpa_s) {
3376 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3379 p2pdev_wpa_s->parent = wpa_s;
3381 wpa_s->pending_interface_name[0] = '\0';
3387 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3388 * @global: Pointer to global data from wpa_supplicant_init()
3389 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3390 * Returns: 0 on success, -1 on failure
3392 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3394 struct p2p_config p2p;
3398 if (wpa_s->conf->p2p_disabled)
3401 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3407 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3408 struct p2p_params params;
3410 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3411 os_memset(¶ms, 0, sizeof(params));
3412 params.dev_name = wpa_s->conf->device_name;
3413 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3415 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3416 os_memcpy(params.sec_dev_type,
3417 wpa_s->conf->sec_device_type,
3418 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3420 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
3426 os_memset(&p2p, 0, sizeof(p2p));
3428 p2p.debug_print = wpas_p2p_debug_print;
3429 p2p.p2p_scan = wpas_p2p_scan;
3430 p2p.send_action = wpas_send_action;
3431 p2p.send_action_done = wpas_send_action_done;
3432 p2p.go_neg_completed = wpas_go_neg_completed;
3433 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3434 p2p.dev_found = wpas_dev_found;
3435 p2p.dev_lost = wpas_dev_lost;
3436 p2p.find_stopped = wpas_find_stopped;
3437 p2p.start_listen = wpas_start_listen;
3438 p2p.stop_listen = wpas_stop_listen;
3439 p2p.send_probe_resp = wpas_send_probe_resp;
3440 p2p.sd_request = wpas_sd_request;
3441 p2p.sd_response = wpas_sd_response;
3442 p2p.prov_disc_req = wpas_prov_disc_req;
3443 p2p.prov_disc_resp = wpas_prov_disc_resp;
3444 p2p.prov_disc_fail = wpas_prov_disc_fail;
3445 p2p.invitation_process = wpas_invitation_process;
3446 p2p.invitation_received = wpas_invitation_received;
3447 p2p.invitation_result = wpas_invitation_result;
3448 p2p.get_noa = wpas_get_noa;
3449 p2p.go_connected = wpas_go_connected;
3451 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
3452 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
3453 p2p.dev_name = wpa_s->conf->device_name;
3454 p2p.manufacturer = wpa_s->conf->manufacturer;
3455 p2p.model_name = wpa_s->conf->model_name;
3456 p2p.model_number = wpa_s->conf->model_number;
3457 p2p.serial_number = wpa_s->conf->serial_number;
3459 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3460 p2p.config_methods = wpa_s->wps->config_methods;
3463 if (wpa_s->conf->p2p_listen_reg_class &&
3464 wpa_s->conf->p2p_listen_channel) {
3465 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3466 p2p.channel = wpa_s->conf->p2p_listen_channel;
3470 * Pick one of the social channels randomly as the listen
3473 os_get_random((u8 *) &r, sizeof(r));
3474 p2p.channel = 1 + (r % 3) * 5;
3476 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3478 if (wpa_s->conf->p2p_oper_reg_class &&
3479 wpa_s->conf->p2p_oper_channel) {
3480 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3481 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3482 p2p.cfg_op_channel = 1;
3483 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3484 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3487 p2p.op_reg_class = 81;
3489 * Use random operation channel from (1, 6, 11) if no other
3490 * preference is indicated.
3492 os_get_random((u8 *) &r, sizeof(r));
3493 p2p.op_channel = 1 + (r % 3) * 5;
3494 p2p.cfg_op_channel = 0;
3495 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3496 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3499 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3500 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3501 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3504 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3505 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3506 p2p.country[2] = 0x04;
3508 os_memcpy(p2p.country, "XX\x04", 3);
3510 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3511 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3516 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3519 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3520 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3521 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3523 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3524 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3526 p2p.max_peers = 100;
3528 if (wpa_s->conf->p2p_ssid_postfix) {
3529 p2p.ssid_postfix_len =
3530 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3531 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3532 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3533 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3534 p2p.ssid_postfix_len);
3537 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3539 p2p.max_listen = wpa_s->max_remain_on_chan;
3541 global->p2p = p2p_init(&p2p);
3542 if (global->p2p == NULL)
3544 global->p2p_init_wpa_s = wpa_s;
3546 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3547 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3549 p2p_add_wps_vendor_extension(
3550 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3553 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3560 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3561 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3563 * This function deinitialize per-interface P2P data.
3565 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3567 if (wpa_s->driver && wpa_s->drv_priv)
3568 wpa_drv_probe_req_report(wpa_s, 0);
3570 if (wpa_s->go_params) {
3571 /* Clear any stored provisioning info */
3572 p2p_clear_provisioning_info(
3574 wpa_s->go_params->peer_device_addr);
3577 os_free(wpa_s->go_params);
3578 wpa_s->go_params = NULL;
3579 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3580 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3581 wpa_s->p2p_long_listen = 0;
3582 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3583 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3584 wpas_p2p_remove_pending_group_interface(wpa_s);
3585 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3587 /* TODO: remove group interface from the driver if this wpa_s instance
3588 * is on top of a P2P group interface */
3593 * wpas_p2p_deinit_global - Deinitialize global P2P module
3594 * @global: Pointer to global data from wpa_supplicant_init()
3596 * This function deinitializes the global (per device) P2P module.
3598 void wpas_p2p_deinit_global(struct wpa_global *global)
3600 struct wpa_supplicant *wpa_s, *tmp;
3602 wpa_s = global->ifaces;
3604 wpas_p2p_service_flush(wpa_s);
3606 if (global->p2p == NULL)
3609 /* Remove remaining P2P group interfaces */
3610 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3611 wpa_s = wpa_s->next;
3613 tmp = global->ifaces;
3616 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3621 /* Disconnect from the P2P group and deinit the interface */
3622 wpas_p2p_disconnect(tmp);
3626 * Deinit GO data on any possibly remaining interface (if main
3627 * interface is used as GO).
3629 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3630 if (wpa_s->ap_iface)
3631 wpas_p2p_group_deinit(wpa_s);
3634 p2p_deinit(global->p2p);
3636 global->p2p_init_wpa_s = NULL;
3640 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3642 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3643 wpa_s->conf->p2p_no_group_iface)
3644 return 0; /* separate interface disabled per configuration */
3645 if (wpa_s->drv_flags &
3646 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3647 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3648 return 1; /* P2P group requires a new interface in every case
3650 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3651 return 0; /* driver does not support concurrent operations */
3652 if (wpa_s->global->ifaces->next)
3653 return 1; /* more that one interface already in use */
3654 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3655 return 1; /* this interface is already in use */
3660 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3661 const u8 *peer_addr,
3662 enum p2p_wps_method wps_method,
3663 int go_intent, const u8 *own_interface_addr,
3664 unsigned int force_freq, int persistent_group,
3665 struct wpa_ssid *ssid, unsigned int pref_freq)
3667 if (persistent_group && wpa_s->conf->persistent_reconnect)
3668 persistent_group = 2;
3670 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3671 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3672 go_intent, own_interface_addr,
3673 force_freq, persistent_group);
3677 * Increase GO config timeout if HT40 is used since it takes some time
3678 * to scan channels for coex purposes before the BSS can be started.
3680 p2p_set_config_timeout(wpa_s->global->p2p,
3681 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3683 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3684 go_intent, own_interface_addr, force_freq,
3685 persistent_group, ssid ? ssid->ssid : NULL,
3686 ssid ? ssid->ssid_len : 0,
3687 wpa_s->p2p_pd_before_go_neg, pref_freq);
3691 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3692 const u8 *peer_addr,
3693 enum p2p_wps_method wps_method,
3694 int go_intent, const u8 *own_interface_addr,
3695 unsigned int force_freq, int persistent_group,
3696 struct wpa_ssid *ssid, unsigned int pref_freq)
3698 if (persistent_group && wpa_s->conf->persistent_reconnect)
3699 persistent_group = 2;
3701 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3704 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3705 go_intent, own_interface_addr, force_freq,
3706 persistent_group, ssid ? ssid->ssid : NULL,
3707 ssid ? ssid->ssid_len : 0, pref_freq);
3711 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3713 wpa_s->p2p_join_scan_count++;
3714 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3715 wpa_s->p2p_join_scan_count);
3716 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3717 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3718 " for join operationg - stop join attempt",
3719 MAC2STR(wpa_s->pending_join_iface_addr));
3720 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3721 if (wpa_s->p2p_auto_pd) {
3722 wpa_s->p2p_auto_pd = 0;
3723 wpa_msg_global(wpa_s, MSG_INFO,
3724 P2P_EVENT_PROV_DISC_FAILURE
3725 " p2p_dev_addr=" MACSTR " status=N/A",
3726 MAC2STR(wpa_s->pending_join_dev_addr));
3729 wpa_msg_global(wpa_s->parent, MSG_INFO,
3730 P2P_EVENT_GROUP_FORMATION_FAILURE);
3735 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3737 int *freqs, res, num, i;
3739 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3740 /* Multiple channels are supported and not all are in use */
3744 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3748 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3749 wpa_s->num_multichan_concurrent);
3755 for (i = 0; i < num; i++) {
3756 if (freqs[i] == freq) {
3757 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3772 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3773 const u8 *peer_dev_addr)
3775 struct wpa_bss *bss;
3778 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3781 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3782 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3787 updated = os_reltime_before(&wpa_s->p2p_auto_started,
3789 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3790 "%ld.%06ld (%supdated in last scan)",
3791 bss->last_update.sec, bss->last_update.usec,
3792 updated ? "": "not ");
3798 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3799 struct wpa_scan_results *scan_res)
3801 struct wpa_bss *bss;
3803 u8 iface_addr[ETH_ALEN];
3805 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3807 if (wpa_s->global->p2p_disabled)
3810 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3811 scan_res ? (int) scan_res->num : -1,
3812 wpa_s->p2p_auto_join ? "auto_" : "");
3815 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3817 if (wpa_s->p2p_auto_pd) {
3818 int join = wpas_p2p_peer_go(wpa_s,
3819 wpa_s->pending_join_dev_addr);
3821 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3822 wpa_s->auto_pd_scan_retry++;
3823 bss = wpa_bss_get_bssid_latest(
3824 wpa_s, wpa_s->pending_join_dev_addr);
3827 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3828 "the peer " MACSTR " at %d MHz",
3829 wpa_s->auto_pd_scan_retry,
3831 pending_join_dev_addr),
3833 wpas_p2p_join_scan_req(wpa_s, freq);
3841 wpa_s->p2p_auto_pd = 0;
3842 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3843 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3844 MAC2STR(wpa_s->pending_join_dev_addr), join);
3845 if (p2p_prov_disc_req(wpa_s->global->p2p,
3846 wpa_s->pending_join_dev_addr,
3847 wpa_s->pending_pd_config_methods, join,
3848 0, wpa_s->user_initiated_pd) < 0) {
3849 wpa_s->p2p_auto_pd = 0;
3850 wpa_msg_global(wpa_s, MSG_INFO,
3851 P2P_EVENT_PROV_DISC_FAILURE
3852 " p2p_dev_addr=" MACSTR " status=N/A",
3853 MAC2STR(wpa_s->pending_join_dev_addr));
3858 if (wpa_s->p2p_auto_join) {
3859 int join = wpas_p2p_peer_go(wpa_s,
3860 wpa_s->pending_join_dev_addr);
3862 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3863 "running a GO -> use GO Negotiation");
3864 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3865 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3866 wpa_s->p2p_persistent_group, 0, 0, 0,
3867 wpa_s->p2p_go_intent,
3868 wpa_s->p2p_connect_freq,
3869 wpa_s->p2p_persistent_id,
3870 wpa_s->p2p_pd_before_go_neg,
3876 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3877 "try to join the group", join ? "" :
3880 wpa_s->p2p_fallback_to_go_neg = 1;
3883 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3884 wpa_s->pending_join_iface_addr);
3886 p2p_get_interface_addr(wpa_s->global->p2p,
3887 wpa_s->pending_join_dev_addr,
3889 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3891 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3892 "address for join from " MACSTR " to " MACSTR
3893 " based on newly discovered P2P peer entry",
3894 MAC2STR(wpa_s->pending_join_iface_addr),
3895 MAC2STR(iface_addr));
3896 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3899 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3900 wpa_s->pending_join_iface_addr);
3903 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3904 "from P2P peer table: %d MHz", freq);
3906 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
3909 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3910 "from BSS table: %d MHz (SSID %s)", freq,
3911 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3916 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3917 wpa_msg_global(wpa_s->parent, MSG_INFO,
3918 P2P_EVENT_GROUP_FORMATION_FAILURE
3919 "reason=FREQ_CONFLICT");
3923 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3924 "prior to joining an existing group (GO " MACSTR
3926 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3927 wpa_s->pending_pd_before_join = 1;
3929 switch (wpa_s->pending_join_wps_method) {
3930 case WPS_PIN_DISPLAY:
3931 method = WPS_CONFIG_KEYPAD;
3933 case WPS_PIN_KEYPAD:
3934 method = WPS_CONFIG_DISPLAY;
3937 method = WPS_CONFIG_PUSHBUTTON;
3944 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3945 wpa_s->pending_join_dev_addr) ==
3948 * We have already performed provision discovery for
3949 * joining the group. Proceed directly to join
3950 * operation without duplicated provision discovery. */
3951 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3952 "with " MACSTR " already done - proceed to "
3954 MAC2STR(wpa_s->pending_join_dev_addr));
3955 wpa_s->pending_pd_before_join = 0;
3959 if (p2p_prov_disc_req(wpa_s->global->p2p,
3960 wpa_s->pending_join_dev_addr, method, 1,
3961 freq, wpa_s->user_initiated_pd) < 0) {
3962 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3963 "Discovery Request before joining an "
3965 wpa_s->pending_pd_before_join = 0;
3971 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3972 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3973 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3974 wpas_p2p_check_join_scan_limit(wpa_s);
3978 /* Start join operation immediately */
3979 wpas_p2p_join_start(wpa_s);
3983 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3986 struct wpa_driver_scan_params params;
3987 struct wpabuf *wps_ie, *ies;
3989 int freqs[2] = { 0, 0 };
3991 os_memset(¶ms, 0, sizeof(params));
3993 /* P2P Wildcard SSID */
3994 params.num_ssids = 1;
3995 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3996 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3998 wpa_s->wps->dev.p2p = 1;
3999 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4000 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4002 if (wps_ie == NULL) {
4003 wpas_p2p_scan_res_join(wpa_s, NULL);
4007 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4008 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4010 wpabuf_free(wps_ie);
4011 wpas_p2p_scan_res_join(wpa_s, NULL);
4014 wpabuf_put_buf(ies, wps_ie);
4015 wpabuf_free(wps_ie);
4017 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4019 params.p2p_probe = 1;
4020 params.extra_ies = wpabuf_head(ies);
4021 params.extra_ies_len = wpabuf_len(ies);
4024 params.freqs = freqs;
4028 * Run a scan to update BSS table and start Provision Discovery once
4029 * the new scan results become available.
4031 ret = wpa_drv_scan(wpa_s, ¶ms);
4033 os_get_reltime(&wpa_s->scan_trigger_time);
4034 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4035 wpa_s->own_scan_requested = 1;
4041 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4043 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4044 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4045 wpas_p2p_check_join_scan_limit(wpa_s);
4050 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4052 struct wpa_supplicant *wpa_s = eloop_ctx;
4053 wpas_p2p_join_scan_req(wpa_s, 0);
4057 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4058 const u8 *dev_addr, enum p2p_wps_method wps_method,
4061 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4062 MACSTR " dev " MACSTR ")%s",
4063 MAC2STR(iface_addr), MAC2STR(dev_addr),
4064 auto_join ? " (auto_join)" : "");
4066 wpa_s->p2p_auto_pd = 0;
4067 wpa_s->p2p_auto_join = !!auto_join;
4068 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4069 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4070 wpa_s->pending_join_wps_method = wps_method;
4072 /* Make sure we are not running find during connection establishment */
4073 wpas_p2p_stop_find(wpa_s);
4075 wpa_s->p2p_join_scan_count = 0;
4076 wpas_p2p_join_scan(wpa_s, NULL);
4081 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
4083 struct wpa_supplicant *group;
4084 struct p2p_go_neg_results res;
4085 struct wpa_bss *bss;
4087 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4090 if (group != wpa_s) {
4091 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4092 sizeof(group->p2p_pin));
4093 group->p2p_wps_method = wpa_s->p2p_wps_method;
4096 * Need to mark the current interface for p2p_group_formation
4097 * when a separate group interface is not used. This is needed
4098 * to allow p2p_cancel stop a pending p2p_connect-join.
4099 * wpas_p2p_init_group_interface() addresses this for the case
4100 * where a separate group interface is used.
4102 wpa_s->global->p2p_group_formation = wpa_s;
4105 group->p2p_in_provisioning = 1;
4106 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4108 os_memset(&res, 0, sizeof(res));
4109 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4111 res.wps_method = wpa_s->pending_join_wps_method;
4112 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
4114 res.freq = bss->freq;
4115 res.ssid_len = bss->ssid_len;
4116 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4117 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
4118 "from BSS table: %d MHz (SSID %s)", bss->freq,
4119 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4122 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4123 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4125 wpa_drv_cancel_remain_on_channel(wpa_s);
4126 wpa_s->off_channel_freq = 0;
4127 wpa_s->roc_waiting_drv_freq = 0;
4129 wpas_start_wps_enrollee(group, &res);
4132 * Allow a longer timeout for join-a-running-group than normal 15
4133 * second group formation timeout since the GO may not have authorized
4134 * our connection yet.
4136 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4137 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4144 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4145 int *force_freq, int *pref_freq, int go)
4148 unsigned int freq_in_use = 0, num, i;
4150 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4154 num = get_shared_radio_freqs(wpa_s, freqs,
4155 wpa_s->num_multichan_concurrent);
4156 wpa_printf(MSG_DEBUG,
4157 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4158 freq, wpa_s->num_multichan_concurrent, num);
4163 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4165 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4167 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4168 "(%u MHz) is not supported for P2P uses",
4174 for (i = 0; i < num; i++) {
4175 if (freqs[i] == freq)
4179 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4180 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4185 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4186 "requested channel (%u MHz)", freq);
4191 for (i = 0; i < num; i++) {
4192 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4195 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
4196 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4198 *pref_freq = freqs[i];
4200 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4202 *force_freq = freqs[i];
4208 if (num < wpa_s->num_multichan_concurrent && num > 0) {
4209 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4211 } else if (num < wpa_s->num_multichan_concurrent) {
4212 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4215 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4230 * wpas_p2p_connect - Request P2P Group Formation to be started
4231 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4232 * @peer_addr: Address of the peer P2P Device
4233 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4234 * @persistent_group: Whether to create a persistent group
4235 * @auto_join: Whether to select join vs. GO Negotiation automatically
4236 * @join: Whether to join an existing group (as a client) instead of starting
4237 * Group Owner negotiation; @peer_addr is BSSID in that case
4238 * @auth: Whether to only authorize the connection instead of doing that and
4239 * initiating Group Owner negotiation
4240 * @go_intent: GO Intent or -1 to use default
4241 * @freq: Frequency for the group or 0 for auto-selection
4242 * @persistent_id: Persistent group credentials to use for forcing GO
4243 * parameters or -1 to generate new values (SSID/passphrase)
4244 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4245 * interoperability workaround when initiating group formation
4246 * @ht40: Start GO with 40 MHz channel width
4247 * @vht: Start GO with VHT support
4248 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4249 * failure, -2 on failure due to channel not currently available,
4250 * -3 if forced channel is not supported
4252 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4253 const char *pin, enum p2p_wps_method wps_method,
4254 int persistent_group, int auto_join, int join, int auth,
4255 int go_intent, int freq, int persistent_id, int pd,
4258 int force_freq = 0, pref_freq = 0;
4260 enum wpa_driver_if_type iftype;
4262 struct wpa_ssid *ssid = NULL;
4264 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4267 if (persistent_id >= 0) {
4268 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4269 if (ssid == NULL || ssid->disabled != 2 ||
4270 ssid->mode != WPAS_MODE_P2P_GO)
4274 os_free(wpa_s->global->add_psk);
4275 wpa_s->global->add_psk = NULL;
4278 go_intent = wpa_s->conf->p2p_go_intent;
4281 wpa_s->p2p_long_listen = 0;
4283 wpa_s->p2p_wps_method = wps_method;
4284 wpa_s->p2p_persistent_group = !!persistent_group;
4285 wpa_s->p2p_persistent_id = persistent_id;
4286 wpa_s->p2p_go_intent = go_intent;
4287 wpa_s->p2p_connect_freq = freq;
4288 wpa_s->p2p_fallback_to_go_neg = 0;
4289 wpa_s->p2p_pd_before_go_neg = !!pd;
4290 wpa_s->p2p_go_ht40 = !!ht40;
4291 wpa_s->p2p_go_vht = !!vht;
4294 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4295 else if (wps_method == WPS_PIN_DISPLAY) {
4296 ret = wps_generate_pin();
4297 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4299 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4302 wpa_s->p2p_pin[0] = '\0';
4304 if (join || auto_join) {
4305 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4307 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4308 "connect a running group from " MACSTR,
4309 MAC2STR(peer_addr));
4310 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4313 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4314 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4316 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4317 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4321 os_get_reltime(&wpa_s->p2p_auto_started);
4322 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4324 wpa_s->p2p_auto_started.sec,
4325 wpa_s->p2p_auto_started.usec);
4327 wpa_s->user_initiated_pd = 1;
4328 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4334 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4338 wpas_p2p_set_own_freq_preference(wpa_s,
4339 force_freq ? force_freq : pref_freq);
4341 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4343 if (wpa_s->create_p2p_iface) {
4344 /* Prepare to add a new interface for the group */
4345 iftype = WPA_IF_P2P_GROUP;
4346 if (go_intent == 15)
4347 iftype = WPA_IF_P2P_GO;
4348 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4349 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4350 "interface for the group");
4354 if_addr = wpa_s->pending_interface_addr;
4356 if_addr = wpa_s->own_addr;
4359 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
4361 force_freq, persistent_group, ssid,
4367 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4368 go_intent, if_addr, force_freq,
4369 persistent_group, ssid, pref_freq) < 0) {
4370 if (wpa_s->create_p2p_iface)
4371 wpas_p2p_remove_pending_group_interface(wpa_s);
4379 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4380 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4381 * @freq: Frequency of the channel in MHz
4382 * @duration: Duration of the stay on the channel in milliseconds
4384 * This callback is called when the driver indicates that it has started the
4385 * requested remain-on-channel duration.
4387 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4388 unsigned int freq, unsigned int duration)
4390 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4392 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4393 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4394 wpa_s->pending_listen_duration);
4395 wpa_s->pending_listen_freq = 0;
4400 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4401 unsigned int timeout)
4403 /* Limit maximum Listen state time based on driver limitation. */
4404 if (timeout > wpa_s->max_remain_on_chan)
4405 timeout = wpa_s->max_remain_on_chan;
4407 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4408 return wpa_drv_p2p_listen(wpa_s, timeout);
4410 return p2p_listen(wpa_s->global->p2p, timeout);
4415 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4416 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4417 * @freq: Frequency of the channel in MHz
4419 * This callback is called when the driver indicates that a remain-on-channel
4420 * operation has been completed, i.e., the duration on the requested channel
4423 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4426 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4427 "(p2p_long_listen=%d ms pending_action_tx=%p)",
4428 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
4429 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4431 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4432 return; /* P2P module started a new operation */
4433 if (offchannel_pending_action_tx(wpa_s))
4435 if (wpa_s->p2p_long_listen > 0)
4436 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4437 if (wpa_s->p2p_long_listen > 0) {
4438 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4439 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4442 * When listen duration is over, stop listen & update p2p_state
4445 p2p_stop_listen(wpa_s->global->p2p);
4451 * wpas_p2p_group_remove - Remove a P2P group
4452 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4453 * @ifname: Network interface name of the group interface or "*" to remove all
4455 * Returns: 0 on success, -1 on failure
4457 * This function is used to remove a P2P group. This can be used to disconnect
4458 * from a group in which the local end is a P2P Client or to end a P2P Group in
4459 * case the local end is the Group Owner. If a virtual network interface was
4460 * created for this group, that interface will be removed. Otherwise, only the
4461 * configured P2P group network will be removed from the interface.
4463 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4465 struct wpa_global *global = wpa_s->global;
4467 if (os_strcmp(ifname, "*") == 0) {
4468 struct wpa_supplicant *prev;
4469 wpa_s = global->ifaces;
4472 wpa_s = wpa_s->next;
4473 if (prev->p2p_group_interface !=
4474 NOT_P2P_GROUP_INTERFACE ||
4475 (prev->current_ssid &&
4476 prev->current_ssid->p2p_group))
4477 wpas_p2p_disconnect(prev);
4482 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4483 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4487 return wpas_p2p_disconnect(wpa_s);
4491 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4496 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4498 if (wpa_s->best_24_freq > 0 &&
4499 p2p_supported_freq_go(wpa_s->global->p2p,
4500 wpa_s->best_24_freq)) {
4501 freq = wpa_s->best_24_freq;
4502 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4503 "channel: %d MHz", freq);
4505 os_get_random((u8 *) &r, sizeof(r));
4506 freq = 2412 + (r % 3) * 25;
4507 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4508 "channel: %d MHz", freq);
4513 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4515 if (wpa_s->best_5_freq > 0 &&
4516 p2p_supported_freq_go(wpa_s->global->p2p,
4517 wpa_s->best_5_freq)) {
4518 freq = wpa_s->best_5_freq;
4519 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4520 "channel: %d MHz", freq);
4522 os_get_random((u8 *) &r, sizeof(r));
4523 freq = 5180 + (r % 4) * 20;
4524 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4525 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4526 "5 GHz channel for P2P group");
4529 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4530 "channel: %d MHz", freq);
4534 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4535 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4536 "(%u MHz) is not supported for P2P uses",
4545 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4546 struct p2p_go_neg_results *params,
4547 int freq, int ht40, int vht,
4548 const struct p2p_channels *channels)
4551 unsigned int pref_freq;
4552 unsigned int num, i;
4554 os_memset(params, 0, sizeof(*params));
4555 params->role_go = 1;
4556 params->ht40 = ht40;
4559 if (!freq_included(channels, freq)) {
4560 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4564 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4565 "frequency %d MHz", freq);
4566 params->freq = freq;
4567 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4568 wpa_s->conf->p2p_oper_channel >= 1 &&
4569 wpa_s->conf->p2p_oper_channel <= 11 &&
4570 freq_included(channels,
4571 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
4572 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4573 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4574 "frequency %d MHz", params->freq);
4575 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4576 wpa_s->conf->p2p_oper_reg_class == 116 ||
4577 wpa_s->conf->p2p_oper_reg_class == 117 ||
4578 wpa_s->conf->p2p_oper_reg_class == 124 ||
4579 wpa_s->conf->p2p_oper_reg_class == 126 ||
4580 wpa_s->conf->p2p_oper_reg_class == 127) &&
4581 freq_included(channels,
4582 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
4583 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4584 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4585 "frequency %d MHz", params->freq);
4586 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4587 wpa_s->best_overall_freq > 0 &&
4588 p2p_supported_freq_go(wpa_s->global->p2p,
4589 wpa_s->best_overall_freq) &&
4590 freq_included(channels, wpa_s->best_overall_freq)) {
4591 params->freq = wpa_s->best_overall_freq;
4592 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4593 "channel %d MHz", params->freq);
4594 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4595 wpa_s->best_24_freq > 0 &&
4596 p2p_supported_freq_go(wpa_s->global->p2p,
4597 wpa_s->best_24_freq) &&
4598 freq_included(channels, wpa_s->best_24_freq)) {
4599 params->freq = wpa_s->best_24_freq;
4600 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4601 "channel %d MHz", params->freq);
4602 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4603 wpa_s->best_5_freq > 0 &&
4604 p2p_supported_freq_go(wpa_s->global->p2p,
4605 wpa_s->best_5_freq) &&
4606 freq_included(channels, wpa_s->best_5_freq)) {
4607 params->freq = wpa_s->best_5_freq;
4608 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4609 "channel %d MHz", params->freq);
4610 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4612 params->freq = pref_freq;
4613 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4614 "channels", params->freq);
4617 for (chan = 0; chan < 11; chan++) {
4618 params->freq = 2412 + chan * 5;
4619 if (!wpas_p2p_disallowed_freq(wpa_s->global,
4621 freq_included(channels, params->freq))
4625 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4629 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4630 "known)", params->freq);
4633 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4637 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4638 wpa_s->num_multichan_concurrent);
4645 for (i = 0; i < num; i++) {
4646 if (freq && freqs[i] == freq)
4648 if (!freq && freq_included(channels, freqs[i])) {
4649 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4651 params->freq = freqs[i];
4657 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
4659 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4661 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4664 } else if (num == 0) {
4665 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
4667 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4676 static struct wpa_supplicant *
4677 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4680 struct wpa_supplicant *group_wpa_s;
4682 if (!wpas_p2p_create_iface(wpa_s)) {
4683 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4685 wpa_s->p2p_first_connection_timeout = 0;
4689 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
4690 WPA_IF_P2P_CLIENT) < 0) {
4691 wpa_msg_global(wpa_s, MSG_ERROR,
4692 "P2P: Failed to add group interface");
4695 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4696 if (group_wpa_s == NULL) {
4697 wpa_msg_global(wpa_s, MSG_ERROR,
4698 "P2P: Failed to initialize group interface");
4699 wpas_p2p_remove_pending_group_interface(wpa_s);
4703 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4704 group_wpa_s->ifname);
4705 group_wpa_s->p2p_first_connection_timeout = 0;
4711 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4712 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4713 * @persistent_group: Whether to create a persistent group
4714 * @freq: Frequency for the group or 0 to indicate no hardcoding
4715 * @ht40: Start GO with 40 MHz channel width
4716 * @vht: Start GO with VHT support
4717 * Returns: 0 on success, -1 on failure
4719 * This function creates a new P2P group with the local end as the Group Owner,
4720 * i.e., without using Group Owner Negotiation.
4722 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
4723 int freq, int ht40, int vht)
4725 struct p2p_go_neg_results params;
4727 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4730 os_free(wpa_s->global->add_psk);
4731 wpa_s->global->add_psk = NULL;
4733 /* Make sure we are not running find during connection establishment */
4734 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
4735 wpas_p2p_stop_find_oper(wpa_s);
4737 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4741 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
4744 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
4745 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4746 "(%u MHz) is not supported for P2P uses",
4750 p2p_go_params(wpa_s->global->p2p, ¶ms);
4751 params.persistent_group = persistent_group;
4753 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4756 wpas_start_wps_go(wpa_s, ¶ms, 0);
4762 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4763 struct wpa_ssid *params, int addr_allocated)
4765 struct wpa_ssid *ssid;
4767 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4770 wpa_s->p2p_last_4way_hs_fail = NULL;
4772 wpa_supplicant_ap_deinit(wpa_s);
4774 ssid = wpa_config_add_network(wpa_s->conf);
4777 wpa_config_set_network_defaults(ssid);
4778 ssid->temporary = 1;
4779 ssid->proto = WPA_PROTO_RSN;
4780 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4781 ssid->group_cipher = WPA_CIPHER_CCMP;
4782 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4783 ssid->ssid = os_malloc(params->ssid_len);
4784 if (ssid->ssid == NULL) {
4785 wpa_config_remove_network(wpa_s->conf, ssid->id);
4788 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4789 ssid->ssid_len = params->ssid_len;
4790 ssid->p2p_group = 1;
4791 ssid->export_keys = 1;
4792 if (params->psk_set) {
4793 os_memcpy(ssid->psk, params->psk, 32);
4796 if (params->passphrase)
4797 ssid->passphrase = os_strdup(params->passphrase);
4799 wpa_s->show_group_started = 1;
4801 wpa_supplicant_select_network(wpa_s, ssid);
4807 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4808 struct wpa_ssid *ssid, int addr_allocated,
4809 int freq, int ht40, int vht,
4810 const struct p2p_channels *channels,
4811 int connection_timeout)
4813 struct p2p_go_neg_results params;
4816 if (ssid->disabled != 2 || ssid->ssid == NULL)
4819 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4820 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4821 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4826 os_free(wpa_s->global->add_psk);
4827 wpa_s->global->add_psk = NULL;
4829 /* Make sure we are not running find during connection establishment */
4830 wpas_p2p_stop_find_oper(wpa_s);
4832 wpa_s->p2p_fallback_to_go_neg = 0;
4834 if (ssid->mode == WPAS_MODE_INFRA)
4835 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4837 if (ssid->mode != WPAS_MODE_P2P_GO)
4840 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4844 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
4848 params.psk_set = ssid->psk_set;
4850 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
4851 if (ssid->passphrase) {
4852 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4853 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4854 "persistent group");
4857 os_strlcpy(params.passphrase, ssid->passphrase,
4858 sizeof(params.passphrase));
4860 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4861 params.ssid_len = ssid->ssid_len;
4862 params.persistent_group = 1;
4864 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4868 wpa_s->p2p_first_connection_timeout = connection_timeout;
4869 wpas_start_wps_go(wpa_s, ¶ms, 0);
4875 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4876 struct wpabuf *proberesp_ies)
4878 struct wpa_supplicant *wpa_s = ctx;
4879 if (wpa_s->ap_iface) {
4880 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
4881 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4882 wpabuf_free(beacon_ies);
4883 wpabuf_free(proberesp_ies);
4887 wpabuf_free(hapd->p2p_beacon_ie);
4888 hapd->p2p_beacon_ie = beacon_ies;
4890 wpabuf_free(hapd->p2p_probe_resp_ie);
4891 hapd->p2p_probe_resp_ie = proberesp_ies;
4893 wpabuf_free(beacon_ies);
4894 wpabuf_free(proberesp_ies);
4896 wpa_supplicant_ap_update_beacon(wpa_s);
4900 static void wpas_p2p_idle_update(void *ctx, int idle)
4902 struct wpa_supplicant *wpa_s = ctx;
4903 if (!wpa_s->ap_iface)
4905 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4907 wpas_p2p_set_group_idle_timeout(wpa_s);
4909 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4913 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
4914 struct wpa_ssid *ssid)
4916 struct p2p_group *group;
4917 struct p2p_group_config *cfg;
4919 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4921 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4924 cfg = os_zalloc(sizeof(*cfg));
4928 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4929 cfg->persistent_group = 2;
4930 else if (ssid->p2p_persistent_group)
4931 cfg->persistent_group = 1;
4932 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4933 if (wpa_s->max_stations &&
4934 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4935 cfg->max_clients = wpa_s->max_stations;
4937 cfg->max_clients = wpa_s->conf->max_num_sta;
4938 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4939 cfg->ssid_len = ssid->ssid_len;
4940 cfg->cb_ctx = wpa_s;
4941 cfg->ie_update = wpas_p2p_ie_update;
4942 cfg->idle_update = wpas_p2p_idle_update;
4944 group = p2p_group_init(wpa_s->global->p2p, cfg);
4947 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4948 p2p_group_notif_formation_done(group);
4949 wpa_s->p2p_group = group;
4954 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4957 struct wpa_ssid *ssid = wpa_s->current_ssid;
4959 if (!wpa_s->p2p_in_provisioning) {
4960 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4961 "provisioning not in progress");
4965 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4966 u8 go_dev_addr[ETH_ALEN];
4967 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4968 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4970 /* Clear any stored provisioning info */
4971 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4974 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4976 wpa_s->p2p_go_group_formation_completed = 1;
4977 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4979 * Use a separate timeout for initial data connection to
4980 * complete to allow the group to be removed automatically if
4981 * something goes wrong in this step before the P2P group idle
4982 * timeout mechanism is taken into use.
4984 wpa_dbg(wpa_s, MSG_DEBUG,
4985 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
4986 P2P_MAX_INITIAL_CONN_WAIT);
4987 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4988 wpas_p2p_group_formation_timeout,
4989 wpa_s->parent, NULL);
4992 * Use a separate timeout for initial data connection to
4993 * complete to allow the group to be removed automatically if
4994 * the client does not complete data connection successfully.
4996 wpa_dbg(wpa_s, MSG_DEBUG,
4997 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
4998 P2P_MAX_INITIAL_CONN_WAIT_GO);
4999 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5000 wpas_p2p_group_formation_timeout,
5001 wpa_s->parent, NULL);
5003 * Complete group formation on first successful data connection
5005 wpa_s->p2p_go_group_formation_completed = 0;
5007 if (wpa_s->global->p2p)
5008 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5009 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5010 wpa_drv_wps_success_cb(wpa_s, peer_addr);
5011 wpas_group_formation_completed(wpa_s, 1);
5015 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5016 struct wps_event_fail *fail)
5018 if (!wpa_s->p2p_in_provisioning) {
5019 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5020 "provisioning not in progress");
5024 if (wpa_s->go_params) {
5025 p2p_clear_provisioning_info(
5027 wpa_s->go_params->peer_device_addr);
5030 wpas_notify_p2p_wps_failed(wpa_s, fail);
5034 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5035 const char *config_method,
5036 enum wpas_p2p_prov_disc_use use)
5040 wpa_s->p2p_fallback_to_go_neg = 0;
5041 wpa_s->pending_pd_use = NORMAL_PD;
5042 if (os_strncmp(config_method, "display", 7) == 0)
5043 config_methods = WPS_CONFIG_DISPLAY;
5044 else if (os_strncmp(config_method, "keypad", 6) == 0)
5045 config_methods = WPS_CONFIG_KEYPAD;
5046 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5047 os_strncmp(config_method, "pushbutton", 10) == 0)
5048 config_methods = WPS_CONFIG_PUSHBUTTON;
5050 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5054 if (use == WPAS_P2P_PD_AUTO) {
5055 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5056 wpa_s->pending_pd_config_methods = config_methods;
5057 wpa_s->p2p_auto_pd = 1;
5058 wpa_s->p2p_auto_join = 0;
5059 wpa_s->pending_pd_before_join = 0;
5060 wpa_s->auto_pd_scan_retry = 0;
5061 wpas_p2p_stop_find(wpa_s);
5062 wpa_s->p2p_join_scan_count = 0;
5063 os_get_reltime(&wpa_s->p2p_auto_started);
5064 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5065 wpa_s->p2p_auto_started.sec,
5066 wpa_s->p2p_auto_started.usec);
5067 wpas_p2p_join_scan(wpa_s, NULL);
5071 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5072 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
5074 use == WPAS_P2P_PD_FOR_JOIN);
5077 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5080 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
5081 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5086 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5089 return p2p_scan_result_text(ies, ies_len, buf, end);
5093 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5095 if (!offchannel_pending_action_tx(wpa_s))
5098 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5099 "operation request");
5100 offchannel_clear_pending_action_tx(wpa_s);
5104 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5105 enum p2p_discovery_type type,
5106 unsigned int num_req_dev_types, const u8 *req_dev_types,
5107 const u8 *dev_id, unsigned int search_delay)
5109 wpas_p2p_clear_pending_action_tx(wpa_s);
5110 wpa_s->p2p_long_listen = 0;
5112 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5113 return wpa_drv_p2p_find(wpa_s, timeout, type);
5115 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5116 wpa_s->p2p_in_provisioning)
5119 wpa_supplicant_cancel_sched_scan(wpa_s);
5121 return p2p_find(wpa_s->global->p2p, timeout, type,
5122 num_req_dev_types, req_dev_types, dev_id,
5127 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
5129 wpas_p2p_clear_pending_action_tx(wpa_s);
5130 wpa_s->p2p_long_listen = 0;
5131 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5132 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5133 wpa_s->global->p2p_cb_on_scan_complete = 0;
5135 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5136 wpa_drv_p2p_stop_find(wpa_s);
5140 if (wpa_s->global->p2p)
5141 p2p_stop_find(wpa_s->global->p2p);
5147 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5149 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5151 wpas_p2p_remove_pending_group_interface(wpa_s);
5155 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5157 struct wpa_supplicant *wpa_s = eloop_ctx;
5158 wpa_s->p2p_long_listen = 0;
5162 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5166 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5169 wpa_supplicant_cancel_sched_scan(wpa_s);
5170 wpas_p2p_clear_pending_action_tx(wpa_s);
5174 * This is a request for unlimited Listen state. However, at
5175 * least for now, this is mapped to a Listen state for one
5180 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5181 wpa_s->p2p_long_listen = 0;
5184 * Stop previous find/listen operation to avoid trying to request a new
5185 * remain-on-channel operation while the driver is still running the
5188 if (wpa_s->global->p2p)
5189 p2p_stop_find(wpa_s->global->p2p);
5191 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5192 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5193 wpa_s->p2p_long_listen = timeout * 1000;
5194 eloop_register_timeout(timeout, 0,
5195 wpas_p2p_long_listen_timeout,
5203 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5204 u8 *buf, size_t len, int p2p_group)
5206 struct wpabuf *p2p_ie;
5209 if (wpa_s->global->p2p_disabled)
5211 if (wpa_s->global->p2p == NULL)
5216 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5217 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5219 wpabuf_free(p2p_ie);
5225 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
5226 const u8 *dst, const u8 *bssid,
5227 const u8 *ie, size_t ie_len, int ssi_signal)
5229 if (wpa_s->global->p2p_disabled)
5231 if (wpa_s->global->p2p == NULL)
5234 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5236 case P2P_PREQ_NOT_P2P:
5237 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5240 case P2P_PREQ_MALFORMED:
5241 case P2P_PREQ_NOT_LISTEN:
5242 case P2P_PREQ_NOT_PROCESSED:
5243 default: /* make gcc happy */
5245 case P2P_PREQ_PROCESSED:
5251 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5252 const u8 *sa, const u8 *bssid,
5253 u8 category, const u8 *data, size_t len, int freq)
5255 if (wpa_s->global->p2p_disabled)
5257 if (wpa_s->global->p2p == NULL)
5260 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5265 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5267 if (wpa_s->global->p2p_disabled)
5269 if (wpa_s->global->p2p == NULL)
5272 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5276 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5278 p2p_group_deinit(wpa_s->p2p_group);
5279 wpa_s->p2p_group = NULL;
5281 wpa_s->ap_configured_cb = NULL;
5282 wpa_s->ap_configured_cb_ctx = NULL;
5283 wpa_s->ap_configured_cb_data = NULL;
5284 wpa_s->connect_without_scan = NULL;
5288 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5290 wpa_s->p2p_long_listen = 0;
5292 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5293 return wpa_drv_p2p_reject(wpa_s, addr);
5295 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5298 return p2p_reject(wpa_s->global->p2p, addr);
5302 /* Invite to reinvoke a persistent group */
5303 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5304 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
5305 int ht40, int vht, int pref_freq)
5307 enum p2p_invite_role role;
5311 int no_pref_freq_given = pref_freq == 0;
5313 wpa_s->global->p2p_invite_group = NULL;
5315 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5317 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5319 wpa_s->p2p_persistent_go_freq = freq;
5320 wpa_s->p2p_go_ht40 = !!ht40;
5321 if (ssid->mode == WPAS_MODE_P2P_GO) {
5322 role = P2P_INVITE_ROLE_GO;
5323 if (peer_addr == NULL) {
5324 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5325 "address in invitation command");
5328 if (wpas_p2p_create_iface(wpa_s)) {
5329 if (wpas_p2p_add_group_interface(wpa_s,
5330 WPA_IF_P2P_GO) < 0) {
5331 wpa_printf(MSG_ERROR, "P2P: Failed to "
5332 "allocate a new interface for the "
5336 bssid = wpa_s->pending_interface_addr;
5338 bssid = wpa_s->own_addr;
5340 role = P2P_INVITE_ROLE_CLIENT;
5341 peer_addr = ssid->bssid;
5343 wpa_s->pending_invite_ssid_id = ssid->id;
5345 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5346 role == P2P_INVITE_ROLE_GO);
5350 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5351 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5352 ssid->ssid, ssid->ssid_len,
5355 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5358 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5359 no_pref_freq_given && pref_freq > 0 &&
5360 wpa_s->num_multichan_concurrent > 1 &&
5361 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5362 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5367 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5368 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5373 /* Invite to join an active group */
5374 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5375 const u8 *peer_addr, const u8 *go_dev_addr)
5377 struct wpa_global *global = wpa_s->global;
5378 enum p2p_invite_role role;
5380 struct wpa_ssid *ssid;
5382 int freq = 0, force_freq = 0, pref_freq = 0;
5385 wpa_s->p2p_persistent_go_freq = 0;
5386 wpa_s->p2p_go_ht40 = 0;
5387 wpa_s->p2p_go_vht = 0;
5389 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5390 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5393 if (wpa_s == NULL) {
5394 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5398 ssid = wpa_s->current_ssid;
5400 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5405 wpa_s->global->p2p_invite_group = wpa_s;
5406 persistent = ssid->p2p_persistent_group &&
5407 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5408 ssid->ssid, ssid->ssid_len);
5410 if (ssid->mode == WPAS_MODE_P2P_GO) {
5411 role = P2P_INVITE_ROLE_ACTIVE_GO;
5412 bssid = wpa_s->own_addr;
5413 if (go_dev_addr == NULL)
5414 go_dev_addr = wpa_s->global->p2p_dev_addr;
5415 freq = ssid->frequency;
5417 role = P2P_INVITE_ROLE_CLIENT;
5418 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5419 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5420 "invite to current group");
5423 bssid = wpa_s->bssid;
5424 if (go_dev_addr == NULL &&
5425 !is_zero_ether_addr(wpa_s->go_dev_addr))
5426 go_dev_addr = wpa_s->go_dev_addr;
5427 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5428 (int) wpa_s->assoc_freq;
5430 wpa_s->parent->pending_invite_ssid_id = -1;
5432 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5433 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5434 ssid->ssid, ssid->ssid_len,
5435 go_dev_addr, persistent);
5437 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5440 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5441 role == P2P_INVITE_ROLE_ACTIVE_GO);
5444 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
5446 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5447 ssid->ssid, ssid->ssid_len, force_freq,
5448 go_dev_addr, persistent, pref_freq);
5452 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5454 struct wpa_ssid *ssid = wpa_s->current_ssid;
5455 const char *ssid_txt;
5456 u8 go_dev_addr[ETH_ALEN];
5457 int network_id = -1;
5461 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5462 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5463 wpa_s->parent, NULL);
5466 if (!wpa_s->show_group_started || !ssid)
5469 wpa_s->show_group_started = 0;
5471 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5472 os_memset(go_dev_addr, 0, ETH_ALEN);
5473 if (ssid->bssid_set)
5474 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5475 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5477 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5479 if (wpa_s->global->p2p_group_formation == wpa_s)
5480 wpa_s->global->p2p_group_formation = NULL;
5482 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5483 (int) wpa_s->assoc_freq;
5484 if (ssid->passphrase == NULL && ssid->psk_set) {
5486 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5487 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5488 "%s client ssid=\"%s\" freq=%d psk=%s "
5489 "go_dev_addr=" MACSTR "%s",
5490 wpa_s->ifname, ssid_txt, freq, psk,
5491 MAC2STR(go_dev_addr),
5492 persistent ? " [PERSISTENT]" : "");
5494 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5495 "%s client ssid=\"%s\" freq=%d "
5496 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5497 wpa_s->ifname, ssid_txt, freq,
5498 ssid->passphrase ? ssid->passphrase : "",
5499 MAC2STR(go_dev_addr),
5500 persistent ? " [PERSISTENT]" : "");
5504 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5507 network_id = ssid->id;
5508 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
5511 wpas_p2p_continue_after_scan(wpa_s);
5515 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5516 u32 interval1, u32 duration2, u32 interval2)
5518 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5520 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5523 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5524 wpa_s->current_ssid == NULL ||
5525 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5528 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5529 wpa_s->own_addr, wpa_s->assoc_freq,
5530 duration1, interval1, duration2, interval2);
5534 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5535 unsigned int interval)
5537 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5540 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5543 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5547 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5549 if (wpa_s->current_ssid == NULL) {
5551 * current_ssid can be cleared when P2P client interface gets
5552 * disconnected, so assume this interface was used as P2P
5557 return wpa_s->current_ssid->p2p_group &&
5558 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5562 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5564 struct wpa_supplicant *wpa_s = eloop_ctx;
5566 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
5567 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5572 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5574 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
5578 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5582 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5583 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5585 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5588 timeout = wpa_s->conf->p2p_group_idle;
5589 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5590 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5591 timeout = P2P_MAX_CLIENT_IDLE;
5597 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5598 timeout = 0; /* special client mode no-timeout */
5603 if (wpa_s->p2p_in_provisioning) {
5605 * Use the normal group formation timeout during the
5606 * provisioning phase to avoid terminating this process too
5607 * early due to group idle timeout.
5609 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5610 "during provisioning");
5614 if (wpa_s->show_group_started) {
5616 * Use the normal group formation timeout between the end of
5617 * the provisioning phase and completion of 4-way handshake to
5618 * avoid terminating this process too early due to group idle
5621 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5622 "while waiting for initial 4-way handshake to "
5627 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
5629 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5634 /* Returns 1 if the interface was removed */
5635 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5636 u16 reason_code, const u8 *ie, size_t ie_len,
5637 int locally_generated)
5639 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5641 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5644 if (!locally_generated)
5645 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5648 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5649 wpa_s->current_ssid &&
5650 wpa_s->current_ssid->p2p_group &&
5651 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5652 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5653 "session is ending");
5654 if (wpas_p2p_group_delete(wpa_s,
5655 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5664 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5665 u16 reason_code, const u8 *ie, size_t ie_len,
5666 int locally_generated)
5668 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5670 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5673 if (!locally_generated)
5674 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5679 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5681 struct p2p_data *p2p = wpa_s->global->p2p;
5686 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5689 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5690 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5692 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5693 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5696 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5697 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5699 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5700 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5702 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5703 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5704 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5705 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5706 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5709 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5710 p2p_set_sec_dev_types(p2p,
5711 (void *) wpa_s->conf->sec_device_type,
5712 wpa_s->conf->num_sec_device_types);
5714 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5716 p2p_remove_wps_vendor_extensions(p2p);
5717 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5718 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5720 p2p_add_wps_vendor_extension(
5721 p2p, wpa_s->conf->wps_vendor_ext[i]);
5725 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5726 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5728 country[0] = wpa_s->conf->country[0];
5729 country[1] = wpa_s->conf->country[1];
5731 p2p_set_country(p2p, country);
5734 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5735 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5736 wpa_s->conf->p2p_ssid_postfix ?
5737 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5741 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5742 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
5744 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5745 u8 reg_class, channel;
5748 if (wpa_s->conf->p2p_listen_reg_class &&
5749 wpa_s->conf->p2p_listen_channel) {
5750 reg_class = wpa_s->conf->p2p_listen_reg_class;
5751 channel = wpa_s->conf->p2p_listen_channel;
5755 * Pick one of the social channels randomly as the
5758 os_get_random((u8 *) &r, sizeof(r));
5759 channel = 1 + (r % 3) * 5;
5761 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5763 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5766 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5767 u8 op_reg_class, op_channel, cfg_op_channel;
5770 if (wpa_s->conf->p2p_oper_reg_class &&
5771 wpa_s->conf->p2p_oper_channel) {
5772 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5773 op_channel = wpa_s->conf->p2p_oper_channel;
5778 * Use random operation channel from (1, 6, 11)
5779 *if no other preference is indicated.
5781 os_get_random((u8 *) &r, sizeof(r));
5782 op_channel = 1 + (r % 3) * 5;
5785 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5788 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5792 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5793 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5794 wpa_s->conf->p2p_pref_chan) < 0) {
5795 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5799 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
5800 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
5807 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5810 if (!wpa_s->ap_iface)
5812 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5817 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5819 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5821 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5824 wpa_s->global->cross_connection = enabled;
5825 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5828 struct wpa_supplicant *iface;
5830 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5832 if (iface->cross_connect_enabled == 0)
5835 iface->cross_connect_enabled = 0;
5836 iface->cross_connect_in_use = 0;
5837 wpa_msg_global(iface->parent, MSG_INFO,
5838 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5840 iface->cross_connect_uplink);
5848 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5850 struct wpa_supplicant *iface;
5852 if (!uplink->global->cross_connection)
5855 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5856 if (!iface->cross_connect_enabled)
5858 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5861 if (iface->ap_iface == NULL)
5863 if (iface->cross_connect_in_use)
5866 iface->cross_connect_in_use = 1;
5867 wpa_msg_global(iface->parent, MSG_INFO,
5868 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5869 iface->ifname, iface->cross_connect_uplink);
5874 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5876 struct wpa_supplicant *iface;
5878 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5879 if (!iface->cross_connect_enabled)
5881 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5884 if (!iface->cross_connect_in_use)
5887 wpa_msg_global(iface->parent, MSG_INFO,
5888 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5889 iface->ifname, iface->cross_connect_uplink);
5890 iface->cross_connect_in_use = 0;
5895 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5897 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5898 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5899 wpa_s->cross_connect_disallowed)
5900 wpas_p2p_disable_cross_connect(wpa_s);
5902 wpas_p2p_enable_cross_connect(wpa_s);
5903 if (!wpa_s->ap_iface &&
5904 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5905 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5909 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5911 wpas_p2p_disable_cross_connect(wpa_s);
5912 if (!wpa_s->ap_iface &&
5913 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5915 wpas_p2p_set_group_idle_timeout(wpa_s);
5919 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5921 struct wpa_supplicant *iface;
5923 if (!wpa_s->global->cross_connection)
5926 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5929 if (iface->drv_flags &
5930 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5932 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5935 wpa_s->cross_connect_enabled = 1;
5936 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5937 sizeof(wpa_s->cross_connect_uplink));
5938 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5939 "%s to %s whenever uplink is available",
5940 wpa_s->ifname, wpa_s->cross_connect_uplink);
5942 if (iface->ap_iface || iface->current_ssid == NULL ||
5943 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5944 iface->cross_connect_disallowed ||
5945 iface->wpa_state != WPA_COMPLETED)
5948 wpa_s->cross_connect_in_use = 1;
5949 wpa_msg_global(wpa_s->parent, MSG_INFO,
5950 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5951 wpa_s->ifname, wpa_s->cross_connect_uplink);
5957 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5959 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5960 !wpa_s->p2p_in_provisioning)
5961 return 0; /* not P2P client operation */
5963 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5965 if (wpa_s != wpa_s->parent)
5966 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5967 wpas_p2p_group_formation_failed(wpa_s);
5972 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5974 struct p2p_channels chan, cli_chan;
5976 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5979 os_memset(&chan, 0, sizeof(chan));
5980 os_memset(&cli_chan, 0, sizeof(cli_chan));
5981 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
5982 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5987 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
5991 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5992 struct wpa_scan_results *scan_res)
5994 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5998 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6000 struct wpa_global *global = wpa_s->global;
6004 if (global->p2p == NULL)
6007 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6009 if (wpa_s->pending_interface_name[0] &&
6010 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6013 peer = p2p_get_go_neg_peer(global->p2p);
6015 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6016 MACSTR, MAC2STR(peer));
6017 p2p_unauthorize(global->p2p, peer);
6021 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6022 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6023 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6027 if (wpa_s->pending_pd_before_join) {
6028 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6029 wpa_s->pending_pd_before_join = 0;
6033 wpas_p2p_stop_find(wpa_s);
6035 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6036 if (wpa_s == global->p2p_group_formation &&
6037 (wpa_s->p2p_in_provisioning ||
6038 wpa_s->parent->pending_interface_type ==
6039 WPA_IF_P2P_CLIENT)) {
6040 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6041 "formation found - cancelling",
6044 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6045 wpa_s->parent, NULL);
6046 if (wpa_s->p2p_in_provisioning) {
6047 wpas_group_formation_completed(wpa_s, 0);
6050 wpas_p2p_group_delete(wpa_s,
6051 P2P_GROUP_REMOVAL_REQUESTED);
6057 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6065 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6067 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6070 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6071 "being available anymore");
6072 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
6076 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6077 int freq_24, int freq_5, int freq_overall)
6079 struct p2p_data *p2p = wpa_s->global->p2p;
6080 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6082 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6086 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6089 struct p2p_data *p2p = wpa_s->global->p2p;
6091 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6094 if (hwaddr_aton(addr, peer))
6097 return p2p_unauthorize(p2p, peer);
6102 * wpas_p2p_disconnect - Disconnect from a P2P Group
6103 * @wpa_s: Pointer to wpa_supplicant data
6104 * Returns: 0 on success, -1 on failure
6106 * This can be used to disconnect from a group in which the local end is a P2P
6107 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6108 * virtual network interface was created for this group, that interface will be
6109 * removed. Otherwise, only the configured P2P group network will be removed
6110 * from the interface.
6112 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6118 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6123 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6127 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6130 ret = p2p_in_progress(wpa_s->global->p2p);
6133 * Check whether there is an ongoing WPS provisioning step (or
6134 * other parts of group formation) on another interface since
6135 * p2p_in_progress() does not report this to avoid issues for
6136 * scans during such provisioning step.
6138 if (wpa_s->global->p2p_group_formation &&
6139 wpa_s->global->p2p_group_formation != wpa_s) {
6140 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6141 "in group formation",
6142 wpa_s->global->p2p_group_formation->ifname);
6147 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6148 struct os_reltime now;
6149 os_get_reltime(&now);
6150 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6151 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
6152 /* Wait for the first client has expired */
6153 wpa_s->global->p2p_go_wait_client.sec = 0;
6155 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6164 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6165 struct wpa_ssid *ssid)
6167 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6168 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6169 wpa_s->parent, NULL) > 0) {
6171 * Remove the network by scheduling the group formation
6172 * timeout to happen immediately. The teardown code
6173 * needs to be scheduled to run asynch later so that we
6174 * don't delete data from under ourselves unexpectedly.
6175 * Calling wpas_p2p_group_formation_timeout directly
6176 * causes a series of crashes in WPS failure scenarios.
6178 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6179 "P2P group network getting removed");
6180 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6181 wpa_s->parent, NULL);
6186 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
6187 const u8 *addr, const u8 *ssid,
6193 for (s = wpa_s->conf->ssid; s; s = s->next) {
6194 if (s->disabled != 2)
6197 (ssid_len != s->ssid_len ||
6198 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6201 if (s->mode == WPAS_MODE_P2P_GO)
6205 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6206 return s; /* peer is GO in the persistent group */
6207 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6209 for (i = 0; i < s->num_p2p_clients; i++) {
6210 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6211 addr, ETH_ALEN) == 0)
6212 return s; /* peer is P2P client in persistent
6221 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6224 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6225 wpa_s->parent, NULL) > 0) {
6227 * This can happen if WPS provisioning step is not terminated
6228 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6229 * peer was able to connect, there is no need to time out group
6230 * formation after this, though. In addition, this is used with
6231 * the initial connection wait on the GO as a separate formation
6232 * timeout and as such, expected to be hit after the initial WPS
6233 * provisioning step.
6235 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6237 if (!wpa_s->p2p_go_group_formation_completed) {
6238 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6239 wpa_s->p2p_go_group_formation_completed = 1;
6240 wpa_s->global->p2p_group_formation = NULL;
6241 wpa_s->p2p_in_provisioning = 0;
6243 wpa_s->global->p2p_go_wait_client.sec = 0;
6246 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6250 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6253 struct wpa_supplicant *group = wpa_s;
6254 if (wpa_s->global->p2p_group_formation)
6255 group = wpa_s->global->p2p_group_formation;
6256 wpa_s = wpa_s->parent;
6257 offchannel_send_action_done(wpa_s);
6259 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
6260 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6261 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6262 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6263 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6264 wpa_s->p2p_persistent_id,
6265 wpa_s->p2p_pd_before_go_neg,
6271 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6273 if (!wpa_s->p2p_fallback_to_go_neg ||
6274 wpa_s->p2p_in_provisioning <= 5)
6277 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6278 return 0; /* peer operating as a GO */
6280 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6281 "fallback to GO Negotiation");
6282 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6288 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6290 struct wpa_supplicant *ifs;
6292 if (wpa_s->wpa_state > WPA_SCANNING) {
6293 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6294 "concurrent operation",
6295 P2P_CONCURRENT_SEARCH_DELAY);
6296 return P2P_CONCURRENT_SEARCH_DELAY;
6299 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6301 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
6302 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6303 "delay due to concurrent operation on "
6305 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6306 return P2P_CONCURRENT_SEARCH_DELAY;
6314 void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
6316 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
6317 "pending anymore (sta_scan_pending=%d "
6318 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
6319 wpa_s->global->p2p_cb_on_scan_complete);
6320 wpa_s->sta_scan_pending = 0;
6322 if (!wpa_s->global->p2p_cb_on_scan_complete)
6324 wpa_s->global->p2p_cb_on_scan_complete = 0;
6326 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6329 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6330 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6331 "continued after successful connection");
6332 p2p_increase_search_delay(wpa_s->global->p2p,
6333 wpas_p2p_search_delay(wpa_s));
6338 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6339 struct wpa_ssid *s, const u8 *addr,
6342 struct psk_list_entry *psk, *tmp;
6345 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6347 if ((iface_addr && !psk->p2p &&
6348 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6349 (!iface_addr && psk->p2p &&
6350 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6351 wpa_dbg(wpa_s, MSG_DEBUG,
6352 "P2P: Remove persistent group PSK list entry for "
6354 MAC2STR(psk->addr), psk->p2p);
6355 dl_list_del(&psk->list);
6365 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6366 const u8 *p2p_dev_addr,
6367 const u8 *psk, size_t psk_len)
6369 struct wpa_ssid *ssid = wpa_s->current_ssid;
6370 struct wpa_ssid *persistent;
6371 struct psk_list_entry *p;
6373 if (psk_len != sizeof(p->psk))
6377 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6378 " p2p_dev_addr=" MACSTR,
6379 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6380 if (is_zero_ether_addr(p2p_dev_addr))
6381 p2p_dev_addr = NULL;
6383 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6387 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6388 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6389 /* To be added to persistent group once created */
6390 if (wpa_s->global->add_psk == NULL) {
6391 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6392 if (wpa_s->global->add_psk == NULL)
6395 p = wpa_s->global->add_psk;
6398 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6401 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6403 os_memcpy(p->psk, psk, psk_len);
6407 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6408 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6412 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6415 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6419 p = os_zalloc(sizeof(*p));
6424 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6427 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6429 os_memcpy(p->psk, psk, psk_len);
6431 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6432 struct psk_list_entry *last;
6433 last = dl_list_last(&persistent->psk_list,
6434 struct psk_list_entry, list);
6435 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6436 MACSTR " (p2p=%u) to make room for a new one",
6437 MAC2STR(last->addr), last->p2p);
6438 dl_list_del(&last->list);
6442 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6443 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6444 p2p_dev_addr == NULL);
6446 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6447 MACSTR, MAC2STR(p2p_dev_addr));
6449 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6452 dl_list_add(&persistent->psk_list, &p->list);
6454 #ifndef CONFIG_NO_CONFIG_WRITE
6455 if (wpa_s->parent->conf->update_config &&
6456 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6457 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6458 #endif /* CONFIG_NO_CONFIG_WRITE */
6462 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6463 struct wpa_ssid *s, const u8 *addr,
6468 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6470 #ifndef CONFIG_NO_CONFIG_WRITE
6471 if (wpa_s->conf->update_config &&
6472 wpa_config_write(wpa_s->confname, wpa_s->conf))
6473 wpa_dbg(wpa_s, MSG_DEBUG,
6474 "P2P: Failed to update configuration");
6475 #endif /* CONFIG_NO_CONFIG_WRITE */
6480 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6481 const u8 *peer, int iface_addr)
6483 struct hostapd_data *hapd;
6484 struct hostapd_wpa_psk *psk, *prev, *rem;
6485 struct sta_info *sta;
6487 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6488 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6491 /* Remove per-station PSK entry */
6492 hapd = wpa_s->ap_iface->bss[0];
6494 psk = hapd->conf->ssid.wpa_psk;
6496 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6498 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6499 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6500 MACSTR " iface_addr=%d",
6501 MAC2STR(peer), iface_addr);
6503 prev->next = psk->next;
6505 hapd->conf->ssid.wpa_psk = psk->next;
6515 /* Disconnect from group */
6517 sta = ap_get_sta(hapd, peer);
6519 sta = ap_get_sta_p2p(hapd, peer);
6521 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6522 " (iface_addr=%d) from group",
6523 MAC2STR(peer), iface_addr);
6524 hostapd_drv_sta_deauth(hapd, sta->addr,
6525 WLAN_REASON_DEAUTH_LEAVING);
6526 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6531 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6535 struct wpa_supplicant *w;
6537 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6539 /* Remove from any persistent group */
6540 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6541 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6544 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6545 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6548 /* Remove from any operating group */
6549 for (w = wpa_s->global->ifaces; w; w = w->next)
6550 wpas_p2p_remove_client_go(w, peer, iface_addr);
6554 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6556 struct wpa_supplicant *wpa_s = eloop_ctx;
6557 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6561 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6563 struct wpa_supplicant *wpa_s = eloop_ctx;
6565 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6566 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6570 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6571 struct wpa_ssid *ssid)
6573 struct wpa_supplicant *iface;
6575 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6576 if (!iface->current_ssid ||
6577 iface->current_ssid->frequency == freq ||
6578 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
6579 !iface->current_ssid->p2p_group))
6582 /* Remove the connection with least priority */
6583 if (!wpas_is_p2p_prioritized(iface)) {
6584 /* STA connection has priority over existing
6585 * P2P connection, so remove the interface. */
6586 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6587 eloop_register_timeout(0, 0,
6588 wpas_p2p_group_freq_conflict,
6590 /* If connection in progress is P2P connection, do not
6591 * proceed for the connection. */
6597 /* P2P connection has priority, disable the STA network
6599 wpa_supplicant_disable_network(wpa_s->global->ifaces,
6601 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
6602 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
6603 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
6605 /* If P2P connection is in progress, continue
6618 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6620 struct wpa_ssid *ssid = wpa_s->current_ssid;
6622 if (ssid == NULL || !ssid->p2p_group)
6625 if (wpa_s->p2p_last_4way_hs_fail &&
6626 wpa_s->p2p_last_4way_hs_fail == ssid) {
6627 u8 go_dev_addr[ETH_ALEN];
6628 struct wpa_ssid *persistent;
6630 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6632 ssid->ssid_len) <= 0) {
6633 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6637 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6638 MACSTR, MAC2STR(go_dev_addr));
6639 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6642 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6643 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6646 wpa_msg_global(wpa_s->parent, MSG_INFO,
6647 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6650 wpa_s->p2p_last_4way_hs_fail = NULL;
6652 * Remove the group from a timeout to avoid issues with caller
6653 * continuing to use the interface if this is on a P2P group
6656 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6661 wpa_s->p2p_last_4way_hs_fail = ssid;