3 * Copyright (c) 2009-2010, Atheros Communications
4 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
14 #include "common/ieee802_11_common.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/wpa_ctrl.h"
17 #include "wps/wps_i.h"
19 #include "ap/hostapd.h"
20 #include "ap/ap_config.h"
21 #include "ap/sta_info.h"
22 #include "ap/ap_drv_ops.h"
23 #include "ap/wps_hostapd.h"
24 #include "ap/p2p_hostapd.h"
25 #include "eapol_supp/eapol_supp_sm.h"
26 #include "rsn_supp/wpa.h"
27 #include "wpa_supplicant_i.h"
30 #include "config_ssid.h"
35 #include "offchannel.h"
36 #include "wps_supplicant.h"
37 #include "p2p_supplicant.h"
38 #include "wifi_display.h"
42 * How many times to try to scan to find the GO before giving up on join
45 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49 #ifndef P2P_MAX_CLIENT_IDLE
51 * How many seconds to try to reconnect to the GO when connection in P2P client
54 #define P2P_MAX_CLIENT_IDLE 10
55 #endif /* P2P_MAX_CLIENT_IDLE */
57 #ifndef P2P_MAX_INITIAL_CONN_WAIT
59 * How many seconds to wait for initial 4-way handshake to get completed after
60 * WPS provisioning step or after the re-invocation of a persistent group on a
63 #define P2P_MAX_INITIAL_CONN_WAIT 10
64 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
66 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
68 * How many seconds to wait for initial 4-way handshake to get completed after
69 * WPS provisioning step on the GO. This controls the extra time the P2P
70 * operation is considered to be in progress (e.g., to delay other scans) after
71 * WPS provisioning has been completed on the GO during group formation.
73 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
74 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
76 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
78 * How many seconds to wait for initial 4-way handshake to get completed after
79 * re-invocation of a persistent group on the GO when the client is expected
80 * to connect automatically (no user interaction).
82 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
83 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
85 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
87 enum p2p_group_removal_reason {
88 P2P_GROUP_REMOVAL_UNKNOWN,
89 P2P_GROUP_REMOVAL_SILENT,
90 P2P_GROUP_REMOVAL_FORMATION_FAILED,
91 P2P_GROUP_REMOVAL_REQUESTED,
92 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
93 P2P_GROUP_REMOVAL_UNAVAILABLE,
94 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
95 P2P_GROUP_REMOVAL_PSK_FAILURE,
96 P2P_GROUP_REMOVAL_FREQ_CONFLICT
100 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
101 static struct wpa_supplicant *
102 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
104 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
105 const u8 *ssid, size_t ssid_len);
106 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
107 const u8 *ssid, size_t ssid_len);
108 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
109 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
110 const u8 *dev_addr, enum p2p_wps_method wps_method,
111 int auto_join, int freq,
112 const u8 *ssid, size_t ssid_len);
113 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
114 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
115 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
116 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
117 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
119 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
120 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
122 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
123 static void wpas_stop_listen(void *ctx);
124 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
125 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
129 * Get the number of concurrent channels that the HW can operate, but that are
130 * currently not in use by any of the wpa_supplicant interfaces.
132 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
137 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
141 num = get_shared_radio_freqs(wpa_s, freqs,
142 wpa_s->num_multichan_concurrent);
145 unused = wpa_s->num_multichan_concurrent - num;
146 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
152 * Get the frequencies that are currently in use by one or more of the virtual
153 * interfaces, and that are also valid for P2P operation.
156 wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
157 struct wpa_used_freq_data *p2p_freqs,
160 struct wpa_used_freq_data *freqs;
161 unsigned int num, i, j;
163 freqs = os_calloc(wpa_s->num_multichan_concurrent,
164 sizeof(struct wpa_used_freq_data));
168 num = get_shared_radio_freqs_data(wpa_s, freqs,
169 wpa_s->num_multichan_concurrent);
171 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
173 for (i = 0, j = 0; i < num && j < len; i++) {
174 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
175 p2p_freqs[j++] = freqs[i];
180 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
186 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
189 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
191 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
192 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
193 wpas_p2p_num_unused_channels(wpa_s) > 0) {
194 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
198 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
202 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
203 struct wpa_scan_results *scan_res)
207 if (wpa_s->p2p_scan_work) {
208 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
209 wpa_s->p2p_scan_work = NULL;
210 radio_work_done(work);
213 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
216 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
217 (int) scan_res->num);
219 for (i = 0; i < scan_res->num; i++) {
220 struct wpa_scan_res *bss = scan_res->res[i];
221 struct os_reltime time_tmp_age, entry_ts;
225 time_tmp_age.sec = bss->age / 1000;
226 time_tmp_age.usec = (bss->age % 1000) * 1000;
227 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
229 ies = (const u8 *) (bss + 1);
230 ies_len = bss->ie_len;
231 if (bss->beacon_ie_len > 0 &&
232 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
233 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
234 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
235 MACSTR, MAC2STR(bss->bssid));
237 ies_len = bss->beacon_ie_len;
241 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
242 bss->freq, &entry_ts, bss->level,
247 p2p_scan_res_handled(wpa_s->global->p2p);
251 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
253 struct wpa_supplicant *wpa_s = work->wpa_s;
254 struct wpa_driver_scan_params *params = work->ctx;
258 if (!work->started) {
259 wpa_scan_free_params(params);
263 wpa_s->p2p_scan_work = NULL;
267 ret = wpa_drv_scan(wpa_s, params);
268 wpa_scan_free_params(params);
271 radio_work_done(work);
275 os_get_reltime(&wpa_s->scan_trigger_time);
276 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
277 wpa_s->own_scan_requested = 1;
278 wpa_s->p2p_scan_work = work;
282 static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
285 if (wpa_s->global->p2p_24ghz_social_channels &&
286 (freq == 2412 || freq == 2437 || freq == 2462)) {
288 * Search all social channels regardless of whether these have
289 * been disabled for P2P operating channel use to avoid missing
294 return p2p_supported_freq(wpa_s->global->p2p, freq);
298 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
299 unsigned int num_req_dev_types,
300 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
302 struct wpa_supplicant *wpa_s = ctx;
303 struct wpa_driver_scan_params *params = NULL;
304 struct wpabuf *wps_ie, *ies;
305 unsigned int num_channels = 0;
306 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
310 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
313 if (wpa_s->p2p_scan_work) {
314 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
318 params = os_zalloc(sizeof(*params));
322 /* P2P Wildcard SSID */
323 params->num_ssids = 1;
324 n = os_malloc(P2P_WILDCARD_SSID_LEN);
327 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
328 params->ssids[0].ssid = n;
329 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
331 wpa_s->wps->dev.p2p = 1;
332 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
333 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
334 num_req_dev_types, req_dev_types);
338 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
339 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
344 wpabuf_put_buf(ies, wps_ie);
347 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
349 params->p2p_probe = 1;
350 n = os_malloc(wpabuf_len(ies));
355 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
356 params->extra_ies = n;
357 params->extra_ies_len = wpabuf_len(ies);
361 case P2P_SCAN_SOCIAL:
362 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
364 if (params->freqs == NULL)
366 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
367 if (wpas_p2p_search_social_channel(
368 wpa_s, social_channels_freq[i]))
369 params->freqs[num_channels++] =
370 social_channels_freq[i];
372 params->freqs[num_channels++] = 0;
376 case P2P_SCAN_SOCIAL_PLUS_ONE:
377 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
379 if (params->freqs == NULL)
381 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
382 if (wpas_p2p_search_social_channel(
383 wpa_s, social_channels_freq[i]))
384 params->freqs[num_channels++] =
385 social_channels_freq[i];
387 if (p2p_supported_freq(wpa_s->global->p2p, freq))
388 params->freqs[num_channels++] = freq;
389 params->freqs[num_channels++] = 0;
393 radio_remove_works(wpa_s, "p2p-scan", 0);
394 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
400 wpa_scan_free_params(params);
405 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
407 switch (p2p_group_interface) {
408 case P2P_GROUP_INTERFACE_PENDING:
409 return WPA_IF_P2P_GROUP;
410 case P2P_GROUP_INTERFACE_GO:
411 return WPA_IF_P2P_GO;
412 case P2P_GROUP_INTERFACE_CLIENT:
413 return WPA_IF_P2P_CLIENT;
416 return WPA_IF_P2P_GROUP;
420 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
422 size_t ssid_len, int *go)
426 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
427 for (s = wpa_s->conf->ssid; s; s = s->next) {
428 if (s->disabled != 0 || !s->p2p_group ||
429 s->ssid_len != ssid_len ||
430 os_memcmp(ssid, s->ssid, ssid_len) != 0)
432 if (s->mode == WPAS_MODE_P2P_GO &&
433 s != wpa_s->current_ssid)
436 *go = s->mode == WPAS_MODE_P2P_GO;
445 static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
447 struct wpa_supplicant *wpa_s = eloop_ctx;
448 wpa_printf(MSG_DEBUG,
449 "P2P: Complete previously requested removal of %s",
451 wpas_p2p_disconnect(wpa_s);
455 static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
456 struct wpa_supplicant *calling_wpa_s)
458 if (calling_wpa_s == wpa_s &&
459 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
461 * The calling wpa_s instance is going to be removed. Do that
462 * from an eloop callback to keep the instance available until
463 * the caller has returned. This my be needed, e.g., to provide
464 * control interface responses on the per-interface socket.
466 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
472 return wpas_p2p_disconnect(wpa_s);
476 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
477 enum p2p_group_removal_reason removal_reason)
479 struct wpa_ssid *ssid;
483 ssid = wpa_s->current_ssid;
486 * The current SSID was not known, but there may still be a
487 * pending P2P group interface waiting for provisioning or a
488 * P2P group that is trying to reconnect.
490 ssid = wpa_s->conf->ssid;
492 if (ssid->p2p_group && ssid->disabled != 2)
497 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
499 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
504 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
506 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
507 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
508 wpa_s->reassociate = 0;
509 wpa_s->disconnected = 1;
514 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
515 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
517 if (os_strcmp(gtype, "client") == 0) {
518 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
519 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
521 wpa_printf(MSG_DEBUG,
522 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
523 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
524 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
529 if (wpa_s->cross_connect_in_use) {
530 wpa_s->cross_connect_in_use = 0;
531 wpa_msg_global(wpa_s->parent, MSG_INFO,
532 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
533 wpa_s->ifname, wpa_s->cross_connect_uplink);
535 switch (removal_reason) {
536 case P2P_GROUP_REMOVAL_REQUESTED:
537 reason = " reason=REQUESTED";
539 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
540 reason = " reason=FORMATION_FAILED";
542 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
543 reason = " reason=IDLE";
545 case P2P_GROUP_REMOVAL_UNAVAILABLE:
546 reason = " reason=UNAVAILABLE";
548 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
549 reason = " reason=GO_ENDING_SESSION";
551 case P2P_GROUP_REMOVAL_PSK_FAILURE:
552 reason = " reason=PSK_FAILURE";
554 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
555 reason = " reason=FREQ_CONFLICT";
561 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
562 wpa_msg_global(wpa_s->parent, MSG_INFO,
563 P2P_EVENT_GROUP_REMOVED "%s %s%s",
564 wpa_s->ifname, gtype, reason);
567 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
568 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
569 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
570 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
571 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
572 wpa_s->parent, NULL) > 0) {
573 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
575 wpa_s->p2p_in_provisioning = 0;
578 wpa_s->p2p_in_invitation = 0;
581 * Make sure wait for the first client does not remain active after the
582 * group has been removed.
584 wpa_s->global->p2p_go_wait_client.sec = 0;
586 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
587 struct wpa_global *global;
589 enum wpa_driver_if_type type;
590 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
592 global = wpa_s->global;
593 ifname = os_strdup(wpa_s->ifname);
594 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
595 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
596 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
597 wpa_s = global->ifaces;
599 wpa_drv_if_remove(wpa_s, type, ifname);
604 if (!wpa_s->p2p_go_group_formation_completed) {
605 wpa_s->global->p2p_group_formation = NULL;
606 wpa_s->p2p_in_provisioning = 0;
609 wpa_s->show_group_started = 0;
610 os_free(wpa_s->go_params);
611 wpa_s->go_params = NULL;
613 os_free(wpa_s->p2p_group_common_freqs);
614 wpa_s->p2p_group_common_freqs = NULL;
615 wpa_s->p2p_group_common_freqs_num = 0;
617 wpa_s->waiting_presence_resp = 0;
619 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
620 if (ssid && (ssid->p2p_group ||
621 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
622 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
624 if (ssid == wpa_s->current_ssid) {
625 wpa_sm_set_config(wpa_s->wpa, NULL);
626 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
627 wpa_s->current_ssid = NULL;
630 * Networks objects created during any P2P activities are not
631 * exposed out as they might/will confuse certain non-P2P aware
632 * applications since these network objects won't behave like
635 * Likewise, we don't send out network removed signals for such
638 wpa_config_remove_network(wpa_s->conf, id);
639 wpa_supplicant_clear_status(wpa_s);
640 wpa_supplicant_cancel_sched_scan(wpa_s);
642 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
646 wpa_supplicant_ap_deinit(wpa_s);
648 wpa_drv_deinit_p2p_cli(wpa_s);
654 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
656 const u8 *ssid, size_t ssid_len)
664 if (wpa_s->go_params)
665 bssid = wpa_s->go_params->peer_interface_addr;
667 bssid = wpa_s->bssid;
669 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
670 if (bss == NULL && wpa_s->go_params &&
671 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
672 bss = wpa_bss_get_p2p_dev_addr(
673 wpa_s, wpa_s->go_params->peer_device_addr);
675 u8 iface_addr[ETH_ALEN];
676 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
678 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
681 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
682 "group is persistent - BSS " MACSTR " not found",
687 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
689 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
692 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
693 "group is persistent - BSS " MACSTR
694 " did not include P2P IE", MAC2STR(bssid));
695 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
696 (u8 *) (bss + 1), bss->ie_len);
697 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
698 ((u8 *) bss + 1) + bss->ie_len,
703 group_capab = p2p_get_group_capab(p2p);
704 addr = p2p_get_go_dev_addr(p2p);
705 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
706 "group_capab=0x%x", group_capab);
708 os_memcpy(go_dev_addr, addr, ETH_ALEN);
709 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
712 os_memset(go_dev_addr, 0, ETH_ALEN);
715 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
716 "go_dev_addr=" MACSTR,
717 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
719 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
723 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
724 struct wpa_ssid *ssid,
725 const u8 *go_dev_addr)
730 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
731 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
732 for (s = wpa_s->conf->ssid; s; s = s->next) {
733 if (s->disabled == 2 &&
734 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
735 s->ssid_len == ssid->ssid_len &&
736 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
741 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
743 if (ssid->passphrase && !s->passphrase)
745 else if (ssid->passphrase && s->passphrase &&
746 os_strcmp(ssid->passphrase, s->passphrase) != 0)
749 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
752 s = wpa_config_add_network(wpa_s->conf);
757 * Instead of network_added we emit persistent_group_added
758 * notification. Also to keep the defense checks in
759 * persistent_group obj registration method, we set the
760 * relevant flags in s to designate it as a persistent group.
763 s->p2p_persistent_group = 1;
764 wpas_notify_persistent_group_added(wpa_s, s);
765 wpa_config_set_network_defaults(s);
769 s->p2p_persistent_group = 1;
772 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
773 s->mode = ssid->mode;
774 s->auth_alg = WPA_AUTH_ALG_OPEN;
775 s->key_mgmt = WPA_KEY_MGMT_PSK;
776 s->proto = WPA_PROTO_RSN;
777 s->pairwise_cipher = WPA_CIPHER_CCMP;
779 if (ssid->passphrase) {
780 os_free(s->passphrase);
781 s->passphrase = os_strdup(ssid->passphrase);
785 os_memcpy(s->psk, ssid->psk, 32);
787 if (s->passphrase && !s->psk_set)
788 wpa_config_update_psk(s);
789 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
791 s->ssid = os_malloc(ssid->ssid_len);
794 s->ssid_len = ssid->ssid_len;
795 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
797 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
798 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
799 wpa_s->global->add_psk = NULL;
803 if (changed && wpa_s->conf->update_config &&
804 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
805 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
812 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
815 struct wpa_ssid *ssid, *s;
820 ssid = wpa_s->current_ssid;
821 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
822 !ssid->p2p_persistent_group)
825 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
826 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
829 if (s->ssid_len == ssid->ssid_len &&
830 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
837 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
838 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
842 if (i == s->num_p2p_clients - 1)
843 return; /* already the most recent entry */
845 /* move the entry to mark it most recent */
846 os_memmove(s->p2p_client_list + i * ETH_ALEN,
847 s->p2p_client_list + (i + 1) * ETH_ALEN,
848 (s->num_p2p_clients - i - 1) * ETH_ALEN);
849 os_memcpy(s->p2p_client_list +
850 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
855 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
856 n = os_realloc_array(s->p2p_client_list,
857 s->num_p2p_clients + 1, ETH_ALEN);
860 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
861 s->p2p_client_list = n;
862 s->num_p2p_clients++;
863 } else if (!found && s->p2p_client_list) {
864 /* Not enough room for an additional entry - drop the oldest
866 os_memmove(s->p2p_client_list,
867 s->p2p_client_list + ETH_ALEN,
868 (s->num_p2p_clients - 1) * ETH_ALEN);
869 os_memcpy(s->p2p_client_list +
870 (s->num_p2p_clients - 1) * ETH_ALEN,
874 if (wpa_s->parent->conf->update_config &&
875 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
876 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
880 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
881 int go, struct wpa_ssid *ssid, int freq,
882 const u8 *psk, const char *passphrase,
883 const u8 *go_dev_addr, int persistent,
886 const char *ssid_txt;
890 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
895 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
899 if (passphrase && passphrase[0] == '\0')
903 * Include PSK/passphrase only in the control interface message and
904 * leave it out from the debug log entry.
906 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
907 P2P_EVENT_GROUP_STARTED
908 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
910 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
911 psk ? " psk=" : "", psk_txt,
912 passphrase ? " passphrase=\"" : "",
913 passphrase ? passphrase : "",
914 passphrase ? "\"" : "",
915 MAC2STR(go_dev_addr),
916 persistent ? " [PERSISTENT]" : "", extra);
917 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
918 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
919 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
920 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
925 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
928 struct wpa_ssid *ssid;
931 u8 go_dev_addr[ETH_ALEN];
935 * This callback is likely called for the main interface. Update wpa_s
936 * to use the group interface if a new interface was created for the
939 if (wpa_s->global->p2p_group_formation)
940 wpa_s = wpa_s->global->p2p_group_formation;
941 if (wpa_s->p2p_go_group_formation_completed) {
942 wpa_s->global->p2p_group_formation = NULL;
943 wpa_s->p2p_in_provisioning = 0;
945 wpa_s->p2p_in_invitation = 0;
946 wpa_s->group_formation_reported = 1;
949 wpa_msg_global(wpa_s->parent, MSG_INFO,
950 P2P_EVENT_GROUP_FORMATION_FAILURE);
951 wpas_p2p_group_delete(wpa_s,
952 P2P_GROUP_REMOVAL_FORMATION_FAILED);
956 wpa_msg_global(wpa_s->parent, MSG_INFO,
957 P2P_EVENT_GROUP_FORMATION_SUCCESS);
959 ssid = wpa_s->current_ssid;
960 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
961 ssid->mode = WPAS_MODE_P2P_GO;
962 p2p_group_notif_formation_done(wpa_s->p2p_group);
963 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
968 client = ssid->mode == WPAS_MODE_INFRA;
969 if (ssid->mode == WPAS_MODE_P2P_GO) {
970 persistent = ssid->p2p_persistent_group;
971 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
974 persistent = wpas_p2p_persistent_group(wpa_s,
979 client = wpa_s->p2p_group_interface ==
980 P2P_GROUP_INTERFACE_CLIENT;
981 os_memset(go_dev_addr, 0, ETH_ALEN);
984 wpa_s->show_group_started = 0;
987 * Indicate event only after successfully completed 4-way
988 * handshake, i.e., when the interface is ready for data
991 wpa_s->show_group_started = 1;
993 wpas_p2p_group_started(wpa_s, 1, ssid,
994 ssid ? ssid->frequency : 0,
995 ssid && ssid->passphrase == NULL &&
996 ssid->psk_set ? ssid->psk : NULL,
997 ssid ? ssid->passphrase : NULL,
998 go_dev_addr, persistent, "");
999 wpas_p2p_cross_connect_setup(wpa_s);
1000 wpas_p2p_set_group_idle_timeout(wpa_s);
1004 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1007 os_free(wpa_s->global->add_psk);
1008 wpa_s->global->add_psk = NULL;
1010 if (network_id < 0 && ssid)
1011 network_id = ssid->id;
1013 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1014 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1019 struct send_action_work {
1025 unsigned int wait_time;
1030 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1033 struct wpa_supplicant *wpa_s = eloop_ctx;
1035 if (!wpa_s->p2p_send_action_work)
1038 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1039 os_free(wpa_s->p2p_send_action_work->ctx);
1040 radio_work_done(wpa_s->p2p_send_action_work);
1041 wpa_s->p2p_send_action_work = NULL;
1045 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1047 if (wpa_s->p2p_send_action_work) {
1048 struct send_action_work *awork;
1049 awork = wpa_s->p2p_send_action_work->ctx;
1050 if (awork->wait_time == 0) {
1052 radio_work_done(wpa_s->p2p_send_action_work);
1053 wpa_s->p2p_send_action_work = NULL;
1056 * In theory, this should not be needed, but number of
1057 * places in the P2P code is still using non-zero wait
1058 * time for the last Action frame in the sequence and
1059 * some of these do not call send_action_done().
1061 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1063 eloop_register_timeout(
1064 0, awork->wait_time * 1000,
1065 wpas_p2p_send_action_work_timeout,
1072 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1074 const u8 *dst, const u8 *src,
1076 const u8 *data, size_t data_len,
1077 enum offchannel_send_action_result
1080 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1082 wpas_p2p_action_tx_clear(wpa_s);
1084 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1088 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1089 res = P2P_SEND_ACTION_SUCCESS;
1091 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1092 res = P2P_SEND_ACTION_NO_ACK;
1094 case OFFCHANNEL_SEND_ACTION_FAILED:
1095 res = P2P_SEND_ACTION_FAILED;
1099 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1101 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1102 wpa_s->pending_pd_before_join &&
1103 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1104 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1105 wpa_s->p2p_fallback_to_go_neg) {
1106 wpa_s->pending_pd_before_join = 0;
1107 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1108 "during p2p_connect-auto");
1109 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1115 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1117 struct wpa_supplicant *wpa_s = work->wpa_s;
1118 struct send_action_work *awork = work->ctx;
1121 if (work->started) {
1122 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1124 wpa_s->p2p_send_action_work = NULL;
1125 offchannel_send_action_done(wpa_s);
1131 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1132 awork->bssid, awork->buf, awork->len,
1134 wpas_p2p_send_action_tx_status, 1) < 0) {
1136 radio_work_done(work);
1139 wpa_s->p2p_send_action_work = work;
1143 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1144 unsigned int freq, const u8 *dst,
1145 const u8 *src, const u8 *bssid, const u8 *buf,
1146 size_t len, unsigned int wait_time)
1148 struct send_action_work *awork;
1150 if (wpa_s->p2p_send_action_work) {
1151 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1155 awork = os_zalloc(sizeof(*awork) + len);
1160 os_memcpy(awork->dst, dst, ETH_ALEN);
1161 os_memcpy(awork->src, src, ETH_ALEN);
1162 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1164 awork->wait_time = wait_time;
1165 os_memcpy(awork->buf, buf, len);
1167 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1168 wpas_send_action_cb, awork) < 0) {
1177 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1178 const u8 *src, const u8 *bssid, const u8 *buf,
1179 size_t len, unsigned int wait_time)
1181 struct wpa_supplicant *wpa_s = ctx;
1182 int listen_freq = -1, send_freq = -1;
1184 if (wpa_s->p2p_listen_work)
1185 listen_freq = wpa_s->p2p_listen_work->freq;
1186 if (wpa_s->p2p_send_action_work)
1187 send_freq = wpa_s->p2p_send_action_work->freq;
1188 if (listen_freq != (int) freq && send_freq != (int) freq) {
1189 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1190 listen_freq, send_freq);
1191 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1195 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1196 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1198 wpas_p2p_send_action_tx_status, 1);
1202 static void wpas_send_action_done(void *ctx)
1204 struct wpa_supplicant *wpa_s = ctx;
1206 if (wpa_s->p2p_send_action_work) {
1207 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1209 os_free(wpa_s->p2p_send_action_work->ctx);
1210 radio_work_done(wpa_s->p2p_send_action_work);
1211 wpa_s->p2p_send_action_work = NULL;
1214 offchannel_send_action_done(wpa_s);
1218 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1219 struct p2p_go_neg_results *params)
1221 if (wpa_s->go_params == NULL) {
1222 wpa_s->go_params = os_malloc(sizeof(*params));
1223 if (wpa_s->go_params == NULL)
1226 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1231 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1232 struct p2p_go_neg_results *res)
1234 wpa_s->group_formation_reported = 0;
1235 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1236 " dev_addr " MACSTR " wps_method %d",
1237 MAC2STR(res->peer_interface_addr),
1238 MAC2STR(res->peer_device_addr), res->wps_method);
1239 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1240 res->ssid, res->ssid_len);
1241 wpa_supplicant_ap_deinit(wpa_s);
1242 wpas_copy_go_neg_results(wpa_s, res);
1243 if (res->wps_method == WPS_PBC) {
1244 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1245 #ifdef CONFIG_WPS_NFC
1246 } else if (res->wps_method == WPS_NFC) {
1247 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1248 res->peer_interface_addr,
1249 wpa_s->parent->p2p_oob_dev_pw,
1250 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1251 wpa_s->parent->p2p_oob_dev_pw_id ==
1252 DEV_PW_NFC_CONNECTION_HANDOVER ?
1253 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1256 #endif /* CONFIG_WPS_NFC */
1258 u16 dev_pw_id = DEV_PW_DEFAULT;
1259 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1260 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1261 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1262 wpa_s->p2p_pin, 1, dev_pw_id);
1267 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1268 struct wpa_ssid *ssid)
1270 struct wpa_ssid *persistent;
1271 struct psk_list_entry *psk;
1272 struct hostapd_data *hapd;
1274 if (!wpa_s->ap_iface)
1277 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1279 if (persistent == NULL)
1282 hapd = wpa_s->ap_iface->bss[0];
1284 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1286 struct hostapd_wpa_psk *hpsk;
1288 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1290 MAC2STR(psk->addr), psk->p2p);
1291 hpsk = os_zalloc(sizeof(*hpsk));
1294 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1296 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1298 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1299 hpsk->next = hapd->conf->ssid.wpa_psk;
1300 hapd->conf->ssid.wpa_psk = hpsk;
1305 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1309 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1310 wpa_s->p2p_group_common_freqs_num);
1312 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1313 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1314 i, wpa_s->p2p_group_common_freqs[i]);
1318 static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1319 struct p2p_go_neg_results *params)
1321 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1323 wpa_s->p2p_group_common_freqs_num = 0;
1324 os_free(wpa_s->p2p_group_common_freqs);
1325 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1326 if (!wpa_s->p2p_group_common_freqs)
1329 for (i = 0; i < len; i++) {
1330 if (!wpa_s->go_params->freq_list[i])
1332 wpa_s->p2p_group_common_freqs[i] =
1333 wpa_s->go_params->freq_list[i];
1335 wpa_s->p2p_group_common_freqs_num = i;
1339 static void p2p_go_configured(void *ctx, void *data)
1341 struct wpa_supplicant *wpa_s = ctx;
1342 struct p2p_go_neg_results *params = data;
1343 struct wpa_ssid *ssid;
1344 int network_id = -1;
1346 p2p_go_save_group_common_freqs(wpa_s, params);
1347 p2p_go_dump_common_freqs(wpa_s);
1349 ssid = wpa_s->current_ssid;
1350 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1351 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1352 if (wpa_s->global->p2p_group_formation == wpa_s)
1353 wpa_s->global->p2p_group_formation = NULL;
1354 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1355 params->passphrase[0] == '\0' ?
1358 wpa_s->global->p2p_dev_addr,
1359 params->persistent_group, "");
1360 wpa_s->group_formation_reported = 1;
1362 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1363 if (params->persistent_group) {
1364 network_id = wpas_p2p_store_persistent_group(
1365 wpa_s->parent, ssid,
1366 wpa_s->global->p2p_dev_addr);
1367 wpas_p2p_add_psk_list(wpa_s, ssid);
1370 network_id = ssid->id;
1371 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1372 wpas_p2p_cross_connect_setup(wpa_s);
1373 wpas_p2p_set_group_idle_timeout(wpa_s);
1375 if (wpa_s->p2p_first_connection_timeout) {
1376 wpa_dbg(wpa_s, MSG_DEBUG,
1377 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1378 wpa_s->p2p_first_connection_timeout);
1379 wpa_s->p2p_go_group_formation_completed = 0;
1380 wpa_s->global->p2p_group_formation = wpa_s;
1381 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1382 wpa_s->parent, NULL);
1383 eloop_register_timeout(
1384 wpa_s->p2p_first_connection_timeout, 0,
1385 wpas_p2p_group_formation_timeout,
1386 wpa_s->parent, NULL);
1392 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1393 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1394 params->peer_interface_addr)) {
1395 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1399 if (params->wps_method == WPS_PBC) {
1400 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1401 params->peer_device_addr);
1402 #ifdef CONFIG_WPS_NFC
1403 } else if (params->wps_method == WPS_NFC) {
1404 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1405 DEV_PW_NFC_CONNECTION_HANDOVER &&
1406 !wpa_s->parent->p2p_oob_dev_pw) {
1407 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1410 wpas_ap_wps_add_nfc_pw(
1411 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1412 wpa_s->parent->p2p_oob_dev_pw,
1413 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1414 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1415 #endif /* CONFIG_WPS_NFC */
1416 } else if (wpa_s->p2p_pin[0])
1417 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1418 wpa_s->p2p_pin, NULL, 0, 0);
1419 os_free(wpa_s->go_params);
1420 wpa_s->go_params = NULL;
1424 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1425 struct p2p_go_neg_results *params,
1426 int group_formation)
1428 struct wpa_ssid *ssid;
1430 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1431 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1432 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1437 ssid = wpa_config_add_network(wpa_s->conf);
1439 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1443 wpa_s->show_group_started = 0;
1444 wpa_s->p2p_go_group_formation_completed = 0;
1445 wpa_s->group_formation_reported = 0;
1447 wpa_config_set_network_defaults(ssid);
1448 ssid->temporary = 1;
1449 ssid->p2p_group = 1;
1450 ssid->p2p_persistent_group = params->persistent_group;
1451 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1453 ssid->frequency = params->freq;
1454 ssid->ht40 = params->ht40;
1455 ssid->vht = params->vht;
1456 ssid->ssid = os_zalloc(params->ssid_len + 1);
1458 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1459 ssid->ssid_len = params->ssid_len;
1461 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1462 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1463 ssid->proto = WPA_PROTO_RSN;
1464 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1465 if (os_strlen(params->passphrase) > 0) {
1466 ssid->passphrase = os_strdup(params->passphrase);
1467 if (ssid->passphrase == NULL) {
1468 wpa_msg_global(wpa_s, MSG_ERROR,
1469 "P2P: Failed to copy passphrase for GO");
1470 wpa_config_remove_network(wpa_s->conf, ssid->id);
1474 ssid->passphrase = NULL;
1475 ssid->psk_set = params->psk_set;
1477 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1478 else if (ssid->passphrase)
1479 wpa_config_update_psk(ssid);
1480 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1482 wpa_s->ap_configured_cb = p2p_go_configured;
1483 wpa_s->ap_configured_cb_ctx = wpa_s;
1484 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1485 wpa_s->scan_req = NORMAL_SCAN_REQ;
1486 wpa_s->connect_without_scan = ssid;
1487 wpa_s->reassociate = 1;
1488 wpa_s->disconnected = 0;
1489 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1491 wpa_supplicant_req_scan(wpa_s, 0, 0);
1495 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1496 const struct wpa_supplicant *src)
1498 struct wpa_config *d;
1499 const struct wpa_config *s;
1504 #define C(n) if (s->n) d->n = os_strdup(s->n)
1513 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1514 os_memcpy(d->sec_device_type, s->sec_device_type,
1515 sizeof(d->sec_device_type));
1516 d->num_sec_device_types = s->num_sec_device_types;
1518 d->p2p_group_idle = s->p2p_group_idle;
1519 d->p2p_intra_bss = s->p2p_intra_bss;
1520 d->persistent_reconnect = s->persistent_reconnect;
1521 d->max_num_sta = s->max_num_sta;
1522 d->pbc_in_m1 = s->pbc_in_m1;
1523 d->ignore_old_scan_res = s->ignore_old_scan_res;
1524 d->beacon_int = s->beacon_int;
1525 d->dtim_period = s->dtim_period;
1526 d->disassoc_low_ack = s->disassoc_low_ack;
1527 d->disable_scan_offload = s->disable_scan_offload;
1529 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1530 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1531 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1536 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1537 char *ifname, size_t len)
1539 char *ifname_ptr = wpa_s->ifname;
1541 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1542 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1543 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1546 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1547 if (os_strlen(ifname) >= IFNAMSIZ &&
1548 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1549 /* Try to avoid going over the IFNAMSIZ length limit */
1550 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1555 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1556 enum wpa_driver_if_type type)
1558 char ifname[120], force_ifname[120];
1560 if (wpa_s->pending_interface_name[0]) {
1561 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1562 "- skip creation of a new one");
1563 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1564 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1565 "unknown?! ifname='%s'",
1566 wpa_s->pending_interface_name);
1572 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1573 force_ifname[0] = '\0';
1575 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1577 wpa_s->p2p_group_idx++;
1579 wpa_s->pending_interface_type = type;
1580 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1581 wpa_s->pending_interface_addr, NULL) < 0) {
1582 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1587 if (force_ifname[0]) {
1588 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1590 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1591 sizeof(wpa_s->pending_interface_name));
1593 os_strlcpy(wpa_s->pending_interface_name, ifname,
1594 sizeof(wpa_s->pending_interface_name));
1595 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1596 MACSTR, wpa_s->pending_interface_name,
1597 MAC2STR(wpa_s->pending_interface_addr));
1603 static void wpas_p2p_remove_pending_group_interface(
1604 struct wpa_supplicant *wpa_s)
1606 if (!wpa_s->pending_interface_name[0] ||
1607 is_zero_ether_addr(wpa_s->pending_interface_addr))
1608 return; /* No pending virtual interface */
1610 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1611 wpa_s->pending_interface_name);
1612 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1613 wpa_s->pending_interface_name);
1614 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1615 wpa_s->pending_interface_name[0] = '\0';
1619 static struct wpa_supplicant *
1620 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1622 struct wpa_interface iface;
1623 struct wpa_supplicant *group_wpa_s;
1625 if (!wpa_s->pending_interface_name[0]) {
1626 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1627 if (!wpas_p2p_create_iface(wpa_s))
1630 * Something has forced us to remove the pending interface; try
1631 * to create a new one and hope for the best that we will get
1632 * the same local address.
1634 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1635 WPA_IF_P2P_CLIENT) < 0)
1639 os_memset(&iface, 0, sizeof(iface));
1640 iface.ifname = wpa_s->pending_interface_name;
1641 iface.driver = wpa_s->driver->name;
1642 if (wpa_s->conf->ctrl_interface == NULL &&
1643 wpa_s->parent != wpa_s &&
1645 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1646 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1648 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1649 iface.driver_param = wpa_s->conf->driver_param;
1650 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1651 if (group_wpa_s == NULL) {
1652 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1653 "wpa_supplicant interface");
1656 wpa_s->pending_interface_name[0] = '\0';
1657 group_wpa_s->parent = wpa_s;
1658 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1659 P2P_GROUP_INTERFACE_CLIENT;
1660 wpa_s->global->p2p_group_formation = group_wpa_s;
1662 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1668 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1671 struct wpa_supplicant *wpa_s = eloop_ctx;
1672 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1673 wpas_p2p_group_formation_failed(wpa_s);
1677 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1679 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1680 wpa_s->parent, NULL);
1681 if (wpa_s->global->p2p)
1682 p2p_group_formation_failed(wpa_s->global->p2p);
1683 wpas_group_formation_completed(wpa_s, 0);
1687 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1689 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1690 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1691 wpa_s->parent, NULL);
1692 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1693 wpa_s->parent, NULL);
1694 wpa_s->global->p2p_fail_on_wps_complete = 0;
1698 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1700 if (wpa_s->global->p2p_group_formation != wpa_s)
1702 /* Speed up group formation timeout since this cannot succeed */
1703 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1704 wpa_s->parent, NULL);
1705 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1706 wpa_s->parent, NULL);
1710 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1712 struct wpa_supplicant *wpa_s = ctx;
1714 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1715 wpa_drv_cancel_remain_on_channel(wpa_s);
1716 wpa_s->off_channel_freq = 0;
1717 wpa_s->roc_waiting_drv_freq = 0;
1721 wpa_msg_global(wpa_s, MSG_INFO,
1722 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1724 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1725 wpas_p2p_remove_pending_group_interface(wpa_s);
1729 if (wpa_s->p2p_go_ht40)
1731 if (wpa_s->p2p_go_vht)
1734 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1735 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1737 res->role_go ? "GO" : "client", res->freq, res->ht40,
1738 MAC2STR(res->peer_device_addr),
1739 MAC2STR(res->peer_interface_addr),
1740 p2p_wps_method_text(res->wps_method));
1741 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1743 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1744 struct wpa_ssid *ssid;
1745 ssid = wpa_config_get_network(wpa_s->conf,
1746 wpa_s->p2p_persistent_id);
1747 if (ssid && ssid->disabled == 2 &&
1748 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1749 size_t len = os_strlen(ssid->passphrase);
1750 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1751 "on requested persistent group");
1752 os_memcpy(res->passphrase, ssid->passphrase, len);
1753 res->passphrase[len] = '\0';
1757 if (wpa_s->create_p2p_iface) {
1758 struct wpa_supplicant *group_wpa_s =
1759 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1760 if (group_wpa_s == NULL) {
1761 wpas_p2p_remove_pending_group_interface(wpa_s);
1762 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
1764 wpas_p2p_group_formation_failed(wpa_s);
1767 if (group_wpa_s != wpa_s) {
1768 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1769 sizeof(group_wpa_s->p2p_pin));
1770 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1772 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1773 wpa_s->pending_interface_name[0] = '\0';
1774 group_wpa_s->p2p_in_provisioning = 1;
1777 wpas_start_wps_go(group_wpa_s, res, 1);
1779 wpas_start_wps_enrollee(group_wpa_s, res);
1781 wpa_s->p2p_in_provisioning = 1;
1782 wpa_s->global->p2p_group_formation = wpa_s;
1785 wpas_start_wps_go(wpa_s, res, 1);
1787 wpas_start_wps_enrollee(ctx, res);
1790 wpa_s->p2p_long_listen = 0;
1791 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1793 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1794 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1795 (res->peer_config_timeout % 100) * 10000,
1796 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1800 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1802 struct wpa_supplicant *wpa_s = ctx;
1803 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1804 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1806 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1810 static void wpas_dev_found(void *ctx, const u8 *addr,
1811 const struct p2p_peer_info *info,
1814 #ifndef CONFIG_NO_STDOUT_DEBUG
1815 struct wpa_supplicant *wpa_s = ctx;
1816 char devtype[WPS_DEV_TYPE_BUFSIZE];
1817 char *wfd_dev_info_hex = NULL;
1819 #ifdef CONFIG_WIFI_DISPLAY
1820 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1821 WFD_SUBELEM_DEVICE_INFO);
1822 #endif /* CONFIG_WIFI_DISPLAY */
1824 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1825 " p2p_dev_addr=" MACSTR
1826 " pri_dev_type=%s name='%s' config_methods=0x%x "
1827 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
1828 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1829 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1831 info->device_name, info->config_methods,
1832 info->dev_capab, info->group_capab,
1833 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1834 wfd_dev_info_hex ? wfd_dev_info_hex : "",
1835 info->vendor_elems ? " vendor_elems=1" : "",
1838 os_free(wfd_dev_info_hex);
1839 #endif /* CONFIG_NO_STDOUT_DEBUG */
1841 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1845 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1847 struct wpa_supplicant *wpa_s = ctx;
1849 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1850 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1852 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1856 static void wpas_find_stopped(void *ctx)
1858 struct wpa_supplicant *wpa_s = ctx;
1859 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1863 struct wpas_p2p_listen_work {
1865 unsigned int duration;
1866 struct wpabuf *probe_resp_ie;
1870 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1874 wpabuf_free(lwork->probe_resp_ie);
1879 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1881 struct wpas_p2p_listen_work *lwork;
1883 if (!wpa_s->p2p_listen_work)
1886 lwork = wpa_s->p2p_listen_work->ctx;
1887 wpas_p2p_listen_work_free(lwork);
1888 radio_work_done(wpa_s->p2p_listen_work);
1889 wpa_s->p2p_listen_work = NULL;
1893 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1895 struct wpa_supplicant *wpa_s = work->wpa_s;
1896 struct wpas_p2p_listen_work *lwork = work->ctx;
1897 unsigned int duration;
1900 if (work->started) {
1901 wpa_s->p2p_listen_work = NULL;
1902 wpas_stop_listen(wpa_s);
1904 wpas_p2p_listen_work_free(lwork);
1908 wpa_s->p2p_listen_work = work;
1910 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1912 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1913 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1914 "report received Probe Request frames");
1915 wpas_p2p_listen_work_done(wpa_s);
1919 wpa_s->pending_listen_freq = lwork->freq;
1920 wpa_s->pending_listen_duration = lwork->duration;
1922 duration = lwork->duration;
1923 #ifdef CONFIG_TESTING_OPTIONS
1924 if (wpa_s->extra_roc_dur) {
1925 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
1926 duration, duration + wpa_s->extra_roc_dur);
1927 duration += wpa_s->extra_roc_dur;
1929 #endif /* CONFIG_TESTING_OPTIONS */
1931 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
1932 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1933 "to remain on channel (%u MHz) for Listen "
1934 "state", lwork->freq);
1935 wpas_p2p_listen_work_done(wpa_s);
1936 wpa_s->pending_listen_freq = 0;
1939 wpa_s->off_channel_freq = 0;
1940 wpa_s->roc_waiting_drv_freq = lwork->freq;
1944 static int wpas_start_listen(void *ctx, unsigned int freq,
1945 unsigned int duration,
1946 const struct wpabuf *probe_resp_ie)
1948 struct wpa_supplicant *wpa_s = ctx;
1949 struct wpas_p2p_listen_work *lwork;
1951 if (wpa_s->p2p_listen_work) {
1952 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
1956 lwork = os_zalloc(sizeof(*lwork));
1960 lwork->duration = duration;
1961 if (probe_resp_ie) {
1962 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1963 if (lwork->probe_resp_ie == NULL) {
1964 wpas_p2p_listen_work_free(lwork);
1969 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1971 wpas_p2p_listen_work_free(lwork);
1979 static void wpas_stop_listen(void *ctx)
1981 struct wpa_supplicant *wpa_s = ctx;
1982 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1983 wpa_drv_cancel_remain_on_channel(wpa_s);
1984 wpa_s->off_channel_freq = 0;
1985 wpa_s->roc_waiting_drv_freq = 0;
1987 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1988 wpa_drv_probe_req_report(wpa_s, 0);
1989 wpas_p2p_listen_work_done(wpa_s);
1993 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1995 struct wpa_supplicant *wpa_s = ctx;
1996 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
2001 * DNS Header section is used only to calculate compression pointers, so the
2002 * contents of this data does not matter, but the length needs to be reserved
2003 * in the virtual packet.
2005 #define DNS_HEADER_LEN 12
2008 * 27-octet in-memory packet from P2P specification containing two implied
2009 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
2011 #define P2P_SD_IN_MEMORY_LEN 27
2013 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
2014 u8 **spos, const u8 *end)
2016 while (*spos < end) {
2017 u8 val = ((*spos)[0] & 0xc0) >> 6;
2020 if (val == 1 || val == 2) {
2021 /* These are reserved values in RFC 1035 */
2022 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2023 "sequence starting with 0x%x", val);
2032 if (*spos + 2 > end) {
2033 wpa_printf(MSG_DEBUG, "P2P: No room for full "
2034 "DNS offset field");
2038 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
2039 if (offset >= *spos - start) {
2040 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
2041 "pointer offset %u", offset);
2046 spos_tmp = start + offset;
2047 return p2p_sd_dns_uncompress_label(upos, uend, start,
2053 len = (*spos)[0] & 0x3f;
2058 if (*spos + len > end) {
2059 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2060 "sequence - no room for label with length "
2065 if (*upos + len + 2 > uend)
2068 os_memcpy(*upos, *spos, len);
2080 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
2081 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
2082 * not large enough */
2083 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
2084 size_t msg_len, size_t offset)
2086 /* 27-octet in-memory packet from P2P specification */
2087 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
2088 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
2089 u8 *tmp, *end, *spos;
2095 if (offset > msg_len)
2098 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
2101 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
2102 end = spos + msg_len;
2105 os_memset(tmp, 0, DNS_HEADER_LEN);
2106 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
2107 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
2110 uend = buf + buf_len;
2112 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
2121 } else if (upos[-1] == '.')
2129 static struct p2p_srv_bonjour *
2130 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
2131 const struct wpabuf *query)
2133 struct p2p_srv_bonjour *bsrv;
2136 len = wpabuf_len(query);
2137 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2138 struct p2p_srv_bonjour, list) {
2139 if (len == wpabuf_len(bsrv->query) &&
2140 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
2148 static struct p2p_srv_upnp *
2149 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2150 const char *service)
2152 struct p2p_srv_upnp *usrv;
2154 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2155 struct p2p_srv_upnp, list) {
2156 if (version == usrv->version &&
2157 os_strcmp(service, usrv->service) == 0)
2164 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2169 if (wpabuf_tailroom(resp) < 5)
2172 /* Length (to be filled) */
2173 len_pos = wpabuf_put(resp, 2);
2174 wpabuf_put_u8(resp, srv_proto);
2175 wpabuf_put_u8(resp, srv_trans_id);
2177 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2178 /* Response Data: empty */
2179 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2183 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2184 struct wpabuf *resp, u8 srv_trans_id)
2186 struct p2p_srv_bonjour *bsrv;
2189 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2191 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2192 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2196 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2197 struct p2p_srv_bonjour, list) {
2198 if (wpabuf_tailroom(resp) <
2199 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2201 /* Length (to be filled) */
2202 len_pos = wpabuf_put(resp, 2);
2203 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2204 wpabuf_put_u8(resp, srv_trans_id);
2206 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2207 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2208 wpabuf_head(bsrv->resp),
2209 wpabuf_len(bsrv->resp));
2211 wpabuf_put_buf(resp, bsrv->query); /* Key */
2212 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2213 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2219 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2222 char str_rx[256], str_srv[256];
2224 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2225 return 0; /* Too short to include DNS Type and Version */
2226 if (os_memcmp(query + query_len - 3,
2227 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2229 return 0; /* Mismatch in DNS Type or Version */
2230 if (query_len == wpabuf_len(bsrv->query) &&
2231 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2232 return 1; /* Binary match */
2234 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2236 return 0; /* Failed to uncompress query */
2237 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2238 wpabuf_head(bsrv->query),
2239 wpabuf_len(bsrv->query) - 3, 0))
2240 return 0; /* Failed to uncompress service */
2242 return os_strcmp(str_rx, str_srv) == 0;
2246 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2247 struct wpabuf *resp, u8 srv_trans_id,
2248 const u8 *query, size_t query_len)
2250 struct p2p_srv_bonjour *bsrv;
2254 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2256 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2257 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2258 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2263 if (query_len == 0) {
2264 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2268 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2269 struct p2p_srv_bonjour, list) {
2270 if (!match_bonjour_query(bsrv, query, query_len))
2273 if (wpabuf_tailroom(resp) <
2274 5 + query_len + wpabuf_len(bsrv->resp))
2279 /* Length (to be filled) */
2280 len_pos = wpabuf_put(resp, 2);
2281 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2282 wpabuf_put_u8(resp, srv_trans_id);
2285 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2286 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2287 wpabuf_head(bsrv->resp),
2288 wpabuf_len(bsrv->resp));
2291 wpabuf_put_data(resp, query, query_len); /* Key */
2292 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2294 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2298 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2300 if (wpabuf_tailroom(resp) < 5)
2303 /* Length (to be filled) */
2304 len_pos = wpabuf_put(resp, 2);
2305 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2306 wpabuf_put_u8(resp, srv_trans_id);
2309 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2310 /* Response Data: empty */
2311 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2317 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2318 struct wpabuf *resp, u8 srv_trans_id)
2320 struct p2p_srv_upnp *usrv;
2323 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2325 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2326 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2330 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2331 struct p2p_srv_upnp, list) {
2332 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2335 /* Length (to be filled) */
2336 len_pos = wpabuf_put(resp, 2);
2337 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2338 wpabuf_put_u8(resp, srv_trans_id);
2341 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2343 wpabuf_put_u8(resp, usrv->version);
2344 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2346 wpabuf_put_str(resp, usrv->service);
2347 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2353 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2354 struct wpabuf *resp, u8 srv_trans_id,
2355 const u8 *query, size_t query_len)
2357 struct p2p_srv_upnp *usrv;
2363 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2366 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2367 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2368 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2373 if (query_len == 0) {
2374 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2378 if (wpabuf_tailroom(resp) < 5)
2381 /* Length (to be filled) */
2382 len_pos = wpabuf_put(resp, 2);
2383 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2384 wpabuf_put_u8(resp, srv_trans_id);
2387 str = os_malloc(query_len);
2390 os_memcpy(str, query + 1, query_len - 1);
2391 str[query_len - 1] = '\0';
2393 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2394 struct p2p_srv_upnp, list) {
2395 if (version != usrv->version)
2398 if (os_strcmp(str, "ssdp:all") != 0 &&
2399 os_strstr(usrv->service, str) == NULL)
2402 if (wpabuf_tailroom(resp) < 2)
2406 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2408 wpabuf_put_u8(resp, version);
2410 wpabuf_put_u8(resp, ',');
2414 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2416 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2418 wpabuf_put_str(resp, usrv->service);
2423 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2426 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2427 /* Response Data: empty */
2430 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2434 #ifdef CONFIG_WIFI_DISPLAY
2435 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2436 struct wpabuf *resp, u8 srv_trans_id,
2437 const u8 *query, size_t query_len)
2443 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2445 if (!wpa_s->global->wifi_display) {
2446 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2447 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2452 if (query_len < 1) {
2453 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2458 if (wpabuf_tailroom(resp) < 5)
2463 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2465 /* TODO: role specific handling */
2467 /* Length (to be filled) */
2468 len_pos = wpabuf_put(resp, 2);
2469 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2470 wpabuf_put_u8(resp, srv_trans_id);
2471 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2473 while (pos < query + query_len) {
2474 if (*pos < MAX_WFD_SUBELEMS &&
2475 wpa_s->global->wfd_subelem[*pos] &&
2476 wpabuf_tailroom(resp) >=
2477 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2478 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2479 "subelement %u", *pos);
2480 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2485 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2487 #endif /* CONFIG_WIFI_DISPLAY */
2490 static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2491 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2493 struct wpa_supplicant *wpa_s = ctx;
2494 const u8 *pos = tlvs;
2495 const u8 *end = tlvs + tlvs_len;
2498 struct wpabuf *resp;
2499 u8 srv_proto, srv_trans_id;
2503 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2505 buf_len = 2 * tlvs_len + 1;
2506 buf = os_malloc(buf_len);
2508 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2509 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2511 freq, MAC2STR(sa), dialog_token, update_indic,
2516 if (wpa_s->p2p_sd_over_ctrl_iface) {
2517 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2518 update_indic, tlvs, tlvs_len);
2519 return; /* to be processed by an external program */
2522 resp = wpabuf_alloc(10000);
2526 while (pos + 1 < end) {
2527 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2528 slen = WPA_GET_LE16(pos);
2530 if (pos + slen > end || slen < 2) {
2531 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2536 tlv_end = pos + slen;
2539 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2541 srv_trans_id = *pos++;
2542 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2545 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2546 pos, tlv_end - pos);
2549 if (wpa_s->force_long_sd) {
2550 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2552 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2553 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2557 switch (srv_proto) {
2558 case P2P_SERV_ALL_SERVICES:
2559 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2560 "for all services");
2561 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2562 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2563 wpa_printf(MSG_DEBUG, "P2P: No service "
2564 "discovery protocols available");
2565 wpas_sd_add_proto_not_avail(
2566 resp, P2P_SERV_ALL_SERVICES,
2570 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2571 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2573 case P2P_SERV_BONJOUR:
2574 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2575 pos, tlv_end - pos);
2578 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2579 pos, tlv_end - pos);
2581 #ifdef CONFIG_WIFI_DISPLAY
2582 case P2P_SERV_WIFI_DISPLAY:
2583 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2584 pos, tlv_end - pos);
2586 #endif /* CONFIG_WIFI_DISPLAY */
2588 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2589 "protocol %u", srv_proto);
2590 wpas_sd_add_proto_not_avail(resp, srv_proto,
2599 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2600 update_indic, tlvs, tlvs_len);
2602 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2608 static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2609 const u8 *tlvs, size_t tlvs_len)
2611 struct wpa_supplicant *wpa_s = ctx;
2612 const u8 *pos = tlvs;
2613 const u8 *end = tlvs + tlvs_len;
2619 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2621 if (tlvs_len > 1500) {
2622 /* TODO: better way for handling this */
2623 wpa_msg_ctrl(wpa_s, MSG_INFO,
2624 P2P_EVENT_SERV_DISC_RESP MACSTR
2625 " %u <long response: %u bytes>",
2626 MAC2STR(sa), update_indic,
2627 (unsigned int) tlvs_len);
2629 buf_len = 2 * tlvs_len + 1;
2630 buf = os_malloc(buf_len);
2632 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2633 wpa_msg_ctrl(wpa_s, MSG_INFO,
2634 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2635 MAC2STR(sa), update_indic, buf);
2641 u8 srv_proto, srv_trans_id, status;
2643 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2644 slen = WPA_GET_LE16(pos);
2646 if (pos + slen > end || slen < 3) {
2647 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2651 tlv_end = pos + slen;
2654 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2656 srv_trans_id = *pos++;
2657 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2660 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2663 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2664 pos, tlv_end - pos);
2669 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2673 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2674 const struct wpabuf *tlvs)
2676 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2678 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2682 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2683 u8 version, const char *query)
2685 struct wpabuf *tlvs;
2688 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2691 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2692 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2693 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2694 wpabuf_put_u8(tlvs, version);
2695 wpabuf_put_str(tlvs, query);
2696 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2702 #ifdef CONFIG_WIFI_DISPLAY
2704 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2705 const struct wpabuf *tlvs)
2707 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2709 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2713 #define MAX_WFD_SD_SUBELEMS 20
2715 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2716 const char *subelems)
2723 len = wpabuf_put(tlvs, 2);
2724 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2725 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2727 wpabuf_put_u8(tlvs, role);
2732 if (val >= 0 && val < 256) {
2733 wpabuf_put_u8(tlvs, val);
2735 if (count == MAX_WFD_SD_SUBELEMS)
2738 pos = os_strchr(pos + 1, ',');
2744 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2748 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2749 const u8 *dst, const char *role)
2751 struct wpabuf *tlvs;
2753 const char *subelems;
2756 subelems = os_strchr(role, ' ');
2757 if (subelems == NULL)
2761 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2765 if (os_strstr(role, "[source]"))
2766 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2767 if (os_strstr(role, "[pri-sink]"))
2768 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2769 if (os_strstr(role, "[sec-sink]"))
2770 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2771 if (os_strstr(role, "[source+sink]"))
2772 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2774 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2779 #endif /* CONFIG_WIFI_DISPLAY */
2782 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2784 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2786 return p2p_sd_cancel_request(wpa_s->global->p2p,
2787 (void *) (uintptr_t) req);
2791 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2792 const u8 *dst, u8 dialog_token,
2793 const struct wpabuf *resp_tlvs)
2795 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2797 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2802 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2804 if (wpa_s->global->p2p)
2805 p2p_sd_service_update(wpa_s->global->p2p);
2809 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2811 dl_list_del(&bsrv->list);
2812 wpabuf_free(bsrv->query);
2813 wpabuf_free(bsrv->resp);
2818 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2820 dl_list_del(&usrv->list);
2821 os_free(usrv->service);
2826 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2828 struct p2p_srv_bonjour *bsrv, *bn;
2829 struct p2p_srv_upnp *usrv, *un;
2831 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2832 struct p2p_srv_bonjour, list)
2833 wpas_p2p_srv_bonjour_free(bsrv);
2835 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2836 struct p2p_srv_upnp, list)
2837 wpas_p2p_srv_upnp_free(usrv);
2839 wpas_p2p_sd_service_update(wpa_s);
2843 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2844 struct wpabuf *query, struct wpabuf *resp)
2846 struct p2p_srv_bonjour *bsrv;
2848 bsrv = os_zalloc(sizeof(*bsrv));
2851 bsrv->query = query;
2853 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2855 wpas_p2p_sd_service_update(wpa_s);
2860 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2861 const struct wpabuf *query)
2863 struct p2p_srv_bonjour *bsrv;
2865 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2868 wpas_p2p_srv_bonjour_free(bsrv);
2869 wpas_p2p_sd_service_update(wpa_s);
2874 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2875 const char *service)
2877 struct p2p_srv_upnp *usrv;
2879 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2880 return 0; /* Already listed */
2881 usrv = os_zalloc(sizeof(*usrv));
2884 usrv->version = version;
2885 usrv->service = os_strdup(service);
2886 if (usrv->service == NULL) {
2890 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2892 wpas_p2p_sd_service_update(wpa_s);
2897 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2898 const char *service)
2900 struct p2p_srv_upnp *usrv;
2902 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2905 wpas_p2p_srv_upnp_free(usrv);
2906 wpas_p2p_sd_service_update(wpa_s);
2911 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2912 const u8 *peer, const char *params,
2913 unsigned int generated_pin)
2915 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2916 " %08d%s", MAC2STR(peer), generated_pin, params);
2920 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2921 const u8 *peer, const char *params)
2923 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2924 "%s", MAC2STR(peer), params);
2928 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2929 const u8 *dev_addr, const u8 *pri_dev_type,
2930 const char *dev_name, u16 supp_config_methods,
2931 u8 dev_capab, u8 group_capab, const u8 *group_id,
2932 size_t group_id_len)
2934 struct wpa_supplicant *wpa_s = ctx;
2935 char devtype[WPS_DEV_TYPE_BUFSIZE];
2937 u8 empty_dev_type[8];
2938 unsigned int generated_pin = 0;
2939 struct wpa_supplicant *group = NULL;
2942 for (group = wpa_s->global->ifaces; group; group = group->next)
2944 struct wpa_ssid *s = group->current_ssid;
2946 s->mode == WPAS_MODE_P2P_GO &&
2947 group_id_len - ETH_ALEN == s->ssid_len &&
2948 os_memcmp(group_id + ETH_ALEN, s->ssid,
2954 if (pri_dev_type == NULL) {
2955 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2956 pri_dev_type = empty_dev_type;
2958 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2959 " pri_dev_type=%s name='%s' config_methods=0x%x "
2960 "dev_capab=0x%x group_capab=0x%x%s%s",
2962 wps_dev_type_bin2str(pri_dev_type, devtype,
2964 dev_name, supp_config_methods, dev_capab, group_capab,
2965 group ? " group=" : "",
2966 group ? group->ifname : "");
2967 params[sizeof(params) - 1] = '\0';
2969 if (config_methods & WPS_CONFIG_DISPLAY) {
2970 generated_pin = wps_generate_pin();
2971 wpas_prov_disc_local_display(wpa_s, peer, params,
2973 } else if (config_methods & WPS_CONFIG_KEYPAD)
2974 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2975 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2976 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2977 MACSTR "%s", MAC2STR(peer), params);
2979 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2980 P2P_PROV_DISC_SUCCESS,
2981 config_methods, generated_pin);
2985 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2987 struct wpa_supplicant *wpa_s = ctx;
2988 unsigned int generated_pin = 0;
2991 if (wpa_s->pending_pd_before_join &&
2992 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2993 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2994 wpa_s->pending_pd_before_join = 0;
2995 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2996 "join-existing-group operation");
2997 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
3001 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
3002 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
3003 os_snprintf(params, sizeof(params), " peer_go=%d",
3004 wpa_s->pending_pd_use == AUTO_PD_JOIN);
3008 if (config_methods & WPS_CONFIG_DISPLAY)
3009 wpas_prov_disc_local_keypad(wpa_s, peer, params);
3010 else if (config_methods & WPS_CONFIG_KEYPAD) {
3011 generated_pin = wps_generate_pin();
3012 wpas_prov_disc_local_display(wpa_s, peer, params,
3014 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
3015 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
3016 MACSTR "%s", MAC2STR(peer), params);
3018 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3019 P2P_PROV_DISC_SUCCESS,
3020 config_methods, generated_pin);
3024 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
3025 enum p2p_prov_disc_status status)
3027 struct wpa_supplicant *wpa_s = ctx;
3029 if (wpa_s->p2p_fallback_to_go_neg) {
3030 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
3031 "failed - fall back to GO Negotiation");
3032 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
3036 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
3037 wpa_s->pending_pd_before_join = 0;
3038 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3039 "join-existing-group operation (no ACK for PD "
3041 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
3045 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3046 " p2p_dev_addr=" MACSTR " status=%d",
3047 MAC2STR(peer), status);
3049 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3054 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
3056 if (channels == NULL)
3057 return 1; /* Assume no restrictions */
3058 return p2p_channels_includes_freq(channels, freq);
3064 * Pick the best frequency to use from all the currently used frequencies.
3066 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3067 struct wpa_used_freq_data *freqs,
3072 /* find a candidate freq that is supported by P2P */
3073 for (c = 0; c < num; c++)
3074 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3080 /* once we have a candidate, try to find a 'better' one */
3081 for (i = c + 1; i < num; i++) {
3082 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3086 * 1. Infrastructure station interfaces have higher preference.
3087 * 2. P2P Clients have higher preference.
3090 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3095 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3098 return freqs[c].freq;
3102 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3103 const u8 *go_dev_addr, const u8 *ssid,
3104 size_t ssid_len, int *go, u8 *group_bssid,
3105 int *force_freq, int persistent_group,
3106 const struct p2p_channels *channels,
3109 struct wpa_supplicant *wpa_s = ctx;
3111 struct wpa_used_freq_data *freqs;
3112 struct wpa_supplicant *grp;
3115 if (!persistent_group) {
3116 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3117 " to join an active group (SSID: %s)",
3118 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
3119 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3120 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3122 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3123 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3124 "authorized invitation");
3128 #ifdef CONFIG_WPS_NFC
3129 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
3130 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
3131 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
3132 wpa_s->parent->p2p_wps_method = WPS_NFC;
3133 wpa_s->parent->pending_join_wps_method = WPS_NFC;
3134 os_memcpy(wpa_s->parent->pending_join_dev_addr,
3135 go_dev_addr, ETH_ALEN);
3136 os_memcpy(wpa_s->parent->pending_join_iface_addr,
3140 #endif /* CONFIG_WPS_NFC */
3143 * Do not accept the invitation automatically; notify user and
3146 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3149 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3151 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3152 "running persistent group");
3154 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3158 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3159 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3160 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3161 "invitation to re-invoke a persistent group");
3162 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3163 } else if (!wpa_s->conf->persistent_reconnect)
3164 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3166 for (s = wpa_s->conf->ssid; s; s = s->next) {
3167 if (s->disabled == 2 &&
3168 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3169 s->ssid_len == ssid_len &&
3170 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3175 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3176 " requested reinvocation of an unknown group",
3178 return P2P_SC_FAIL_UNKNOWN_GROUP;
3181 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3183 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3184 wpa_printf(MSG_DEBUG, "P2P: The only available "
3185 "interface is already in use - reject "
3187 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3189 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3190 } else if (s->mode == WPAS_MODE_P2P_GO) {
3192 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3194 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3195 "interface address for the group");
3196 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3198 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3203 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3206 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3207 sizeof(struct wpa_used_freq_data));
3209 int num_channels = wpa_s->num_multichan_concurrent;
3210 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3211 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3215 /* Get one of the frequencies currently in use */
3216 if (best_freq > 0) {
3217 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3218 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
3220 if (wpa_s->num_multichan_concurrent < 2 ||
3221 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3222 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3223 *force_freq = best_freq;
3227 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3228 wpas_p2p_num_unused_channels(wpa_s) > 0) {
3230 /* We are the client */
3231 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3232 "running a GO but we are capable of MCC, "
3233 "figure out the best channel to use");
3235 } else if (!freq_included(channels, *force_freq)) {
3236 /* We are the GO, and *force_freq is not in the
3238 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3239 "in intersection but we are capable of MCC, "
3240 "figure out the best channel to use",
3246 return P2P_SC_SUCCESS;
3250 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3251 const u8 *ssid, size_t ssid_len,
3252 const u8 *go_dev_addr, u8 status,
3255 struct wpa_supplicant *wpa_s = ctx;
3258 for (s = wpa_s->conf->ssid; s; s = s->next) {
3259 if (s->disabled == 2 &&
3260 s->ssid_len == ssid_len &&
3261 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3265 if (status == P2P_SC_SUCCESS) {
3266 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3267 " was accepted; op_freq=%d MHz, SSID=%s",
3268 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3270 int go = s->mode == WPAS_MODE_P2P_GO;
3271 wpas_p2p_group_add_persistent(
3272 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
3273 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3275 wpa_s->user_initiated_pd = 0;
3276 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
3277 wpa_s->p2p_wps_method, 0, op_freq,
3283 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3284 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3285 " was rejected (status %u)", MAC2STR(sa), status);
3291 wpa_msg_global(wpa_s, MSG_INFO,
3292 P2P_EVENT_INVITATION_RECEIVED
3293 "sa=" MACSTR " go_dev_addr=" MACSTR
3294 " bssid=" MACSTR " unknown-network",
3295 MAC2STR(sa), MAC2STR(go_dev_addr),
3298 wpa_msg_global(wpa_s, MSG_INFO,
3299 P2P_EVENT_INVITATION_RECEIVED
3300 "sa=" MACSTR " go_dev_addr=" MACSTR
3302 MAC2STR(sa), MAC2STR(go_dev_addr));
3307 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
3308 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3309 "sa=" MACSTR " persistent=%d freq=%d",
3310 MAC2STR(sa), s->id, op_freq);
3312 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3313 "sa=" MACSTR " persistent=%d",
3314 MAC2STR(sa), s->id);
3319 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3320 struct wpa_ssid *ssid,
3321 const u8 *peer, int inv)
3328 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3329 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3333 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
3334 if (ssid->mode != WPAS_MODE_P2P_GO &&
3335 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3336 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3337 "due to invitation result", ssid->id);
3338 wpas_notify_network_removed(wpa_s, ssid);
3339 wpa_config_remove_network(wpa_s->conf, ssid->id);
3342 return; /* Peer not found in client list */
3345 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
3346 "group %d client list%s",
3347 MAC2STR(peer), ssid->id,
3348 inv ? " due to invitation result" : "");
3349 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3350 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3351 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3352 ssid->num_p2p_clients--;
3353 if (wpa_s->parent->conf->update_config &&
3354 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3355 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3359 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3362 struct wpa_ssid *ssid;
3364 wpa_s = wpa_s->global->p2p_invite_group;
3366 return; /* No known invitation group */
3367 ssid = wpa_s->current_ssid;
3368 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3369 !ssid->p2p_persistent_group)
3370 return; /* Not operating as a GO in persistent group */
3371 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3372 ssid->ssid, ssid->ssid_len);
3373 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3377 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
3378 const struct p2p_channels *channels,
3379 const u8 *peer, int neg_freq,
3382 struct wpa_supplicant *wpa_s = ctx;
3383 struct wpa_ssid *ssid;
3387 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3388 "status=%d " MACSTR,
3389 status, MAC2STR(bssid));
3391 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3392 "status=%d ", status);
3394 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3396 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3397 status, MAC2STR(peer));
3398 if (wpa_s->pending_invite_ssid_id == -1) {
3399 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3400 wpas_remove_persistent_client(wpa_s, peer);
3401 return; /* Invitation to active group */
3404 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3405 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3406 "invitation exchange to indicate readiness for "
3410 if (status != P2P_SC_SUCCESS) {
3411 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3412 ssid = wpa_config_get_network(
3413 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3414 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3416 wpas_p2p_remove_pending_group_interface(wpa_s);
3420 ssid = wpa_config_get_network(wpa_s->conf,
3421 wpa_s->pending_invite_ssid_id);
3423 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3424 "data matching with invitation");
3429 * The peer could have missed our ctrl::ack frame for Invitation
3430 * Response and continue retransmitting the frame. To reduce the
3431 * likelihood of the peer not getting successful TX status for the
3432 * Invitation Response frame, wait a short time here before starting
3433 * the persistent group so that we will remain on the current channel to
3434 * acknowledge any possible retransmission from the peer.
3436 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3437 "starting persistent group");
3440 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3441 freq_included(channels, neg_freq))
3443 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3444 freq_included(channels, peer_oper_freq))
3445 freq = peer_oper_freq;
3449 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3450 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3451 wpas_p2p_group_add_persistent(wpa_s, ssid,
3452 ssid->mode == WPAS_MODE_P2P_GO,
3453 wpa_s->p2p_persistent_go_freq,
3455 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3457 ssid->mode == WPAS_MODE_P2P_GO ?
3458 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3463 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3466 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3468 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3472 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3474 reg->channel[reg->channels] = chan;
3479 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3480 struct p2p_channels *chan,
3481 struct p2p_channels *cli_chan)
3485 wpa_s->global->p2p_24ghz_social_channels = 1;
3487 os_memset(cli_chan, 0, sizeof(*cli_chan));
3489 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3492 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3493 chan->reg_class[cla].reg_class = 81;
3494 chan->reg_class[cla].channels = 0;
3495 for (i = 0; i < 11; i++) {
3496 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3497 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3499 if (chan->reg_class[cla].channels)
3502 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3505 /* Operating class 115 - 5 GHz, channels 36-48 */
3506 chan->reg_class[cla].reg_class = 115;
3507 chan->reg_class[cla].channels = 0;
3508 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3509 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3510 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3511 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3512 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3513 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3514 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3515 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3516 if (chan->reg_class[cla].channels)
3519 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3522 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3523 chan->reg_class[cla].reg_class = 124;
3524 chan->reg_class[cla].channels = 0;
3525 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3526 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3527 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3528 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3529 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3530 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3531 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3532 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3533 if (chan->reg_class[cla].channels)
3536 chan->reg_classes = cla;
3541 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3543 enum hostapd_hw_mode mode)
3547 for (i = 0; i < num_modes; i++) {
3548 if (modes[i].mode == mode)
3557 NOT_ALLOWED, NO_IR, ALLOWED
3560 static int has_channel(struct wpa_global *global,
3561 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3566 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3568 if (wpas_p2p_disallowed_freq(global, freq))
3571 for (i = 0; i < mode->num_channels; i++) {
3572 if (mode->channels[i].chan == chan) {
3574 *flags = mode->channels[i].flag;
3575 if (mode->channels[i].flag &
3576 (HOSTAPD_CHAN_DISABLED |
3577 HOSTAPD_CHAN_RADAR))
3579 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3589 struct p2p_oper_class_map {
3590 enum hostapd_hw_mode mode;
3595 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
3598 static struct p2p_oper_class_map op_class[] = {
3599 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3600 #if 0 /* Do not enable HT40 on 2 GHz for now */
3601 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3602 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3604 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3605 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3606 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3607 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3608 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3609 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3612 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3613 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3614 * 80 MHz, but currently use the following definition for simplicity
3615 * (these center frequencies are not actual channels, which makes
3616 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3617 * removing invalid channels.
3619 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3620 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
3621 { -1, 0, 0, 0, 0, BW20 }
3625 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3626 struct hostapd_hw_modes *mode,
3629 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3632 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3635 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3637 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3638 * so the center channel is 6 channels away from the start/end.
3640 if (channel >= center_channels[i] - 6 &&
3641 channel <= center_channels[i] + 6)
3642 return center_channels[i];
3648 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3649 struct hostapd_hw_modes *mode,
3654 enum chan_allowed res, ret = ALLOWED;
3656 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3659 if (center_chan >= 58 && center_chan <= 138)
3660 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3662 /* check all the channels are available */
3663 for (i = 0; i < 4; i++) {
3664 int adj_chan = center_chan - 6 + i * 4;
3666 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3667 if (res == NOT_ALLOWED)
3672 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3674 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3676 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3678 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3686 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3687 struct hostapd_hw_modes *mode,
3691 enum chan_allowed res, res2;
3693 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3694 if (bw == BW40MINUS) {
3695 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3697 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3698 } else if (bw == BW40PLUS) {
3699 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3701 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3702 } else if (bw == BW80) {
3703 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3706 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3708 if (res == NO_IR || res2 == NO_IR)
3714 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3715 struct p2p_channels *chan,
3716 struct p2p_channels *cli_chan)
3718 struct hostapd_hw_modes *mode;
3719 int cla, op, cli_cla;
3721 if (wpa_s->hw.modes == NULL) {
3722 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3723 "of all supported channels; assume dualband "
3725 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3730 for (op = 0; op_class[op].op_class; op++) {
3731 struct p2p_oper_class_map *o = &op_class[op];
3733 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3735 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3738 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3739 wpa_s->global->p2p_24ghz_social_channels = 1;
3740 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3741 enum chan_allowed res;
3742 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3743 if (res == ALLOWED) {
3745 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3747 reg = &chan->reg_class[cla];
3749 reg->reg_class = o->op_class;
3751 reg->channel[reg->channels] = ch;
3753 } else if (res == NO_IR &&
3754 wpa_s->conf->p2p_add_cli_chan) {
3755 if (cli_reg == NULL) {
3756 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3758 cli_reg = &cli_chan->reg_class[cli_cla];
3760 cli_reg->reg_class = o->op_class;
3762 cli_reg->channel[cli_reg->channels] = ch;
3763 cli_reg->channels++;
3767 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3768 reg->channel, reg->channels);
3771 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3772 cli_reg->channel, cli_reg->channels);
3776 chan->reg_classes = cla;
3777 cli_chan->reg_classes = cli_cla;
3783 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3784 struct hostapd_hw_modes *mode, u8 channel)
3787 enum chan_allowed ret;
3789 for (op = 0; op_class[op].op_class; op++) {
3790 struct p2p_oper_class_map *o = &op_class[op];
3793 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3794 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3795 o->bw == BW20 || ch != channel)
3797 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3799 return (o->bw == BW40MINUS) ? -1 : 1;
3806 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3807 struct hostapd_hw_modes *mode, u8 channel)
3809 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3812 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3816 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3819 struct wpa_supplicant *wpa_s = ctx;
3821 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3822 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3828 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3832 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3833 const u8 *ssid, size_t ssid_len)
3835 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3836 struct wpa_ssid *s = wpa_s->current_ssid;
3839 if (s->mode != WPAS_MODE_P2P_GO &&
3840 s->mode != WPAS_MODE_AP &&
3841 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3843 if (s->ssid_len != ssid_len ||
3844 os_memcmp(ssid, s->ssid, ssid_len) != 0)
3854 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3855 const u8 *peer_dev_addr)
3857 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3858 struct wpa_ssid *ssid = wpa_s->current_ssid;
3861 if (ssid->mode != WPAS_MODE_INFRA)
3863 if (wpa_s->wpa_state != WPA_COMPLETED &&
3864 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3866 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3874 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3876 struct wpa_supplicant *wpa_s = ctx;
3878 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3882 static int wpas_is_concurrent_session_active(void *ctx)
3884 struct wpa_supplicant *wpa_s = ctx;
3885 struct wpa_supplicant *ifs;
3887 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3890 if (ifs->wpa_state > WPA_ASSOCIATED)
3897 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3899 struct wpa_supplicant *wpa_s = ctx;
3900 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3904 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3905 const char *conf_p2p_dev)
3907 struct wpa_interface iface;
3908 struct wpa_supplicant *p2pdev_wpa_s;
3910 char force_name[100];
3913 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3915 force_name[0] = '\0';
3916 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3917 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3918 force_name, wpa_s->pending_interface_addr, NULL);
3920 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3923 os_strlcpy(wpa_s->pending_interface_name, ifname,
3924 sizeof(wpa_s->pending_interface_name));
3926 os_memset(&iface, 0, sizeof(iface));
3928 iface.ifname = wpa_s->pending_interface_name;
3929 iface.driver = wpa_s->driver->name;
3930 iface.driver_param = wpa_s->conf->driver_param;
3933 * If a P2P Device configuration file was given, use it as the interface
3934 * configuration file (instead of using parent's configuration file.
3937 iface.confname = conf_p2p_dev;
3938 iface.ctrl_interface = NULL;
3940 iface.confname = wpa_s->confname;
3941 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3943 iface.conf_p2p_dev = NULL;
3945 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3946 if (!p2pdev_wpa_s) {
3947 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3950 p2pdev_wpa_s->parent = wpa_s;
3952 wpa_s->pending_interface_name[0] = '\0';
3957 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3958 const u8 *noa, size_t noa_len)
3960 struct wpa_supplicant *wpa_s, *intf = ctx;
3963 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3964 if (wpa_s->waiting_presence_resp)
3968 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3971 wpa_s->waiting_presence_resp = 0;
3973 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3974 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3975 " status=%u noa=%s", MAC2STR(src), status, hex);
3979 static int _wpas_p2p_in_progress(void *ctx)
3981 struct wpa_supplicant *wpa_s = ctx;
3982 return wpas_p2p_in_progress(wpa_s);
3987 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3988 * @global: Pointer to global data from wpa_supplicant_init()
3989 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3990 * Returns: 0 on success, -1 on failure
3992 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3994 struct p2p_config p2p;
3997 if (wpa_s->conf->p2p_disabled)
4000 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4006 os_memset(&p2p, 0, sizeof(p2p));
4008 p2p.debug_print = wpas_p2p_debug_print;
4009 p2p.p2p_scan = wpas_p2p_scan;
4010 p2p.send_action = wpas_send_action;
4011 p2p.send_action_done = wpas_send_action_done;
4012 p2p.go_neg_completed = wpas_go_neg_completed;
4013 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4014 p2p.dev_found = wpas_dev_found;
4015 p2p.dev_lost = wpas_dev_lost;
4016 p2p.find_stopped = wpas_find_stopped;
4017 p2p.start_listen = wpas_start_listen;
4018 p2p.stop_listen = wpas_stop_listen;
4019 p2p.send_probe_resp = wpas_send_probe_resp;
4020 p2p.sd_request = wpas_sd_request;
4021 p2p.sd_response = wpas_sd_response;
4022 p2p.prov_disc_req = wpas_prov_disc_req;
4023 p2p.prov_disc_resp = wpas_prov_disc_resp;
4024 p2p.prov_disc_fail = wpas_prov_disc_fail;
4025 p2p.invitation_process = wpas_invitation_process;
4026 p2p.invitation_received = wpas_invitation_received;
4027 p2p.invitation_result = wpas_invitation_result;
4028 p2p.get_noa = wpas_get_noa;
4029 p2p.go_connected = wpas_go_connected;
4030 p2p.presence_resp = wpas_presence_resp;
4031 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
4032 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
4034 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
4035 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
4036 p2p.dev_name = wpa_s->conf->device_name;
4037 p2p.manufacturer = wpa_s->conf->manufacturer;
4038 p2p.model_name = wpa_s->conf->model_name;
4039 p2p.model_number = wpa_s->conf->model_number;
4040 p2p.serial_number = wpa_s->conf->serial_number;
4042 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4043 p2p.config_methods = wpa_s->wps->config_methods;
4046 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4047 wpa_printf(MSG_ERROR,
4048 "P2P: Failed to configure supported channel list");
4052 if (wpa_s->conf->p2p_listen_reg_class &&
4053 wpa_s->conf->p2p_listen_channel) {
4054 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4055 p2p.channel = wpa_s->conf->p2p_listen_channel;
4056 p2p.channel_forced = 1;
4059 * Pick one of the social channels randomly as the listen
4062 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4063 &p2p.channel) != 0) {
4064 wpa_printf(MSG_ERROR,
4065 "P2P: Failed to select random social channel as listen channel");
4068 p2p.channel_forced = 0;
4070 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4071 p2p.reg_class, p2p.channel);
4073 if (wpa_s->conf->p2p_oper_reg_class &&
4074 wpa_s->conf->p2p_oper_channel) {
4075 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4076 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4077 p2p.cfg_op_channel = 1;
4078 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4079 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4083 * Use random operation channel from 2.4 GHz band social
4084 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4085 * other preference is indicated.
4087 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4088 &p2p.op_channel) != 0) {
4089 wpa_printf(MSG_ERROR,
4090 "P2P: Failed to select random social channel as operation channel");
4093 p2p.cfg_op_channel = 0;
4094 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4095 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4098 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4099 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4100 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4103 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4104 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4105 p2p.country[2] = 0x04;
4107 os_memcpy(p2p.country, "XX\x04", 3);
4109 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4112 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4113 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4114 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4116 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4117 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4119 p2p.max_peers = 100;
4121 if (wpa_s->conf->p2p_ssid_postfix) {
4122 p2p.ssid_postfix_len =
4123 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4124 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4125 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4126 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4127 p2p.ssid_postfix_len);
4130 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4132 p2p.max_listen = wpa_s->max_remain_on_chan;
4134 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4135 wpa_s->conf->p2p_passphrase_len <= 63)
4136 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4138 p2p.passphrase_len = 8;
4140 global->p2p = p2p_init(&p2p);
4141 if (global->p2p == NULL)
4143 global->p2p_init_wpa_s = wpa_s;
4145 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4146 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4148 p2p_add_wps_vendor_extension(
4149 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4152 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4159 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4160 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4162 * This function deinitialize per-interface P2P data.
4164 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4166 if (wpa_s->driver && wpa_s->drv_priv)
4167 wpa_drv_probe_req_report(wpa_s, 0);
4169 if (wpa_s->go_params) {
4170 /* Clear any stored provisioning info */
4171 p2p_clear_provisioning_info(
4173 wpa_s->go_params->peer_device_addr);
4176 os_free(wpa_s->go_params);
4177 wpa_s->go_params = NULL;
4178 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4179 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4180 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4181 wpa_s->p2p_long_listen = 0;
4182 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4183 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4184 wpas_p2p_remove_pending_group_interface(wpa_s);
4185 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4186 wpas_p2p_listen_work_done(wpa_s);
4187 if (wpa_s->p2p_send_action_work) {
4188 os_free(wpa_s->p2p_send_action_work->ctx);
4189 radio_work_done(wpa_s->p2p_send_action_work);
4190 wpa_s->p2p_send_action_work = NULL;
4192 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4194 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4195 wpa_s->p2p_oob_dev_pw = NULL;
4197 os_free(wpa_s->p2p_group_common_freqs);
4198 wpa_s->p2p_group_common_freqs = NULL;
4199 wpa_s->p2p_group_common_freqs_num = 0;
4201 /* TODO: remove group interface from the driver if this wpa_s instance
4202 * is on top of a P2P group interface */
4207 * wpas_p2p_deinit_global - Deinitialize global P2P module
4208 * @global: Pointer to global data from wpa_supplicant_init()
4210 * This function deinitializes the global (per device) P2P module.
4212 static void wpas_p2p_deinit_global(struct wpa_global *global)
4214 struct wpa_supplicant *wpa_s, *tmp;
4216 wpa_s = global->ifaces;
4218 wpas_p2p_service_flush(global->p2p_init_wpa_s);
4220 /* Remove remaining P2P group interfaces */
4221 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4222 wpa_s = wpa_s->next;
4224 tmp = global->ifaces;
4227 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4232 /* Disconnect from the P2P group and deinit the interface */
4233 wpas_p2p_disconnect(tmp);
4237 * Deinit GO data on any possibly remaining interface (if main
4238 * interface is used as GO).
4240 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4241 if (wpa_s->ap_iface)
4242 wpas_p2p_group_deinit(wpa_s);
4245 p2p_deinit(global->p2p);
4247 global->p2p_init_wpa_s = NULL;
4251 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4253 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4254 wpa_s->conf->p2p_no_group_iface)
4255 return 0; /* separate interface disabled per configuration */
4256 if (wpa_s->drv_flags &
4257 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4258 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4259 return 1; /* P2P group requires a new interface in every case
4261 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4262 return 0; /* driver does not support concurrent operations */
4263 if (wpa_s->global->ifaces->next)
4264 return 1; /* more that one interface already in use */
4265 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4266 return 1; /* this interface is already in use */
4271 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4272 const u8 *peer_addr,
4273 enum p2p_wps_method wps_method,
4274 int go_intent, const u8 *own_interface_addr,
4275 unsigned int force_freq, int persistent_group,
4276 struct wpa_ssid *ssid, unsigned int pref_freq)
4278 if (persistent_group && wpa_s->conf->persistent_reconnect)
4279 persistent_group = 2;
4282 * Increase GO config timeout if HT40 is used since it takes some time
4283 * to scan channels for coex purposes before the BSS can be started.
4285 p2p_set_config_timeout(wpa_s->global->p2p,
4286 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4288 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4289 go_intent, own_interface_addr, force_freq,
4290 persistent_group, ssid ? ssid->ssid : NULL,
4291 ssid ? ssid->ssid_len : 0,
4292 wpa_s->p2p_pd_before_go_neg, pref_freq,
4293 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4298 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4299 const u8 *peer_addr,
4300 enum p2p_wps_method wps_method,
4301 int go_intent, const u8 *own_interface_addr,
4302 unsigned int force_freq, int persistent_group,
4303 struct wpa_ssid *ssid, unsigned int pref_freq)
4305 if (persistent_group && wpa_s->conf->persistent_reconnect)
4306 persistent_group = 2;
4308 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4309 go_intent, own_interface_addr, force_freq,
4310 persistent_group, ssid ? ssid->ssid : NULL,
4311 ssid ? ssid->ssid_len : 0, pref_freq,
4312 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4317 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4319 wpa_s->p2p_join_scan_count++;
4320 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4321 wpa_s->p2p_join_scan_count);
4322 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4323 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4324 " for join operationg - stop join attempt",
4325 MAC2STR(wpa_s->pending_join_iface_addr));
4326 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4327 if (wpa_s->p2p_auto_pd) {
4328 wpa_s->p2p_auto_pd = 0;
4329 wpa_msg_global(wpa_s, MSG_INFO,
4330 P2P_EVENT_PROV_DISC_FAILURE
4331 " p2p_dev_addr=" MACSTR " status=N/A",
4332 MAC2STR(wpa_s->pending_join_dev_addr));
4335 wpa_msg_global(wpa_s->parent, MSG_INFO,
4336 P2P_EVENT_GROUP_FORMATION_FAILURE);
4341 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4344 unsigned int num, i;
4345 struct wpa_used_freq_data *freqs;
4347 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4348 /* Multiple channels are supported and not all are in use */
4352 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4353 sizeof(struct wpa_used_freq_data));
4357 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4358 wpa_s->num_multichan_concurrent);
4360 for (i = 0; i < num; i++) {
4361 if (freqs[i].freq == freq) {
4362 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4369 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4378 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4379 const u8 *peer_dev_addr)
4381 struct wpa_bss *bss;
4384 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4387 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4388 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4393 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4395 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4396 "%ld.%06ld (%supdated in last scan)",
4397 bss->last_update.sec, bss->last_update.usec,
4398 updated ? "": "not ");
4404 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4405 struct wpa_scan_results *scan_res)
4407 struct wpa_bss *bss = NULL;
4409 u8 iface_addr[ETH_ALEN];
4411 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4413 if (wpa_s->global->p2p_disabled)
4416 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4417 scan_res ? (int) scan_res->num : -1,
4418 wpa_s->p2p_auto_join ? "auto_" : "");
4421 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4423 if (wpa_s->p2p_auto_pd) {
4424 int join = wpas_p2p_peer_go(wpa_s,
4425 wpa_s->pending_join_dev_addr);
4427 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4428 wpa_s->auto_pd_scan_retry++;
4429 bss = wpa_bss_get_bssid_latest(
4430 wpa_s, wpa_s->pending_join_dev_addr);
4433 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4434 "the peer " MACSTR " at %d MHz",
4435 wpa_s->auto_pd_scan_retry,
4437 pending_join_dev_addr),
4439 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4447 wpa_s->p2p_auto_pd = 0;
4448 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4449 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4450 MAC2STR(wpa_s->pending_join_dev_addr), join);
4451 if (p2p_prov_disc_req(wpa_s->global->p2p,
4452 wpa_s->pending_join_dev_addr,
4453 wpa_s->pending_pd_config_methods, join,
4454 0, wpa_s->user_initiated_pd) < 0) {
4455 wpa_s->p2p_auto_pd = 0;
4456 wpa_msg_global(wpa_s, MSG_INFO,
4457 P2P_EVENT_PROV_DISC_FAILURE
4458 " p2p_dev_addr=" MACSTR " status=N/A",
4459 MAC2STR(wpa_s->pending_join_dev_addr));
4464 if (wpa_s->p2p_auto_join) {
4465 int join = wpas_p2p_peer_go(wpa_s,
4466 wpa_s->pending_join_dev_addr);
4468 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4469 "running a GO -> use GO Negotiation");
4470 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4471 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4472 wpa_s->p2p_persistent_group, 0, 0, 0,
4473 wpa_s->p2p_go_intent,
4474 wpa_s->p2p_connect_freq,
4475 wpa_s->p2p_persistent_id,
4476 wpa_s->p2p_pd_before_go_neg,
4482 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4483 "try to join the group", join ? "" :
4486 wpa_s->p2p_fallback_to_go_neg = 1;
4489 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4490 wpa_s->pending_join_iface_addr);
4492 p2p_get_interface_addr(wpa_s->global->p2p,
4493 wpa_s->pending_join_dev_addr,
4495 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4496 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
4497 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4498 "address for join from " MACSTR " to " MACSTR
4499 " based on newly discovered P2P peer entry",
4500 MAC2STR(wpa_s->pending_join_iface_addr),
4501 MAC2STR(iface_addr));
4502 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4505 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4506 wpa_s->pending_join_iface_addr);
4509 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4510 "from P2P peer table: %d MHz", freq);
4512 if (wpa_s->p2p_join_ssid_len) {
4513 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4514 MACSTR " and SSID %s",
4515 MAC2STR(wpa_s->pending_join_iface_addr),
4516 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4517 wpa_s->p2p_join_ssid_len));
4518 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4519 wpa_s->p2p_join_ssid,
4520 wpa_s->p2p_join_ssid_len);
4523 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4524 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4525 bss = wpa_bss_get_bssid_latest(wpa_s,
4526 wpa_s->pending_join_iface_addr);
4530 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4531 "from BSS table: %d MHz (SSID %s)", freq,
4532 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4537 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4538 wpa_msg_global(wpa_s->parent, MSG_INFO,
4539 P2P_EVENT_GROUP_FORMATION_FAILURE
4540 "reason=FREQ_CONFLICT");
4544 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4545 "prior to joining an existing group (GO " MACSTR
4547 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4548 wpa_s->pending_pd_before_join = 1;
4550 switch (wpa_s->pending_join_wps_method) {
4551 case WPS_PIN_DISPLAY:
4552 method = WPS_CONFIG_KEYPAD;
4554 case WPS_PIN_KEYPAD:
4555 method = WPS_CONFIG_DISPLAY;
4558 method = WPS_CONFIG_PUSHBUTTON;
4565 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4566 wpa_s->pending_join_dev_addr) ==
4569 * We have already performed provision discovery for
4570 * joining the group. Proceed directly to join
4571 * operation without duplicated provision discovery. */
4572 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4573 "with " MACSTR " already done - proceed to "
4575 MAC2STR(wpa_s->pending_join_dev_addr));
4576 wpa_s->pending_pd_before_join = 0;
4580 if (p2p_prov_disc_req(wpa_s->global->p2p,
4581 wpa_s->pending_join_dev_addr, method, 1,
4582 freq, wpa_s->user_initiated_pd) < 0) {
4583 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4584 "Discovery Request before joining an "
4586 wpa_s->pending_pd_before_join = 0;
4592 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4593 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4594 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4595 wpas_p2p_check_join_scan_limit(wpa_s);
4599 /* Start join operation immediately */
4600 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4604 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4605 const u8 *ssid, size_t ssid_len)
4608 struct wpa_driver_scan_params params;
4609 struct wpabuf *wps_ie, *ies;
4611 int freqs[2] = { 0, 0 };
4613 os_memset(¶ms, 0, sizeof(params));
4615 /* P2P Wildcard SSID */
4616 params.num_ssids = 1;
4617 if (ssid && ssid_len) {
4618 params.ssids[0].ssid = ssid;
4619 params.ssids[0].ssid_len = ssid_len;
4620 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4621 wpa_s->p2p_join_ssid_len = ssid_len;
4623 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4624 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4625 wpa_s->p2p_join_ssid_len = 0;
4628 wpa_s->wps->dev.p2p = 1;
4629 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4630 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4632 if (wps_ie == NULL) {
4633 wpas_p2p_scan_res_join(wpa_s, NULL);
4637 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4638 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4640 wpabuf_free(wps_ie);
4641 wpas_p2p_scan_res_join(wpa_s, NULL);
4644 wpabuf_put_buf(ies, wps_ie);
4645 wpabuf_free(wps_ie);
4647 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4649 params.p2p_probe = 1;
4650 params.extra_ies = wpabuf_head(ies);
4651 params.extra_ies_len = wpabuf_len(ies);
4656 * If freq is not provided, check the operating freq of the GO
4657 * and use a single channel scan on if possible.
4659 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4660 wpa_s->pending_join_iface_addr);
4666 params.freqs = freqs;
4670 * Run a scan to update BSS table and start Provision Discovery once
4671 * the new scan results become available.
4673 ret = wpa_drv_scan(wpa_s, ¶ms);
4675 os_get_reltime(&wpa_s->scan_trigger_time);
4676 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4677 wpa_s->own_scan_requested = 1;
4683 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4685 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4686 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4687 wpas_p2p_check_join_scan_limit(wpa_s);
4692 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4694 struct wpa_supplicant *wpa_s = eloop_ctx;
4695 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4699 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4700 const u8 *dev_addr, enum p2p_wps_method wps_method,
4701 int auto_join, int op_freq,
4702 const u8 *ssid, size_t ssid_len)
4704 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4705 MACSTR " dev " MACSTR " op_freq=%d)%s",
4706 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4707 auto_join ? " (auto_join)" : "");
4708 if (ssid && ssid_len) {
4709 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4710 wpa_ssid_txt(ssid, ssid_len));
4713 wpa_s->p2p_auto_pd = 0;
4714 wpa_s->p2p_auto_join = !!auto_join;
4715 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4716 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4717 wpa_s->pending_join_wps_method = wps_method;
4719 /* Make sure we are not running find during connection establishment */
4720 wpas_p2p_stop_find(wpa_s);
4722 wpa_s->p2p_join_scan_count = 0;
4723 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4728 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4729 const u8 *ssid, size_t ssid_len)
4731 struct wpa_supplicant *group;
4732 struct p2p_go_neg_results res;
4733 struct wpa_bss *bss;
4735 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4738 if (group != wpa_s) {
4739 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4740 sizeof(group->p2p_pin));
4741 group->p2p_wps_method = wpa_s->p2p_wps_method;
4744 * Need to mark the current interface for p2p_group_formation
4745 * when a separate group interface is not used. This is needed
4746 * to allow p2p_cancel stop a pending p2p_connect-join.
4747 * wpas_p2p_init_group_interface() addresses this for the case
4748 * where a separate group interface is used.
4750 wpa_s->global->p2p_group_formation = wpa_s;
4753 group->p2p_in_provisioning = 1;
4754 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4756 os_memset(&res, 0, sizeof(res));
4757 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4758 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4760 res.wps_method = wpa_s->pending_join_wps_method;
4761 if (freq && ssid && ssid_len) {
4763 res.ssid_len = ssid_len;
4764 os_memcpy(res.ssid, ssid, ssid_len);
4766 bss = wpa_bss_get_bssid_latest(wpa_s,
4767 wpa_s->pending_join_iface_addr);
4769 res.freq = bss->freq;
4770 res.ssid_len = bss->ssid_len;
4771 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4772 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4774 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4778 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4779 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4781 wpa_drv_cancel_remain_on_channel(wpa_s);
4782 wpa_s->off_channel_freq = 0;
4783 wpa_s->roc_waiting_drv_freq = 0;
4785 wpas_start_wps_enrollee(group, &res);
4788 * Allow a longer timeout for join-a-running-group than normal 15
4789 * second group formation timeout since the GO may not have authorized
4790 * our connection yet.
4792 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4793 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4800 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4801 int *force_freq, int *pref_freq, int go)
4803 struct wpa_used_freq_data *freqs;
4804 int res, best_freq, num_unused;
4805 unsigned int freq_in_use = 0, num, i;
4807 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4808 sizeof(struct wpa_used_freq_data));
4812 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4813 wpa_s->num_multichan_concurrent);
4816 * It is possible that the total number of used frequencies is bigger
4817 * than the number of frequencies used for P2P, so get the system wide
4818 * number of unused frequencies.
4820 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4822 wpa_printf(MSG_DEBUG,
4823 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4824 freq, wpa_s->num_multichan_concurrent, num, num_unused);
4829 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4831 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4833 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4834 "(%u MHz) is not supported for P2P uses",
4840 for (i = 0; i < num; i++) {
4841 if (freqs[i].freq == freq)
4845 if (num_unused <= 0 && !freq_in_use) {
4846 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4851 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4852 "requested channel (%u MHz)", freq);
4857 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
4859 /* We have a candidate frequency to use */
4860 if (best_freq > 0) {
4861 if (*pref_freq == 0 && num_unused > 0) {
4862 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4864 *pref_freq = best_freq;
4866 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4868 *force_freq = best_freq;
4870 } else if (num_unused > 0) {
4871 wpa_printf(MSG_DEBUG,
4872 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4875 wpa_printf(MSG_DEBUG,
4876 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4890 * wpas_p2p_connect - Request P2P Group Formation to be started
4891 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4892 * @peer_addr: Address of the peer P2P Device
4893 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4894 * @persistent_group: Whether to create a persistent group
4895 * @auto_join: Whether to select join vs. GO Negotiation automatically
4896 * @join: Whether to join an existing group (as a client) instead of starting
4897 * Group Owner negotiation; @peer_addr is BSSID in that case
4898 * @auth: Whether to only authorize the connection instead of doing that and
4899 * initiating Group Owner negotiation
4900 * @go_intent: GO Intent or -1 to use default
4901 * @freq: Frequency for the group or 0 for auto-selection
4902 * @persistent_id: Persistent group credentials to use for forcing GO
4903 * parameters or -1 to generate new values (SSID/passphrase)
4904 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4905 * interoperability workaround when initiating group formation
4906 * @ht40: Start GO with 40 MHz channel width
4907 * @vht: Start GO with VHT support
4908 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4909 * failure, -2 on failure due to channel not currently available,
4910 * -3 if forced channel is not supported
4912 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4913 const char *pin, enum p2p_wps_method wps_method,
4914 int persistent_group, int auto_join, int join, int auth,
4915 int go_intent, int freq, int persistent_id, int pd,
4918 int force_freq = 0, pref_freq = 0;
4920 enum wpa_driver_if_type iftype;
4922 struct wpa_ssid *ssid = NULL;
4924 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4927 if (persistent_id >= 0) {
4928 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4929 if (ssid == NULL || ssid->disabled != 2 ||
4930 ssid->mode != WPAS_MODE_P2P_GO)
4934 os_free(wpa_s->global->add_psk);
4935 wpa_s->global->add_psk = NULL;
4937 wpa_s->global->p2p_fail_on_wps_complete = 0;
4940 go_intent = wpa_s->conf->p2p_go_intent;
4943 wpa_s->p2p_long_listen = 0;
4945 wpa_s->p2p_wps_method = wps_method;
4946 wpa_s->p2p_persistent_group = !!persistent_group;
4947 wpa_s->p2p_persistent_id = persistent_id;
4948 wpa_s->p2p_go_intent = go_intent;
4949 wpa_s->p2p_connect_freq = freq;
4950 wpa_s->p2p_fallback_to_go_neg = 0;
4951 wpa_s->p2p_pd_before_go_neg = !!pd;
4952 wpa_s->p2p_go_ht40 = !!ht40;
4953 wpa_s->p2p_go_vht = !!vht;
4956 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4957 else if (wps_method == WPS_PIN_DISPLAY) {
4958 ret = wps_generate_pin();
4959 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4961 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4964 wpa_s->p2p_pin[0] = '\0';
4966 if (join || auto_join) {
4967 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4969 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4970 "connect a running group from " MACSTR,
4971 MAC2STR(peer_addr));
4972 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4975 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4976 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4978 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4979 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4983 os_get_reltime(&wpa_s->p2p_auto_started);
4984 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4986 wpa_s->p2p_auto_started.sec,
4987 wpa_s->p2p_auto_started.usec);
4989 wpa_s->user_initiated_pd = 1;
4990 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4991 auto_join, freq, NULL, 0) < 0)
4996 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5000 wpas_p2p_set_own_freq_preference(wpa_s,
5001 force_freq ? force_freq : pref_freq);
5003 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5005 if (wpa_s->create_p2p_iface) {
5006 /* Prepare to add a new interface for the group */
5007 iftype = WPA_IF_P2P_GROUP;
5008 if (go_intent == 15)
5009 iftype = WPA_IF_P2P_GO;
5010 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5011 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5012 "interface for the group");
5016 if_addr = wpa_s->pending_interface_addr;
5018 if_addr = wpa_s->own_addr;
5021 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
5023 force_freq, persistent_group, ssid,
5029 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5030 go_intent, if_addr, force_freq,
5031 persistent_group, ssid, pref_freq) < 0) {
5032 if (wpa_s->create_p2p_iface)
5033 wpas_p2p_remove_pending_group_interface(wpa_s);
5041 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5042 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5043 * @freq: Frequency of the channel in MHz
5044 * @duration: Duration of the stay on the channel in milliseconds
5046 * This callback is called when the driver indicates that it has started the
5047 * requested remain-on-channel duration.
5049 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5050 unsigned int freq, unsigned int duration)
5052 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5054 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5055 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5056 wpa_s->pending_listen_duration);
5057 wpa_s->pending_listen_freq = 0;
5059 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5060 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5066 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
5067 unsigned int timeout)
5069 /* Limit maximum Listen state time based on driver limitation. */
5070 if (timeout > wpa_s->max_remain_on_chan)
5071 timeout = wpa_s->max_remain_on_chan;
5073 return p2p_listen(wpa_s->global->p2p, timeout);
5078 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5079 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5080 * @freq: Frequency of the channel in MHz
5082 * This callback is called when the driver indicates that a remain-on-channel
5083 * operation has been completed, i.e., the duration on the requested channel
5086 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5089 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5090 "(p2p_long_listen=%d ms pending_action_tx=%p)",
5091 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
5092 wpas_p2p_listen_work_done(wpa_s);
5093 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5095 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5096 return; /* P2P module started a new operation */
5097 if (offchannel_pending_action_tx(wpa_s))
5099 if (wpa_s->p2p_long_listen > 0)
5100 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
5101 if (wpa_s->p2p_long_listen > 0) {
5102 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5103 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
5106 * When listen duration is over, stop listen & update p2p_state
5109 p2p_stop_listen(wpa_s->global->p2p);
5115 * wpas_p2p_group_remove - Remove a P2P group
5116 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5117 * @ifname: Network interface name of the group interface or "*" to remove all
5119 * Returns: 0 on success, -1 on failure
5121 * This function is used to remove a P2P group. This can be used to disconnect
5122 * from a group in which the local end is a P2P Client or to end a P2P Group in
5123 * case the local end is the Group Owner. If a virtual network interface was
5124 * created for this group, that interface will be removed. Otherwise, only the
5125 * configured P2P group network will be removed from the interface.
5127 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5129 struct wpa_global *global = wpa_s->global;
5130 struct wpa_supplicant *calling_wpa_s = wpa_s;
5132 if (os_strcmp(ifname, "*") == 0) {
5133 struct wpa_supplicant *prev;
5134 wpa_s = global->ifaces;
5137 wpa_s = wpa_s->next;
5138 if (prev->p2p_group_interface !=
5139 NOT_P2P_GROUP_INTERFACE ||
5140 (prev->current_ssid &&
5141 prev->current_ssid->p2p_group))
5142 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
5147 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5148 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5152 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
5156 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5161 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5163 if (wpa_s->best_24_freq > 0 &&
5164 p2p_supported_freq_go(wpa_s->global->p2p,
5165 wpa_s->best_24_freq)) {
5166 freq = wpa_s->best_24_freq;
5167 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5168 "channel: %d MHz", freq);
5170 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5172 freq = 2412 + (r % 3) * 25;
5173 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5174 "channel: %d MHz", freq);
5179 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5181 if (wpa_s->best_5_freq > 0 &&
5182 p2p_supported_freq_go(wpa_s->global->p2p,
5183 wpa_s->best_5_freq)) {
5184 freq = wpa_s->best_5_freq;
5185 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5186 "channel: %d MHz", freq);
5188 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5190 freq = 5180 + (r % 4) * 20;
5191 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5192 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5193 "5 GHz channel for P2P group");
5196 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5197 "channel: %d MHz", freq);
5201 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5202 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5203 "(%u MHz) is not supported for P2P uses",
5212 static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5213 struct p2p_go_neg_results *params,
5214 const struct p2p_channels *channels)
5218 /* first try some random selection of the social channels */
5219 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5222 for (i = 0; i < 3; i++) {
5223 params->freq = 2412 + ((r + i) % 3) * 25;
5224 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5225 freq_included(channels, params->freq) &&
5226 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5230 /* try all channels in reg. class 81 */
5231 for (i = 0; i < 11; i++) {
5232 params->freq = 2412 + i * 5;
5233 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5234 freq_included(channels, params->freq) &&
5235 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5239 /* try social channel class 180 channel 2 */
5240 params->freq = 58320 + 1 * 2160;
5241 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5242 freq_included(channels, params->freq) &&
5243 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5246 /* try all channels in reg. class 180 */
5247 for (i = 0; i < 4; i++) {
5248 params->freq = 58320 + i * 2160;
5249 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5250 freq_included(channels, params->freq) &&
5251 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5255 wpa_printf(MSG_DEBUG, "P2P: No 2.4 and 60 GHz channel allowed");
5258 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5264 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5265 struct p2p_go_neg_results *params,
5266 int freq, int ht40, int vht,
5267 const struct p2p_channels *channels)
5269 struct wpa_used_freq_data *freqs;
5270 unsigned int pref_freq, cand_freq;
5271 unsigned int num, i;
5273 os_memset(params, 0, sizeof(*params));
5274 params->role_go = 1;
5275 params->ht40 = ht40;
5278 if (!freq_included(channels, freq)) {
5279 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5283 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5284 "frequency %d MHz", freq);
5285 params->freq = freq;
5286 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5287 wpa_s->conf->p2p_oper_channel >= 1 &&
5288 wpa_s->conf->p2p_oper_channel <= 11 &&
5289 freq_included(channels,
5290 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5291 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5292 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5293 "frequency %d MHz", params->freq);
5294 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5295 wpa_s->conf->p2p_oper_reg_class == 116 ||
5296 wpa_s->conf->p2p_oper_reg_class == 117 ||
5297 wpa_s->conf->p2p_oper_reg_class == 124 ||
5298 wpa_s->conf->p2p_oper_reg_class == 126 ||
5299 wpa_s->conf->p2p_oper_reg_class == 127) &&
5300 freq_included(channels,
5301 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
5302 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5303 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5304 "frequency %d MHz", params->freq);
5305 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5306 wpa_s->best_overall_freq > 0 &&
5307 p2p_supported_freq_go(wpa_s->global->p2p,
5308 wpa_s->best_overall_freq) &&
5309 freq_included(channels, wpa_s->best_overall_freq)) {
5310 params->freq = wpa_s->best_overall_freq;
5311 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5312 "channel %d MHz", params->freq);
5313 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5314 wpa_s->best_24_freq > 0 &&
5315 p2p_supported_freq_go(wpa_s->global->p2p,
5316 wpa_s->best_24_freq) &&
5317 freq_included(channels, wpa_s->best_24_freq)) {
5318 params->freq = wpa_s->best_24_freq;
5319 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5320 "channel %d MHz", params->freq);
5321 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5322 wpa_s->best_5_freq > 0 &&
5323 p2p_supported_freq_go(wpa_s->global->p2p,
5324 wpa_s->best_5_freq) &&
5325 freq_included(channels, wpa_s->best_5_freq)) {
5326 params->freq = wpa_s->best_5_freq;
5327 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5328 "channel %d MHz", params->freq);
5329 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5331 params->freq = pref_freq;
5332 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5333 "channels", params->freq);
5335 /* no preference, select some channel */
5336 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
5340 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5341 sizeof(struct wpa_used_freq_data));
5345 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5346 wpa_s->num_multichan_concurrent);
5348 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5350 /* First try the best used frequency if possible */
5351 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5352 params->freq = cand_freq;
5354 /* Try any of the used frequencies */
5355 for (i = 0; i < num; i++) {
5356 if (freq_included(channels, freqs[i].freq)) {
5357 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5359 params->freq = freqs[i].freq;
5365 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5366 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5370 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5374 for (i = 0; i < num; i++) {
5375 if (freqs[i].freq == freq)
5380 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5382 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5386 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5396 static struct wpa_supplicant *
5397 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5400 struct wpa_supplicant *group_wpa_s;
5402 if (!wpas_p2p_create_iface(wpa_s)) {
5403 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5405 wpa_s->p2p_first_connection_timeout = 0;
5409 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5410 WPA_IF_P2P_CLIENT) < 0) {
5411 wpa_msg_global(wpa_s, MSG_ERROR,
5412 "P2P: Failed to add group interface");
5415 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5416 if (group_wpa_s == NULL) {
5417 wpa_msg_global(wpa_s, MSG_ERROR,
5418 "P2P: Failed to initialize group interface");
5419 wpas_p2p_remove_pending_group_interface(wpa_s);
5423 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5424 group_wpa_s->ifname);
5425 group_wpa_s->p2p_first_connection_timeout = 0;
5431 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5432 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5433 * @persistent_group: Whether to create a persistent group
5434 * @freq: Frequency for the group or 0 to indicate no hardcoding
5435 * @ht40: Start GO with 40 MHz channel width
5436 * @vht: Start GO with VHT support
5437 * Returns: 0 on success, -1 on failure
5439 * This function creates a new P2P group with the local end as the Group Owner,
5440 * i.e., without using Group Owner Negotiation.
5442 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5443 int freq, int ht40, int vht)
5445 struct p2p_go_neg_results params;
5447 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5450 os_free(wpa_s->global->add_psk);
5451 wpa_s->global->add_psk = NULL;
5453 /* Make sure we are not running find during connection establishment */
5454 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5455 wpas_p2p_stop_find_oper(wpa_s);
5457 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5461 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5464 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5465 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5466 "(%u MHz) is not supported for P2P uses",
5470 p2p_go_params(wpa_s->global->p2p, ¶ms);
5471 params.persistent_group = persistent_group;
5473 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5476 wpas_start_wps_go(wpa_s, ¶ms, 0);
5482 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5483 struct wpa_ssid *params, int addr_allocated,
5486 struct wpa_ssid *ssid;
5488 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5491 wpa_s->p2p_last_4way_hs_fail = NULL;
5493 wpa_supplicant_ap_deinit(wpa_s);
5495 ssid = wpa_config_add_network(wpa_s->conf);
5498 wpa_config_set_network_defaults(ssid);
5499 ssid->temporary = 1;
5500 ssid->proto = WPA_PROTO_RSN;
5501 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5502 ssid->group_cipher = WPA_CIPHER_CCMP;
5503 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5504 ssid->ssid = os_malloc(params->ssid_len);
5505 if (ssid->ssid == NULL) {
5506 wpa_config_remove_network(wpa_s->conf, ssid->id);
5509 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5510 ssid->ssid_len = params->ssid_len;
5511 ssid->p2p_group = 1;
5512 ssid->export_keys = 1;
5513 if (params->psk_set) {
5514 os_memcpy(ssid->psk, params->psk, 32);
5517 if (params->passphrase)
5518 ssid->passphrase = os_strdup(params->passphrase);
5520 wpa_s->show_group_started = 1;
5521 wpa_s->p2p_in_invitation = 1;
5522 wpa_s->p2p_invite_go_freq = freq;
5524 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5526 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5527 wpas_p2p_group_formation_timeout,
5528 wpa_s->parent, NULL);
5529 wpa_supplicant_select_network(wpa_s, ssid);
5535 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5536 struct wpa_ssid *ssid, int addr_allocated,
5537 int force_freq, int neg_freq, int ht40,
5538 int vht, const struct p2p_channels *channels,
5539 int connection_timeout)
5541 struct p2p_go_neg_results params;
5544 if (ssid->disabled != 2 || ssid->ssid == NULL)
5547 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5548 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5549 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5554 os_free(wpa_s->global->add_psk);
5555 wpa_s->global->add_psk = NULL;
5557 /* Make sure we are not running find during connection establishment */
5558 wpas_p2p_stop_find_oper(wpa_s);
5560 wpa_s->p2p_fallback_to_go_neg = 0;
5562 if (ssid->mode == WPAS_MODE_P2P_GO) {
5563 if (force_freq > 0) {
5564 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5568 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5570 (freq > 0 && !freq_included(channels, freq)))
5576 (freq > 0 && !freq_included(channels, freq)))
5580 if (ssid->mode == WPAS_MODE_INFRA)
5581 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5583 if (ssid->mode != WPAS_MODE_P2P_GO)
5586 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
5590 params.psk_set = ssid->psk_set;
5592 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5593 if (ssid->passphrase) {
5594 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5595 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5596 "persistent group");
5599 os_strlcpy(params.passphrase, ssid->passphrase,
5600 sizeof(params.passphrase));
5602 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5603 params.ssid_len = ssid->ssid_len;
5604 params.persistent_group = 1;
5606 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5610 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
5612 wpa_s->p2p_first_connection_timeout = connection_timeout;
5613 wpas_start_wps_go(wpa_s, ¶ms, 0);
5619 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5620 struct wpabuf *proberesp_ies)
5622 struct wpa_supplicant *wpa_s = ctx;
5623 if (wpa_s->ap_iface) {
5624 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5625 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5626 wpabuf_free(beacon_ies);
5627 wpabuf_free(proberesp_ies);
5631 wpabuf_free(hapd->p2p_beacon_ie);
5632 hapd->p2p_beacon_ie = beacon_ies;
5634 wpabuf_free(hapd->p2p_probe_resp_ie);
5635 hapd->p2p_probe_resp_ie = proberesp_ies;
5637 wpabuf_free(beacon_ies);
5638 wpabuf_free(proberesp_ies);
5640 wpa_supplicant_ap_update_beacon(wpa_s);
5644 static void wpas_p2p_idle_update(void *ctx, int idle)
5646 struct wpa_supplicant *wpa_s = ctx;
5647 if (!wpa_s->ap_iface)
5649 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
5651 if (wpa_s->global->p2p_fail_on_wps_complete &&
5652 wpa_s->p2p_in_provisioning) {
5653 wpas_p2p_grpform_fail_after_wps(wpa_s);
5656 wpas_p2p_set_group_idle_timeout(wpa_s);
5658 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5662 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
5663 struct wpa_ssid *ssid)
5665 struct p2p_group *group;
5666 struct p2p_group_config *cfg;
5668 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5671 cfg = os_zalloc(sizeof(*cfg));
5675 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
5676 cfg->persistent_group = 2;
5677 else if (ssid->p2p_persistent_group)
5678 cfg->persistent_group = 1;
5679 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5680 if (wpa_s->max_stations &&
5681 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5682 cfg->max_clients = wpa_s->max_stations;
5684 cfg->max_clients = wpa_s->conf->max_num_sta;
5685 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5686 cfg->ssid_len = ssid->ssid_len;
5687 cfg->freq = ssid->frequency;
5688 cfg->cb_ctx = wpa_s;
5689 cfg->ie_update = wpas_p2p_ie_update;
5690 cfg->idle_update = wpas_p2p_idle_update;
5692 group = p2p_group_init(wpa_s->global->p2p, cfg);
5695 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
5696 p2p_group_notif_formation_done(group);
5697 wpa_s->p2p_group = group;
5702 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5705 struct wpa_ssid *ssid = wpa_s->current_ssid;
5707 if (!wpa_s->p2p_in_provisioning) {
5708 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5709 "provisioning not in progress");
5713 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5714 u8 go_dev_addr[ETH_ALEN];
5715 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5716 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5718 /* Clear any stored provisioning info */
5719 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5722 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5724 wpa_s->p2p_go_group_formation_completed = 1;
5725 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5727 * Use a separate timeout for initial data connection to
5728 * complete to allow the group to be removed automatically if
5729 * something goes wrong in this step before the P2P group idle
5730 * timeout mechanism is taken into use.
5732 wpa_dbg(wpa_s, MSG_DEBUG,
5733 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5734 P2P_MAX_INITIAL_CONN_WAIT);
5735 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5736 wpas_p2p_group_formation_timeout,
5737 wpa_s->parent, NULL);
5740 * Use a separate timeout for initial data connection to
5741 * complete to allow the group to be removed automatically if
5742 * the client does not complete data connection successfully.
5744 wpa_dbg(wpa_s, MSG_DEBUG,
5745 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5746 P2P_MAX_INITIAL_CONN_WAIT_GO);
5747 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5748 wpas_p2p_group_formation_timeout,
5749 wpa_s->parent, NULL);
5751 * Complete group formation on first successful data connection
5753 wpa_s->p2p_go_group_formation_completed = 0;
5755 if (wpa_s->global->p2p)
5756 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5757 wpas_group_formation_completed(wpa_s, 1);
5761 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5762 struct wps_event_fail *fail)
5764 if (!wpa_s->p2p_in_provisioning) {
5765 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5766 "provisioning not in progress");
5770 if (wpa_s->go_params) {
5771 p2p_clear_provisioning_info(
5773 wpa_s->go_params->peer_device_addr);
5776 wpas_notify_p2p_wps_failed(wpa_s, fail);
5778 if (wpa_s == wpa_s->global->p2p_group_formation) {
5780 * Allow some time for the failed WPS negotiation exchange to
5781 * complete, but remove the group since group formation cannot
5782 * succeed after provisioning failure.
5784 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5785 wpa_s->global->p2p_fail_on_wps_complete = 1;
5786 eloop_deplete_timeout(0, 50000,
5787 wpas_p2p_group_formation_timeout,
5788 wpa_s->parent, NULL);
5793 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5795 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5796 !wpa_s->p2p_in_provisioning)
5799 wpas_p2p_grpform_fail_after_wps(wpa_s);
5805 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5806 const char *config_method,
5807 enum wpas_p2p_prov_disc_use use)
5811 wpa_s->p2p_fallback_to_go_neg = 0;
5812 wpa_s->pending_pd_use = NORMAL_PD;
5813 if (os_strncmp(config_method, "display", 7) == 0)
5814 config_methods = WPS_CONFIG_DISPLAY;
5815 else if (os_strncmp(config_method, "keypad", 6) == 0)
5816 config_methods = WPS_CONFIG_KEYPAD;
5817 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5818 os_strncmp(config_method, "pushbutton", 10) == 0)
5819 config_methods = WPS_CONFIG_PUSHBUTTON;
5821 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5825 if (use == WPAS_P2P_PD_AUTO) {
5826 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5827 wpa_s->pending_pd_config_methods = config_methods;
5828 wpa_s->p2p_auto_pd = 1;
5829 wpa_s->p2p_auto_join = 0;
5830 wpa_s->pending_pd_before_join = 0;
5831 wpa_s->auto_pd_scan_retry = 0;
5832 wpas_p2p_stop_find(wpa_s);
5833 wpa_s->p2p_join_scan_count = 0;
5834 os_get_reltime(&wpa_s->p2p_auto_started);
5835 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5836 wpa_s->p2p_auto_started.sec,
5837 wpa_s->p2p_auto_started.usec);
5838 wpas_p2p_join_scan(wpa_s, NULL);
5842 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5845 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
5846 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5851 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5854 return p2p_scan_result_text(ies, ies_len, buf, end);
5858 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5860 if (!offchannel_pending_action_tx(wpa_s))
5863 wpas_p2p_action_tx_clear(wpa_s);
5865 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5866 "operation request");
5867 offchannel_clear_pending_action_tx(wpa_s);
5871 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5872 enum p2p_discovery_type type,
5873 unsigned int num_req_dev_types, const u8 *req_dev_types,
5874 const u8 *dev_id, unsigned int search_delay)
5876 wpas_p2p_clear_pending_action_tx(wpa_s);
5877 wpa_s->p2p_long_listen = 0;
5879 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5880 wpa_s->p2p_in_provisioning)
5883 wpa_supplicant_cancel_sched_scan(wpa_s);
5885 return p2p_find(wpa_s->global->p2p, timeout, type,
5886 num_req_dev_types, req_dev_types, dev_id,
5891 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
5893 wpas_p2p_clear_pending_action_tx(wpa_s);
5894 wpa_s->p2p_long_listen = 0;
5895 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5896 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5898 if (wpa_s->global->p2p)
5899 p2p_stop_find(wpa_s->global->p2p);
5903 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5905 wpas_p2p_stop_find_oper(wpa_s);
5906 wpas_p2p_remove_pending_group_interface(wpa_s);
5910 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5912 struct wpa_supplicant *wpa_s = eloop_ctx;
5913 wpa_s->p2p_long_listen = 0;
5917 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5921 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5924 wpa_supplicant_cancel_sched_scan(wpa_s);
5925 wpas_p2p_clear_pending_action_tx(wpa_s);
5929 * This is a request for unlimited Listen state. However, at
5930 * least for now, this is mapped to a Listen state for one
5935 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5936 wpa_s->p2p_long_listen = 0;
5939 * Stop previous find/listen operation to avoid trying to request a new
5940 * remain-on-channel operation while the driver is still running the
5943 if (wpa_s->global->p2p)
5944 p2p_stop_find(wpa_s->global->p2p);
5946 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5947 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5948 wpa_s->p2p_long_listen = timeout * 1000;
5949 eloop_register_timeout(timeout, 0,
5950 wpas_p2p_long_listen_timeout,
5958 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5959 u8 *buf, size_t len, int p2p_group)
5961 struct wpabuf *p2p_ie;
5964 if (wpa_s->global->p2p_disabled)
5966 if (wpa_s->conf->p2p_disabled)
5968 if (wpa_s->global->p2p == NULL)
5973 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5974 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5976 wpabuf_free(p2p_ie);
5982 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
5983 const u8 *dst, const u8 *bssid,
5984 const u8 *ie, size_t ie_len, int ssi_signal)
5986 if (wpa_s->global->p2p_disabled)
5988 if (wpa_s->global->p2p == NULL)
5991 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5993 case P2P_PREQ_NOT_P2P:
5994 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5997 case P2P_PREQ_MALFORMED:
5998 case P2P_PREQ_NOT_LISTEN:
5999 case P2P_PREQ_NOT_PROCESSED:
6000 default: /* make gcc happy */
6002 case P2P_PREQ_PROCESSED:
6008 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6009 const u8 *sa, const u8 *bssid,
6010 u8 category, const u8 *data, size_t len, int freq)
6012 if (wpa_s->global->p2p_disabled)
6014 if (wpa_s->global->p2p == NULL)
6017 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6022 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6024 if (wpa_s->global->p2p_disabled)
6026 if (wpa_s->global->p2p == NULL)
6029 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
6033 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
6035 p2p_group_deinit(wpa_s->p2p_group);
6036 wpa_s->p2p_group = NULL;
6038 wpa_s->ap_configured_cb = NULL;
6039 wpa_s->ap_configured_cb_ctx = NULL;
6040 wpa_s->ap_configured_cb_data = NULL;
6041 wpa_s->connect_without_scan = NULL;
6045 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6047 wpa_s->p2p_long_listen = 0;
6049 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6052 return p2p_reject(wpa_s->global->p2p, addr);
6056 /* Invite to reinvoke a persistent group */
6057 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6058 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
6059 int ht40, int vht, int pref_freq)
6061 enum p2p_invite_role role;
6065 int no_pref_freq_given = pref_freq == 0;
6067 wpa_s->global->p2p_invite_group = NULL;
6069 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6071 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6073 wpa_s->p2p_persistent_go_freq = freq;
6074 wpa_s->p2p_go_ht40 = !!ht40;
6075 if (ssid->mode == WPAS_MODE_P2P_GO) {
6076 role = P2P_INVITE_ROLE_GO;
6077 if (peer_addr == NULL) {
6078 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6079 "address in invitation command");
6082 if (wpas_p2p_create_iface(wpa_s)) {
6083 if (wpas_p2p_add_group_interface(wpa_s,
6084 WPA_IF_P2P_GO) < 0) {
6085 wpa_printf(MSG_ERROR, "P2P: Failed to "
6086 "allocate a new interface for the "
6090 bssid = wpa_s->pending_interface_addr;
6092 bssid = wpa_s->own_addr;
6094 role = P2P_INVITE_ROLE_CLIENT;
6095 peer_addr = ssid->bssid;
6097 wpa_s->pending_invite_ssid_id = ssid->id;
6099 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6100 role == P2P_INVITE_ROLE_GO);
6104 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6107 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6108 no_pref_freq_given && pref_freq > 0 &&
6109 wpa_s->num_multichan_concurrent > 1 &&
6110 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6111 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6116 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6117 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
6122 /* Invite to join an active group */
6123 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6124 const u8 *peer_addr, const u8 *go_dev_addr)
6126 struct wpa_global *global = wpa_s->global;
6127 enum p2p_invite_role role;
6129 struct wpa_ssid *ssid;
6131 int freq = 0, force_freq = 0, pref_freq = 0;
6134 wpa_s->p2p_persistent_go_freq = 0;
6135 wpa_s->p2p_go_ht40 = 0;
6136 wpa_s->p2p_go_vht = 0;
6138 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6139 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6142 if (wpa_s == NULL) {
6143 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6147 ssid = wpa_s->current_ssid;
6149 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6154 wpa_s->global->p2p_invite_group = wpa_s;
6155 persistent = ssid->p2p_persistent_group &&
6156 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6157 ssid->ssid, ssid->ssid_len);
6159 if (ssid->mode == WPAS_MODE_P2P_GO) {
6160 role = P2P_INVITE_ROLE_ACTIVE_GO;
6161 bssid = wpa_s->own_addr;
6162 if (go_dev_addr == NULL)
6163 go_dev_addr = wpa_s->global->p2p_dev_addr;
6164 freq = ssid->frequency;
6166 role = P2P_INVITE_ROLE_CLIENT;
6167 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6168 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6169 "invite to current group");
6172 bssid = wpa_s->bssid;
6173 if (go_dev_addr == NULL &&
6174 !is_zero_ether_addr(wpa_s->go_dev_addr))
6175 go_dev_addr = wpa_s->go_dev_addr;
6176 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6177 (int) wpa_s->assoc_freq;
6179 wpa_s->parent->pending_invite_ssid_id = -1;
6181 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6184 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6185 role == P2P_INVITE_ROLE_ACTIVE_GO);
6188 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
6190 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6191 ssid->ssid, ssid->ssid_len, force_freq,
6192 go_dev_addr, persistent, pref_freq, -1);
6196 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6198 struct wpa_ssid *ssid = wpa_s->current_ssid;
6199 u8 go_dev_addr[ETH_ALEN];
6200 int network_id = -1;
6206 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6207 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6208 wpa_s->parent, NULL);
6211 if (!wpa_s->show_group_started || !ssid)
6214 wpa_s->show_group_started = 0;
6216 os_memset(go_dev_addr, 0, ETH_ALEN);
6217 if (ssid->bssid_set)
6218 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6219 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6221 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6223 if (wpa_s->global->p2p_group_formation == wpa_s)
6224 wpa_s->global->p2p_group_formation = NULL;
6226 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6227 (int) wpa_s->assoc_freq;
6230 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6231 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
6232 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6233 ip[0], ip[1], ip[2], ip[3],
6234 ip[4], ip[5], ip[6], ip[7],
6235 ip[8], ip[9], ip[10], ip[11]);
6238 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6239 ssid->passphrase == NULL && ssid->psk_set ?
6241 ssid->passphrase, go_dev_addr, persistent,
6245 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6248 network_id = ssid->id;
6249 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6253 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6254 u32 interval1, u32 duration2, u32 interval2)
6258 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6261 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6262 wpa_s->current_ssid == NULL ||
6263 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6266 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6267 wpa_s->own_addr, wpa_s->assoc_freq,
6268 duration1, interval1, duration2, interval2);
6270 wpa_s->waiting_presence_resp = 1;
6276 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6277 unsigned int interval)
6279 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6282 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6286 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6288 if (wpa_s->current_ssid == NULL) {
6290 * current_ssid can be cleared when P2P client interface gets
6291 * disconnected, so assume this interface was used as P2P
6296 return wpa_s->current_ssid->p2p_group &&
6297 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6301 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6303 struct wpa_supplicant *wpa_s = eloop_ctx;
6305 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6306 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6311 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6313 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6317 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6321 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6322 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6324 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6327 timeout = wpa_s->conf->p2p_group_idle;
6328 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6329 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6330 timeout = P2P_MAX_CLIENT_IDLE;
6336 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6337 timeout = 0; /* special client mode no-timeout */
6342 if (wpa_s->p2p_in_provisioning) {
6344 * Use the normal group formation timeout during the
6345 * provisioning phase to avoid terminating this process too
6346 * early due to group idle timeout.
6348 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6349 "during provisioning");
6353 if (wpa_s->show_group_started) {
6355 * Use the normal group formation timeout between the end of
6356 * the provisioning phase and completion of 4-way handshake to
6357 * avoid terminating this process too early due to group idle
6360 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6361 "while waiting for initial 4-way handshake to "
6366 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6368 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6373 /* Returns 1 if the interface was removed */
6374 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6375 u16 reason_code, const u8 *ie, size_t ie_len,
6376 int locally_generated)
6378 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6381 if (!locally_generated)
6382 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6385 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6386 wpa_s->current_ssid &&
6387 wpa_s->current_ssid->p2p_group &&
6388 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6389 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6390 "session is ending");
6391 if (wpas_p2p_group_delete(wpa_s,
6392 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6401 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6402 u16 reason_code, const u8 *ie, size_t ie_len,
6403 int locally_generated)
6405 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6408 if (!locally_generated)
6409 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6414 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6416 struct p2p_data *p2p = wpa_s->global->p2p;
6421 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6424 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6425 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6427 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6428 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6431 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6432 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6434 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6435 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6437 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6438 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6439 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6440 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6441 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6444 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6445 p2p_set_sec_dev_types(p2p,
6446 (void *) wpa_s->conf->sec_device_type,
6447 wpa_s->conf->num_sec_device_types);
6449 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6451 p2p_remove_wps_vendor_extensions(p2p);
6452 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6453 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6455 p2p_add_wps_vendor_extension(
6456 p2p, wpa_s->conf->wps_vendor_ext[i]);
6460 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6461 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6463 country[0] = wpa_s->conf->country[0];
6464 country[1] = wpa_s->conf->country[1];
6466 p2p_set_country(p2p, country);
6469 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6470 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6471 wpa_s->conf->p2p_ssid_postfix ?
6472 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6476 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6477 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6479 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6480 u8 reg_class, channel;
6485 if (wpa_s->conf->p2p_listen_reg_class &&
6486 wpa_s->conf->p2p_listen_channel) {
6487 reg_class = wpa_s->conf->p2p_listen_reg_class;
6488 channel = wpa_s->conf->p2p_listen_channel;
6493 * Pick one of the social channels randomly as the
6496 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6499 channel = 1 + (r % 3) * 5;
6502 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6505 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6508 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6509 u8 op_reg_class, op_channel, cfg_op_channel;
6512 if (wpa_s->conf->p2p_oper_reg_class &&
6513 wpa_s->conf->p2p_oper_channel) {
6514 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6515 op_channel = wpa_s->conf->p2p_oper_channel;
6520 * Use random operation channel from (1, 6, 11)
6521 *if no other preference is indicated.
6523 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6526 op_channel = 1 + (r % 3) * 5;
6529 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6532 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6536 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6537 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6538 wpa_s->conf->p2p_pref_chan) < 0) {
6539 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6543 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6544 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6549 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6550 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
6554 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6557 if (!wpa_s->ap_iface)
6559 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6564 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6566 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6569 wpa_s->global->cross_connection = enabled;
6570 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6573 struct wpa_supplicant *iface;
6575 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6577 if (iface->cross_connect_enabled == 0)
6580 iface->cross_connect_enabled = 0;
6581 iface->cross_connect_in_use = 0;
6582 wpa_msg_global(iface->parent, MSG_INFO,
6583 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6585 iface->cross_connect_uplink);
6593 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6595 struct wpa_supplicant *iface;
6597 if (!uplink->global->cross_connection)
6600 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6601 if (!iface->cross_connect_enabled)
6603 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6606 if (iface->ap_iface == NULL)
6608 if (iface->cross_connect_in_use)
6611 iface->cross_connect_in_use = 1;
6612 wpa_msg_global(iface->parent, MSG_INFO,
6613 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6614 iface->ifname, iface->cross_connect_uplink);
6619 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6621 struct wpa_supplicant *iface;
6623 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6624 if (!iface->cross_connect_enabled)
6626 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6629 if (!iface->cross_connect_in_use)
6632 wpa_msg_global(iface->parent, MSG_INFO,
6633 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6634 iface->ifname, iface->cross_connect_uplink);
6635 iface->cross_connect_in_use = 0;
6640 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6642 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6643 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6644 wpa_s->cross_connect_disallowed)
6645 wpas_p2p_disable_cross_connect(wpa_s);
6647 wpas_p2p_enable_cross_connect(wpa_s);
6648 if (!wpa_s->ap_iface &&
6649 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6650 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6654 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6656 wpas_p2p_disable_cross_connect(wpa_s);
6657 if (!wpa_s->ap_iface &&
6658 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6660 wpas_p2p_set_group_idle_timeout(wpa_s);
6664 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6666 struct wpa_supplicant *iface;
6668 if (!wpa_s->global->cross_connection)
6671 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6674 if (iface->drv_flags &
6675 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6677 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6680 wpa_s->cross_connect_enabled = 1;
6681 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6682 sizeof(wpa_s->cross_connect_uplink));
6683 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6684 "%s to %s whenever uplink is available",
6685 wpa_s->ifname, wpa_s->cross_connect_uplink);
6687 if (iface->ap_iface || iface->current_ssid == NULL ||
6688 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6689 iface->cross_connect_disallowed ||
6690 iface->wpa_state != WPA_COMPLETED)
6693 wpa_s->cross_connect_in_use = 1;
6694 wpa_msg_global(wpa_s->parent, MSG_INFO,
6695 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6696 wpa_s->ifname, wpa_s->cross_connect_uplink);
6702 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6704 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6705 !wpa_s->p2p_in_provisioning)
6706 return 0; /* not P2P client operation */
6708 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6710 if (wpa_s != wpa_s->parent)
6711 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
6712 wpas_p2p_group_formation_failed(wpa_s);
6717 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6719 struct wpa_supplicant *wpa_s = eloop_ctx;
6720 wpas_p2p_notif_pbc_overlap(wpa_s);
6724 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6726 struct p2p_channels chan, cli_chan;
6727 struct wpa_supplicant *ifs;
6729 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6732 os_memset(&chan, 0, sizeof(chan));
6733 os_memset(&cli_chan, 0, sizeof(cli_chan));
6734 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
6735 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6740 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
6742 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6744 if (!ifs->current_ssid ||
6745 !ifs->current_ssid->p2p_group ||
6746 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6747 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6749 freq = ifs->current_ssid->frequency;
6750 if (freq_included(&chan, freq)) {
6751 wpa_dbg(ifs, MSG_DEBUG,
6752 "P2P GO operating frequency %d MHz in valid range",
6757 wpa_dbg(ifs, MSG_DEBUG,
6758 "P2P GO operating in invalid frequency %d MHz", freq);
6759 /* TODO: Consider using CSA or removing the group within
6761 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6766 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6767 struct wpa_scan_results *scan_res)
6769 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6773 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6775 struct wpa_global *global = wpa_s->global;
6779 if (global->p2p == NULL)
6782 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6784 if (wpa_s->pending_interface_name[0] &&
6785 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6788 peer = p2p_get_go_neg_peer(global->p2p);
6790 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6791 MACSTR, MAC2STR(peer));
6792 p2p_unauthorize(global->p2p, peer);
6796 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6797 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6798 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6802 if (wpa_s->pending_pd_before_join) {
6803 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6804 wpa_s->pending_pd_before_join = 0;
6808 wpas_p2p_stop_find(wpa_s);
6810 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6811 if (wpa_s == global->p2p_group_formation &&
6812 (wpa_s->p2p_in_provisioning ||
6813 wpa_s->parent->pending_interface_type ==
6814 WPA_IF_P2P_CLIENT)) {
6815 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6816 "formation found - cancelling",
6819 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6820 wpa_s->parent, NULL);
6821 if (wpa_s->p2p_in_provisioning) {
6822 wpas_group_formation_completed(wpa_s, 0);
6825 wpas_p2p_group_delete(wpa_s,
6826 P2P_GROUP_REMOVAL_REQUESTED);
6828 } else if (wpa_s->p2p_in_invitation) {
6829 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6832 wpas_p2p_group_formation_failed(wpa_s);
6837 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6845 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6847 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6850 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6851 "being available anymore");
6852 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
6856 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6857 int freq_24, int freq_5, int freq_overall)
6859 struct p2p_data *p2p = wpa_s->global->p2p;
6862 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6866 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6869 struct p2p_data *p2p = wpa_s->global->p2p;
6874 if (hwaddr_aton(addr, peer))
6877 return p2p_unauthorize(p2p, peer);
6882 * wpas_p2p_disconnect - Disconnect from a P2P Group
6883 * @wpa_s: Pointer to wpa_supplicant data
6884 * Returns: 0 on success, -1 on failure
6886 * This can be used to disconnect from a group in which the local end is a P2P
6887 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6888 * virtual network interface was created for this group, that interface will be
6889 * removed. Otherwise, only the configured P2P group network will be removed
6890 * from the interface.
6892 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6898 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6903 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6907 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6910 ret = p2p_in_progress(wpa_s->global->p2p);
6913 * Check whether there is an ongoing WPS provisioning step (or
6914 * other parts of group formation) on another interface since
6915 * p2p_in_progress() does not report this to avoid issues for
6916 * scans during such provisioning step.
6918 if (wpa_s->global->p2p_group_formation &&
6919 wpa_s->global->p2p_group_formation != wpa_s) {
6920 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6921 "in group formation",
6922 wpa_s->global->p2p_group_formation->ifname);
6927 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6928 struct os_reltime now;
6929 os_get_reltime(&now);
6930 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6931 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
6932 /* Wait for the first client has expired */
6933 wpa_s->global->p2p_go_wait_client.sec = 0;
6935 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6944 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6945 struct wpa_ssid *ssid)
6947 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6948 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6949 wpa_s->parent, NULL) > 0) {
6951 * Remove the network by scheduling the group formation
6952 * timeout to happen immediately. The teardown code
6953 * needs to be scheduled to run asynch later so that we
6954 * don't delete data from under ourselves unexpectedly.
6955 * Calling wpas_p2p_group_formation_timeout directly
6956 * causes a series of crashes in WPS failure scenarios.
6958 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6959 "P2P group network getting removed");
6960 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6961 wpa_s->parent, NULL);
6966 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
6967 const u8 *addr, const u8 *ssid,
6973 for (s = wpa_s->conf->ssid; s; s = s->next) {
6974 if (s->disabled != 2)
6977 (ssid_len != s->ssid_len ||
6978 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6981 if (s->mode == WPAS_MODE_P2P_GO)
6985 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6986 return s; /* peer is GO in the persistent group */
6987 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6989 for (i = 0; i < s->num_p2p_clients; i++) {
6990 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6991 addr, ETH_ALEN) == 0)
6992 return s; /* peer is P2P client in persistent
7001 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7004 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7005 wpa_s->parent, NULL) > 0) {
7007 * This can happen if WPS provisioning step is not terminated
7008 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7009 * peer was able to connect, there is no need to time out group
7010 * formation after this, though. In addition, this is used with
7011 * the initial connection wait on the GO as a separate formation
7012 * timeout and as such, expected to be hit after the initial WPS
7013 * provisioning step.
7015 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
7017 if (!wpa_s->p2p_go_group_formation_completed &&
7018 !wpa_s->group_formation_reported) {
7020 * GO has not yet notified group formation success since
7021 * the WPS step was not completed cleanly. Do that
7022 * notification now since the P2P Client was able to
7023 * connect and as such, must have received the
7024 * credential from the WPS step.
7026 if (wpa_s->global->p2p)
7027 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7028 wpas_group_formation_completed(wpa_s, 1);
7031 if (!wpa_s->p2p_go_group_formation_completed) {
7032 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7033 wpa_s->p2p_go_group_formation_completed = 1;
7034 wpa_s->global->p2p_group_formation = NULL;
7035 wpa_s->p2p_in_provisioning = 0;
7036 wpa_s->p2p_in_invitation = 0;
7038 wpa_s->global->p2p_go_wait_client.sec = 0;
7041 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7045 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7048 struct wpa_supplicant *group = wpa_s;
7049 if (wpa_s->global->p2p_group_formation)
7050 group = wpa_s->global->p2p_group_formation;
7051 wpa_s = wpa_s->parent;
7052 offchannel_send_action_done(wpa_s);
7054 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
7055 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7056 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7057 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7058 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7059 wpa_s->p2p_persistent_id,
7060 wpa_s->p2p_pd_before_go_neg,
7066 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7068 if (!wpa_s->p2p_fallback_to_go_neg ||
7069 wpa_s->p2p_in_provisioning <= 5)
7072 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7073 return 0; /* peer operating as a GO */
7075 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7076 "fallback to GO Negotiation");
7077 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
7083 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7085 struct wpa_supplicant *ifs;
7087 if (wpa_s->wpa_state > WPA_SCANNING) {
7088 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7089 "concurrent operation",
7090 wpa_s->conf->p2p_search_delay);
7091 return wpa_s->conf->p2p_search_delay;
7094 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7096 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
7097 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7098 "delay due to concurrent operation on "
7100 wpa_s->conf->p2p_search_delay,
7102 return wpa_s->conf->p2p_search_delay;
7110 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7111 struct wpa_ssid *s, const u8 *addr,
7114 struct psk_list_entry *psk, *tmp;
7117 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7119 if ((iface_addr && !psk->p2p &&
7120 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7121 (!iface_addr && psk->p2p &&
7122 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7123 wpa_dbg(wpa_s, MSG_DEBUG,
7124 "P2P: Remove persistent group PSK list entry for "
7126 MAC2STR(psk->addr), psk->p2p);
7127 dl_list_del(&psk->list);
7137 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7138 const u8 *p2p_dev_addr,
7139 const u8 *psk, size_t psk_len)
7141 struct wpa_ssid *ssid = wpa_s->current_ssid;
7142 struct wpa_ssid *persistent;
7143 struct psk_list_entry *p, *last;
7145 if (psk_len != sizeof(p->psk))
7149 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7150 " p2p_dev_addr=" MACSTR,
7151 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7152 if (is_zero_ether_addr(p2p_dev_addr))
7153 p2p_dev_addr = NULL;
7155 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7159 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7160 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7161 /* To be added to persistent group once created */
7162 if (wpa_s->global->add_psk == NULL) {
7163 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7164 if (wpa_s->global->add_psk == NULL)
7167 p = wpa_s->global->add_psk;
7170 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7173 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7175 os_memcpy(p->psk, psk, psk_len);
7179 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7180 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7184 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7187 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7191 p = os_zalloc(sizeof(*p));
7196 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7199 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7201 os_memcpy(p->psk, psk, psk_len);
7203 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7204 (last = dl_list_last(&persistent->psk_list,
7205 struct psk_list_entry, list))) {
7206 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7207 MACSTR " (p2p=%u) to make room for a new one",
7208 MAC2STR(last->addr), last->p2p);
7209 dl_list_del(&last->list);
7213 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7214 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7215 p2p_dev_addr == NULL);
7217 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7218 MACSTR, MAC2STR(p2p_dev_addr));
7220 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7223 dl_list_add(&persistent->psk_list, &p->list);
7225 if (wpa_s->parent->conf->update_config &&
7226 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7227 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
7231 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7232 struct wpa_ssid *s, const u8 *addr,
7237 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
7238 if (res > 0 && wpa_s->conf->update_config &&
7239 wpa_config_write(wpa_s->confname, wpa_s->conf))
7240 wpa_dbg(wpa_s, MSG_DEBUG,
7241 "P2P: Failed to update configuration");
7245 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7246 const u8 *peer, int iface_addr)
7248 struct hostapd_data *hapd;
7249 struct hostapd_wpa_psk *psk, *prev, *rem;
7250 struct sta_info *sta;
7252 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7253 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7256 /* Remove per-station PSK entry */
7257 hapd = wpa_s->ap_iface->bss[0];
7259 psk = hapd->conf->ssid.wpa_psk;
7261 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7263 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7264 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7265 MACSTR " iface_addr=%d",
7266 MAC2STR(peer), iface_addr);
7268 prev->next = psk->next;
7270 hapd->conf->ssid.wpa_psk = psk->next;
7280 /* Disconnect from group */
7282 sta = ap_get_sta(hapd, peer);
7284 sta = ap_get_sta_p2p(hapd, peer);
7286 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7287 " (iface_addr=%d) from group",
7288 MAC2STR(peer), iface_addr);
7289 hostapd_drv_sta_deauth(hapd, sta->addr,
7290 WLAN_REASON_DEAUTH_LEAVING);
7291 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7296 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7300 struct wpa_supplicant *w;
7302 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7304 /* Remove from any persistent group */
7305 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
7306 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7309 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
7310 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
7313 /* Remove from any operating group */
7314 for (w = wpa_s->global->ifaces; w; w = w->next)
7315 wpas_p2p_remove_client_go(w, peer, iface_addr);
7319 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7321 struct wpa_supplicant *wpa_s = eloop_ctx;
7322 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7326 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7328 struct wpa_supplicant *wpa_s = eloop_ctx;
7330 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7331 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7335 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7336 struct wpa_ssid *ssid)
7338 struct wpa_supplicant *iface;
7340 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7341 if (!iface->current_ssid ||
7342 iface->current_ssid->frequency == freq ||
7343 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7344 !iface->current_ssid->p2p_group))
7347 /* Remove the connection with least priority */
7348 if (!wpas_is_p2p_prioritized(iface)) {
7349 /* STA connection has priority over existing
7350 * P2P connection, so remove the interface. */
7351 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7352 eloop_register_timeout(0, 0,
7353 wpas_p2p_group_freq_conflict,
7355 /* If connection in progress is P2P connection, do not
7356 * proceed for the connection. */
7362 /* P2P connection has priority, disable the STA network
7364 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7366 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7367 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7368 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7370 /* If P2P connection is in progress, continue
7383 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7385 struct wpa_ssid *ssid = wpa_s->current_ssid;
7387 if (ssid == NULL || !ssid->p2p_group)
7390 if (wpa_s->p2p_last_4way_hs_fail &&
7391 wpa_s->p2p_last_4way_hs_fail == ssid) {
7392 u8 go_dev_addr[ETH_ALEN];
7393 struct wpa_ssid *persistent;
7395 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7397 ssid->ssid_len) <= 0) {
7398 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7402 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7403 MACSTR, MAC2STR(go_dev_addr));
7404 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7407 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7408 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7411 wpa_msg_global(wpa_s->parent, MSG_INFO,
7412 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7415 wpa_s->p2p_last_4way_hs_fail = NULL;
7417 * Remove the group from a timeout to avoid issues with caller
7418 * continuing to use the interface if this is on a P2P group
7421 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7426 wpa_s->p2p_last_4way_hs_fail = ssid;
7431 #ifdef CONFIG_WPS_NFC
7433 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7441 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7445 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7446 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7453 wpabuf_put_be16(ret, wsc_len);
7455 wpabuf_put_buf(ret, wsc);
7456 wpabuf_put_be16(ret, wpabuf_len(p2p));
7457 wpabuf_put_buf(ret, p2p);
7461 wpa_hexdump_buf(MSG_DEBUG,
7462 "P2P: Generated NFC connection handover message", ret);
7466 tmp = ndef_build_p2p(ret);
7469 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7479 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7480 struct wpa_ssid **ssid, u8 *go_dev_addr)
7482 struct wpa_supplicant *iface;
7485 os_memset(go_dev_addr, 0, ETH_ALEN);
7488 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7489 if (iface->wpa_state < WPA_ASSOCIATING ||
7490 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7491 !iface->current_ssid->p2p_group ||
7492 iface->current_ssid->mode != WPAS_MODE_INFRA)
7495 *ssid = iface->current_ssid;
7497 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7498 return iface->assoc_freq;
7504 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7507 struct wpabuf *wsc, *p2p;
7508 struct wpa_ssid *ssid;
7509 u8 go_dev_addr[ETH_ALEN];
7510 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7512 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7513 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7517 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7518 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7519 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7520 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7524 if (cli_freq == 0) {
7525 wsc = wps_build_nfc_handover_req_p2p(
7526 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7529 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7530 go_dev_addr, ssid ? ssid->ssid : NULL,
7531 ssid ? ssid->ssid_len : 0);
7533 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7537 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7540 struct wpabuf *wsc, *p2p;
7541 struct wpa_ssid *ssid;
7542 u8 go_dev_addr[ETH_ALEN];
7543 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7545 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7548 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7549 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7550 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7553 if (cli_freq == 0) {
7554 wsc = wps_build_nfc_handover_sel_p2p(
7556 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7557 DEV_PW_NFC_CONNECTION_HANDOVER,
7558 wpa_s->conf->wps_nfc_dh_pubkey,
7559 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7562 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7563 go_dev_addr, ssid ? ssid->ssid : NULL,
7564 ssid ? ssid->ssid_len : 0);
7566 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7570 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7571 struct p2p_nfc_params *params)
7573 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7574 "connection handover (freq=%d)",
7577 if (params->go_freq && params->go_ssid_len) {
7578 wpa_s->p2p_wps_method = WPS_NFC;
7579 wpa_s->pending_join_wps_method = WPS_NFC;
7580 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7581 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7583 return wpas_p2p_join_start(wpa_s, params->go_freq,
7585 params->go_ssid_len);
7588 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7589 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7590 params->go_freq, -1, 0, 1, 1);
7594 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7595 struct p2p_nfc_params *params, int tag)
7597 int res, persistent;
7598 struct wpa_ssid *ssid;
7600 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7601 "connection handover");
7602 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7603 ssid = wpa_s->current_ssid;
7606 if (ssid->mode != WPAS_MODE_P2P_GO)
7608 if (wpa_s->ap_iface == NULL)
7612 if (wpa_s == NULL) {
7613 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7617 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7618 DEV_PW_NFC_CONNECTION_HANDOVER &&
7619 !wpa_s->parent->p2p_oob_dev_pw) {
7620 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7623 res = wpas_ap_wps_add_nfc_pw(
7624 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7625 wpa_s->parent->p2p_oob_dev_pw,
7626 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7627 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7632 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7636 if (!params->peer ||
7637 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7640 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7641 " to join", MAC2STR(params->peer->p2p_device_addr));
7643 wpa_s->global->p2p_invite_group = wpa_s;
7644 persistent = ssid->p2p_persistent_group &&
7645 wpas_p2p_get_persistent(wpa_s->parent,
7646 params->peer->p2p_device_addr,
7647 ssid->ssid, ssid->ssid_len);
7648 wpa_s->parent->pending_invite_ssid_id = -1;
7650 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7651 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7652 ssid->ssid, ssid->ssid_len, ssid->frequency,
7653 wpa_s->global->p2p_dev_addr, persistent, 0,
7654 wpa_s->parent->p2p_oob_dev_pw_id);
7658 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7659 struct p2p_nfc_params *params,
7662 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7663 "connection handover");
7664 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7665 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7666 forced_freq, -1, 0, 1, 1);
7670 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7671 struct p2p_nfc_params *params,
7676 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7677 "connection handover");
7678 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7679 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7680 forced_freq, -1, 0, 1, 1);
7684 res = wpas_p2p_listen(wpa_s, 60);
7686 p2p_unauthorize(wpa_s->global->p2p,
7687 params->peer->p2p_device_addr);
7694 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7695 const struct wpabuf *data,
7696 int sel, int tag, int forced_freq)
7698 const u8 *pos, *end;
7700 struct p2p_nfc_params params;
7703 os_memset(¶ms, 0, sizeof(params));
7706 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7708 pos = wpabuf_head(data);
7709 end = pos + wpabuf_len(data);
7711 if (end - pos < 2) {
7712 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7716 len = WPA_GET_BE16(pos);
7718 if (len > end - pos) {
7719 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7723 params.wsc_attr = pos;
7724 params.wsc_len = len;
7727 if (end - pos < 2) {
7728 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7732 len = WPA_GET_BE16(pos);
7734 if (len > end - pos) {
7735 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7739 params.p2p_attr = pos;
7740 params.p2p_len = len;
7743 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7744 params.wsc_attr, params.wsc_len);
7745 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7746 params.p2p_attr, params.p2p_len);
7748 wpa_hexdump(MSG_DEBUG,
7749 "P2P: Ignored extra data after P2P attributes",
7753 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
7757 if (params.next_step == NO_ACTION)
7760 if (params.next_step == BOTH_GO) {
7761 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7762 MAC2STR(params.peer->p2p_device_addr));
7766 if (params.next_step == PEER_CLIENT) {
7767 if (!is_zero_ether_addr(params.go_dev_addr)) {
7768 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7769 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7771 MAC2STR(params.peer->p2p_device_addr),
7773 MAC2STR(params.go_dev_addr),
7774 wpa_ssid_txt(params.go_ssid,
7775 params.go_ssid_len));
7777 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7778 "peer=" MACSTR " freq=%d",
7779 MAC2STR(params.peer->p2p_device_addr),
7785 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7786 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7787 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7791 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7792 wpa_s->p2p_oob_dev_pw = NULL;
7794 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7795 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7800 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7801 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7802 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7803 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7804 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7805 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7806 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7810 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7811 "peer OOB Device Password Id %u", id);
7814 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7815 "Device Password Id %u", id);
7816 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7817 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7818 params.oob_dev_pw_len -
7819 WPS_OOB_PUBKEY_HASH_LEN - 2);
7820 wpa_s->p2p_oob_dev_pw_id = id;
7821 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7822 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7823 params.oob_dev_pw_len -
7824 WPS_OOB_PUBKEY_HASH_LEN - 2);
7825 if (wpa_s->p2p_oob_dev_pw == NULL)
7828 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7829 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7830 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7833 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7834 "without Device Password");
7835 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7838 switch (params.next_step) {
7842 /* already covered above */
7845 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
7847 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
7849 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
7851 /* TODO: use own OOB Dev Pw */
7852 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
7859 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7860 const struct wpabuf *data, int forced_freq)
7862 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7865 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7869 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7870 const struct wpabuf *req,
7871 const struct wpabuf *sel, int forced_freq)
7876 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7879 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7881 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7882 wpabuf_head(req), wpabuf_len(req));
7883 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7884 wpabuf_head(sel), wpabuf_len(sel));
7886 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7887 tmp = ndef_parse_p2p(init ? sel : req);
7889 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7893 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7901 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7904 int go_intent = wpa_s->conf->p2p_go_intent;
7905 struct wpa_supplicant *iface;
7907 if (wpa_s->global->p2p == NULL)
7911 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7912 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7914 if (!iface->ap_iface)
7916 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7918 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7920 if (wpa_s->p2p_nfc_tag_enabled)
7921 wpas_p2p_remove_pending_group_interface(wpa_s);
7922 wpa_s->p2p_nfc_tag_enabled = 0;
7926 if (wpa_s->global->p2p_disabled)
7929 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7930 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7931 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7932 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7933 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7934 "to allow static handover cases");
7938 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7940 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7941 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7942 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7943 if (wpa_s->p2p_oob_dev_pw == NULL)
7945 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7947 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
7948 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
7950 * P2P Group Interface present and the command came on group
7951 * interface, so enable the token for the current interface.
7953 wpa_s->create_p2p_iface = 0;
7955 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7958 if (wpa_s->create_p2p_iface) {
7959 enum wpa_driver_if_type iftype;
7960 /* Prepare to add a new interface for the group */
7961 iftype = WPA_IF_P2P_GROUP;
7962 if (go_intent == 15)
7963 iftype = WPA_IF_P2P_GO;
7964 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7965 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7966 "interface for the group");
7970 if_addr = wpa_s->pending_interface_addr;
7972 if_addr = wpa_s->own_addr;
7974 wpa_s->p2p_nfc_tag_enabled = enabled;
7976 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7977 struct hostapd_data *hapd;
7978 if (iface->ap_iface == NULL)
7980 hapd = iface->ap_iface->bss[0];
7981 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7982 hapd->conf->wps_nfc_dh_pubkey =
7983 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7984 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7985 hapd->conf->wps_nfc_dh_privkey =
7986 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7987 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7988 hapd->conf->wps_nfc_dev_pw =
7989 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7990 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7992 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7993 wpa_dbg(iface, MSG_DEBUG,
7994 "P2P: Failed to enable NFC Tag for GO");
7997 p2p_set_authorized_oob_dev_pw_id(
7998 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8004 #endif /* CONFIG_WPS_NFC */
8007 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8008 struct wpa_used_freq_data *freqs,
8011 u8 curr_chan, cand, chan;
8014 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8015 for (i = 0, cand = 0; i < num; i++) {
8016 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8017 if (curr_chan == chan) {
8022 if (chan == 1 || chan == 6 || chan == 11)
8027 wpa_dbg(wpa_s, MSG_DEBUG,
8028 "P2P: Update Listen channel to %u baased on operating channel",
8030 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8035 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8037 struct wpa_used_freq_data *freqs;
8038 unsigned int num = wpa_s->num_multichan_concurrent;
8040 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8044 * If possible, optimize the Listen channel to be a channel that is
8045 * already used by one of the other interfaces.
8047 if (!wpa_s->conf->p2p_optimize_listen_chan)
8050 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8053 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8057 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8059 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
8064 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8066 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8067 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8068 "the management interface is being removed");
8069 wpas_p2p_deinit_global(wpa_s->global);
8074 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8076 if (wpa_s->ap_iface->bss)
8077 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8078 wpas_p2p_group_deinit(wpa_s);