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);
272 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
276 p2p_notify_scan_trigger_status(wpa_s->global->p2p, ret);
277 os_get_reltime(&wpa_s->scan_trigger_time);
278 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
279 wpa_s->own_scan_requested = 1;
280 wpa_s->p2p_scan_work = work;
284 static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
287 if (wpa_s->global->p2p_24ghz_social_channels &&
288 (freq == 2412 || freq == 2437 || freq == 2462)) {
290 * Search all social channels regardless of whether these have
291 * been disabled for P2P operating channel use to avoid missing
296 return p2p_supported_freq(wpa_s->global->p2p, freq);
300 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
301 unsigned int num_req_dev_types,
302 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
304 struct wpa_supplicant *wpa_s = ctx;
305 struct wpa_driver_scan_params *params = NULL;
306 struct wpabuf *wps_ie, *ies;
307 unsigned int num_channels = 0;
308 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
312 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
315 if (wpa_s->p2p_scan_work) {
316 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
320 params = os_zalloc(sizeof(*params));
324 /* P2P Wildcard SSID */
325 params->num_ssids = 1;
326 n = os_malloc(P2P_WILDCARD_SSID_LEN);
329 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
330 params->ssids[0].ssid = n;
331 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
333 wpa_s->wps->dev.p2p = 1;
334 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
335 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
336 num_req_dev_types, req_dev_types);
340 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
341 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
346 wpabuf_put_buf(ies, wps_ie);
349 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
351 params->p2p_probe = 1;
352 n = os_malloc(wpabuf_len(ies));
357 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
358 params->extra_ies = n;
359 params->extra_ies_len = wpabuf_len(ies);
363 case P2P_SCAN_SOCIAL:
364 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
366 if (params->freqs == NULL)
368 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
369 if (wpas_p2p_search_social_channel(
370 wpa_s, social_channels_freq[i]))
371 params->freqs[num_channels++] =
372 social_channels_freq[i];
374 params->freqs[num_channels++] = 0;
378 case P2P_SCAN_SOCIAL_PLUS_ONE:
379 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
381 if (params->freqs == NULL)
383 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
384 if (wpas_p2p_search_social_channel(
385 wpa_s, social_channels_freq[i]))
386 params->freqs[num_channels++] =
387 social_channels_freq[i];
389 if (p2p_supported_freq(wpa_s->global->p2p, freq))
390 params->freqs[num_channels++] = freq;
391 params->freqs[num_channels++] = 0;
395 radio_remove_works(wpa_s, "p2p-scan", 0);
396 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
402 wpa_scan_free_params(params);
407 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
409 switch (p2p_group_interface) {
410 case P2P_GROUP_INTERFACE_PENDING:
411 return WPA_IF_P2P_GROUP;
412 case P2P_GROUP_INTERFACE_GO:
413 return WPA_IF_P2P_GO;
414 case P2P_GROUP_INTERFACE_CLIENT:
415 return WPA_IF_P2P_CLIENT;
418 return WPA_IF_P2P_GROUP;
422 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
424 size_t ssid_len, int *go)
428 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
429 for (s = wpa_s->conf->ssid; s; s = s->next) {
430 if (s->disabled != 0 || !s->p2p_group ||
431 s->ssid_len != ssid_len ||
432 os_memcmp(ssid, s->ssid, ssid_len) != 0)
434 if (s->mode == WPAS_MODE_P2P_GO &&
435 s != wpa_s->current_ssid)
438 *go = s->mode == WPAS_MODE_P2P_GO;
447 static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
449 struct wpa_supplicant *wpa_s = eloop_ctx;
450 wpa_printf(MSG_DEBUG,
451 "P2P: Complete previously requested removal of %s",
453 wpas_p2p_disconnect(wpa_s);
457 static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
458 struct wpa_supplicant *calling_wpa_s)
460 if (calling_wpa_s == wpa_s && wpa_s &&
461 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
463 * The calling wpa_s instance is going to be removed. Do that
464 * from an eloop callback to keep the instance available until
465 * the caller has returned. This my be needed, e.g., to provide
466 * control interface responses on the per-interface socket.
468 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
474 return wpas_p2p_disconnect(wpa_s);
478 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
479 enum p2p_group_removal_reason removal_reason)
481 struct wpa_ssid *ssid;
485 ssid = wpa_s->current_ssid;
488 * The current SSID was not known, but there may still be a
489 * pending P2P group interface waiting for provisioning or a
490 * P2P group that is trying to reconnect.
492 ssid = wpa_s->conf->ssid;
494 if (ssid->p2p_group && ssid->disabled != 2)
499 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
501 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
506 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
508 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
509 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
510 wpa_s->reassociate = 0;
511 wpa_s->disconnected = 1;
516 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
517 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
519 if (os_strcmp(gtype, "client") == 0) {
520 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
521 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
523 wpa_printf(MSG_DEBUG,
524 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
525 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
526 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
531 if (wpa_s->cross_connect_in_use) {
532 wpa_s->cross_connect_in_use = 0;
533 wpa_msg_global(wpa_s->parent, MSG_INFO,
534 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
535 wpa_s->ifname, wpa_s->cross_connect_uplink);
537 switch (removal_reason) {
538 case P2P_GROUP_REMOVAL_REQUESTED:
539 reason = " reason=REQUESTED";
541 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
542 reason = " reason=FORMATION_FAILED";
544 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
545 reason = " reason=IDLE";
547 case P2P_GROUP_REMOVAL_UNAVAILABLE:
548 reason = " reason=UNAVAILABLE";
550 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
551 reason = " reason=GO_ENDING_SESSION";
553 case P2P_GROUP_REMOVAL_PSK_FAILURE:
554 reason = " reason=PSK_FAILURE";
556 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
557 reason = " reason=FREQ_CONFLICT";
563 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
564 wpa_msg_global(wpa_s->parent, MSG_INFO,
565 P2P_EVENT_GROUP_REMOVED "%s %s%s",
566 wpa_s->ifname, gtype, reason);
569 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
570 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
571 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
572 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
573 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
574 wpa_s->parent, NULL) > 0) {
575 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
577 wpa_s->p2p_in_provisioning = 0;
580 wpa_s->p2p_in_invitation = 0;
583 * Make sure wait for the first client does not remain active after the
584 * group has been removed.
586 wpa_s->global->p2p_go_wait_client.sec = 0;
588 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
589 struct wpa_global *global;
591 enum wpa_driver_if_type type;
592 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
594 global = wpa_s->global;
595 ifname = os_strdup(wpa_s->ifname);
596 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
597 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
598 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
599 wpa_s = global->ifaces;
601 wpa_drv_if_remove(wpa_s, type, ifname);
606 if (!wpa_s->p2p_go_group_formation_completed) {
607 wpa_s->global->p2p_group_formation = NULL;
608 wpa_s->p2p_in_provisioning = 0;
611 wpa_s->show_group_started = 0;
612 os_free(wpa_s->go_params);
613 wpa_s->go_params = NULL;
615 os_free(wpa_s->p2p_group_common_freqs);
616 wpa_s->p2p_group_common_freqs = NULL;
617 wpa_s->p2p_group_common_freqs_num = 0;
619 wpa_s->waiting_presence_resp = 0;
621 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
622 if (ssid && (ssid->p2p_group ||
623 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
624 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
626 if (ssid == wpa_s->current_ssid) {
627 wpa_sm_set_config(wpa_s->wpa, NULL);
628 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
629 wpa_s->current_ssid = NULL;
632 * Networks objects created during any P2P activities are not
633 * exposed out as they might/will confuse certain non-P2P aware
634 * applications since these network objects won't behave like
637 * Likewise, we don't send out network removed signals for such
640 wpa_config_remove_network(wpa_s->conf, id);
641 wpa_supplicant_clear_status(wpa_s);
642 wpa_supplicant_cancel_sched_scan(wpa_s);
644 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
648 wpa_supplicant_ap_deinit(wpa_s);
650 wpa_drv_deinit_p2p_cli(wpa_s);
656 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
658 const u8 *ssid, size_t ssid_len)
666 if (wpa_s->go_params)
667 bssid = wpa_s->go_params->peer_interface_addr;
669 bssid = wpa_s->bssid;
671 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
672 if (bss == NULL && wpa_s->go_params &&
673 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
674 bss = wpa_bss_get_p2p_dev_addr(
675 wpa_s, wpa_s->go_params->peer_device_addr);
677 u8 iface_addr[ETH_ALEN];
678 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
680 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
683 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
684 "group is persistent - BSS " MACSTR " not found",
689 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
691 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
694 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
695 "group is persistent - BSS " MACSTR
696 " did not include P2P IE", MAC2STR(bssid));
697 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
698 (u8 *) (bss + 1), bss->ie_len);
699 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
700 ((u8 *) bss + 1) + bss->ie_len,
705 group_capab = p2p_get_group_capab(p2p);
706 addr = p2p_get_go_dev_addr(p2p);
707 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
708 "group_capab=0x%x", group_capab);
710 os_memcpy(go_dev_addr, addr, ETH_ALEN);
711 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
714 os_memset(go_dev_addr, 0, ETH_ALEN);
717 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
718 "go_dev_addr=" MACSTR,
719 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
721 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
725 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
726 struct wpa_ssid *ssid,
727 const u8 *go_dev_addr)
732 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
733 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
734 for (s = wpa_s->conf->ssid; s; s = s->next) {
735 if (s->disabled == 2 &&
736 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
737 s->ssid_len == ssid->ssid_len &&
738 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
743 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
745 if (ssid->passphrase && !s->passphrase)
747 else if (ssid->passphrase && s->passphrase &&
748 os_strcmp(ssid->passphrase, s->passphrase) != 0)
751 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
754 s = wpa_config_add_network(wpa_s->conf);
759 * Instead of network_added we emit persistent_group_added
760 * notification. Also to keep the defense checks in
761 * persistent_group obj registration method, we set the
762 * relevant flags in s to designate it as a persistent group.
765 s->p2p_persistent_group = 1;
766 wpas_notify_persistent_group_added(wpa_s, s);
767 wpa_config_set_network_defaults(s);
771 s->p2p_persistent_group = 1;
774 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
775 s->mode = ssid->mode;
776 s->auth_alg = WPA_AUTH_ALG_OPEN;
777 s->key_mgmt = WPA_KEY_MGMT_PSK;
778 s->proto = WPA_PROTO_RSN;
779 s->pairwise_cipher = WPA_CIPHER_CCMP;
781 if (ssid->passphrase) {
782 os_free(s->passphrase);
783 s->passphrase = os_strdup(ssid->passphrase);
787 os_memcpy(s->psk, ssid->psk, 32);
789 if (s->passphrase && !s->psk_set)
790 wpa_config_update_psk(s);
791 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
793 s->ssid = os_malloc(ssid->ssid_len);
796 s->ssid_len = ssid->ssid_len;
797 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
799 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
800 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
801 wpa_s->global->add_psk = NULL;
805 if (changed && wpa_s->conf->update_config &&
806 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
807 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
814 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
817 struct wpa_ssid *ssid, *s;
822 ssid = wpa_s->current_ssid;
823 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
824 !ssid->p2p_persistent_group)
827 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
828 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
831 if (s->ssid_len == ssid->ssid_len &&
832 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
839 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
840 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN, addr,
844 if (i == s->num_p2p_clients - 1)
845 return; /* already the most recent entry */
847 /* move the entry to mark it most recent */
848 os_memmove(s->p2p_client_list + i * 2 * ETH_ALEN,
849 s->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
850 (s->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
851 os_memcpy(s->p2p_client_list +
852 (s->num_p2p_clients - 1) * 2 * ETH_ALEN, addr,
854 os_memset(s->p2p_client_list +
855 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
861 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
862 n = os_realloc_array(s->p2p_client_list,
863 s->num_p2p_clients + 1, 2 * ETH_ALEN);
866 os_memcpy(n + s->num_p2p_clients * 2 * ETH_ALEN, addr,
868 os_memset(n + s->num_p2p_clients * 2 * ETH_ALEN + ETH_ALEN,
870 s->p2p_client_list = n;
871 s->num_p2p_clients++;
872 } else if (!found && s->p2p_client_list) {
873 /* Not enough room for an additional entry - drop the oldest
875 os_memmove(s->p2p_client_list,
876 s->p2p_client_list + 2 * ETH_ALEN,
877 (s->num_p2p_clients - 1) * 2 * ETH_ALEN);
878 os_memcpy(s->p2p_client_list +
879 (s->num_p2p_clients - 1) * 2 * ETH_ALEN,
881 os_memset(s->p2p_client_list +
882 (s->num_p2p_clients - 1) * 2 * ETH_ALEN + ETH_ALEN,
886 if (wpa_s->parent->conf->update_config &&
887 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
888 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
892 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
893 int go, struct wpa_ssid *ssid, int freq,
894 const u8 *psk, const char *passphrase,
895 const u8 *go_dev_addr, int persistent,
898 const char *ssid_txt;
902 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
907 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
911 if (passphrase && passphrase[0] == '\0')
915 * Include PSK/passphrase only in the control interface message and
916 * leave it out from the debug log entry.
918 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
919 P2P_EVENT_GROUP_STARTED
920 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
922 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
923 psk ? " psk=" : "", psk_txt,
924 passphrase ? " passphrase=\"" : "",
925 passphrase ? passphrase : "",
926 passphrase ? "\"" : "",
927 MAC2STR(go_dev_addr),
928 persistent ? " [PERSISTENT]" : "", extra);
929 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
930 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
931 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
932 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
937 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
940 struct wpa_ssid *ssid;
943 u8 go_dev_addr[ETH_ALEN];
947 * This callback is likely called for the main interface. Update wpa_s
948 * to use the group interface if a new interface was created for the
951 if (wpa_s->global->p2p_group_formation)
952 wpa_s = wpa_s->global->p2p_group_formation;
953 if (wpa_s->p2p_go_group_formation_completed) {
954 wpa_s->global->p2p_group_formation = NULL;
955 wpa_s->p2p_in_provisioning = 0;
957 wpa_s->p2p_in_invitation = 0;
958 wpa_s->group_formation_reported = 1;
961 wpa_msg_global(wpa_s->parent, MSG_INFO,
962 P2P_EVENT_GROUP_FORMATION_FAILURE);
963 wpas_p2p_group_delete(wpa_s,
964 P2P_GROUP_REMOVAL_FORMATION_FAILED);
968 wpa_msg_global(wpa_s->parent, MSG_INFO,
969 P2P_EVENT_GROUP_FORMATION_SUCCESS);
971 ssid = wpa_s->current_ssid;
972 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
973 ssid->mode = WPAS_MODE_P2P_GO;
974 p2p_group_notif_formation_done(wpa_s->p2p_group);
975 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
980 client = ssid->mode == WPAS_MODE_INFRA;
981 if (ssid->mode == WPAS_MODE_P2P_GO) {
982 persistent = ssid->p2p_persistent_group;
983 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
986 persistent = wpas_p2p_persistent_group(wpa_s,
991 client = wpa_s->p2p_group_interface ==
992 P2P_GROUP_INTERFACE_CLIENT;
993 os_memset(go_dev_addr, 0, ETH_ALEN);
996 wpa_s->show_group_started = 0;
999 * Indicate event only after successfully completed 4-way
1000 * handshake, i.e., when the interface is ready for data
1003 wpa_s->show_group_started = 1;
1005 wpas_p2p_group_started(wpa_s, 1, ssid,
1006 ssid ? ssid->frequency : 0,
1007 ssid && ssid->passphrase == NULL &&
1008 ssid->psk_set ? ssid->psk : NULL,
1009 ssid ? ssid->passphrase : NULL,
1010 go_dev_addr, persistent, "");
1011 wpas_p2p_cross_connect_setup(wpa_s);
1012 wpas_p2p_set_group_idle_timeout(wpa_s);
1016 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1019 os_free(wpa_s->global->add_psk);
1020 wpa_s->global->add_psk = NULL;
1022 if (network_id < 0 && ssid)
1023 network_id = ssid->id;
1025 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1026 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1031 struct send_action_work {
1037 unsigned int wait_time;
1042 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1045 struct wpa_supplicant *wpa_s = eloop_ctx;
1047 if (!wpa_s->p2p_send_action_work)
1050 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1051 os_free(wpa_s->p2p_send_action_work->ctx);
1052 radio_work_done(wpa_s->p2p_send_action_work);
1053 wpa_s->p2p_send_action_work = NULL;
1057 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1059 if (wpa_s->p2p_send_action_work) {
1060 struct send_action_work *awork;
1061 awork = wpa_s->p2p_send_action_work->ctx;
1062 if (awork->wait_time == 0) {
1064 radio_work_done(wpa_s->p2p_send_action_work);
1065 wpa_s->p2p_send_action_work = NULL;
1068 * In theory, this should not be needed, but number of
1069 * places in the P2P code is still using non-zero wait
1070 * time for the last Action frame in the sequence and
1071 * some of these do not call send_action_done().
1073 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1075 eloop_register_timeout(
1076 0, awork->wait_time * 1000,
1077 wpas_p2p_send_action_work_timeout,
1084 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1086 const u8 *dst, const u8 *src,
1088 const u8 *data, size_t data_len,
1089 enum offchannel_send_action_result
1092 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1094 wpas_p2p_action_tx_clear(wpa_s);
1096 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1100 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1101 res = P2P_SEND_ACTION_SUCCESS;
1103 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1104 res = P2P_SEND_ACTION_NO_ACK;
1106 case OFFCHANNEL_SEND_ACTION_FAILED:
1107 res = P2P_SEND_ACTION_FAILED;
1111 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1113 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1114 wpa_s->pending_pd_before_join &&
1115 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1116 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1117 wpa_s->p2p_fallback_to_go_neg) {
1118 wpa_s->pending_pd_before_join = 0;
1119 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1120 "during p2p_connect-auto");
1121 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1127 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1129 struct wpa_supplicant *wpa_s = work->wpa_s;
1130 struct send_action_work *awork = work->ctx;
1133 if (work->started) {
1134 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1136 wpa_s->p2p_send_action_work = NULL;
1137 offchannel_send_action_done(wpa_s);
1143 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1144 awork->bssid, awork->buf, awork->len,
1146 wpas_p2p_send_action_tx_status, 1) < 0) {
1148 radio_work_done(work);
1151 wpa_s->p2p_send_action_work = work;
1155 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1156 unsigned int freq, const u8 *dst,
1157 const u8 *src, const u8 *bssid, const u8 *buf,
1158 size_t len, unsigned int wait_time)
1160 struct send_action_work *awork;
1162 if (wpa_s->p2p_send_action_work) {
1163 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1167 awork = os_zalloc(sizeof(*awork) + len);
1172 os_memcpy(awork->dst, dst, ETH_ALEN);
1173 os_memcpy(awork->src, src, ETH_ALEN);
1174 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1176 awork->wait_time = wait_time;
1177 os_memcpy(awork->buf, buf, len);
1179 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1180 wpas_send_action_cb, awork) < 0) {
1189 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1190 const u8 *src, const u8 *bssid, const u8 *buf,
1191 size_t len, unsigned int wait_time)
1193 struct wpa_supplicant *wpa_s = ctx;
1194 int listen_freq = -1, send_freq = -1;
1196 if (wpa_s->p2p_listen_work)
1197 listen_freq = wpa_s->p2p_listen_work->freq;
1198 if (wpa_s->p2p_send_action_work)
1199 send_freq = wpa_s->p2p_send_action_work->freq;
1200 if (listen_freq != (int) freq && send_freq != (int) freq) {
1201 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1202 listen_freq, send_freq);
1203 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1207 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1208 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1210 wpas_p2p_send_action_tx_status, 1);
1214 static void wpas_send_action_done(void *ctx)
1216 struct wpa_supplicant *wpa_s = ctx;
1218 if (wpa_s->p2p_send_action_work) {
1219 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1221 os_free(wpa_s->p2p_send_action_work->ctx);
1222 radio_work_done(wpa_s->p2p_send_action_work);
1223 wpa_s->p2p_send_action_work = NULL;
1226 offchannel_send_action_done(wpa_s);
1230 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1231 struct p2p_go_neg_results *params)
1233 if (wpa_s->go_params == NULL) {
1234 wpa_s->go_params = os_malloc(sizeof(*params));
1235 if (wpa_s->go_params == NULL)
1238 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1243 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1244 struct p2p_go_neg_results *res)
1246 wpa_s->group_formation_reported = 0;
1247 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1248 " dev_addr " MACSTR " wps_method %d",
1249 MAC2STR(res->peer_interface_addr),
1250 MAC2STR(res->peer_device_addr), res->wps_method);
1251 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1252 res->ssid, res->ssid_len);
1253 wpa_supplicant_ap_deinit(wpa_s);
1254 wpas_copy_go_neg_results(wpa_s, res);
1255 if (res->wps_method == WPS_PBC) {
1256 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1257 #ifdef CONFIG_WPS_NFC
1258 } else if (res->wps_method == WPS_NFC) {
1259 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1260 res->peer_interface_addr,
1261 wpa_s->parent->p2p_oob_dev_pw,
1262 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1263 wpa_s->parent->p2p_oob_dev_pw_id ==
1264 DEV_PW_NFC_CONNECTION_HANDOVER ?
1265 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1268 #endif /* CONFIG_WPS_NFC */
1270 u16 dev_pw_id = DEV_PW_DEFAULT;
1271 if (wpa_s->p2p_wps_method == WPS_P2PS)
1272 dev_pw_id = DEV_PW_P2PS_DEFAULT;
1273 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1274 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1275 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1276 wpa_s->p2p_pin, 1, dev_pw_id);
1281 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1282 struct wpa_ssid *ssid)
1284 struct wpa_ssid *persistent;
1285 struct psk_list_entry *psk;
1286 struct hostapd_data *hapd;
1288 if (!wpa_s->ap_iface)
1291 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1293 if (persistent == NULL)
1296 hapd = wpa_s->ap_iface->bss[0];
1298 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1300 struct hostapd_wpa_psk *hpsk;
1302 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1304 MAC2STR(psk->addr), psk->p2p);
1305 hpsk = os_zalloc(sizeof(*hpsk));
1308 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1310 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1312 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1313 hpsk->next = hapd->conf->ssid.wpa_psk;
1314 hapd->conf->ssid.wpa_psk = hpsk;
1319 static void p2p_go_dump_common_freqs(struct wpa_supplicant *wpa_s)
1323 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Common group frequencies (len=%u):",
1324 wpa_s->p2p_group_common_freqs_num);
1326 for (i = 0; i < wpa_s->p2p_group_common_freqs_num; i++)
1327 wpa_dbg(wpa_s, MSG_DEBUG, "freq[%u]: %d",
1328 i, wpa_s->p2p_group_common_freqs[i]);
1332 static void p2p_go_save_group_common_freqs(struct wpa_supplicant *wpa_s,
1333 struct p2p_go_neg_results *params)
1335 unsigned int i, len = int_array_len(wpa_s->go_params->freq_list);
1337 wpa_s->p2p_group_common_freqs_num = 0;
1338 os_free(wpa_s->p2p_group_common_freqs);
1339 wpa_s->p2p_group_common_freqs = os_calloc(len, sizeof(int));
1340 if (!wpa_s->p2p_group_common_freqs)
1343 for (i = 0; i < len; i++) {
1344 if (!wpa_s->go_params->freq_list[i])
1346 wpa_s->p2p_group_common_freqs[i] =
1347 wpa_s->go_params->freq_list[i];
1349 wpa_s->p2p_group_common_freqs_num = i;
1353 static void p2p_go_configured(void *ctx, void *data)
1355 struct wpa_supplicant *wpa_s = ctx;
1356 struct p2p_go_neg_results *params = data;
1357 struct wpa_ssid *ssid;
1358 int network_id = -1;
1360 p2p_go_save_group_common_freqs(wpa_s, params);
1361 p2p_go_dump_common_freqs(wpa_s);
1363 ssid = wpa_s->current_ssid;
1364 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1365 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1366 if (wpa_s->global->p2p_group_formation == wpa_s)
1367 wpa_s->global->p2p_group_formation = NULL;
1368 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1369 params->passphrase[0] == '\0' ?
1372 wpa_s->global->p2p_dev_addr,
1373 params->persistent_group, "");
1374 wpa_s->group_formation_reported = 1;
1376 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1377 if (params->persistent_group) {
1378 network_id = wpas_p2p_store_persistent_group(
1379 wpa_s->parent, ssid,
1380 wpa_s->global->p2p_dev_addr);
1381 wpas_p2p_add_psk_list(wpa_s, ssid);
1384 network_id = ssid->id;
1385 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1386 wpas_p2p_cross_connect_setup(wpa_s);
1387 wpas_p2p_set_group_idle_timeout(wpa_s);
1389 if (wpa_s->p2p_first_connection_timeout) {
1390 wpa_dbg(wpa_s, MSG_DEBUG,
1391 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1392 wpa_s->p2p_first_connection_timeout);
1393 wpa_s->p2p_go_group_formation_completed = 0;
1394 wpa_s->global->p2p_group_formation = wpa_s;
1395 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1396 wpa_s->parent, NULL);
1397 eloop_register_timeout(
1398 wpa_s->p2p_first_connection_timeout, 0,
1399 wpas_p2p_group_formation_timeout,
1400 wpa_s->parent, NULL);
1406 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1407 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1408 params->peer_interface_addr)) {
1409 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1413 if (params->wps_method == WPS_PBC) {
1414 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1415 params->peer_device_addr);
1416 #ifdef CONFIG_WPS_NFC
1417 } else if (params->wps_method == WPS_NFC) {
1418 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1419 DEV_PW_NFC_CONNECTION_HANDOVER &&
1420 !wpa_s->parent->p2p_oob_dev_pw) {
1421 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1424 wpas_ap_wps_add_nfc_pw(
1425 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1426 wpa_s->parent->p2p_oob_dev_pw,
1427 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1428 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1429 #endif /* CONFIG_WPS_NFC */
1430 } else if (wpa_s->p2p_pin[0])
1431 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1432 wpa_s->p2p_pin, NULL, 0, 0);
1433 os_free(wpa_s->go_params);
1434 wpa_s->go_params = NULL;
1438 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1439 struct p2p_go_neg_results *params,
1440 int group_formation)
1442 struct wpa_ssid *ssid;
1444 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1445 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1446 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1451 ssid = wpa_config_add_network(wpa_s->conf);
1453 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1457 wpa_s->show_group_started = 0;
1458 wpa_s->p2p_go_group_formation_completed = 0;
1459 wpa_s->group_formation_reported = 0;
1461 wpa_config_set_network_defaults(ssid);
1462 ssid->temporary = 1;
1463 ssid->p2p_group = 1;
1464 ssid->p2p_persistent_group = params->persistent_group;
1465 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1467 ssid->frequency = params->freq;
1468 ssid->ht40 = params->ht40;
1469 ssid->vht = params->vht;
1470 ssid->ssid = os_zalloc(params->ssid_len + 1);
1472 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1473 ssid->ssid_len = params->ssid_len;
1475 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1476 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1477 ssid->proto = WPA_PROTO_RSN;
1478 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1479 ssid->group_cipher = WPA_CIPHER_CCMP;
1480 if (params->freq > 56160) {
1482 * Enable GCMP instead of CCMP as pairwise_cipher and
1483 * group_cipher in 60 GHz.
1485 ssid->pairwise_cipher = WPA_CIPHER_GCMP;
1486 ssid->group_cipher = WPA_CIPHER_GCMP;
1488 if (os_strlen(params->passphrase) > 0) {
1489 ssid->passphrase = os_strdup(params->passphrase);
1490 if (ssid->passphrase == NULL) {
1491 wpa_msg_global(wpa_s, MSG_ERROR,
1492 "P2P: Failed to copy passphrase for GO");
1493 wpa_config_remove_network(wpa_s->conf, ssid->id);
1497 ssid->passphrase = NULL;
1498 ssid->psk_set = params->psk_set;
1500 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1501 else if (ssid->passphrase)
1502 wpa_config_update_psk(ssid);
1503 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1505 wpa_s->ap_configured_cb = p2p_go_configured;
1506 wpa_s->ap_configured_cb_ctx = wpa_s;
1507 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1508 wpa_s->scan_req = NORMAL_SCAN_REQ;
1509 wpa_s->connect_without_scan = ssid;
1510 wpa_s->reassociate = 1;
1511 wpa_s->disconnected = 0;
1512 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1514 wpa_supplicant_req_scan(wpa_s, 0, 0);
1518 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1519 const struct wpa_supplicant *src)
1521 struct wpa_config *d;
1522 const struct wpa_config *s;
1527 #define C(n) if (s->n) d->n = os_strdup(s->n)
1536 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1537 os_memcpy(d->sec_device_type, s->sec_device_type,
1538 sizeof(d->sec_device_type));
1539 d->num_sec_device_types = s->num_sec_device_types;
1541 d->p2p_group_idle = s->p2p_group_idle;
1542 d->p2p_intra_bss = s->p2p_intra_bss;
1543 d->persistent_reconnect = s->persistent_reconnect;
1544 d->max_num_sta = s->max_num_sta;
1545 d->pbc_in_m1 = s->pbc_in_m1;
1546 d->ignore_old_scan_res = s->ignore_old_scan_res;
1547 d->beacon_int = s->beacon_int;
1548 d->dtim_period = s->dtim_period;
1549 d->disassoc_low_ack = s->disassoc_low_ack;
1550 d->disable_scan_offload = s->disable_scan_offload;
1551 d->passive_scan = s->passive_scan;
1553 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1554 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1555 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1560 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1561 char *ifname, size_t len)
1563 char *ifname_ptr = wpa_s->ifname;
1565 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1566 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1567 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1570 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1571 if (os_strlen(ifname) >= IFNAMSIZ &&
1572 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1575 /* Try to avoid going over the IFNAMSIZ length limit */
1576 res = os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1577 if (os_snprintf_error(len, res) && len)
1578 ifname[len - 1] = '\0';
1583 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1584 enum wpa_driver_if_type type)
1586 char ifname[120], force_ifname[120];
1588 if (wpa_s->pending_interface_name[0]) {
1589 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1590 "- skip creation of a new one");
1591 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1592 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1593 "unknown?! ifname='%s'",
1594 wpa_s->pending_interface_name);
1600 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1601 force_ifname[0] = '\0';
1603 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1605 wpa_s->p2p_group_idx++;
1607 wpa_s->pending_interface_type = type;
1608 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1609 wpa_s->pending_interface_addr, NULL) < 0) {
1610 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1615 if (force_ifname[0]) {
1616 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1618 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1619 sizeof(wpa_s->pending_interface_name));
1621 os_strlcpy(wpa_s->pending_interface_name, ifname,
1622 sizeof(wpa_s->pending_interface_name));
1623 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1624 MACSTR, wpa_s->pending_interface_name,
1625 MAC2STR(wpa_s->pending_interface_addr));
1631 static void wpas_p2p_remove_pending_group_interface(
1632 struct wpa_supplicant *wpa_s)
1634 if (!wpa_s->pending_interface_name[0] ||
1635 is_zero_ether_addr(wpa_s->pending_interface_addr))
1636 return; /* No pending virtual interface */
1638 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1639 wpa_s->pending_interface_name);
1640 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1641 wpa_s->pending_interface_name);
1642 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1643 wpa_s->pending_interface_name[0] = '\0';
1647 static struct wpa_supplicant *
1648 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1650 struct wpa_interface iface;
1651 struct wpa_supplicant *group_wpa_s;
1653 if (!wpa_s->pending_interface_name[0]) {
1654 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1655 if (!wpas_p2p_create_iface(wpa_s))
1658 * Something has forced us to remove the pending interface; try
1659 * to create a new one and hope for the best that we will get
1660 * the same local address.
1662 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1663 WPA_IF_P2P_CLIENT) < 0)
1667 os_memset(&iface, 0, sizeof(iface));
1668 iface.ifname = wpa_s->pending_interface_name;
1669 iface.driver = wpa_s->driver->name;
1670 if (wpa_s->conf->ctrl_interface == NULL &&
1671 wpa_s->parent != wpa_s &&
1673 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1674 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1676 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1677 iface.driver_param = wpa_s->conf->driver_param;
1678 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1679 if (group_wpa_s == NULL) {
1680 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1681 "wpa_supplicant interface");
1684 wpa_s->pending_interface_name[0] = '\0';
1685 group_wpa_s->parent = wpa_s;
1686 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1687 P2P_GROUP_INTERFACE_CLIENT;
1688 wpa_s->global->p2p_group_formation = group_wpa_s;
1690 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1696 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1699 struct wpa_supplicant *wpa_s = eloop_ctx;
1700 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1701 wpas_p2p_group_formation_failed(wpa_s);
1705 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1707 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1708 wpa_s->parent, NULL);
1709 if (wpa_s->global->p2p)
1710 p2p_group_formation_failed(wpa_s->global->p2p);
1711 wpas_group_formation_completed(wpa_s, 0);
1715 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1717 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1718 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1719 wpa_s->parent, NULL);
1720 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1721 wpa_s->parent, NULL);
1722 wpa_s->global->p2p_fail_on_wps_complete = 0;
1726 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1728 if (wpa_s->global->p2p_group_formation != wpa_s)
1730 /* Speed up group formation timeout since this cannot succeed */
1731 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1732 wpa_s->parent, NULL);
1733 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1734 wpa_s->parent, NULL);
1738 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1740 struct wpa_supplicant *wpa_s = ctx;
1742 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1743 wpa_drv_cancel_remain_on_channel(wpa_s);
1744 wpa_s->off_channel_freq = 0;
1745 wpa_s->roc_waiting_drv_freq = 0;
1749 wpa_msg_global(wpa_s, MSG_INFO,
1750 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1752 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1753 wpas_p2p_remove_pending_group_interface(wpa_s);
1757 if (wpa_s->p2p_go_ht40)
1759 if (wpa_s->p2p_go_vht)
1762 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1763 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1765 res->role_go ? "GO" : "client", res->freq, res->ht40,
1766 MAC2STR(res->peer_device_addr),
1767 MAC2STR(res->peer_interface_addr),
1768 p2p_wps_method_text(res->wps_method));
1769 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1771 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1772 struct wpa_ssid *ssid;
1773 ssid = wpa_config_get_network(wpa_s->conf,
1774 wpa_s->p2p_persistent_id);
1775 if (ssid && ssid->disabled == 2 &&
1776 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1777 size_t len = os_strlen(ssid->passphrase);
1778 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1779 "on requested persistent group");
1780 os_memcpy(res->passphrase, ssid->passphrase, len);
1781 res->passphrase[len] = '\0';
1785 if (wpa_s->create_p2p_iface) {
1786 struct wpa_supplicant *group_wpa_s =
1787 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1788 if (group_wpa_s == NULL) {
1789 wpas_p2p_remove_pending_group_interface(wpa_s);
1790 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
1792 wpas_p2p_group_formation_failed(wpa_s);
1795 if (group_wpa_s != wpa_s) {
1796 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1797 sizeof(group_wpa_s->p2p_pin));
1798 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1800 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1801 wpa_s->pending_interface_name[0] = '\0';
1802 group_wpa_s->p2p_in_provisioning = 1;
1805 wpas_start_wps_go(group_wpa_s, res, 1);
1807 wpas_start_wps_enrollee(group_wpa_s, res);
1809 wpa_s->p2p_in_provisioning = 1;
1810 wpa_s->global->p2p_group_formation = wpa_s;
1813 wpas_start_wps_go(wpa_s, res, 1);
1815 wpas_start_wps_enrollee(ctx, res);
1818 wpa_s->p2p_long_listen = 0;
1819 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1821 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1822 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1823 (res->peer_config_timeout % 100) * 10000,
1824 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1828 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1830 struct wpa_supplicant *wpa_s = ctx;
1831 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1832 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1834 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1838 static void wpas_dev_found(void *ctx, const u8 *addr,
1839 const struct p2p_peer_info *info,
1842 #ifndef CONFIG_NO_STDOUT_DEBUG
1843 struct wpa_supplicant *wpa_s = ctx;
1844 char devtype[WPS_DEV_TYPE_BUFSIZE];
1845 char *wfd_dev_info_hex = NULL;
1847 #ifdef CONFIG_WIFI_DISPLAY
1848 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1849 WFD_SUBELEM_DEVICE_INFO);
1850 #endif /* CONFIG_WIFI_DISPLAY */
1852 if (info->p2ps_instance) {
1854 const u8 *buf = wpabuf_head(info->p2ps_instance);
1855 size_t len = wpabuf_len(info->p2ps_instance);
1862 if (len < 4 + 2 + 1)
1864 id = WPA_GET_LE32(buf);
1866 methods = WPA_GET_BE16(buf);
1869 if (str_len > len - 4 - 2 - 1)
1871 os_memcpy(str, buf, str_len);
1872 str[str_len] = '\0';
1874 len -= str_len + sizeof(u32) + sizeof(u16) + sizeof(u8);
1876 wpa_msg_global(wpa_s, MSG_INFO,
1877 P2P_EVENT_DEVICE_FOUND MACSTR
1878 " p2p_dev_addr=" MACSTR
1879 " pri_dev_type=%s name='%s'"
1880 " config_methods=0x%x"
1883 " adv_id=%x asp_svc=%s%s",
1885 MAC2STR(info->p2p_device_addr),
1886 wps_dev_type_bin2str(
1888 devtype, sizeof(devtype)),
1889 info->device_name, methods,
1890 info->dev_capab, info->group_capab,
1892 info->vendor_elems ?
1893 " vendor_elems=1" : "");
1898 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1899 " p2p_dev_addr=" MACSTR
1900 " pri_dev_type=%s name='%s' config_methods=0x%x "
1901 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
1902 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1903 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1905 info->device_name, info->config_methods,
1906 info->dev_capab, info->group_capab,
1907 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1908 wfd_dev_info_hex ? wfd_dev_info_hex : "",
1909 info->vendor_elems ? " vendor_elems=1" : "",
1913 os_free(wfd_dev_info_hex);
1914 #endif /* CONFIG_NO_STDOUT_DEBUG */
1916 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1920 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1922 struct wpa_supplicant *wpa_s = ctx;
1924 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1925 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1927 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1931 static void wpas_find_stopped(void *ctx)
1933 struct wpa_supplicant *wpa_s = ctx;
1934 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1938 struct wpas_p2p_listen_work {
1940 unsigned int duration;
1941 struct wpabuf *probe_resp_ie;
1945 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1949 wpabuf_free(lwork->probe_resp_ie);
1954 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1956 struct wpas_p2p_listen_work *lwork;
1958 if (!wpa_s->p2p_listen_work)
1961 lwork = wpa_s->p2p_listen_work->ctx;
1962 wpas_p2p_listen_work_free(lwork);
1963 radio_work_done(wpa_s->p2p_listen_work);
1964 wpa_s->p2p_listen_work = NULL;
1968 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1970 struct wpa_supplicant *wpa_s = work->wpa_s;
1971 struct wpas_p2p_listen_work *lwork = work->ctx;
1972 unsigned int duration;
1975 if (work->started) {
1976 wpa_s->p2p_listen_work = NULL;
1977 wpas_stop_listen(wpa_s);
1979 wpas_p2p_listen_work_free(lwork);
1983 wpa_s->p2p_listen_work = work;
1985 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1987 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1988 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1989 "report received Probe Request frames");
1990 wpas_p2p_listen_work_done(wpa_s);
1994 wpa_s->pending_listen_freq = lwork->freq;
1995 wpa_s->pending_listen_duration = lwork->duration;
1997 duration = lwork->duration;
1998 #ifdef CONFIG_TESTING_OPTIONS
1999 if (wpa_s->extra_roc_dur) {
2000 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
2001 duration, duration + wpa_s->extra_roc_dur);
2002 duration += wpa_s->extra_roc_dur;
2004 #endif /* CONFIG_TESTING_OPTIONS */
2006 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
2007 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
2008 "to remain on channel (%u MHz) for Listen "
2009 "state", lwork->freq);
2010 wpas_p2p_listen_work_done(wpa_s);
2011 wpa_s->pending_listen_freq = 0;
2014 wpa_s->off_channel_freq = 0;
2015 wpa_s->roc_waiting_drv_freq = lwork->freq;
2019 static int wpas_start_listen(void *ctx, unsigned int freq,
2020 unsigned int duration,
2021 const struct wpabuf *probe_resp_ie)
2023 struct wpa_supplicant *wpa_s = ctx;
2024 struct wpas_p2p_listen_work *lwork;
2026 if (wpa_s->p2p_listen_work) {
2027 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
2031 lwork = os_zalloc(sizeof(*lwork));
2035 lwork->duration = duration;
2036 if (probe_resp_ie) {
2037 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
2038 if (lwork->probe_resp_ie == NULL) {
2039 wpas_p2p_listen_work_free(lwork);
2044 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
2046 wpas_p2p_listen_work_free(lwork);
2054 static void wpas_stop_listen(void *ctx)
2056 struct wpa_supplicant *wpa_s = ctx;
2057 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2058 wpa_drv_cancel_remain_on_channel(wpa_s);
2059 wpa_s->off_channel_freq = 0;
2060 wpa_s->roc_waiting_drv_freq = 0;
2062 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2063 wpa_drv_probe_req_report(wpa_s, 0);
2064 wpas_p2p_listen_work_done(wpa_s);
2068 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
2070 struct wpa_supplicant *wpa_s = ctx;
2071 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
2076 * DNS Header section is used only to calculate compression pointers, so the
2077 * contents of this data does not matter, but the length needs to be reserved
2078 * in the virtual packet.
2080 #define DNS_HEADER_LEN 12
2083 * 27-octet in-memory packet from P2P specification containing two implied
2084 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
2086 #define P2P_SD_IN_MEMORY_LEN 27
2088 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
2089 u8 **spos, const u8 *end)
2091 while (*spos < end) {
2092 u8 val = ((*spos)[0] & 0xc0) >> 6;
2095 if (val == 1 || val == 2) {
2096 /* These are reserved values in RFC 1035 */
2097 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2098 "sequence starting with 0x%x", val);
2107 if (*spos + 2 > end) {
2108 wpa_printf(MSG_DEBUG, "P2P: No room for full "
2109 "DNS offset field");
2113 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
2114 if (offset >= *spos - start) {
2115 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
2116 "pointer offset %u", offset);
2121 spos_tmp = start + offset;
2122 return p2p_sd_dns_uncompress_label(upos, uend, start,
2128 len = (*spos)[0] & 0x3f;
2133 if (*spos + len > end) {
2134 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2135 "sequence - no room for label with length "
2140 if (*upos + len + 2 > uend)
2143 os_memcpy(*upos, *spos, len);
2155 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
2156 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
2157 * not large enough */
2158 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
2159 size_t msg_len, size_t offset)
2161 /* 27-octet in-memory packet from P2P specification */
2162 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
2163 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
2164 u8 *tmp, *end, *spos;
2170 if (offset > msg_len)
2173 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
2176 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
2177 end = spos + msg_len;
2180 os_memset(tmp, 0, DNS_HEADER_LEN);
2181 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
2182 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
2185 uend = buf + buf_len;
2187 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
2196 } else if (upos[-1] == '.')
2204 static struct p2p_srv_bonjour *
2205 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
2206 const struct wpabuf *query)
2208 struct p2p_srv_bonjour *bsrv;
2211 len = wpabuf_len(query);
2212 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2213 struct p2p_srv_bonjour, list) {
2214 if (len == wpabuf_len(bsrv->query) &&
2215 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
2223 static struct p2p_srv_upnp *
2224 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2225 const char *service)
2227 struct p2p_srv_upnp *usrv;
2229 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2230 struct p2p_srv_upnp, list) {
2231 if (version == usrv->version &&
2232 os_strcmp(service, usrv->service) == 0)
2239 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2244 if (wpabuf_tailroom(resp) < 5)
2247 /* Length (to be filled) */
2248 len_pos = wpabuf_put(resp, 2);
2249 wpabuf_put_u8(resp, srv_proto);
2250 wpabuf_put_u8(resp, srv_trans_id);
2252 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2253 /* Response Data: empty */
2254 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2258 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2259 struct wpabuf *resp, u8 srv_trans_id)
2261 struct p2p_srv_bonjour *bsrv;
2264 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2266 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2267 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2271 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2272 struct p2p_srv_bonjour, list) {
2273 if (wpabuf_tailroom(resp) <
2274 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2276 /* Length (to be filled) */
2277 len_pos = wpabuf_put(resp, 2);
2278 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2279 wpabuf_put_u8(resp, srv_trans_id);
2281 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2282 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2283 wpabuf_head(bsrv->resp),
2284 wpabuf_len(bsrv->resp));
2286 wpabuf_put_buf(resp, bsrv->query); /* Key */
2287 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2288 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2294 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2297 char str_rx[256], str_srv[256];
2299 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2300 return 0; /* Too short to include DNS Type and Version */
2301 if (os_memcmp(query + query_len - 3,
2302 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2304 return 0; /* Mismatch in DNS Type or Version */
2305 if (query_len == wpabuf_len(bsrv->query) &&
2306 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2307 return 1; /* Binary match */
2309 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2311 return 0; /* Failed to uncompress query */
2312 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2313 wpabuf_head(bsrv->query),
2314 wpabuf_len(bsrv->query) - 3, 0))
2315 return 0; /* Failed to uncompress service */
2317 return os_strcmp(str_rx, str_srv) == 0;
2321 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2322 struct wpabuf *resp, u8 srv_trans_id,
2323 const u8 *query, size_t query_len)
2325 struct p2p_srv_bonjour *bsrv;
2329 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2331 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2332 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2333 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2338 if (query_len == 0) {
2339 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2343 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2344 struct p2p_srv_bonjour, list) {
2345 if (!match_bonjour_query(bsrv, query, query_len))
2348 if (wpabuf_tailroom(resp) <
2349 5 + query_len + wpabuf_len(bsrv->resp))
2354 /* Length (to be filled) */
2355 len_pos = wpabuf_put(resp, 2);
2356 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2357 wpabuf_put_u8(resp, srv_trans_id);
2360 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2361 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2362 wpabuf_head(bsrv->resp),
2363 wpabuf_len(bsrv->resp));
2366 wpabuf_put_data(resp, query, query_len); /* Key */
2367 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2369 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2373 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2375 if (wpabuf_tailroom(resp) < 5)
2378 /* Length (to be filled) */
2379 len_pos = wpabuf_put(resp, 2);
2380 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2381 wpabuf_put_u8(resp, srv_trans_id);
2384 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2385 /* Response Data: empty */
2386 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2392 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2393 struct wpabuf *resp, u8 srv_trans_id)
2395 struct p2p_srv_upnp *usrv;
2398 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2400 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2401 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2405 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2406 struct p2p_srv_upnp, list) {
2407 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2410 /* Length (to be filled) */
2411 len_pos = wpabuf_put(resp, 2);
2412 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2413 wpabuf_put_u8(resp, srv_trans_id);
2416 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2418 wpabuf_put_u8(resp, usrv->version);
2419 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2421 wpabuf_put_str(resp, usrv->service);
2422 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2428 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2429 struct wpabuf *resp, u8 srv_trans_id,
2430 const u8 *query, size_t query_len)
2432 struct p2p_srv_upnp *usrv;
2438 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2441 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2442 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2443 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2448 if (query_len == 0) {
2449 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2453 if (wpabuf_tailroom(resp) < 5)
2456 /* Length (to be filled) */
2457 len_pos = wpabuf_put(resp, 2);
2458 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2459 wpabuf_put_u8(resp, srv_trans_id);
2462 str = os_malloc(query_len);
2465 os_memcpy(str, query + 1, query_len - 1);
2466 str[query_len - 1] = '\0';
2468 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2469 struct p2p_srv_upnp, list) {
2470 if (version != usrv->version)
2473 if (os_strcmp(str, "ssdp:all") != 0 &&
2474 os_strstr(usrv->service, str) == NULL)
2477 if (wpabuf_tailroom(resp) < 2)
2481 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2483 wpabuf_put_u8(resp, version);
2485 wpabuf_put_u8(resp, ',');
2489 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2491 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2493 wpabuf_put_str(resp, usrv->service);
2498 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2501 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2502 /* Response Data: empty */
2505 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2509 #ifdef CONFIG_WIFI_DISPLAY
2510 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2511 struct wpabuf *resp, u8 srv_trans_id,
2512 const u8 *query, size_t query_len)
2518 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2520 if (!wpa_s->global->wifi_display) {
2521 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2522 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2527 if (query_len < 1) {
2528 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2533 if (wpabuf_tailroom(resp) < 5)
2538 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2540 /* TODO: role specific handling */
2542 /* Length (to be filled) */
2543 len_pos = wpabuf_put(resp, 2);
2544 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2545 wpabuf_put_u8(resp, srv_trans_id);
2546 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2548 while (pos < query + query_len) {
2549 if (*pos < MAX_WFD_SUBELEMS &&
2550 wpa_s->global->wfd_subelem[*pos] &&
2551 wpabuf_tailroom(resp) >=
2552 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2553 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2554 "subelement %u", *pos);
2555 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2560 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2562 #endif /* CONFIG_WIFI_DISPLAY */
2565 static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2566 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2568 struct wpa_supplicant *wpa_s = ctx;
2569 const u8 *pos = tlvs;
2570 const u8 *end = tlvs + tlvs_len;
2573 struct wpabuf *resp;
2574 u8 srv_proto, srv_trans_id;
2578 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2580 buf_len = 2 * tlvs_len + 1;
2581 buf = os_malloc(buf_len);
2583 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2584 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2586 freq, MAC2STR(sa), dialog_token, update_indic,
2591 if (wpa_s->p2p_sd_over_ctrl_iface) {
2592 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2593 update_indic, tlvs, tlvs_len);
2594 return; /* to be processed by an external program */
2597 resp = wpabuf_alloc(10000);
2601 while (pos + 1 < end) {
2602 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2603 slen = WPA_GET_LE16(pos);
2605 if (pos + slen > end || slen < 2) {
2606 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2611 tlv_end = pos + slen;
2614 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2616 srv_trans_id = *pos++;
2617 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2620 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2621 pos, tlv_end - pos);
2624 if (wpa_s->force_long_sd) {
2625 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2627 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2628 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2632 switch (srv_proto) {
2633 case P2P_SERV_ALL_SERVICES:
2634 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2635 "for all services");
2636 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2637 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2638 wpa_printf(MSG_DEBUG, "P2P: No service "
2639 "discovery protocols available");
2640 wpas_sd_add_proto_not_avail(
2641 resp, P2P_SERV_ALL_SERVICES,
2645 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2646 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2648 case P2P_SERV_BONJOUR:
2649 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2650 pos, tlv_end - pos);
2653 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2654 pos, tlv_end - pos);
2656 #ifdef CONFIG_WIFI_DISPLAY
2657 case P2P_SERV_WIFI_DISPLAY:
2658 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2659 pos, tlv_end - pos);
2661 #endif /* CONFIG_WIFI_DISPLAY */
2663 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2664 "protocol %u", srv_proto);
2665 wpas_sd_add_proto_not_avail(resp, srv_proto,
2674 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2675 update_indic, tlvs, tlvs_len);
2677 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2683 static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2684 const u8 *tlvs, size_t tlvs_len)
2686 struct wpa_supplicant *wpa_s = ctx;
2687 const u8 *pos = tlvs;
2688 const u8 *end = tlvs + tlvs_len;
2694 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2696 if (tlvs_len > 1500) {
2697 /* TODO: better way for handling this */
2698 wpa_msg_ctrl(wpa_s, MSG_INFO,
2699 P2P_EVENT_SERV_DISC_RESP MACSTR
2700 " %u <long response: %u bytes>",
2701 MAC2STR(sa), update_indic,
2702 (unsigned int) tlvs_len);
2704 buf_len = 2 * tlvs_len + 1;
2705 buf = os_malloc(buf_len);
2707 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2708 wpa_msg_ctrl(wpa_s, MSG_INFO,
2709 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2710 MAC2STR(sa), update_indic, buf);
2716 u8 srv_proto, srv_trans_id, status;
2718 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2719 slen = WPA_GET_LE16(pos);
2721 if (pos + slen > end || slen < 3) {
2722 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2726 tlv_end = pos + slen;
2729 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2731 srv_trans_id = *pos++;
2732 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2735 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2738 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2739 pos, tlv_end - pos);
2744 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2748 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2749 const struct wpabuf *tlvs)
2751 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2753 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2757 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2758 u8 version, const char *query)
2760 struct wpabuf *tlvs;
2763 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2766 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2767 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2768 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2769 wpabuf_put_u8(tlvs, version);
2770 wpabuf_put_str(tlvs, query);
2771 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2777 #ifdef CONFIG_WIFI_DISPLAY
2779 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2780 const struct wpabuf *tlvs)
2782 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2784 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2788 #define MAX_WFD_SD_SUBELEMS 20
2790 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2791 const char *subelems)
2798 len = wpabuf_put(tlvs, 2);
2799 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2800 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2802 wpabuf_put_u8(tlvs, role);
2807 if (val >= 0 && val < 256) {
2808 wpabuf_put_u8(tlvs, val);
2810 if (count == MAX_WFD_SD_SUBELEMS)
2813 pos = os_strchr(pos + 1, ',');
2819 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2823 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2824 const u8 *dst, const char *role)
2826 struct wpabuf *tlvs;
2828 const char *subelems;
2831 subelems = os_strchr(role, ' ');
2832 if (subelems == NULL)
2836 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2840 if (os_strstr(role, "[source]"))
2841 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2842 if (os_strstr(role, "[pri-sink]"))
2843 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2844 if (os_strstr(role, "[sec-sink]"))
2845 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2846 if (os_strstr(role, "[source+sink]"))
2847 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2849 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2854 #endif /* CONFIG_WIFI_DISPLAY */
2857 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2859 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2861 return p2p_sd_cancel_request(wpa_s->global->p2p,
2862 (void *) (uintptr_t) req);
2866 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2867 const u8 *dst, u8 dialog_token,
2868 const struct wpabuf *resp_tlvs)
2870 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2872 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2877 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2879 if (wpa_s->global->p2p)
2880 p2p_sd_service_update(wpa_s->global->p2p);
2884 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2886 dl_list_del(&bsrv->list);
2887 wpabuf_free(bsrv->query);
2888 wpabuf_free(bsrv->resp);
2893 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2895 dl_list_del(&usrv->list);
2896 os_free(usrv->service);
2901 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2903 struct p2p_srv_bonjour *bsrv, *bn;
2904 struct p2p_srv_upnp *usrv, *un;
2906 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2907 struct p2p_srv_bonjour, list)
2908 wpas_p2p_srv_bonjour_free(bsrv);
2910 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2911 struct p2p_srv_upnp, list)
2912 wpas_p2p_srv_upnp_free(usrv);
2914 wpas_p2p_sd_service_update(wpa_s);
2918 int wpas_p2p_service_p2ps_id_exists(struct wpa_supplicant *wpa_s, u32 adv_id)
2923 if (p2p_service_p2ps_id(wpa_s->global->p2p, adv_id))
2930 int wpas_p2p_service_del_asp(struct wpa_supplicant *wpa_s, u32 adv_id)
2932 return p2p_service_del_asp(wpa_s->global->p2p, adv_id);
2936 int wpas_p2p_service_add_asp(struct wpa_supplicant *wpa_s,
2937 int auto_accept, u32 adv_id,
2938 const char *adv_str, u8 svc_state,
2939 u16 config_methods, const char *svc_info)
2941 return p2p_service_add_asp(wpa_s->global->p2p, auto_accept, adv_id,
2942 adv_str, svc_state, config_methods,
2947 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2948 struct wpabuf *query, struct wpabuf *resp)
2950 struct p2p_srv_bonjour *bsrv;
2952 bsrv = os_zalloc(sizeof(*bsrv));
2955 bsrv->query = query;
2957 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2959 wpas_p2p_sd_service_update(wpa_s);
2964 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2965 const struct wpabuf *query)
2967 struct p2p_srv_bonjour *bsrv;
2969 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2972 wpas_p2p_srv_bonjour_free(bsrv);
2973 wpas_p2p_sd_service_update(wpa_s);
2978 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2979 const char *service)
2981 struct p2p_srv_upnp *usrv;
2983 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2984 return 0; /* Already listed */
2985 usrv = os_zalloc(sizeof(*usrv));
2988 usrv->version = version;
2989 usrv->service = os_strdup(service);
2990 if (usrv->service == NULL) {
2994 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2996 wpas_p2p_sd_service_update(wpa_s);
3001 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
3002 const char *service)
3004 struct p2p_srv_upnp *usrv;
3006 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
3009 wpas_p2p_srv_upnp_free(usrv);
3010 wpas_p2p_sd_service_update(wpa_s);
3015 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
3016 const u8 *peer, const char *params,
3017 unsigned int generated_pin)
3019 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
3020 " %08d%s", MAC2STR(peer), generated_pin, params);
3024 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
3025 const u8 *peer, const char *params)
3027 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
3028 "%s", MAC2STR(peer), params);
3032 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
3033 const u8 *dev_addr, const u8 *pri_dev_type,
3034 const char *dev_name, u16 supp_config_methods,
3035 u8 dev_capab, u8 group_capab, const u8 *group_id,
3036 size_t group_id_len)
3038 struct wpa_supplicant *wpa_s = ctx;
3039 char devtype[WPS_DEV_TYPE_BUFSIZE];
3041 u8 empty_dev_type[8];
3042 unsigned int generated_pin = 0;
3043 struct wpa_supplicant *group = NULL;
3047 for (group = wpa_s->global->ifaces; group; group = group->next)
3049 struct wpa_ssid *s = group->current_ssid;
3051 s->mode == WPAS_MODE_P2P_GO &&
3052 group_id_len - ETH_ALEN == s->ssid_len &&
3053 os_memcmp(group_id + ETH_ALEN, s->ssid,
3059 if (pri_dev_type == NULL) {
3060 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
3061 pri_dev_type = empty_dev_type;
3063 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
3064 " pri_dev_type=%s name='%s' config_methods=0x%x "
3065 "dev_capab=0x%x group_capab=0x%x%s%s",
3067 wps_dev_type_bin2str(pri_dev_type, devtype,
3069 dev_name, supp_config_methods, dev_capab, group_capab,
3070 group ? " group=" : "",
3071 group ? group->ifname : "");
3072 if (os_snprintf_error(sizeof(params), res))
3073 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
3074 params[sizeof(params) - 1] = '\0';
3076 if (config_methods & WPS_CONFIG_DISPLAY) {
3077 generated_pin = wps_generate_pin();
3078 wpas_prov_disc_local_display(wpa_s, peer, params,
3080 } else if (config_methods & WPS_CONFIG_KEYPAD)
3081 wpas_prov_disc_local_keypad(wpa_s, peer, params);
3082 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
3083 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
3084 MACSTR "%s", MAC2STR(peer), params);
3086 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
3087 P2P_PROV_DISC_SUCCESS,
3088 config_methods, generated_pin);
3092 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
3094 struct wpa_supplicant *wpa_s = ctx;
3095 unsigned int generated_pin = 0;
3098 if (wpa_s->pending_pd_before_join &&
3099 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
3100 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
3101 wpa_s->pending_pd_before_join = 0;
3102 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3103 "join-existing-group operation");
3104 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
3108 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
3109 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
3112 res = os_snprintf(params, sizeof(params), " peer_go=%d",
3113 wpa_s->pending_pd_use == AUTO_PD_JOIN);
3114 if (os_snprintf_error(sizeof(params), res))
3115 params[sizeof(params) - 1] = '\0';
3119 if (config_methods & WPS_CONFIG_DISPLAY)
3120 wpas_prov_disc_local_keypad(wpa_s, peer, params);
3121 else if (config_methods & WPS_CONFIG_KEYPAD) {
3122 generated_pin = wps_generate_pin();
3123 wpas_prov_disc_local_display(wpa_s, peer, params,
3125 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
3126 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
3127 MACSTR "%s", MAC2STR(peer), params);
3129 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3130 P2P_PROV_DISC_SUCCESS,
3131 config_methods, generated_pin);
3135 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
3136 enum p2p_prov_disc_status status)
3138 struct wpa_supplicant *wpa_s = ctx;
3140 if (wpa_s->p2p_fallback_to_go_neg) {
3141 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
3142 "failed - fall back to GO Negotiation");
3143 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
3147 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
3148 wpa_s->pending_pd_before_join = 0;
3149 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3150 "join-existing-group operation (no ACK for PD "
3152 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
3156 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3157 " p2p_dev_addr=" MACSTR " status=%d",
3158 MAC2STR(peer), status);
3160 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3165 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
3167 if (channels == NULL)
3168 return 1; /* Assume no restrictions */
3169 return p2p_channels_includes_freq(channels, freq);
3175 * Pick the best frequency to use from all the currently used frequencies.
3177 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3178 struct wpa_used_freq_data *freqs,
3183 /* find a candidate freq that is supported by P2P */
3184 for (c = 0; c < num; c++)
3185 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3191 /* once we have a candidate, try to find a 'better' one */
3192 for (i = c + 1; i < num; i++) {
3193 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3197 * 1. Infrastructure station interfaces have higher preference.
3198 * 2. P2P Clients have higher preference.
3201 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3206 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3209 return freqs[c].freq;
3213 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3214 const u8 *go_dev_addr, const u8 *ssid,
3215 size_t ssid_len, int *go, u8 *group_bssid,
3216 int *force_freq, int persistent_group,
3217 const struct p2p_channels *channels,
3220 struct wpa_supplicant *wpa_s = ctx;
3222 struct wpa_used_freq_data *freqs;
3223 struct wpa_supplicant *grp;
3226 if (!persistent_group) {
3227 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3228 " to join an active group (SSID: %s)",
3229 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
3230 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3231 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3233 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3234 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3235 "authorized invitation");
3239 #ifdef CONFIG_WPS_NFC
3240 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3241 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
3242 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
3243 wpa_s->p2p_wps_method = WPS_NFC;
3244 wpa_s->pending_join_wps_method = WPS_NFC;
3245 os_memcpy(wpa_s->pending_join_dev_addr,
3246 go_dev_addr, ETH_ALEN);
3247 os_memcpy(wpa_s->pending_join_iface_addr,
3251 #endif /* CONFIG_WPS_NFC */
3254 * Do not accept the invitation automatically; notify user and
3257 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3260 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3262 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3263 "running persistent group");
3265 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3269 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3270 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3271 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3272 "invitation to re-invoke a persistent group");
3273 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3274 } else if (!wpa_s->conf->persistent_reconnect)
3275 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3277 for (s = wpa_s->conf->ssid; s; s = s->next) {
3278 if (s->disabled == 2 &&
3279 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3280 s->ssid_len == ssid_len &&
3281 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3286 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3287 " requested reinvocation of an unknown group",
3289 return P2P_SC_FAIL_UNKNOWN_GROUP;
3292 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3294 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3295 wpa_printf(MSG_DEBUG, "P2P: The only available "
3296 "interface is already in use - reject "
3298 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3300 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3301 } else if (s->mode == WPAS_MODE_P2P_GO) {
3303 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3305 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3306 "interface address for the group");
3307 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3309 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3314 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3317 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3318 sizeof(struct wpa_used_freq_data));
3320 int num_channels = wpa_s->num_multichan_concurrent;
3321 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3322 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3326 /* Get one of the frequencies currently in use */
3327 if (best_freq > 0) {
3328 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3329 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
3331 if (wpa_s->num_multichan_concurrent < 2 ||
3332 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3333 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3334 *force_freq = best_freq;
3338 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3339 wpas_p2p_num_unused_channels(wpa_s) > 0) {
3341 /* We are the client */
3342 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3343 "running a GO but we are capable of MCC, "
3344 "figure out the best channel to use");
3346 } else if (!freq_included(channels, *force_freq)) {
3347 /* We are the GO, and *force_freq is not in the
3349 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3350 "in intersection but we are capable of MCC, "
3351 "figure out the best channel to use",
3357 return P2P_SC_SUCCESS;
3361 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3362 const u8 *ssid, size_t ssid_len,
3363 const u8 *go_dev_addr, u8 status,
3366 struct wpa_supplicant *wpa_s = ctx;
3369 for (s = wpa_s->conf->ssid; s; s = s->next) {
3370 if (s->disabled == 2 &&
3371 s->ssid_len == ssid_len &&
3372 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3376 if (status == P2P_SC_SUCCESS) {
3377 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3378 " was accepted; op_freq=%d MHz, SSID=%s",
3379 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3381 int go = s->mode == WPAS_MODE_P2P_GO;
3382 wpas_p2p_group_add_persistent(
3383 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
3384 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3386 wpa_s->user_initiated_pd = 0;
3387 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
3388 wpa_s->p2p_wps_method, 0, op_freq,
3394 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3395 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3396 " was rejected (status %u)", MAC2STR(sa), status);
3402 wpa_msg_global(wpa_s, MSG_INFO,
3403 P2P_EVENT_INVITATION_RECEIVED
3404 "sa=" MACSTR " go_dev_addr=" MACSTR
3405 " bssid=" MACSTR " unknown-network",
3406 MAC2STR(sa), MAC2STR(go_dev_addr),
3409 wpa_msg_global(wpa_s, MSG_INFO,
3410 P2P_EVENT_INVITATION_RECEIVED
3411 "sa=" MACSTR " go_dev_addr=" MACSTR
3413 MAC2STR(sa), MAC2STR(go_dev_addr));
3418 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
3419 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3420 "sa=" MACSTR " persistent=%d freq=%d",
3421 MAC2STR(sa), s->id, op_freq);
3423 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3424 "sa=" MACSTR " persistent=%d",
3425 MAC2STR(sa), s->id);
3430 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3431 struct wpa_ssid *ssid,
3432 const u8 *peer, int inv)
3439 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3440 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
3444 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
3445 if (ssid->mode != WPAS_MODE_P2P_GO &&
3446 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3447 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3448 "due to invitation result", ssid->id);
3449 wpas_notify_network_removed(wpa_s, ssid);
3450 wpa_config_remove_network(wpa_s->conf, ssid->id);
3453 return; /* Peer not found in client list */
3456 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
3457 "group %d client list%s",
3458 MAC2STR(peer), ssid->id,
3459 inv ? " due to invitation result" : "");
3460 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3461 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3462 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3463 ssid->num_p2p_clients--;
3464 if (wpa_s->parent->conf->update_config &&
3465 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3466 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3470 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3473 struct wpa_ssid *ssid;
3475 wpa_s = wpa_s->global->p2p_invite_group;
3477 return; /* No known invitation group */
3478 ssid = wpa_s->current_ssid;
3479 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3480 !ssid->p2p_persistent_group)
3481 return; /* Not operating as a GO in persistent group */
3482 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3483 ssid->ssid, ssid->ssid_len);
3484 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3488 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
3489 const struct p2p_channels *channels,
3490 const u8 *peer, int neg_freq,
3493 struct wpa_supplicant *wpa_s = ctx;
3494 struct wpa_ssid *ssid;
3498 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3499 "status=%d " MACSTR,
3500 status, MAC2STR(bssid));
3502 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3503 "status=%d ", status);
3505 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3507 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3508 status, MAC2STR(peer));
3509 if (wpa_s->pending_invite_ssid_id == -1) {
3510 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3511 wpas_remove_persistent_client(wpa_s, peer);
3512 return; /* Invitation to active group */
3515 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3516 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3517 "invitation exchange to indicate readiness for "
3521 if (status != P2P_SC_SUCCESS) {
3522 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3523 ssid = wpa_config_get_network(
3524 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3525 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3527 wpas_p2p_remove_pending_group_interface(wpa_s);
3531 ssid = wpa_config_get_network(wpa_s->conf,
3532 wpa_s->pending_invite_ssid_id);
3534 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3535 "data matching with invitation");
3540 * The peer could have missed our ctrl::ack frame for Invitation
3541 * Response and continue retransmitting the frame. To reduce the
3542 * likelihood of the peer not getting successful TX status for the
3543 * Invitation Response frame, wait a short time here before starting
3544 * the persistent group so that we will remain on the current channel to
3545 * acknowledge any possible retransmission from the peer.
3547 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3548 "starting persistent group");
3551 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3552 freq_included(channels, neg_freq))
3554 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3555 freq_included(channels, peer_oper_freq))
3556 freq = peer_oper_freq;
3560 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3561 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3562 wpas_p2p_group_add_persistent(wpa_s, ssid,
3563 ssid->mode == WPAS_MODE_P2P_GO,
3564 wpa_s->p2p_persistent_go_freq,
3566 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3568 ssid->mode == WPAS_MODE_P2P_GO ?
3569 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3574 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3577 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3579 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3583 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3585 reg->channel[reg->channels] = chan;
3590 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3591 struct p2p_channels *chan,
3592 struct p2p_channels *cli_chan)
3596 wpa_s->global->p2p_24ghz_social_channels = 1;
3598 os_memset(cli_chan, 0, sizeof(*cli_chan));
3600 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3603 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3604 chan->reg_class[cla].reg_class = 81;
3605 chan->reg_class[cla].channels = 0;
3606 for (i = 0; i < 11; i++) {
3607 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3608 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3610 if (chan->reg_class[cla].channels)
3613 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3616 /* Operating class 115 - 5 GHz, channels 36-48 */
3617 chan->reg_class[cla].reg_class = 115;
3618 chan->reg_class[cla].channels = 0;
3619 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3620 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3621 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3622 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3623 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3624 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3625 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3626 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3627 if (chan->reg_class[cla].channels)
3630 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3633 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3634 chan->reg_class[cla].reg_class = 124;
3635 chan->reg_class[cla].channels = 0;
3636 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3637 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3638 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3639 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3640 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3641 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3642 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3643 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3644 if (chan->reg_class[cla].channels)
3647 chan->reg_classes = cla;
3652 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3654 enum hostapd_hw_mode mode)
3658 for (i = 0; i < num_modes; i++) {
3659 if (modes[i].mode == mode)
3668 NOT_ALLOWED, NO_IR, ALLOWED
3671 static int has_channel(struct wpa_global *global,
3672 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3677 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3679 if (wpas_p2p_disallowed_freq(global, freq))
3682 for (i = 0; i < mode->num_channels; i++) {
3683 if (mode->channels[i].chan == chan) {
3685 *flags = mode->channels[i].flag;
3686 if (mode->channels[i].flag &
3687 (HOSTAPD_CHAN_DISABLED |
3688 HOSTAPD_CHAN_RADAR))
3690 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3700 struct p2p_oper_class_map {
3701 enum hostapd_hw_mode mode;
3706 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
3709 static struct p2p_oper_class_map op_class[] = {
3710 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3711 #if 0 /* Do not enable HT40 on 2 GHz for now */
3712 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3713 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3715 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3716 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3717 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3718 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3719 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3720 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3723 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3724 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3725 * 80 MHz, but currently use the following definition for simplicity
3726 * (these center frequencies are not actual channels, which makes
3727 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3728 * removing invalid channels.
3730 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3731 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
3732 { -1, 0, 0, 0, 0, BW20 }
3736 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3737 struct hostapd_hw_modes *mode,
3740 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3743 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3746 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3748 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3749 * so the center channel is 6 channels away from the start/end.
3751 if (channel >= center_channels[i] - 6 &&
3752 channel <= center_channels[i] + 6)
3753 return center_channels[i];
3759 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3760 struct hostapd_hw_modes *mode,
3765 enum chan_allowed res, ret = ALLOWED;
3767 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3770 if (center_chan >= 58 && center_chan <= 138)
3771 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3773 /* check all the channels are available */
3774 for (i = 0; i < 4; i++) {
3775 int adj_chan = center_chan - 6 + i * 4;
3777 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3778 if (res == NOT_ALLOWED)
3783 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3785 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3787 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3789 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3797 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3798 struct hostapd_hw_modes *mode,
3802 enum chan_allowed res, res2;
3804 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3805 if (bw == BW40MINUS) {
3806 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3808 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3809 } else if (bw == BW40PLUS) {
3810 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3812 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3813 } else if (bw == BW80) {
3814 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3817 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3819 if (res == NO_IR || res2 == NO_IR)
3825 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3826 struct p2p_channels *chan,
3827 struct p2p_channels *cli_chan)
3829 struct hostapd_hw_modes *mode;
3830 int cla, op, cli_cla;
3832 if (wpa_s->hw.modes == NULL) {
3833 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3834 "of all supported channels; assume dualband "
3836 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3841 for (op = 0; op_class[op].op_class; op++) {
3842 struct p2p_oper_class_map *o = &op_class[op];
3844 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3846 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3849 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3850 wpa_s->global->p2p_24ghz_social_channels = 1;
3851 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3852 enum chan_allowed res;
3853 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3854 if (res == ALLOWED) {
3856 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3858 reg = &chan->reg_class[cla];
3860 reg->reg_class = o->op_class;
3862 reg->channel[reg->channels] = ch;
3864 } else if (res == NO_IR &&
3865 wpa_s->conf->p2p_add_cli_chan) {
3866 if (cli_reg == NULL) {
3867 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3869 cli_reg = &cli_chan->reg_class[cli_cla];
3871 cli_reg->reg_class = o->op_class;
3873 cli_reg->channel[cli_reg->channels] = ch;
3874 cli_reg->channels++;
3878 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3879 reg->channel, reg->channels);
3882 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3883 cli_reg->channel, cli_reg->channels);
3887 chan->reg_classes = cla;
3888 cli_chan->reg_classes = cli_cla;
3894 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3895 struct hostapd_hw_modes *mode, u8 channel)
3898 enum chan_allowed ret;
3900 for (op = 0; op_class[op].op_class; op++) {
3901 struct p2p_oper_class_map *o = &op_class[op];
3904 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3905 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3906 o->bw == BW20 || ch != channel)
3908 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3910 return (o->bw == BW40MINUS) ? -1 : 1;
3917 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3918 struct hostapd_hw_modes *mode, u8 channel)
3920 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3923 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3927 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3930 struct wpa_supplicant *wpa_s = ctx;
3932 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3933 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3939 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3943 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3944 const u8 *ssid, size_t ssid_len)
3946 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3947 struct wpa_ssid *s = wpa_s->current_ssid;
3950 if (s->mode != WPAS_MODE_P2P_GO &&
3951 s->mode != WPAS_MODE_AP &&
3952 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3954 if (s->ssid_len != ssid_len ||
3955 os_memcmp(ssid, s->ssid, ssid_len) != 0)
3965 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3966 const u8 *peer_dev_addr)
3968 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3969 struct wpa_ssid *ssid = wpa_s->current_ssid;
3972 if (ssid->mode != WPAS_MODE_INFRA)
3974 if (wpa_s->wpa_state != WPA_COMPLETED &&
3975 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3977 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3985 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3987 struct wpa_supplicant *wpa_s = ctx;
3989 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3993 static int wpas_is_concurrent_session_active(void *ctx)
3995 struct wpa_supplicant *wpa_s = ctx;
3996 struct wpa_supplicant *ifs;
3998 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
4001 if (ifs->wpa_state > WPA_ASSOCIATED)
4008 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
4010 struct wpa_supplicant *wpa_s = ctx;
4011 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
4015 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
4016 const char *conf_p2p_dev)
4018 struct wpa_interface iface;
4019 struct wpa_supplicant *p2pdev_wpa_s;
4021 char force_name[100];
4024 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
4026 if (os_snprintf_error(sizeof(ifname), ret))
4028 force_name[0] = '\0';
4029 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
4030 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
4031 force_name, wpa_s->pending_interface_addr, NULL);
4033 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
4036 os_strlcpy(wpa_s->pending_interface_name, ifname,
4037 sizeof(wpa_s->pending_interface_name));
4039 os_memset(&iface, 0, sizeof(iface));
4041 iface.ifname = wpa_s->pending_interface_name;
4042 iface.driver = wpa_s->driver->name;
4043 iface.driver_param = wpa_s->conf->driver_param;
4046 * If a P2P Device configuration file was given, use it as the interface
4047 * configuration file (instead of using parent's configuration file.
4050 iface.confname = conf_p2p_dev;
4051 iface.ctrl_interface = NULL;
4053 iface.confname = wpa_s->confname;
4054 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
4056 iface.conf_p2p_dev = NULL;
4058 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
4059 if (!p2pdev_wpa_s) {
4060 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
4063 p2pdev_wpa_s->parent = wpa_s;
4064 wpa_s->p2p_dev = p2pdev_wpa_s;
4066 wpa_s->pending_interface_name[0] = '\0';
4071 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
4072 const u8 *noa, size_t noa_len)
4074 struct wpa_supplicant *wpa_s, *intf = ctx;
4077 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4078 if (wpa_s->waiting_presence_resp)
4082 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4085 wpa_s->waiting_presence_resp = 0;
4087 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4088 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4089 " status=%u noa=%s", MAC2STR(src), status, hex);
4093 static int _wpas_p2p_in_progress(void *ctx)
4095 struct wpa_supplicant *wpa_s = ctx;
4096 return wpas_p2p_in_progress(wpa_s);
4101 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4102 * @global: Pointer to global data from wpa_supplicant_init()
4103 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4104 * Returns: 0 on success, -1 on failure
4106 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4108 struct p2p_config p2p;
4111 if (wpa_s->conf->p2p_disabled)
4114 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4120 os_memset(&p2p, 0, sizeof(p2p));
4122 p2p.debug_print = wpas_p2p_debug_print;
4123 p2p.p2p_scan = wpas_p2p_scan;
4124 p2p.send_action = wpas_send_action;
4125 p2p.send_action_done = wpas_send_action_done;
4126 p2p.go_neg_completed = wpas_go_neg_completed;
4127 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4128 p2p.dev_found = wpas_dev_found;
4129 p2p.dev_lost = wpas_dev_lost;
4130 p2p.find_stopped = wpas_find_stopped;
4131 p2p.start_listen = wpas_start_listen;
4132 p2p.stop_listen = wpas_stop_listen;
4133 p2p.send_probe_resp = wpas_send_probe_resp;
4134 p2p.sd_request = wpas_sd_request;
4135 p2p.sd_response = wpas_sd_response;
4136 p2p.prov_disc_req = wpas_prov_disc_req;
4137 p2p.prov_disc_resp = wpas_prov_disc_resp;
4138 p2p.prov_disc_fail = wpas_prov_disc_fail;
4139 p2p.invitation_process = wpas_invitation_process;
4140 p2p.invitation_received = wpas_invitation_received;
4141 p2p.invitation_result = wpas_invitation_result;
4142 p2p.get_noa = wpas_get_noa;
4143 p2p.go_connected = wpas_go_connected;
4144 p2p.presence_resp = wpas_presence_resp;
4145 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
4146 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
4148 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
4149 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
4150 p2p.dev_name = wpa_s->conf->device_name;
4151 p2p.manufacturer = wpa_s->conf->manufacturer;
4152 p2p.model_name = wpa_s->conf->model_name;
4153 p2p.model_number = wpa_s->conf->model_number;
4154 p2p.serial_number = wpa_s->conf->serial_number;
4156 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4157 p2p.config_methods = wpa_s->wps->config_methods;
4160 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4161 wpa_printf(MSG_ERROR,
4162 "P2P: Failed to configure supported channel list");
4166 if (wpa_s->conf->p2p_listen_reg_class &&
4167 wpa_s->conf->p2p_listen_channel) {
4168 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4169 p2p.channel = wpa_s->conf->p2p_listen_channel;
4170 p2p.channel_forced = 1;
4173 * Pick one of the social channels randomly as the listen
4176 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4177 &p2p.channel) != 0) {
4178 wpa_printf(MSG_ERROR,
4179 "P2P: Failed to select random social channel as listen channel");
4182 p2p.channel_forced = 0;
4184 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4185 p2p.reg_class, p2p.channel);
4187 if (wpa_s->conf->p2p_oper_reg_class &&
4188 wpa_s->conf->p2p_oper_channel) {
4189 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4190 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4191 p2p.cfg_op_channel = 1;
4192 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4193 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4197 * Use random operation channel from 2.4 GHz band social
4198 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4199 * other preference is indicated.
4201 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4202 &p2p.op_channel) != 0) {
4203 wpa_printf(MSG_ERROR,
4204 "P2P: Failed to select random social channel as operation channel");
4207 p2p.cfg_op_channel = 0;
4208 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4209 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4212 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4213 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4214 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4217 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4218 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4219 p2p.country[2] = 0x04;
4221 os_memcpy(p2p.country, "XX\x04", 3);
4223 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4226 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4227 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4228 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4230 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4231 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4233 p2p.max_peers = 100;
4235 if (wpa_s->conf->p2p_ssid_postfix) {
4236 p2p.ssid_postfix_len =
4237 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4238 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4239 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4240 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4241 p2p.ssid_postfix_len);
4244 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4246 p2p.max_listen = wpa_s->max_remain_on_chan;
4248 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4249 wpa_s->conf->p2p_passphrase_len <= 63)
4250 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4252 p2p.passphrase_len = 8;
4254 global->p2p = p2p_init(&p2p);
4255 if (global->p2p == NULL)
4257 global->p2p_init_wpa_s = wpa_s;
4259 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4260 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4262 p2p_add_wps_vendor_extension(
4263 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4266 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4273 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4274 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4276 * This function deinitialize per-interface P2P data.
4278 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4280 if (wpa_s->driver && wpa_s->drv_priv)
4281 wpa_drv_probe_req_report(wpa_s, 0);
4283 if (wpa_s->go_params) {
4284 /* Clear any stored provisioning info */
4285 p2p_clear_provisioning_info(
4287 wpa_s->go_params->peer_device_addr);
4290 os_free(wpa_s->go_params);
4291 wpa_s->go_params = NULL;
4292 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4293 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4294 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4295 wpa_s->p2p_long_listen = 0;
4296 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4297 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4298 wpas_p2p_remove_pending_group_interface(wpa_s);
4299 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4300 wpas_p2p_listen_work_done(wpa_s);
4301 if (wpa_s->p2p_send_action_work) {
4302 os_free(wpa_s->p2p_send_action_work->ctx);
4303 radio_work_done(wpa_s->p2p_send_action_work);
4304 wpa_s->p2p_send_action_work = NULL;
4306 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4308 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4309 wpa_s->p2p_oob_dev_pw = NULL;
4311 os_free(wpa_s->p2p_group_common_freqs);
4312 wpa_s->p2p_group_common_freqs = NULL;
4313 wpa_s->p2p_group_common_freqs_num = 0;
4315 /* TODO: remove group interface from the driver if this wpa_s instance
4316 * is on top of a P2P group interface */
4321 * wpas_p2p_deinit_global - Deinitialize global P2P module
4322 * @global: Pointer to global data from wpa_supplicant_init()
4324 * This function deinitializes the global (per device) P2P module.
4326 static void wpas_p2p_deinit_global(struct wpa_global *global)
4328 struct wpa_supplicant *wpa_s, *tmp;
4330 wpa_s = global->ifaces;
4332 wpas_p2p_service_flush(global->p2p_init_wpa_s);
4334 /* Remove remaining P2P group interfaces */
4335 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4336 wpa_s = wpa_s->next;
4338 tmp = global->ifaces;
4341 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4346 /* Disconnect from the P2P group and deinit the interface */
4347 wpas_p2p_disconnect(tmp);
4351 * Deinit GO data on any possibly remaining interface (if main
4352 * interface is used as GO).
4354 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4355 if (wpa_s->ap_iface)
4356 wpas_p2p_group_deinit(wpa_s);
4359 p2p_deinit(global->p2p);
4361 global->p2p_init_wpa_s = NULL;
4365 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4367 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4368 wpa_s->conf->p2p_no_group_iface)
4369 return 0; /* separate interface disabled per configuration */
4370 if (wpa_s->drv_flags &
4371 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4372 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4373 return 1; /* P2P group requires a new interface in every case
4375 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4376 return 0; /* driver does not support concurrent operations */
4377 if (wpa_s->global->ifaces->next)
4378 return 1; /* more that one interface already in use */
4379 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4380 return 1; /* this interface is already in use */
4385 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4386 const u8 *peer_addr,
4387 enum p2p_wps_method wps_method,
4388 int go_intent, const u8 *own_interface_addr,
4389 unsigned int force_freq, int persistent_group,
4390 struct wpa_ssid *ssid, unsigned int pref_freq)
4392 if (persistent_group && wpa_s->conf->persistent_reconnect)
4393 persistent_group = 2;
4396 * Increase GO config timeout if HT40 is used since it takes some time
4397 * to scan channels for coex purposes before the BSS can be started.
4399 p2p_set_config_timeout(wpa_s->global->p2p,
4400 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4402 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4403 go_intent, own_interface_addr, force_freq,
4404 persistent_group, ssid ? ssid->ssid : NULL,
4405 ssid ? ssid->ssid_len : 0,
4406 wpa_s->p2p_pd_before_go_neg, pref_freq,
4407 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4412 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4413 const u8 *peer_addr,
4414 enum p2p_wps_method wps_method,
4415 int go_intent, const u8 *own_interface_addr,
4416 unsigned int force_freq, int persistent_group,
4417 struct wpa_ssid *ssid, unsigned int pref_freq)
4419 if (persistent_group && wpa_s->conf->persistent_reconnect)
4420 persistent_group = 2;
4422 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4423 go_intent, own_interface_addr, force_freq,
4424 persistent_group, ssid ? ssid->ssid : NULL,
4425 ssid ? ssid->ssid_len : 0, pref_freq,
4426 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4431 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4433 wpa_s->p2p_join_scan_count++;
4434 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4435 wpa_s->p2p_join_scan_count);
4436 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4437 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4438 " for join operationg - stop join attempt",
4439 MAC2STR(wpa_s->pending_join_iface_addr));
4440 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4441 if (wpa_s->p2p_auto_pd) {
4442 wpa_s->p2p_auto_pd = 0;
4443 wpa_msg_global(wpa_s, MSG_INFO,
4444 P2P_EVENT_PROV_DISC_FAILURE
4445 " p2p_dev_addr=" MACSTR " status=N/A",
4446 MAC2STR(wpa_s->pending_join_dev_addr));
4449 wpa_msg_global(wpa_s->parent, MSG_INFO,
4450 P2P_EVENT_GROUP_FORMATION_FAILURE);
4455 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4458 unsigned int num, i;
4459 struct wpa_used_freq_data *freqs;
4461 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4462 /* Multiple channels are supported and not all are in use */
4466 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4467 sizeof(struct wpa_used_freq_data));
4471 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4472 wpa_s->num_multichan_concurrent);
4474 for (i = 0; i < num; i++) {
4475 if (freqs[i].freq == freq) {
4476 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4483 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4492 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4493 const u8 *peer_dev_addr)
4495 struct wpa_bss *bss;
4498 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4501 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4502 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4507 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4509 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4510 "%ld.%06ld (%supdated in last scan)",
4511 bss->last_update.sec, bss->last_update.usec,
4512 updated ? "": "not ");
4518 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4519 struct wpa_scan_results *scan_res)
4521 struct wpa_bss *bss = NULL;
4523 u8 iface_addr[ETH_ALEN];
4525 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4527 if (wpa_s->global->p2p_disabled)
4530 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4531 scan_res ? (int) scan_res->num : -1,
4532 wpa_s->p2p_auto_join ? "auto_" : "");
4535 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4537 if (wpa_s->p2p_auto_pd) {
4538 int join = wpas_p2p_peer_go(wpa_s,
4539 wpa_s->pending_join_dev_addr);
4541 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4542 wpa_s->auto_pd_scan_retry++;
4543 bss = wpa_bss_get_bssid_latest(
4544 wpa_s, wpa_s->pending_join_dev_addr);
4547 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4548 "the peer " MACSTR " at %d MHz",
4549 wpa_s->auto_pd_scan_retry,
4551 pending_join_dev_addr),
4553 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4561 wpa_s->p2p_auto_pd = 0;
4562 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4563 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4564 MAC2STR(wpa_s->pending_join_dev_addr), join);
4565 if (p2p_prov_disc_req(wpa_s->global->p2p,
4566 wpa_s->pending_join_dev_addr,
4567 wpa_s->pending_pd_config_methods, join,
4568 0, wpa_s->user_initiated_pd) < 0) {
4569 wpa_s->p2p_auto_pd = 0;
4570 wpa_msg_global(wpa_s, MSG_INFO,
4571 P2P_EVENT_PROV_DISC_FAILURE
4572 " p2p_dev_addr=" MACSTR " status=N/A",
4573 MAC2STR(wpa_s->pending_join_dev_addr));
4578 if (wpa_s->p2p_auto_join) {
4579 int join = wpas_p2p_peer_go(wpa_s,
4580 wpa_s->pending_join_dev_addr);
4582 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4583 "running a GO -> use GO Negotiation");
4584 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4585 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4586 wpa_s->p2p_persistent_group, 0, 0, 0,
4587 wpa_s->p2p_go_intent,
4588 wpa_s->p2p_connect_freq,
4589 wpa_s->p2p_persistent_id,
4590 wpa_s->p2p_pd_before_go_neg,
4596 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4597 "try to join the group", join ? "" :
4600 wpa_s->p2p_fallback_to_go_neg = 1;
4603 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4604 wpa_s->pending_join_iface_addr);
4606 p2p_get_interface_addr(wpa_s->global->p2p,
4607 wpa_s->pending_join_dev_addr,
4609 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4610 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
4611 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4612 "address for join from " MACSTR " to " MACSTR
4613 " based on newly discovered P2P peer entry",
4614 MAC2STR(wpa_s->pending_join_iface_addr),
4615 MAC2STR(iface_addr));
4616 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4619 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4620 wpa_s->pending_join_iface_addr);
4623 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4624 "from P2P peer table: %d MHz", freq);
4626 if (wpa_s->p2p_join_ssid_len) {
4627 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4628 MACSTR " and SSID %s",
4629 MAC2STR(wpa_s->pending_join_iface_addr),
4630 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4631 wpa_s->p2p_join_ssid_len));
4632 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4633 wpa_s->p2p_join_ssid,
4634 wpa_s->p2p_join_ssid_len);
4637 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4638 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4639 bss = wpa_bss_get_bssid_latest(wpa_s,
4640 wpa_s->pending_join_iface_addr);
4644 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4645 "from BSS table: %d MHz (SSID %s)", freq,
4646 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4651 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4652 wpa_msg_global(wpa_s->parent, MSG_INFO,
4653 P2P_EVENT_GROUP_FORMATION_FAILURE
4654 "reason=FREQ_CONFLICT");
4658 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4659 "prior to joining an existing group (GO " MACSTR
4661 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4662 wpa_s->pending_pd_before_join = 1;
4664 switch (wpa_s->pending_join_wps_method) {
4665 case WPS_PIN_DISPLAY:
4666 method = WPS_CONFIG_KEYPAD;
4668 case WPS_PIN_KEYPAD:
4669 method = WPS_CONFIG_DISPLAY;
4672 method = WPS_CONFIG_PUSHBUTTON;
4679 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4680 wpa_s->pending_join_dev_addr) ==
4683 * We have already performed provision discovery for
4684 * joining the group. Proceed directly to join
4685 * operation without duplicated provision discovery. */
4686 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4687 "with " MACSTR " already done - proceed to "
4689 MAC2STR(wpa_s->pending_join_dev_addr));
4690 wpa_s->pending_pd_before_join = 0;
4694 if (p2p_prov_disc_req(wpa_s->global->p2p,
4695 wpa_s->pending_join_dev_addr, method, 1,
4696 freq, wpa_s->user_initiated_pd) < 0) {
4697 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4698 "Discovery Request before joining an "
4700 wpa_s->pending_pd_before_join = 0;
4706 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4707 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4708 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4709 wpas_p2p_check_join_scan_limit(wpa_s);
4713 /* Start join operation immediately */
4714 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4718 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4719 const u8 *ssid, size_t ssid_len)
4722 struct wpa_driver_scan_params params;
4723 struct wpabuf *wps_ie, *ies;
4725 int freqs[2] = { 0, 0 };
4727 os_memset(¶ms, 0, sizeof(params));
4729 /* P2P Wildcard SSID */
4730 params.num_ssids = 1;
4731 if (ssid && ssid_len) {
4732 params.ssids[0].ssid = ssid;
4733 params.ssids[0].ssid_len = ssid_len;
4734 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4735 wpa_s->p2p_join_ssid_len = ssid_len;
4737 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4738 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4739 wpa_s->p2p_join_ssid_len = 0;
4742 wpa_s->wps->dev.p2p = 1;
4743 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4744 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4746 if (wps_ie == NULL) {
4747 wpas_p2p_scan_res_join(wpa_s, NULL);
4751 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4752 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4754 wpabuf_free(wps_ie);
4755 wpas_p2p_scan_res_join(wpa_s, NULL);
4758 wpabuf_put_buf(ies, wps_ie);
4759 wpabuf_free(wps_ie);
4761 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4763 params.p2p_probe = 1;
4764 params.extra_ies = wpabuf_head(ies);
4765 params.extra_ies_len = wpabuf_len(ies);
4770 * If freq is not provided, check the operating freq of the GO
4771 * and use a single channel scan on if possible.
4773 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4774 wpa_s->pending_join_iface_addr);
4780 params.freqs = freqs;
4784 * Run a scan to update BSS table and start Provision Discovery once
4785 * the new scan results become available.
4787 ret = wpa_drv_scan(wpa_s, ¶ms);
4789 os_get_reltime(&wpa_s->scan_trigger_time);
4790 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4791 wpa_s->own_scan_requested = 1;
4797 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4799 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4800 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4801 wpas_p2p_check_join_scan_limit(wpa_s);
4806 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4808 struct wpa_supplicant *wpa_s = eloop_ctx;
4809 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4813 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4814 const u8 *dev_addr, enum p2p_wps_method wps_method,
4815 int auto_join, int op_freq,
4816 const u8 *ssid, size_t ssid_len)
4818 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4819 MACSTR " dev " MACSTR " op_freq=%d)%s",
4820 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4821 auto_join ? " (auto_join)" : "");
4822 if (ssid && ssid_len) {
4823 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4824 wpa_ssid_txt(ssid, ssid_len));
4827 wpa_s->p2p_auto_pd = 0;
4828 wpa_s->p2p_auto_join = !!auto_join;
4829 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4830 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4831 wpa_s->pending_join_wps_method = wps_method;
4833 /* Make sure we are not running find during connection establishment */
4834 wpas_p2p_stop_find(wpa_s);
4836 wpa_s->p2p_join_scan_count = 0;
4837 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4842 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4843 const u8 *ssid, size_t ssid_len)
4845 struct wpa_supplicant *group;
4846 struct p2p_go_neg_results res;
4847 struct wpa_bss *bss;
4849 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4852 if (group != wpa_s) {
4853 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4854 sizeof(group->p2p_pin));
4855 group->p2p_wps_method = wpa_s->p2p_wps_method;
4858 * Need to mark the current interface for p2p_group_formation
4859 * when a separate group interface is not used. This is needed
4860 * to allow p2p_cancel stop a pending p2p_connect-join.
4861 * wpas_p2p_init_group_interface() addresses this for the case
4862 * where a separate group interface is used.
4864 wpa_s->global->p2p_group_formation = wpa_s;
4867 group->p2p_in_provisioning = 1;
4868 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4870 os_memset(&res, 0, sizeof(res));
4871 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4872 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4874 res.wps_method = wpa_s->pending_join_wps_method;
4875 if (freq && ssid && ssid_len) {
4877 res.ssid_len = ssid_len;
4878 os_memcpy(res.ssid, ssid, ssid_len);
4880 bss = wpa_bss_get_bssid_latest(wpa_s,
4881 wpa_s->pending_join_iface_addr);
4883 res.freq = bss->freq;
4884 res.ssid_len = bss->ssid_len;
4885 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4886 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4888 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4892 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4893 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4895 wpa_drv_cancel_remain_on_channel(wpa_s);
4896 wpa_s->off_channel_freq = 0;
4897 wpa_s->roc_waiting_drv_freq = 0;
4899 wpas_start_wps_enrollee(group, &res);
4902 * Allow a longer timeout for join-a-running-group than normal 15
4903 * second group formation timeout since the GO may not have authorized
4904 * our connection yet.
4906 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4907 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4914 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4915 int *force_freq, int *pref_freq, int go)
4917 struct wpa_used_freq_data *freqs;
4918 int res, best_freq, num_unused;
4919 unsigned int freq_in_use = 0, num, i;
4921 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4922 sizeof(struct wpa_used_freq_data));
4926 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4927 wpa_s->num_multichan_concurrent);
4930 * It is possible that the total number of used frequencies is bigger
4931 * than the number of frequencies used for P2P, so get the system wide
4932 * number of unused frequencies.
4934 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4936 wpa_printf(MSG_DEBUG,
4937 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4938 freq, wpa_s->num_multichan_concurrent, num, num_unused);
4943 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4945 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4947 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4948 "(%u MHz) is not supported for P2P uses",
4954 for (i = 0; i < num; i++) {
4955 if (freqs[i].freq == freq)
4959 if (num_unused <= 0 && !freq_in_use) {
4960 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4965 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4966 "requested channel (%u MHz)", freq);
4971 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
4973 /* We have a candidate frequency to use */
4974 if (best_freq > 0) {
4975 if (*pref_freq == 0 && num_unused > 0) {
4976 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4978 *pref_freq = best_freq;
4980 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4982 *force_freq = best_freq;
4984 } else if (num_unused > 0) {
4985 wpa_printf(MSG_DEBUG,
4986 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4989 wpa_printf(MSG_DEBUG,
4990 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
5004 * wpas_p2p_connect - Request P2P Group Formation to be started
5005 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5006 * @peer_addr: Address of the peer P2P Device
5007 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
5008 * @persistent_group: Whether to create a persistent group
5009 * @auto_join: Whether to select join vs. GO Negotiation automatically
5010 * @join: Whether to join an existing group (as a client) instead of starting
5011 * Group Owner negotiation; @peer_addr is BSSID in that case
5012 * @auth: Whether to only authorize the connection instead of doing that and
5013 * initiating Group Owner negotiation
5014 * @go_intent: GO Intent or -1 to use default
5015 * @freq: Frequency for the group or 0 for auto-selection
5016 * @persistent_id: Persistent group credentials to use for forcing GO
5017 * parameters or -1 to generate new values (SSID/passphrase)
5018 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
5019 * interoperability workaround when initiating group formation
5020 * @ht40: Start GO with 40 MHz channel width
5021 * @vht: Start GO with VHT support
5022 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
5023 * failure, -2 on failure due to channel not currently available,
5024 * -3 if forced channel is not supported
5026 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5027 const char *pin, enum p2p_wps_method wps_method,
5028 int persistent_group, int auto_join, int join, int auth,
5029 int go_intent, int freq, int persistent_id, int pd,
5032 int force_freq = 0, pref_freq = 0;
5034 enum wpa_driver_if_type iftype;
5036 struct wpa_ssid *ssid = NULL;
5038 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5041 if (persistent_id >= 0) {
5042 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
5043 if (ssid == NULL || ssid->disabled != 2 ||
5044 ssid->mode != WPAS_MODE_P2P_GO)
5048 os_free(wpa_s->global->add_psk);
5049 wpa_s->global->add_psk = NULL;
5051 wpa_s->global->p2p_fail_on_wps_complete = 0;
5054 go_intent = wpa_s->conf->p2p_go_intent;
5057 wpa_s->p2p_long_listen = 0;
5059 wpa_s->p2p_wps_method = wps_method;
5060 wpa_s->p2p_persistent_group = !!persistent_group;
5061 wpa_s->p2p_persistent_id = persistent_id;
5062 wpa_s->p2p_go_intent = go_intent;
5063 wpa_s->p2p_connect_freq = freq;
5064 wpa_s->p2p_fallback_to_go_neg = 0;
5065 wpa_s->p2p_pd_before_go_neg = !!pd;
5066 wpa_s->p2p_go_ht40 = !!ht40;
5067 wpa_s->p2p_go_vht = !!vht;
5070 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
5071 else if (wps_method == WPS_PIN_DISPLAY) {
5072 ret = wps_generate_pin();
5073 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
5075 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5076 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
5077 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5080 wpa_s->p2p_pin[0] = '\0';
5082 if (join || auto_join) {
5083 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5085 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5086 "connect a running group from " MACSTR,
5087 MAC2STR(peer_addr));
5088 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5091 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5092 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5094 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5095 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5099 os_get_reltime(&wpa_s->p2p_auto_started);
5100 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5102 wpa_s->p2p_auto_started.sec,
5103 wpa_s->p2p_auto_started.usec);
5105 wpa_s->user_initiated_pd = 1;
5106 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
5107 auto_join, freq, NULL, 0) < 0)
5112 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5116 wpas_p2p_set_own_freq_preference(wpa_s,
5117 force_freq ? force_freq : pref_freq);
5119 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5121 if (wpa_s->create_p2p_iface) {
5122 /* Prepare to add a new interface for the group */
5123 iftype = WPA_IF_P2P_GROUP;
5124 if (go_intent == 15)
5125 iftype = WPA_IF_P2P_GO;
5126 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5127 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5128 "interface for the group");
5132 if_addr = wpa_s->pending_interface_addr;
5134 if_addr = wpa_s->own_addr;
5137 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
5139 force_freq, persistent_group, ssid,
5145 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5146 go_intent, if_addr, force_freq,
5147 persistent_group, ssid, pref_freq) < 0) {
5148 if (wpa_s->create_p2p_iface)
5149 wpas_p2p_remove_pending_group_interface(wpa_s);
5157 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5158 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5159 * @freq: Frequency of the channel in MHz
5160 * @duration: Duration of the stay on the channel in milliseconds
5162 * This callback is called when the driver indicates that it has started the
5163 * requested remain-on-channel duration.
5165 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5166 unsigned int freq, unsigned int duration)
5168 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5170 wpa_printf(MSG_DEBUG, "P2P: remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d roc_waiting_drv_freq=%d freq=%u duration=%u)",
5171 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5172 wpa_s->roc_waiting_drv_freq, freq, duration);
5173 if (wpa_s->off_channel_freq &&
5174 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5175 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5176 wpa_s->pending_listen_duration);
5177 wpa_s->pending_listen_freq = 0;
5179 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5180 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5186 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
5188 /* Limit maximum Listen state time based on driver limitation. */
5189 if (timeout > wpa_s->max_remain_on_chan)
5190 timeout = wpa_s->max_remain_on_chan;
5192 return p2p_listen(wpa_s->global->p2p, timeout);
5197 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5198 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5199 * @freq: Frequency of the channel in MHz
5201 * This callback is called when the driver indicates that a remain-on-channel
5202 * operation has been completed, i.e., the duration on the requested channel
5205 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5208 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5209 "(p2p_long_listen=%d ms pending_action_tx=%p)",
5210 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
5211 wpas_p2p_listen_work_done(wpa_s);
5212 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5214 if (wpa_s->p2p_long_listen > 0)
5215 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
5216 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5217 return; /* P2P module started a new operation */
5218 if (offchannel_pending_action_tx(wpa_s))
5220 if (wpa_s->p2p_long_listen > 0) {
5221 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5222 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
5225 * When listen duration is over, stop listen & update p2p_state
5228 p2p_stop_listen(wpa_s->global->p2p);
5234 * wpas_p2p_group_remove - Remove a P2P group
5235 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5236 * @ifname: Network interface name of the group interface or "*" to remove all
5238 * Returns: 0 on success, -1 on failure
5240 * This function is used to remove a P2P group. This can be used to disconnect
5241 * from a group in which the local end is a P2P Client or to end a P2P Group in
5242 * case the local end is the Group Owner. If a virtual network interface was
5243 * created for this group, that interface will be removed. Otherwise, only the
5244 * configured P2P group network will be removed from the interface.
5246 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5248 struct wpa_global *global = wpa_s->global;
5249 struct wpa_supplicant *calling_wpa_s = wpa_s;
5251 if (os_strcmp(ifname, "*") == 0) {
5252 struct wpa_supplicant *prev;
5253 wpa_s = global->ifaces;
5256 wpa_s = wpa_s->next;
5257 if (prev->p2p_group_interface !=
5258 NOT_P2P_GROUP_INTERFACE ||
5259 (prev->current_ssid &&
5260 prev->current_ssid->p2p_group))
5261 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
5266 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5267 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5271 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
5275 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5280 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5282 if (wpa_s->best_24_freq > 0 &&
5283 p2p_supported_freq_go(wpa_s->global->p2p,
5284 wpa_s->best_24_freq)) {
5285 freq = wpa_s->best_24_freq;
5286 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5287 "channel: %d MHz", freq);
5289 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5291 freq = 2412 + (r % 3) * 25;
5292 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5293 "channel: %d MHz", freq);
5298 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5300 if (wpa_s->best_5_freq > 0 &&
5301 p2p_supported_freq_go(wpa_s->global->p2p,
5302 wpa_s->best_5_freq)) {
5303 freq = wpa_s->best_5_freq;
5304 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5305 "channel: %d MHz", freq);
5307 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5309 freq = 5180 + (r % 4) * 20;
5310 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5311 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5312 "5 GHz channel for P2P group");
5315 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5316 "channel: %d MHz", freq);
5320 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5321 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5322 "(%u MHz) is not supported for P2P uses",
5331 static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5332 struct p2p_go_neg_results *params,
5333 const struct p2p_channels *channels)
5337 /* first try some random selection of the social channels */
5338 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5341 for (i = 0; i < 3; i++) {
5342 params->freq = 2412 + ((r + i) % 3) * 25;
5343 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5344 freq_included(channels, params->freq) &&
5345 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5349 /* try all channels in reg. class 81 */
5350 for (i = 0; i < 11; i++) {
5351 params->freq = 2412 + i * 5;
5352 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5353 freq_included(channels, params->freq) &&
5354 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5358 /* try social channel class 180 channel 2 */
5359 params->freq = 58320 + 1 * 2160;
5360 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5361 freq_included(channels, params->freq) &&
5362 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5365 /* try all channels in reg. class 180 */
5366 for (i = 0; i < 4; i++) {
5367 params->freq = 58320 + i * 2160;
5368 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5369 freq_included(channels, params->freq) &&
5370 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5374 wpa_printf(MSG_DEBUG, "P2P: No 2.4 and 60 GHz channel allowed");
5377 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5383 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5384 struct p2p_go_neg_results *params,
5385 int freq, int ht40, int vht,
5386 const struct p2p_channels *channels)
5388 struct wpa_used_freq_data *freqs;
5389 unsigned int pref_freq, cand_freq;
5390 unsigned int num, i;
5392 os_memset(params, 0, sizeof(*params));
5393 params->role_go = 1;
5394 params->ht40 = ht40;
5397 if (!freq_included(channels, freq)) {
5398 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5402 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5403 "frequency %d MHz", freq);
5404 params->freq = freq;
5405 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5406 wpa_s->conf->p2p_oper_channel >= 1 &&
5407 wpa_s->conf->p2p_oper_channel <= 11 &&
5408 freq_included(channels,
5409 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5410 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5411 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5412 "frequency %d MHz", params->freq);
5413 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5414 wpa_s->conf->p2p_oper_reg_class == 116 ||
5415 wpa_s->conf->p2p_oper_reg_class == 117 ||
5416 wpa_s->conf->p2p_oper_reg_class == 124 ||
5417 wpa_s->conf->p2p_oper_reg_class == 126 ||
5418 wpa_s->conf->p2p_oper_reg_class == 127) &&
5419 freq_included(channels,
5420 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
5421 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5422 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5423 "frequency %d MHz", params->freq);
5424 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5425 wpa_s->best_overall_freq > 0 &&
5426 p2p_supported_freq_go(wpa_s->global->p2p,
5427 wpa_s->best_overall_freq) &&
5428 freq_included(channels, wpa_s->best_overall_freq)) {
5429 params->freq = wpa_s->best_overall_freq;
5430 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5431 "channel %d MHz", params->freq);
5432 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5433 wpa_s->best_24_freq > 0 &&
5434 p2p_supported_freq_go(wpa_s->global->p2p,
5435 wpa_s->best_24_freq) &&
5436 freq_included(channels, wpa_s->best_24_freq)) {
5437 params->freq = wpa_s->best_24_freq;
5438 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5439 "channel %d MHz", params->freq);
5440 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5441 wpa_s->best_5_freq > 0 &&
5442 p2p_supported_freq_go(wpa_s->global->p2p,
5443 wpa_s->best_5_freq) &&
5444 freq_included(channels, wpa_s->best_5_freq)) {
5445 params->freq = wpa_s->best_5_freq;
5446 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5447 "channel %d MHz", params->freq);
5448 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5450 params->freq = pref_freq;
5451 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5452 "channels", params->freq);
5454 /* no preference, select some channel */
5455 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
5459 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5460 sizeof(struct wpa_used_freq_data));
5464 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5465 wpa_s->num_multichan_concurrent);
5467 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5469 /* First try the best used frequency if possible */
5470 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5471 params->freq = cand_freq;
5473 /* Try any of the used frequencies */
5474 for (i = 0; i < num; i++) {
5475 if (freq_included(channels, freqs[i].freq)) {
5476 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5478 params->freq = freqs[i].freq;
5484 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5485 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5489 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5493 for (i = 0; i < num; i++) {
5494 if (freqs[i].freq == freq)
5499 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5501 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5505 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5515 static struct wpa_supplicant *
5516 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5519 struct wpa_supplicant *group_wpa_s;
5521 if (!wpas_p2p_create_iface(wpa_s)) {
5522 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5524 wpa_s->p2p_first_connection_timeout = 0;
5528 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5529 WPA_IF_P2P_CLIENT) < 0) {
5530 wpa_msg_global(wpa_s, MSG_ERROR,
5531 "P2P: Failed to add group interface");
5534 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5535 if (group_wpa_s == NULL) {
5536 wpa_msg_global(wpa_s, MSG_ERROR,
5537 "P2P: Failed to initialize group interface");
5538 wpas_p2p_remove_pending_group_interface(wpa_s);
5542 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5543 group_wpa_s->ifname);
5544 group_wpa_s->p2p_first_connection_timeout = 0;
5550 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5551 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5552 * @persistent_group: Whether to create a persistent group
5553 * @freq: Frequency for the group or 0 to indicate no hardcoding
5554 * @ht40: Start GO with 40 MHz channel width
5555 * @vht: Start GO with VHT support
5556 * Returns: 0 on success, -1 on failure
5558 * This function creates a new P2P group with the local end as the Group Owner,
5559 * i.e., without using Group Owner Negotiation.
5561 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5562 int freq, int ht40, int vht)
5564 struct p2p_go_neg_results params;
5566 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5569 os_free(wpa_s->global->add_psk);
5570 wpa_s->global->add_psk = NULL;
5572 /* Make sure we are not running find during connection establishment */
5573 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5574 wpas_p2p_stop_find_oper(wpa_s);
5576 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5580 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5583 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5584 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5585 "(%u MHz) is not supported for P2P uses",
5589 p2p_go_params(wpa_s->global->p2p, ¶ms);
5590 params.persistent_group = persistent_group;
5592 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5595 wpas_start_wps_go(wpa_s, ¶ms, 0);
5601 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5602 struct wpa_ssid *params, int addr_allocated,
5605 struct wpa_ssid *ssid;
5607 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5610 wpa_s->p2p_last_4way_hs_fail = NULL;
5612 wpa_supplicant_ap_deinit(wpa_s);
5614 ssid = wpa_config_add_network(wpa_s->conf);
5617 wpa_config_set_network_defaults(ssid);
5618 ssid->temporary = 1;
5619 ssid->proto = WPA_PROTO_RSN;
5620 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5621 ssid->group_cipher = WPA_CIPHER_CCMP;
5622 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5623 ssid->ssid = os_malloc(params->ssid_len);
5624 if (ssid->ssid == NULL) {
5625 wpa_config_remove_network(wpa_s->conf, ssid->id);
5628 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5629 ssid->ssid_len = params->ssid_len;
5630 ssid->p2p_group = 1;
5631 ssid->export_keys = 1;
5632 if (params->psk_set) {
5633 os_memcpy(ssid->psk, params->psk, 32);
5636 if (params->passphrase)
5637 ssid->passphrase = os_strdup(params->passphrase);
5639 wpa_s->show_group_started = 1;
5640 wpa_s->p2p_in_invitation = 1;
5641 wpa_s->p2p_invite_go_freq = freq;
5643 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5645 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5646 wpas_p2p_group_formation_timeout,
5647 wpa_s->parent, NULL);
5648 wpa_supplicant_select_network(wpa_s, ssid);
5654 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5655 struct wpa_ssid *ssid, int addr_allocated,
5656 int force_freq, int neg_freq, int ht40,
5657 int vht, const struct p2p_channels *channels,
5658 int connection_timeout)
5660 struct p2p_go_neg_results params;
5663 if (ssid->disabled != 2 || ssid->ssid == NULL)
5666 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5667 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5668 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5673 os_free(wpa_s->global->add_psk);
5674 wpa_s->global->add_psk = NULL;
5676 /* Make sure we are not running find during connection establishment */
5677 wpas_p2p_stop_find_oper(wpa_s);
5679 wpa_s->p2p_fallback_to_go_neg = 0;
5681 if (ssid->mode == WPAS_MODE_P2P_GO) {
5682 if (force_freq > 0) {
5683 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5687 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5689 (freq > 0 && !freq_included(channels, freq)))
5695 (freq > 0 && !freq_included(channels, freq)))
5699 if (ssid->mode == WPAS_MODE_INFRA)
5700 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5702 if (ssid->mode != WPAS_MODE_P2P_GO)
5705 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
5709 params.psk_set = ssid->psk_set;
5711 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5712 if (ssid->passphrase) {
5713 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5714 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5715 "persistent group");
5718 os_strlcpy(params.passphrase, ssid->passphrase,
5719 sizeof(params.passphrase));
5721 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5722 params.ssid_len = ssid->ssid_len;
5723 params.persistent_group = 1;
5725 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5729 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
5731 wpa_s->p2p_first_connection_timeout = connection_timeout;
5732 wpas_start_wps_go(wpa_s, ¶ms, 0);
5738 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5739 struct wpabuf *proberesp_ies)
5741 struct wpa_supplicant *wpa_s = ctx;
5742 if (wpa_s->ap_iface) {
5743 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5744 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5745 wpabuf_free(beacon_ies);
5746 wpabuf_free(proberesp_ies);
5750 wpabuf_free(hapd->p2p_beacon_ie);
5751 hapd->p2p_beacon_ie = beacon_ies;
5753 wpabuf_free(hapd->p2p_probe_resp_ie);
5754 hapd->p2p_probe_resp_ie = proberesp_ies;
5756 wpabuf_free(beacon_ies);
5757 wpabuf_free(proberesp_ies);
5759 wpa_supplicant_ap_update_beacon(wpa_s);
5763 static void wpas_p2p_idle_update(void *ctx, int idle)
5765 struct wpa_supplicant *wpa_s = ctx;
5766 if (!wpa_s->ap_iface)
5768 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
5770 if (wpa_s->global->p2p_fail_on_wps_complete &&
5771 wpa_s->p2p_in_provisioning) {
5772 wpas_p2p_grpform_fail_after_wps(wpa_s);
5775 wpas_p2p_set_group_idle_timeout(wpa_s);
5777 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5781 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
5782 struct wpa_ssid *ssid)
5784 struct p2p_group *group;
5785 struct p2p_group_config *cfg;
5787 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5790 cfg = os_zalloc(sizeof(*cfg));
5794 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
5795 cfg->persistent_group = 2;
5796 else if (ssid->p2p_persistent_group)
5797 cfg->persistent_group = 1;
5798 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5799 if (wpa_s->max_stations &&
5800 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5801 cfg->max_clients = wpa_s->max_stations;
5803 cfg->max_clients = wpa_s->conf->max_num_sta;
5804 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5805 cfg->ssid_len = ssid->ssid_len;
5806 cfg->freq = ssid->frequency;
5807 cfg->cb_ctx = wpa_s;
5808 cfg->ie_update = wpas_p2p_ie_update;
5809 cfg->idle_update = wpas_p2p_idle_update;
5811 group = p2p_group_init(wpa_s->global->p2p, cfg);
5814 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
5815 p2p_group_notif_formation_done(group);
5816 wpa_s->p2p_group = group;
5821 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5824 struct wpa_ssid *ssid = wpa_s->current_ssid;
5826 if (!wpa_s->p2p_in_provisioning) {
5827 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5828 "provisioning not in progress");
5832 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5833 u8 go_dev_addr[ETH_ALEN];
5834 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5835 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5837 /* Clear any stored provisioning info */
5838 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5841 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5843 wpa_s->p2p_go_group_formation_completed = 1;
5844 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5846 * Use a separate timeout for initial data connection to
5847 * complete to allow the group to be removed automatically if
5848 * something goes wrong in this step before the P2P group idle
5849 * timeout mechanism is taken into use.
5851 wpa_dbg(wpa_s, MSG_DEBUG,
5852 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5853 P2P_MAX_INITIAL_CONN_WAIT);
5854 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5855 wpas_p2p_group_formation_timeout,
5856 wpa_s->parent, NULL);
5859 * Use a separate timeout for initial data connection to
5860 * complete to allow the group to be removed automatically if
5861 * the client does not complete data connection successfully.
5863 wpa_dbg(wpa_s, MSG_DEBUG,
5864 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5865 P2P_MAX_INITIAL_CONN_WAIT_GO);
5866 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5867 wpas_p2p_group_formation_timeout,
5868 wpa_s->parent, NULL);
5870 * Complete group formation on first successful data connection
5872 wpa_s->p2p_go_group_formation_completed = 0;
5874 if (wpa_s->global->p2p)
5875 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5876 wpas_group_formation_completed(wpa_s, 1);
5880 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5881 struct wps_event_fail *fail)
5883 if (!wpa_s->p2p_in_provisioning) {
5884 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5885 "provisioning not in progress");
5889 if (wpa_s->go_params) {
5890 p2p_clear_provisioning_info(
5892 wpa_s->go_params->peer_device_addr);
5895 wpas_notify_p2p_wps_failed(wpa_s, fail);
5897 if (wpa_s == wpa_s->global->p2p_group_formation) {
5899 * Allow some time for the failed WPS negotiation exchange to
5900 * complete, but remove the group since group formation cannot
5901 * succeed after provisioning failure.
5903 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5904 wpa_s->global->p2p_fail_on_wps_complete = 1;
5905 eloop_deplete_timeout(0, 50000,
5906 wpas_p2p_group_formation_timeout,
5907 wpa_s->parent, NULL);
5912 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5914 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5915 !wpa_s->p2p_in_provisioning)
5918 wpas_p2p_grpform_fail_after_wps(wpa_s);
5924 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5925 const char *config_method,
5926 enum wpas_p2p_prov_disc_use use)
5930 wpa_s->p2p_fallback_to_go_neg = 0;
5931 wpa_s->pending_pd_use = NORMAL_PD;
5932 if (os_strncmp(config_method, "display", 7) == 0)
5933 config_methods = WPS_CONFIG_DISPLAY;
5934 else if (os_strncmp(config_method, "keypad", 6) == 0)
5935 config_methods = WPS_CONFIG_KEYPAD;
5936 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5937 os_strncmp(config_method, "pushbutton", 10) == 0)
5938 config_methods = WPS_CONFIG_PUSHBUTTON;
5940 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5944 if (use == WPAS_P2P_PD_AUTO) {
5945 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5946 wpa_s->pending_pd_config_methods = config_methods;
5947 wpa_s->p2p_auto_pd = 1;
5948 wpa_s->p2p_auto_join = 0;
5949 wpa_s->pending_pd_before_join = 0;
5950 wpa_s->auto_pd_scan_retry = 0;
5951 wpas_p2p_stop_find(wpa_s);
5952 wpa_s->p2p_join_scan_count = 0;
5953 os_get_reltime(&wpa_s->p2p_auto_started);
5954 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5955 wpa_s->p2p_auto_started.sec,
5956 wpa_s->p2p_auto_started.usec);
5957 wpas_p2p_join_scan(wpa_s, NULL);
5961 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5964 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
5965 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5970 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5973 return p2p_scan_result_text(ies, ies_len, buf, end);
5977 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5979 if (!offchannel_pending_action_tx(wpa_s))
5982 wpas_p2p_action_tx_clear(wpa_s);
5984 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5985 "operation request");
5986 offchannel_clear_pending_action_tx(wpa_s);
5990 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5991 enum p2p_discovery_type type,
5992 unsigned int num_req_dev_types, const u8 *req_dev_types,
5993 const u8 *dev_id, unsigned int search_delay,
5994 u8 seek_cnt, const char **seek_string)
5996 wpas_p2p_clear_pending_action_tx(wpa_s);
5997 wpa_s->p2p_long_listen = 0;
5999 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
6000 wpa_s->p2p_in_provisioning)
6003 wpa_supplicant_cancel_sched_scan(wpa_s);
6005 return p2p_find(wpa_s->global->p2p, timeout, type,
6006 num_req_dev_types, req_dev_types, dev_id,
6007 search_delay, seek_cnt, seek_string);
6011 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
6012 struct wpa_scan_results *scan_res)
6014 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6016 if (wpa_s->p2p_scan_work) {
6017 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
6018 wpa_s->p2p_scan_work = NULL;
6019 radio_work_done(work);
6022 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6026 * Indicate that results have been processed so that the P2P module can
6027 * continue pending tasks.
6029 p2p_scan_res_handled(wpa_s->global->p2p);
6033 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
6035 wpas_p2p_clear_pending_action_tx(wpa_s);
6036 wpa_s->p2p_long_listen = 0;
6037 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6038 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
6040 if (wpa_s->global->p2p)
6041 p2p_stop_find(wpa_s->global->p2p);
6043 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
6044 wpa_printf(MSG_DEBUG,
6045 "P2P: Do not consider the scan results after stop_find");
6046 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
6051 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
6053 wpas_p2p_stop_find_oper(wpa_s);
6054 wpas_p2p_remove_pending_group_interface(wpa_s);
6058 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
6060 struct wpa_supplicant *wpa_s = eloop_ctx;
6061 wpa_s->p2p_long_listen = 0;
6065 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
6069 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6072 wpa_supplicant_cancel_sched_scan(wpa_s);
6073 wpas_p2p_clear_pending_action_tx(wpa_s);
6077 * This is a request for unlimited Listen state. However, at
6078 * least for now, this is mapped to a Listen state for one
6083 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6084 wpa_s->p2p_long_listen = 0;
6087 * Stop previous find/listen operation to avoid trying to request a new
6088 * remain-on-channel operation while the driver is still running the
6091 if (wpa_s->global->p2p)
6092 p2p_stop_find(wpa_s->global->p2p);
6094 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6095 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6096 wpa_s->p2p_long_listen = timeout * 1000;
6097 eloop_register_timeout(timeout, 0,
6098 wpas_p2p_long_listen_timeout,
6106 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6107 u8 *buf, size_t len, int p2p_group)
6109 struct wpabuf *p2p_ie;
6112 if (wpa_s->global->p2p_disabled)
6114 if (wpa_s->conf->p2p_disabled)
6116 if (wpa_s->global->p2p == NULL)
6121 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6122 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6124 wpabuf_free(p2p_ie);
6130 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
6131 const u8 *dst, const u8 *bssid,
6132 const u8 *ie, size_t ie_len, int ssi_signal)
6134 if (wpa_s->global->p2p_disabled)
6136 if (wpa_s->global->p2p == NULL)
6139 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
6141 case P2P_PREQ_NOT_P2P:
6142 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6145 case P2P_PREQ_MALFORMED:
6146 case P2P_PREQ_NOT_LISTEN:
6147 case P2P_PREQ_NOT_PROCESSED:
6148 default: /* make gcc happy */
6150 case P2P_PREQ_PROCESSED:
6156 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6157 const u8 *sa, const u8 *bssid,
6158 u8 category, const u8 *data, size_t len, int freq)
6160 if (wpa_s->global->p2p_disabled)
6162 if (wpa_s->global->p2p == NULL)
6165 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6170 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6172 if (wpa_s->global->p2p_disabled)
6174 if (wpa_s->global->p2p == NULL)
6177 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
6181 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
6183 p2p_group_deinit(wpa_s->p2p_group);
6184 wpa_s->p2p_group = NULL;
6186 wpa_s->ap_configured_cb = NULL;
6187 wpa_s->ap_configured_cb_ctx = NULL;
6188 wpa_s->ap_configured_cb_data = NULL;
6189 wpa_s->connect_without_scan = NULL;
6193 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6195 wpa_s->p2p_long_listen = 0;
6197 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6200 return p2p_reject(wpa_s->global->p2p, addr);
6204 /* Invite to reinvoke a persistent group */
6205 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6206 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
6207 int ht40, int vht, int pref_freq)
6209 enum p2p_invite_role role;
6213 int no_pref_freq_given = pref_freq == 0;
6215 wpa_s->global->p2p_invite_group = NULL;
6217 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6219 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6221 wpa_s->p2p_persistent_go_freq = freq;
6222 wpa_s->p2p_go_ht40 = !!ht40;
6223 if (ssid->mode == WPAS_MODE_P2P_GO) {
6224 role = P2P_INVITE_ROLE_GO;
6225 if (peer_addr == NULL) {
6226 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6227 "address in invitation command");
6230 if (wpas_p2p_create_iface(wpa_s)) {
6231 if (wpas_p2p_add_group_interface(wpa_s,
6232 WPA_IF_P2P_GO) < 0) {
6233 wpa_printf(MSG_ERROR, "P2P: Failed to "
6234 "allocate a new interface for the "
6238 bssid = wpa_s->pending_interface_addr;
6240 bssid = wpa_s->own_addr;
6242 role = P2P_INVITE_ROLE_CLIENT;
6243 peer_addr = ssid->bssid;
6245 wpa_s->pending_invite_ssid_id = ssid->id;
6247 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6248 role == P2P_INVITE_ROLE_GO);
6252 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6255 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6256 no_pref_freq_given && pref_freq > 0 &&
6257 wpa_s->num_multichan_concurrent > 1 &&
6258 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6259 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6265 * Stop any find/listen operations before invitation and possibly
6266 * connection establishment.
6268 wpas_p2p_stop_find_oper(wpa_s);
6270 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6271 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
6276 /* Invite to join an active group */
6277 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6278 const u8 *peer_addr, const u8 *go_dev_addr)
6280 struct wpa_global *global = wpa_s->global;
6281 enum p2p_invite_role role;
6283 struct wpa_ssid *ssid;
6285 int freq = 0, force_freq = 0, pref_freq = 0;
6288 wpa_s->p2p_persistent_go_freq = 0;
6289 wpa_s->p2p_go_ht40 = 0;
6290 wpa_s->p2p_go_vht = 0;
6292 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6293 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6296 if (wpa_s == NULL) {
6297 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6301 ssid = wpa_s->current_ssid;
6303 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6308 wpa_s->global->p2p_invite_group = wpa_s;
6309 persistent = ssid->p2p_persistent_group &&
6310 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6311 ssid->ssid, ssid->ssid_len);
6313 if (ssid->mode == WPAS_MODE_P2P_GO) {
6314 role = P2P_INVITE_ROLE_ACTIVE_GO;
6315 bssid = wpa_s->own_addr;
6316 if (go_dev_addr == NULL)
6317 go_dev_addr = wpa_s->global->p2p_dev_addr;
6318 freq = ssid->frequency;
6320 role = P2P_INVITE_ROLE_CLIENT;
6321 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6322 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6323 "invite to current group");
6326 bssid = wpa_s->bssid;
6327 if (go_dev_addr == NULL &&
6328 !is_zero_ether_addr(wpa_s->go_dev_addr))
6329 go_dev_addr = wpa_s->go_dev_addr;
6330 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6331 (int) wpa_s->assoc_freq;
6333 wpa_s->parent->pending_invite_ssid_id = -1;
6335 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6338 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6339 role == P2P_INVITE_ROLE_ACTIVE_GO);
6342 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
6344 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6345 ssid->ssid, ssid->ssid_len, force_freq,
6346 go_dev_addr, persistent, pref_freq, -1);
6350 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6352 struct wpa_ssid *ssid = wpa_s->current_ssid;
6353 u8 go_dev_addr[ETH_ALEN];
6354 int network_id = -1;
6360 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6361 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6362 wpa_s->parent, NULL);
6365 if (!wpa_s->show_group_started || !ssid)
6368 wpa_s->show_group_started = 0;
6370 os_memset(go_dev_addr, 0, ETH_ALEN);
6371 if (ssid->bssid_set)
6372 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6373 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6375 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6377 if (wpa_s->global->p2p_group_formation == wpa_s)
6378 wpa_s->global->p2p_group_formation = NULL;
6380 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6381 (int) wpa_s->assoc_freq;
6384 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6387 res = os_snprintf(ip_addr, sizeof(ip_addr),
6388 " ip_addr=%u.%u.%u.%u "
6389 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6390 ip[0], ip[1], ip[2], ip[3],
6391 ip[4], ip[5], ip[6], ip[7],
6392 ip[8], ip[9], ip[10], ip[11]);
6393 if (os_snprintf_error(sizeof(ip_addr), res))
6397 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6398 ssid->passphrase == NULL && ssid->psk_set ?
6400 ssid->passphrase, go_dev_addr, persistent,
6404 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6407 network_id = ssid->id;
6408 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6412 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6413 u32 interval1, u32 duration2, u32 interval2)
6417 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6420 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6421 wpa_s->current_ssid == NULL ||
6422 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6425 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6426 wpa_s->own_addr, wpa_s->assoc_freq,
6427 duration1, interval1, duration2, interval2);
6429 wpa_s->waiting_presence_resp = 1;
6435 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6436 unsigned int interval)
6438 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6441 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6445 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6447 if (wpa_s->current_ssid == NULL) {
6449 * current_ssid can be cleared when P2P client interface gets
6450 * disconnected, so assume this interface was used as P2P
6455 return wpa_s->current_ssid->p2p_group &&
6456 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6460 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6462 struct wpa_supplicant *wpa_s = eloop_ctx;
6464 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6465 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6470 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6472 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6476 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6480 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6481 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6483 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6486 timeout = wpa_s->conf->p2p_group_idle;
6487 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6488 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6489 timeout = P2P_MAX_CLIENT_IDLE;
6495 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6496 timeout = 0; /* special client mode no-timeout */
6501 if (wpa_s->p2p_in_provisioning) {
6503 * Use the normal group formation timeout during the
6504 * provisioning phase to avoid terminating this process too
6505 * early due to group idle timeout.
6507 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6508 "during provisioning");
6512 if (wpa_s->show_group_started) {
6514 * Use the normal group formation timeout between the end of
6515 * the provisioning phase and completion of 4-way handshake to
6516 * avoid terminating this process too early due to group idle
6519 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6520 "while waiting for initial 4-way handshake to "
6525 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6527 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6532 /* Returns 1 if the interface was removed */
6533 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6534 u16 reason_code, const u8 *ie, size_t ie_len,
6535 int locally_generated)
6537 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6540 if (!locally_generated)
6541 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6544 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6545 wpa_s->current_ssid &&
6546 wpa_s->current_ssid->p2p_group &&
6547 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6548 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6549 "session is ending");
6550 if (wpas_p2p_group_delete(wpa_s,
6551 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6560 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6561 u16 reason_code, const u8 *ie, size_t ie_len,
6562 int locally_generated)
6564 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6567 if (!locally_generated)
6568 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6573 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6575 struct p2p_data *p2p = wpa_s->global->p2p;
6580 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6583 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6584 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6586 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6587 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6590 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6591 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6593 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6594 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6596 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6597 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6598 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6599 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6600 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6603 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6604 p2p_set_sec_dev_types(p2p,
6605 (void *) wpa_s->conf->sec_device_type,
6606 wpa_s->conf->num_sec_device_types);
6608 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6610 p2p_remove_wps_vendor_extensions(p2p);
6611 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6612 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6614 p2p_add_wps_vendor_extension(
6615 p2p, wpa_s->conf->wps_vendor_ext[i]);
6619 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6620 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6622 country[0] = wpa_s->conf->country[0];
6623 country[1] = wpa_s->conf->country[1];
6625 p2p_set_country(p2p, country);
6628 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6629 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6630 wpa_s->conf->p2p_ssid_postfix ?
6631 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6635 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6636 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6638 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6639 u8 reg_class, channel;
6644 if (wpa_s->conf->p2p_listen_reg_class &&
6645 wpa_s->conf->p2p_listen_channel) {
6646 reg_class = wpa_s->conf->p2p_listen_reg_class;
6647 channel = wpa_s->conf->p2p_listen_channel;
6652 * Pick one of the social channels randomly as the
6655 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6658 channel = 1 + (r % 3) * 5;
6661 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6664 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6667 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6668 u8 op_reg_class, op_channel, cfg_op_channel;
6671 if (wpa_s->conf->p2p_oper_reg_class &&
6672 wpa_s->conf->p2p_oper_channel) {
6673 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6674 op_channel = wpa_s->conf->p2p_oper_channel;
6679 * Use random operation channel from (1, 6, 11)
6680 *if no other preference is indicated.
6682 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6685 op_channel = 1 + (r % 3) * 5;
6688 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6691 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6695 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6696 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6697 wpa_s->conf->p2p_pref_chan) < 0) {
6698 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6702 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6703 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6708 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6709 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
6713 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6716 if (!wpa_s->ap_iface)
6718 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6723 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6725 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6728 wpa_s->global->cross_connection = enabled;
6729 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6732 struct wpa_supplicant *iface;
6734 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6736 if (iface->cross_connect_enabled == 0)
6739 iface->cross_connect_enabled = 0;
6740 iface->cross_connect_in_use = 0;
6741 wpa_msg_global(iface->parent, MSG_INFO,
6742 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6744 iface->cross_connect_uplink);
6752 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6754 struct wpa_supplicant *iface;
6756 if (!uplink->global->cross_connection)
6759 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6760 if (!iface->cross_connect_enabled)
6762 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6765 if (iface->ap_iface == NULL)
6767 if (iface->cross_connect_in_use)
6770 iface->cross_connect_in_use = 1;
6771 wpa_msg_global(iface->parent, MSG_INFO,
6772 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6773 iface->ifname, iface->cross_connect_uplink);
6778 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6780 struct wpa_supplicant *iface;
6782 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6783 if (!iface->cross_connect_enabled)
6785 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6788 if (!iface->cross_connect_in_use)
6791 wpa_msg_global(iface->parent, MSG_INFO,
6792 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6793 iface->ifname, iface->cross_connect_uplink);
6794 iface->cross_connect_in_use = 0;
6799 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6801 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6802 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6803 wpa_s->cross_connect_disallowed)
6804 wpas_p2p_disable_cross_connect(wpa_s);
6806 wpas_p2p_enable_cross_connect(wpa_s);
6807 if (!wpa_s->ap_iface &&
6808 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6809 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6813 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6815 wpas_p2p_disable_cross_connect(wpa_s);
6816 if (!wpa_s->ap_iface &&
6817 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6819 wpas_p2p_set_group_idle_timeout(wpa_s);
6823 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6825 struct wpa_supplicant *iface;
6827 if (!wpa_s->global->cross_connection)
6830 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6833 if (iface->drv_flags &
6834 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6836 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
6837 iface != wpa_s->parent)
6840 wpa_s->cross_connect_enabled = 1;
6841 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6842 sizeof(wpa_s->cross_connect_uplink));
6843 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6844 "%s to %s whenever uplink is available",
6845 wpa_s->ifname, wpa_s->cross_connect_uplink);
6847 if (iface->ap_iface || iface->current_ssid == NULL ||
6848 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6849 iface->cross_connect_disallowed ||
6850 iface->wpa_state != WPA_COMPLETED)
6853 wpa_s->cross_connect_in_use = 1;
6854 wpa_msg_global(wpa_s->parent, MSG_INFO,
6855 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6856 wpa_s->ifname, wpa_s->cross_connect_uplink);
6862 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6864 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6865 !wpa_s->p2p_in_provisioning)
6866 return 0; /* not P2P client operation */
6868 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6870 if (wpa_s != wpa_s->parent)
6871 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
6872 wpas_p2p_group_formation_failed(wpa_s);
6877 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6879 struct wpa_supplicant *wpa_s = eloop_ctx;
6880 wpas_p2p_notif_pbc_overlap(wpa_s);
6884 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6886 struct p2p_channels chan, cli_chan;
6887 struct wpa_supplicant *ifs;
6889 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6892 os_memset(&chan, 0, sizeof(chan));
6893 os_memset(&cli_chan, 0, sizeof(cli_chan));
6894 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
6895 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6900 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
6902 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6904 if (!ifs->current_ssid ||
6905 !ifs->current_ssid->p2p_group ||
6906 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6907 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6909 freq = ifs->current_ssid->frequency;
6910 if (freq_included(&chan, freq)) {
6911 wpa_dbg(ifs, MSG_DEBUG,
6912 "P2P GO operating frequency %d MHz in valid range",
6917 wpa_dbg(ifs, MSG_DEBUG,
6918 "P2P GO operating in invalid frequency %d MHz", freq);
6919 /* TODO: Consider using CSA or removing the group within
6921 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6926 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6927 struct wpa_scan_results *scan_res)
6929 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6933 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6935 struct wpa_global *global = wpa_s->global;
6939 if (global->p2p == NULL)
6942 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6944 if (wpa_s->pending_interface_name[0] &&
6945 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6948 peer = p2p_get_go_neg_peer(global->p2p);
6950 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6951 MACSTR, MAC2STR(peer));
6952 p2p_unauthorize(global->p2p, peer);
6956 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6957 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6958 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6962 if (wpa_s->pending_pd_before_join) {
6963 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6964 wpa_s->pending_pd_before_join = 0;
6968 wpas_p2p_stop_find(wpa_s);
6970 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6971 if (wpa_s == global->p2p_group_formation &&
6972 (wpa_s->p2p_in_provisioning ||
6973 wpa_s->parent->pending_interface_type ==
6974 WPA_IF_P2P_CLIENT)) {
6975 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6976 "formation found - cancelling",
6979 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6980 wpa_s->parent, NULL);
6981 if (wpa_s->p2p_in_provisioning) {
6982 wpas_group_formation_completed(wpa_s, 0);
6985 wpas_p2p_group_delete(wpa_s,
6986 P2P_GROUP_REMOVAL_REQUESTED);
6988 } else if (wpa_s->p2p_in_invitation) {
6989 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6992 wpas_p2p_group_formation_failed(wpa_s);
6997 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
7005 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
7007 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
7010 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
7011 "being available anymore");
7012 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
7016 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
7017 int freq_24, int freq_5, int freq_overall)
7019 struct p2p_data *p2p = wpa_s->global->p2p;
7022 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
7026 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
7029 struct p2p_data *p2p = wpa_s->global->p2p;
7034 if (hwaddr_aton(addr, peer))
7037 return p2p_unauthorize(p2p, peer);
7042 * wpas_p2p_disconnect - Disconnect from a P2P Group
7043 * @wpa_s: Pointer to wpa_supplicant data
7044 * Returns: 0 on success, -1 on failure
7046 * This can be used to disconnect from a group in which the local end is a P2P
7047 * Client or to end a P2P Group in case the local end is the Group Owner. If a
7048 * virtual network interface was created for this group, that interface will be
7049 * removed. Otherwise, only the configured P2P group network will be removed
7050 * from the interface.
7052 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
7058 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
7063 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
7067 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7070 ret = p2p_in_progress(wpa_s->global->p2p);
7073 * Check whether there is an ongoing WPS provisioning step (or
7074 * other parts of group formation) on another interface since
7075 * p2p_in_progress() does not report this to avoid issues for
7076 * scans during such provisioning step.
7078 if (wpa_s->global->p2p_group_formation &&
7079 wpa_s->global->p2p_group_formation != wpa_s) {
7080 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7081 "in group formation",
7082 wpa_s->global->p2p_group_formation->ifname);
7087 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
7088 struct os_reltime now;
7089 os_get_reltime(&now);
7090 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7091 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
7092 /* Wait for the first client has expired */
7093 wpa_s->global->p2p_go_wait_client.sec = 0;
7095 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7104 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7105 struct wpa_ssid *ssid)
7107 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7108 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7109 wpa_s->parent, NULL) > 0) {
7111 * Remove the network by scheduling the group formation
7112 * timeout to happen immediately. The teardown code
7113 * needs to be scheduled to run asynch later so that we
7114 * don't delete data from under ourselves unexpectedly.
7115 * Calling wpas_p2p_group_formation_timeout directly
7116 * causes a series of crashes in WPS failure scenarios.
7118 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7119 "P2P group network getting removed");
7120 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
7121 wpa_s->parent, NULL);
7126 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
7127 const u8 *addr, const u8 *ssid,
7133 for (s = wpa_s->conf->ssid; s; s = s->next) {
7134 if (s->disabled != 2)
7137 (ssid_len != s->ssid_len ||
7138 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7141 if (s->mode == WPAS_MODE_P2P_GO)
7145 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7146 return s; /* peer is GO in the persistent group */
7147 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7149 for (i = 0; i < s->num_p2p_clients; i++) {
7150 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
7151 addr, ETH_ALEN) == 0)
7152 return s; /* peer is P2P client in persistent
7161 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7164 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7165 wpa_s->parent, NULL) > 0) {
7167 * This can happen if WPS provisioning step is not terminated
7168 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7169 * peer was able to connect, there is no need to time out group
7170 * formation after this, though. In addition, this is used with
7171 * the initial connection wait on the GO as a separate formation
7172 * timeout and as such, expected to be hit after the initial WPS
7173 * provisioning step.
7175 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
7177 if (!wpa_s->p2p_go_group_formation_completed &&
7178 !wpa_s->group_formation_reported) {
7180 * GO has not yet notified group formation success since
7181 * the WPS step was not completed cleanly. Do that
7182 * notification now since the P2P Client was able to
7183 * connect and as such, must have received the
7184 * credential from the WPS step.
7186 if (wpa_s->global->p2p)
7187 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7188 wpas_group_formation_completed(wpa_s, 1);
7191 if (!wpa_s->p2p_go_group_formation_completed) {
7192 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7193 wpa_s->p2p_go_group_formation_completed = 1;
7194 wpa_s->global->p2p_group_formation = NULL;
7195 wpa_s->p2p_in_provisioning = 0;
7196 wpa_s->p2p_in_invitation = 0;
7198 wpa_s->global->p2p_go_wait_client.sec = 0;
7201 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7205 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7208 struct wpa_supplicant *group = wpa_s;
7209 if (wpa_s->global->p2p_group_formation)
7210 group = wpa_s->global->p2p_group_formation;
7211 wpa_s = wpa_s->parent;
7212 offchannel_send_action_done(wpa_s);
7214 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
7215 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7216 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7217 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7218 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7219 wpa_s->p2p_persistent_id,
7220 wpa_s->p2p_pd_before_go_neg,
7226 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7228 if (!wpa_s->p2p_fallback_to_go_neg ||
7229 wpa_s->p2p_in_provisioning <= 5)
7232 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7233 return 0; /* peer operating as a GO */
7235 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7236 "fallback to GO Negotiation");
7237 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
7243 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7245 struct wpa_supplicant *ifs;
7247 if (wpa_s->wpa_state > WPA_SCANNING) {
7248 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7249 "concurrent operation",
7250 wpa_s->conf->p2p_search_delay);
7251 return wpa_s->conf->p2p_search_delay;
7254 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7256 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
7257 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7258 "delay due to concurrent operation on "
7260 wpa_s->conf->p2p_search_delay,
7262 return wpa_s->conf->p2p_search_delay;
7270 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7271 struct wpa_ssid *s, const u8 *addr,
7274 struct psk_list_entry *psk, *tmp;
7277 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7279 if ((iface_addr && !psk->p2p &&
7280 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7281 (!iface_addr && psk->p2p &&
7282 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7283 wpa_dbg(wpa_s, MSG_DEBUG,
7284 "P2P: Remove persistent group PSK list entry for "
7286 MAC2STR(psk->addr), psk->p2p);
7287 dl_list_del(&psk->list);
7297 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7298 const u8 *p2p_dev_addr,
7299 const u8 *psk, size_t psk_len)
7301 struct wpa_ssid *ssid = wpa_s->current_ssid;
7302 struct wpa_ssid *persistent;
7303 struct psk_list_entry *p, *last;
7305 if (psk_len != sizeof(p->psk))
7309 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7310 " p2p_dev_addr=" MACSTR,
7311 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7312 if (is_zero_ether_addr(p2p_dev_addr))
7313 p2p_dev_addr = NULL;
7315 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7319 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7320 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7321 /* To be added to persistent group once created */
7322 if (wpa_s->global->add_psk == NULL) {
7323 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7324 if (wpa_s->global->add_psk == NULL)
7327 p = wpa_s->global->add_psk;
7330 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7333 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7335 os_memcpy(p->psk, psk, psk_len);
7339 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7340 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7344 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7347 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7351 p = os_zalloc(sizeof(*p));
7356 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7359 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7361 os_memcpy(p->psk, psk, psk_len);
7363 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7364 (last = dl_list_last(&persistent->psk_list,
7365 struct psk_list_entry, list))) {
7366 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7367 MACSTR " (p2p=%u) to make room for a new one",
7368 MAC2STR(last->addr), last->p2p);
7369 dl_list_del(&last->list);
7373 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7374 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7375 p2p_dev_addr == NULL);
7377 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7378 MACSTR, MAC2STR(p2p_dev_addr));
7380 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7383 dl_list_add(&persistent->psk_list, &p->list);
7385 if (wpa_s->parent->conf->update_config &&
7386 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7387 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
7391 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7392 struct wpa_ssid *s, const u8 *addr,
7397 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
7398 if (res > 0 && wpa_s->conf->update_config &&
7399 wpa_config_write(wpa_s->confname, wpa_s->conf))
7400 wpa_dbg(wpa_s, MSG_DEBUG,
7401 "P2P: Failed to update configuration");
7405 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7406 const u8 *peer, int iface_addr)
7408 struct hostapd_data *hapd;
7409 struct hostapd_wpa_psk *psk, *prev, *rem;
7410 struct sta_info *sta;
7412 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7413 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7416 /* Remove per-station PSK entry */
7417 hapd = wpa_s->ap_iface->bss[0];
7419 psk = hapd->conf->ssid.wpa_psk;
7421 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7423 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7424 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7425 MACSTR " iface_addr=%d",
7426 MAC2STR(peer), iface_addr);
7428 prev->next = psk->next;
7430 hapd->conf->ssid.wpa_psk = psk->next;
7440 /* Disconnect from group */
7442 sta = ap_get_sta(hapd, peer);
7444 sta = ap_get_sta_p2p(hapd, peer);
7446 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7447 " (iface_addr=%d) from group",
7448 MAC2STR(peer), iface_addr);
7449 hostapd_drv_sta_deauth(hapd, sta->addr,
7450 WLAN_REASON_DEAUTH_LEAVING);
7451 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7456 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7460 struct wpa_supplicant *w;
7462 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7464 /* Remove from any persistent group */
7465 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
7466 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7469 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
7470 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
7473 /* Remove from any operating group */
7474 for (w = wpa_s->global->ifaces; w; w = w->next)
7475 wpas_p2p_remove_client_go(w, peer, iface_addr);
7479 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7481 struct wpa_supplicant *wpa_s = eloop_ctx;
7482 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7486 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7488 struct wpa_supplicant *wpa_s = eloop_ctx;
7490 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7491 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7495 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7496 struct wpa_ssid *ssid)
7498 struct wpa_supplicant *iface;
7500 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7501 if (!iface->current_ssid ||
7502 iface->current_ssid->frequency == freq ||
7503 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7504 !iface->current_ssid->p2p_group))
7507 /* Remove the connection with least priority */
7508 if (!wpas_is_p2p_prioritized(iface)) {
7509 /* STA connection has priority over existing
7510 * P2P connection, so remove the interface. */
7511 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7512 eloop_register_timeout(0, 0,
7513 wpas_p2p_group_freq_conflict,
7515 /* If connection in progress is P2P connection, do not
7516 * proceed for the connection. */
7522 /* P2P connection has priority, disable the STA network
7524 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7526 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7527 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7528 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7530 /* If P2P connection is in progress, continue
7543 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7545 struct wpa_ssid *ssid = wpa_s->current_ssid;
7547 if (ssid == NULL || !ssid->p2p_group)
7550 if (wpa_s->p2p_last_4way_hs_fail &&
7551 wpa_s->p2p_last_4way_hs_fail == ssid) {
7552 u8 go_dev_addr[ETH_ALEN];
7553 struct wpa_ssid *persistent;
7555 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7557 ssid->ssid_len) <= 0) {
7558 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7562 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7563 MACSTR, MAC2STR(go_dev_addr));
7564 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7567 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7568 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7571 wpa_msg_global(wpa_s->parent, MSG_INFO,
7572 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7575 wpa_s->p2p_last_4way_hs_fail = NULL;
7577 * Remove the group from a timeout to avoid issues with caller
7578 * continuing to use the interface if this is on a P2P group
7581 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7586 wpa_s->p2p_last_4way_hs_fail = ssid;
7591 #ifdef CONFIG_WPS_NFC
7593 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7601 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7605 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7606 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7613 wpabuf_put_be16(ret, wsc_len);
7615 wpabuf_put_buf(ret, wsc);
7616 wpabuf_put_be16(ret, wpabuf_len(p2p));
7617 wpabuf_put_buf(ret, p2p);
7621 wpa_hexdump_buf(MSG_DEBUG,
7622 "P2P: Generated NFC connection handover message", ret);
7626 tmp = ndef_build_p2p(ret);
7629 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7639 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7640 struct wpa_ssid **ssid, u8 *go_dev_addr)
7642 struct wpa_supplicant *iface;
7645 os_memset(go_dev_addr, 0, ETH_ALEN);
7648 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7649 if (iface->wpa_state < WPA_ASSOCIATING ||
7650 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7651 !iface->current_ssid->p2p_group ||
7652 iface->current_ssid->mode != WPAS_MODE_INFRA)
7655 *ssid = iface->current_ssid;
7657 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7658 return iface->assoc_freq;
7664 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7667 struct wpabuf *wsc, *p2p;
7668 struct wpa_ssid *ssid;
7669 u8 go_dev_addr[ETH_ALEN];
7670 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7672 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7673 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7677 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7678 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7679 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7680 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7684 if (cli_freq == 0) {
7685 wsc = wps_build_nfc_handover_req_p2p(
7686 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7689 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7690 go_dev_addr, ssid ? ssid->ssid : NULL,
7691 ssid ? ssid->ssid_len : 0);
7693 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7697 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7700 struct wpabuf *wsc, *p2p;
7701 struct wpa_ssid *ssid;
7702 u8 go_dev_addr[ETH_ALEN];
7703 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7705 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7708 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7709 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7710 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7713 if (cli_freq == 0) {
7714 wsc = wps_build_nfc_handover_sel_p2p(
7716 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7717 DEV_PW_NFC_CONNECTION_HANDOVER,
7718 wpa_s->conf->wps_nfc_dh_pubkey,
7719 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7722 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7723 go_dev_addr, ssid ? ssid->ssid : NULL,
7724 ssid ? ssid->ssid_len : 0);
7726 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7730 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7731 struct p2p_nfc_params *params)
7733 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7734 "connection handover (freq=%d)",
7737 if (params->go_freq && params->go_ssid_len) {
7738 wpa_s->p2p_wps_method = WPS_NFC;
7739 wpa_s->pending_join_wps_method = WPS_NFC;
7740 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7741 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7743 return wpas_p2p_join_start(wpa_s, params->go_freq,
7745 params->go_ssid_len);
7748 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7749 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7750 params->go_freq, -1, 0, 1, 1);
7754 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7755 struct p2p_nfc_params *params, int tag)
7757 int res, persistent;
7758 struct wpa_ssid *ssid;
7760 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7761 "connection handover");
7762 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7763 ssid = wpa_s->current_ssid;
7766 if (ssid->mode != WPAS_MODE_P2P_GO)
7768 if (wpa_s->ap_iface == NULL)
7772 if (wpa_s == NULL) {
7773 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7777 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7778 DEV_PW_NFC_CONNECTION_HANDOVER &&
7779 !wpa_s->parent->p2p_oob_dev_pw) {
7780 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7783 res = wpas_ap_wps_add_nfc_pw(
7784 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7785 wpa_s->parent->p2p_oob_dev_pw,
7786 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7787 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7792 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7796 if (!params->peer ||
7797 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7800 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7801 " to join", MAC2STR(params->peer->p2p_device_addr));
7803 wpa_s->global->p2p_invite_group = wpa_s;
7804 persistent = ssid->p2p_persistent_group &&
7805 wpas_p2p_get_persistent(wpa_s->parent,
7806 params->peer->p2p_device_addr,
7807 ssid->ssid, ssid->ssid_len);
7808 wpa_s->parent->pending_invite_ssid_id = -1;
7810 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7811 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7812 ssid->ssid, ssid->ssid_len, ssid->frequency,
7813 wpa_s->global->p2p_dev_addr, persistent, 0,
7814 wpa_s->parent->p2p_oob_dev_pw_id);
7818 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7819 struct p2p_nfc_params *params,
7822 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7823 "connection handover");
7824 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7825 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7826 forced_freq, -1, 0, 1, 1);
7830 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7831 struct p2p_nfc_params *params,
7836 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7837 "connection handover");
7838 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7839 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7840 forced_freq, -1, 0, 1, 1);
7844 res = wpas_p2p_listen(wpa_s, 60);
7846 p2p_unauthorize(wpa_s->global->p2p,
7847 params->peer->p2p_device_addr);
7854 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7855 const struct wpabuf *data,
7856 int sel, int tag, int forced_freq)
7858 const u8 *pos, *end;
7860 struct p2p_nfc_params params;
7863 os_memset(¶ms, 0, sizeof(params));
7866 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7868 pos = wpabuf_head(data);
7869 end = pos + wpabuf_len(data);
7871 if (end - pos < 2) {
7872 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7876 len = WPA_GET_BE16(pos);
7878 if (len > end - pos) {
7879 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7883 params.wsc_attr = pos;
7884 params.wsc_len = len;
7887 if (end - pos < 2) {
7888 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7892 len = WPA_GET_BE16(pos);
7894 if (len > end - pos) {
7895 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7899 params.p2p_attr = pos;
7900 params.p2p_len = len;
7903 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7904 params.wsc_attr, params.wsc_len);
7905 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7906 params.p2p_attr, params.p2p_len);
7908 wpa_hexdump(MSG_DEBUG,
7909 "P2P: Ignored extra data after P2P attributes",
7913 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
7917 if (params.next_step == NO_ACTION)
7920 if (params.next_step == BOTH_GO) {
7921 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7922 MAC2STR(params.peer->p2p_device_addr));
7926 if (params.next_step == PEER_CLIENT) {
7927 if (!is_zero_ether_addr(params.go_dev_addr)) {
7928 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7929 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7931 MAC2STR(params.peer->p2p_device_addr),
7933 MAC2STR(params.go_dev_addr),
7934 wpa_ssid_txt(params.go_ssid,
7935 params.go_ssid_len));
7937 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7938 "peer=" MACSTR " freq=%d",
7939 MAC2STR(params.peer->p2p_device_addr),
7945 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7946 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7947 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7951 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7952 wpa_s->p2p_oob_dev_pw = NULL;
7954 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7955 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7960 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7961 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7962 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7963 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7964 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7965 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7966 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7970 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7971 "peer OOB Device Password Id %u", id);
7974 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7975 "Device Password Id %u", id);
7976 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7977 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7978 params.oob_dev_pw_len -
7979 WPS_OOB_PUBKEY_HASH_LEN - 2);
7980 wpa_s->p2p_oob_dev_pw_id = id;
7981 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7982 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7983 params.oob_dev_pw_len -
7984 WPS_OOB_PUBKEY_HASH_LEN - 2);
7985 if (wpa_s->p2p_oob_dev_pw == NULL)
7988 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7989 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7990 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7993 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7994 "without Device Password");
7995 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7998 switch (params.next_step) {
8002 /* already covered above */
8005 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
8007 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
8009 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
8011 /* TODO: use own OOB Dev Pw */
8012 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
8019 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
8020 const struct wpabuf *data, int forced_freq)
8022 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8025 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
8029 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
8030 const struct wpabuf *req,
8031 const struct wpabuf *sel, int forced_freq)
8036 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8039 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
8041 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
8042 wpabuf_head(req), wpabuf_len(req));
8043 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
8044 wpabuf_head(sel), wpabuf_len(sel));
8046 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
8047 tmp = ndef_parse_p2p(init ? sel : req);
8049 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
8053 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
8061 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
8064 int go_intent = wpa_s->conf->p2p_go_intent;
8065 struct wpa_supplicant *iface;
8067 if (wpa_s->global->p2p == NULL)
8071 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
8072 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
8074 if (!iface->ap_iface)
8076 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8078 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8080 if (wpa_s->p2p_nfc_tag_enabled)
8081 wpas_p2p_remove_pending_group_interface(wpa_s);
8082 wpa_s->p2p_nfc_tag_enabled = 0;
8086 if (wpa_s->global->p2p_disabled)
8089 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8090 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8091 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8092 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8093 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8094 "to allow static handover cases");
8098 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8100 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8101 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8102 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8103 if (wpa_s->p2p_oob_dev_pw == NULL)
8105 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8107 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8108 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8110 * P2P Group Interface present and the command came on group
8111 * interface, so enable the token for the current interface.
8113 wpa_s->create_p2p_iface = 0;
8115 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8118 if (wpa_s->create_p2p_iface) {
8119 enum wpa_driver_if_type iftype;
8120 /* Prepare to add a new interface for the group */
8121 iftype = WPA_IF_P2P_GROUP;
8122 if (go_intent == 15)
8123 iftype = WPA_IF_P2P_GO;
8124 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8125 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8126 "interface for the group");
8130 if_addr = wpa_s->pending_interface_addr;
8132 if_addr = wpa_s->own_addr;
8134 wpa_s->p2p_nfc_tag_enabled = enabled;
8136 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8137 struct hostapd_data *hapd;
8138 if (iface->ap_iface == NULL)
8140 hapd = iface->ap_iface->bss[0];
8141 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8142 hapd->conf->wps_nfc_dh_pubkey =
8143 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8144 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8145 hapd->conf->wps_nfc_dh_privkey =
8146 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8147 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8148 hapd->conf->wps_nfc_dev_pw =
8149 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8150 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8152 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8153 wpa_dbg(iface, MSG_DEBUG,
8154 "P2P: Failed to enable NFC Tag for GO");
8157 p2p_set_authorized_oob_dev_pw_id(
8158 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8164 #endif /* CONFIG_WPS_NFC */
8167 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8168 struct wpa_used_freq_data *freqs,
8171 u8 curr_chan, cand, chan;
8174 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8175 for (i = 0, cand = 0; i < num; i++) {
8176 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8177 if (curr_chan == chan) {
8182 if (chan == 1 || chan == 6 || chan == 11)
8187 wpa_dbg(wpa_s, MSG_DEBUG,
8188 "P2P: Update Listen channel to %u baased on operating channel",
8190 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8195 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8197 struct wpa_used_freq_data *freqs;
8198 unsigned int num = wpa_s->num_multichan_concurrent;
8200 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8204 * If possible, optimize the Listen channel to be a channel that is
8205 * already used by one of the other interfaces.
8207 if (!wpa_s->conf->p2p_optimize_listen_chan)
8210 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8213 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8217 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8219 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
8224 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8226 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8227 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8228 "the management interface is being removed");
8229 wpas_p2p_deinit_global(wpa_s->global);
8234 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8236 if (wpa_s->ap_iface->bss)
8237 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8238 wpas_p2p_group_deinit(wpa_s);