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 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1853 " p2p_dev_addr=" MACSTR
1854 " pri_dev_type=%s name='%s' config_methods=0x%x "
1855 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
1856 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1857 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1859 info->device_name, info->config_methods,
1860 info->dev_capab, info->group_capab,
1861 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1862 wfd_dev_info_hex ? wfd_dev_info_hex : "",
1863 info->vendor_elems ? " vendor_elems=1" : "",
1866 os_free(wfd_dev_info_hex);
1867 #endif /* CONFIG_NO_STDOUT_DEBUG */
1869 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1873 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1875 struct wpa_supplicant *wpa_s = ctx;
1877 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1878 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1880 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1884 static void wpas_find_stopped(void *ctx)
1886 struct wpa_supplicant *wpa_s = ctx;
1887 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1891 struct wpas_p2p_listen_work {
1893 unsigned int duration;
1894 struct wpabuf *probe_resp_ie;
1898 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1902 wpabuf_free(lwork->probe_resp_ie);
1907 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1909 struct wpas_p2p_listen_work *lwork;
1911 if (!wpa_s->p2p_listen_work)
1914 lwork = wpa_s->p2p_listen_work->ctx;
1915 wpas_p2p_listen_work_free(lwork);
1916 radio_work_done(wpa_s->p2p_listen_work);
1917 wpa_s->p2p_listen_work = NULL;
1921 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1923 struct wpa_supplicant *wpa_s = work->wpa_s;
1924 struct wpas_p2p_listen_work *lwork = work->ctx;
1925 unsigned int duration;
1928 if (work->started) {
1929 wpa_s->p2p_listen_work = NULL;
1930 wpas_stop_listen(wpa_s);
1932 wpas_p2p_listen_work_free(lwork);
1936 wpa_s->p2p_listen_work = work;
1938 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1940 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1941 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1942 "report received Probe Request frames");
1943 wpas_p2p_listen_work_done(wpa_s);
1947 wpa_s->pending_listen_freq = lwork->freq;
1948 wpa_s->pending_listen_duration = lwork->duration;
1950 duration = lwork->duration;
1951 #ifdef CONFIG_TESTING_OPTIONS
1952 if (wpa_s->extra_roc_dur) {
1953 wpa_printf(MSG_DEBUG, "TESTING: Increase ROC duration %u -> %u",
1954 duration, duration + wpa_s->extra_roc_dur);
1955 duration += wpa_s->extra_roc_dur;
1957 #endif /* CONFIG_TESTING_OPTIONS */
1959 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, duration) < 0) {
1960 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1961 "to remain on channel (%u MHz) for Listen "
1962 "state", lwork->freq);
1963 wpas_p2p_listen_work_done(wpa_s);
1964 wpa_s->pending_listen_freq = 0;
1967 wpa_s->off_channel_freq = 0;
1968 wpa_s->roc_waiting_drv_freq = lwork->freq;
1972 static int wpas_start_listen(void *ctx, unsigned int freq,
1973 unsigned int duration,
1974 const struct wpabuf *probe_resp_ie)
1976 struct wpa_supplicant *wpa_s = ctx;
1977 struct wpas_p2p_listen_work *lwork;
1979 if (wpa_s->p2p_listen_work) {
1980 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
1984 lwork = os_zalloc(sizeof(*lwork));
1988 lwork->duration = duration;
1989 if (probe_resp_ie) {
1990 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1991 if (lwork->probe_resp_ie == NULL) {
1992 wpas_p2p_listen_work_free(lwork);
1997 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1999 wpas_p2p_listen_work_free(lwork);
2007 static void wpas_stop_listen(void *ctx)
2009 struct wpa_supplicant *wpa_s = ctx;
2010 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
2011 wpa_drv_cancel_remain_on_channel(wpa_s);
2012 wpa_s->off_channel_freq = 0;
2013 wpa_s->roc_waiting_drv_freq = 0;
2015 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
2016 wpa_drv_probe_req_report(wpa_s, 0);
2017 wpas_p2p_listen_work_done(wpa_s);
2021 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
2023 struct wpa_supplicant *wpa_s = ctx;
2024 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
2029 * DNS Header section is used only to calculate compression pointers, so the
2030 * contents of this data does not matter, but the length needs to be reserved
2031 * in the virtual packet.
2033 #define DNS_HEADER_LEN 12
2036 * 27-octet in-memory packet from P2P specification containing two implied
2037 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
2039 #define P2P_SD_IN_MEMORY_LEN 27
2041 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
2042 u8 **spos, const u8 *end)
2044 while (*spos < end) {
2045 u8 val = ((*spos)[0] & 0xc0) >> 6;
2048 if (val == 1 || val == 2) {
2049 /* These are reserved values in RFC 1035 */
2050 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2051 "sequence starting with 0x%x", val);
2060 if (*spos + 2 > end) {
2061 wpa_printf(MSG_DEBUG, "P2P: No room for full "
2062 "DNS offset field");
2066 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
2067 if (offset >= *spos - start) {
2068 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
2069 "pointer offset %u", offset);
2074 spos_tmp = start + offset;
2075 return p2p_sd_dns_uncompress_label(upos, uend, start,
2081 len = (*spos)[0] & 0x3f;
2086 if (*spos + len > end) {
2087 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2088 "sequence - no room for label with length "
2093 if (*upos + len + 2 > uend)
2096 os_memcpy(*upos, *spos, len);
2108 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
2109 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
2110 * not large enough */
2111 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
2112 size_t msg_len, size_t offset)
2114 /* 27-octet in-memory packet from P2P specification */
2115 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
2116 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
2117 u8 *tmp, *end, *spos;
2123 if (offset > msg_len)
2126 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
2129 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
2130 end = spos + msg_len;
2133 os_memset(tmp, 0, DNS_HEADER_LEN);
2134 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
2135 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
2138 uend = buf + buf_len;
2140 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
2149 } else if (upos[-1] == '.')
2157 static struct p2p_srv_bonjour *
2158 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
2159 const struct wpabuf *query)
2161 struct p2p_srv_bonjour *bsrv;
2164 len = wpabuf_len(query);
2165 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2166 struct p2p_srv_bonjour, list) {
2167 if (len == wpabuf_len(bsrv->query) &&
2168 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
2176 static struct p2p_srv_upnp *
2177 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2178 const char *service)
2180 struct p2p_srv_upnp *usrv;
2182 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2183 struct p2p_srv_upnp, list) {
2184 if (version == usrv->version &&
2185 os_strcmp(service, usrv->service) == 0)
2192 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2197 if (wpabuf_tailroom(resp) < 5)
2200 /* Length (to be filled) */
2201 len_pos = wpabuf_put(resp, 2);
2202 wpabuf_put_u8(resp, srv_proto);
2203 wpabuf_put_u8(resp, srv_trans_id);
2205 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2206 /* Response Data: empty */
2207 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2211 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2212 struct wpabuf *resp, u8 srv_trans_id)
2214 struct p2p_srv_bonjour *bsrv;
2217 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2219 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2220 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2224 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2225 struct p2p_srv_bonjour, list) {
2226 if (wpabuf_tailroom(resp) <
2227 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2229 /* Length (to be filled) */
2230 len_pos = wpabuf_put(resp, 2);
2231 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2232 wpabuf_put_u8(resp, srv_trans_id);
2234 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2235 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2236 wpabuf_head(bsrv->resp),
2237 wpabuf_len(bsrv->resp));
2239 wpabuf_put_buf(resp, bsrv->query); /* Key */
2240 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2241 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2247 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2250 char str_rx[256], str_srv[256];
2252 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2253 return 0; /* Too short to include DNS Type and Version */
2254 if (os_memcmp(query + query_len - 3,
2255 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2257 return 0; /* Mismatch in DNS Type or Version */
2258 if (query_len == wpabuf_len(bsrv->query) &&
2259 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2260 return 1; /* Binary match */
2262 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2264 return 0; /* Failed to uncompress query */
2265 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2266 wpabuf_head(bsrv->query),
2267 wpabuf_len(bsrv->query) - 3, 0))
2268 return 0; /* Failed to uncompress service */
2270 return os_strcmp(str_rx, str_srv) == 0;
2274 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2275 struct wpabuf *resp, u8 srv_trans_id,
2276 const u8 *query, size_t query_len)
2278 struct p2p_srv_bonjour *bsrv;
2282 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2284 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2285 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2286 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2291 if (query_len == 0) {
2292 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2296 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2297 struct p2p_srv_bonjour, list) {
2298 if (!match_bonjour_query(bsrv, query, query_len))
2301 if (wpabuf_tailroom(resp) <
2302 5 + query_len + wpabuf_len(bsrv->resp))
2307 /* Length (to be filled) */
2308 len_pos = wpabuf_put(resp, 2);
2309 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2310 wpabuf_put_u8(resp, srv_trans_id);
2313 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2314 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2315 wpabuf_head(bsrv->resp),
2316 wpabuf_len(bsrv->resp));
2319 wpabuf_put_data(resp, query, query_len); /* Key */
2320 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2322 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2326 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2328 if (wpabuf_tailroom(resp) < 5)
2331 /* Length (to be filled) */
2332 len_pos = wpabuf_put(resp, 2);
2333 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2334 wpabuf_put_u8(resp, srv_trans_id);
2337 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2338 /* Response Data: empty */
2339 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2345 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2346 struct wpabuf *resp, u8 srv_trans_id)
2348 struct p2p_srv_upnp *usrv;
2351 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2353 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2354 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2358 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2359 struct p2p_srv_upnp, list) {
2360 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2363 /* Length (to be filled) */
2364 len_pos = wpabuf_put(resp, 2);
2365 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2366 wpabuf_put_u8(resp, srv_trans_id);
2369 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2371 wpabuf_put_u8(resp, usrv->version);
2372 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2374 wpabuf_put_str(resp, usrv->service);
2375 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2381 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2382 struct wpabuf *resp, u8 srv_trans_id,
2383 const u8 *query, size_t query_len)
2385 struct p2p_srv_upnp *usrv;
2391 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2394 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2395 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2396 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2401 if (query_len == 0) {
2402 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2406 if (wpabuf_tailroom(resp) < 5)
2409 /* Length (to be filled) */
2410 len_pos = wpabuf_put(resp, 2);
2411 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2412 wpabuf_put_u8(resp, srv_trans_id);
2415 str = os_malloc(query_len);
2418 os_memcpy(str, query + 1, query_len - 1);
2419 str[query_len - 1] = '\0';
2421 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2422 struct p2p_srv_upnp, list) {
2423 if (version != usrv->version)
2426 if (os_strcmp(str, "ssdp:all") != 0 &&
2427 os_strstr(usrv->service, str) == NULL)
2430 if (wpabuf_tailroom(resp) < 2)
2434 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2436 wpabuf_put_u8(resp, version);
2438 wpabuf_put_u8(resp, ',');
2442 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2444 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2446 wpabuf_put_str(resp, usrv->service);
2451 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2454 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2455 /* Response Data: empty */
2458 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2462 #ifdef CONFIG_WIFI_DISPLAY
2463 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2464 struct wpabuf *resp, u8 srv_trans_id,
2465 const u8 *query, size_t query_len)
2471 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2473 if (!wpa_s->global->wifi_display) {
2474 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2475 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2480 if (query_len < 1) {
2481 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2486 if (wpabuf_tailroom(resp) < 5)
2491 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2493 /* TODO: role specific handling */
2495 /* Length (to be filled) */
2496 len_pos = wpabuf_put(resp, 2);
2497 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2498 wpabuf_put_u8(resp, srv_trans_id);
2499 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2501 while (pos < query + query_len) {
2502 if (*pos < MAX_WFD_SUBELEMS &&
2503 wpa_s->global->wfd_subelem[*pos] &&
2504 wpabuf_tailroom(resp) >=
2505 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2506 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2507 "subelement %u", *pos);
2508 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2513 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2515 #endif /* CONFIG_WIFI_DISPLAY */
2518 static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2519 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2521 struct wpa_supplicant *wpa_s = ctx;
2522 const u8 *pos = tlvs;
2523 const u8 *end = tlvs + tlvs_len;
2526 struct wpabuf *resp;
2527 u8 srv_proto, srv_trans_id;
2531 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2533 buf_len = 2 * tlvs_len + 1;
2534 buf = os_malloc(buf_len);
2536 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2537 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2539 freq, MAC2STR(sa), dialog_token, update_indic,
2544 if (wpa_s->p2p_sd_over_ctrl_iface) {
2545 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2546 update_indic, tlvs, tlvs_len);
2547 return; /* to be processed by an external program */
2550 resp = wpabuf_alloc(10000);
2554 while (pos + 1 < end) {
2555 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2556 slen = WPA_GET_LE16(pos);
2558 if (pos + slen > end || slen < 2) {
2559 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2564 tlv_end = pos + slen;
2567 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2569 srv_trans_id = *pos++;
2570 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2573 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2574 pos, tlv_end - pos);
2577 if (wpa_s->force_long_sd) {
2578 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2580 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2581 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2585 switch (srv_proto) {
2586 case P2P_SERV_ALL_SERVICES:
2587 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2588 "for all services");
2589 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2590 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2591 wpa_printf(MSG_DEBUG, "P2P: No service "
2592 "discovery protocols available");
2593 wpas_sd_add_proto_not_avail(
2594 resp, P2P_SERV_ALL_SERVICES,
2598 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2599 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2601 case P2P_SERV_BONJOUR:
2602 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2603 pos, tlv_end - pos);
2606 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2607 pos, tlv_end - pos);
2609 #ifdef CONFIG_WIFI_DISPLAY
2610 case P2P_SERV_WIFI_DISPLAY:
2611 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2612 pos, tlv_end - pos);
2614 #endif /* CONFIG_WIFI_DISPLAY */
2616 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2617 "protocol %u", srv_proto);
2618 wpas_sd_add_proto_not_avail(resp, srv_proto,
2627 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2628 update_indic, tlvs, tlvs_len);
2630 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2636 static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2637 const u8 *tlvs, size_t tlvs_len)
2639 struct wpa_supplicant *wpa_s = ctx;
2640 const u8 *pos = tlvs;
2641 const u8 *end = tlvs + tlvs_len;
2647 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2649 if (tlvs_len > 1500) {
2650 /* TODO: better way for handling this */
2651 wpa_msg_ctrl(wpa_s, MSG_INFO,
2652 P2P_EVENT_SERV_DISC_RESP MACSTR
2653 " %u <long response: %u bytes>",
2654 MAC2STR(sa), update_indic,
2655 (unsigned int) tlvs_len);
2657 buf_len = 2 * tlvs_len + 1;
2658 buf = os_malloc(buf_len);
2660 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2661 wpa_msg_ctrl(wpa_s, MSG_INFO,
2662 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2663 MAC2STR(sa), update_indic, buf);
2669 u8 srv_proto, srv_trans_id, status;
2671 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2672 slen = WPA_GET_LE16(pos);
2674 if (pos + slen > end || slen < 3) {
2675 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2679 tlv_end = pos + slen;
2682 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2684 srv_trans_id = *pos++;
2685 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2688 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2691 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2692 pos, tlv_end - pos);
2697 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2701 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2702 const struct wpabuf *tlvs)
2704 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2706 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2710 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2711 u8 version, const char *query)
2713 struct wpabuf *tlvs;
2716 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2719 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2720 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2721 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2722 wpabuf_put_u8(tlvs, version);
2723 wpabuf_put_str(tlvs, query);
2724 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2730 #ifdef CONFIG_WIFI_DISPLAY
2732 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2733 const struct wpabuf *tlvs)
2735 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2737 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2741 #define MAX_WFD_SD_SUBELEMS 20
2743 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2744 const char *subelems)
2751 len = wpabuf_put(tlvs, 2);
2752 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2753 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2755 wpabuf_put_u8(tlvs, role);
2760 if (val >= 0 && val < 256) {
2761 wpabuf_put_u8(tlvs, val);
2763 if (count == MAX_WFD_SD_SUBELEMS)
2766 pos = os_strchr(pos + 1, ',');
2772 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2776 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2777 const u8 *dst, const char *role)
2779 struct wpabuf *tlvs;
2781 const char *subelems;
2784 subelems = os_strchr(role, ' ');
2785 if (subelems == NULL)
2789 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2793 if (os_strstr(role, "[source]"))
2794 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2795 if (os_strstr(role, "[pri-sink]"))
2796 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2797 if (os_strstr(role, "[sec-sink]"))
2798 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2799 if (os_strstr(role, "[source+sink]"))
2800 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2802 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2807 #endif /* CONFIG_WIFI_DISPLAY */
2810 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2812 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2814 return p2p_sd_cancel_request(wpa_s->global->p2p,
2815 (void *) (uintptr_t) req);
2819 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2820 const u8 *dst, u8 dialog_token,
2821 const struct wpabuf *resp_tlvs)
2823 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2825 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2830 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2832 if (wpa_s->global->p2p)
2833 p2p_sd_service_update(wpa_s->global->p2p);
2837 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2839 dl_list_del(&bsrv->list);
2840 wpabuf_free(bsrv->query);
2841 wpabuf_free(bsrv->resp);
2846 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2848 dl_list_del(&usrv->list);
2849 os_free(usrv->service);
2854 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2856 struct p2p_srv_bonjour *bsrv, *bn;
2857 struct p2p_srv_upnp *usrv, *un;
2859 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2860 struct p2p_srv_bonjour, list)
2861 wpas_p2p_srv_bonjour_free(bsrv);
2863 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2864 struct p2p_srv_upnp, list)
2865 wpas_p2p_srv_upnp_free(usrv);
2867 wpas_p2p_sd_service_update(wpa_s);
2871 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2872 struct wpabuf *query, struct wpabuf *resp)
2874 struct p2p_srv_bonjour *bsrv;
2876 bsrv = os_zalloc(sizeof(*bsrv));
2879 bsrv->query = query;
2881 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2883 wpas_p2p_sd_service_update(wpa_s);
2888 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2889 const struct wpabuf *query)
2891 struct p2p_srv_bonjour *bsrv;
2893 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2896 wpas_p2p_srv_bonjour_free(bsrv);
2897 wpas_p2p_sd_service_update(wpa_s);
2902 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2903 const char *service)
2905 struct p2p_srv_upnp *usrv;
2907 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2908 return 0; /* Already listed */
2909 usrv = os_zalloc(sizeof(*usrv));
2912 usrv->version = version;
2913 usrv->service = os_strdup(service);
2914 if (usrv->service == NULL) {
2918 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2920 wpas_p2p_sd_service_update(wpa_s);
2925 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2926 const char *service)
2928 struct p2p_srv_upnp *usrv;
2930 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2933 wpas_p2p_srv_upnp_free(usrv);
2934 wpas_p2p_sd_service_update(wpa_s);
2939 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2940 const u8 *peer, const char *params,
2941 unsigned int generated_pin)
2943 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2944 " %08d%s", MAC2STR(peer), generated_pin, params);
2948 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2949 const u8 *peer, const char *params)
2951 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2952 "%s", MAC2STR(peer), params);
2956 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2957 const u8 *dev_addr, const u8 *pri_dev_type,
2958 const char *dev_name, u16 supp_config_methods,
2959 u8 dev_capab, u8 group_capab, const u8 *group_id,
2960 size_t group_id_len)
2962 struct wpa_supplicant *wpa_s = ctx;
2963 char devtype[WPS_DEV_TYPE_BUFSIZE];
2965 u8 empty_dev_type[8];
2966 unsigned int generated_pin = 0;
2967 struct wpa_supplicant *group = NULL;
2971 for (group = wpa_s->global->ifaces; group; group = group->next)
2973 struct wpa_ssid *s = group->current_ssid;
2975 s->mode == WPAS_MODE_P2P_GO &&
2976 group_id_len - ETH_ALEN == s->ssid_len &&
2977 os_memcmp(group_id + ETH_ALEN, s->ssid,
2983 if (pri_dev_type == NULL) {
2984 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2985 pri_dev_type = empty_dev_type;
2987 res = os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2988 " pri_dev_type=%s name='%s' config_methods=0x%x "
2989 "dev_capab=0x%x group_capab=0x%x%s%s",
2991 wps_dev_type_bin2str(pri_dev_type, devtype,
2993 dev_name, supp_config_methods, dev_capab, group_capab,
2994 group ? " group=" : "",
2995 group ? group->ifname : "");
2996 if (os_snprintf_error(sizeof(params), res))
2997 wpa_printf(MSG_DEBUG, "P2P: PD Request event truncated");
2998 params[sizeof(params) - 1] = '\0';
3000 if (config_methods & WPS_CONFIG_DISPLAY) {
3001 generated_pin = wps_generate_pin();
3002 wpas_prov_disc_local_display(wpa_s, peer, params,
3004 } else if (config_methods & WPS_CONFIG_KEYPAD)
3005 wpas_prov_disc_local_keypad(wpa_s, peer, params);
3006 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
3007 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
3008 MACSTR "%s", MAC2STR(peer), params);
3010 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
3011 P2P_PROV_DISC_SUCCESS,
3012 config_methods, generated_pin);
3016 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
3018 struct wpa_supplicant *wpa_s = ctx;
3019 unsigned int generated_pin = 0;
3022 if (wpa_s->pending_pd_before_join &&
3023 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
3024 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
3025 wpa_s->pending_pd_before_join = 0;
3026 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3027 "join-existing-group operation");
3028 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
3032 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
3033 wpa_s->pending_pd_use == AUTO_PD_GO_NEG) {
3036 res = os_snprintf(params, sizeof(params), " peer_go=%d",
3037 wpa_s->pending_pd_use == AUTO_PD_JOIN);
3038 if (os_snprintf_error(sizeof(params), res))
3039 params[sizeof(params) - 1] = '\0';
3043 if (config_methods & WPS_CONFIG_DISPLAY)
3044 wpas_prov_disc_local_keypad(wpa_s, peer, params);
3045 else if (config_methods & WPS_CONFIG_KEYPAD) {
3046 generated_pin = wps_generate_pin();
3047 wpas_prov_disc_local_display(wpa_s, peer, params,
3049 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
3050 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
3051 MACSTR "%s", MAC2STR(peer), params);
3053 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3054 P2P_PROV_DISC_SUCCESS,
3055 config_methods, generated_pin);
3059 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
3060 enum p2p_prov_disc_status status)
3062 struct wpa_supplicant *wpa_s = ctx;
3064 if (wpa_s->p2p_fallback_to_go_neg) {
3065 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
3066 "failed - fall back to GO Negotiation");
3067 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
3071 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
3072 wpa_s->pending_pd_before_join = 0;
3073 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
3074 "join-existing-group operation (no ACK for PD "
3076 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
3080 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
3081 " p2p_dev_addr=" MACSTR " status=%d",
3082 MAC2STR(peer), status);
3084 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3089 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
3091 if (channels == NULL)
3092 return 1; /* Assume no restrictions */
3093 return p2p_channels_includes_freq(channels, freq);
3099 * Pick the best frequency to use from all the currently used frequencies.
3101 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3102 struct wpa_used_freq_data *freqs,
3107 /* find a candidate freq that is supported by P2P */
3108 for (c = 0; c < num; c++)
3109 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3115 /* once we have a candidate, try to find a 'better' one */
3116 for (i = c + 1; i < num; i++) {
3117 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3121 * 1. Infrastructure station interfaces have higher preference.
3122 * 2. P2P Clients have higher preference.
3125 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3130 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3133 return freqs[c].freq;
3137 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3138 const u8 *go_dev_addr, const u8 *ssid,
3139 size_t ssid_len, int *go, u8 *group_bssid,
3140 int *force_freq, int persistent_group,
3141 const struct p2p_channels *channels,
3144 struct wpa_supplicant *wpa_s = ctx;
3146 struct wpa_used_freq_data *freqs;
3147 struct wpa_supplicant *grp;
3150 if (!persistent_group) {
3151 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3152 " to join an active group (SSID: %s)",
3153 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
3154 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3155 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3157 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3158 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3159 "authorized invitation");
3163 #ifdef CONFIG_WPS_NFC
3164 if (dev_pw_id >= 0 && wpa_s->p2p_nfc_tag_enabled &&
3165 dev_pw_id == wpa_s->p2p_oob_dev_pw_id) {
3166 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
3167 wpa_s->p2p_wps_method = WPS_NFC;
3168 wpa_s->pending_join_wps_method = WPS_NFC;
3169 os_memcpy(wpa_s->pending_join_dev_addr,
3170 go_dev_addr, ETH_ALEN);
3171 os_memcpy(wpa_s->pending_join_iface_addr,
3175 #endif /* CONFIG_WPS_NFC */
3178 * Do not accept the invitation automatically; notify user and
3181 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3184 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3186 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3187 "running persistent group");
3189 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3193 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3194 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3195 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3196 "invitation to re-invoke a persistent group");
3197 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3198 } else if (!wpa_s->conf->persistent_reconnect)
3199 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3201 for (s = wpa_s->conf->ssid; s; s = s->next) {
3202 if (s->disabled == 2 &&
3203 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3204 s->ssid_len == ssid_len &&
3205 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3210 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3211 " requested reinvocation of an unknown group",
3213 return P2P_SC_FAIL_UNKNOWN_GROUP;
3216 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3218 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3219 wpa_printf(MSG_DEBUG, "P2P: The only available "
3220 "interface is already in use - reject "
3222 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3224 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3225 } else if (s->mode == WPAS_MODE_P2P_GO) {
3227 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3229 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3230 "interface address for the group");
3231 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3233 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3238 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3241 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3242 sizeof(struct wpa_used_freq_data));
3244 int num_channels = wpa_s->num_multichan_concurrent;
3245 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3246 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3250 /* Get one of the frequencies currently in use */
3251 if (best_freq > 0) {
3252 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3253 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
3255 if (wpa_s->num_multichan_concurrent < 2 ||
3256 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3257 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3258 *force_freq = best_freq;
3262 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3263 wpas_p2p_num_unused_channels(wpa_s) > 0) {
3265 /* We are the client */
3266 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3267 "running a GO but we are capable of MCC, "
3268 "figure out the best channel to use");
3270 } else if (!freq_included(channels, *force_freq)) {
3271 /* We are the GO, and *force_freq is not in the
3273 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3274 "in intersection but we are capable of MCC, "
3275 "figure out the best channel to use",
3281 return P2P_SC_SUCCESS;
3285 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3286 const u8 *ssid, size_t ssid_len,
3287 const u8 *go_dev_addr, u8 status,
3290 struct wpa_supplicant *wpa_s = ctx;
3293 for (s = wpa_s->conf->ssid; s; s = s->next) {
3294 if (s->disabled == 2 &&
3295 s->ssid_len == ssid_len &&
3296 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3300 if (status == P2P_SC_SUCCESS) {
3301 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3302 " was accepted; op_freq=%d MHz, SSID=%s",
3303 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3305 int go = s->mode == WPAS_MODE_P2P_GO;
3306 wpas_p2p_group_add_persistent(
3307 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
3308 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3310 wpa_s->user_initiated_pd = 0;
3311 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
3312 wpa_s->p2p_wps_method, 0, op_freq,
3318 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3319 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3320 " was rejected (status %u)", MAC2STR(sa), status);
3326 wpa_msg_global(wpa_s, MSG_INFO,
3327 P2P_EVENT_INVITATION_RECEIVED
3328 "sa=" MACSTR " go_dev_addr=" MACSTR
3329 " bssid=" MACSTR " unknown-network",
3330 MAC2STR(sa), MAC2STR(go_dev_addr),
3333 wpa_msg_global(wpa_s, MSG_INFO,
3334 P2P_EVENT_INVITATION_RECEIVED
3335 "sa=" MACSTR " go_dev_addr=" MACSTR
3337 MAC2STR(sa), MAC2STR(go_dev_addr));
3342 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
3343 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3344 "sa=" MACSTR " persistent=%d freq=%d",
3345 MAC2STR(sa), s->id, op_freq);
3347 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3348 "sa=" MACSTR " persistent=%d",
3349 MAC2STR(sa), s->id);
3354 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3355 struct wpa_ssid *ssid,
3356 const u8 *peer, int inv)
3363 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3364 if (os_memcmp(ssid->p2p_client_list + i * 2 * ETH_ALEN, peer,
3368 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
3369 if (ssid->mode != WPAS_MODE_P2P_GO &&
3370 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3371 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3372 "due to invitation result", ssid->id);
3373 wpas_notify_network_removed(wpa_s, ssid);
3374 wpa_config_remove_network(wpa_s->conf, ssid->id);
3377 return; /* Peer not found in client list */
3380 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
3381 "group %d client list%s",
3382 MAC2STR(peer), ssid->id,
3383 inv ? " due to invitation result" : "");
3384 os_memmove(ssid->p2p_client_list + i * 2 * ETH_ALEN,
3385 ssid->p2p_client_list + (i + 1) * 2 * ETH_ALEN,
3386 (ssid->num_p2p_clients - i - 1) * 2 * ETH_ALEN);
3387 ssid->num_p2p_clients--;
3388 if (wpa_s->parent->conf->update_config &&
3389 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3390 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3394 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3397 struct wpa_ssid *ssid;
3399 wpa_s = wpa_s->global->p2p_invite_group;
3401 return; /* No known invitation group */
3402 ssid = wpa_s->current_ssid;
3403 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3404 !ssid->p2p_persistent_group)
3405 return; /* Not operating as a GO in persistent group */
3406 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3407 ssid->ssid, ssid->ssid_len);
3408 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3412 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
3413 const struct p2p_channels *channels,
3414 const u8 *peer, int neg_freq,
3417 struct wpa_supplicant *wpa_s = ctx;
3418 struct wpa_ssid *ssid;
3422 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3423 "status=%d " MACSTR,
3424 status, MAC2STR(bssid));
3426 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3427 "status=%d ", status);
3429 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3431 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3432 status, MAC2STR(peer));
3433 if (wpa_s->pending_invite_ssid_id == -1) {
3434 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3435 wpas_remove_persistent_client(wpa_s, peer);
3436 return; /* Invitation to active group */
3439 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3440 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3441 "invitation exchange to indicate readiness for "
3445 if (status != P2P_SC_SUCCESS) {
3446 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3447 ssid = wpa_config_get_network(
3448 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3449 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3451 wpas_p2p_remove_pending_group_interface(wpa_s);
3455 ssid = wpa_config_get_network(wpa_s->conf,
3456 wpa_s->pending_invite_ssid_id);
3458 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3459 "data matching with invitation");
3464 * The peer could have missed our ctrl::ack frame for Invitation
3465 * Response and continue retransmitting the frame. To reduce the
3466 * likelihood of the peer not getting successful TX status for the
3467 * Invitation Response frame, wait a short time here before starting
3468 * the persistent group so that we will remain on the current channel to
3469 * acknowledge any possible retransmission from the peer.
3471 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3472 "starting persistent group");
3475 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3476 freq_included(channels, neg_freq))
3478 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3479 freq_included(channels, peer_oper_freq))
3480 freq = peer_oper_freq;
3484 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3485 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3486 wpas_p2p_group_add_persistent(wpa_s, ssid,
3487 ssid->mode == WPAS_MODE_P2P_GO,
3488 wpa_s->p2p_persistent_go_freq,
3490 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3492 ssid->mode == WPAS_MODE_P2P_GO ?
3493 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3498 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3501 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3503 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3507 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3509 reg->channel[reg->channels] = chan;
3514 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3515 struct p2p_channels *chan,
3516 struct p2p_channels *cli_chan)
3520 wpa_s->global->p2p_24ghz_social_channels = 1;
3522 os_memset(cli_chan, 0, sizeof(*cli_chan));
3524 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3527 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3528 chan->reg_class[cla].reg_class = 81;
3529 chan->reg_class[cla].channels = 0;
3530 for (i = 0; i < 11; i++) {
3531 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3532 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3534 if (chan->reg_class[cla].channels)
3537 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3540 /* Operating class 115 - 5 GHz, channels 36-48 */
3541 chan->reg_class[cla].reg_class = 115;
3542 chan->reg_class[cla].channels = 0;
3543 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3544 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3545 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3546 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3547 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3548 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3549 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3550 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3551 if (chan->reg_class[cla].channels)
3554 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3557 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3558 chan->reg_class[cla].reg_class = 124;
3559 chan->reg_class[cla].channels = 0;
3560 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3561 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3562 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3563 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3564 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3565 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3566 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3567 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3568 if (chan->reg_class[cla].channels)
3571 chan->reg_classes = cla;
3576 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3578 enum hostapd_hw_mode mode)
3582 for (i = 0; i < num_modes; i++) {
3583 if (modes[i].mode == mode)
3592 NOT_ALLOWED, NO_IR, ALLOWED
3595 static int has_channel(struct wpa_global *global,
3596 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3601 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3603 if (wpas_p2p_disallowed_freq(global, freq))
3606 for (i = 0; i < mode->num_channels; i++) {
3607 if (mode->channels[i].chan == chan) {
3609 *flags = mode->channels[i].flag;
3610 if (mode->channels[i].flag &
3611 (HOSTAPD_CHAN_DISABLED |
3612 HOSTAPD_CHAN_RADAR))
3614 if (mode->channels[i].flag & HOSTAPD_CHAN_NO_IR)
3624 struct p2p_oper_class_map {
3625 enum hostapd_hw_mode mode;
3630 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
3633 static struct p2p_oper_class_map op_class[] = {
3634 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3635 #if 0 /* Do not enable HT40 on 2 GHz for now */
3636 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3637 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3639 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3640 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3641 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3642 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3643 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3644 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3647 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3648 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3649 * 80 MHz, but currently use the following definition for simplicity
3650 * (these center frequencies are not actual channels, which makes
3651 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3652 * removing invalid channels.
3654 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3655 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
3656 { -1, 0, 0, 0, 0, BW20 }
3660 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3661 struct hostapd_hw_modes *mode,
3664 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3667 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3670 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3672 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3673 * so the center channel is 6 channels away from the start/end.
3675 if (channel >= center_channels[i] - 6 &&
3676 channel <= center_channels[i] + 6)
3677 return center_channels[i];
3683 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3684 struct hostapd_hw_modes *mode,
3689 enum chan_allowed res, ret = ALLOWED;
3691 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3694 if (center_chan >= 58 && center_chan <= 138)
3695 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3697 /* check all the channels are available */
3698 for (i = 0; i < 4; i++) {
3699 int adj_chan = center_chan - 6 + i * 4;
3701 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3702 if (res == NOT_ALLOWED)
3707 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3709 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3711 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3713 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3721 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3722 struct hostapd_hw_modes *mode,
3726 enum chan_allowed res, res2;
3728 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3729 if (bw == BW40MINUS) {
3730 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3732 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3733 } else if (bw == BW40PLUS) {
3734 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3736 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3737 } else if (bw == BW80) {
3738 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3741 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3743 if (res == NO_IR || res2 == NO_IR)
3749 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3750 struct p2p_channels *chan,
3751 struct p2p_channels *cli_chan)
3753 struct hostapd_hw_modes *mode;
3754 int cla, op, cli_cla;
3756 if (wpa_s->hw.modes == NULL) {
3757 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3758 "of all supported channels; assume dualband "
3760 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3765 for (op = 0; op_class[op].op_class; op++) {
3766 struct p2p_oper_class_map *o = &op_class[op];
3768 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3770 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3773 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3774 wpa_s->global->p2p_24ghz_social_channels = 1;
3775 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3776 enum chan_allowed res;
3777 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3778 if (res == ALLOWED) {
3780 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3782 reg = &chan->reg_class[cla];
3784 reg->reg_class = o->op_class;
3786 reg->channel[reg->channels] = ch;
3788 } else if (res == NO_IR &&
3789 wpa_s->conf->p2p_add_cli_chan) {
3790 if (cli_reg == NULL) {
3791 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3793 cli_reg = &cli_chan->reg_class[cli_cla];
3795 cli_reg->reg_class = o->op_class;
3797 cli_reg->channel[cli_reg->channels] = ch;
3798 cli_reg->channels++;
3802 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3803 reg->channel, reg->channels);
3806 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3807 cli_reg->channel, cli_reg->channels);
3811 chan->reg_classes = cla;
3812 cli_chan->reg_classes = cli_cla;
3818 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3819 struct hostapd_hw_modes *mode, u8 channel)
3822 enum chan_allowed ret;
3824 for (op = 0; op_class[op].op_class; op++) {
3825 struct p2p_oper_class_map *o = &op_class[op];
3828 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3829 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3830 o->bw == BW20 || ch != channel)
3832 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3834 return (o->bw == BW40MINUS) ? -1 : 1;
3841 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3842 struct hostapd_hw_modes *mode, u8 channel)
3844 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3847 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3851 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3854 struct wpa_supplicant *wpa_s = ctx;
3856 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3857 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3863 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3867 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3868 const u8 *ssid, size_t ssid_len)
3870 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3871 struct wpa_ssid *s = wpa_s->current_ssid;
3874 if (s->mode != WPAS_MODE_P2P_GO &&
3875 s->mode != WPAS_MODE_AP &&
3876 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3878 if (s->ssid_len != ssid_len ||
3879 os_memcmp(ssid, s->ssid, ssid_len) != 0)
3889 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3890 const u8 *peer_dev_addr)
3892 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3893 struct wpa_ssid *ssid = wpa_s->current_ssid;
3896 if (ssid->mode != WPAS_MODE_INFRA)
3898 if (wpa_s->wpa_state != WPA_COMPLETED &&
3899 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3901 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3909 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3911 struct wpa_supplicant *wpa_s = ctx;
3913 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3917 static int wpas_is_concurrent_session_active(void *ctx)
3919 struct wpa_supplicant *wpa_s = ctx;
3920 struct wpa_supplicant *ifs;
3922 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3925 if (ifs->wpa_state > WPA_ASSOCIATED)
3932 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3934 struct wpa_supplicant *wpa_s = ctx;
3935 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3939 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3940 const char *conf_p2p_dev)
3942 struct wpa_interface iface;
3943 struct wpa_supplicant *p2pdev_wpa_s;
3945 char force_name[100];
3948 ret = os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3950 if (os_snprintf_error(sizeof(ifname), ret))
3952 force_name[0] = '\0';
3953 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3954 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3955 force_name, wpa_s->pending_interface_addr, NULL);
3957 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3960 os_strlcpy(wpa_s->pending_interface_name, ifname,
3961 sizeof(wpa_s->pending_interface_name));
3963 os_memset(&iface, 0, sizeof(iface));
3965 iface.ifname = wpa_s->pending_interface_name;
3966 iface.driver = wpa_s->driver->name;
3967 iface.driver_param = wpa_s->conf->driver_param;
3970 * If a P2P Device configuration file was given, use it as the interface
3971 * configuration file (instead of using parent's configuration file.
3974 iface.confname = conf_p2p_dev;
3975 iface.ctrl_interface = NULL;
3977 iface.confname = wpa_s->confname;
3978 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3980 iface.conf_p2p_dev = NULL;
3982 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3983 if (!p2pdev_wpa_s) {
3984 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3987 p2pdev_wpa_s->parent = wpa_s;
3988 wpa_s->p2p_dev = p2pdev_wpa_s;
3990 wpa_s->pending_interface_name[0] = '\0';
3995 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3996 const u8 *noa, size_t noa_len)
3998 struct wpa_supplicant *wpa_s, *intf = ctx;
4001 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4002 if (wpa_s->waiting_presence_resp)
4006 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
4009 wpa_s->waiting_presence_resp = 0;
4011 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
4012 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
4013 " status=%u noa=%s", MAC2STR(src), status, hex);
4017 static int _wpas_p2p_in_progress(void *ctx)
4019 struct wpa_supplicant *wpa_s = ctx;
4020 return wpas_p2p_in_progress(wpa_s);
4025 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
4026 * @global: Pointer to global data from wpa_supplicant_init()
4027 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4028 * Returns: 0 on success, -1 on failure
4030 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
4032 struct p2p_config p2p;
4035 if (wpa_s->conf->p2p_disabled)
4038 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
4044 os_memset(&p2p, 0, sizeof(p2p));
4046 p2p.debug_print = wpas_p2p_debug_print;
4047 p2p.p2p_scan = wpas_p2p_scan;
4048 p2p.send_action = wpas_send_action;
4049 p2p.send_action_done = wpas_send_action_done;
4050 p2p.go_neg_completed = wpas_go_neg_completed;
4051 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
4052 p2p.dev_found = wpas_dev_found;
4053 p2p.dev_lost = wpas_dev_lost;
4054 p2p.find_stopped = wpas_find_stopped;
4055 p2p.start_listen = wpas_start_listen;
4056 p2p.stop_listen = wpas_stop_listen;
4057 p2p.send_probe_resp = wpas_send_probe_resp;
4058 p2p.sd_request = wpas_sd_request;
4059 p2p.sd_response = wpas_sd_response;
4060 p2p.prov_disc_req = wpas_prov_disc_req;
4061 p2p.prov_disc_resp = wpas_prov_disc_resp;
4062 p2p.prov_disc_fail = wpas_prov_disc_fail;
4063 p2p.invitation_process = wpas_invitation_process;
4064 p2p.invitation_received = wpas_invitation_received;
4065 p2p.invitation_result = wpas_invitation_result;
4066 p2p.get_noa = wpas_get_noa;
4067 p2p.go_connected = wpas_go_connected;
4068 p2p.presence_resp = wpas_presence_resp;
4069 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
4070 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
4072 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
4073 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
4074 p2p.dev_name = wpa_s->conf->device_name;
4075 p2p.manufacturer = wpa_s->conf->manufacturer;
4076 p2p.model_name = wpa_s->conf->model_name;
4077 p2p.model_number = wpa_s->conf->model_number;
4078 p2p.serial_number = wpa_s->conf->serial_number;
4080 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
4081 p2p.config_methods = wpa_s->wps->config_methods;
4084 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
4085 wpa_printf(MSG_ERROR,
4086 "P2P: Failed to configure supported channel list");
4090 if (wpa_s->conf->p2p_listen_reg_class &&
4091 wpa_s->conf->p2p_listen_channel) {
4092 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4093 p2p.channel = wpa_s->conf->p2p_listen_channel;
4094 p2p.channel_forced = 1;
4097 * Pick one of the social channels randomly as the listen
4100 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4101 &p2p.channel) != 0) {
4102 wpa_printf(MSG_ERROR,
4103 "P2P: Failed to select random social channel as listen channel");
4106 p2p.channel_forced = 0;
4108 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4109 p2p.reg_class, p2p.channel);
4111 if (wpa_s->conf->p2p_oper_reg_class &&
4112 wpa_s->conf->p2p_oper_channel) {
4113 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4114 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4115 p2p.cfg_op_channel = 1;
4116 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4117 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4121 * Use random operation channel from 2.4 GHz band social
4122 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4123 * other preference is indicated.
4125 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4126 &p2p.op_channel) != 0) {
4127 wpa_printf(MSG_ERROR,
4128 "P2P: Failed to select random social channel as operation channel");
4131 p2p.cfg_op_channel = 0;
4132 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4133 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4136 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4137 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4138 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4141 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4142 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4143 p2p.country[2] = 0x04;
4145 os_memcpy(p2p.country, "XX\x04", 3);
4147 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4150 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4151 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4152 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4154 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4155 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4157 p2p.max_peers = 100;
4159 if (wpa_s->conf->p2p_ssid_postfix) {
4160 p2p.ssid_postfix_len =
4161 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4162 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4163 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4164 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4165 p2p.ssid_postfix_len);
4168 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4170 p2p.max_listen = wpa_s->max_remain_on_chan;
4172 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4173 wpa_s->conf->p2p_passphrase_len <= 63)
4174 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4176 p2p.passphrase_len = 8;
4178 global->p2p = p2p_init(&p2p);
4179 if (global->p2p == NULL)
4181 global->p2p_init_wpa_s = wpa_s;
4183 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4184 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4186 p2p_add_wps_vendor_extension(
4187 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4190 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4197 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4198 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4200 * This function deinitialize per-interface P2P data.
4202 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4204 if (wpa_s->driver && wpa_s->drv_priv)
4205 wpa_drv_probe_req_report(wpa_s, 0);
4207 if (wpa_s->go_params) {
4208 /* Clear any stored provisioning info */
4209 p2p_clear_provisioning_info(
4211 wpa_s->go_params->peer_device_addr);
4214 os_free(wpa_s->go_params);
4215 wpa_s->go_params = NULL;
4216 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4217 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4218 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4219 wpa_s->p2p_long_listen = 0;
4220 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4221 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4222 wpas_p2p_remove_pending_group_interface(wpa_s);
4223 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4224 wpas_p2p_listen_work_done(wpa_s);
4225 if (wpa_s->p2p_send_action_work) {
4226 os_free(wpa_s->p2p_send_action_work->ctx);
4227 radio_work_done(wpa_s->p2p_send_action_work);
4228 wpa_s->p2p_send_action_work = NULL;
4230 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4232 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4233 wpa_s->p2p_oob_dev_pw = NULL;
4235 os_free(wpa_s->p2p_group_common_freqs);
4236 wpa_s->p2p_group_common_freqs = NULL;
4237 wpa_s->p2p_group_common_freqs_num = 0;
4239 /* TODO: remove group interface from the driver if this wpa_s instance
4240 * is on top of a P2P group interface */
4245 * wpas_p2p_deinit_global - Deinitialize global P2P module
4246 * @global: Pointer to global data from wpa_supplicant_init()
4248 * This function deinitializes the global (per device) P2P module.
4250 static void wpas_p2p_deinit_global(struct wpa_global *global)
4252 struct wpa_supplicant *wpa_s, *tmp;
4254 wpa_s = global->ifaces;
4256 wpas_p2p_service_flush(global->p2p_init_wpa_s);
4258 /* Remove remaining P2P group interfaces */
4259 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4260 wpa_s = wpa_s->next;
4262 tmp = global->ifaces;
4265 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4270 /* Disconnect from the P2P group and deinit the interface */
4271 wpas_p2p_disconnect(tmp);
4275 * Deinit GO data on any possibly remaining interface (if main
4276 * interface is used as GO).
4278 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4279 if (wpa_s->ap_iface)
4280 wpas_p2p_group_deinit(wpa_s);
4283 p2p_deinit(global->p2p);
4285 global->p2p_init_wpa_s = NULL;
4289 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4291 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4292 wpa_s->conf->p2p_no_group_iface)
4293 return 0; /* separate interface disabled per configuration */
4294 if (wpa_s->drv_flags &
4295 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4296 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4297 return 1; /* P2P group requires a new interface in every case
4299 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4300 return 0; /* driver does not support concurrent operations */
4301 if (wpa_s->global->ifaces->next)
4302 return 1; /* more that one interface already in use */
4303 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4304 return 1; /* this interface is already in use */
4309 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4310 const u8 *peer_addr,
4311 enum p2p_wps_method wps_method,
4312 int go_intent, const u8 *own_interface_addr,
4313 unsigned int force_freq, int persistent_group,
4314 struct wpa_ssid *ssid, unsigned int pref_freq)
4316 if (persistent_group && wpa_s->conf->persistent_reconnect)
4317 persistent_group = 2;
4320 * Increase GO config timeout if HT40 is used since it takes some time
4321 * to scan channels for coex purposes before the BSS can be started.
4323 p2p_set_config_timeout(wpa_s->global->p2p,
4324 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4326 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4327 go_intent, own_interface_addr, force_freq,
4328 persistent_group, ssid ? ssid->ssid : NULL,
4329 ssid ? ssid->ssid_len : 0,
4330 wpa_s->p2p_pd_before_go_neg, pref_freq,
4331 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4336 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4337 const u8 *peer_addr,
4338 enum p2p_wps_method wps_method,
4339 int go_intent, const u8 *own_interface_addr,
4340 unsigned int force_freq, int persistent_group,
4341 struct wpa_ssid *ssid, unsigned int pref_freq)
4343 if (persistent_group && wpa_s->conf->persistent_reconnect)
4344 persistent_group = 2;
4346 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4347 go_intent, own_interface_addr, force_freq,
4348 persistent_group, ssid ? ssid->ssid : NULL,
4349 ssid ? ssid->ssid_len : 0, pref_freq,
4350 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4355 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4357 wpa_s->p2p_join_scan_count++;
4358 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4359 wpa_s->p2p_join_scan_count);
4360 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4361 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4362 " for join operationg - stop join attempt",
4363 MAC2STR(wpa_s->pending_join_iface_addr));
4364 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4365 if (wpa_s->p2p_auto_pd) {
4366 wpa_s->p2p_auto_pd = 0;
4367 wpa_msg_global(wpa_s, MSG_INFO,
4368 P2P_EVENT_PROV_DISC_FAILURE
4369 " p2p_dev_addr=" MACSTR " status=N/A",
4370 MAC2STR(wpa_s->pending_join_dev_addr));
4373 wpa_msg_global(wpa_s->parent, MSG_INFO,
4374 P2P_EVENT_GROUP_FORMATION_FAILURE);
4379 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4382 unsigned int num, i;
4383 struct wpa_used_freq_data *freqs;
4385 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4386 /* Multiple channels are supported and not all are in use */
4390 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4391 sizeof(struct wpa_used_freq_data));
4395 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4396 wpa_s->num_multichan_concurrent);
4398 for (i = 0; i < num; i++) {
4399 if (freqs[i].freq == freq) {
4400 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4407 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4416 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4417 const u8 *peer_dev_addr)
4419 struct wpa_bss *bss;
4422 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4425 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4426 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4431 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4433 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4434 "%ld.%06ld (%supdated in last scan)",
4435 bss->last_update.sec, bss->last_update.usec,
4436 updated ? "": "not ");
4442 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4443 struct wpa_scan_results *scan_res)
4445 struct wpa_bss *bss = NULL;
4447 u8 iface_addr[ETH_ALEN];
4449 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4451 if (wpa_s->global->p2p_disabled)
4454 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4455 scan_res ? (int) scan_res->num : -1,
4456 wpa_s->p2p_auto_join ? "auto_" : "");
4459 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4461 if (wpa_s->p2p_auto_pd) {
4462 int join = wpas_p2p_peer_go(wpa_s,
4463 wpa_s->pending_join_dev_addr);
4465 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4466 wpa_s->auto_pd_scan_retry++;
4467 bss = wpa_bss_get_bssid_latest(
4468 wpa_s, wpa_s->pending_join_dev_addr);
4471 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4472 "the peer " MACSTR " at %d MHz",
4473 wpa_s->auto_pd_scan_retry,
4475 pending_join_dev_addr),
4477 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4485 wpa_s->p2p_auto_pd = 0;
4486 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4487 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4488 MAC2STR(wpa_s->pending_join_dev_addr), join);
4489 if (p2p_prov_disc_req(wpa_s->global->p2p,
4490 wpa_s->pending_join_dev_addr,
4491 wpa_s->pending_pd_config_methods, join,
4492 0, wpa_s->user_initiated_pd) < 0) {
4493 wpa_s->p2p_auto_pd = 0;
4494 wpa_msg_global(wpa_s, MSG_INFO,
4495 P2P_EVENT_PROV_DISC_FAILURE
4496 " p2p_dev_addr=" MACSTR " status=N/A",
4497 MAC2STR(wpa_s->pending_join_dev_addr));
4502 if (wpa_s->p2p_auto_join) {
4503 int join = wpas_p2p_peer_go(wpa_s,
4504 wpa_s->pending_join_dev_addr);
4506 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4507 "running a GO -> use GO Negotiation");
4508 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4509 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4510 wpa_s->p2p_persistent_group, 0, 0, 0,
4511 wpa_s->p2p_go_intent,
4512 wpa_s->p2p_connect_freq,
4513 wpa_s->p2p_persistent_id,
4514 wpa_s->p2p_pd_before_go_neg,
4520 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4521 "try to join the group", join ? "" :
4524 wpa_s->p2p_fallback_to_go_neg = 1;
4527 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4528 wpa_s->pending_join_iface_addr);
4530 p2p_get_interface_addr(wpa_s->global->p2p,
4531 wpa_s->pending_join_dev_addr,
4533 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4534 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
4535 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4536 "address for join from " MACSTR " to " MACSTR
4537 " based on newly discovered P2P peer entry",
4538 MAC2STR(wpa_s->pending_join_iface_addr),
4539 MAC2STR(iface_addr));
4540 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4543 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4544 wpa_s->pending_join_iface_addr);
4547 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4548 "from P2P peer table: %d MHz", freq);
4550 if (wpa_s->p2p_join_ssid_len) {
4551 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4552 MACSTR " and SSID %s",
4553 MAC2STR(wpa_s->pending_join_iface_addr),
4554 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4555 wpa_s->p2p_join_ssid_len));
4556 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4557 wpa_s->p2p_join_ssid,
4558 wpa_s->p2p_join_ssid_len);
4561 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4562 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4563 bss = wpa_bss_get_bssid_latest(wpa_s,
4564 wpa_s->pending_join_iface_addr);
4568 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4569 "from BSS table: %d MHz (SSID %s)", freq,
4570 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4575 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4576 wpa_msg_global(wpa_s->parent, MSG_INFO,
4577 P2P_EVENT_GROUP_FORMATION_FAILURE
4578 "reason=FREQ_CONFLICT");
4582 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4583 "prior to joining an existing group (GO " MACSTR
4585 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4586 wpa_s->pending_pd_before_join = 1;
4588 switch (wpa_s->pending_join_wps_method) {
4589 case WPS_PIN_DISPLAY:
4590 method = WPS_CONFIG_KEYPAD;
4592 case WPS_PIN_KEYPAD:
4593 method = WPS_CONFIG_DISPLAY;
4596 method = WPS_CONFIG_PUSHBUTTON;
4603 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4604 wpa_s->pending_join_dev_addr) ==
4607 * We have already performed provision discovery for
4608 * joining the group. Proceed directly to join
4609 * operation without duplicated provision discovery. */
4610 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4611 "with " MACSTR " already done - proceed to "
4613 MAC2STR(wpa_s->pending_join_dev_addr));
4614 wpa_s->pending_pd_before_join = 0;
4618 if (p2p_prov_disc_req(wpa_s->global->p2p,
4619 wpa_s->pending_join_dev_addr, method, 1,
4620 freq, wpa_s->user_initiated_pd) < 0) {
4621 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4622 "Discovery Request before joining an "
4624 wpa_s->pending_pd_before_join = 0;
4630 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4631 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4632 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4633 wpas_p2p_check_join_scan_limit(wpa_s);
4637 /* Start join operation immediately */
4638 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4642 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4643 const u8 *ssid, size_t ssid_len)
4646 struct wpa_driver_scan_params params;
4647 struct wpabuf *wps_ie, *ies;
4649 int freqs[2] = { 0, 0 };
4651 os_memset(¶ms, 0, sizeof(params));
4653 /* P2P Wildcard SSID */
4654 params.num_ssids = 1;
4655 if (ssid && ssid_len) {
4656 params.ssids[0].ssid = ssid;
4657 params.ssids[0].ssid_len = ssid_len;
4658 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4659 wpa_s->p2p_join_ssid_len = ssid_len;
4661 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4662 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4663 wpa_s->p2p_join_ssid_len = 0;
4666 wpa_s->wps->dev.p2p = 1;
4667 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4668 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4670 if (wps_ie == NULL) {
4671 wpas_p2p_scan_res_join(wpa_s, NULL);
4675 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4676 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4678 wpabuf_free(wps_ie);
4679 wpas_p2p_scan_res_join(wpa_s, NULL);
4682 wpabuf_put_buf(ies, wps_ie);
4683 wpabuf_free(wps_ie);
4685 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4687 params.p2p_probe = 1;
4688 params.extra_ies = wpabuf_head(ies);
4689 params.extra_ies_len = wpabuf_len(ies);
4694 * If freq is not provided, check the operating freq of the GO
4695 * and use a single channel scan on if possible.
4697 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4698 wpa_s->pending_join_iface_addr);
4704 params.freqs = freqs;
4708 * Run a scan to update BSS table and start Provision Discovery once
4709 * the new scan results become available.
4711 ret = wpa_drv_scan(wpa_s, ¶ms);
4713 os_get_reltime(&wpa_s->scan_trigger_time);
4714 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4715 wpa_s->own_scan_requested = 1;
4721 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4723 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4724 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4725 wpas_p2p_check_join_scan_limit(wpa_s);
4730 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4732 struct wpa_supplicant *wpa_s = eloop_ctx;
4733 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4737 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4738 const u8 *dev_addr, enum p2p_wps_method wps_method,
4739 int auto_join, int op_freq,
4740 const u8 *ssid, size_t ssid_len)
4742 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4743 MACSTR " dev " MACSTR " op_freq=%d)%s",
4744 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4745 auto_join ? " (auto_join)" : "");
4746 if (ssid && ssid_len) {
4747 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4748 wpa_ssid_txt(ssid, ssid_len));
4751 wpa_s->p2p_auto_pd = 0;
4752 wpa_s->p2p_auto_join = !!auto_join;
4753 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4754 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4755 wpa_s->pending_join_wps_method = wps_method;
4757 /* Make sure we are not running find during connection establishment */
4758 wpas_p2p_stop_find(wpa_s);
4760 wpa_s->p2p_join_scan_count = 0;
4761 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4766 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4767 const u8 *ssid, size_t ssid_len)
4769 struct wpa_supplicant *group;
4770 struct p2p_go_neg_results res;
4771 struct wpa_bss *bss;
4773 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4776 if (group != wpa_s) {
4777 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4778 sizeof(group->p2p_pin));
4779 group->p2p_wps_method = wpa_s->p2p_wps_method;
4782 * Need to mark the current interface for p2p_group_formation
4783 * when a separate group interface is not used. This is needed
4784 * to allow p2p_cancel stop a pending p2p_connect-join.
4785 * wpas_p2p_init_group_interface() addresses this for the case
4786 * where a separate group interface is used.
4788 wpa_s->global->p2p_group_formation = wpa_s;
4791 group->p2p_in_provisioning = 1;
4792 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4794 os_memset(&res, 0, sizeof(res));
4795 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4796 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4798 res.wps_method = wpa_s->pending_join_wps_method;
4799 if (freq && ssid && ssid_len) {
4801 res.ssid_len = ssid_len;
4802 os_memcpy(res.ssid, ssid, ssid_len);
4804 bss = wpa_bss_get_bssid_latest(wpa_s,
4805 wpa_s->pending_join_iface_addr);
4807 res.freq = bss->freq;
4808 res.ssid_len = bss->ssid_len;
4809 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4810 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4812 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4816 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4817 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4819 wpa_drv_cancel_remain_on_channel(wpa_s);
4820 wpa_s->off_channel_freq = 0;
4821 wpa_s->roc_waiting_drv_freq = 0;
4823 wpas_start_wps_enrollee(group, &res);
4826 * Allow a longer timeout for join-a-running-group than normal 15
4827 * second group formation timeout since the GO may not have authorized
4828 * our connection yet.
4830 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4831 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4838 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4839 int *force_freq, int *pref_freq, int go)
4841 struct wpa_used_freq_data *freqs;
4842 int res, best_freq, num_unused;
4843 unsigned int freq_in_use = 0, num, i;
4845 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4846 sizeof(struct wpa_used_freq_data));
4850 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4851 wpa_s->num_multichan_concurrent);
4854 * It is possible that the total number of used frequencies is bigger
4855 * than the number of frequencies used for P2P, so get the system wide
4856 * number of unused frequencies.
4858 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4860 wpa_printf(MSG_DEBUG,
4861 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4862 freq, wpa_s->num_multichan_concurrent, num, num_unused);
4867 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4869 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4871 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4872 "(%u MHz) is not supported for P2P uses",
4878 for (i = 0; i < num; i++) {
4879 if (freqs[i].freq == freq)
4883 if (num_unused <= 0 && !freq_in_use) {
4884 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4889 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4890 "requested channel (%u MHz)", freq);
4895 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
4897 /* We have a candidate frequency to use */
4898 if (best_freq > 0) {
4899 if (*pref_freq == 0 && num_unused > 0) {
4900 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4902 *pref_freq = best_freq;
4904 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4906 *force_freq = best_freq;
4908 } else if (num_unused > 0) {
4909 wpa_printf(MSG_DEBUG,
4910 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4913 wpa_printf(MSG_DEBUG,
4914 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4928 * wpas_p2p_connect - Request P2P Group Formation to be started
4929 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4930 * @peer_addr: Address of the peer P2P Device
4931 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4932 * @persistent_group: Whether to create a persistent group
4933 * @auto_join: Whether to select join vs. GO Negotiation automatically
4934 * @join: Whether to join an existing group (as a client) instead of starting
4935 * Group Owner negotiation; @peer_addr is BSSID in that case
4936 * @auth: Whether to only authorize the connection instead of doing that and
4937 * initiating Group Owner negotiation
4938 * @go_intent: GO Intent or -1 to use default
4939 * @freq: Frequency for the group or 0 for auto-selection
4940 * @persistent_id: Persistent group credentials to use for forcing GO
4941 * parameters or -1 to generate new values (SSID/passphrase)
4942 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4943 * interoperability workaround when initiating group formation
4944 * @ht40: Start GO with 40 MHz channel width
4945 * @vht: Start GO with VHT support
4946 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4947 * failure, -2 on failure due to channel not currently available,
4948 * -3 if forced channel is not supported
4950 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4951 const char *pin, enum p2p_wps_method wps_method,
4952 int persistent_group, int auto_join, int join, int auth,
4953 int go_intent, int freq, int persistent_id, int pd,
4956 int force_freq = 0, pref_freq = 0;
4958 enum wpa_driver_if_type iftype;
4960 struct wpa_ssid *ssid = NULL;
4962 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4965 if (persistent_id >= 0) {
4966 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4967 if (ssid == NULL || ssid->disabled != 2 ||
4968 ssid->mode != WPAS_MODE_P2P_GO)
4972 os_free(wpa_s->global->add_psk);
4973 wpa_s->global->add_psk = NULL;
4975 wpa_s->global->p2p_fail_on_wps_complete = 0;
4978 go_intent = wpa_s->conf->p2p_go_intent;
4981 wpa_s->p2p_long_listen = 0;
4983 wpa_s->p2p_wps_method = wps_method;
4984 wpa_s->p2p_persistent_group = !!persistent_group;
4985 wpa_s->p2p_persistent_id = persistent_id;
4986 wpa_s->p2p_go_intent = go_intent;
4987 wpa_s->p2p_connect_freq = freq;
4988 wpa_s->p2p_fallback_to_go_neg = 0;
4989 wpa_s->p2p_pd_before_go_neg = !!pd;
4990 wpa_s->p2p_go_ht40 = !!ht40;
4991 wpa_s->p2p_go_vht = !!vht;
4994 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4995 else if (wps_method == WPS_PIN_DISPLAY) {
4996 ret = wps_generate_pin();
4997 res = os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin),
4999 if (os_snprintf_error(sizeof(wpa_s->p2p_pin), res))
5000 wpa_s->p2p_pin[sizeof(wpa_s->p2p_pin) - 1] = '\0';
5001 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
5004 wpa_s->p2p_pin[0] = '\0';
5006 if (join || auto_join) {
5007 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
5009 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
5010 "connect a running group from " MACSTR,
5011 MAC2STR(peer_addr));
5012 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5015 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
5016 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
5018 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
5019 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
5023 os_get_reltime(&wpa_s->p2p_auto_started);
5024 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
5026 wpa_s->p2p_auto_started.sec,
5027 wpa_s->p2p_auto_started.usec);
5029 wpa_s->user_initiated_pd = 1;
5030 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
5031 auto_join, freq, NULL, 0) < 0)
5036 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5040 wpas_p2p_set_own_freq_preference(wpa_s,
5041 force_freq ? force_freq : pref_freq);
5043 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
5045 if (wpa_s->create_p2p_iface) {
5046 /* Prepare to add a new interface for the group */
5047 iftype = WPA_IF_P2P_GROUP;
5048 if (go_intent == 15)
5049 iftype = WPA_IF_P2P_GO;
5050 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
5051 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
5052 "interface for the group");
5056 if_addr = wpa_s->pending_interface_addr;
5058 if_addr = wpa_s->own_addr;
5061 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
5063 force_freq, persistent_group, ssid,
5069 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
5070 go_intent, if_addr, force_freq,
5071 persistent_group, ssid, pref_freq) < 0) {
5072 if (wpa_s->create_p2p_iface)
5073 wpas_p2p_remove_pending_group_interface(wpa_s);
5081 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
5082 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5083 * @freq: Frequency of the channel in MHz
5084 * @duration: Duration of the stay on the channel in milliseconds
5086 * This callback is called when the driver indicates that it has started the
5087 * requested remain-on-channel duration.
5089 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5090 unsigned int freq, unsigned int duration)
5092 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5094 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)",
5095 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5096 wpa_s->roc_waiting_drv_freq, freq, duration);
5097 if (wpa_s->off_channel_freq &&
5098 wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5099 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5100 wpa_s->pending_listen_duration);
5101 wpa_s->pending_listen_freq = 0;
5103 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5104 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5110 int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s, unsigned int timeout)
5112 /* Limit maximum Listen state time based on driver limitation. */
5113 if (timeout > wpa_s->max_remain_on_chan)
5114 timeout = wpa_s->max_remain_on_chan;
5116 return p2p_listen(wpa_s->global->p2p, timeout);
5121 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5122 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5123 * @freq: Frequency of the channel in MHz
5125 * This callback is called when the driver indicates that a remain-on-channel
5126 * operation has been completed, i.e., the duration on the requested channel
5129 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5132 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5133 "(p2p_long_listen=%d ms pending_action_tx=%p)",
5134 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
5135 wpas_p2p_listen_work_done(wpa_s);
5136 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5138 if (wpa_s->p2p_long_listen > 0)
5139 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
5140 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5141 return; /* P2P module started a new operation */
5142 if (offchannel_pending_action_tx(wpa_s))
5144 if (wpa_s->p2p_long_listen > 0) {
5145 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5146 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
5149 * When listen duration is over, stop listen & update p2p_state
5152 p2p_stop_listen(wpa_s->global->p2p);
5158 * wpas_p2p_group_remove - Remove a P2P group
5159 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5160 * @ifname: Network interface name of the group interface or "*" to remove all
5162 * Returns: 0 on success, -1 on failure
5164 * This function is used to remove a P2P group. This can be used to disconnect
5165 * from a group in which the local end is a P2P Client or to end a P2P Group in
5166 * case the local end is the Group Owner. If a virtual network interface was
5167 * created for this group, that interface will be removed. Otherwise, only the
5168 * configured P2P group network will be removed from the interface.
5170 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5172 struct wpa_global *global = wpa_s->global;
5173 struct wpa_supplicant *calling_wpa_s = wpa_s;
5175 if (os_strcmp(ifname, "*") == 0) {
5176 struct wpa_supplicant *prev;
5177 wpa_s = global->ifaces;
5180 wpa_s = wpa_s->next;
5181 if (prev->p2p_group_interface !=
5182 NOT_P2P_GROUP_INTERFACE ||
5183 (prev->current_ssid &&
5184 prev->current_ssid->p2p_group))
5185 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
5190 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5191 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5195 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
5199 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5204 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5206 if (wpa_s->best_24_freq > 0 &&
5207 p2p_supported_freq_go(wpa_s->global->p2p,
5208 wpa_s->best_24_freq)) {
5209 freq = wpa_s->best_24_freq;
5210 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5211 "channel: %d MHz", freq);
5213 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5215 freq = 2412 + (r % 3) * 25;
5216 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5217 "channel: %d MHz", freq);
5222 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5224 if (wpa_s->best_5_freq > 0 &&
5225 p2p_supported_freq_go(wpa_s->global->p2p,
5226 wpa_s->best_5_freq)) {
5227 freq = wpa_s->best_5_freq;
5228 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5229 "channel: %d MHz", freq);
5231 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5233 freq = 5180 + (r % 4) * 20;
5234 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5235 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5236 "5 GHz channel for P2P group");
5239 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5240 "channel: %d MHz", freq);
5244 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5245 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5246 "(%u MHz) is not supported for P2P uses",
5255 static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5256 struct p2p_go_neg_results *params,
5257 const struct p2p_channels *channels)
5261 /* first try some random selection of the social channels */
5262 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5265 for (i = 0; i < 3; i++) {
5266 params->freq = 2412 + ((r + i) % 3) * 25;
5267 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5268 freq_included(channels, params->freq) &&
5269 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5273 /* try all channels in reg. class 81 */
5274 for (i = 0; i < 11; i++) {
5275 params->freq = 2412 + i * 5;
5276 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5277 freq_included(channels, params->freq) &&
5278 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5282 /* try social channel class 180 channel 2 */
5283 params->freq = 58320 + 1 * 2160;
5284 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5285 freq_included(channels, params->freq) &&
5286 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5289 /* try all channels in reg. class 180 */
5290 for (i = 0; i < 4; i++) {
5291 params->freq = 58320 + i * 2160;
5292 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5293 freq_included(channels, params->freq) &&
5294 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5298 wpa_printf(MSG_DEBUG, "P2P: No 2.4 and 60 GHz channel allowed");
5301 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5307 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5308 struct p2p_go_neg_results *params,
5309 int freq, int ht40, int vht,
5310 const struct p2p_channels *channels)
5312 struct wpa_used_freq_data *freqs;
5313 unsigned int pref_freq, cand_freq;
5314 unsigned int num, i;
5316 os_memset(params, 0, sizeof(*params));
5317 params->role_go = 1;
5318 params->ht40 = ht40;
5321 if (!freq_included(channels, freq)) {
5322 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5326 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5327 "frequency %d MHz", freq);
5328 params->freq = freq;
5329 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5330 wpa_s->conf->p2p_oper_channel >= 1 &&
5331 wpa_s->conf->p2p_oper_channel <= 11 &&
5332 freq_included(channels,
5333 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5334 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5335 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5336 "frequency %d MHz", params->freq);
5337 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5338 wpa_s->conf->p2p_oper_reg_class == 116 ||
5339 wpa_s->conf->p2p_oper_reg_class == 117 ||
5340 wpa_s->conf->p2p_oper_reg_class == 124 ||
5341 wpa_s->conf->p2p_oper_reg_class == 126 ||
5342 wpa_s->conf->p2p_oper_reg_class == 127) &&
5343 freq_included(channels,
5344 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
5345 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5346 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5347 "frequency %d MHz", params->freq);
5348 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5349 wpa_s->best_overall_freq > 0 &&
5350 p2p_supported_freq_go(wpa_s->global->p2p,
5351 wpa_s->best_overall_freq) &&
5352 freq_included(channels, wpa_s->best_overall_freq)) {
5353 params->freq = wpa_s->best_overall_freq;
5354 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5355 "channel %d MHz", params->freq);
5356 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5357 wpa_s->best_24_freq > 0 &&
5358 p2p_supported_freq_go(wpa_s->global->p2p,
5359 wpa_s->best_24_freq) &&
5360 freq_included(channels, wpa_s->best_24_freq)) {
5361 params->freq = wpa_s->best_24_freq;
5362 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5363 "channel %d MHz", params->freq);
5364 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5365 wpa_s->best_5_freq > 0 &&
5366 p2p_supported_freq_go(wpa_s->global->p2p,
5367 wpa_s->best_5_freq) &&
5368 freq_included(channels, wpa_s->best_5_freq)) {
5369 params->freq = wpa_s->best_5_freq;
5370 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5371 "channel %d MHz", params->freq);
5372 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5374 params->freq = pref_freq;
5375 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5376 "channels", params->freq);
5378 /* no preference, select some channel */
5379 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
5383 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5384 sizeof(struct wpa_used_freq_data));
5388 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5389 wpa_s->num_multichan_concurrent);
5391 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5393 /* First try the best used frequency if possible */
5394 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5395 params->freq = cand_freq;
5397 /* Try any of the used frequencies */
5398 for (i = 0; i < num; i++) {
5399 if (freq_included(channels, freqs[i].freq)) {
5400 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5402 params->freq = freqs[i].freq;
5408 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5409 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5413 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5417 for (i = 0; i < num; i++) {
5418 if (freqs[i].freq == freq)
5423 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5425 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5429 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5439 static struct wpa_supplicant *
5440 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5443 struct wpa_supplicant *group_wpa_s;
5445 if (!wpas_p2p_create_iface(wpa_s)) {
5446 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5448 wpa_s->p2p_first_connection_timeout = 0;
5452 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5453 WPA_IF_P2P_CLIENT) < 0) {
5454 wpa_msg_global(wpa_s, MSG_ERROR,
5455 "P2P: Failed to add group interface");
5458 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5459 if (group_wpa_s == NULL) {
5460 wpa_msg_global(wpa_s, MSG_ERROR,
5461 "P2P: Failed to initialize group interface");
5462 wpas_p2p_remove_pending_group_interface(wpa_s);
5466 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5467 group_wpa_s->ifname);
5468 group_wpa_s->p2p_first_connection_timeout = 0;
5474 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5475 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5476 * @persistent_group: Whether to create a persistent group
5477 * @freq: Frequency for the group or 0 to indicate no hardcoding
5478 * @ht40: Start GO with 40 MHz channel width
5479 * @vht: Start GO with VHT support
5480 * Returns: 0 on success, -1 on failure
5482 * This function creates a new P2P group with the local end as the Group Owner,
5483 * i.e., without using Group Owner Negotiation.
5485 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5486 int freq, int ht40, int vht)
5488 struct p2p_go_neg_results params;
5490 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5493 os_free(wpa_s->global->add_psk);
5494 wpa_s->global->add_psk = NULL;
5496 /* Make sure we are not running find during connection establishment */
5497 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5498 wpas_p2p_stop_find_oper(wpa_s);
5500 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5504 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5507 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5508 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5509 "(%u MHz) is not supported for P2P uses",
5513 p2p_go_params(wpa_s->global->p2p, ¶ms);
5514 params.persistent_group = persistent_group;
5516 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5519 wpas_start_wps_go(wpa_s, ¶ms, 0);
5525 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5526 struct wpa_ssid *params, int addr_allocated,
5529 struct wpa_ssid *ssid;
5531 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5534 wpa_s->p2p_last_4way_hs_fail = NULL;
5536 wpa_supplicant_ap_deinit(wpa_s);
5538 ssid = wpa_config_add_network(wpa_s->conf);
5541 wpa_config_set_network_defaults(ssid);
5542 ssid->temporary = 1;
5543 ssid->proto = WPA_PROTO_RSN;
5544 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5545 ssid->group_cipher = WPA_CIPHER_CCMP;
5546 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5547 ssid->ssid = os_malloc(params->ssid_len);
5548 if (ssid->ssid == NULL) {
5549 wpa_config_remove_network(wpa_s->conf, ssid->id);
5552 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5553 ssid->ssid_len = params->ssid_len;
5554 ssid->p2p_group = 1;
5555 ssid->export_keys = 1;
5556 if (params->psk_set) {
5557 os_memcpy(ssid->psk, params->psk, 32);
5560 if (params->passphrase)
5561 ssid->passphrase = os_strdup(params->passphrase);
5563 wpa_s->show_group_started = 1;
5564 wpa_s->p2p_in_invitation = 1;
5565 wpa_s->p2p_invite_go_freq = freq;
5567 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5569 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5570 wpas_p2p_group_formation_timeout,
5571 wpa_s->parent, NULL);
5572 wpa_supplicant_select_network(wpa_s, ssid);
5578 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5579 struct wpa_ssid *ssid, int addr_allocated,
5580 int force_freq, int neg_freq, int ht40,
5581 int vht, const struct p2p_channels *channels,
5582 int connection_timeout)
5584 struct p2p_go_neg_results params;
5587 if (ssid->disabled != 2 || ssid->ssid == NULL)
5590 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5591 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5592 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5597 os_free(wpa_s->global->add_psk);
5598 wpa_s->global->add_psk = NULL;
5600 /* Make sure we are not running find during connection establishment */
5601 wpas_p2p_stop_find_oper(wpa_s);
5603 wpa_s->p2p_fallback_to_go_neg = 0;
5605 if (ssid->mode == WPAS_MODE_P2P_GO) {
5606 if (force_freq > 0) {
5607 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5611 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5613 (freq > 0 && !freq_included(channels, freq)))
5619 (freq > 0 && !freq_included(channels, freq)))
5623 if (ssid->mode == WPAS_MODE_INFRA)
5624 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5626 if (ssid->mode != WPAS_MODE_P2P_GO)
5629 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
5633 params.psk_set = ssid->psk_set;
5635 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5636 if (ssid->passphrase) {
5637 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5638 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5639 "persistent group");
5642 os_strlcpy(params.passphrase, ssid->passphrase,
5643 sizeof(params.passphrase));
5645 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5646 params.ssid_len = ssid->ssid_len;
5647 params.persistent_group = 1;
5649 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5653 p2p_channels_to_freqs(channels, params.freq_list, P2P_MAX_CHANNELS);
5655 wpa_s->p2p_first_connection_timeout = connection_timeout;
5656 wpas_start_wps_go(wpa_s, ¶ms, 0);
5662 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5663 struct wpabuf *proberesp_ies)
5665 struct wpa_supplicant *wpa_s = ctx;
5666 if (wpa_s->ap_iface) {
5667 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5668 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5669 wpabuf_free(beacon_ies);
5670 wpabuf_free(proberesp_ies);
5674 wpabuf_free(hapd->p2p_beacon_ie);
5675 hapd->p2p_beacon_ie = beacon_ies;
5677 wpabuf_free(hapd->p2p_probe_resp_ie);
5678 hapd->p2p_probe_resp_ie = proberesp_ies;
5680 wpabuf_free(beacon_ies);
5681 wpabuf_free(proberesp_ies);
5683 wpa_supplicant_ap_update_beacon(wpa_s);
5687 static void wpas_p2p_idle_update(void *ctx, int idle)
5689 struct wpa_supplicant *wpa_s = ctx;
5690 if (!wpa_s->ap_iface)
5692 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
5694 if (wpa_s->global->p2p_fail_on_wps_complete &&
5695 wpa_s->p2p_in_provisioning) {
5696 wpas_p2p_grpform_fail_after_wps(wpa_s);
5699 wpas_p2p_set_group_idle_timeout(wpa_s);
5701 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5705 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
5706 struct wpa_ssid *ssid)
5708 struct p2p_group *group;
5709 struct p2p_group_config *cfg;
5711 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5714 cfg = os_zalloc(sizeof(*cfg));
5718 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
5719 cfg->persistent_group = 2;
5720 else if (ssid->p2p_persistent_group)
5721 cfg->persistent_group = 1;
5722 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5723 if (wpa_s->max_stations &&
5724 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5725 cfg->max_clients = wpa_s->max_stations;
5727 cfg->max_clients = wpa_s->conf->max_num_sta;
5728 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5729 cfg->ssid_len = ssid->ssid_len;
5730 cfg->freq = ssid->frequency;
5731 cfg->cb_ctx = wpa_s;
5732 cfg->ie_update = wpas_p2p_ie_update;
5733 cfg->idle_update = wpas_p2p_idle_update;
5735 group = p2p_group_init(wpa_s->global->p2p, cfg);
5738 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
5739 p2p_group_notif_formation_done(group);
5740 wpa_s->p2p_group = group;
5745 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5748 struct wpa_ssid *ssid = wpa_s->current_ssid;
5750 if (!wpa_s->p2p_in_provisioning) {
5751 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5752 "provisioning not in progress");
5756 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5757 u8 go_dev_addr[ETH_ALEN];
5758 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5759 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5761 /* Clear any stored provisioning info */
5762 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5765 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5767 wpa_s->p2p_go_group_formation_completed = 1;
5768 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5770 * Use a separate timeout for initial data connection to
5771 * complete to allow the group to be removed automatically if
5772 * something goes wrong in this step before the P2P group idle
5773 * timeout mechanism is taken into use.
5775 wpa_dbg(wpa_s, MSG_DEBUG,
5776 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5777 P2P_MAX_INITIAL_CONN_WAIT);
5778 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5779 wpas_p2p_group_formation_timeout,
5780 wpa_s->parent, NULL);
5783 * Use a separate timeout for initial data connection to
5784 * complete to allow the group to be removed automatically if
5785 * the client does not complete data connection successfully.
5787 wpa_dbg(wpa_s, MSG_DEBUG,
5788 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5789 P2P_MAX_INITIAL_CONN_WAIT_GO);
5790 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5791 wpas_p2p_group_formation_timeout,
5792 wpa_s->parent, NULL);
5794 * Complete group formation on first successful data connection
5796 wpa_s->p2p_go_group_formation_completed = 0;
5798 if (wpa_s->global->p2p)
5799 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5800 wpas_group_formation_completed(wpa_s, 1);
5804 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5805 struct wps_event_fail *fail)
5807 if (!wpa_s->p2p_in_provisioning) {
5808 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5809 "provisioning not in progress");
5813 if (wpa_s->go_params) {
5814 p2p_clear_provisioning_info(
5816 wpa_s->go_params->peer_device_addr);
5819 wpas_notify_p2p_wps_failed(wpa_s, fail);
5821 if (wpa_s == wpa_s->global->p2p_group_formation) {
5823 * Allow some time for the failed WPS negotiation exchange to
5824 * complete, but remove the group since group formation cannot
5825 * succeed after provisioning failure.
5827 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5828 wpa_s->global->p2p_fail_on_wps_complete = 1;
5829 eloop_deplete_timeout(0, 50000,
5830 wpas_p2p_group_formation_timeout,
5831 wpa_s->parent, NULL);
5836 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5838 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5839 !wpa_s->p2p_in_provisioning)
5842 wpas_p2p_grpform_fail_after_wps(wpa_s);
5848 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5849 const char *config_method,
5850 enum wpas_p2p_prov_disc_use use)
5854 wpa_s->p2p_fallback_to_go_neg = 0;
5855 wpa_s->pending_pd_use = NORMAL_PD;
5856 if (os_strncmp(config_method, "display", 7) == 0)
5857 config_methods = WPS_CONFIG_DISPLAY;
5858 else if (os_strncmp(config_method, "keypad", 6) == 0)
5859 config_methods = WPS_CONFIG_KEYPAD;
5860 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5861 os_strncmp(config_method, "pushbutton", 10) == 0)
5862 config_methods = WPS_CONFIG_PUSHBUTTON;
5864 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5868 if (use == WPAS_P2P_PD_AUTO) {
5869 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5870 wpa_s->pending_pd_config_methods = config_methods;
5871 wpa_s->p2p_auto_pd = 1;
5872 wpa_s->p2p_auto_join = 0;
5873 wpa_s->pending_pd_before_join = 0;
5874 wpa_s->auto_pd_scan_retry = 0;
5875 wpas_p2p_stop_find(wpa_s);
5876 wpa_s->p2p_join_scan_count = 0;
5877 os_get_reltime(&wpa_s->p2p_auto_started);
5878 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5879 wpa_s->p2p_auto_started.sec,
5880 wpa_s->p2p_auto_started.usec);
5881 wpas_p2p_join_scan(wpa_s, NULL);
5885 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5888 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
5889 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5894 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5897 return p2p_scan_result_text(ies, ies_len, buf, end);
5901 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5903 if (!offchannel_pending_action_tx(wpa_s))
5906 wpas_p2p_action_tx_clear(wpa_s);
5908 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5909 "operation request");
5910 offchannel_clear_pending_action_tx(wpa_s);
5914 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5915 enum p2p_discovery_type type,
5916 unsigned int num_req_dev_types, const u8 *req_dev_types,
5917 const u8 *dev_id, unsigned int search_delay,
5918 u8 seek_cnt, const char **seek_string)
5920 wpas_p2p_clear_pending_action_tx(wpa_s);
5921 wpa_s->p2p_long_listen = 0;
5923 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5924 wpa_s->p2p_in_provisioning)
5927 wpa_supplicant_cancel_sched_scan(wpa_s);
5929 return p2p_find(wpa_s->global->p2p, timeout, type,
5930 num_req_dev_types, req_dev_types, dev_id,
5931 search_delay, seek_cnt, seek_string);
5935 static void wpas_p2p_scan_res_ignore_search(struct wpa_supplicant *wpa_s,
5936 struct wpa_scan_results *scan_res)
5938 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
5940 if (wpa_s->p2p_scan_work) {
5941 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
5942 wpa_s->p2p_scan_work = NULL;
5943 radio_work_done(work);
5946 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5950 * Indicate that results have been processed so that the P2P module can
5951 * continue pending tasks.
5953 p2p_scan_res_handled(wpa_s->global->p2p);
5957 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
5959 wpas_p2p_clear_pending_action_tx(wpa_s);
5960 wpa_s->p2p_long_listen = 0;
5961 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5962 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5964 if (wpa_s->global->p2p)
5965 p2p_stop_find(wpa_s->global->p2p);
5967 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_handler) {
5968 wpa_printf(MSG_DEBUG,
5969 "P2P: Do not consider the scan results after stop_find");
5970 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore_search;
5975 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5977 wpas_p2p_stop_find_oper(wpa_s);
5978 wpas_p2p_remove_pending_group_interface(wpa_s);
5982 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5984 struct wpa_supplicant *wpa_s = eloop_ctx;
5985 wpa_s->p2p_long_listen = 0;
5989 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5993 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5996 wpa_supplicant_cancel_sched_scan(wpa_s);
5997 wpas_p2p_clear_pending_action_tx(wpa_s);
6001 * This is a request for unlimited Listen state. However, at
6002 * least for now, this is mapped to a Listen state for one
6007 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
6008 wpa_s->p2p_long_listen = 0;
6011 * Stop previous find/listen operation to avoid trying to request a new
6012 * remain-on-channel operation while the driver is still running the
6015 if (wpa_s->global->p2p)
6016 p2p_stop_find(wpa_s->global->p2p);
6018 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
6019 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
6020 wpa_s->p2p_long_listen = timeout * 1000;
6021 eloop_register_timeout(timeout, 0,
6022 wpas_p2p_long_listen_timeout,
6030 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
6031 u8 *buf, size_t len, int p2p_group)
6033 struct wpabuf *p2p_ie;
6036 if (wpa_s->global->p2p_disabled)
6038 if (wpa_s->conf->p2p_disabled)
6040 if (wpa_s->global->p2p == NULL)
6045 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
6046 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
6048 wpabuf_free(p2p_ie);
6054 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
6055 const u8 *dst, const u8 *bssid,
6056 const u8 *ie, size_t ie_len, int ssi_signal)
6058 if (wpa_s->global->p2p_disabled)
6060 if (wpa_s->global->p2p == NULL)
6063 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
6065 case P2P_PREQ_NOT_P2P:
6066 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
6069 case P2P_PREQ_MALFORMED:
6070 case P2P_PREQ_NOT_LISTEN:
6071 case P2P_PREQ_NOT_PROCESSED:
6072 default: /* make gcc happy */
6074 case P2P_PREQ_PROCESSED:
6080 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
6081 const u8 *sa, const u8 *bssid,
6082 u8 category, const u8 *data, size_t len, int freq)
6084 if (wpa_s->global->p2p_disabled)
6086 if (wpa_s->global->p2p == NULL)
6089 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
6094 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
6096 if (wpa_s->global->p2p_disabled)
6098 if (wpa_s->global->p2p == NULL)
6101 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
6105 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
6107 p2p_group_deinit(wpa_s->p2p_group);
6108 wpa_s->p2p_group = NULL;
6110 wpa_s->ap_configured_cb = NULL;
6111 wpa_s->ap_configured_cb_ctx = NULL;
6112 wpa_s->ap_configured_cb_data = NULL;
6113 wpa_s->connect_without_scan = NULL;
6117 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
6119 wpa_s->p2p_long_listen = 0;
6121 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6124 return p2p_reject(wpa_s->global->p2p, addr);
6128 /* Invite to reinvoke a persistent group */
6129 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
6130 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
6131 int ht40, int vht, int pref_freq)
6133 enum p2p_invite_role role;
6137 int no_pref_freq_given = pref_freq == 0;
6139 wpa_s->global->p2p_invite_group = NULL;
6141 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6143 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6145 wpa_s->p2p_persistent_go_freq = freq;
6146 wpa_s->p2p_go_ht40 = !!ht40;
6147 if (ssid->mode == WPAS_MODE_P2P_GO) {
6148 role = P2P_INVITE_ROLE_GO;
6149 if (peer_addr == NULL) {
6150 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6151 "address in invitation command");
6154 if (wpas_p2p_create_iface(wpa_s)) {
6155 if (wpas_p2p_add_group_interface(wpa_s,
6156 WPA_IF_P2P_GO) < 0) {
6157 wpa_printf(MSG_ERROR, "P2P: Failed to "
6158 "allocate a new interface for the "
6162 bssid = wpa_s->pending_interface_addr;
6164 bssid = wpa_s->own_addr;
6166 role = P2P_INVITE_ROLE_CLIENT;
6167 peer_addr = ssid->bssid;
6169 wpa_s->pending_invite_ssid_id = ssid->id;
6171 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6172 role == P2P_INVITE_ROLE_GO);
6176 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6179 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6180 no_pref_freq_given && pref_freq > 0 &&
6181 wpa_s->num_multichan_concurrent > 1 &&
6182 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6183 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6189 * Stop any find/listen operations before invitation and possibly
6190 * connection establishment.
6192 wpas_p2p_stop_find_oper(wpa_s);
6194 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6195 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
6200 /* Invite to join an active group */
6201 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6202 const u8 *peer_addr, const u8 *go_dev_addr)
6204 struct wpa_global *global = wpa_s->global;
6205 enum p2p_invite_role role;
6207 struct wpa_ssid *ssid;
6209 int freq = 0, force_freq = 0, pref_freq = 0;
6212 wpa_s->p2p_persistent_go_freq = 0;
6213 wpa_s->p2p_go_ht40 = 0;
6214 wpa_s->p2p_go_vht = 0;
6216 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6217 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6220 if (wpa_s == NULL) {
6221 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6225 ssid = wpa_s->current_ssid;
6227 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6232 wpa_s->global->p2p_invite_group = wpa_s;
6233 persistent = ssid->p2p_persistent_group &&
6234 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6235 ssid->ssid, ssid->ssid_len);
6237 if (ssid->mode == WPAS_MODE_P2P_GO) {
6238 role = P2P_INVITE_ROLE_ACTIVE_GO;
6239 bssid = wpa_s->own_addr;
6240 if (go_dev_addr == NULL)
6241 go_dev_addr = wpa_s->global->p2p_dev_addr;
6242 freq = ssid->frequency;
6244 role = P2P_INVITE_ROLE_CLIENT;
6245 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6246 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6247 "invite to current group");
6250 bssid = wpa_s->bssid;
6251 if (go_dev_addr == NULL &&
6252 !is_zero_ether_addr(wpa_s->go_dev_addr))
6253 go_dev_addr = wpa_s->go_dev_addr;
6254 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6255 (int) wpa_s->assoc_freq;
6257 wpa_s->parent->pending_invite_ssid_id = -1;
6259 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6262 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6263 role == P2P_INVITE_ROLE_ACTIVE_GO);
6266 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
6268 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6269 ssid->ssid, ssid->ssid_len, force_freq,
6270 go_dev_addr, persistent, pref_freq, -1);
6274 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6276 struct wpa_ssid *ssid = wpa_s->current_ssid;
6277 u8 go_dev_addr[ETH_ALEN];
6278 int network_id = -1;
6284 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6285 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6286 wpa_s->parent, NULL);
6289 if (!wpa_s->show_group_started || !ssid)
6292 wpa_s->show_group_started = 0;
6294 os_memset(go_dev_addr, 0, ETH_ALEN);
6295 if (ssid->bssid_set)
6296 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6297 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6299 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6301 if (wpa_s->global->p2p_group_formation == wpa_s)
6302 wpa_s->global->p2p_group_formation = NULL;
6304 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6305 (int) wpa_s->assoc_freq;
6308 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6311 res = os_snprintf(ip_addr, sizeof(ip_addr),
6312 " ip_addr=%u.%u.%u.%u "
6313 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6314 ip[0], ip[1], ip[2], ip[3],
6315 ip[4], ip[5], ip[6], ip[7],
6316 ip[8], ip[9], ip[10], ip[11]);
6317 if (os_snprintf_error(sizeof(ip_addr), res))
6321 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6322 ssid->passphrase == NULL && ssid->psk_set ?
6324 ssid->passphrase, go_dev_addr, persistent,
6328 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6331 network_id = ssid->id;
6332 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6336 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6337 u32 interval1, u32 duration2, u32 interval2)
6341 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6344 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6345 wpa_s->current_ssid == NULL ||
6346 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6349 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6350 wpa_s->own_addr, wpa_s->assoc_freq,
6351 duration1, interval1, duration2, interval2);
6353 wpa_s->waiting_presence_resp = 1;
6359 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6360 unsigned int interval)
6362 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6365 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6369 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6371 if (wpa_s->current_ssid == NULL) {
6373 * current_ssid can be cleared when P2P client interface gets
6374 * disconnected, so assume this interface was used as P2P
6379 return wpa_s->current_ssid->p2p_group &&
6380 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6384 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6386 struct wpa_supplicant *wpa_s = eloop_ctx;
6388 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6389 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6394 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6396 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6400 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6404 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6405 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6407 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6410 timeout = wpa_s->conf->p2p_group_idle;
6411 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6412 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6413 timeout = P2P_MAX_CLIENT_IDLE;
6419 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6420 timeout = 0; /* special client mode no-timeout */
6425 if (wpa_s->p2p_in_provisioning) {
6427 * Use the normal group formation timeout during the
6428 * provisioning phase to avoid terminating this process too
6429 * early due to group idle timeout.
6431 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6432 "during provisioning");
6436 if (wpa_s->show_group_started) {
6438 * Use the normal group formation timeout between the end of
6439 * the provisioning phase and completion of 4-way handshake to
6440 * avoid terminating this process too early due to group idle
6443 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6444 "while waiting for initial 4-way handshake to "
6449 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6451 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6456 /* Returns 1 if the interface was removed */
6457 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6458 u16 reason_code, const u8 *ie, size_t ie_len,
6459 int locally_generated)
6461 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6464 if (!locally_generated)
6465 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6468 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6469 wpa_s->current_ssid &&
6470 wpa_s->current_ssid->p2p_group &&
6471 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6472 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6473 "session is ending");
6474 if (wpas_p2p_group_delete(wpa_s,
6475 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6484 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6485 u16 reason_code, const u8 *ie, size_t ie_len,
6486 int locally_generated)
6488 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6491 if (!locally_generated)
6492 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6497 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6499 struct p2p_data *p2p = wpa_s->global->p2p;
6504 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6507 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6508 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6510 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6511 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6514 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6515 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6517 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6518 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6520 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6521 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6522 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6523 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6524 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6527 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6528 p2p_set_sec_dev_types(p2p,
6529 (void *) wpa_s->conf->sec_device_type,
6530 wpa_s->conf->num_sec_device_types);
6532 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6534 p2p_remove_wps_vendor_extensions(p2p);
6535 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6536 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6538 p2p_add_wps_vendor_extension(
6539 p2p, wpa_s->conf->wps_vendor_ext[i]);
6543 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6544 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6546 country[0] = wpa_s->conf->country[0];
6547 country[1] = wpa_s->conf->country[1];
6549 p2p_set_country(p2p, country);
6552 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6553 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6554 wpa_s->conf->p2p_ssid_postfix ?
6555 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6559 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6560 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6562 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6563 u8 reg_class, channel;
6568 if (wpa_s->conf->p2p_listen_reg_class &&
6569 wpa_s->conf->p2p_listen_channel) {
6570 reg_class = wpa_s->conf->p2p_listen_reg_class;
6571 channel = wpa_s->conf->p2p_listen_channel;
6576 * Pick one of the social channels randomly as the
6579 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6582 channel = 1 + (r % 3) * 5;
6585 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6588 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6591 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6592 u8 op_reg_class, op_channel, cfg_op_channel;
6595 if (wpa_s->conf->p2p_oper_reg_class &&
6596 wpa_s->conf->p2p_oper_channel) {
6597 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6598 op_channel = wpa_s->conf->p2p_oper_channel;
6603 * Use random operation channel from (1, 6, 11)
6604 *if no other preference is indicated.
6606 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6609 op_channel = 1 + (r % 3) * 5;
6612 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6615 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6619 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6620 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6621 wpa_s->conf->p2p_pref_chan) < 0) {
6622 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6626 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6627 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6632 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6633 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
6637 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6640 if (!wpa_s->ap_iface)
6642 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6647 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6649 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6652 wpa_s->global->cross_connection = enabled;
6653 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6656 struct wpa_supplicant *iface;
6658 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6660 if (iface->cross_connect_enabled == 0)
6663 iface->cross_connect_enabled = 0;
6664 iface->cross_connect_in_use = 0;
6665 wpa_msg_global(iface->parent, MSG_INFO,
6666 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6668 iface->cross_connect_uplink);
6676 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6678 struct wpa_supplicant *iface;
6680 if (!uplink->global->cross_connection)
6683 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6684 if (!iface->cross_connect_enabled)
6686 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6689 if (iface->ap_iface == NULL)
6691 if (iface->cross_connect_in_use)
6694 iface->cross_connect_in_use = 1;
6695 wpa_msg_global(iface->parent, MSG_INFO,
6696 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6697 iface->ifname, iface->cross_connect_uplink);
6702 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6704 struct wpa_supplicant *iface;
6706 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6707 if (!iface->cross_connect_enabled)
6709 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6712 if (!iface->cross_connect_in_use)
6715 wpa_msg_global(iface->parent, MSG_INFO,
6716 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6717 iface->ifname, iface->cross_connect_uplink);
6718 iface->cross_connect_in_use = 0;
6723 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6725 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6726 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6727 wpa_s->cross_connect_disallowed)
6728 wpas_p2p_disable_cross_connect(wpa_s);
6730 wpas_p2p_enable_cross_connect(wpa_s);
6731 if (!wpa_s->ap_iface &&
6732 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6733 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6737 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6739 wpas_p2p_disable_cross_connect(wpa_s);
6740 if (!wpa_s->ap_iface &&
6741 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6743 wpas_p2p_set_group_idle_timeout(wpa_s);
6747 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6749 struct wpa_supplicant *iface;
6751 if (!wpa_s->global->cross_connection)
6754 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6757 if (iface->drv_flags &
6758 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6760 if ((iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE) &&
6761 iface != wpa_s->parent)
6764 wpa_s->cross_connect_enabled = 1;
6765 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6766 sizeof(wpa_s->cross_connect_uplink));
6767 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6768 "%s to %s whenever uplink is available",
6769 wpa_s->ifname, wpa_s->cross_connect_uplink);
6771 if (iface->ap_iface || iface->current_ssid == NULL ||
6772 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6773 iface->cross_connect_disallowed ||
6774 iface->wpa_state != WPA_COMPLETED)
6777 wpa_s->cross_connect_in_use = 1;
6778 wpa_msg_global(wpa_s->parent, MSG_INFO,
6779 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6780 wpa_s->ifname, wpa_s->cross_connect_uplink);
6786 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6788 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6789 !wpa_s->p2p_in_provisioning)
6790 return 0; /* not P2P client operation */
6792 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6794 if (wpa_s != wpa_s->parent)
6795 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
6796 wpas_p2p_group_formation_failed(wpa_s);
6801 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6803 struct wpa_supplicant *wpa_s = eloop_ctx;
6804 wpas_p2p_notif_pbc_overlap(wpa_s);
6808 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6810 struct p2p_channels chan, cli_chan;
6811 struct wpa_supplicant *ifs;
6813 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6816 os_memset(&chan, 0, sizeof(chan));
6817 os_memset(&cli_chan, 0, sizeof(cli_chan));
6818 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
6819 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6824 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
6826 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6828 if (!ifs->current_ssid ||
6829 !ifs->current_ssid->p2p_group ||
6830 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6831 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6833 freq = ifs->current_ssid->frequency;
6834 if (freq_included(&chan, freq)) {
6835 wpa_dbg(ifs, MSG_DEBUG,
6836 "P2P GO operating frequency %d MHz in valid range",
6841 wpa_dbg(ifs, MSG_DEBUG,
6842 "P2P GO operating in invalid frequency %d MHz", freq);
6843 /* TODO: Consider using CSA or removing the group within
6845 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6850 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6851 struct wpa_scan_results *scan_res)
6853 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6857 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6859 struct wpa_global *global = wpa_s->global;
6863 if (global->p2p == NULL)
6866 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6868 if (wpa_s->pending_interface_name[0] &&
6869 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6872 peer = p2p_get_go_neg_peer(global->p2p);
6874 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6875 MACSTR, MAC2STR(peer));
6876 p2p_unauthorize(global->p2p, peer);
6880 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6881 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6882 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6886 if (wpa_s->pending_pd_before_join) {
6887 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6888 wpa_s->pending_pd_before_join = 0;
6892 wpas_p2p_stop_find(wpa_s);
6894 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6895 if (wpa_s == global->p2p_group_formation &&
6896 (wpa_s->p2p_in_provisioning ||
6897 wpa_s->parent->pending_interface_type ==
6898 WPA_IF_P2P_CLIENT)) {
6899 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6900 "formation found - cancelling",
6903 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6904 wpa_s->parent, NULL);
6905 if (wpa_s->p2p_in_provisioning) {
6906 wpas_group_formation_completed(wpa_s, 0);
6909 wpas_p2p_group_delete(wpa_s,
6910 P2P_GROUP_REMOVAL_REQUESTED);
6912 } else if (wpa_s->p2p_in_invitation) {
6913 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6916 wpas_p2p_group_formation_failed(wpa_s);
6921 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6929 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6931 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6934 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6935 "being available anymore");
6936 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
6940 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6941 int freq_24, int freq_5, int freq_overall)
6943 struct p2p_data *p2p = wpa_s->global->p2p;
6946 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6950 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6953 struct p2p_data *p2p = wpa_s->global->p2p;
6958 if (hwaddr_aton(addr, peer))
6961 return p2p_unauthorize(p2p, peer);
6966 * wpas_p2p_disconnect - Disconnect from a P2P Group
6967 * @wpa_s: Pointer to wpa_supplicant data
6968 * Returns: 0 on success, -1 on failure
6970 * This can be used to disconnect from a group in which the local end is a P2P
6971 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6972 * virtual network interface was created for this group, that interface will be
6973 * removed. Otherwise, only the configured P2P group network will be removed
6974 * from the interface.
6976 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6982 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6987 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6991 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6994 ret = p2p_in_progress(wpa_s->global->p2p);
6997 * Check whether there is an ongoing WPS provisioning step (or
6998 * other parts of group formation) on another interface since
6999 * p2p_in_progress() does not report this to avoid issues for
7000 * scans during such provisioning step.
7002 if (wpa_s->global->p2p_group_formation &&
7003 wpa_s->global->p2p_group_formation != wpa_s) {
7004 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
7005 "in group formation",
7006 wpa_s->global->p2p_group_formation->ifname);
7011 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
7012 struct os_reltime now;
7013 os_get_reltime(&now);
7014 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
7015 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
7016 /* Wait for the first client has expired */
7017 wpa_s->global->p2p_go_wait_client.sec = 0;
7019 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
7028 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
7029 struct wpa_ssid *ssid)
7031 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
7032 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7033 wpa_s->parent, NULL) > 0) {
7035 * Remove the network by scheduling the group formation
7036 * timeout to happen immediately. The teardown code
7037 * needs to be scheduled to run asynch later so that we
7038 * don't delete data from under ourselves unexpectedly.
7039 * Calling wpas_p2p_group_formation_timeout directly
7040 * causes a series of crashes in WPS failure scenarios.
7042 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
7043 "P2P group network getting removed");
7044 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
7045 wpa_s->parent, NULL);
7050 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
7051 const u8 *addr, const u8 *ssid,
7057 for (s = wpa_s->conf->ssid; s; s = s->next) {
7058 if (s->disabled != 2)
7061 (ssid_len != s->ssid_len ||
7062 os_memcmp(ssid, s->ssid, ssid_len) != 0))
7065 if (s->mode == WPAS_MODE_P2P_GO)
7069 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
7070 return s; /* peer is GO in the persistent group */
7071 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
7073 for (i = 0; i < s->num_p2p_clients; i++) {
7074 if (os_memcmp(s->p2p_client_list + i * 2 * ETH_ALEN,
7075 addr, ETH_ALEN) == 0)
7076 return s; /* peer is P2P client in persistent
7085 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
7088 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
7089 wpa_s->parent, NULL) > 0) {
7091 * This can happen if WPS provisioning step is not terminated
7092 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
7093 * peer was able to connect, there is no need to time out group
7094 * formation after this, though. In addition, this is used with
7095 * the initial connection wait on the GO as a separate formation
7096 * timeout and as such, expected to be hit after the initial WPS
7097 * provisioning step.
7099 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
7101 if (!wpa_s->p2p_go_group_formation_completed &&
7102 !wpa_s->group_formation_reported) {
7104 * GO has not yet notified group formation success since
7105 * the WPS step was not completed cleanly. Do that
7106 * notification now since the P2P Client was able to
7107 * connect and as such, must have received the
7108 * credential from the WPS step.
7110 if (wpa_s->global->p2p)
7111 p2p_wps_success_cb(wpa_s->global->p2p, addr);
7112 wpas_group_formation_completed(wpa_s, 1);
7115 if (!wpa_s->p2p_go_group_formation_completed) {
7116 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
7117 wpa_s->p2p_go_group_formation_completed = 1;
7118 wpa_s->global->p2p_group_formation = NULL;
7119 wpa_s->p2p_in_provisioning = 0;
7120 wpa_s->p2p_in_invitation = 0;
7122 wpa_s->global->p2p_go_wait_client.sec = 0;
7125 wpas_p2p_add_persistent_group_client(wpa_s, addr);
7129 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
7132 struct wpa_supplicant *group = wpa_s;
7133 if (wpa_s->global->p2p_group_formation)
7134 group = wpa_s->global->p2p_group_formation;
7135 wpa_s = wpa_s->parent;
7136 offchannel_send_action_done(wpa_s);
7138 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
7139 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
7140 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
7141 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
7142 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
7143 wpa_s->p2p_persistent_id,
7144 wpa_s->p2p_pd_before_go_neg,
7150 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7152 if (!wpa_s->p2p_fallback_to_go_neg ||
7153 wpa_s->p2p_in_provisioning <= 5)
7156 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7157 return 0; /* peer operating as a GO */
7159 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7160 "fallback to GO Negotiation");
7161 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
7167 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7169 struct wpa_supplicant *ifs;
7171 if (wpa_s->wpa_state > WPA_SCANNING) {
7172 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7173 "concurrent operation",
7174 wpa_s->conf->p2p_search_delay);
7175 return wpa_s->conf->p2p_search_delay;
7178 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7180 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
7181 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7182 "delay due to concurrent operation on "
7184 wpa_s->conf->p2p_search_delay,
7186 return wpa_s->conf->p2p_search_delay;
7194 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7195 struct wpa_ssid *s, const u8 *addr,
7198 struct psk_list_entry *psk, *tmp;
7201 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7203 if ((iface_addr && !psk->p2p &&
7204 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7205 (!iface_addr && psk->p2p &&
7206 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7207 wpa_dbg(wpa_s, MSG_DEBUG,
7208 "P2P: Remove persistent group PSK list entry for "
7210 MAC2STR(psk->addr), psk->p2p);
7211 dl_list_del(&psk->list);
7221 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7222 const u8 *p2p_dev_addr,
7223 const u8 *psk, size_t psk_len)
7225 struct wpa_ssid *ssid = wpa_s->current_ssid;
7226 struct wpa_ssid *persistent;
7227 struct psk_list_entry *p, *last;
7229 if (psk_len != sizeof(p->psk))
7233 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7234 " p2p_dev_addr=" MACSTR,
7235 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7236 if (is_zero_ether_addr(p2p_dev_addr))
7237 p2p_dev_addr = NULL;
7239 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7243 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7244 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7245 /* To be added to persistent group once created */
7246 if (wpa_s->global->add_psk == NULL) {
7247 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7248 if (wpa_s->global->add_psk == NULL)
7251 p = wpa_s->global->add_psk;
7254 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7257 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7259 os_memcpy(p->psk, psk, psk_len);
7263 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7264 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7268 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7271 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7275 p = os_zalloc(sizeof(*p));
7280 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7283 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7285 os_memcpy(p->psk, psk, psk_len);
7287 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7288 (last = dl_list_last(&persistent->psk_list,
7289 struct psk_list_entry, list))) {
7290 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7291 MACSTR " (p2p=%u) to make room for a new one",
7292 MAC2STR(last->addr), last->p2p);
7293 dl_list_del(&last->list);
7297 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7298 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7299 p2p_dev_addr == NULL);
7301 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7302 MACSTR, MAC2STR(p2p_dev_addr));
7304 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7307 dl_list_add(&persistent->psk_list, &p->list);
7309 if (wpa_s->parent->conf->update_config &&
7310 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7311 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
7315 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7316 struct wpa_ssid *s, const u8 *addr,
7321 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
7322 if (res > 0 && wpa_s->conf->update_config &&
7323 wpa_config_write(wpa_s->confname, wpa_s->conf))
7324 wpa_dbg(wpa_s, MSG_DEBUG,
7325 "P2P: Failed to update configuration");
7329 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7330 const u8 *peer, int iface_addr)
7332 struct hostapd_data *hapd;
7333 struct hostapd_wpa_psk *psk, *prev, *rem;
7334 struct sta_info *sta;
7336 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7337 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7340 /* Remove per-station PSK entry */
7341 hapd = wpa_s->ap_iface->bss[0];
7343 psk = hapd->conf->ssid.wpa_psk;
7345 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7347 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7348 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7349 MACSTR " iface_addr=%d",
7350 MAC2STR(peer), iface_addr);
7352 prev->next = psk->next;
7354 hapd->conf->ssid.wpa_psk = psk->next;
7364 /* Disconnect from group */
7366 sta = ap_get_sta(hapd, peer);
7368 sta = ap_get_sta_p2p(hapd, peer);
7370 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7371 " (iface_addr=%d) from group",
7372 MAC2STR(peer), iface_addr);
7373 hostapd_drv_sta_deauth(hapd, sta->addr,
7374 WLAN_REASON_DEAUTH_LEAVING);
7375 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7380 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7384 struct wpa_supplicant *w;
7386 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7388 /* Remove from any persistent group */
7389 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
7390 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7393 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
7394 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
7397 /* Remove from any operating group */
7398 for (w = wpa_s->global->ifaces; w; w = w->next)
7399 wpas_p2p_remove_client_go(w, peer, iface_addr);
7403 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7405 struct wpa_supplicant *wpa_s = eloop_ctx;
7406 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7410 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7412 struct wpa_supplicant *wpa_s = eloop_ctx;
7414 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7415 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7419 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7420 struct wpa_ssid *ssid)
7422 struct wpa_supplicant *iface;
7424 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7425 if (!iface->current_ssid ||
7426 iface->current_ssid->frequency == freq ||
7427 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7428 !iface->current_ssid->p2p_group))
7431 /* Remove the connection with least priority */
7432 if (!wpas_is_p2p_prioritized(iface)) {
7433 /* STA connection has priority over existing
7434 * P2P connection, so remove the interface. */
7435 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7436 eloop_register_timeout(0, 0,
7437 wpas_p2p_group_freq_conflict,
7439 /* If connection in progress is P2P connection, do not
7440 * proceed for the connection. */
7446 /* P2P connection has priority, disable the STA network
7448 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7450 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7451 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7452 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7454 /* If P2P connection is in progress, continue
7467 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7469 struct wpa_ssid *ssid = wpa_s->current_ssid;
7471 if (ssid == NULL || !ssid->p2p_group)
7474 if (wpa_s->p2p_last_4way_hs_fail &&
7475 wpa_s->p2p_last_4way_hs_fail == ssid) {
7476 u8 go_dev_addr[ETH_ALEN];
7477 struct wpa_ssid *persistent;
7479 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7481 ssid->ssid_len) <= 0) {
7482 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7486 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7487 MACSTR, MAC2STR(go_dev_addr));
7488 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7491 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7492 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7495 wpa_msg_global(wpa_s->parent, MSG_INFO,
7496 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7499 wpa_s->p2p_last_4way_hs_fail = NULL;
7501 * Remove the group from a timeout to avoid issues with caller
7502 * continuing to use the interface if this is on a P2P group
7505 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7510 wpa_s->p2p_last_4way_hs_fail = ssid;
7515 #ifdef CONFIG_WPS_NFC
7517 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7525 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7529 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7530 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7537 wpabuf_put_be16(ret, wsc_len);
7539 wpabuf_put_buf(ret, wsc);
7540 wpabuf_put_be16(ret, wpabuf_len(p2p));
7541 wpabuf_put_buf(ret, p2p);
7545 wpa_hexdump_buf(MSG_DEBUG,
7546 "P2P: Generated NFC connection handover message", ret);
7550 tmp = ndef_build_p2p(ret);
7553 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7563 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7564 struct wpa_ssid **ssid, u8 *go_dev_addr)
7566 struct wpa_supplicant *iface;
7569 os_memset(go_dev_addr, 0, ETH_ALEN);
7572 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7573 if (iface->wpa_state < WPA_ASSOCIATING ||
7574 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7575 !iface->current_ssid->p2p_group ||
7576 iface->current_ssid->mode != WPAS_MODE_INFRA)
7579 *ssid = iface->current_ssid;
7581 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7582 return iface->assoc_freq;
7588 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7591 struct wpabuf *wsc, *p2p;
7592 struct wpa_ssid *ssid;
7593 u8 go_dev_addr[ETH_ALEN];
7594 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7596 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7597 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7601 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7602 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7603 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7604 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7608 if (cli_freq == 0) {
7609 wsc = wps_build_nfc_handover_req_p2p(
7610 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7613 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7614 go_dev_addr, ssid ? ssid->ssid : NULL,
7615 ssid ? ssid->ssid_len : 0);
7617 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7621 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7624 struct wpabuf *wsc, *p2p;
7625 struct wpa_ssid *ssid;
7626 u8 go_dev_addr[ETH_ALEN];
7627 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7629 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7632 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7633 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7634 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7637 if (cli_freq == 0) {
7638 wsc = wps_build_nfc_handover_sel_p2p(
7640 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7641 DEV_PW_NFC_CONNECTION_HANDOVER,
7642 wpa_s->conf->wps_nfc_dh_pubkey,
7643 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7646 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7647 go_dev_addr, ssid ? ssid->ssid : NULL,
7648 ssid ? ssid->ssid_len : 0);
7650 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7654 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7655 struct p2p_nfc_params *params)
7657 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7658 "connection handover (freq=%d)",
7661 if (params->go_freq && params->go_ssid_len) {
7662 wpa_s->p2p_wps_method = WPS_NFC;
7663 wpa_s->pending_join_wps_method = WPS_NFC;
7664 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7665 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7667 return wpas_p2p_join_start(wpa_s, params->go_freq,
7669 params->go_ssid_len);
7672 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7673 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7674 params->go_freq, -1, 0, 1, 1);
7678 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7679 struct p2p_nfc_params *params, int tag)
7681 int res, persistent;
7682 struct wpa_ssid *ssid;
7684 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7685 "connection handover");
7686 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7687 ssid = wpa_s->current_ssid;
7690 if (ssid->mode != WPAS_MODE_P2P_GO)
7692 if (wpa_s->ap_iface == NULL)
7696 if (wpa_s == NULL) {
7697 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7701 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7702 DEV_PW_NFC_CONNECTION_HANDOVER &&
7703 !wpa_s->parent->p2p_oob_dev_pw) {
7704 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7707 res = wpas_ap_wps_add_nfc_pw(
7708 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7709 wpa_s->parent->p2p_oob_dev_pw,
7710 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7711 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7716 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7720 if (!params->peer ||
7721 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7724 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7725 " to join", MAC2STR(params->peer->p2p_device_addr));
7727 wpa_s->global->p2p_invite_group = wpa_s;
7728 persistent = ssid->p2p_persistent_group &&
7729 wpas_p2p_get_persistent(wpa_s->parent,
7730 params->peer->p2p_device_addr,
7731 ssid->ssid, ssid->ssid_len);
7732 wpa_s->parent->pending_invite_ssid_id = -1;
7734 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7735 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7736 ssid->ssid, ssid->ssid_len, ssid->frequency,
7737 wpa_s->global->p2p_dev_addr, persistent, 0,
7738 wpa_s->parent->p2p_oob_dev_pw_id);
7742 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7743 struct p2p_nfc_params *params,
7746 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7747 "connection handover");
7748 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7749 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7750 forced_freq, -1, 0, 1, 1);
7754 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7755 struct p2p_nfc_params *params,
7760 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7761 "connection handover");
7762 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7763 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7764 forced_freq, -1, 0, 1, 1);
7768 res = wpas_p2p_listen(wpa_s, 60);
7770 p2p_unauthorize(wpa_s->global->p2p,
7771 params->peer->p2p_device_addr);
7778 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7779 const struct wpabuf *data,
7780 int sel, int tag, int forced_freq)
7782 const u8 *pos, *end;
7784 struct p2p_nfc_params params;
7787 os_memset(¶ms, 0, sizeof(params));
7790 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7792 pos = wpabuf_head(data);
7793 end = pos + wpabuf_len(data);
7795 if (end - pos < 2) {
7796 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7800 len = WPA_GET_BE16(pos);
7802 if (len > end - pos) {
7803 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7807 params.wsc_attr = pos;
7808 params.wsc_len = len;
7811 if (end - pos < 2) {
7812 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7816 len = WPA_GET_BE16(pos);
7818 if (len > end - pos) {
7819 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7823 params.p2p_attr = pos;
7824 params.p2p_len = len;
7827 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7828 params.wsc_attr, params.wsc_len);
7829 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7830 params.p2p_attr, params.p2p_len);
7832 wpa_hexdump(MSG_DEBUG,
7833 "P2P: Ignored extra data after P2P attributes",
7837 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
7841 if (params.next_step == NO_ACTION)
7844 if (params.next_step == BOTH_GO) {
7845 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7846 MAC2STR(params.peer->p2p_device_addr));
7850 if (params.next_step == PEER_CLIENT) {
7851 if (!is_zero_ether_addr(params.go_dev_addr)) {
7852 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7853 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7855 MAC2STR(params.peer->p2p_device_addr),
7857 MAC2STR(params.go_dev_addr),
7858 wpa_ssid_txt(params.go_ssid,
7859 params.go_ssid_len));
7861 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7862 "peer=" MACSTR " freq=%d",
7863 MAC2STR(params.peer->p2p_device_addr),
7869 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7870 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7871 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7875 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7876 wpa_s->p2p_oob_dev_pw = NULL;
7878 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7879 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7884 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7885 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7886 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7887 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7888 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7889 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7890 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7894 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7895 "peer OOB Device Password Id %u", id);
7898 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7899 "Device Password Id %u", id);
7900 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7901 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7902 params.oob_dev_pw_len -
7903 WPS_OOB_PUBKEY_HASH_LEN - 2);
7904 wpa_s->p2p_oob_dev_pw_id = id;
7905 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7906 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7907 params.oob_dev_pw_len -
7908 WPS_OOB_PUBKEY_HASH_LEN - 2);
7909 if (wpa_s->p2p_oob_dev_pw == NULL)
7912 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7913 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7914 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7917 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7918 "without Device Password");
7919 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7922 switch (params.next_step) {
7926 /* already covered above */
7929 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
7931 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
7933 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
7935 /* TODO: use own OOB Dev Pw */
7936 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
7943 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7944 const struct wpabuf *data, int forced_freq)
7946 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7949 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7953 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7954 const struct wpabuf *req,
7955 const struct wpabuf *sel, int forced_freq)
7960 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7963 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7965 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7966 wpabuf_head(req), wpabuf_len(req));
7967 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7968 wpabuf_head(sel), wpabuf_len(sel));
7970 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7971 tmp = ndef_parse_p2p(init ? sel : req);
7973 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7977 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7985 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7988 int go_intent = wpa_s->conf->p2p_go_intent;
7989 struct wpa_supplicant *iface;
7991 if (wpa_s->global->p2p == NULL)
7995 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7996 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7998 if (!iface->ap_iface)
8000 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
8002 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
8004 if (wpa_s->p2p_nfc_tag_enabled)
8005 wpas_p2p_remove_pending_group_interface(wpa_s);
8006 wpa_s->p2p_nfc_tag_enabled = 0;
8010 if (wpa_s->global->p2p_disabled)
8013 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
8014 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
8015 wpa_s->conf->wps_nfc_dev_pw == NULL ||
8016 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
8017 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
8018 "to allow static handover cases");
8022 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
8024 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8025 wpabuf_free(wpa_s->p2p_oob_dev_pw);
8026 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8027 if (wpa_s->p2p_oob_dev_pw == NULL)
8029 wpa_s->p2p_peer_oob_pk_hash_known = 0;
8031 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
8032 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
8034 * P2P Group Interface present and the command came on group
8035 * interface, so enable the token for the current interface.
8037 wpa_s->create_p2p_iface = 0;
8039 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
8042 if (wpa_s->create_p2p_iface) {
8043 enum wpa_driver_if_type iftype;
8044 /* Prepare to add a new interface for the group */
8045 iftype = WPA_IF_P2P_GROUP;
8046 if (go_intent == 15)
8047 iftype = WPA_IF_P2P_GO;
8048 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
8049 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
8050 "interface for the group");
8054 if_addr = wpa_s->pending_interface_addr;
8056 if_addr = wpa_s->own_addr;
8058 wpa_s->p2p_nfc_tag_enabled = enabled;
8060 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
8061 struct hostapd_data *hapd;
8062 if (iface->ap_iface == NULL)
8064 hapd = iface->ap_iface->bss[0];
8065 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
8066 hapd->conf->wps_nfc_dh_pubkey =
8067 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
8068 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
8069 hapd->conf->wps_nfc_dh_privkey =
8070 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
8071 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
8072 hapd->conf->wps_nfc_dev_pw =
8073 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
8074 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
8076 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
8077 wpa_dbg(iface, MSG_DEBUG,
8078 "P2P: Failed to enable NFC Tag for GO");
8081 p2p_set_authorized_oob_dev_pw_id(
8082 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
8088 #endif /* CONFIG_WPS_NFC */
8091 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
8092 struct wpa_used_freq_data *freqs,
8095 u8 curr_chan, cand, chan;
8098 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
8099 for (i = 0, cand = 0; i < num; i++) {
8100 ieee80211_freq_to_chan(freqs[i].freq, &chan);
8101 if (curr_chan == chan) {
8106 if (chan == 1 || chan == 6 || chan == 11)
8111 wpa_dbg(wpa_s, MSG_DEBUG,
8112 "P2P: Update Listen channel to %u baased on operating channel",
8114 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
8119 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
8121 struct wpa_used_freq_data *freqs;
8122 unsigned int num = wpa_s->num_multichan_concurrent;
8124 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
8128 * If possible, optimize the Listen channel to be a channel that is
8129 * already used by one of the other interfaces.
8131 if (!wpa_s->conf->p2p_optimize_listen_chan)
8134 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
8137 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
8141 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
8143 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
8148 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8150 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8151 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8152 "the management interface is being removed");
8153 wpas_p2p_deinit_global(wpa_s->global);
8158 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8160 if (wpa_s->ap_iface->bss)
8161 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8162 wpas_p2p_group_deinit(wpa_s);