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;
328 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
330 switch (p2p_group_interface) {
331 case P2P_GROUP_INTERFACE_PENDING:
332 return WPA_IF_P2P_GROUP;
333 case P2P_GROUP_INTERFACE_GO:
334 return WPA_IF_P2P_GO;
335 case P2P_GROUP_INTERFACE_CLIENT:
336 return WPA_IF_P2P_CLIENT;
339 return WPA_IF_P2P_GROUP;
343 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
345 size_t ssid_len, int *go)
349 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
350 for (s = wpa_s->conf->ssid; s; s = s->next) {
351 if (s->disabled != 0 || !s->p2p_group ||
352 s->ssid_len != ssid_len ||
353 os_memcmp(ssid, s->ssid, ssid_len) != 0)
355 if (s->mode == WPAS_MODE_P2P_GO &&
356 s != wpa_s->current_ssid)
359 *go = s->mode == WPAS_MODE_P2P_GO;
368 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
369 enum p2p_group_removal_reason removal_reason)
371 struct wpa_ssid *ssid;
375 ssid = wpa_s->current_ssid;
378 * The current SSID was not known, but there may still be a
379 * pending P2P group interface waiting for provisioning or a
380 * P2P group that is trying to reconnect.
382 ssid = wpa_s->conf->ssid;
384 if (ssid->p2p_group && ssid->disabled != 2)
389 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
391 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
396 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
398 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
399 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
400 wpa_s->reassociate = 0;
401 wpa_s->disconnected = 1;
402 wpa_supplicant_deauthenticate(wpa_s,
403 WLAN_REASON_DEAUTH_LEAVING);
407 if (wpa_s->cross_connect_in_use) {
408 wpa_s->cross_connect_in_use = 0;
409 wpa_msg_global(wpa_s->parent, MSG_INFO,
410 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
411 wpa_s->ifname, wpa_s->cross_connect_uplink);
413 switch (removal_reason) {
414 case P2P_GROUP_REMOVAL_REQUESTED:
415 reason = " reason=REQUESTED";
417 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
418 reason = " reason=FORMATION_FAILED";
420 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
421 reason = " reason=IDLE";
423 case P2P_GROUP_REMOVAL_UNAVAILABLE:
424 reason = " reason=UNAVAILABLE";
426 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
427 reason = " reason=GO_ENDING_SESSION";
429 case P2P_GROUP_REMOVAL_PSK_FAILURE:
430 reason = " reason=PSK_FAILURE";
432 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
433 reason = " reason=FREQ_CONFLICT";
439 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
440 wpa_msg_global(wpa_s->parent, MSG_INFO,
441 P2P_EVENT_GROUP_REMOVED "%s %s%s",
442 wpa_s->ifname, gtype, reason);
445 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
446 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
447 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
448 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
449 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
450 wpa_s->parent, NULL) > 0) {
451 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
453 wpa_s->p2p_in_provisioning = 0;
457 * Make sure wait for the first client does not remain active after the
458 * group has been removed.
460 wpa_s->global->p2p_go_wait_client.sec = 0;
462 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
463 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
465 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
466 struct wpa_global *global;
468 enum wpa_driver_if_type type;
469 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
471 global = wpa_s->global;
472 ifname = os_strdup(wpa_s->ifname);
473 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
474 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
475 wpa_s = global->ifaces;
477 wpa_drv_if_remove(wpa_s, type, ifname);
482 if (!wpa_s->p2p_go_group_formation_completed) {
483 wpa_s->global->p2p_group_formation = NULL;
484 wpa_s->p2p_in_provisioning = 0;
487 wpa_s->show_group_started = 0;
488 os_free(wpa_s->go_params);
489 wpa_s->go_params = NULL;
491 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
492 if (ssid && (ssid->p2p_group ||
493 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
494 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
496 if (ssid == wpa_s->current_ssid) {
497 wpa_sm_set_config(wpa_s->wpa, NULL);
498 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
499 wpa_s->current_ssid = NULL;
502 * Networks objects created during any P2P activities are not
503 * exposed out as they might/will confuse certain non-P2P aware
504 * applications since these network objects won't behave like
507 * Likewise, we don't send out network removed signals for such
510 wpa_config_remove_network(wpa_s->conf, id);
511 wpa_supplicant_clear_status(wpa_s);
512 wpa_supplicant_cancel_sched_scan(wpa_s);
513 wpa_s->sta_scan_pending = 0;
515 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
519 wpa_supplicant_ap_deinit(wpa_s);
521 wpa_drv_deinit_p2p_cli(wpa_s);
527 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
529 const u8 *ssid, size_t ssid_len)
537 if (wpa_s->go_params)
538 bssid = wpa_s->go_params->peer_interface_addr;
540 bssid = wpa_s->bssid;
542 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
544 u8 iface_addr[ETH_ALEN];
545 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
547 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
550 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
551 "group is persistent - BSS " MACSTR " not found",
556 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
558 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
561 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
562 "group is persistent - BSS " MACSTR
563 " did not include P2P IE", MAC2STR(bssid));
564 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
565 (u8 *) (bss + 1), bss->ie_len);
566 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
567 ((u8 *) bss + 1) + bss->ie_len,
572 group_capab = p2p_get_group_capab(p2p);
573 addr = p2p_get_go_dev_addr(p2p);
574 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
575 "group_capab=0x%x", group_capab);
577 os_memcpy(go_dev_addr, addr, ETH_ALEN);
578 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
581 os_memset(go_dev_addr, 0, ETH_ALEN);
584 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
585 "go_dev_addr=" MACSTR,
586 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
588 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
592 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
593 struct wpa_ssid *ssid,
594 const u8 *go_dev_addr)
599 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
600 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
601 for (s = wpa_s->conf->ssid; s; s = s->next) {
602 if (s->disabled == 2 &&
603 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
604 s->ssid_len == ssid->ssid_len &&
605 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
610 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
612 if (ssid->passphrase && !s->passphrase)
614 else if (ssid->passphrase && s->passphrase &&
615 os_strcmp(ssid->passphrase, s->passphrase) != 0)
618 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
621 s = wpa_config_add_network(wpa_s->conf);
626 * Instead of network_added we emit persistent_group_added
627 * notification. Also to keep the defense checks in
628 * persistent_group obj registration method, we set the
629 * relevant flags in s to designate it as a persistent group.
632 s->p2p_persistent_group = 1;
633 wpas_notify_persistent_group_added(wpa_s, s);
634 wpa_config_set_network_defaults(s);
638 s->p2p_persistent_group = 1;
641 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
642 s->mode = ssid->mode;
643 s->auth_alg = WPA_AUTH_ALG_OPEN;
644 s->key_mgmt = WPA_KEY_MGMT_PSK;
645 s->proto = WPA_PROTO_RSN;
646 s->pairwise_cipher = WPA_CIPHER_CCMP;
648 if (ssid->passphrase) {
649 os_free(s->passphrase);
650 s->passphrase = os_strdup(ssid->passphrase);
654 os_memcpy(s->psk, ssid->psk, 32);
656 if (s->passphrase && !s->psk_set)
657 wpa_config_update_psk(s);
658 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
660 s->ssid = os_malloc(ssid->ssid_len);
663 s->ssid_len = ssid->ssid_len;
664 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
666 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
667 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
668 wpa_s->global->add_psk = NULL;
672 #ifndef CONFIG_NO_CONFIG_WRITE
673 if (changed && wpa_s->conf->update_config &&
674 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
675 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
677 #endif /* CONFIG_NO_CONFIG_WRITE */
683 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
686 struct wpa_ssid *ssid, *s;
691 ssid = wpa_s->current_ssid;
692 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
693 !ssid->p2p_persistent_group)
696 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
697 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
700 if (s->ssid_len == ssid->ssid_len &&
701 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
708 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
709 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
713 if (i == s->num_p2p_clients - 1)
714 return; /* already the most recent entry */
716 /* move the entry to mark it most recent */
717 os_memmove(s->p2p_client_list + i * ETH_ALEN,
718 s->p2p_client_list + (i + 1) * ETH_ALEN,
719 (s->num_p2p_clients - i - 1) * ETH_ALEN);
720 os_memcpy(s->p2p_client_list +
721 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
726 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
727 n = os_realloc_array(s->p2p_client_list,
728 s->num_p2p_clients + 1, ETH_ALEN);
731 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
732 s->p2p_client_list = n;
733 s->num_p2p_clients++;
735 /* Not enough room for an additional entry - drop the oldest
737 os_memmove(s->p2p_client_list,
738 s->p2p_client_list + ETH_ALEN,
739 (s->num_p2p_clients - 1) * ETH_ALEN);
740 os_memcpy(s->p2p_client_list +
741 (s->num_p2p_clients - 1) * ETH_ALEN,
745 #ifndef CONFIG_NO_CONFIG_WRITE
746 if (wpa_s->parent->conf->update_config &&
747 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
748 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
749 #endif /* CONFIG_NO_CONFIG_WRITE */
753 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
756 struct wpa_ssid *ssid;
757 const char *ssid_txt;
760 u8 go_dev_addr[ETH_ALEN];
764 * This callback is likely called for the main interface. Update wpa_s
765 * to use the group interface if a new interface was created for the
768 if (wpa_s->global->p2p_group_formation)
769 wpa_s = wpa_s->global->p2p_group_formation;
770 if (wpa_s->p2p_go_group_formation_completed) {
771 wpa_s->global->p2p_group_formation = NULL;
772 wpa_s->p2p_in_provisioning = 0;
776 wpa_msg_global(wpa_s->parent, MSG_INFO,
777 P2P_EVENT_GROUP_FORMATION_FAILURE);
778 wpas_p2p_group_delete(wpa_s,
779 P2P_GROUP_REMOVAL_FORMATION_FAILED);
783 wpa_msg_global(wpa_s->parent, MSG_INFO,
784 P2P_EVENT_GROUP_FORMATION_SUCCESS);
786 ssid = wpa_s->current_ssid;
787 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
788 ssid->mode = WPAS_MODE_P2P_GO;
789 p2p_group_notif_formation_done(wpa_s->p2p_group);
790 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
795 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
796 client = ssid->mode == WPAS_MODE_INFRA;
797 if (ssid->mode == WPAS_MODE_P2P_GO) {
798 persistent = ssid->p2p_persistent_group;
799 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
802 persistent = wpas_p2p_persistent_group(wpa_s,
808 client = wpa_s->p2p_group_interface ==
809 P2P_GROUP_INTERFACE_CLIENT;
810 os_memset(go_dev_addr, 0, ETH_ALEN);
813 wpa_s->show_group_started = 0;
816 * Indicate event only after successfully completed 4-way
817 * handshake, i.e., when the interface is ready for data
820 wpa_s->show_group_started = 1;
821 } else if (ssid && ssid->passphrase == NULL && ssid->psk_set) {
823 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
824 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
825 "%s GO ssid=\"%s\" freq=%d psk=%s go_dev_addr="
827 wpa_s->ifname, ssid_txt, ssid->frequency, psk,
828 MAC2STR(go_dev_addr),
829 persistent ? " [PERSISTENT]" : "");
830 wpas_p2p_cross_connect_setup(wpa_s);
831 wpas_p2p_set_group_idle_timeout(wpa_s);
833 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
834 "%s GO ssid=\"%s\" freq=%d passphrase=\"%s\" "
835 "go_dev_addr=" MACSTR "%s",
836 wpa_s->ifname, ssid_txt,
837 ssid ? ssid->frequency : 0,
838 ssid && ssid->passphrase ? ssid->passphrase : "",
839 MAC2STR(go_dev_addr),
840 persistent ? " [PERSISTENT]" : "");
841 wpas_p2p_cross_connect_setup(wpa_s);
842 wpas_p2p_set_group_idle_timeout(wpa_s);
846 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
849 os_free(wpa_s->global->add_psk);
850 wpa_s->global->add_psk = NULL;
852 if (network_id < 0 && ssid)
853 network_id = ssid->id;
855 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
856 os_get_time(&wpa_s->global->p2p_go_wait_client);
861 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
863 const u8 *dst, const u8 *src,
865 const u8 *data, size_t data_len,
866 enum offchannel_send_action_result
869 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
871 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
873 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
877 case OFFCHANNEL_SEND_ACTION_SUCCESS:
878 res = P2P_SEND_ACTION_SUCCESS;
880 case OFFCHANNEL_SEND_ACTION_NO_ACK:
881 res = P2P_SEND_ACTION_NO_ACK;
883 case OFFCHANNEL_SEND_ACTION_FAILED:
884 res = P2P_SEND_ACTION_FAILED;
888 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
890 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
891 wpa_s->pending_pd_before_join &&
892 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
893 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
894 wpa_s->p2p_fallback_to_go_neg) {
895 wpa_s->pending_pd_before_join = 0;
896 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
897 "during p2p_connect-auto");
898 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
904 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
905 const u8 *src, const u8 *bssid, const u8 *buf,
906 size_t len, unsigned int wait_time)
908 struct wpa_supplicant *wpa_s = ctx;
909 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
911 wpas_p2p_send_action_tx_status, 1);
915 static void wpas_send_action_done(void *ctx)
917 struct wpa_supplicant *wpa_s = ctx;
918 offchannel_send_action_done(wpa_s);
922 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
923 struct p2p_go_neg_results *params)
925 if (wpa_s->go_params == NULL) {
926 wpa_s->go_params = os_malloc(sizeof(*params));
927 if (wpa_s->go_params == NULL)
930 os_memcpy(wpa_s->go_params, params, sizeof(*params));
935 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
936 struct p2p_go_neg_results *res)
938 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR,
939 MAC2STR(res->peer_interface_addr));
940 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
941 res->ssid, res->ssid_len);
942 wpa_supplicant_ap_deinit(wpa_s);
943 wpas_copy_go_neg_results(wpa_s, res);
944 if (res->wps_method == WPS_PBC)
945 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
947 u16 dev_pw_id = DEV_PW_DEFAULT;
948 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
949 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
950 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
951 wpa_s->p2p_pin, 1, dev_pw_id);
956 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
957 struct wpa_ssid *ssid)
959 struct wpa_ssid *persistent;
960 struct psk_list_entry *psk;
961 struct hostapd_data *hapd;
963 if (!wpa_s->ap_iface)
966 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
968 if (persistent == NULL)
971 hapd = wpa_s->ap_iface->bss[0];
973 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
975 struct hostapd_wpa_psk *hpsk;
977 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
979 MAC2STR(psk->addr), psk->p2p);
980 hpsk = os_zalloc(sizeof(*hpsk));
983 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
985 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
987 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
988 hpsk->next = hapd->conf->ssid.wpa_psk;
989 hapd->conf->ssid.wpa_psk = hpsk;
994 static void p2p_go_configured(void *ctx, void *data)
996 struct wpa_supplicant *wpa_s = ctx;
997 struct p2p_go_neg_results *params = data;
998 struct wpa_ssid *ssid;
1001 ssid = wpa_s->current_ssid;
1002 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1003 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1004 if (wpa_s->global->p2p_group_formation == wpa_s)
1005 wpa_s->global->p2p_group_formation = NULL;
1006 if (os_strlen(params->passphrase) > 0) {
1007 wpa_msg_global(wpa_s->parent, MSG_INFO,
1008 P2P_EVENT_GROUP_STARTED
1009 "%s GO ssid=\"%s\" freq=%d "
1010 "passphrase=\"%s\" go_dev_addr=" MACSTR
1011 "%s", wpa_s->ifname,
1012 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1013 ssid->frequency, params->passphrase,
1014 MAC2STR(wpa_s->global->p2p_dev_addr),
1015 params->persistent_group ?
1016 " [PERSISTENT]" : "");
1019 wpa_snprintf_hex(psk, sizeof(psk), params->psk,
1020 sizeof(params->psk));
1021 wpa_msg_global(wpa_s->parent, MSG_INFO,
1022 P2P_EVENT_GROUP_STARTED
1023 "%s GO ssid=\"%s\" freq=%d psk=%s "
1024 "go_dev_addr=" MACSTR "%s",
1026 wpa_ssid_txt(ssid->ssid, ssid->ssid_len),
1027 ssid->frequency, psk,
1028 MAC2STR(wpa_s->global->p2p_dev_addr),
1029 params->persistent_group ?
1030 " [PERSISTENT]" : "");
1033 os_get_time(&wpa_s->global->p2p_go_wait_client);
1034 if (params->persistent_group) {
1035 network_id = wpas_p2p_store_persistent_group(
1036 wpa_s->parent, ssid,
1037 wpa_s->global->p2p_dev_addr);
1038 wpas_p2p_add_psk_list(wpa_s, ssid);
1041 network_id = ssid->id;
1042 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1043 wpas_p2p_cross_connect_setup(wpa_s);
1044 wpas_p2p_set_group_idle_timeout(wpa_s);
1046 if (wpa_s->p2p_first_connection_timeout) {
1047 wpa_dbg(wpa_s, MSG_DEBUG,
1048 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1049 wpa_s->p2p_first_connection_timeout);
1050 wpa_s->p2p_go_group_formation_completed = 0;
1051 wpa_s->global->p2p_group_formation = wpa_s;
1052 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1053 wpa_s->parent, NULL);
1054 eloop_register_timeout(
1055 wpa_s->p2p_first_connection_timeout, 0,
1056 wpas_p2p_group_formation_timeout,
1057 wpa_s->parent, NULL);
1063 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1064 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1065 params->peer_interface_addr)) {
1066 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1070 if (params->wps_method == WPS_PBC)
1071 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1072 params->peer_device_addr);
1073 else if (wpa_s->p2p_pin[0])
1074 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1075 wpa_s->p2p_pin, NULL, 0, 0);
1076 os_free(wpa_s->go_params);
1077 wpa_s->go_params = NULL;
1081 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1082 struct p2p_go_neg_results *params,
1083 int group_formation)
1085 struct wpa_ssid *ssid;
1087 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1088 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1089 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1094 ssid = wpa_config_add_network(wpa_s->conf);
1096 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1100 wpa_s->show_group_started = 0;
1102 wpa_config_set_network_defaults(ssid);
1103 ssid->temporary = 1;
1104 ssid->p2p_group = 1;
1105 ssid->p2p_persistent_group = params->persistent_group;
1106 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1108 ssid->frequency = params->freq;
1109 ssid->ht40 = params->ht40;
1110 ssid->vht = params->vht;
1111 ssid->ssid = os_zalloc(params->ssid_len + 1);
1113 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1114 ssid->ssid_len = params->ssid_len;
1116 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1117 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1118 ssid->proto = WPA_PROTO_RSN;
1119 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1120 if (os_strlen(params->passphrase) > 0) {
1121 ssid->passphrase = os_strdup(params->passphrase);
1122 if (ssid->passphrase == NULL) {
1123 wpa_msg_global(wpa_s, MSG_ERROR,
1124 "P2P: Failed to copy passphrase for GO");
1125 wpa_config_remove_network(wpa_s->conf, ssid->id);
1129 ssid->passphrase = NULL;
1130 ssid->psk_set = params->psk_set;
1132 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1133 else if (ssid->passphrase)
1134 wpa_config_update_psk(ssid);
1135 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1137 wpa_s->ap_configured_cb = p2p_go_configured;
1138 wpa_s->ap_configured_cb_ctx = wpa_s;
1139 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1140 wpa_s->connect_without_scan = ssid;
1141 wpa_s->reassociate = 1;
1142 wpa_s->disconnected = 0;
1143 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1145 wpa_supplicant_req_scan(wpa_s, 0, 0);
1149 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1150 const struct wpa_supplicant *src)
1152 struct wpa_config *d;
1153 const struct wpa_config *s;
1158 #define C(n) if (s->n) d->n = os_strdup(s->n)
1167 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1168 os_memcpy(d->sec_device_type, s->sec_device_type,
1169 sizeof(d->sec_device_type));
1170 d->num_sec_device_types = s->num_sec_device_types;
1172 d->p2p_group_idle = s->p2p_group_idle;
1173 d->p2p_intra_bss = s->p2p_intra_bss;
1174 d->persistent_reconnect = s->persistent_reconnect;
1175 d->max_num_sta = s->max_num_sta;
1176 d->pbc_in_m1 = s->pbc_in_m1;
1177 d->ignore_old_scan_res = s->ignore_old_scan_res;
1178 d->beacon_int = s->beacon_int;
1179 d->disassoc_low_ack = s->disassoc_low_ack;
1180 d->disable_scan_offload = s->disable_scan_offload;
1184 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1185 char *ifname, size_t len)
1187 char *ifname_ptr = wpa_s->ifname;
1189 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1190 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1191 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1194 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1195 if (os_strlen(ifname) >= IFNAMSIZ &&
1196 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1197 /* Try to avoid going over the IFNAMSIZ length limit */
1198 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1203 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1204 enum wpa_driver_if_type type)
1206 char ifname[120], force_ifname[120];
1208 if (wpa_s->pending_interface_name[0]) {
1209 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1210 "- skip creation of a new one");
1211 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1212 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1213 "unknown?! ifname='%s'",
1214 wpa_s->pending_interface_name);
1220 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1221 force_ifname[0] = '\0';
1223 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1225 wpa_s->p2p_group_idx++;
1227 wpa_s->pending_interface_type = type;
1228 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1229 wpa_s->pending_interface_addr, NULL) < 0) {
1230 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1235 if (force_ifname[0]) {
1236 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1238 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1239 sizeof(wpa_s->pending_interface_name));
1241 os_strlcpy(wpa_s->pending_interface_name, ifname,
1242 sizeof(wpa_s->pending_interface_name));
1243 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1244 MACSTR, wpa_s->pending_interface_name,
1245 MAC2STR(wpa_s->pending_interface_addr));
1251 static void wpas_p2p_remove_pending_group_interface(
1252 struct wpa_supplicant *wpa_s)
1254 if (!wpa_s->pending_interface_name[0] ||
1255 is_zero_ether_addr(wpa_s->pending_interface_addr))
1256 return; /* No pending virtual interface */
1258 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1259 wpa_s->pending_interface_name);
1260 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1261 wpa_s->pending_interface_name);
1262 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1263 wpa_s->pending_interface_name[0] = '\0';
1267 static struct wpa_supplicant *
1268 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1270 struct wpa_interface iface;
1271 struct wpa_supplicant *group_wpa_s;
1273 if (!wpa_s->pending_interface_name[0]) {
1274 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1275 if (!wpas_p2p_create_iface(wpa_s))
1278 * Something has forced us to remove the pending interface; try
1279 * to create a new one and hope for the best that we will get
1280 * the same local address.
1282 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1283 WPA_IF_P2P_CLIENT) < 0)
1287 os_memset(&iface, 0, sizeof(iface));
1288 iface.ifname = wpa_s->pending_interface_name;
1289 iface.driver = wpa_s->driver->name;
1290 if (wpa_s->conf->ctrl_interface == NULL &&
1291 wpa_s->parent != wpa_s &&
1293 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1294 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1296 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1297 iface.driver_param = wpa_s->conf->driver_param;
1298 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1299 if (group_wpa_s == NULL) {
1300 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1301 "wpa_supplicant interface");
1304 wpa_s->pending_interface_name[0] = '\0';
1305 group_wpa_s->parent = wpa_s;
1306 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1307 P2P_GROUP_INTERFACE_CLIENT;
1308 wpa_s->global->p2p_group_formation = group_wpa_s;
1310 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1316 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1319 struct wpa_supplicant *wpa_s = eloop_ctx;
1320 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1321 wpas_p2p_group_formation_failed(wpa_s);
1325 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1327 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1328 wpa_s->parent, NULL);
1329 if (wpa_s->global->p2p)
1330 p2p_group_formation_failed(wpa_s->global->p2p);
1331 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
1332 wpa_drv_p2p_group_formation_failed(wpa_s);
1333 wpas_group_formation_completed(wpa_s, 0);
1337 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1339 if (wpa_s->global->p2p_group_formation != wpa_s)
1341 /* Speed up group formation timeout since this cannot succeed */
1342 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1343 wpa_s->parent, NULL);
1344 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1345 wpa_s->parent, NULL);
1349 void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1351 struct wpa_supplicant *wpa_s = ctx;
1353 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1354 wpa_drv_cancel_remain_on_channel(wpa_s);
1355 wpa_s->off_channel_freq = 0;
1356 wpa_s->roc_waiting_drv_freq = 0;
1360 wpa_msg_global(wpa_s, MSG_INFO,
1361 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1363 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1364 wpas_p2p_remove_pending_group_interface(wpa_s);
1368 if (wpa_s->p2p_go_ht40)
1370 if (wpa_s->p2p_go_vht)
1373 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1374 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1376 res->role_go ? "GO" : "client", res->freq, res->ht40,
1377 MAC2STR(res->peer_device_addr),
1378 MAC2STR(res->peer_interface_addr),
1379 p2p_wps_method_text(res->wps_method));
1380 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1382 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1383 struct wpa_ssid *ssid;
1384 ssid = wpa_config_get_network(wpa_s->conf,
1385 wpa_s->p2p_persistent_id);
1386 if (ssid && ssid->disabled == 2 &&
1387 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1388 size_t len = os_strlen(ssid->passphrase);
1389 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1390 "on requested persistent group");
1391 os_memcpy(res->passphrase, ssid->passphrase, len);
1392 res->passphrase[len] = '\0';
1396 if (wpa_s->create_p2p_iface) {
1397 struct wpa_supplicant *group_wpa_s =
1398 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1399 if (group_wpa_s == NULL) {
1400 wpas_p2p_remove_pending_group_interface(wpa_s);
1403 if (group_wpa_s != wpa_s) {
1404 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1405 sizeof(group_wpa_s->p2p_pin));
1406 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1408 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1409 wpa_s->pending_interface_name[0] = '\0';
1410 group_wpa_s->p2p_in_provisioning = 1;
1413 wpas_start_wps_go(group_wpa_s, res, 1);
1415 wpas_start_wps_enrollee(group_wpa_s, res);
1417 wpa_s->p2p_in_provisioning = 1;
1418 wpa_s->global->p2p_group_formation = wpa_s;
1421 wpas_start_wps_go(wpa_s, res, 1);
1423 wpas_start_wps_enrollee(ctx, res);
1426 wpa_s->p2p_long_listen = 0;
1427 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1429 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1430 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1431 (res->peer_config_timeout % 100) * 10000,
1432 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1436 void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1438 struct wpa_supplicant *wpa_s = ctx;
1439 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1440 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1442 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1446 void wpas_dev_found(void *ctx, const u8 *addr,
1447 const struct p2p_peer_info *info,
1450 #ifndef CONFIG_NO_STDOUT_DEBUG
1451 struct wpa_supplicant *wpa_s = ctx;
1452 char devtype[WPS_DEV_TYPE_BUFSIZE];
1453 char *wfd_dev_info_hex = NULL;
1455 #ifdef CONFIG_WIFI_DISPLAY
1456 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1457 WFD_SUBELEM_DEVICE_INFO);
1458 #endif /* CONFIG_WIFI_DISPLAY */
1460 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1461 " p2p_dev_addr=" MACSTR
1462 " pri_dev_type=%s name='%s' config_methods=0x%x "
1463 "dev_capab=0x%x group_capab=0x%x%s%s",
1464 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1465 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1467 info->device_name, info->config_methods,
1468 info->dev_capab, info->group_capab,
1469 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1470 wfd_dev_info_hex ? wfd_dev_info_hex : "");
1471 #endif /* CONFIG_NO_STDOUT_DEBUG */
1473 os_free(wfd_dev_info_hex);
1475 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1479 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1481 struct wpa_supplicant *wpa_s = ctx;
1483 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1484 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1486 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1490 static void wpas_find_stopped(void *ctx)
1492 struct wpa_supplicant *wpa_s = ctx;
1493 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1497 static int wpas_start_listen(void *ctx, unsigned int freq,
1498 unsigned int duration,
1499 const struct wpabuf *probe_resp_ie)
1501 struct wpa_supplicant *wpa_s = ctx;
1503 wpa_drv_set_ap_wps_ie(wpa_s, NULL, probe_resp_ie, NULL);
1505 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1506 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1507 "report received Probe Request frames");
1511 wpa_s->pending_listen_freq = freq;
1512 wpa_s->pending_listen_duration = duration;
1514 if (wpa_drv_remain_on_channel(wpa_s, freq, duration) < 0) {
1515 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1516 "to remain on channel (%u MHz) for Listen "
1518 wpa_s->pending_listen_freq = 0;
1521 wpa_s->off_channel_freq = 0;
1522 wpa_s->roc_waiting_drv_freq = freq;
1528 static void wpas_stop_listen(void *ctx)
1530 struct wpa_supplicant *wpa_s = ctx;
1531 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1532 wpa_drv_cancel_remain_on_channel(wpa_s);
1533 wpa_s->off_channel_freq = 0;
1534 wpa_s->roc_waiting_drv_freq = 0;
1536 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1537 wpa_drv_probe_req_report(wpa_s, 0);
1541 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1543 struct wpa_supplicant *wpa_s = ctx;
1544 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1549 * DNS Header section is used only to calculate compression pointers, so the
1550 * contents of this data does not matter, but the length needs to be reserved
1551 * in the virtual packet.
1553 #define DNS_HEADER_LEN 12
1556 * 27-octet in-memory packet from P2P specification containing two implied
1557 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1559 #define P2P_SD_IN_MEMORY_LEN 27
1561 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1562 u8 **spos, const u8 *end)
1564 while (*spos < end) {
1565 u8 val = ((*spos)[0] & 0xc0) >> 6;
1568 if (val == 1 || val == 2) {
1569 /* These are reserved values in RFC 1035 */
1570 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1571 "sequence starting with 0x%x", val);
1580 if (*spos + 2 > end) {
1581 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1582 "DNS offset field");
1586 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1587 if (offset >= *spos - start) {
1588 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1589 "pointer offset %u", offset);
1594 spos_tmp = start + offset;
1595 return p2p_sd_dns_uncompress_label(upos, uend, start,
1601 len = (*spos)[0] & 0x3f;
1606 if (*spos + len > end) {
1607 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1608 "sequence - no room for label with length "
1613 if (*upos + len + 2 > uend)
1616 os_memcpy(*upos, *spos, len);
1628 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1629 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1630 * not large enough */
1631 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1632 size_t msg_len, size_t offset)
1634 /* 27-octet in-memory packet from P2P specification */
1635 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1636 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1637 u8 *tmp, *end, *spos;
1643 if (offset > msg_len)
1646 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1649 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1650 end = spos + msg_len;
1653 os_memset(tmp, 0, DNS_HEADER_LEN);
1654 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1655 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1658 uend = buf + buf_len;
1660 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1669 } else if (upos[-1] == '.')
1677 static struct p2p_srv_bonjour *
1678 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1679 const struct wpabuf *query)
1681 struct p2p_srv_bonjour *bsrv;
1684 len = wpabuf_len(query);
1685 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1686 struct p2p_srv_bonjour, list) {
1687 if (len == wpabuf_len(bsrv->query) &&
1688 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
1696 static struct p2p_srv_upnp *
1697 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
1698 const char *service)
1700 struct p2p_srv_upnp *usrv;
1702 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1703 struct p2p_srv_upnp, list) {
1704 if (version == usrv->version &&
1705 os_strcmp(service, usrv->service) == 0)
1712 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
1717 if (wpabuf_tailroom(resp) < 5)
1720 /* Length (to be filled) */
1721 len_pos = wpabuf_put(resp, 2);
1722 wpabuf_put_u8(resp, srv_proto);
1723 wpabuf_put_u8(resp, srv_trans_id);
1725 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
1726 /* Response Data: empty */
1727 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1731 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
1732 struct wpabuf *resp, u8 srv_trans_id)
1734 struct p2p_srv_bonjour *bsrv;
1737 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
1739 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1740 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1744 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1745 struct p2p_srv_bonjour, list) {
1746 if (wpabuf_tailroom(resp) <
1747 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
1749 /* Length (to be filled) */
1750 len_pos = wpabuf_put(resp, 2);
1751 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1752 wpabuf_put_u8(resp, srv_trans_id);
1754 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1755 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1756 wpabuf_head(bsrv->resp),
1757 wpabuf_len(bsrv->resp));
1759 wpabuf_put_buf(resp, bsrv->query); /* Key */
1760 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1761 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1767 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
1770 char str_rx[256], str_srv[256];
1772 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
1773 return 0; /* Too short to include DNS Type and Version */
1774 if (os_memcmp(query + query_len - 3,
1775 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
1777 return 0; /* Mismatch in DNS Type or Version */
1778 if (query_len == wpabuf_len(bsrv->query) &&
1779 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
1780 return 1; /* Binary match */
1782 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
1784 return 0; /* Failed to uncompress query */
1785 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
1786 wpabuf_head(bsrv->query),
1787 wpabuf_len(bsrv->query) - 3, 0))
1788 return 0; /* Failed to uncompress service */
1790 return os_strcmp(str_rx, str_srv) == 0;
1794 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
1795 struct wpabuf *resp, u8 srv_trans_id,
1796 const u8 *query, size_t query_len)
1798 struct p2p_srv_bonjour *bsrv;
1802 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
1804 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
1805 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
1806 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
1811 if (query_len == 0) {
1812 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
1816 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1817 struct p2p_srv_bonjour, list) {
1818 if (!match_bonjour_query(bsrv, query, query_len))
1821 if (wpabuf_tailroom(resp) <
1822 5 + query_len + wpabuf_len(bsrv->resp))
1827 /* Length (to be filled) */
1828 len_pos = wpabuf_put(resp, 2);
1829 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1830 wpabuf_put_u8(resp, srv_trans_id);
1833 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1834 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
1835 wpabuf_head(bsrv->resp),
1836 wpabuf_len(bsrv->resp));
1839 wpabuf_put_data(resp, query, query_len); /* Key */
1840 wpabuf_put_buf(resp, bsrv->resp); /* Value */
1842 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1846 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
1848 if (wpabuf_tailroom(resp) < 5)
1851 /* Length (to be filled) */
1852 len_pos = wpabuf_put(resp, 2);
1853 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
1854 wpabuf_put_u8(resp, srv_trans_id);
1857 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1858 /* Response Data: empty */
1859 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1865 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
1866 struct wpabuf *resp, u8 srv_trans_id)
1868 struct p2p_srv_upnp *usrv;
1871 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
1873 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1874 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1878 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1879 struct p2p_srv_upnp, list) {
1880 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
1883 /* Length (to be filled) */
1884 len_pos = wpabuf_put(resp, 2);
1885 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1886 wpabuf_put_u8(resp, srv_trans_id);
1889 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1891 wpabuf_put_u8(resp, usrv->version);
1892 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1894 wpabuf_put_str(resp, usrv->service);
1895 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
1901 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
1902 struct wpabuf *resp, u8 srv_trans_id,
1903 const u8 *query, size_t query_len)
1905 struct p2p_srv_upnp *usrv;
1911 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
1914 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
1915 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
1916 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
1921 if (query_len == 0) {
1922 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
1926 if (wpabuf_tailroom(resp) < 5)
1929 /* Length (to be filled) */
1930 len_pos = wpabuf_put(resp, 2);
1931 wpabuf_put_u8(resp, P2P_SERV_UPNP);
1932 wpabuf_put_u8(resp, srv_trans_id);
1935 str = os_malloc(query_len);
1938 os_memcpy(str, query + 1, query_len - 1);
1939 str[query_len - 1] = '\0';
1941 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
1942 struct p2p_srv_upnp, list) {
1943 if (version != usrv->version)
1946 if (os_strcmp(str, "ssdp:all") != 0 &&
1947 os_strstr(usrv->service, str) == NULL)
1950 if (wpabuf_tailroom(resp) < 2)
1954 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
1956 wpabuf_put_u8(resp, version);
1958 wpabuf_put_u8(resp, ',');
1962 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
1964 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
1966 wpabuf_put_str(resp, usrv->service);
1971 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
1974 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
1975 /* Response Data: empty */
1978 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
1982 #ifdef CONFIG_WIFI_DISPLAY
1983 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
1984 struct wpabuf *resp, u8 srv_trans_id,
1985 const u8 *query, size_t query_len)
1991 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
1993 if (!wpa_s->global->wifi_display) {
1994 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
1995 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2000 if (query_len < 1) {
2001 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2006 if (wpabuf_tailroom(resp) < 5)
2011 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2013 /* TODO: role specific handling */
2015 /* Length (to be filled) */
2016 len_pos = wpabuf_put(resp, 2);
2017 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2018 wpabuf_put_u8(resp, srv_trans_id);
2019 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2021 while (pos < query + query_len) {
2022 if (*pos < MAX_WFD_SUBELEMS &&
2023 wpa_s->global->wfd_subelem[*pos] &&
2024 wpabuf_tailroom(resp) >=
2025 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2026 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2027 "subelement %u", *pos);
2028 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2033 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2035 #endif /* CONFIG_WIFI_DISPLAY */
2038 void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2039 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2041 struct wpa_supplicant *wpa_s = ctx;
2042 const u8 *pos = tlvs;
2043 const u8 *end = tlvs + tlvs_len;
2046 struct wpabuf *resp;
2047 u8 srv_proto, srv_trans_id;
2051 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2053 buf_len = 2 * tlvs_len + 1;
2054 buf = os_malloc(buf_len);
2056 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2057 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2059 freq, MAC2STR(sa), dialog_token, update_indic,
2064 if (wpa_s->p2p_sd_over_ctrl_iface) {
2065 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2066 update_indic, tlvs, tlvs_len);
2067 return; /* to be processed by an external program */
2070 resp = wpabuf_alloc(10000);
2074 while (pos + 1 < end) {
2075 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2076 slen = WPA_GET_LE16(pos);
2078 if (pos + slen > end || slen < 2) {
2079 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2084 tlv_end = pos + slen;
2087 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2089 srv_trans_id = *pos++;
2090 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2093 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2094 pos, tlv_end - pos);
2097 if (wpa_s->force_long_sd) {
2098 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2100 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2101 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2105 switch (srv_proto) {
2106 case P2P_SERV_ALL_SERVICES:
2107 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2108 "for all services");
2109 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2110 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2111 wpa_printf(MSG_DEBUG, "P2P: No service "
2112 "discovery protocols available");
2113 wpas_sd_add_proto_not_avail(
2114 resp, P2P_SERV_ALL_SERVICES,
2118 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2119 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2121 case P2P_SERV_BONJOUR:
2122 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2123 pos, tlv_end - pos);
2126 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2127 pos, tlv_end - pos);
2129 #ifdef CONFIG_WIFI_DISPLAY
2130 case P2P_SERV_WIFI_DISPLAY:
2131 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2132 pos, tlv_end - pos);
2134 #endif /* CONFIG_WIFI_DISPLAY */
2136 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2137 "protocol %u", srv_proto);
2138 wpas_sd_add_proto_not_avail(resp, srv_proto,
2147 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2148 update_indic, tlvs, tlvs_len);
2150 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2156 void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2157 const u8 *tlvs, size_t tlvs_len)
2159 struct wpa_supplicant *wpa_s = ctx;
2160 const u8 *pos = tlvs;
2161 const u8 *end = tlvs + tlvs_len;
2167 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2169 if (tlvs_len > 1500) {
2170 /* TODO: better way for handling this */
2171 wpa_msg_ctrl(wpa_s, MSG_INFO,
2172 P2P_EVENT_SERV_DISC_RESP MACSTR
2173 " %u <long response: %u bytes>",
2174 MAC2STR(sa), update_indic,
2175 (unsigned int) tlvs_len);
2177 buf_len = 2 * tlvs_len + 1;
2178 buf = os_malloc(buf_len);
2180 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2181 wpa_msg_ctrl(wpa_s, MSG_INFO,
2182 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2183 MAC2STR(sa), update_indic, buf);
2189 u8 srv_proto, srv_trans_id, status;
2191 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2192 slen = WPA_GET_LE16(pos);
2194 if (pos + slen > end || slen < 3) {
2195 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2199 tlv_end = pos + slen;
2202 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2204 srv_trans_id = *pos++;
2205 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2208 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2211 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2212 pos, tlv_end - pos);
2217 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2221 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2222 const struct wpabuf *tlvs)
2224 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2225 return wpa_drv_p2p_sd_request(wpa_s, dst, tlvs);
2226 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2228 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2232 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2233 u8 version, const char *query)
2235 struct wpabuf *tlvs;
2238 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2241 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2242 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2243 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2244 wpabuf_put_u8(tlvs, version);
2245 wpabuf_put_str(tlvs, query);
2246 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2252 #ifdef CONFIG_WIFI_DISPLAY
2254 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2255 const struct wpabuf *tlvs)
2257 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2259 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2261 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2265 #define MAX_WFD_SD_SUBELEMS 20
2267 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2268 const char *subelems)
2275 len = wpabuf_put(tlvs, 2);
2276 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2277 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2279 wpabuf_put_u8(tlvs, role);
2284 if (val >= 0 && val < 256) {
2285 wpabuf_put_u8(tlvs, val);
2287 if (count == MAX_WFD_SD_SUBELEMS)
2290 pos = os_strchr(pos + 1, ',');
2296 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2300 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2301 const u8 *dst, const char *role)
2303 struct wpabuf *tlvs;
2305 const char *subelems;
2308 subelems = os_strchr(role, ' ');
2309 if (subelems == NULL)
2313 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2317 if (os_strstr(role, "[source]"))
2318 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2319 if (os_strstr(role, "[pri-sink]"))
2320 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2321 if (os_strstr(role, "[sec-sink]"))
2322 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2323 if (os_strstr(role, "[source+sink]"))
2324 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2326 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2331 #endif /* CONFIG_WIFI_DISPLAY */
2334 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2336 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
2337 return wpa_drv_p2p_sd_cancel_request(wpa_s, req);
2338 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2340 return p2p_sd_cancel_request(wpa_s->global->p2p,
2341 (void *) (uintptr_t) req);
2345 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2346 const u8 *dst, u8 dialog_token,
2347 const struct wpabuf *resp_tlvs)
2349 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2350 wpa_drv_p2p_sd_response(wpa_s, freq, dst, dialog_token,
2354 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2356 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2361 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2363 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
2364 wpa_drv_p2p_service_update(wpa_s);
2367 if (wpa_s->global->p2p)
2368 p2p_sd_service_update(wpa_s->global->p2p);
2372 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2374 dl_list_del(&bsrv->list);
2375 wpabuf_free(bsrv->query);
2376 wpabuf_free(bsrv->resp);
2381 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2383 dl_list_del(&usrv->list);
2384 os_free(usrv->service);
2389 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2391 struct p2p_srv_bonjour *bsrv, *bn;
2392 struct p2p_srv_upnp *usrv, *un;
2394 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2395 struct p2p_srv_bonjour, list)
2396 wpas_p2p_srv_bonjour_free(bsrv);
2398 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2399 struct p2p_srv_upnp, list)
2400 wpas_p2p_srv_upnp_free(usrv);
2402 wpas_p2p_sd_service_update(wpa_s);
2406 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2407 struct wpabuf *query, struct wpabuf *resp)
2409 struct p2p_srv_bonjour *bsrv;
2411 bsrv = os_zalloc(sizeof(*bsrv));
2414 bsrv->query = query;
2416 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2418 wpas_p2p_sd_service_update(wpa_s);
2423 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2424 const struct wpabuf *query)
2426 struct p2p_srv_bonjour *bsrv;
2428 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2431 wpas_p2p_srv_bonjour_free(bsrv);
2432 wpas_p2p_sd_service_update(wpa_s);
2437 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2438 const char *service)
2440 struct p2p_srv_upnp *usrv;
2442 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2443 return 0; /* Already listed */
2444 usrv = os_zalloc(sizeof(*usrv));
2447 usrv->version = version;
2448 usrv->service = os_strdup(service);
2449 if (usrv->service == NULL) {
2453 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2455 wpas_p2p_sd_service_update(wpa_s);
2460 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2461 const char *service)
2463 struct p2p_srv_upnp *usrv;
2465 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2468 wpas_p2p_srv_upnp_free(usrv);
2469 wpas_p2p_sd_service_update(wpa_s);
2474 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2475 const u8 *peer, const char *params,
2476 unsigned int generated_pin)
2478 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2479 " %08d%s", MAC2STR(peer), generated_pin, params);
2483 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2484 const u8 *peer, const char *params)
2486 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2487 "%s", MAC2STR(peer), params);
2491 void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2492 const u8 *dev_addr, const u8 *pri_dev_type,
2493 const char *dev_name, u16 supp_config_methods,
2494 u8 dev_capab, u8 group_capab, const u8 *group_id,
2495 size_t group_id_len)
2497 struct wpa_supplicant *wpa_s = ctx;
2498 char devtype[WPS_DEV_TYPE_BUFSIZE];
2500 u8 empty_dev_type[8];
2501 unsigned int generated_pin = 0;
2502 struct wpa_supplicant *group = NULL;
2505 for (group = wpa_s->global->ifaces; group; group = group->next)
2507 struct wpa_ssid *s = group->current_ssid;
2509 s->mode == WPAS_MODE_P2P_GO &&
2510 group_id_len - ETH_ALEN == s->ssid_len &&
2511 os_memcmp(group_id + ETH_ALEN, s->ssid,
2517 if (pri_dev_type == NULL) {
2518 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2519 pri_dev_type = empty_dev_type;
2521 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2522 " pri_dev_type=%s name='%s' config_methods=0x%x "
2523 "dev_capab=0x%x group_capab=0x%x%s%s",
2525 wps_dev_type_bin2str(pri_dev_type, devtype,
2527 dev_name, supp_config_methods, dev_capab, group_capab,
2528 group ? " group=" : "",
2529 group ? group->ifname : "");
2530 params[sizeof(params) - 1] = '\0';
2532 if (config_methods & WPS_CONFIG_DISPLAY) {
2533 generated_pin = wps_generate_pin();
2534 wpas_prov_disc_local_display(wpa_s, peer, params,
2536 } else if (config_methods & WPS_CONFIG_KEYPAD)
2537 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2538 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2539 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2540 MACSTR "%s", MAC2STR(peer), params);
2542 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2543 P2P_PROV_DISC_SUCCESS,
2544 config_methods, generated_pin);
2548 void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2550 struct wpa_supplicant *wpa_s = ctx;
2551 unsigned int generated_pin = 0;
2554 if (wpa_s->pending_pd_before_join &&
2555 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2556 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2557 wpa_s->pending_pd_before_join = 0;
2558 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2559 "join-existing-group operation");
2560 wpas_p2p_join_start(wpa_s);
2564 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2565 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2566 os_snprintf(params, sizeof(params), " peer_go=%d",
2567 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2571 if (config_methods & WPS_CONFIG_DISPLAY)
2572 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2573 else if (config_methods & WPS_CONFIG_KEYPAD) {
2574 generated_pin = wps_generate_pin();
2575 wpas_prov_disc_local_display(wpa_s, peer, params,
2577 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2578 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2579 MACSTR "%s", MAC2STR(peer), params);
2581 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2582 P2P_PROV_DISC_SUCCESS,
2583 config_methods, generated_pin);
2587 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2588 enum p2p_prov_disc_status status)
2590 struct wpa_supplicant *wpa_s = ctx;
2592 if (wpa_s->p2p_fallback_to_go_neg) {
2593 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2594 "failed - fall back to GO Negotiation");
2595 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2599 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2600 wpa_s->pending_pd_before_join = 0;
2601 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2602 "join-existing-group operation (no ACK for PD "
2604 wpas_p2p_join_start(wpa_s);
2608 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2609 " p2p_dev_addr=" MACSTR " status=%d",
2610 MAC2STR(peer), status);
2612 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2617 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2619 if (channels == NULL)
2620 return 1; /* Assume no restrictions */
2621 return p2p_channels_includes_freq(channels, freq);
2626 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2627 const u8 *go_dev_addr, const u8 *ssid,
2628 size_t ssid_len, int *go, u8 *group_bssid,
2629 int *force_freq, int persistent_group,
2630 const struct p2p_channels *channels)
2632 struct wpa_supplicant *wpa_s = ctx;
2635 struct wpa_supplicant *grp;
2637 if (!persistent_group) {
2638 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2639 " to join an active group", MAC2STR(sa));
2640 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2641 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2643 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2644 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2645 "authorized invitation");
2649 * Do not accept the invitation automatically; notify user and
2652 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2655 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
2657 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
2658 "running persistent group");
2660 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
2664 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2665 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
2666 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
2667 "invitation to re-invoke a persistent group");
2668 } else if (!wpa_s->conf->persistent_reconnect)
2669 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
2671 for (s = wpa_s->conf->ssid; s; s = s->next) {
2672 if (s->disabled == 2 &&
2673 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
2674 s->ssid_len == ssid_len &&
2675 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2680 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2681 " requested reinvocation of an unknown group",
2683 return P2P_SC_FAIL_UNKNOWN_GROUP;
2686 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
2688 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
2689 wpa_printf(MSG_DEBUG, "P2P: The only available "
2690 "interface is already in use - reject "
2692 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2694 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
2695 } else if (s->mode == WPAS_MODE_P2P_GO) {
2697 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
2699 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
2700 "interface address for the group");
2701 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
2703 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
2708 wpas_p2p_set_own_freq_preference(wpa_s, 0);
2710 /* Get one of the frequencies currently in use */
2711 if (wpas_p2p_valid_oper_freqs(wpa_s, &res, 1) > 0) {
2712 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
2713 wpas_p2p_set_own_freq_preference(wpa_s, res);
2715 if (wpa_s->num_multichan_concurrent < 2 ||
2716 wpas_p2p_num_unused_channels(wpa_s) < 1) {
2717 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
2722 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
2723 wpas_p2p_num_unused_channels(wpa_s) > 0) {
2725 /* We are the client */
2726 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
2727 "running a GO but we are capable of MCC, "
2728 "figure out the best channel to use");
2730 } else if (!freq_included(channels, *force_freq)) {
2731 /* We are the GO, and *force_freq is not in the
2733 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
2734 "in intersection but we are capable of MCC, "
2735 "figure out the best channel to use",
2741 return P2P_SC_SUCCESS;
2745 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
2746 const u8 *ssid, size_t ssid_len,
2747 const u8 *go_dev_addr, u8 status,
2750 struct wpa_supplicant *wpa_s = ctx;
2753 for (s = wpa_s->conf->ssid; s; s = s->next) {
2754 if (s->disabled == 2 &&
2755 s->ssid_len == ssid_len &&
2756 os_memcmp(ssid, s->ssid, ssid_len) == 0)
2760 if (status == P2P_SC_SUCCESS) {
2761 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2762 " was accepted; op_freq=%d MHz",
2763 MAC2STR(sa), op_freq);
2765 int go = s->mode == WPAS_MODE_P2P_GO;
2766 wpas_p2p_group_add_persistent(
2767 wpa_s, s, go, go ? op_freq : 0, 0, 0, NULL,
2768 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
2770 wpa_s->user_initiated_pd = 0;
2771 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
2772 wpa_s->p2p_wps_method, 0);
2777 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2778 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
2779 " was rejected (status %u)", MAC2STR(sa), status);
2785 wpa_msg_global(wpa_s, MSG_INFO,
2786 P2P_EVENT_INVITATION_RECEIVED
2787 "sa=" MACSTR " go_dev_addr=" MACSTR
2788 " bssid=" MACSTR " unknown-network",
2789 MAC2STR(sa), MAC2STR(go_dev_addr),
2792 wpa_msg_global(wpa_s, MSG_INFO,
2793 P2P_EVENT_INVITATION_RECEIVED
2794 "sa=" MACSTR " go_dev_addr=" MACSTR
2796 MAC2STR(sa), MAC2STR(go_dev_addr));
2801 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
2802 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2803 "sa=" MACSTR " persistent=%d freq=%d",
2804 MAC2STR(sa), s->id, op_freq);
2806 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
2807 "sa=" MACSTR " persistent=%d",
2808 MAC2STR(sa), s->id);
2813 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
2814 struct wpa_ssid *ssid,
2815 const u8 *peer, int inv)
2822 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
2823 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
2827 if (i >= ssid->num_p2p_clients) {
2828 if (ssid->mode != WPAS_MODE_P2P_GO &&
2829 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
2830 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
2831 "due to invitation result", ssid->id);
2832 wpas_notify_network_removed(wpa_s, ssid);
2833 wpa_config_remove_network(wpa_s->conf, ssid->id);
2836 return; /* Peer not found in client list */
2839 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
2840 "group %d client list%s",
2841 MAC2STR(peer), ssid->id,
2842 inv ? " due to invitation result" : "");
2843 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
2844 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
2845 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
2846 ssid->num_p2p_clients--;
2847 #ifndef CONFIG_NO_CONFIG_WRITE
2848 if (wpa_s->parent->conf->update_config &&
2849 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
2850 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
2851 #endif /* CONFIG_NO_CONFIG_WRITE */
2855 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
2858 struct wpa_ssid *ssid;
2860 wpa_s = wpa_s->global->p2p_invite_group;
2862 return; /* No known invitation group */
2863 ssid = wpa_s->current_ssid;
2864 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
2865 !ssid->p2p_persistent_group)
2866 return; /* Not operating as a GO in persistent group */
2867 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
2868 ssid->ssid, ssid->ssid_len);
2869 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2873 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
2874 const struct p2p_channels *channels,
2875 const u8 *peer, int neg_freq)
2877 struct wpa_supplicant *wpa_s = ctx;
2878 struct wpa_ssid *ssid;
2882 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2883 "status=%d " MACSTR,
2884 status, MAC2STR(bssid));
2886 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
2887 "status=%d ", status);
2889 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
2891 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
2892 status, MAC2STR(peer));
2893 if (wpa_s->pending_invite_ssid_id == -1) {
2894 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
2895 wpas_remove_persistent_client(wpa_s, peer);
2896 return; /* Invitation to active group */
2899 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
2900 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
2901 "invitation exchange to indicate readiness for "
2905 if (status != P2P_SC_SUCCESS) {
2906 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
2907 ssid = wpa_config_get_network(
2908 wpa_s->conf, wpa_s->pending_invite_ssid_id);
2909 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
2911 wpas_p2p_remove_pending_group_interface(wpa_s);
2915 ssid = wpa_config_get_network(wpa_s->conf,
2916 wpa_s->pending_invite_ssid_id);
2918 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
2919 "data matching with invitation");
2924 * The peer could have missed our ctrl::ack frame for Invitation
2925 * Response and continue retransmitting the frame. To reduce the
2926 * likelihood of the peer not getting successful TX status for the
2927 * Invitation Response frame, wait a short time here before starting
2928 * the persistent group so that we will remain on the current channel to
2929 * acknowledge any possible retransmission from the peer.
2931 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
2932 "starting persistent group");
2935 freq = wpa_s->p2p_persistent_go_freq;
2936 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
2937 freq_included(channels, neg_freq)) {
2938 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use frequence %d MHz from invitation for GO mode",
2943 wpas_p2p_group_add_persistent(wpa_s, ssid,
2944 ssid->mode == WPAS_MODE_P2P_GO,
2946 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
2948 ssid->mode == WPAS_MODE_P2P_GO ?
2949 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
2954 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
2957 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
2961 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
2963 reg->channel[reg->channels] = chan;
2968 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
2969 struct p2p_channels *chan,
2970 struct p2p_channels *cli_chan)
2974 os_memset(cli_chan, 0, sizeof(*cli_chan));
2976 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
2979 /* Operating class 81 - 2.4 GHz band channels 1..13 */
2980 chan->reg_class[cla].reg_class = 81;
2981 chan->reg_class[cla].channels = 0;
2982 for (i = 0; i < 11; i++) {
2983 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
2984 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
2986 if (chan->reg_class[cla].channels)
2989 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
2992 /* Operating class 115 - 5 GHz, channels 36-48 */
2993 chan->reg_class[cla].reg_class = 115;
2994 chan->reg_class[cla].channels = 0;
2995 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
2996 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
2997 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
2998 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
2999 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3000 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3001 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3002 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3003 if (chan->reg_class[cla].channels)
3006 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3009 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3010 chan->reg_class[cla].reg_class = 124;
3011 chan->reg_class[cla].channels = 0;
3012 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3013 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3014 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3015 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3016 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3017 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3018 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3019 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3020 if (chan->reg_class[cla].channels)
3023 chan->reg_classes = cla;
3028 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3030 enum hostapd_hw_mode mode)
3034 for (i = 0; i < num_modes; i++) {
3035 if (modes[i].mode == mode)
3044 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3047 static int has_channel(struct wpa_global *global,
3048 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3053 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3055 if (wpas_p2p_disallowed_freq(global, freq))
3058 for (i = 0; i < mode->num_channels; i++) {
3059 if (mode->channels[i].chan == chan) {
3061 *flags = mode->channels[i].flag;
3062 if (mode->channels[i].flag &
3063 (HOSTAPD_CHAN_DISABLED |
3064 HOSTAPD_CHAN_RADAR))
3066 if (mode->channels[i].flag &
3067 (HOSTAPD_CHAN_PASSIVE_SCAN |
3068 HOSTAPD_CHAN_NO_IBSS))
3069 return PASSIVE_ONLY;
3078 struct p2p_oper_class_map {
3079 enum hostapd_hw_mode mode;
3084 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
3087 static struct p2p_oper_class_map op_class[] = {
3088 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3089 #if 0 /* Do not enable HT40 on 2 GHz for now */
3090 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3091 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3093 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3094 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3095 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3096 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3097 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3098 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3101 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3102 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3103 * 80 MHz, but currently use the following definition for simplicity
3104 * (these center frequencies are not actual channels, which makes
3105 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3106 * removing invalid channels.
3108 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3109 { -1, 0, 0, 0, 0, BW20 }
3113 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3114 struct hostapd_hw_modes *mode,
3117 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3120 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3123 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3125 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3126 * so the center channel is 6 channels away from the start/end.
3128 if (channel >= center_channels[i] - 6 &&
3129 channel <= center_channels[i] + 6)
3130 return center_channels[i];
3136 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3137 struct hostapd_hw_modes *mode,
3142 enum chan_allowed res, ret = ALLOWED;
3144 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3147 if (center_chan >= 58 && center_chan <= 138)
3148 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3150 /* check all the channels are available */
3151 for (i = 0; i < 4; i++) {
3152 int adj_chan = center_chan - 6 + i * 4;
3154 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3155 if (res == NOT_ALLOWED)
3157 if (res == PASSIVE_ONLY)
3160 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3162 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3164 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3166 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3174 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3175 struct hostapd_hw_modes *mode,
3179 enum chan_allowed res, res2;
3181 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3182 if (bw == BW40MINUS) {
3183 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3185 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3186 } else if (bw == BW40PLUS) {
3187 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3189 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3190 } else if (bw == BW80) {
3191 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3194 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3196 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3197 return PASSIVE_ONLY;
3202 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3203 struct p2p_channels *chan,
3204 struct p2p_channels *cli_chan)
3206 struct hostapd_hw_modes *mode;
3207 int cla, op, cli_cla;
3209 if (wpa_s->hw.modes == NULL) {
3210 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3211 "of all supported channels; assume dualband "
3213 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3218 for (op = 0; op_class[op].op_class; op++) {
3219 struct p2p_oper_class_map *o = &op_class[op];
3221 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3223 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3226 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3227 enum chan_allowed res;
3228 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3229 if (res == ALLOWED) {
3231 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3233 reg = &chan->reg_class[cla];
3235 reg->reg_class = o->op_class;
3237 reg->channel[reg->channels] = ch;
3239 } else if (res == PASSIVE_ONLY &&
3240 wpa_s->conf->p2p_add_cli_chan) {
3241 if (cli_reg == NULL) {
3242 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3244 cli_reg = &cli_chan->reg_class[cli_cla];
3246 cli_reg->reg_class = o->op_class;
3248 cli_reg->channel[cli_reg->channels] = ch;
3249 cli_reg->channels++;
3253 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3254 reg->channel, reg->channels);
3257 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3258 cli_reg->channel, cli_reg->channels);
3262 chan->reg_classes = cla;
3263 cli_chan->reg_classes = cli_cla;
3269 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3270 struct hostapd_hw_modes *mode, u8 channel)
3273 enum chan_allowed ret;
3275 for (op = 0; op_class[op].op_class; op++) {
3276 struct p2p_oper_class_map *o = &op_class[op];
3279 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3280 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3281 o->bw == BW20 || ch != channel)
3283 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3285 return (o->bw == BW40MINUS) ? -1 : 1;
3292 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3293 struct hostapd_hw_modes *mode, u8 channel)
3295 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3298 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3302 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3305 struct wpa_supplicant *wpa_s = ctx;
3307 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3308 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3314 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3318 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3320 struct wpa_supplicant *wpa_s = ctx;
3322 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3323 struct wpa_ssid *ssid = wpa_s->current_ssid;
3326 if (ssid->mode != WPAS_MODE_INFRA)
3328 if (wpa_s->wpa_state != WPA_COMPLETED &&
3329 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3331 if (os_memcmp(wpa_s->go_dev_addr, dev_addr, ETH_ALEN) == 0)
3339 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3341 struct wpa_supplicant *wpa_s = ctx;
3342 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3346 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3348 struct wpa_interface iface;
3349 struct wpa_supplicant *p2pdev_wpa_s;
3351 char force_name[100];
3354 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3356 force_name[0] = '\0';
3357 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3358 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3359 force_name, wpa_s->pending_interface_addr, NULL);
3361 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3364 os_strlcpy(wpa_s->pending_interface_name, ifname,
3365 sizeof(wpa_s->pending_interface_name));
3367 os_memset(&iface, 0, sizeof(iface));
3369 iface.ifname = wpa_s->pending_interface_name;
3370 iface.driver = wpa_s->driver->name;
3371 iface.driver_param = wpa_s->conf->driver_param;
3372 iface.confname = wpa_s->confname;
3373 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3374 if (!p2pdev_wpa_s) {
3375 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3378 p2pdev_wpa_s->parent = wpa_s;
3380 wpa_s->pending_interface_name[0] = '\0';
3386 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3387 * @global: Pointer to global data from wpa_supplicant_init()
3388 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3389 * Returns: 0 on success, -1 on failure
3391 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3393 struct p2p_config p2p;
3397 if (wpa_s->conf->p2p_disabled)
3400 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3406 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3407 struct p2p_params params;
3409 wpa_printf(MSG_DEBUG, "P2P: Use driver-based P2P management");
3410 os_memset(¶ms, 0, sizeof(params));
3411 params.dev_name = wpa_s->conf->device_name;
3412 os_memcpy(params.pri_dev_type, wpa_s->conf->device_type,
3414 params.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3415 os_memcpy(params.sec_dev_type,
3416 wpa_s->conf->sec_device_type,
3417 params.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3419 if (wpa_drv_p2p_set_params(wpa_s, ¶ms) < 0)
3425 os_memset(&p2p, 0, sizeof(p2p));
3427 p2p.debug_print = wpas_p2p_debug_print;
3428 p2p.p2p_scan = wpas_p2p_scan;
3429 p2p.send_action = wpas_send_action;
3430 p2p.send_action_done = wpas_send_action_done;
3431 p2p.go_neg_completed = wpas_go_neg_completed;
3432 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3433 p2p.dev_found = wpas_dev_found;
3434 p2p.dev_lost = wpas_dev_lost;
3435 p2p.find_stopped = wpas_find_stopped;
3436 p2p.start_listen = wpas_start_listen;
3437 p2p.stop_listen = wpas_stop_listen;
3438 p2p.send_probe_resp = wpas_send_probe_resp;
3439 p2p.sd_request = wpas_sd_request;
3440 p2p.sd_response = wpas_sd_response;
3441 p2p.prov_disc_req = wpas_prov_disc_req;
3442 p2p.prov_disc_resp = wpas_prov_disc_resp;
3443 p2p.prov_disc_fail = wpas_prov_disc_fail;
3444 p2p.invitation_process = wpas_invitation_process;
3445 p2p.invitation_received = wpas_invitation_received;
3446 p2p.invitation_result = wpas_invitation_result;
3447 p2p.get_noa = wpas_get_noa;
3448 p2p.go_connected = wpas_go_connected;
3450 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
3451 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
3452 p2p.dev_name = wpa_s->conf->device_name;
3453 p2p.manufacturer = wpa_s->conf->manufacturer;
3454 p2p.model_name = wpa_s->conf->model_name;
3455 p2p.model_number = wpa_s->conf->model_number;
3456 p2p.serial_number = wpa_s->conf->serial_number;
3458 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3459 p2p.config_methods = wpa_s->wps->config_methods;
3462 if (wpa_s->conf->p2p_listen_reg_class &&
3463 wpa_s->conf->p2p_listen_channel) {
3464 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3465 p2p.channel = wpa_s->conf->p2p_listen_channel;
3469 * Pick one of the social channels randomly as the listen
3472 os_get_random((u8 *) &r, sizeof(r));
3473 p2p.channel = 1 + (r % 3) * 5;
3475 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3477 if (wpa_s->conf->p2p_oper_reg_class &&
3478 wpa_s->conf->p2p_oper_channel) {
3479 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3480 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3481 p2p.cfg_op_channel = 1;
3482 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3483 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3486 p2p.op_reg_class = 81;
3488 * Use random operation channel from (1, 6, 11) if no other
3489 * preference is indicated.
3491 os_get_random((u8 *) &r, sizeof(r));
3492 p2p.op_channel = 1 + (r % 3) * 5;
3493 p2p.cfg_op_channel = 0;
3494 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3495 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3498 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3499 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3500 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3503 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3504 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3505 p2p.country[2] = 0x04;
3507 os_memcpy(p2p.country, "XX\x04", 3);
3509 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3510 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3515 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3518 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3519 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3520 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3522 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3523 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3525 p2p.max_peers = 100;
3527 if (wpa_s->conf->p2p_ssid_postfix) {
3528 p2p.ssid_postfix_len =
3529 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3530 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3531 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3532 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3533 p2p.ssid_postfix_len);
3536 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3538 p2p.max_listen = wpa_s->max_remain_on_chan;
3540 global->p2p = p2p_init(&p2p);
3541 if (global->p2p == NULL)
3543 global->p2p_init_wpa_s = wpa_s;
3545 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3546 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3548 p2p_add_wps_vendor_extension(
3549 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
3552 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
3559 * wpas_p2p_deinit - Deinitialize per-interface P2P data
3560 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3562 * This function deinitialize per-interface P2P data.
3564 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
3566 if (wpa_s->driver && wpa_s->drv_priv)
3567 wpa_drv_probe_req_report(wpa_s, 0);
3569 if (wpa_s->go_params) {
3570 /* Clear any stored provisioning info */
3571 p2p_clear_provisioning_info(
3573 wpa_s->go_params->peer_device_addr);
3576 os_free(wpa_s->go_params);
3577 wpa_s->go_params = NULL;
3578 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
3579 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3580 wpa_s->p2p_long_listen = 0;
3581 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
3582 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
3583 wpas_p2p_remove_pending_group_interface(wpa_s);
3584 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
3586 /* TODO: remove group interface from the driver if this wpa_s instance
3587 * is on top of a P2P group interface */
3592 * wpas_p2p_deinit_global - Deinitialize global P2P module
3593 * @global: Pointer to global data from wpa_supplicant_init()
3595 * This function deinitializes the global (per device) P2P module.
3597 void wpas_p2p_deinit_global(struct wpa_global *global)
3599 struct wpa_supplicant *wpa_s, *tmp;
3601 wpa_s = global->ifaces;
3603 wpas_p2p_service_flush(wpa_s);
3605 if (global->p2p == NULL)
3608 /* Remove remaining P2P group interfaces */
3609 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
3610 wpa_s = wpa_s->next;
3612 tmp = global->ifaces;
3615 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
3620 /* Disconnect from the P2P group and deinit the interface */
3621 wpas_p2p_disconnect(tmp);
3625 * Deinit GO data on any possibly remaining interface (if main
3626 * interface is used as GO).
3628 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3629 if (wpa_s->ap_iface)
3630 wpas_p2p_group_deinit(wpa_s);
3633 p2p_deinit(global->p2p);
3635 global->p2p_init_wpa_s = NULL;
3639 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
3641 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
3642 wpa_s->conf->p2p_no_group_iface)
3643 return 0; /* separate interface disabled per configuration */
3644 if (wpa_s->drv_flags &
3645 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
3646 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
3647 return 1; /* P2P group requires a new interface in every case
3649 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
3650 return 0; /* driver does not support concurrent operations */
3651 if (wpa_s->global->ifaces->next)
3652 return 1; /* more that one interface already in use */
3653 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
3654 return 1; /* this interface is already in use */
3659 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
3660 const u8 *peer_addr,
3661 enum p2p_wps_method wps_method,
3662 int go_intent, const u8 *own_interface_addr,
3663 unsigned int force_freq, int persistent_group,
3664 struct wpa_ssid *ssid, unsigned int pref_freq)
3666 if (persistent_group && wpa_s->conf->persistent_reconnect)
3667 persistent_group = 2;
3669 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
3670 return wpa_drv_p2p_connect(wpa_s, peer_addr, wps_method,
3671 go_intent, own_interface_addr,
3672 force_freq, persistent_group);
3676 * Increase GO config timeout if HT40 is used since it takes some time
3677 * to scan channels for coex purposes before the BSS can be started.
3679 p2p_set_config_timeout(wpa_s->global->p2p,
3680 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
3682 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
3683 go_intent, own_interface_addr, force_freq,
3684 persistent_group, ssid ? ssid->ssid : NULL,
3685 ssid ? ssid->ssid_len : 0,
3686 wpa_s->p2p_pd_before_go_neg, pref_freq);
3690 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
3691 const u8 *peer_addr,
3692 enum p2p_wps_method wps_method,
3693 int go_intent, const u8 *own_interface_addr,
3694 unsigned int force_freq, int persistent_group,
3695 struct wpa_ssid *ssid, unsigned int pref_freq)
3697 if (persistent_group && wpa_s->conf->persistent_reconnect)
3698 persistent_group = 2;
3700 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
3703 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
3704 go_intent, own_interface_addr, force_freq,
3705 persistent_group, ssid ? ssid->ssid : NULL,
3706 ssid ? ssid->ssid_len : 0, pref_freq);
3710 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
3712 wpa_s->p2p_join_scan_count++;
3713 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
3714 wpa_s->p2p_join_scan_count);
3715 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
3716 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
3717 " for join operationg - stop join attempt",
3718 MAC2STR(wpa_s->pending_join_iface_addr));
3719 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3720 if (wpa_s->p2p_auto_pd) {
3721 wpa_s->p2p_auto_pd = 0;
3722 wpa_msg_global(wpa_s, MSG_INFO,
3723 P2P_EVENT_PROV_DISC_FAILURE
3724 " p2p_dev_addr=" MACSTR " status=N/A",
3725 MAC2STR(wpa_s->pending_join_dev_addr));
3728 wpa_msg_global(wpa_s->parent, MSG_INFO,
3729 P2P_EVENT_GROUP_FORMATION_FAILURE);
3734 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
3736 int *freqs, res, num, i;
3738 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
3739 /* Multiple channels are supported and not all are in use */
3743 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
3747 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
3748 wpa_s->num_multichan_concurrent);
3754 for (i = 0; i < num; i++) {
3755 if (freqs[i] == freq) {
3756 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
3771 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
3772 const u8 *peer_dev_addr)
3774 struct wpa_bss *bss;
3777 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
3780 if (bss->last_update_idx < wpa_s->bss_update_idx) {
3781 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
3786 updated = os_reltime_before(&wpa_s->p2p_auto_started,
3788 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
3789 "%ld.%06ld (%supdated in last scan)",
3790 bss->last_update.sec, bss->last_update.usec,
3791 updated ? "": "not ");
3797 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
3798 struct wpa_scan_results *scan_res)
3800 struct wpa_bss *bss;
3802 u8 iface_addr[ETH_ALEN];
3804 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3806 if (wpa_s->global->p2p_disabled)
3809 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
3810 scan_res ? (int) scan_res->num : -1,
3811 wpa_s->p2p_auto_join ? "auto_" : "");
3814 wpas_p2p_scan_res_handler(wpa_s, scan_res);
3816 if (wpa_s->p2p_auto_pd) {
3817 int join = wpas_p2p_peer_go(wpa_s,
3818 wpa_s->pending_join_dev_addr);
3820 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
3821 wpa_s->auto_pd_scan_retry++;
3822 bss = wpa_bss_get_bssid_latest(
3823 wpa_s, wpa_s->pending_join_dev_addr);
3826 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
3827 "the peer " MACSTR " at %d MHz",
3828 wpa_s->auto_pd_scan_retry,
3830 pending_join_dev_addr),
3832 wpas_p2p_join_scan_req(wpa_s, freq);
3840 wpa_s->p2p_auto_pd = 0;
3841 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
3842 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
3843 MAC2STR(wpa_s->pending_join_dev_addr), join);
3844 if (p2p_prov_disc_req(wpa_s->global->p2p,
3845 wpa_s->pending_join_dev_addr,
3846 wpa_s->pending_pd_config_methods, join,
3847 0, wpa_s->user_initiated_pd) < 0) {
3848 wpa_s->p2p_auto_pd = 0;
3849 wpa_msg_global(wpa_s, MSG_INFO,
3850 P2P_EVENT_PROV_DISC_FAILURE
3851 " p2p_dev_addr=" MACSTR " status=N/A",
3852 MAC2STR(wpa_s->pending_join_dev_addr));
3857 if (wpa_s->p2p_auto_join) {
3858 int join = wpas_p2p_peer_go(wpa_s,
3859 wpa_s->pending_join_dev_addr);
3861 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
3862 "running a GO -> use GO Negotiation");
3863 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
3864 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
3865 wpa_s->p2p_persistent_group, 0, 0, 0,
3866 wpa_s->p2p_go_intent,
3867 wpa_s->p2p_connect_freq,
3868 wpa_s->p2p_persistent_id,
3869 wpa_s->p2p_pd_before_go_neg,
3875 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
3876 "try to join the group", join ? "" :
3879 wpa_s->p2p_fallback_to_go_neg = 1;
3882 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3883 wpa_s->pending_join_iface_addr);
3885 p2p_get_interface_addr(wpa_s->global->p2p,
3886 wpa_s->pending_join_dev_addr,
3888 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
3890 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
3891 "address for join from " MACSTR " to " MACSTR
3892 " based on newly discovered P2P peer entry",
3893 MAC2STR(wpa_s->pending_join_iface_addr),
3894 MAC2STR(iface_addr));
3895 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
3898 freq = p2p_get_oper_freq(wpa_s->global->p2p,
3899 wpa_s->pending_join_iface_addr);
3902 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3903 "from P2P peer table: %d MHz", freq);
3905 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
3908 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
3909 "from BSS table: %d MHz (SSID %s)", freq,
3910 wpa_ssid_txt(bss->ssid, bss->ssid_len));
3915 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
3916 wpa_msg_global(wpa_s->parent, MSG_INFO,
3917 P2P_EVENT_GROUP_FORMATION_FAILURE
3918 "reason=FREQ_CONFLICT");
3922 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
3923 "prior to joining an existing group (GO " MACSTR
3925 MAC2STR(wpa_s->pending_join_dev_addr), freq);
3926 wpa_s->pending_pd_before_join = 1;
3928 switch (wpa_s->pending_join_wps_method) {
3929 case WPS_PIN_DISPLAY:
3930 method = WPS_CONFIG_KEYPAD;
3932 case WPS_PIN_KEYPAD:
3933 method = WPS_CONFIG_DISPLAY;
3936 method = WPS_CONFIG_PUSHBUTTON;
3943 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
3944 wpa_s->pending_join_dev_addr) ==
3947 * We have already performed provision discovery for
3948 * joining the group. Proceed directly to join
3949 * operation without duplicated provision discovery. */
3950 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
3951 "with " MACSTR " already done - proceed to "
3953 MAC2STR(wpa_s->pending_join_dev_addr));
3954 wpa_s->pending_pd_before_join = 0;
3958 if (p2p_prov_disc_req(wpa_s->global->p2p,
3959 wpa_s->pending_join_dev_addr, method, 1,
3960 freq, wpa_s->user_initiated_pd) < 0) {
3961 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
3962 "Discovery Request before joining an "
3964 wpa_s->pending_pd_before_join = 0;
3970 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
3971 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
3972 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
3973 wpas_p2p_check_join_scan_limit(wpa_s);
3977 /* Start join operation immediately */
3978 wpas_p2p_join_start(wpa_s);
3982 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq)
3985 struct wpa_driver_scan_params params;
3986 struct wpabuf *wps_ie, *ies;
3988 int freqs[2] = { 0, 0 };
3990 os_memset(¶ms, 0, sizeof(params));
3992 /* P2P Wildcard SSID */
3993 params.num_ssids = 1;
3994 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
3995 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
3997 wpa_s->wps->dev.p2p = 1;
3998 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
3999 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4001 if (wps_ie == NULL) {
4002 wpas_p2p_scan_res_join(wpa_s, NULL);
4006 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4007 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4009 wpabuf_free(wps_ie);
4010 wpas_p2p_scan_res_join(wpa_s, NULL);
4013 wpabuf_put_buf(ies, wps_ie);
4014 wpabuf_free(wps_ie);
4016 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4018 params.p2p_probe = 1;
4019 params.extra_ies = wpabuf_head(ies);
4020 params.extra_ies_len = wpabuf_len(ies);
4023 params.freqs = freqs;
4027 * Run a scan to update BSS table and start Provision Discovery once
4028 * the new scan results become available.
4030 ret = wpa_drv_scan(wpa_s, ¶ms);
4032 os_get_reltime(&wpa_s->scan_trigger_time);
4033 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4039 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4041 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4042 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4043 wpas_p2p_check_join_scan_limit(wpa_s);
4048 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4050 struct wpa_supplicant *wpa_s = eloop_ctx;
4051 wpas_p2p_join_scan_req(wpa_s, 0);
4055 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4056 const u8 *dev_addr, enum p2p_wps_method wps_method,
4059 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4060 MACSTR " dev " MACSTR ")%s",
4061 MAC2STR(iface_addr), MAC2STR(dev_addr),
4062 auto_join ? " (auto_join)" : "");
4064 wpa_s->p2p_auto_pd = 0;
4065 wpa_s->p2p_auto_join = !!auto_join;
4066 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4067 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4068 wpa_s->pending_join_wps_method = wps_method;
4070 /* Make sure we are not running find during connection establishment */
4071 wpas_p2p_stop_find(wpa_s);
4073 wpa_s->p2p_join_scan_count = 0;
4074 wpas_p2p_join_scan(wpa_s, NULL);
4079 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s)
4081 struct wpa_supplicant *group;
4082 struct p2p_go_neg_results res;
4083 struct wpa_bss *bss;
4085 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4088 if (group != wpa_s) {
4089 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4090 sizeof(group->p2p_pin));
4091 group->p2p_wps_method = wpa_s->p2p_wps_method;
4094 * Need to mark the current interface for p2p_group_formation
4095 * when a separate group interface is not used. This is needed
4096 * to allow p2p_cancel stop a pending p2p_connect-join.
4097 * wpas_p2p_init_group_interface() addresses this for the case
4098 * where a separate group interface is used.
4100 wpa_s->global->p2p_group_formation = wpa_s;
4103 group->p2p_in_provisioning = 1;
4104 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4106 os_memset(&res, 0, sizeof(res));
4107 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4109 res.wps_method = wpa_s->pending_join_wps_method;
4110 bss = wpa_bss_get_bssid_latest(wpa_s, wpa_s->pending_join_iface_addr);
4112 res.freq = bss->freq;
4113 res.ssid_len = bss->ssid_len;
4114 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4115 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency "
4116 "from BSS table: %d MHz (SSID %s)", bss->freq,
4117 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4120 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4121 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4123 wpa_drv_cancel_remain_on_channel(wpa_s);
4124 wpa_s->off_channel_freq = 0;
4125 wpa_s->roc_waiting_drv_freq = 0;
4127 wpas_start_wps_enrollee(group, &res);
4130 * Allow a longer timeout for join-a-running-group than normal 15
4131 * second group formation timeout since the GO may not have authorized
4132 * our connection yet.
4134 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4135 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4142 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4143 int *force_freq, int *pref_freq, int go)
4146 unsigned int freq_in_use = 0, num, i;
4148 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4152 num = get_shared_radio_freqs(wpa_s, freqs,
4153 wpa_s->num_multichan_concurrent);
4154 wpa_printf(MSG_DEBUG,
4155 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u",
4156 freq, wpa_s->num_multichan_concurrent, num);
4161 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4163 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4165 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4166 "(%u MHz) is not supported for P2P uses",
4172 for (i = 0; i < num; i++) {
4173 if (freqs[i] == freq)
4177 if (num == wpa_s->num_multichan_concurrent && !freq_in_use) {
4178 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4183 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4184 "requested channel (%u MHz)", freq);
4189 for (i = 0; i < num; i++) {
4190 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i]))
4193 if (*pref_freq == 0 && num < wpa_s->num_multichan_concurrent) {
4194 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4196 *pref_freq = freqs[i];
4198 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4200 *force_freq = freqs[i];
4206 if (num < wpa_s->num_multichan_concurrent && num > 0) {
4207 wpa_printf(MSG_DEBUG, "P2P: Current operating channels are not available for P2P. Try to use another channel");
4209 } else if (num < wpa_s->num_multichan_concurrent) {
4210 wpa_printf(MSG_DEBUG, "P2P: No current operating channels - try to use a new channel");
4213 wpa_printf(MSG_DEBUG, "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4228 * wpas_p2p_connect - Request P2P Group Formation to be started
4229 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4230 * @peer_addr: Address of the peer P2P Device
4231 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4232 * @persistent_group: Whether to create a persistent group
4233 * @auto_join: Whether to select join vs. GO Negotiation automatically
4234 * @join: Whether to join an existing group (as a client) instead of starting
4235 * Group Owner negotiation; @peer_addr is BSSID in that case
4236 * @auth: Whether to only authorize the connection instead of doing that and
4237 * initiating Group Owner negotiation
4238 * @go_intent: GO Intent or -1 to use default
4239 * @freq: Frequency for the group or 0 for auto-selection
4240 * @persistent_id: Persistent group credentials to use for forcing GO
4241 * parameters or -1 to generate new values (SSID/passphrase)
4242 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4243 * interoperability workaround when initiating group formation
4244 * @ht40: Start GO with 40 MHz channel width
4245 * @vht: Start GO with VHT support
4246 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4247 * failure, -2 on failure due to channel not currently available,
4248 * -3 if forced channel is not supported
4250 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4251 const char *pin, enum p2p_wps_method wps_method,
4252 int persistent_group, int auto_join, int join, int auth,
4253 int go_intent, int freq, int persistent_id, int pd,
4256 int force_freq = 0, pref_freq = 0;
4258 enum wpa_driver_if_type iftype;
4260 struct wpa_ssid *ssid = NULL;
4262 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4265 if (persistent_id >= 0) {
4266 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4267 if (ssid == NULL || ssid->disabled != 2 ||
4268 ssid->mode != WPAS_MODE_P2P_GO)
4272 os_free(wpa_s->global->add_psk);
4273 wpa_s->global->add_psk = NULL;
4276 go_intent = wpa_s->conf->p2p_go_intent;
4279 wpa_s->p2p_long_listen = 0;
4281 wpa_s->p2p_wps_method = wps_method;
4282 wpa_s->p2p_persistent_group = !!persistent_group;
4283 wpa_s->p2p_persistent_id = persistent_id;
4284 wpa_s->p2p_go_intent = go_intent;
4285 wpa_s->p2p_connect_freq = freq;
4286 wpa_s->p2p_fallback_to_go_neg = 0;
4287 wpa_s->p2p_pd_before_go_neg = !!pd;
4288 wpa_s->p2p_go_ht40 = !!ht40;
4289 wpa_s->p2p_go_vht = !!vht;
4292 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4293 else if (wps_method == WPS_PIN_DISPLAY) {
4294 ret = wps_generate_pin();
4295 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4297 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4300 wpa_s->p2p_pin[0] = '\0';
4302 if (join || auto_join) {
4303 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4305 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4306 "connect a running group from " MACSTR,
4307 MAC2STR(peer_addr));
4308 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4311 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4312 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4314 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4315 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4319 os_get_reltime(&wpa_s->p2p_auto_started);
4320 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4322 wpa_s->p2p_auto_started.sec,
4323 wpa_s->p2p_auto_started.usec);
4325 wpa_s->user_initiated_pd = 1;
4326 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4332 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4336 wpas_p2p_set_own_freq_preference(wpa_s,
4337 force_freq ? force_freq : pref_freq);
4339 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4341 if (wpa_s->create_p2p_iface) {
4342 /* Prepare to add a new interface for the group */
4343 iftype = WPA_IF_P2P_GROUP;
4344 if (go_intent == 15)
4345 iftype = WPA_IF_P2P_GO;
4346 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4347 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4348 "interface for the group");
4352 if_addr = wpa_s->pending_interface_addr;
4354 if_addr = wpa_s->own_addr;
4357 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
4359 force_freq, persistent_group, ssid,
4365 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4366 go_intent, if_addr, force_freq,
4367 persistent_group, ssid, pref_freq) < 0) {
4368 if (wpa_s->create_p2p_iface)
4369 wpas_p2p_remove_pending_group_interface(wpa_s);
4377 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4378 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4379 * @freq: Frequency of the channel in MHz
4380 * @duration: Duration of the stay on the channel in milliseconds
4382 * This callback is called when the driver indicates that it has started the
4383 * requested remain-on-channel duration.
4385 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4386 unsigned int freq, unsigned int duration)
4388 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4390 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4391 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4392 wpa_s->pending_listen_duration);
4393 wpa_s->pending_listen_freq = 0;
4398 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4399 unsigned int timeout)
4401 /* Limit maximum Listen state time based on driver limitation. */
4402 if (timeout > wpa_s->max_remain_on_chan)
4403 timeout = wpa_s->max_remain_on_chan;
4405 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4406 return wpa_drv_p2p_listen(wpa_s, timeout);
4408 return p2p_listen(wpa_s->global->p2p, timeout);
4413 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4414 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4415 * @freq: Frequency of the channel in MHz
4417 * This callback is called when the driver indicates that a remain-on-channel
4418 * operation has been completed, i.e., the duration on the requested channel
4421 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4424 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4425 "(p2p_long_listen=%d ms pending_action_tx=%p)",
4426 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
4427 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4429 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4430 return; /* P2P module started a new operation */
4431 if (offchannel_pending_action_tx(wpa_s))
4433 if (wpa_s->p2p_long_listen > 0)
4434 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4435 if (wpa_s->p2p_long_listen > 0) {
4436 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4437 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4440 * When listen duration is over, stop listen & update p2p_state
4443 p2p_stop_listen(wpa_s->global->p2p);
4449 * wpas_p2p_group_remove - Remove a P2P group
4450 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4451 * @ifname: Network interface name of the group interface or "*" to remove all
4453 * Returns: 0 on success, -1 on failure
4455 * This function is used to remove a P2P group. This can be used to disconnect
4456 * from a group in which the local end is a P2P Client or to end a P2P Group in
4457 * case the local end is the Group Owner. If a virtual network interface was
4458 * created for this group, that interface will be removed. Otherwise, only the
4459 * configured P2P group network will be removed from the interface.
4461 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4463 struct wpa_global *global = wpa_s->global;
4465 if (os_strcmp(ifname, "*") == 0) {
4466 struct wpa_supplicant *prev;
4467 wpa_s = global->ifaces;
4470 wpa_s = wpa_s->next;
4471 if (prev->p2p_group_interface !=
4472 NOT_P2P_GROUP_INTERFACE ||
4473 (prev->current_ssid &&
4474 prev->current_ssid->p2p_group))
4475 wpas_p2p_disconnect(prev);
4480 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4481 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4485 return wpas_p2p_disconnect(wpa_s);
4489 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
4494 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
4496 if (wpa_s->best_24_freq > 0 &&
4497 p2p_supported_freq_go(wpa_s->global->p2p,
4498 wpa_s->best_24_freq)) {
4499 freq = wpa_s->best_24_freq;
4500 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
4501 "channel: %d MHz", freq);
4503 os_get_random((u8 *) &r, sizeof(r));
4504 freq = 2412 + (r % 3) * 25;
4505 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
4506 "channel: %d MHz", freq);
4511 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
4513 if (wpa_s->best_5_freq > 0 &&
4514 p2p_supported_freq_go(wpa_s->global->p2p,
4515 wpa_s->best_5_freq)) {
4516 freq = wpa_s->best_5_freq;
4517 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
4518 "channel: %d MHz", freq);
4520 os_get_random((u8 *) &r, sizeof(r));
4521 freq = 5180 + (r % 4) * 20;
4522 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4523 wpa_printf(MSG_DEBUG, "P2P: Could not select "
4524 "5 GHz channel for P2P group");
4527 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
4528 "channel: %d MHz", freq);
4532 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
4533 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
4534 "(%u MHz) is not supported for P2P uses",
4543 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
4544 struct p2p_go_neg_results *params,
4545 int freq, int ht40, int vht,
4546 const struct p2p_channels *channels)
4549 unsigned int pref_freq;
4550 unsigned int num, i;
4552 os_memset(params, 0, sizeof(*params));
4553 params->role_go = 1;
4554 params->ht40 = ht40;
4557 if (!freq_included(channels, freq)) {
4558 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
4562 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
4563 "frequency %d MHz", freq);
4564 params->freq = freq;
4565 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
4566 wpa_s->conf->p2p_oper_channel >= 1 &&
4567 wpa_s->conf->p2p_oper_channel <= 11 &&
4568 freq_included(channels,
4569 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
4570 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
4571 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4572 "frequency %d MHz", params->freq);
4573 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
4574 wpa_s->conf->p2p_oper_reg_class == 116 ||
4575 wpa_s->conf->p2p_oper_reg_class == 117 ||
4576 wpa_s->conf->p2p_oper_reg_class == 124 ||
4577 wpa_s->conf->p2p_oper_reg_class == 126 ||
4578 wpa_s->conf->p2p_oper_reg_class == 127) &&
4579 freq_included(channels,
4580 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
4581 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
4582 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
4583 "frequency %d MHz", params->freq);
4584 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4585 wpa_s->best_overall_freq > 0 &&
4586 p2p_supported_freq_go(wpa_s->global->p2p,
4587 wpa_s->best_overall_freq) &&
4588 freq_included(channels, wpa_s->best_overall_freq)) {
4589 params->freq = wpa_s->best_overall_freq;
4590 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
4591 "channel %d MHz", params->freq);
4592 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4593 wpa_s->best_24_freq > 0 &&
4594 p2p_supported_freq_go(wpa_s->global->p2p,
4595 wpa_s->best_24_freq) &&
4596 freq_included(channels, wpa_s->best_24_freq)) {
4597 params->freq = wpa_s->best_24_freq;
4598 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
4599 "channel %d MHz", params->freq);
4600 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
4601 wpa_s->best_5_freq > 0 &&
4602 p2p_supported_freq_go(wpa_s->global->p2p,
4603 wpa_s->best_5_freq) &&
4604 freq_included(channels, wpa_s->best_5_freq)) {
4605 params->freq = wpa_s->best_5_freq;
4606 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
4607 "channel %d MHz", params->freq);
4608 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
4610 params->freq = pref_freq;
4611 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
4612 "channels", params->freq);
4615 for (chan = 0; chan < 11; chan++) {
4616 params->freq = 2412 + chan * 5;
4617 if (!wpas_p2p_disallowed_freq(wpa_s->global,
4619 freq_included(channels, params->freq))
4623 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel "
4627 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference "
4628 "known)", params->freq);
4631 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
4635 res = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4636 wpa_s->num_multichan_concurrent);
4643 for (i = 0; i < num; i++) {
4644 if (freq && freqs[i] == freq)
4646 if (!freq && freq_included(channels, freqs[i])) {
4647 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
4649 params->freq = freqs[i];
4655 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
4657 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
4659 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
4662 } else if (num == 0) {
4663 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
4665 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
4674 static struct wpa_supplicant *
4675 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
4678 struct wpa_supplicant *group_wpa_s;
4680 if (!wpas_p2p_create_iface(wpa_s)) {
4681 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
4683 wpa_s->p2p_first_connection_timeout = 0;
4687 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
4688 WPA_IF_P2P_CLIENT) < 0) {
4689 wpa_msg_global(wpa_s, MSG_ERROR,
4690 "P2P: Failed to add group interface");
4693 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
4694 if (group_wpa_s == NULL) {
4695 wpa_msg_global(wpa_s, MSG_ERROR,
4696 "P2P: Failed to initialize group interface");
4697 wpas_p2p_remove_pending_group_interface(wpa_s);
4701 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
4702 group_wpa_s->ifname);
4703 group_wpa_s->p2p_first_connection_timeout = 0;
4709 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
4710 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4711 * @persistent_group: Whether to create a persistent group
4712 * @freq: Frequency for the group or 0 to indicate no hardcoding
4713 * @ht40: Start GO with 40 MHz channel width
4714 * @vht: Start GO with VHT support
4715 * Returns: 0 on success, -1 on failure
4717 * This function creates a new P2P group with the local end as the Group Owner,
4718 * i.e., without using Group Owner Negotiation.
4720 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
4721 int freq, int ht40, int vht)
4723 struct p2p_go_neg_results params;
4725 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4728 os_free(wpa_s->global->add_psk);
4729 wpa_s->global->add_psk = NULL;
4731 /* Make sure we are not running find during connection establishment */
4732 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
4733 wpas_p2p_stop_find_oper(wpa_s);
4735 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4739 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
4742 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
4743 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
4744 "(%u MHz) is not supported for P2P uses",
4748 p2p_go_params(wpa_s->global->p2p, ¶ms);
4749 params.persistent_group = persistent_group;
4751 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
4754 wpas_start_wps_go(wpa_s, ¶ms, 0);
4760 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
4761 struct wpa_ssid *params, int addr_allocated)
4763 struct wpa_ssid *ssid;
4765 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
4768 wpa_s->p2p_last_4way_hs_fail = NULL;
4770 wpa_supplicant_ap_deinit(wpa_s);
4772 ssid = wpa_config_add_network(wpa_s->conf);
4775 wpa_config_set_network_defaults(ssid);
4776 ssid->temporary = 1;
4777 ssid->proto = WPA_PROTO_RSN;
4778 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
4779 ssid->group_cipher = WPA_CIPHER_CCMP;
4780 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
4781 ssid->ssid = os_malloc(params->ssid_len);
4782 if (ssid->ssid == NULL) {
4783 wpa_config_remove_network(wpa_s->conf, ssid->id);
4786 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
4787 ssid->ssid_len = params->ssid_len;
4788 ssid->p2p_group = 1;
4789 ssid->export_keys = 1;
4790 if (params->psk_set) {
4791 os_memcpy(ssid->psk, params->psk, 32);
4794 if (params->passphrase)
4795 ssid->passphrase = os_strdup(params->passphrase);
4797 wpa_s->show_group_started = 1;
4799 wpa_supplicant_select_network(wpa_s, ssid);
4805 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
4806 struct wpa_ssid *ssid, int addr_allocated,
4807 int freq, int ht40, int vht,
4808 const struct p2p_channels *channels,
4809 int connection_timeout)
4811 struct p2p_go_neg_results params;
4814 if (ssid->disabled != 2 || ssid->ssid == NULL)
4817 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
4818 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
4819 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
4824 os_free(wpa_s->global->add_psk);
4825 wpa_s->global->add_psk = NULL;
4827 /* Make sure we are not running find during connection establishment */
4828 wpas_p2p_stop_find_oper(wpa_s);
4830 wpa_s->p2p_fallback_to_go_neg = 0;
4832 if (ssid->mode == WPAS_MODE_INFRA)
4833 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated);
4835 if (ssid->mode != WPAS_MODE_P2P_GO)
4838 freq = wpas_p2p_select_go_freq(wpa_s, freq);
4842 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
4846 params.psk_set = ssid->psk_set;
4848 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
4849 if (ssid->passphrase) {
4850 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
4851 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
4852 "persistent group");
4855 os_strlcpy(params.passphrase, ssid->passphrase,
4856 sizeof(params.passphrase));
4858 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
4859 params.ssid_len = ssid->ssid_len;
4860 params.persistent_group = 1;
4862 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
4866 wpa_s->p2p_first_connection_timeout = connection_timeout;
4867 wpas_start_wps_go(wpa_s, ¶ms, 0);
4873 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
4874 struct wpabuf *proberesp_ies)
4876 struct wpa_supplicant *wpa_s = ctx;
4877 if (wpa_s->ap_iface) {
4878 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
4879 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
4880 wpabuf_free(beacon_ies);
4881 wpabuf_free(proberesp_ies);
4885 wpabuf_free(hapd->p2p_beacon_ie);
4886 hapd->p2p_beacon_ie = beacon_ies;
4888 wpabuf_free(hapd->p2p_probe_resp_ie);
4889 hapd->p2p_probe_resp_ie = proberesp_ies;
4891 wpabuf_free(beacon_ies);
4892 wpabuf_free(proberesp_ies);
4894 wpa_supplicant_ap_update_beacon(wpa_s);
4898 static void wpas_p2p_idle_update(void *ctx, int idle)
4900 struct wpa_supplicant *wpa_s = ctx;
4901 if (!wpa_s->ap_iface)
4903 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
4905 wpas_p2p_set_group_idle_timeout(wpa_s);
4907 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4911 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
4912 struct wpa_ssid *ssid)
4914 struct p2p_group *group;
4915 struct p2p_group_config *cfg;
4917 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
4919 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4922 cfg = os_zalloc(sizeof(*cfg));
4926 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
4927 cfg->persistent_group = 2;
4928 else if (ssid->p2p_persistent_group)
4929 cfg->persistent_group = 1;
4930 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
4931 if (wpa_s->max_stations &&
4932 wpa_s->max_stations < wpa_s->conf->max_num_sta)
4933 cfg->max_clients = wpa_s->max_stations;
4935 cfg->max_clients = wpa_s->conf->max_num_sta;
4936 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
4937 cfg->ssid_len = ssid->ssid_len;
4938 cfg->cb_ctx = wpa_s;
4939 cfg->ie_update = wpas_p2p_ie_update;
4940 cfg->idle_update = wpas_p2p_idle_update;
4942 group = p2p_group_init(wpa_s->global->p2p, cfg);
4945 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
4946 p2p_group_notif_formation_done(group);
4947 wpa_s->p2p_group = group;
4952 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4955 struct wpa_ssid *ssid = wpa_s->current_ssid;
4957 if (!wpa_s->p2p_in_provisioning) {
4958 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
4959 "provisioning not in progress");
4963 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4964 u8 go_dev_addr[ETH_ALEN];
4965 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
4966 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
4968 /* Clear any stored provisioning info */
4969 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
4972 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
4974 wpa_s->p2p_go_group_formation_completed = 1;
4975 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
4977 * Use a separate timeout for initial data connection to
4978 * complete to allow the group to be removed automatically if
4979 * something goes wrong in this step before the P2P group idle
4980 * timeout mechanism is taken into use.
4982 wpa_dbg(wpa_s, MSG_DEBUG,
4983 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
4984 P2P_MAX_INITIAL_CONN_WAIT);
4985 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
4986 wpas_p2p_group_formation_timeout,
4987 wpa_s->parent, NULL);
4990 * Use a separate timeout for initial data connection to
4991 * complete to allow the group to be removed automatically if
4992 * the client does not complete data connection successfully.
4994 wpa_dbg(wpa_s, MSG_DEBUG,
4995 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
4996 P2P_MAX_INITIAL_CONN_WAIT_GO);
4997 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
4998 wpas_p2p_group_formation_timeout,
4999 wpa_s->parent, NULL);
5001 * Complete group formation on first successful data connection
5003 wpa_s->p2p_go_group_formation_completed = 0;
5005 if (wpa_s->global->p2p)
5006 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5007 else if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5008 wpa_drv_wps_success_cb(wpa_s, peer_addr);
5009 wpas_group_formation_completed(wpa_s, 1);
5013 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5014 struct wps_event_fail *fail)
5016 if (!wpa_s->p2p_in_provisioning) {
5017 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5018 "provisioning not in progress");
5022 if (wpa_s->go_params) {
5023 p2p_clear_provisioning_info(
5025 wpa_s->go_params->peer_device_addr);
5028 wpas_notify_p2p_wps_failed(wpa_s, fail);
5032 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5033 const char *config_method,
5034 enum wpas_p2p_prov_disc_use use)
5038 wpa_s->p2p_fallback_to_go_neg = 0;
5039 wpa_s->pending_pd_use = NORMAL_PD;
5040 if (os_strncmp(config_method, "display", 7) == 0)
5041 config_methods = WPS_CONFIG_DISPLAY;
5042 else if (os_strncmp(config_method, "keypad", 6) == 0)
5043 config_methods = WPS_CONFIG_KEYPAD;
5044 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5045 os_strncmp(config_method, "pushbutton", 10) == 0)
5046 config_methods = WPS_CONFIG_PUSHBUTTON;
5048 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5052 if (use == WPAS_P2P_PD_AUTO) {
5053 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5054 wpa_s->pending_pd_config_methods = config_methods;
5055 wpa_s->p2p_auto_pd = 1;
5056 wpa_s->p2p_auto_join = 0;
5057 wpa_s->pending_pd_before_join = 0;
5058 wpa_s->auto_pd_scan_retry = 0;
5059 wpas_p2p_stop_find(wpa_s);
5060 wpa_s->p2p_join_scan_count = 0;
5061 os_get_reltime(&wpa_s->p2p_auto_started);
5062 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5063 wpa_s->p2p_auto_started.sec,
5064 wpa_s->p2p_auto_started.usec);
5065 wpas_p2p_join_scan(wpa_s, NULL);
5069 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5070 return wpa_drv_p2p_prov_disc_req(wpa_s, peer_addr,
5072 use == WPAS_P2P_PD_FOR_JOIN);
5075 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5078 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
5079 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5084 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5087 return p2p_scan_result_text(ies, ies_len, buf, end);
5091 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5093 if (!offchannel_pending_action_tx(wpa_s))
5096 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5097 "operation request");
5098 offchannel_clear_pending_action_tx(wpa_s);
5102 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5103 enum p2p_discovery_type type,
5104 unsigned int num_req_dev_types, const u8 *req_dev_types,
5105 const u8 *dev_id, unsigned int search_delay)
5107 wpas_p2p_clear_pending_action_tx(wpa_s);
5108 wpa_s->p2p_long_listen = 0;
5110 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5111 return wpa_drv_p2p_find(wpa_s, timeout, type);
5113 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5114 wpa_s->p2p_in_provisioning)
5117 wpa_supplicant_cancel_sched_scan(wpa_s);
5119 return p2p_find(wpa_s->global->p2p, timeout, type,
5120 num_req_dev_types, req_dev_types, dev_id,
5125 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
5127 wpas_p2p_clear_pending_action_tx(wpa_s);
5128 wpa_s->p2p_long_listen = 0;
5129 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5130 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5131 wpa_s->global->p2p_cb_on_scan_complete = 0;
5133 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT) {
5134 wpa_drv_p2p_stop_find(wpa_s);
5138 if (wpa_s->global->p2p)
5139 p2p_stop_find(wpa_s->global->p2p);
5145 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5147 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5149 wpas_p2p_remove_pending_group_interface(wpa_s);
5153 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5155 struct wpa_supplicant *wpa_s = eloop_ctx;
5156 wpa_s->p2p_long_listen = 0;
5160 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5164 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5167 wpa_supplicant_cancel_sched_scan(wpa_s);
5168 wpas_p2p_clear_pending_action_tx(wpa_s);
5172 * This is a request for unlimited Listen state. However, at
5173 * least for now, this is mapped to a Listen state for one
5178 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5179 wpa_s->p2p_long_listen = 0;
5182 * Stop previous find/listen operation to avoid trying to request a new
5183 * remain-on-channel operation while the driver is still running the
5186 if (wpa_s->global->p2p)
5187 p2p_stop_find(wpa_s->global->p2p);
5189 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5190 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5191 wpa_s->p2p_long_listen = timeout * 1000;
5192 eloop_register_timeout(timeout, 0,
5193 wpas_p2p_long_listen_timeout,
5201 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5202 u8 *buf, size_t len, int p2p_group)
5204 struct wpabuf *p2p_ie;
5207 if (wpa_s->global->p2p_disabled)
5209 if (wpa_s->global->p2p == NULL)
5214 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5215 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5217 wpabuf_free(p2p_ie);
5223 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
5224 const u8 *dst, const u8 *bssid,
5225 const u8 *ie, size_t ie_len, int ssi_signal)
5227 if (wpa_s->global->p2p_disabled)
5229 if (wpa_s->global->p2p == NULL)
5232 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5234 case P2P_PREQ_NOT_P2P:
5235 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5238 case P2P_PREQ_MALFORMED:
5239 case P2P_PREQ_NOT_LISTEN:
5240 case P2P_PREQ_NOT_PROCESSED:
5241 default: /* make gcc happy */
5243 case P2P_PREQ_PROCESSED:
5249 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5250 const u8 *sa, const u8 *bssid,
5251 u8 category, const u8 *data, size_t len, int freq)
5253 if (wpa_s->global->p2p_disabled)
5255 if (wpa_s->global->p2p == NULL)
5258 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5263 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5265 if (wpa_s->global->p2p_disabled)
5267 if (wpa_s->global->p2p == NULL)
5270 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5274 void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5276 p2p_group_deinit(wpa_s->p2p_group);
5277 wpa_s->p2p_group = NULL;
5279 wpa_s->ap_configured_cb = NULL;
5280 wpa_s->ap_configured_cb_ctx = NULL;
5281 wpa_s->ap_configured_cb_data = NULL;
5282 wpa_s->connect_without_scan = NULL;
5286 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5288 wpa_s->p2p_long_listen = 0;
5290 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5291 return wpa_drv_p2p_reject(wpa_s, addr);
5293 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5296 return p2p_reject(wpa_s->global->p2p, addr);
5300 /* Invite to reinvoke a persistent group */
5301 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5302 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
5303 int ht40, int vht, int pref_freq)
5305 enum p2p_invite_role role;
5309 int no_pref_freq_given = pref_freq == 0;
5311 wpa_s->global->p2p_invite_group = NULL;
5313 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5315 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5317 wpa_s->p2p_persistent_go_freq = freq;
5318 wpa_s->p2p_go_ht40 = !!ht40;
5319 if (ssid->mode == WPAS_MODE_P2P_GO) {
5320 role = P2P_INVITE_ROLE_GO;
5321 if (peer_addr == NULL) {
5322 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5323 "address in invitation command");
5326 if (wpas_p2p_create_iface(wpa_s)) {
5327 if (wpas_p2p_add_group_interface(wpa_s,
5328 WPA_IF_P2P_GO) < 0) {
5329 wpa_printf(MSG_ERROR, "P2P: Failed to "
5330 "allocate a new interface for the "
5334 bssid = wpa_s->pending_interface_addr;
5336 bssid = wpa_s->own_addr;
5338 role = P2P_INVITE_ROLE_CLIENT;
5339 peer_addr = ssid->bssid;
5341 wpa_s->pending_invite_ssid_id = ssid->id;
5343 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5344 role == P2P_INVITE_ROLE_GO);
5348 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5349 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5350 ssid->ssid, ssid->ssid_len,
5353 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5356 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5357 no_pref_freq_given && pref_freq > 0 &&
5358 wpa_s->num_multichan_concurrent > 1 &&
5359 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5360 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5365 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5366 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5371 /* Invite to join an active group */
5372 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5373 const u8 *peer_addr, const u8 *go_dev_addr)
5375 struct wpa_global *global = wpa_s->global;
5376 enum p2p_invite_role role;
5378 struct wpa_ssid *ssid;
5380 int freq = 0, force_freq = 0, pref_freq = 0;
5383 wpa_s->p2p_persistent_go_freq = 0;
5384 wpa_s->p2p_go_ht40 = 0;
5385 wpa_s->p2p_go_vht = 0;
5387 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5388 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5391 if (wpa_s == NULL) {
5392 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5396 ssid = wpa_s->current_ssid;
5398 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5403 wpa_s->global->p2p_invite_group = wpa_s;
5404 persistent = ssid->p2p_persistent_group &&
5405 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5406 ssid->ssid, ssid->ssid_len);
5408 if (ssid->mode == WPAS_MODE_P2P_GO) {
5409 role = P2P_INVITE_ROLE_ACTIVE_GO;
5410 bssid = wpa_s->own_addr;
5411 if (go_dev_addr == NULL)
5412 go_dev_addr = wpa_s->global->p2p_dev_addr;
5413 freq = ssid->frequency;
5415 role = P2P_INVITE_ROLE_CLIENT;
5416 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5417 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5418 "invite to current group");
5421 bssid = wpa_s->bssid;
5422 if (go_dev_addr == NULL &&
5423 !is_zero_ether_addr(wpa_s->go_dev_addr))
5424 go_dev_addr = wpa_s->go_dev_addr;
5425 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5426 (int) wpa_s->assoc_freq;
5428 wpa_s->parent->pending_invite_ssid_id = -1;
5430 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5431 return wpa_drv_p2p_invite(wpa_s, peer_addr, role, bssid,
5432 ssid->ssid, ssid->ssid_len,
5433 go_dev_addr, persistent);
5435 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5438 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5439 role == P2P_INVITE_ROLE_ACTIVE_GO);
5442 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
5444 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5445 ssid->ssid, ssid->ssid_len, force_freq,
5446 go_dev_addr, persistent, pref_freq);
5450 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
5452 struct wpa_ssid *ssid = wpa_s->current_ssid;
5453 const char *ssid_txt;
5454 u8 go_dev_addr[ETH_ALEN];
5455 int network_id = -1;
5459 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
5460 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
5461 wpa_s->parent, NULL);
5464 if (!wpa_s->show_group_started || !ssid)
5467 wpa_s->show_group_started = 0;
5469 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
5470 os_memset(go_dev_addr, 0, ETH_ALEN);
5471 if (ssid->bssid_set)
5472 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
5473 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5475 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
5477 if (wpa_s->global->p2p_group_formation == wpa_s)
5478 wpa_s->global->p2p_group_formation = NULL;
5480 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5481 (int) wpa_s->assoc_freq;
5482 if (ssid->passphrase == NULL && ssid->psk_set) {
5484 wpa_snprintf_hex(psk, sizeof(psk), ssid->psk, 32);
5485 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5486 "%s client ssid=\"%s\" freq=%d psk=%s "
5487 "go_dev_addr=" MACSTR "%s",
5488 wpa_s->ifname, ssid_txt, freq, psk,
5489 MAC2STR(go_dev_addr),
5490 persistent ? " [PERSISTENT]" : "");
5492 wpa_msg_global(wpa_s->parent, MSG_INFO, P2P_EVENT_GROUP_STARTED
5493 "%s client ssid=\"%s\" freq=%d "
5494 "passphrase=\"%s\" go_dev_addr=" MACSTR "%s",
5495 wpa_s->ifname, ssid_txt, freq,
5496 ssid->passphrase ? ssid->passphrase : "",
5497 MAC2STR(go_dev_addr),
5498 persistent ? " [PERSISTENT]" : "");
5502 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
5505 network_id = ssid->id;
5506 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
5509 wpas_p2p_continue_after_scan(wpa_s);
5513 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
5514 u32 interval1, u32 duration2, u32 interval2)
5516 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5518 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5521 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
5522 wpa_s->current_ssid == NULL ||
5523 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
5526 return p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
5527 wpa_s->own_addr, wpa_s->assoc_freq,
5528 duration1, interval1, duration2, interval2);
5532 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
5533 unsigned int interval)
5535 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5538 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5541 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
5545 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
5547 if (wpa_s->current_ssid == NULL) {
5549 * current_ssid can be cleared when P2P client interface gets
5550 * disconnected, so assume this interface was used as P2P
5555 return wpa_s->current_ssid->p2p_group &&
5556 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
5560 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
5562 struct wpa_supplicant *wpa_s = eloop_ctx;
5564 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
5565 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
5570 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
5572 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
5576 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
5580 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5581 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5583 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
5586 timeout = wpa_s->conf->p2p_group_idle;
5587 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
5588 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
5589 timeout = P2P_MAX_CLIENT_IDLE;
5595 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
5596 timeout = 0; /* special client mode no-timeout */
5601 if (wpa_s->p2p_in_provisioning) {
5603 * Use the normal group formation timeout during the
5604 * provisioning phase to avoid terminating this process too
5605 * early due to group idle timeout.
5607 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5608 "during provisioning");
5612 if (wpa_s->show_group_started) {
5614 * Use the normal group formation timeout between the end of
5615 * the provisioning phase and completion of 4-way handshake to
5616 * avoid terminating this process too early due to group idle
5619 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
5620 "while waiting for initial 4-way handshake to "
5625 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
5627 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
5632 /* Returns 1 if the interface was removed */
5633 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5634 u16 reason_code, const u8 *ie, size_t ie_len,
5635 int locally_generated)
5637 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5639 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5642 if (!locally_generated)
5643 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5646 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
5647 wpa_s->current_ssid &&
5648 wpa_s->current_ssid->p2p_group &&
5649 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
5650 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
5651 "session is ending");
5652 if (wpas_p2p_group_delete(wpa_s,
5653 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
5662 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
5663 u16 reason_code, const u8 *ie, size_t ie_len,
5664 int locally_generated)
5666 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5668 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5671 if (!locally_generated)
5672 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
5677 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
5679 struct p2p_data *p2p = wpa_s->global->p2p;
5684 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
5687 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
5688 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
5690 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
5691 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
5694 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
5695 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
5697 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
5698 p2p_set_uuid(p2p, wpa_s->wps->uuid);
5700 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
5701 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
5702 p2p_set_model_name(p2p, wpa_s->conf->model_name);
5703 p2p_set_model_number(p2p, wpa_s->conf->model_number);
5704 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
5707 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
5708 p2p_set_sec_dev_types(p2p,
5709 (void *) wpa_s->conf->sec_device_type,
5710 wpa_s->conf->num_sec_device_types);
5712 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
5714 p2p_remove_wps_vendor_extensions(p2p);
5715 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
5716 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
5718 p2p_add_wps_vendor_extension(
5719 p2p, wpa_s->conf->wps_vendor_ext[i]);
5723 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
5724 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
5726 country[0] = wpa_s->conf->country[0];
5727 country[1] = wpa_s->conf->country[1];
5729 p2p_set_country(p2p, country);
5732 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
5733 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
5734 wpa_s->conf->p2p_ssid_postfix ?
5735 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
5739 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
5740 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
5742 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
5743 u8 reg_class, channel;
5746 if (wpa_s->conf->p2p_listen_reg_class &&
5747 wpa_s->conf->p2p_listen_channel) {
5748 reg_class = wpa_s->conf->p2p_listen_reg_class;
5749 channel = wpa_s->conf->p2p_listen_channel;
5753 * Pick one of the social channels randomly as the
5756 os_get_random((u8 *) &r, sizeof(r));
5757 channel = 1 + (r % 3) * 5;
5759 ret = p2p_set_listen_channel(p2p, reg_class, channel);
5761 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
5764 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
5765 u8 op_reg_class, op_channel, cfg_op_channel;
5768 if (wpa_s->conf->p2p_oper_reg_class &&
5769 wpa_s->conf->p2p_oper_channel) {
5770 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
5771 op_channel = wpa_s->conf->p2p_oper_channel;
5776 * Use random operation channel from (1, 6, 11)
5777 *if no other preference is indicated.
5779 os_get_random((u8 *) &r, sizeof(r));
5780 op_channel = 1 + (r % 3) * 5;
5783 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
5786 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
5790 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
5791 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
5792 wpa_s->conf->p2p_pref_chan) < 0) {
5793 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
5797 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
5798 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
5805 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
5808 if (!wpa_s->ap_iface)
5810 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
5815 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
5817 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5819 if (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT)
5822 wpa_s->global->cross_connection = enabled;
5823 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
5826 struct wpa_supplicant *iface;
5828 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
5830 if (iface->cross_connect_enabled == 0)
5833 iface->cross_connect_enabled = 0;
5834 iface->cross_connect_in_use = 0;
5835 wpa_msg_global(iface->parent, MSG_INFO,
5836 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5838 iface->cross_connect_uplink);
5846 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
5848 struct wpa_supplicant *iface;
5850 if (!uplink->global->cross_connection)
5853 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5854 if (!iface->cross_connect_enabled)
5856 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5859 if (iface->ap_iface == NULL)
5861 if (iface->cross_connect_in_use)
5864 iface->cross_connect_in_use = 1;
5865 wpa_msg_global(iface->parent, MSG_INFO,
5866 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5867 iface->ifname, iface->cross_connect_uplink);
5872 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
5874 struct wpa_supplicant *iface;
5876 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
5877 if (!iface->cross_connect_enabled)
5879 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
5882 if (!iface->cross_connect_in_use)
5885 wpa_msg_global(iface->parent, MSG_INFO,
5886 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
5887 iface->ifname, iface->cross_connect_uplink);
5888 iface->cross_connect_in_use = 0;
5893 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
5895 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
5896 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
5897 wpa_s->cross_connect_disallowed)
5898 wpas_p2p_disable_cross_connect(wpa_s);
5900 wpas_p2p_enable_cross_connect(wpa_s);
5901 if (!wpa_s->ap_iface &&
5902 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
5903 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
5907 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
5909 wpas_p2p_disable_cross_connect(wpa_s);
5910 if (!wpa_s->ap_iface &&
5911 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
5913 wpas_p2p_set_group_idle_timeout(wpa_s);
5917 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
5919 struct wpa_supplicant *iface;
5921 if (!wpa_s->global->cross_connection)
5924 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
5927 if (iface->drv_flags &
5928 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
5930 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
5933 wpa_s->cross_connect_enabled = 1;
5934 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
5935 sizeof(wpa_s->cross_connect_uplink));
5936 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
5937 "%s to %s whenever uplink is available",
5938 wpa_s->ifname, wpa_s->cross_connect_uplink);
5940 if (iface->ap_iface || iface->current_ssid == NULL ||
5941 iface->current_ssid->mode != WPAS_MODE_INFRA ||
5942 iface->cross_connect_disallowed ||
5943 iface->wpa_state != WPA_COMPLETED)
5946 wpa_s->cross_connect_in_use = 1;
5947 wpa_msg_global(wpa_s->parent, MSG_INFO,
5948 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
5949 wpa_s->ifname, wpa_s->cross_connect_uplink);
5955 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
5957 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
5958 !wpa_s->p2p_in_provisioning)
5959 return 0; /* not P2P client operation */
5961 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
5963 if (wpa_s != wpa_s->parent)
5964 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
5965 wpas_p2p_group_formation_failed(wpa_s);
5970 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
5972 struct p2p_channels chan, cli_chan;
5974 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
5977 os_memset(&chan, 0, sizeof(chan));
5978 os_memset(&cli_chan, 0, sizeof(cli_chan));
5979 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
5980 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
5985 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
5989 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
5990 struct wpa_scan_results *scan_res)
5992 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5996 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
5998 struct wpa_global *global = wpa_s->global;
6002 if (global->p2p == NULL)
6005 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6007 if (wpa_s->pending_interface_name[0] &&
6008 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6011 peer = p2p_get_go_neg_peer(global->p2p);
6013 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6014 MACSTR, MAC2STR(peer));
6015 p2p_unauthorize(global->p2p, peer);
6019 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6020 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6021 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6025 if (wpa_s->pending_pd_before_join) {
6026 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6027 wpa_s->pending_pd_before_join = 0;
6031 wpas_p2p_stop_find(wpa_s);
6033 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6034 if (wpa_s == global->p2p_group_formation &&
6035 (wpa_s->p2p_in_provisioning ||
6036 wpa_s->parent->pending_interface_type ==
6037 WPA_IF_P2P_CLIENT)) {
6038 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6039 "formation found - cancelling",
6042 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6043 wpa_s->parent, NULL);
6044 if (wpa_s->p2p_in_provisioning) {
6045 wpas_group_formation_completed(wpa_s, 0);
6048 wpas_p2p_group_delete(wpa_s,
6049 P2P_GROUP_REMOVAL_REQUESTED);
6055 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6063 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6065 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6068 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6069 "being available anymore");
6070 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
6074 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6075 int freq_24, int freq_5, int freq_overall)
6077 struct p2p_data *p2p = wpa_s->global->p2p;
6078 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6080 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6084 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6087 struct p2p_data *p2p = wpa_s->global->p2p;
6089 if (p2p == NULL || (wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_MGMT))
6092 if (hwaddr_aton(addr, peer))
6095 return p2p_unauthorize(p2p, peer);
6100 * wpas_p2p_disconnect - Disconnect from a P2P Group
6101 * @wpa_s: Pointer to wpa_supplicant data
6102 * Returns: 0 on success, -1 on failure
6104 * This can be used to disconnect from a group in which the local end is a P2P
6105 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6106 * virtual network interface was created for this group, that interface will be
6107 * removed. Otherwise, only the configured P2P group network will be removed
6108 * from the interface.
6110 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6116 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6121 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6125 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6128 ret = p2p_in_progress(wpa_s->global->p2p);
6131 * Check whether there is an ongoing WPS provisioning step (or
6132 * other parts of group formation) on another interface since
6133 * p2p_in_progress() does not report this to avoid issues for
6134 * scans during such provisioning step.
6136 if (wpa_s->global->p2p_group_formation &&
6137 wpa_s->global->p2p_group_formation != wpa_s) {
6138 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6139 "in group formation",
6140 wpa_s->global->p2p_group_formation->ifname);
6145 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6148 if (now.sec > wpa_s->global->p2p_go_wait_client.sec +
6149 P2P_MAX_INITIAL_CONN_WAIT_GO) {
6150 /* Wait for the first client has expired */
6151 wpa_s->global->p2p_go_wait_client.sec = 0;
6153 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6162 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6163 struct wpa_ssid *ssid)
6165 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6166 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6167 wpa_s->parent, NULL) > 0) {
6169 * Remove the network by scheduling the group formation
6170 * timeout to happen immediately. The teardown code
6171 * needs to be scheduled to run asynch later so that we
6172 * don't delete data from under ourselves unexpectedly.
6173 * Calling wpas_p2p_group_formation_timeout directly
6174 * causes a series of crashes in WPS failure scenarios.
6176 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6177 "P2P group network getting removed");
6178 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6179 wpa_s->parent, NULL);
6184 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
6185 const u8 *addr, const u8 *ssid,
6191 for (s = wpa_s->conf->ssid; s; s = s->next) {
6192 if (s->disabled != 2)
6195 (ssid_len != s->ssid_len ||
6196 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6199 if (s->mode == WPAS_MODE_P2P_GO)
6203 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6204 return s; /* peer is GO in the persistent group */
6205 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6207 for (i = 0; i < s->num_p2p_clients; i++) {
6208 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6209 addr, ETH_ALEN) == 0)
6210 return s; /* peer is P2P client in persistent
6219 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6222 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6223 wpa_s->parent, NULL) > 0) {
6225 * This can happen if WPS provisioning step is not terminated
6226 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6227 * peer was able to connect, there is no need to time out group
6228 * formation after this, though. In addition, this is used with
6229 * the initial connection wait on the GO as a separate formation
6230 * timeout and as such, expected to be hit after the initial WPS
6231 * provisioning step.
6233 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6235 if (!wpa_s->p2p_go_group_formation_completed) {
6236 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6237 wpa_s->p2p_go_group_formation_completed = 1;
6238 wpa_s->global->p2p_group_formation = NULL;
6239 wpa_s->p2p_in_provisioning = 0;
6241 wpa_s->global->p2p_go_wait_client.sec = 0;
6244 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6248 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6251 struct wpa_supplicant *group = wpa_s;
6252 if (wpa_s->global->p2p_group_formation)
6253 group = wpa_s->global->p2p_group_formation;
6254 wpa_s = wpa_s->parent;
6255 offchannel_send_action_done(wpa_s);
6257 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
6258 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6259 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6260 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6261 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6262 wpa_s->p2p_persistent_id,
6263 wpa_s->p2p_pd_before_go_neg,
6269 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6271 if (!wpa_s->p2p_fallback_to_go_neg ||
6272 wpa_s->p2p_in_provisioning <= 5)
6275 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6276 return 0; /* peer operating as a GO */
6278 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6279 "fallback to GO Negotiation");
6280 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6286 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6288 struct wpa_supplicant *ifs;
6290 if (wpa_s->wpa_state > WPA_SCANNING) {
6291 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6292 "concurrent operation",
6293 P2P_CONCURRENT_SEARCH_DELAY);
6294 return P2P_CONCURRENT_SEARCH_DELAY;
6297 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6299 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
6300 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6301 "delay due to concurrent operation on "
6303 P2P_CONCURRENT_SEARCH_DELAY, ifs->ifname);
6304 return P2P_CONCURRENT_SEARCH_DELAY;
6312 void wpas_p2p_continue_after_scan(struct wpa_supplicant *wpa_s)
6314 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Station mode scan operation not "
6315 "pending anymore (sta_scan_pending=%d "
6316 "p2p_cb_on_scan_complete=%d)", wpa_s->sta_scan_pending,
6317 wpa_s->global->p2p_cb_on_scan_complete);
6318 wpa_s->sta_scan_pending = 0;
6320 if (!wpa_s->global->p2p_cb_on_scan_complete)
6322 wpa_s->global->p2p_cb_on_scan_complete = 0;
6324 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6327 if (p2p_other_scan_completed(wpa_s->global->p2p) == 1) {
6328 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Pending P2P operation "
6329 "continued after successful connection");
6330 p2p_increase_search_delay(wpa_s->global->p2p,
6331 wpas_p2p_search_delay(wpa_s));
6336 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6337 struct wpa_ssid *s, const u8 *addr,
6340 struct psk_list_entry *psk, *tmp;
6343 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6345 if ((iface_addr && !psk->p2p &&
6346 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6347 (!iface_addr && psk->p2p &&
6348 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6349 wpa_dbg(wpa_s, MSG_DEBUG,
6350 "P2P: Remove persistent group PSK list entry for "
6352 MAC2STR(psk->addr), psk->p2p);
6353 dl_list_del(&psk->list);
6363 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6364 const u8 *p2p_dev_addr,
6365 const u8 *psk, size_t psk_len)
6367 struct wpa_ssid *ssid = wpa_s->current_ssid;
6368 struct wpa_ssid *persistent;
6369 struct psk_list_entry *p;
6371 if (psk_len != sizeof(p->psk))
6375 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6376 " p2p_dev_addr=" MACSTR,
6377 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6378 if (is_zero_ether_addr(p2p_dev_addr))
6379 p2p_dev_addr = NULL;
6381 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6385 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6386 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6387 /* To be added to persistent group once created */
6388 if (wpa_s->global->add_psk == NULL) {
6389 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6390 if (wpa_s->global->add_psk == NULL)
6393 p = wpa_s->global->add_psk;
6396 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6399 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6401 os_memcpy(p->psk, psk, psk_len);
6405 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6406 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6410 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6413 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6417 p = os_zalloc(sizeof(*p));
6422 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6425 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6427 os_memcpy(p->psk, psk, psk_len);
6429 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS) {
6430 struct psk_list_entry *last;
6431 last = dl_list_last(&persistent->psk_list,
6432 struct psk_list_entry, list);
6433 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
6434 MACSTR " (p2p=%u) to make room for a new one",
6435 MAC2STR(last->addr), last->p2p);
6436 dl_list_del(&last->list);
6440 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
6441 p2p_dev_addr ? p2p_dev_addr : mac_addr,
6442 p2p_dev_addr == NULL);
6444 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
6445 MACSTR, MAC2STR(p2p_dev_addr));
6447 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
6450 dl_list_add(&persistent->psk_list, &p->list);
6452 #ifndef CONFIG_NO_CONFIG_WRITE
6453 if (wpa_s->parent->conf->update_config &&
6454 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
6455 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
6456 #endif /* CONFIG_NO_CONFIG_WRITE */
6460 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
6461 struct wpa_ssid *s, const u8 *addr,
6466 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
6468 #ifndef CONFIG_NO_CONFIG_WRITE
6469 if (wpa_s->conf->update_config &&
6470 wpa_config_write(wpa_s->confname, wpa_s->conf))
6471 wpa_dbg(wpa_s, MSG_DEBUG,
6472 "P2P: Failed to update configuration");
6473 #endif /* CONFIG_NO_CONFIG_WRITE */
6478 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
6479 const u8 *peer, int iface_addr)
6481 struct hostapd_data *hapd;
6482 struct hostapd_wpa_psk *psk, *prev, *rem;
6483 struct sta_info *sta;
6485 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
6486 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
6489 /* Remove per-station PSK entry */
6490 hapd = wpa_s->ap_iface->bss[0];
6492 psk = hapd->conf->ssid.wpa_psk;
6494 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
6496 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
6497 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
6498 MACSTR " iface_addr=%d",
6499 MAC2STR(peer), iface_addr);
6501 prev->next = psk->next;
6503 hapd->conf->ssid.wpa_psk = psk->next;
6513 /* Disconnect from group */
6515 sta = ap_get_sta(hapd, peer);
6517 sta = ap_get_sta_p2p(hapd, peer);
6519 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
6520 " (iface_addr=%d) from group",
6521 MAC2STR(peer), iface_addr);
6522 hostapd_drv_sta_deauth(hapd, sta->addr,
6523 WLAN_REASON_DEAUTH_LEAVING);
6524 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
6529 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
6533 struct wpa_supplicant *w;
6535 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
6537 /* Remove from any persistent group */
6538 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
6539 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
6542 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
6543 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
6546 /* Remove from any operating group */
6547 for (w = wpa_s->global->ifaces; w; w = w->next)
6548 wpas_p2p_remove_client_go(w, peer, iface_addr);
6552 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
6554 struct wpa_supplicant *wpa_s = eloop_ctx;
6555 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
6559 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
6561 struct wpa_supplicant *wpa_s = eloop_ctx;
6563 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
6564 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
6568 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
6569 struct wpa_ssid *ssid)
6571 struct wpa_supplicant *iface;
6573 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6574 if (!iface->current_ssid ||
6575 iface->current_ssid->frequency == freq ||
6576 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
6577 !iface->current_ssid->p2p_group))
6580 /* Remove the connection with least priority */
6581 if (!wpas_is_p2p_prioritized(iface)) {
6582 /* STA connection has priority over existing
6583 * P2P connection, so remove the interface. */
6584 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
6585 eloop_register_timeout(0, 0,
6586 wpas_p2p_group_freq_conflict,
6588 /* If connection in progress is P2P connection, do not
6589 * proceed for the connection. */
6595 /* P2P connection has priority, disable the STA network
6597 wpa_supplicant_disable_network(wpa_s->global->ifaces,
6599 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
6600 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
6601 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
6603 /* If P2P connection is in progress, continue
6616 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
6618 struct wpa_ssid *ssid = wpa_s->current_ssid;
6620 if (ssid == NULL || !ssid->p2p_group)
6623 if (wpa_s->p2p_last_4way_hs_fail &&
6624 wpa_s->p2p_last_4way_hs_fail == ssid) {
6625 u8 go_dev_addr[ETH_ALEN];
6626 struct wpa_ssid *persistent;
6628 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
6630 ssid->ssid_len) <= 0) {
6631 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
6635 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
6636 MACSTR, MAC2STR(go_dev_addr));
6637 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
6640 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
6641 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
6644 wpa_msg_global(wpa_s->parent, MSG_INFO,
6645 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
6648 wpa_s->p2p_last_4way_hs_fail = NULL;
6650 * Remove the group from a timeout to avoid issues with caller
6651 * continuing to use the interface if this is on a P2P group
6654 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
6659 wpa_s->p2p_last_4way_hs_fail = ssid;