3 * Copyright (c) 2009-2010, Atheros Communications
4 * Copyright (c) 2010-2014, Jouni Malinen <j@w1.fi>
6 * This software may be distributed under the terms of the BSD license.
7 * See README for more details.
14 #include "common/ieee802_11_common.h"
15 #include "common/ieee802_11_defs.h"
16 #include "common/wpa_ctrl.h"
17 #include "wps/wps_i.h"
19 #include "ap/hostapd.h"
20 #include "ap/ap_config.h"
21 #include "ap/sta_info.h"
22 #include "ap/ap_drv_ops.h"
23 #include "ap/wps_hostapd.h"
24 #include "ap/p2p_hostapd.h"
25 #include "eapol_supp/eapol_supp_sm.h"
26 #include "rsn_supp/wpa.h"
27 #include "wpa_supplicant_i.h"
30 #include "config_ssid.h"
35 #include "offchannel.h"
36 #include "wps_supplicant.h"
37 #include "p2p_supplicant.h"
38 #include "wifi_display.h"
42 * How many times to try to scan to find the GO before giving up on join
45 #define P2P_MAX_JOIN_SCAN_ATTEMPTS 10
47 #define P2P_AUTO_PD_SCAN_ATTEMPTS 5
49 #ifndef P2P_MAX_CLIENT_IDLE
51 * How many seconds to try to reconnect to the GO when connection in P2P client
54 #define P2P_MAX_CLIENT_IDLE 10
55 #endif /* P2P_MAX_CLIENT_IDLE */
57 #ifndef P2P_MAX_INITIAL_CONN_WAIT
59 * How many seconds to wait for initial 4-way handshake to get completed after
60 * WPS provisioning step or after the re-invocation of a persistent group on a
63 #define P2P_MAX_INITIAL_CONN_WAIT 10
64 #endif /* P2P_MAX_INITIAL_CONN_WAIT */
66 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO
68 * How many seconds to wait for initial 4-way handshake to get completed after
69 * WPS provisioning step on the GO. This controls the extra time the P2P
70 * operation is considered to be in progress (e.g., to delay other scans) after
71 * WPS provisioning has been completed on the GO during group formation.
73 #define P2P_MAX_INITIAL_CONN_WAIT_GO 10
74 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO */
76 #ifndef P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE
78 * How many seconds to wait for initial 4-way handshake to get completed after
79 * re-invocation of a persistent group on the GO when the client is expected
80 * to connect automatically (no user interaction).
82 #define P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE 15
83 #endif /* P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE */
85 #define P2P_MGMT_DEVICE_PREFIX "p2p-dev-"
87 enum p2p_group_removal_reason {
88 P2P_GROUP_REMOVAL_UNKNOWN,
89 P2P_GROUP_REMOVAL_SILENT,
90 P2P_GROUP_REMOVAL_FORMATION_FAILED,
91 P2P_GROUP_REMOVAL_REQUESTED,
92 P2P_GROUP_REMOVAL_IDLE_TIMEOUT,
93 P2P_GROUP_REMOVAL_UNAVAILABLE,
94 P2P_GROUP_REMOVAL_GO_ENDING_SESSION,
95 P2P_GROUP_REMOVAL_PSK_FAILURE,
96 P2P_GROUP_REMOVAL_FREQ_CONFLICT
100 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx);
101 static struct wpa_supplicant *
102 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
104 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
105 const u8 *ssid, size_t ssid_len);
106 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
107 const u8 *ssid, size_t ssid_len);
108 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx);
109 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
110 const u8 *dev_addr, enum p2p_wps_method wps_method,
111 int auto_join, int freq,
112 const u8 *ssid, size_t ssid_len);
113 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s);
114 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s);
115 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx);
116 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s);
117 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
119 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx);
120 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
122 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s);
123 static void wpas_stop_listen(void *ctx);
124 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx);
125 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s);
129 * Get the number of concurrent channels that the HW can operate, but that are
130 * currently not in use by any of the wpa_supplicant interfaces.
132 static int wpas_p2p_num_unused_channels(struct wpa_supplicant *wpa_s)
137 freqs = os_calloc(wpa_s->num_multichan_concurrent, sizeof(int));
141 num = get_shared_radio_freqs(wpa_s, freqs,
142 wpa_s->num_multichan_concurrent);
145 unused = wpa_s->num_multichan_concurrent - num;
146 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: num_unused_channels: %d", unused);
152 * Get the frequencies that are currently in use by one or more of the virtual
153 * interfaces, and that are also valid for P2P operation.
156 wpas_p2p_valid_oper_freqs(struct wpa_supplicant *wpa_s,
157 struct wpa_used_freq_data *p2p_freqs,
160 struct wpa_used_freq_data *freqs;
161 unsigned int num, i, j;
163 freqs = os_calloc(wpa_s->num_multichan_concurrent,
164 sizeof(struct wpa_used_freq_data));
168 num = get_shared_radio_freqs_data(wpa_s, freqs,
169 wpa_s->num_multichan_concurrent);
171 os_memset(p2p_freqs, 0, sizeof(struct wpa_used_freq_data) * len);
173 for (i = 0, j = 0; i < num && j < len; i++) {
174 if (p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
175 p2p_freqs[j++] = freqs[i];
180 dump_freq_data(wpa_s, "valid for P2P", p2p_freqs, j);
186 static void wpas_p2p_set_own_freq_preference(struct wpa_supplicant *wpa_s,
189 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
191 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
192 freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
193 wpas_p2p_num_unused_channels(wpa_s) > 0) {
194 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz due to p2p_ignore_shared_freq=1 configuration",
198 p2p_set_own_freq_preference(wpa_s->global->p2p, freq);
202 static void wpas_p2p_scan_res_handler(struct wpa_supplicant *wpa_s,
203 struct wpa_scan_results *scan_res)
207 if (wpa_s->p2p_scan_work) {
208 struct wpa_radio_work *work = wpa_s->p2p_scan_work;
209 wpa_s->p2p_scan_work = NULL;
210 radio_work_done(work);
213 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
216 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS)",
217 (int) scan_res->num);
219 for (i = 0; i < scan_res->num; i++) {
220 struct wpa_scan_res *bss = scan_res->res[i];
221 struct os_reltime time_tmp_age, entry_ts;
225 time_tmp_age.sec = bss->age / 1000;
226 time_tmp_age.usec = (bss->age % 1000) * 1000;
227 os_reltime_sub(&scan_res->fetch_time, &time_tmp_age, &entry_ts);
229 ies = (const u8 *) (bss + 1);
230 ies_len = bss->ie_len;
231 if (bss->beacon_ie_len > 0 &&
232 !wpa_scan_get_vendor_ie(bss, P2P_IE_VENDOR_TYPE) &&
233 wpa_scan_get_vendor_ie_beacon(bss, P2P_IE_VENDOR_TYPE)) {
234 wpa_printf(MSG_DEBUG, "P2P: Use P2P IE(s) from Beacon frame since no P2P IE(s) in Probe Response frames received for "
235 MACSTR, MAC2STR(bss->bssid));
237 ies_len = bss->beacon_ie_len;
241 if (p2p_scan_res_handler(wpa_s->global->p2p, bss->bssid,
242 bss->freq, &entry_ts, bss->level,
247 p2p_scan_res_handled(wpa_s->global->p2p);
251 static void wpas_p2p_trigger_scan_cb(struct wpa_radio_work *work, int deinit)
253 struct wpa_supplicant *wpa_s = work->wpa_s;
254 struct wpa_driver_scan_params *params = work->ctx;
258 if (!work->started) {
259 wpa_scan_free_params(params);
263 wpa_s->p2p_scan_work = NULL;
267 ret = wpa_drv_scan(wpa_s, params);
268 wpa_scan_free_params(params);
271 radio_work_done(work);
275 os_get_reltime(&wpa_s->scan_trigger_time);
276 wpa_s->scan_res_handler = wpas_p2p_scan_res_handler;
277 wpa_s->own_scan_requested = 1;
278 wpa_s->p2p_scan_work = work;
282 static int wpas_p2p_search_social_channel(struct wpa_supplicant *wpa_s,
285 if (wpa_s->global->p2p_24ghz_social_channels &&
286 (freq == 2412 || freq == 2437 || freq == 2462)) {
288 * Search all social channels regardless of whether these have
289 * been disabled for P2P operating channel use to avoid missing
294 return p2p_supported_freq(wpa_s->global->p2p, freq);
298 static int wpas_p2p_scan(void *ctx, enum p2p_scan_type type, int freq,
299 unsigned int num_req_dev_types,
300 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
302 struct wpa_supplicant *wpa_s = ctx;
303 struct wpa_driver_scan_params *params = NULL;
304 struct wpabuf *wps_ie, *ies;
305 unsigned int num_channels = 0;
306 int social_channels_freq[] = { 2412, 2437, 2462, 60480 };
310 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
313 if (wpa_s->p2p_scan_work) {
314 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
318 params = os_zalloc(sizeof(*params));
322 /* P2P Wildcard SSID */
323 params->num_ssids = 1;
324 n = os_malloc(P2P_WILDCARD_SSID_LEN);
327 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
328 params->ssids[0].ssid = n;
329 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
331 wpa_s->wps->dev.p2p = 1;
332 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
333 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
334 num_req_dev_types, req_dev_types);
338 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
339 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
344 wpabuf_put_buf(ies, wps_ie);
347 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
349 params->p2p_probe = 1;
350 n = os_malloc(wpabuf_len(ies));
355 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
356 params->extra_ies = n;
357 params->extra_ies_len = wpabuf_len(ies);
361 case P2P_SCAN_SOCIAL:
362 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 1,
364 if (params->freqs == NULL)
366 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
367 if (wpas_p2p_search_social_channel(
368 wpa_s, social_channels_freq[i]))
369 params->freqs[num_channels++] =
370 social_channels_freq[i];
372 params->freqs[num_channels++] = 0;
376 case P2P_SCAN_SOCIAL_PLUS_ONE:
377 params->freqs = os_calloc(ARRAY_SIZE(social_channels_freq) + 2,
379 if (params->freqs == NULL)
381 for (i = 0; i < ARRAY_SIZE(social_channels_freq); i++) {
382 if (wpas_p2p_search_social_channel(
383 wpa_s, social_channels_freq[i]))
384 params->freqs[num_channels++] =
385 social_channels_freq[i];
387 if (p2p_supported_freq(wpa_s->global->p2p, freq))
388 params->freqs[num_channels++] = freq;
389 params->freqs[num_channels++] = 0;
393 radio_remove_works(wpa_s, "p2p-scan", 0);
394 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
400 wpa_scan_free_params(params);
405 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
407 switch (p2p_group_interface) {
408 case P2P_GROUP_INTERFACE_PENDING:
409 return WPA_IF_P2P_GROUP;
410 case P2P_GROUP_INTERFACE_GO:
411 return WPA_IF_P2P_GO;
412 case P2P_GROUP_INTERFACE_CLIENT:
413 return WPA_IF_P2P_CLIENT;
416 return WPA_IF_P2P_GROUP;
420 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
422 size_t ssid_len, int *go)
426 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
427 for (s = wpa_s->conf->ssid; s; s = s->next) {
428 if (s->disabled != 0 || !s->p2p_group ||
429 s->ssid_len != ssid_len ||
430 os_memcmp(ssid, s->ssid, ssid_len) != 0)
432 if (s->mode == WPAS_MODE_P2P_GO &&
433 s != wpa_s->current_ssid)
436 *go = s->mode == WPAS_MODE_P2P_GO;
445 static void run_wpas_p2p_disconnect(void *eloop_ctx, void *timeout_ctx)
447 struct wpa_supplicant *wpa_s = eloop_ctx;
448 wpa_printf(MSG_DEBUG,
449 "P2P: Complete previously requested removal of %s",
451 wpas_p2p_disconnect(wpa_s);
455 static int wpas_p2p_disconnect_safely(struct wpa_supplicant *wpa_s,
456 struct wpa_supplicant *calling_wpa_s)
458 if (calling_wpa_s == wpa_s &&
459 wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
461 * The calling wpa_s instance is going to be removed. Do that
462 * from an eloop callback to keep the instance available until
463 * the caller has returned. This my be needed, e.g., to provide
464 * control interface responses on the per-interface socket.
466 if (eloop_register_timeout(0, 0, run_wpas_p2p_disconnect,
472 return wpas_p2p_disconnect(wpa_s);
476 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
477 enum p2p_group_removal_reason removal_reason)
479 struct wpa_ssid *ssid;
483 ssid = wpa_s->current_ssid;
486 * The current SSID was not known, but there may still be a
487 * pending P2P group interface waiting for provisioning or a
488 * P2P group that is trying to reconnect.
490 ssid = wpa_s->conf->ssid;
492 if (ssid->p2p_group && ssid->disabled != 2)
497 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
499 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
504 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
506 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
507 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
508 wpa_s->reassociate = 0;
509 wpa_s->disconnected = 1;
514 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
515 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
517 if (os_strcmp(gtype, "client") == 0) {
518 wpa_supplicant_deauthenticate(wpa_s, WLAN_REASON_DEAUTH_LEAVING);
519 if (eloop_is_timeout_registered(wpas_p2p_psk_failure_removal,
521 wpa_printf(MSG_DEBUG,
522 "P2P: PSK failure removal was scheduled, so use PSK failure as reason for group removal");
523 removal_reason = P2P_GROUP_REMOVAL_PSK_FAILURE;
524 eloop_cancel_timeout(wpas_p2p_psk_failure_removal,
529 if (wpa_s->cross_connect_in_use) {
530 wpa_s->cross_connect_in_use = 0;
531 wpa_msg_global(wpa_s->parent, MSG_INFO,
532 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
533 wpa_s->ifname, wpa_s->cross_connect_uplink);
535 switch (removal_reason) {
536 case P2P_GROUP_REMOVAL_REQUESTED:
537 reason = " reason=REQUESTED";
539 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
540 reason = " reason=FORMATION_FAILED";
542 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
543 reason = " reason=IDLE";
545 case P2P_GROUP_REMOVAL_UNAVAILABLE:
546 reason = " reason=UNAVAILABLE";
548 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
549 reason = " reason=GO_ENDING_SESSION";
551 case P2P_GROUP_REMOVAL_PSK_FAILURE:
552 reason = " reason=PSK_FAILURE";
554 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
555 reason = " reason=FREQ_CONFLICT";
561 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
562 wpa_msg_global(wpa_s->parent, MSG_INFO,
563 P2P_EVENT_GROUP_REMOVED "%s %s%s",
564 wpa_s->ifname, gtype, reason);
567 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
568 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
569 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
570 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
571 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
572 wpa_s->parent, NULL) > 0) {
573 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
575 wpa_s->p2p_in_provisioning = 0;
578 wpa_s->p2p_in_invitation = 0;
581 * Make sure wait for the first client does not remain active after the
582 * group has been removed.
584 wpa_s->global->p2p_go_wait_client.sec = 0;
586 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
587 struct wpa_global *global;
589 enum wpa_driver_if_type type;
590 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
592 global = wpa_s->global;
593 ifname = os_strdup(wpa_s->ifname);
594 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
595 eloop_cancel_timeout(run_wpas_p2p_disconnect, wpa_s, NULL);
596 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
597 wpa_s = global->ifaces;
599 wpa_drv_if_remove(wpa_s, type, ifname);
604 if (!wpa_s->p2p_go_group_formation_completed) {
605 wpa_s->global->p2p_group_formation = NULL;
606 wpa_s->p2p_in_provisioning = 0;
609 wpa_s->show_group_started = 0;
610 os_free(wpa_s->go_params);
611 wpa_s->go_params = NULL;
613 wpa_s->waiting_presence_resp = 0;
615 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
616 if (ssid && (ssid->p2p_group ||
617 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
618 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
620 if (ssid == wpa_s->current_ssid) {
621 wpa_sm_set_config(wpa_s->wpa, NULL);
622 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
623 wpa_s->current_ssid = NULL;
626 * Networks objects created during any P2P activities are not
627 * exposed out as they might/will confuse certain non-P2P aware
628 * applications since these network objects won't behave like
631 * Likewise, we don't send out network removed signals for such
634 wpa_config_remove_network(wpa_s->conf, id);
635 wpa_supplicant_clear_status(wpa_s);
636 wpa_supplicant_cancel_sched_scan(wpa_s);
638 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
642 wpa_supplicant_ap_deinit(wpa_s);
644 wpa_drv_deinit_p2p_cli(wpa_s);
650 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
652 const u8 *ssid, size_t ssid_len)
660 if (wpa_s->go_params)
661 bssid = wpa_s->go_params->peer_interface_addr;
663 bssid = wpa_s->bssid;
665 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
666 if (bss == NULL && wpa_s->go_params &&
667 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
668 bss = wpa_bss_get_p2p_dev_addr(
669 wpa_s, wpa_s->go_params->peer_device_addr);
671 u8 iface_addr[ETH_ALEN];
672 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
674 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
677 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
678 "group is persistent - BSS " MACSTR " not found",
683 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
685 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
688 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
689 "group is persistent - BSS " MACSTR
690 " did not include P2P IE", MAC2STR(bssid));
691 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
692 (u8 *) (bss + 1), bss->ie_len);
693 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
694 ((u8 *) bss + 1) + bss->ie_len,
699 group_capab = p2p_get_group_capab(p2p);
700 addr = p2p_get_go_dev_addr(p2p);
701 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
702 "group_capab=0x%x", group_capab);
704 os_memcpy(go_dev_addr, addr, ETH_ALEN);
705 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
708 os_memset(go_dev_addr, 0, ETH_ALEN);
711 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
712 "go_dev_addr=" MACSTR,
713 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
715 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
719 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
720 struct wpa_ssid *ssid,
721 const u8 *go_dev_addr)
726 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
727 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
728 for (s = wpa_s->conf->ssid; s; s = s->next) {
729 if (s->disabled == 2 &&
730 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
731 s->ssid_len == ssid->ssid_len &&
732 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
737 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
739 if (ssid->passphrase && !s->passphrase)
741 else if (ssid->passphrase && s->passphrase &&
742 os_strcmp(ssid->passphrase, s->passphrase) != 0)
745 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
748 s = wpa_config_add_network(wpa_s->conf);
753 * Instead of network_added we emit persistent_group_added
754 * notification. Also to keep the defense checks in
755 * persistent_group obj registration method, we set the
756 * relevant flags in s to designate it as a persistent group.
759 s->p2p_persistent_group = 1;
760 wpas_notify_persistent_group_added(wpa_s, s);
761 wpa_config_set_network_defaults(s);
765 s->p2p_persistent_group = 1;
768 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
769 s->mode = ssid->mode;
770 s->auth_alg = WPA_AUTH_ALG_OPEN;
771 s->key_mgmt = WPA_KEY_MGMT_PSK;
772 s->proto = WPA_PROTO_RSN;
773 s->pairwise_cipher = WPA_CIPHER_CCMP;
775 if (ssid->passphrase) {
776 os_free(s->passphrase);
777 s->passphrase = os_strdup(ssid->passphrase);
781 os_memcpy(s->psk, ssid->psk, 32);
783 if (s->passphrase && !s->psk_set)
784 wpa_config_update_psk(s);
785 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
787 s->ssid = os_malloc(ssid->ssid_len);
790 s->ssid_len = ssid->ssid_len;
791 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
793 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
794 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
795 wpa_s->global->add_psk = NULL;
799 if (changed && wpa_s->conf->update_config &&
800 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
801 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
808 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
811 struct wpa_ssid *ssid, *s;
816 ssid = wpa_s->current_ssid;
817 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
818 !ssid->p2p_persistent_group)
821 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
822 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
825 if (s->ssid_len == ssid->ssid_len &&
826 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
833 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
834 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
838 if (i == s->num_p2p_clients - 1)
839 return; /* already the most recent entry */
841 /* move the entry to mark it most recent */
842 os_memmove(s->p2p_client_list + i * ETH_ALEN,
843 s->p2p_client_list + (i + 1) * ETH_ALEN,
844 (s->num_p2p_clients - i - 1) * ETH_ALEN);
845 os_memcpy(s->p2p_client_list +
846 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
851 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
852 n = os_realloc_array(s->p2p_client_list,
853 s->num_p2p_clients + 1, ETH_ALEN);
856 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
857 s->p2p_client_list = n;
858 s->num_p2p_clients++;
859 } else if (!found && s->p2p_client_list) {
860 /* Not enough room for an additional entry - drop the oldest
862 os_memmove(s->p2p_client_list,
863 s->p2p_client_list + ETH_ALEN,
864 (s->num_p2p_clients - 1) * ETH_ALEN);
865 os_memcpy(s->p2p_client_list +
866 (s->num_p2p_clients - 1) * ETH_ALEN,
870 if (wpa_s->parent->conf->update_config &&
871 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
872 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
876 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
877 int go, struct wpa_ssid *ssid, int freq,
878 const u8 *psk, const char *passphrase,
879 const u8 *go_dev_addr, int persistent,
882 const char *ssid_txt;
886 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
891 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
895 if (passphrase && passphrase[0] == '\0')
899 * Include PSK/passphrase only in the control interface message and
900 * leave it out from the debug log entry.
902 wpa_msg_global_ctrl(wpa_s->parent, MSG_INFO,
903 P2P_EVENT_GROUP_STARTED
904 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
906 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
907 psk ? " psk=" : "", psk_txt,
908 passphrase ? " passphrase=\"" : "",
909 passphrase ? passphrase : "",
910 passphrase ? "\"" : "",
911 MAC2STR(go_dev_addr),
912 persistent ? " [PERSISTENT]" : "", extra);
913 wpa_printf(MSG_INFO, P2P_EVENT_GROUP_STARTED
914 "%s %s ssid=\"%s\" freq=%d go_dev_addr=" MACSTR "%s%s",
915 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
916 MAC2STR(go_dev_addr), persistent ? " [PERSISTENT]" : "",
921 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
924 struct wpa_ssid *ssid;
927 u8 go_dev_addr[ETH_ALEN];
931 * This callback is likely called for the main interface. Update wpa_s
932 * to use the group interface if a new interface was created for the
935 if (wpa_s->global->p2p_group_formation)
936 wpa_s = wpa_s->global->p2p_group_formation;
937 if (wpa_s->p2p_go_group_formation_completed) {
938 wpa_s->global->p2p_group_formation = NULL;
939 wpa_s->p2p_in_provisioning = 0;
941 wpa_s->p2p_in_invitation = 0;
942 wpa_s->group_formation_reported = 1;
945 wpa_msg_global(wpa_s->parent, MSG_INFO,
946 P2P_EVENT_GROUP_FORMATION_FAILURE);
947 wpas_p2p_group_delete(wpa_s,
948 P2P_GROUP_REMOVAL_FORMATION_FAILED);
952 wpa_msg_global(wpa_s->parent, MSG_INFO,
953 P2P_EVENT_GROUP_FORMATION_SUCCESS);
955 ssid = wpa_s->current_ssid;
956 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
957 ssid->mode = WPAS_MODE_P2P_GO;
958 p2p_group_notif_formation_done(wpa_s->p2p_group);
959 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
964 client = ssid->mode == WPAS_MODE_INFRA;
965 if (ssid->mode == WPAS_MODE_P2P_GO) {
966 persistent = ssid->p2p_persistent_group;
967 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
970 persistent = wpas_p2p_persistent_group(wpa_s,
975 client = wpa_s->p2p_group_interface ==
976 P2P_GROUP_INTERFACE_CLIENT;
977 os_memset(go_dev_addr, 0, ETH_ALEN);
980 wpa_s->show_group_started = 0;
983 * Indicate event only after successfully completed 4-way
984 * handshake, i.e., when the interface is ready for data
987 wpa_s->show_group_started = 1;
989 wpas_p2p_group_started(wpa_s, 1, ssid,
990 ssid ? ssid->frequency : 0,
991 ssid && ssid->passphrase == NULL &&
992 ssid->psk_set ? ssid->psk : NULL,
993 ssid ? ssid->passphrase : NULL,
994 go_dev_addr, persistent, "");
995 wpas_p2p_cross_connect_setup(wpa_s);
996 wpas_p2p_set_group_idle_timeout(wpa_s);
1000 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
1003 os_free(wpa_s->global->add_psk);
1004 wpa_s->global->add_psk = NULL;
1006 if (network_id < 0 && ssid)
1007 network_id = ssid->id;
1009 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1010 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1015 struct send_action_work {
1021 unsigned int wait_time;
1026 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
1029 struct wpa_supplicant *wpa_s = eloop_ctx;
1031 if (!wpa_s->p2p_send_action_work)
1034 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
1035 os_free(wpa_s->p2p_send_action_work->ctx);
1036 radio_work_done(wpa_s->p2p_send_action_work);
1037 wpa_s->p2p_send_action_work = NULL;
1041 static void wpas_p2p_action_tx_clear(struct wpa_supplicant *wpa_s)
1043 if (wpa_s->p2p_send_action_work) {
1044 struct send_action_work *awork;
1045 awork = wpa_s->p2p_send_action_work->ctx;
1046 if (awork->wait_time == 0) {
1048 radio_work_done(wpa_s->p2p_send_action_work);
1049 wpa_s->p2p_send_action_work = NULL;
1052 * In theory, this should not be needed, but number of
1053 * places in the P2P code is still using non-zero wait
1054 * time for the last Action frame in the sequence and
1055 * some of these do not call send_action_done().
1057 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1059 eloop_register_timeout(
1060 0, awork->wait_time * 1000,
1061 wpas_p2p_send_action_work_timeout,
1068 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
1070 const u8 *dst, const u8 *src,
1072 const u8 *data, size_t data_len,
1073 enum offchannel_send_action_result
1076 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
1078 wpas_p2p_action_tx_clear(wpa_s);
1080 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
1084 case OFFCHANNEL_SEND_ACTION_SUCCESS:
1085 res = P2P_SEND_ACTION_SUCCESS;
1087 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1088 res = P2P_SEND_ACTION_NO_ACK;
1090 case OFFCHANNEL_SEND_ACTION_FAILED:
1091 res = P2P_SEND_ACTION_FAILED;
1095 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1097 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1098 wpa_s->pending_pd_before_join &&
1099 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1100 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1101 wpa_s->p2p_fallback_to_go_neg) {
1102 wpa_s->pending_pd_before_join = 0;
1103 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1104 "during p2p_connect-auto");
1105 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1111 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1113 struct wpa_supplicant *wpa_s = work->wpa_s;
1114 struct send_action_work *awork = work->ctx;
1117 if (work->started) {
1118 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1120 wpa_s->p2p_send_action_work = NULL;
1121 offchannel_send_action_done(wpa_s);
1127 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1128 awork->bssid, awork->buf, awork->len,
1130 wpas_p2p_send_action_tx_status, 1) < 0) {
1132 radio_work_done(work);
1135 wpa_s->p2p_send_action_work = work;
1139 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1140 unsigned int freq, const u8 *dst,
1141 const u8 *src, const u8 *bssid, const u8 *buf,
1142 size_t len, unsigned int wait_time)
1144 struct send_action_work *awork;
1146 if (wpa_s->p2p_send_action_work) {
1147 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1151 awork = os_zalloc(sizeof(*awork) + len);
1156 os_memcpy(awork->dst, dst, ETH_ALEN);
1157 os_memcpy(awork->src, src, ETH_ALEN);
1158 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1160 awork->wait_time = wait_time;
1161 os_memcpy(awork->buf, buf, len);
1163 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1164 wpas_send_action_cb, awork) < 0) {
1173 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1174 const u8 *src, const u8 *bssid, const u8 *buf,
1175 size_t len, unsigned int wait_time)
1177 struct wpa_supplicant *wpa_s = ctx;
1178 int listen_freq = -1, send_freq = -1;
1180 if (wpa_s->p2p_listen_work)
1181 listen_freq = wpa_s->p2p_listen_work->freq;
1182 if (wpa_s->p2p_send_action_work)
1183 send_freq = wpa_s->p2p_send_action_work->freq;
1184 if (listen_freq != (int) freq && send_freq != (int) freq) {
1185 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1186 listen_freq, send_freq);
1187 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1191 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1192 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1194 wpas_p2p_send_action_tx_status, 1);
1198 static void wpas_send_action_done(void *ctx)
1200 struct wpa_supplicant *wpa_s = ctx;
1202 if (wpa_s->p2p_send_action_work) {
1203 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1205 os_free(wpa_s->p2p_send_action_work->ctx);
1206 radio_work_done(wpa_s->p2p_send_action_work);
1207 wpa_s->p2p_send_action_work = NULL;
1210 offchannel_send_action_done(wpa_s);
1214 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1215 struct p2p_go_neg_results *params)
1217 if (wpa_s->go_params == NULL) {
1218 wpa_s->go_params = os_malloc(sizeof(*params));
1219 if (wpa_s->go_params == NULL)
1222 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1227 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1228 struct p2p_go_neg_results *res)
1230 wpa_s->group_formation_reported = 0;
1231 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1232 " dev_addr " MACSTR " wps_method %d",
1233 MAC2STR(res->peer_interface_addr),
1234 MAC2STR(res->peer_device_addr), res->wps_method);
1235 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1236 res->ssid, res->ssid_len);
1237 wpa_supplicant_ap_deinit(wpa_s);
1238 wpas_copy_go_neg_results(wpa_s, res);
1239 if (res->wps_method == WPS_PBC) {
1240 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1241 #ifdef CONFIG_WPS_NFC
1242 } else if (res->wps_method == WPS_NFC) {
1243 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1244 res->peer_interface_addr,
1245 wpa_s->parent->p2p_oob_dev_pw,
1246 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1247 wpa_s->parent->p2p_oob_dev_pw_id ==
1248 DEV_PW_NFC_CONNECTION_HANDOVER ?
1249 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1252 #endif /* CONFIG_WPS_NFC */
1254 u16 dev_pw_id = DEV_PW_DEFAULT;
1255 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1256 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1257 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1258 wpa_s->p2p_pin, 1, dev_pw_id);
1263 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1264 struct wpa_ssid *ssid)
1266 struct wpa_ssid *persistent;
1267 struct psk_list_entry *psk;
1268 struct hostapd_data *hapd;
1270 if (!wpa_s->ap_iface)
1273 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1275 if (persistent == NULL)
1278 hapd = wpa_s->ap_iface->bss[0];
1280 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1282 struct hostapd_wpa_psk *hpsk;
1284 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1286 MAC2STR(psk->addr), psk->p2p);
1287 hpsk = os_zalloc(sizeof(*hpsk));
1290 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1292 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1294 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1295 hpsk->next = hapd->conf->ssid.wpa_psk;
1296 hapd->conf->ssid.wpa_psk = hpsk;
1301 static void p2p_go_configured(void *ctx, void *data)
1303 struct wpa_supplicant *wpa_s = ctx;
1304 struct p2p_go_neg_results *params = data;
1305 struct wpa_ssid *ssid;
1306 int network_id = -1;
1308 ssid = wpa_s->current_ssid;
1309 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1310 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1311 if (wpa_s->global->p2p_group_formation == wpa_s)
1312 wpa_s->global->p2p_group_formation = NULL;
1313 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1314 params->passphrase[0] == '\0' ?
1317 wpa_s->global->p2p_dev_addr,
1318 params->persistent_group, "");
1319 wpa_s->group_formation_reported = 1;
1321 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1322 if (params->persistent_group) {
1323 network_id = wpas_p2p_store_persistent_group(
1324 wpa_s->parent, ssid,
1325 wpa_s->global->p2p_dev_addr);
1326 wpas_p2p_add_psk_list(wpa_s, ssid);
1329 network_id = ssid->id;
1330 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1331 wpas_p2p_cross_connect_setup(wpa_s);
1332 wpas_p2p_set_group_idle_timeout(wpa_s);
1334 if (wpa_s->p2p_first_connection_timeout) {
1335 wpa_dbg(wpa_s, MSG_DEBUG,
1336 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1337 wpa_s->p2p_first_connection_timeout);
1338 wpa_s->p2p_go_group_formation_completed = 0;
1339 wpa_s->global->p2p_group_formation = wpa_s;
1340 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1341 wpa_s->parent, NULL);
1342 eloop_register_timeout(
1343 wpa_s->p2p_first_connection_timeout, 0,
1344 wpas_p2p_group_formation_timeout,
1345 wpa_s->parent, NULL);
1351 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1352 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1353 params->peer_interface_addr)) {
1354 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1358 if (params->wps_method == WPS_PBC) {
1359 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1360 params->peer_device_addr);
1361 #ifdef CONFIG_WPS_NFC
1362 } else if (params->wps_method == WPS_NFC) {
1363 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1364 DEV_PW_NFC_CONNECTION_HANDOVER &&
1365 !wpa_s->parent->p2p_oob_dev_pw) {
1366 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1369 wpas_ap_wps_add_nfc_pw(
1370 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1371 wpa_s->parent->p2p_oob_dev_pw,
1372 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1373 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1374 #endif /* CONFIG_WPS_NFC */
1375 } else if (wpa_s->p2p_pin[0])
1376 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1377 wpa_s->p2p_pin, NULL, 0, 0);
1378 os_free(wpa_s->go_params);
1379 wpa_s->go_params = NULL;
1383 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1384 struct p2p_go_neg_results *params,
1385 int group_formation)
1387 struct wpa_ssid *ssid;
1389 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1390 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1391 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1396 ssid = wpa_config_add_network(wpa_s->conf);
1398 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1402 wpa_s->show_group_started = 0;
1403 wpa_s->p2p_go_group_formation_completed = 0;
1404 wpa_s->group_formation_reported = 0;
1406 wpa_config_set_network_defaults(ssid);
1407 ssid->temporary = 1;
1408 ssid->p2p_group = 1;
1409 ssid->p2p_persistent_group = params->persistent_group;
1410 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1412 ssid->frequency = params->freq;
1413 ssid->ht40 = params->ht40;
1414 ssid->vht = params->vht;
1415 ssid->ssid = os_zalloc(params->ssid_len + 1);
1417 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1418 ssid->ssid_len = params->ssid_len;
1420 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1421 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1422 ssid->proto = WPA_PROTO_RSN;
1423 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1424 if (os_strlen(params->passphrase) > 0) {
1425 ssid->passphrase = os_strdup(params->passphrase);
1426 if (ssid->passphrase == NULL) {
1427 wpa_msg_global(wpa_s, MSG_ERROR,
1428 "P2P: Failed to copy passphrase for GO");
1429 wpa_config_remove_network(wpa_s->conf, ssid->id);
1433 ssid->passphrase = NULL;
1434 ssid->psk_set = params->psk_set;
1436 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1437 else if (ssid->passphrase)
1438 wpa_config_update_psk(ssid);
1439 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1441 wpa_s->ap_configured_cb = p2p_go_configured;
1442 wpa_s->ap_configured_cb_ctx = wpa_s;
1443 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1444 wpa_s->scan_req = NORMAL_SCAN_REQ;
1445 wpa_s->connect_without_scan = ssid;
1446 wpa_s->reassociate = 1;
1447 wpa_s->disconnected = 0;
1448 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1450 wpa_supplicant_req_scan(wpa_s, 0, 0);
1454 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1455 const struct wpa_supplicant *src)
1457 struct wpa_config *d;
1458 const struct wpa_config *s;
1463 #define C(n) if (s->n) d->n = os_strdup(s->n)
1472 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1473 os_memcpy(d->sec_device_type, s->sec_device_type,
1474 sizeof(d->sec_device_type));
1475 d->num_sec_device_types = s->num_sec_device_types;
1477 d->p2p_group_idle = s->p2p_group_idle;
1478 d->p2p_intra_bss = s->p2p_intra_bss;
1479 d->persistent_reconnect = s->persistent_reconnect;
1480 d->max_num_sta = s->max_num_sta;
1481 d->pbc_in_m1 = s->pbc_in_m1;
1482 d->ignore_old_scan_res = s->ignore_old_scan_res;
1483 d->beacon_int = s->beacon_int;
1484 d->dtim_period = s->dtim_period;
1485 d->disassoc_low_ack = s->disassoc_low_ack;
1486 d->disable_scan_offload = s->disable_scan_offload;
1488 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1489 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1490 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1495 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1496 char *ifname, size_t len)
1498 char *ifname_ptr = wpa_s->ifname;
1500 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1501 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1502 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1505 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1506 if (os_strlen(ifname) >= IFNAMSIZ &&
1507 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1508 /* Try to avoid going over the IFNAMSIZ length limit */
1509 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1514 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1515 enum wpa_driver_if_type type)
1517 char ifname[120], force_ifname[120];
1519 if (wpa_s->pending_interface_name[0]) {
1520 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1521 "- skip creation of a new one");
1522 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1523 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1524 "unknown?! ifname='%s'",
1525 wpa_s->pending_interface_name);
1531 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1532 force_ifname[0] = '\0';
1534 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1536 wpa_s->p2p_group_idx++;
1538 wpa_s->pending_interface_type = type;
1539 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1540 wpa_s->pending_interface_addr, NULL) < 0) {
1541 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1546 if (force_ifname[0]) {
1547 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1549 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1550 sizeof(wpa_s->pending_interface_name));
1552 os_strlcpy(wpa_s->pending_interface_name, ifname,
1553 sizeof(wpa_s->pending_interface_name));
1554 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1555 MACSTR, wpa_s->pending_interface_name,
1556 MAC2STR(wpa_s->pending_interface_addr));
1562 static void wpas_p2p_remove_pending_group_interface(
1563 struct wpa_supplicant *wpa_s)
1565 if (!wpa_s->pending_interface_name[0] ||
1566 is_zero_ether_addr(wpa_s->pending_interface_addr))
1567 return; /* No pending virtual interface */
1569 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1570 wpa_s->pending_interface_name);
1571 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1572 wpa_s->pending_interface_name);
1573 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1574 wpa_s->pending_interface_name[0] = '\0';
1578 static struct wpa_supplicant *
1579 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1581 struct wpa_interface iface;
1582 struct wpa_supplicant *group_wpa_s;
1584 if (!wpa_s->pending_interface_name[0]) {
1585 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1586 if (!wpas_p2p_create_iface(wpa_s))
1589 * Something has forced us to remove the pending interface; try
1590 * to create a new one and hope for the best that we will get
1591 * the same local address.
1593 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1594 WPA_IF_P2P_CLIENT) < 0)
1598 os_memset(&iface, 0, sizeof(iface));
1599 iface.ifname = wpa_s->pending_interface_name;
1600 iface.driver = wpa_s->driver->name;
1601 if (wpa_s->conf->ctrl_interface == NULL &&
1602 wpa_s->parent != wpa_s &&
1604 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1605 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1607 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1608 iface.driver_param = wpa_s->conf->driver_param;
1609 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1610 if (group_wpa_s == NULL) {
1611 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1612 "wpa_supplicant interface");
1615 wpa_s->pending_interface_name[0] = '\0';
1616 group_wpa_s->parent = wpa_s;
1617 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1618 P2P_GROUP_INTERFACE_CLIENT;
1619 wpa_s->global->p2p_group_formation = group_wpa_s;
1621 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1627 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1630 struct wpa_supplicant *wpa_s = eloop_ctx;
1631 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1632 wpas_p2p_group_formation_failed(wpa_s);
1636 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1638 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1639 wpa_s->parent, NULL);
1640 if (wpa_s->global->p2p)
1641 p2p_group_formation_failed(wpa_s->global->p2p);
1642 wpas_group_formation_completed(wpa_s, 0);
1646 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1648 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1649 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1650 wpa_s->parent, NULL);
1651 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1652 wpa_s->parent, NULL);
1653 wpa_s->global->p2p_fail_on_wps_complete = 0;
1657 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1659 if (wpa_s->global->p2p_group_formation != wpa_s)
1661 /* Speed up group formation timeout since this cannot succeed */
1662 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1663 wpa_s->parent, NULL);
1664 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1665 wpa_s->parent, NULL);
1669 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1671 struct wpa_supplicant *wpa_s = ctx;
1673 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1674 wpa_drv_cancel_remain_on_channel(wpa_s);
1675 wpa_s->off_channel_freq = 0;
1676 wpa_s->roc_waiting_drv_freq = 0;
1680 wpa_msg_global(wpa_s, MSG_INFO,
1681 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1683 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1684 wpas_p2p_remove_pending_group_interface(wpa_s);
1688 if (wpa_s->p2p_go_ht40)
1690 if (wpa_s->p2p_go_vht)
1693 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1694 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1696 res->role_go ? "GO" : "client", res->freq, res->ht40,
1697 MAC2STR(res->peer_device_addr),
1698 MAC2STR(res->peer_interface_addr),
1699 p2p_wps_method_text(res->wps_method));
1700 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1702 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1703 struct wpa_ssid *ssid;
1704 ssid = wpa_config_get_network(wpa_s->conf,
1705 wpa_s->p2p_persistent_id);
1706 if (ssid && ssid->disabled == 2 &&
1707 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1708 size_t len = os_strlen(ssid->passphrase);
1709 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1710 "on requested persistent group");
1711 os_memcpy(res->passphrase, ssid->passphrase, len);
1712 res->passphrase[len] = '\0';
1716 if (wpa_s->create_p2p_iface) {
1717 struct wpa_supplicant *group_wpa_s =
1718 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1719 if (group_wpa_s == NULL) {
1720 wpas_p2p_remove_pending_group_interface(wpa_s);
1721 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
1723 wpas_p2p_group_formation_failed(wpa_s);
1726 if (group_wpa_s != wpa_s) {
1727 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1728 sizeof(group_wpa_s->p2p_pin));
1729 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1731 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1732 wpa_s->pending_interface_name[0] = '\0';
1733 group_wpa_s->p2p_in_provisioning = 1;
1736 wpas_start_wps_go(group_wpa_s, res, 1);
1738 wpas_start_wps_enrollee(group_wpa_s, res);
1740 wpa_s->p2p_in_provisioning = 1;
1741 wpa_s->global->p2p_group_formation = wpa_s;
1744 wpas_start_wps_go(wpa_s, res, 1);
1746 wpas_start_wps_enrollee(ctx, res);
1749 wpa_s->p2p_long_listen = 0;
1750 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1752 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1753 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1754 (res->peer_config_timeout % 100) * 10000,
1755 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1759 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1761 struct wpa_supplicant *wpa_s = ctx;
1762 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1763 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1765 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1769 static void wpas_dev_found(void *ctx, const u8 *addr,
1770 const struct p2p_peer_info *info,
1773 #ifndef CONFIG_NO_STDOUT_DEBUG
1774 struct wpa_supplicant *wpa_s = ctx;
1775 char devtype[WPS_DEV_TYPE_BUFSIZE];
1776 char *wfd_dev_info_hex = NULL;
1778 #ifdef CONFIG_WIFI_DISPLAY
1779 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1780 WFD_SUBELEM_DEVICE_INFO);
1781 #endif /* CONFIG_WIFI_DISPLAY */
1783 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1784 " p2p_dev_addr=" MACSTR
1785 " pri_dev_type=%s name='%s' config_methods=0x%x "
1786 "dev_capab=0x%x group_capab=0x%x%s%s%s new=%d",
1787 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1788 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1790 info->device_name, info->config_methods,
1791 info->dev_capab, info->group_capab,
1792 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1793 wfd_dev_info_hex ? wfd_dev_info_hex : "",
1794 info->vendor_elems ? " vendor_elems=1" : "",
1797 os_free(wfd_dev_info_hex);
1798 #endif /* CONFIG_NO_STDOUT_DEBUG */
1800 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1804 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1806 struct wpa_supplicant *wpa_s = ctx;
1808 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1809 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1811 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1815 static void wpas_find_stopped(void *ctx)
1817 struct wpa_supplicant *wpa_s = ctx;
1818 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1822 struct wpas_p2p_listen_work {
1824 unsigned int duration;
1825 struct wpabuf *probe_resp_ie;
1829 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1833 wpabuf_free(lwork->probe_resp_ie);
1838 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1840 struct wpas_p2p_listen_work *lwork;
1842 if (!wpa_s->p2p_listen_work)
1845 lwork = wpa_s->p2p_listen_work->ctx;
1846 wpas_p2p_listen_work_free(lwork);
1847 radio_work_done(wpa_s->p2p_listen_work);
1848 wpa_s->p2p_listen_work = NULL;
1852 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1854 struct wpa_supplicant *wpa_s = work->wpa_s;
1855 struct wpas_p2p_listen_work *lwork = work->ctx;
1858 if (work->started) {
1859 wpa_s->p2p_listen_work = NULL;
1860 wpas_stop_listen(wpa_s);
1862 wpas_p2p_listen_work_free(lwork);
1866 wpa_s->p2p_listen_work = work;
1868 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1870 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1871 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1872 "report received Probe Request frames");
1873 wpas_p2p_listen_work_done(wpa_s);
1877 wpa_s->pending_listen_freq = lwork->freq;
1878 wpa_s->pending_listen_duration = lwork->duration;
1880 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1882 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1883 "to remain on channel (%u MHz) for Listen "
1884 "state", lwork->freq);
1885 wpas_p2p_listen_work_done(wpa_s);
1886 wpa_s->pending_listen_freq = 0;
1889 wpa_s->off_channel_freq = 0;
1890 wpa_s->roc_waiting_drv_freq = lwork->freq;
1894 static int wpas_start_listen(void *ctx, unsigned int freq,
1895 unsigned int duration,
1896 const struct wpabuf *probe_resp_ie)
1898 struct wpa_supplicant *wpa_s = ctx;
1899 struct wpas_p2p_listen_work *lwork;
1901 if (wpa_s->p2p_listen_work) {
1902 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
1906 lwork = os_zalloc(sizeof(*lwork));
1910 lwork->duration = duration;
1911 if (probe_resp_ie) {
1912 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1913 if (lwork->probe_resp_ie == NULL) {
1914 wpas_p2p_listen_work_free(lwork);
1919 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1921 wpas_p2p_listen_work_free(lwork);
1929 static void wpas_stop_listen(void *ctx)
1931 struct wpa_supplicant *wpa_s = ctx;
1932 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1933 wpa_drv_cancel_remain_on_channel(wpa_s);
1934 wpa_s->off_channel_freq = 0;
1935 wpa_s->roc_waiting_drv_freq = 0;
1937 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1938 wpa_drv_probe_req_report(wpa_s, 0);
1939 wpas_p2p_listen_work_done(wpa_s);
1943 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1945 struct wpa_supplicant *wpa_s = ctx;
1946 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1951 * DNS Header section is used only to calculate compression pointers, so the
1952 * contents of this data does not matter, but the length needs to be reserved
1953 * in the virtual packet.
1955 #define DNS_HEADER_LEN 12
1958 * 27-octet in-memory packet from P2P specification containing two implied
1959 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1961 #define P2P_SD_IN_MEMORY_LEN 27
1963 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1964 u8 **spos, const u8 *end)
1966 while (*spos < end) {
1967 u8 val = ((*spos)[0] & 0xc0) >> 6;
1970 if (val == 1 || val == 2) {
1971 /* These are reserved values in RFC 1035 */
1972 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1973 "sequence starting with 0x%x", val);
1982 if (*spos + 2 > end) {
1983 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1984 "DNS offset field");
1988 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1989 if (offset >= *spos - start) {
1990 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1991 "pointer offset %u", offset);
1996 spos_tmp = start + offset;
1997 return p2p_sd_dns_uncompress_label(upos, uend, start,
2003 len = (*spos)[0] & 0x3f;
2008 if (*spos + len > end) {
2009 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
2010 "sequence - no room for label with length "
2015 if (*upos + len + 2 > uend)
2018 os_memcpy(*upos, *spos, len);
2030 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
2031 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
2032 * not large enough */
2033 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
2034 size_t msg_len, size_t offset)
2036 /* 27-octet in-memory packet from P2P specification */
2037 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
2038 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
2039 u8 *tmp, *end, *spos;
2045 if (offset > msg_len)
2048 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
2051 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
2052 end = spos + msg_len;
2055 os_memset(tmp, 0, DNS_HEADER_LEN);
2056 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
2057 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
2060 uend = buf + buf_len;
2062 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
2071 } else if (upos[-1] == '.')
2079 static struct p2p_srv_bonjour *
2080 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
2081 const struct wpabuf *query)
2083 struct p2p_srv_bonjour *bsrv;
2086 len = wpabuf_len(query);
2087 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2088 struct p2p_srv_bonjour, list) {
2089 if (len == wpabuf_len(bsrv->query) &&
2090 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
2098 static struct p2p_srv_upnp *
2099 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2100 const char *service)
2102 struct p2p_srv_upnp *usrv;
2104 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2105 struct p2p_srv_upnp, list) {
2106 if (version == usrv->version &&
2107 os_strcmp(service, usrv->service) == 0)
2114 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2119 if (wpabuf_tailroom(resp) < 5)
2122 /* Length (to be filled) */
2123 len_pos = wpabuf_put(resp, 2);
2124 wpabuf_put_u8(resp, srv_proto);
2125 wpabuf_put_u8(resp, srv_trans_id);
2127 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2128 /* Response Data: empty */
2129 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2133 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2134 struct wpabuf *resp, u8 srv_trans_id)
2136 struct p2p_srv_bonjour *bsrv;
2139 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2141 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2142 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2146 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2147 struct p2p_srv_bonjour, list) {
2148 if (wpabuf_tailroom(resp) <
2149 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2151 /* Length (to be filled) */
2152 len_pos = wpabuf_put(resp, 2);
2153 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2154 wpabuf_put_u8(resp, srv_trans_id);
2156 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2157 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2158 wpabuf_head(bsrv->resp),
2159 wpabuf_len(bsrv->resp));
2161 wpabuf_put_buf(resp, bsrv->query); /* Key */
2162 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2163 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2169 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2172 char str_rx[256], str_srv[256];
2174 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2175 return 0; /* Too short to include DNS Type and Version */
2176 if (os_memcmp(query + query_len - 3,
2177 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2179 return 0; /* Mismatch in DNS Type or Version */
2180 if (query_len == wpabuf_len(bsrv->query) &&
2181 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2182 return 1; /* Binary match */
2184 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2186 return 0; /* Failed to uncompress query */
2187 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2188 wpabuf_head(bsrv->query),
2189 wpabuf_len(bsrv->query) - 3, 0))
2190 return 0; /* Failed to uncompress service */
2192 return os_strcmp(str_rx, str_srv) == 0;
2196 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2197 struct wpabuf *resp, u8 srv_trans_id,
2198 const u8 *query, size_t query_len)
2200 struct p2p_srv_bonjour *bsrv;
2204 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2206 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2207 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2208 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2213 if (query_len == 0) {
2214 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2218 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2219 struct p2p_srv_bonjour, list) {
2220 if (!match_bonjour_query(bsrv, query, query_len))
2223 if (wpabuf_tailroom(resp) <
2224 5 + query_len + 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);
2235 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2236 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2237 wpabuf_head(bsrv->resp),
2238 wpabuf_len(bsrv->resp));
2241 wpabuf_put_data(resp, query, query_len); /* Key */
2242 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2244 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2248 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2250 if (wpabuf_tailroom(resp) < 5)
2253 /* Length (to be filled) */
2254 len_pos = wpabuf_put(resp, 2);
2255 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2256 wpabuf_put_u8(resp, srv_trans_id);
2259 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2260 /* Response Data: empty */
2261 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2267 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2268 struct wpabuf *resp, u8 srv_trans_id)
2270 struct p2p_srv_upnp *usrv;
2273 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2275 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2276 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2280 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2281 struct p2p_srv_upnp, list) {
2282 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2285 /* Length (to be filled) */
2286 len_pos = wpabuf_put(resp, 2);
2287 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2288 wpabuf_put_u8(resp, srv_trans_id);
2291 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2293 wpabuf_put_u8(resp, usrv->version);
2294 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2296 wpabuf_put_str(resp, usrv->service);
2297 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2303 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2304 struct wpabuf *resp, u8 srv_trans_id,
2305 const u8 *query, size_t query_len)
2307 struct p2p_srv_upnp *usrv;
2313 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2316 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2317 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2318 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2323 if (query_len == 0) {
2324 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
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_UPNP);
2334 wpabuf_put_u8(resp, srv_trans_id);
2337 str = os_malloc(query_len);
2340 os_memcpy(str, query + 1, query_len - 1);
2341 str[query_len - 1] = '\0';
2343 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2344 struct p2p_srv_upnp, list) {
2345 if (version != usrv->version)
2348 if (os_strcmp(str, "ssdp:all") != 0 &&
2349 os_strstr(usrv->service, str) == NULL)
2352 if (wpabuf_tailroom(resp) < 2)
2356 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2358 wpabuf_put_u8(resp, version);
2360 wpabuf_put_u8(resp, ',');
2364 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2366 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2368 wpabuf_put_str(resp, usrv->service);
2373 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2376 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2377 /* Response Data: empty */
2380 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2384 #ifdef CONFIG_WIFI_DISPLAY
2385 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2386 struct wpabuf *resp, u8 srv_trans_id,
2387 const u8 *query, size_t query_len)
2393 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2395 if (!wpa_s->global->wifi_display) {
2396 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2397 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2402 if (query_len < 1) {
2403 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2408 if (wpabuf_tailroom(resp) < 5)
2413 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2415 /* TODO: role specific handling */
2417 /* Length (to be filled) */
2418 len_pos = wpabuf_put(resp, 2);
2419 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2420 wpabuf_put_u8(resp, srv_trans_id);
2421 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2423 while (pos < query + query_len) {
2424 if (*pos < MAX_WFD_SUBELEMS &&
2425 wpa_s->global->wfd_subelem[*pos] &&
2426 wpabuf_tailroom(resp) >=
2427 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2428 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2429 "subelement %u", *pos);
2430 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2435 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2437 #endif /* CONFIG_WIFI_DISPLAY */
2440 static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2441 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2443 struct wpa_supplicant *wpa_s = ctx;
2444 const u8 *pos = tlvs;
2445 const u8 *end = tlvs + tlvs_len;
2448 struct wpabuf *resp;
2449 u8 srv_proto, srv_trans_id;
2453 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2455 buf_len = 2 * tlvs_len + 1;
2456 buf = os_malloc(buf_len);
2458 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2459 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2461 freq, MAC2STR(sa), dialog_token, update_indic,
2466 if (wpa_s->p2p_sd_over_ctrl_iface) {
2467 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2468 update_indic, tlvs, tlvs_len);
2469 return; /* to be processed by an external program */
2472 resp = wpabuf_alloc(10000);
2476 while (pos + 1 < end) {
2477 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2478 slen = WPA_GET_LE16(pos);
2480 if (pos + slen > end || slen < 2) {
2481 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2486 tlv_end = pos + slen;
2489 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2491 srv_trans_id = *pos++;
2492 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2495 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2496 pos, tlv_end - pos);
2499 if (wpa_s->force_long_sd) {
2500 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2502 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2503 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2507 switch (srv_proto) {
2508 case P2P_SERV_ALL_SERVICES:
2509 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2510 "for all services");
2511 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2512 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2513 wpa_printf(MSG_DEBUG, "P2P: No service "
2514 "discovery protocols available");
2515 wpas_sd_add_proto_not_avail(
2516 resp, P2P_SERV_ALL_SERVICES,
2520 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2521 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2523 case P2P_SERV_BONJOUR:
2524 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2525 pos, tlv_end - pos);
2528 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2529 pos, tlv_end - pos);
2531 #ifdef CONFIG_WIFI_DISPLAY
2532 case P2P_SERV_WIFI_DISPLAY:
2533 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2534 pos, tlv_end - pos);
2536 #endif /* CONFIG_WIFI_DISPLAY */
2538 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2539 "protocol %u", srv_proto);
2540 wpas_sd_add_proto_not_avail(resp, srv_proto,
2549 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2550 update_indic, tlvs, tlvs_len);
2552 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2558 static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2559 const u8 *tlvs, size_t tlvs_len)
2561 struct wpa_supplicant *wpa_s = ctx;
2562 const u8 *pos = tlvs;
2563 const u8 *end = tlvs + tlvs_len;
2569 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2571 if (tlvs_len > 1500) {
2572 /* TODO: better way for handling this */
2573 wpa_msg_ctrl(wpa_s, MSG_INFO,
2574 P2P_EVENT_SERV_DISC_RESP MACSTR
2575 " %u <long response: %u bytes>",
2576 MAC2STR(sa), update_indic,
2577 (unsigned int) tlvs_len);
2579 buf_len = 2 * tlvs_len + 1;
2580 buf = os_malloc(buf_len);
2582 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2583 wpa_msg_ctrl(wpa_s, MSG_INFO,
2584 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2585 MAC2STR(sa), update_indic, buf);
2591 u8 srv_proto, srv_trans_id, status;
2593 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2594 slen = WPA_GET_LE16(pos);
2596 if (pos + slen > end || slen < 3) {
2597 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2601 tlv_end = pos + slen;
2604 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2606 srv_trans_id = *pos++;
2607 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2610 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2613 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2614 pos, tlv_end - pos);
2619 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2623 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2624 const struct wpabuf *tlvs)
2626 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2628 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2632 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2633 u8 version, const char *query)
2635 struct wpabuf *tlvs;
2638 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2641 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2642 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2643 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2644 wpabuf_put_u8(tlvs, version);
2645 wpabuf_put_str(tlvs, query);
2646 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2652 #ifdef CONFIG_WIFI_DISPLAY
2654 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2655 const struct wpabuf *tlvs)
2657 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2659 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2663 #define MAX_WFD_SD_SUBELEMS 20
2665 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2666 const char *subelems)
2673 len = wpabuf_put(tlvs, 2);
2674 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2675 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2677 wpabuf_put_u8(tlvs, role);
2682 if (val >= 0 && val < 256) {
2683 wpabuf_put_u8(tlvs, val);
2685 if (count == MAX_WFD_SD_SUBELEMS)
2688 pos = os_strchr(pos + 1, ',');
2694 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2698 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2699 const u8 *dst, const char *role)
2701 struct wpabuf *tlvs;
2703 const char *subelems;
2706 subelems = os_strchr(role, ' ');
2707 if (subelems == NULL)
2711 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2715 if (os_strstr(role, "[source]"))
2716 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2717 if (os_strstr(role, "[pri-sink]"))
2718 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2719 if (os_strstr(role, "[sec-sink]"))
2720 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2721 if (os_strstr(role, "[source+sink]"))
2722 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2724 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2729 #endif /* CONFIG_WIFI_DISPLAY */
2732 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2734 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2736 return p2p_sd_cancel_request(wpa_s->global->p2p,
2737 (void *) (uintptr_t) req);
2741 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2742 const u8 *dst, u8 dialog_token,
2743 const struct wpabuf *resp_tlvs)
2745 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2747 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2752 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2754 if (wpa_s->global->p2p)
2755 p2p_sd_service_update(wpa_s->global->p2p);
2759 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2761 dl_list_del(&bsrv->list);
2762 wpabuf_free(bsrv->query);
2763 wpabuf_free(bsrv->resp);
2768 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2770 dl_list_del(&usrv->list);
2771 os_free(usrv->service);
2776 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2778 struct p2p_srv_bonjour *bsrv, *bn;
2779 struct p2p_srv_upnp *usrv, *un;
2781 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2782 struct p2p_srv_bonjour, list)
2783 wpas_p2p_srv_bonjour_free(bsrv);
2785 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2786 struct p2p_srv_upnp, list)
2787 wpas_p2p_srv_upnp_free(usrv);
2789 wpas_p2p_sd_service_update(wpa_s);
2793 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2794 struct wpabuf *query, struct wpabuf *resp)
2796 struct p2p_srv_bonjour *bsrv;
2798 bsrv = os_zalloc(sizeof(*bsrv));
2801 bsrv->query = query;
2803 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2805 wpas_p2p_sd_service_update(wpa_s);
2810 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2811 const struct wpabuf *query)
2813 struct p2p_srv_bonjour *bsrv;
2815 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2818 wpas_p2p_srv_bonjour_free(bsrv);
2819 wpas_p2p_sd_service_update(wpa_s);
2824 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2825 const char *service)
2827 struct p2p_srv_upnp *usrv;
2829 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2830 return 0; /* Already listed */
2831 usrv = os_zalloc(sizeof(*usrv));
2834 usrv->version = version;
2835 usrv->service = os_strdup(service);
2836 if (usrv->service == NULL) {
2840 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2842 wpas_p2p_sd_service_update(wpa_s);
2847 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2848 const char *service)
2850 struct p2p_srv_upnp *usrv;
2852 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2855 wpas_p2p_srv_upnp_free(usrv);
2856 wpas_p2p_sd_service_update(wpa_s);
2861 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2862 const u8 *peer, const char *params,
2863 unsigned int generated_pin)
2865 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2866 " %08d%s", MAC2STR(peer), generated_pin, params);
2870 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2871 const u8 *peer, const char *params)
2873 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2874 "%s", MAC2STR(peer), params);
2878 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2879 const u8 *dev_addr, const u8 *pri_dev_type,
2880 const char *dev_name, u16 supp_config_methods,
2881 u8 dev_capab, u8 group_capab, const u8 *group_id,
2882 size_t group_id_len)
2884 struct wpa_supplicant *wpa_s = ctx;
2885 char devtype[WPS_DEV_TYPE_BUFSIZE];
2887 u8 empty_dev_type[8];
2888 unsigned int generated_pin = 0;
2889 struct wpa_supplicant *group = NULL;
2892 for (group = wpa_s->global->ifaces; group; group = group->next)
2894 struct wpa_ssid *s = group->current_ssid;
2896 s->mode == WPAS_MODE_P2P_GO &&
2897 group_id_len - ETH_ALEN == s->ssid_len &&
2898 os_memcmp(group_id + ETH_ALEN, s->ssid,
2904 if (pri_dev_type == NULL) {
2905 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2906 pri_dev_type = empty_dev_type;
2908 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2909 " pri_dev_type=%s name='%s' config_methods=0x%x "
2910 "dev_capab=0x%x group_capab=0x%x%s%s",
2912 wps_dev_type_bin2str(pri_dev_type, devtype,
2914 dev_name, supp_config_methods, dev_capab, group_capab,
2915 group ? " group=" : "",
2916 group ? group->ifname : "");
2917 params[sizeof(params) - 1] = '\0';
2919 if (config_methods & WPS_CONFIG_DISPLAY) {
2920 generated_pin = wps_generate_pin();
2921 wpas_prov_disc_local_display(wpa_s, peer, params,
2923 } else if (config_methods & WPS_CONFIG_KEYPAD)
2924 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2925 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2926 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2927 MACSTR "%s", MAC2STR(peer), params);
2929 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2930 P2P_PROV_DISC_SUCCESS,
2931 config_methods, generated_pin);
2935 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2937 struct wpa_supplicant *wpa_s = ctx;
2938 unsigned int generated_pin = 0;
2941 if (wpa_s->pending_pd_before_join &&
2942 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2943 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2944 wpa_s->pending_pd_before_join = 0;
2945 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2946 "join-existing-group operation");
2947 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2951 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2952 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2953 os_snprintf(params, sizeof(params), " peer_go=%d",
2954 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2958 if (config_methods & WPS_CONFIG_DISPLAY)
2959 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2960 else if (config_methods & WPS_CONFIG_KEYPAD) {
2961 generated_pin = wps_generate_pin();
2962 wpas_prov_disc_local_display(wpa_s, peer, params,
2964 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2965 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2966 MACSTR "%s", MAC2STR(peer), params);
2968 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2969 P2P_PROV_DISC_SUCCESS,
2970 config_methods, generated_pin);
2974 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2975 enum p2p_prov_disc_status status)
2977 struct wpa_supplicant *wpa_s = ctx;
2979 if (wpa_s->p2p_fallback_to_go_neg) {
2980 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2981 "failed - fall back to GO Negotiation");
2982 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2986 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2987 wpa_s->pending_pd_before_join = 0;
2988 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2989 "join-existing-group operation (no ACK for PD "
2991 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2995 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2996 " p2p_dev_addr=" MACSTR " status=%d",
2997 MAC2STR(peer), status);
2999 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
3004 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
3006 if (channels == NULL)
3007 return 1; /* Assume no restrictions */
3008 return p2p_channels_includes_freq(channels, freq);
3014 * Pick the best frequency to use from all the currently used frequencies.
3016 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
3017 struct wpa_used_freq_data *freqs,
3022 /* find a candidate freq that is supported by P2P */
3023 for (c = 0; c < num; c++)
3024 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
3030 /* once we have a candidate, try to find a 'better' one */
3031 for (i = c + 1; i < num; i++) {
3032 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
3036 * 1. Infrastructure station interfaces have higher preference.
3037 * 2. P2P Clients have higher preference.
3040 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
3045 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
3048 return freqs[c].freq;
3052 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
3053 const u8 *go_dev_addr, const u8 *ssid,
3054 size_t ssid_len, int *go, u8 *group_bssid,
3055 int *force_freq, int persistent_group,
3056 const struct p2p_channels *channels,
3059 struct wpa_supplicant *wpa_s = ctx;
3061 struct wpa_used_freq_data *freqs;
3062 struct wpa_supplicant *grp;
3065 if (!persistent_group) {
3066 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3067 " to join an active group (SSID: %s)",
3068 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
3069 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3070 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
3072 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
3073 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
3074 "authorized invitation");
3078 #ifdef CONFIG_WPS_NFC
3079 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
3080 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
3081 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
3082 wpa_s->parent->p2p_wps_method = WPS_NFC;
3083 wpa_s->parent->pending_join_wps_method = WPS_NFC;
3084 os_memcpy(wpa_s->parent->pending_join_dev_addr,
3085 go_dev_addr, ETH_ALEN);
3086 os_memcpy(wpa_s->parent->pending_join_iface_addr,
3090 #endif /* CONFIG_WPS_NFC */
3093 * Do not accept the invitation automatically; notify user and
3096 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3099 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3101 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3102 "running persistent group");
3104 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3108 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3109 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3110 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3111 "invitation to re-invoke a persistent group");
3112 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3113 } else if (!wpa_s->conf->persistent_reconnect)
3114 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3116 for (s = wpa_s->conf->ssid; s; s = s->next) {
3117 if (s->disabled == 2 &&
3118 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3119 s->ssid_len == ssid_len &&
3120 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3125 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3126 " requested reinvocation of an unknown group",
3128 return P2P_SC_FAIL_UNKNOWN_GROUP;
3131 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3133 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3134 wpa_printf(MSG_DEBUG, "P2P: The only available "
3135 "interface is already in use - reject "
3137 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3139 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3140 } else if (s->mode == WPAS_MODE_P2P_GO) {
3142 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3144 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3145 "interface address for the group");
3146 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3148 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3153 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3156 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3157 sizeof(struct wpa_used_freq_data));
3159 int num_channels = wpa_s->num_multichan_concurrent;
3160 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3161 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3165 /* Get one of the frequencies currently in use */
3166 if (best_freq > 0) {
3167 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3168 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
3170 if (wpa_s->num_multichan_concurrent < 2 ||
3171 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3172 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3173 *force_freq = best_freq;
3177 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3178 wpas_p2p_num_unused_channels(wpa_s) > 0) {
3180 /* We are the client */
3181 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3182 "running a GO but we are capable of MCC, "
3183 "figure out the best channel to use");
3185 } else if (!freq_included(channels, *force_freq)) {
3186 /* We are the GO, and *force_freq is not in the
3188 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3189 "in intersection but we are capable of MCC, "
3190 "figure out the best channel to use",
3196 return P2P_SC_SUCCESS;
3200 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3201 const u8 *ssid, size_t ssid_len,
3202 const u8 *go_dev_addr, u8 status,
3205 struct wpa_supplicant *wpa_s = ctx;
3208 for (s = wpa_s->conf->ssid; s; s = s->next) {
3209 if (s->disabled == 2 &&
3210 s->ssid_len == ssid_len &&
3211 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3215 if (status == P2P_SC_SUCCESS) {
3216 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3217 " was accepted; op_freq=%d MHz, SSID=%s",
3218 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3220 int go = s->mode == WPAS_MODE_P2P_GO;
3221 wpas_p2p_group_add_persistent(
3222 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
3223 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3225 wpa_s->user_initiated_pd = 0;
3226 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
3227 wpa_s->p2p_wps_method, 0, op_freq,
3233 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3234 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3235 " was rejected (status %u)", MAC2STR(sa), status);
3241 wpa_msg_global(wpa_s, MSG_INFO,
3242 P2P_EVENT_INVITATION_RECEIVED
3243 "sa=" MACSTR " go_dev_addr=" MACSTR
3244 " bssid=" MACSTR " unknown-network",
3245 MAC2STR(sa), MAC2STR(go_dev_addr),
3248 wpa_msg_global(wpa_s, MSG_INFO,
3249 P2P_EVENT_INVITATION_RECEIVED
3250 "sa=" MACSTR " go_dev_addr=" MACSTR
3252 MAC2STR(sa), MAC2STR(go_dev_addr));
3257 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
3258 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3259 "sa=" MACSTR " persistent=%d freq=%d",
3260 MAC2STR(sa), s->id, op_freq);
3262 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3263 "sa=" MACSTR " persistent=%d",
3264 MAC2STR(sa), s->id);
3269 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3270 struct wpa_ssid *ssid,
3271 const u8 *peer, int inv)
3278 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3279 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3283 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
3284 if (ssid->mode != WPAS_MODE_P2P_GO &&
3285 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3286 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3287 "due to invitation result", ssid->id);
3288 wpas_notify_network_removed(wpa_s, ssid);
3289 wpa_config_remove_network(wpa_s->conf, ssid->id);
3292 return; /* Peer not found in client list */
3295 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
3296 "group %d client list%s",
3297 MAC2STR(peer), ssid->id,
3298 inv ? " due to invitation result" : "");
3299 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3300 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3301 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3302 ssid->num_p2p_clients--;
3303 if (wpa_s->parent->conf->update_config &&
3304 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3305 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3309 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3312 struct wpa_ssid *ssid;
3314 wpa_s = wpa_s->global->p2p_invite_group;
3316 return; /* No known invitation group */
3317 ssid = wpa_s->current_ssid;
3318 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3319 !ssid->p2p_persistent_group)
3320 return; /* Not operating as a GO in persistent group */
3321 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3322 ssid->ssid, ssid->ssid_len);
3323 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3327 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
3328 const struct p2p_channels *channels,
3329 const u8 *peer, int neg_freq,
3332 struct wpa_supplicant *wpa_s = ctx;
3333 struct wpa_ssid *ssid;
3337 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3338 "status=%d " MACSTR,
3339 status, MAC2STR(bssid));
3341 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3342 "status=%d ", status);
3344 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3346 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3347 status, MAC2STR(peer));
3348 if (wpa_s->pending_invite_ssid_id == -1) {
3349 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3350 wpas_remove_persistent_client(wpa_s, peer);
3351 return; /* Invitation to active group */
3354 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3355 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3356 "invitation exchange to indicate readiness for "
3360 if (status != P2P_SC_SUCCESS) {
3361 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3362 ssid = wpa_config_get_network(
3363 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3364 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3366 wpas_p2p_remove_pending_group_interface(wpa_s);
3370 ssid = wpa_config_get_network(wpa_s->conf,
3371 wpa_s->pending_invite_ssid_id);
3373 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3374 "data matching with invitation");
3379 * The peer could have missed our ctrl::ack frame for Invitation
3380 * Response and continue retransmitting the frame. To reduce the
3381 * likelihood of the peer not getting successful TX status for the
3382 * Invitation Response frame, wait a short time here before starting
3383 * the persistent group so that we will remain on the current channel to
3384 * acknowledge any possible retransmission from the peer.
3386 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3387 "starting persistent group");
3390 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3391 freq_included(channels, neg_freq))
3393 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3394 freq_included(channels, peer_oper_freq))
3395 freq = peer_oper_freq;
3399 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3400 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3401 wpas_p2p_group_add_persistent(wpa_s, ssid,
3402 ssid->mode == WPAS_MODE_P2P_GO,
3403 wpa_s->p2p_persistent_go_freq,
3405 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3407 ssid->mode == WPAS_MODE_P2P_GO ?
3408 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3413 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3416 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3418 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3422 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3424 reg->channel[reg->channels] = chan;
3429 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3430 struct p2p_channels *chan,
3431 struct p2p_channels *cli_chan)
3435 wpa_s->global->p2p_24ghz_social_channels = 1;
3437 os_memset(cli_chan, 0, sizeof(*cli_chan));
3439 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3442 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3443 chan->reg_class[cla].reg_class = 81;
3444 chan->reg_class[cla].channels = 0;
3445 for (i = 0; i < 11; i++) {
3446 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3447 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3449 if (chan->reg_class[cla].channels)
3452 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3455 /* Operating class 115 - 5 GHz, channels 36-48 */
3456 chan->reg_class[cla].reg_class = 115;
3457 chan->reg_class[cla].channels = 0;
3458 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3459 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3460 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3461 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3462 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3463 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3464 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3465 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3466 if (chan->reg_class[cla].channels)
3469 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3472 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3473 chan->reg_class[cla].reg_class = 124;
3474 chan->reg_class[cla].channels = 0;
3475 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3476 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3477 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3478 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3479 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3480 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3481 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3482 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3483 if (chan->reg_class[cla].channels)
3486 chan->reg_classes = cla;
3491 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3493 enum hostapd_hw_mode mode)
3497 for (i = 0; i < num_modes; i++) {
3498 if (modes[i].mode == mode)
3507 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3510 static int has_channel(struct wpa_global *global,
3511 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3516 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3518 if (wpas_p2p_disallowed_freq(global, freq))
3521 for (i = 0; i < mode->num_channels; i++) {
3522 if (mode->channels[i].chan == chan) {
3524 *flags = mode->channels[i].flag;
3525 if (mode->channels[i].flag &
3526 (HOSTAPD_CHAN_DISABLED |
3527 HOSTAPD_CHAN_RADAR))
3529 if (mode->channels[i].flag &
3530 (HOSTAPD_CHAN_PASSIVE_SCAN |
3531 HOSTAPD_CHAN_NO_IBSS))
3532 return PASSIVE_ONLY;
3541 struct p2p_oper_class_map {
3542 enum hostapd_hw_mode mode;
3547 enum { BW20, BW40PLUS, BW40MINUS, BW80, BW2160 } bw;
3550 static struct p2p_oper_class_map op_class[] = {
3551 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3552 #if 0 /* Do not enable HT40 on 2 GHz for now */
3553 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3554 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3556 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3557 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3558 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3559 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3560 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3561 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3564 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3565 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3566 * 80 MHz, but currently use the following definition for simplicity
3567 * (these center frequencies are not actual channels, which makes
3568 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3569 * removing invalid channels.
3571 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3572 { HOSTAPD_MODE_IEEE80211AD, 180, 1, 4, 1, BW2160 },
3573 { -1, 0, 0, 0, 0, BW20 }
3577 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3578 struct hostapd_hw_modes *mode,
3581 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3584 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3587 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3589 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3590 * so the center channel is 6 channels away from the start/end.
3592 if (channel >= center_channels[i] - 6 &&
3593 channel <= center_channels[i] + 6)
3594 return center_channels[i];
3600 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3601 struct hostapd_hw_modes *mode,
3606 enum chan_allowed res, ret = ALLOWED;
3608 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3611 if (center_chan >= 58 && center_chan <= 138)
3612 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3614 /* check all the channels are available */
3615 for (i = 0; i < 4; i++) {
3616 int adj_chan = center_chan - 6 + i * 4;
3618 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3619 if (res == NOT_ALLOWED)
3621 if (res == PASSIVE_ONLY)
3624 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3626 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3628 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3630 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3638 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3639 struct hostapd_hw_modes *mode,
3643 enum chan_allowed res, res2;
3645 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3646 if (bw == BW40MINUS) {
3647 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3649 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3650 } else if (bw == BW40PLUS) {
3651 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3653 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3654 } else if (bw == BW80) {
3655 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3658 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3660 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3661 return PASSIVE_ONLY;
3666 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3667 struct p2p_channels *chan,
3668 struct p2p_channels *cli_chan)
3670 struct hostapd_hw_modes *mode;
3671 int cla, op, cli_cla;
3673 if (wpa_s->hw.modes == NULL) {
3674 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3675 "of all supported channels; assume dualband "
3677 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3682 for (op = 0; op_class[op].op_class; op++) {
3683 struct p2p_oper_class_map *o = &op_class[op];
3685 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3687 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3690 if (mode->mode == HOSTAPD_MODE_IEEE80211G)
3691 wpa_s->global->p2p_24ghz_social_channels = 1;
3692 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3693 enum chan_allowed res;
3694 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3695 if (res == ALLOWED) {
3697 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3699 reg = &chan->reg_class[cla];
3701 reg->reg_class = o->op_class;
3703 reg->channel[reg->channels] = ch;
3705 } else if (res == PASSIVE_ONLY &&
3706 wpa_s->conf->p2p_add_cli_chan) {
3707 if (cli_reg == NULL) {
3708 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3710 cli_reg = &cli_chan->reg_class[cli_cla];
3712 cli_reg->reg_class = o->op_class;
3714 cli_reg->channel[cli_reg->channels] = ch;
3715 cli_reg->channels++;
3719 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3720 reg->channel, reg->channels);
3723 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3724 cli_reg->channel, cli_reg->channels);
3728 chan->reg_classes = cla;
3729 cli_chan->reg_classes = cli_cla;
3735 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3736 struct hostapd_hw_modes *mode, u8 channel)
3739 enum chan_allowed ret;
3741 for (op = 0; op_class[op].op_class; op++) {
3742 struct p2p_oper_class_map *o = &op_class[op];
3745 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3746 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3747 o->bw == BW20 || ch != channel)
3749 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3751 return (o->bw == BW40MINUS) ? -1 : 1;
3758 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3759 struct hostapd_hw_modes *mode, u8 channel)
3761 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3764 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3768 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3771 struct wpa_supplicant *wpa_s = ctx;
3773 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3774 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3780 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3784 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3785 const u8 *ssid, size_t ssid_len)
3787 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3788 struct wpa_ssid *s = wpa_s->current_ssid;
3791 if (s->mode != WPAS_MODE_P2P_GO &&
3792 s->mode != WPAS_MODE_AP &&
3793 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3795 if (s->ssid_len != ssid_len ||
3796 os_memcmp(ssid, s->ssid, ssid_len) != 0)
3806 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3807 const u8 *peer_dev_addr)
3809 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3810 struct wpa_ssid *ssid = wpa_s->current_ssid;
3813 if (ssid->mode != WPAS_MODE_INFRA)
3815 if (wpa_s->wpa_state != WPA_COMPLETED &&
3816 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3818 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3826 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3828 struct wpa_supplicant *wpa_s = ctx;
3830 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3834 static int wpas_is_concurrent_session_active(void *ctx)
3836 struct wpa_supplicant *wpa_s = ctx;
3837 struct wpa_supplicant *ifs;
3839 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3842 if (ifs->wpa_state > WPA_ASSOCIATED)
3849 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3851 struct wpa_supplicant *wpa_s = ctx;
3852 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3856 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s,
3857 const char *conf_p2p_dev)
3859 struct wpa_interface iface;
3860 struct wpa_supplicant *p2pdev_wpa_s;
3862 char force_name[100];
3865 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3867 force_name[0] = '\0';
3868 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3869 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3870 force_name, wpa_s->pending_interface_addr, NULL);
3872 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3875 os_strlcpy(wpa_s->pending_interface_name, ifname,
3876 sizeof(wpa_s->pending_interface_name));
3878 os_memset(&iface, 0, sizeof(iface));
3880 iface.ifname = wpa_s->pending_interface_name;
3881 iface.driver = wpa_s->driver->name;
3882 iface.driver_param = wpa_s->conf->driver_param;
3885 * If a P2P Device configuration file was given, use it as the interface
3886 * configuration file (instead of using parent's configuration file.
3889 iface.confname = conf_p2p_dev;
3890 iface.ctrl_interface = NULL;
3892 iface.confname = wpa_s->confname;
3893 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3895 iface.conf_p2p_dev = NULL;
3897 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3898 if (!p2pdev_wpa_s) {
3899 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3902 p2pdev_wpa_s->parent = wpa_s;
3904 wpa_s->pending_interface_name[0] = '\0';
3909 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3910 const u8 *noa, size_t noa_len)
3912 struct wpa_supplicant *wpa_s, *intf = ctx;
3915 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3916 if (wpa_s->waiting_presence_resp)
3920 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3923 wpa_s->waiting_presence_resp = 0;
3925 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3926 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3927 " status=%u noa=%s", MAC2STR(src), status, hex);
3931 static int _wpas_p2p_in_progress(void *ctx)
3933 struct wpa_supplicant *wpa_s = ctx;
3934 return wpas_p2p_in_progress(wpa_s);
3939 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3940 * @global: Pointer to global data from wpa_supplicant_init()
3941 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3942 * Returns: 0 on success, -1 on failure
3944 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3946 struct p2p_config p2p;
3949 if (wpa_s->conf->p2p_disabled)
3952 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3958 os_memset(&p2p, 0, sizeof(p2p));
3960 p2p.debug_print = wpas_p2p_debug_print;
3961 p2p.p2p_scan = wpas_p2p_scan;
3962 p2p.send_action = wpas_send_action;
3963 p2p.send_action_done = wpas_send_action_done;
3964 p2p.go_neg_completed = wpas_go_neg_completed;
3965 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3966 p2p.dev_found = wpas_dev_found;
3967 p2p.dev_lost = wpas_dev_lost;
3968 p2p.find_stopped = wpas_find_stopped;
3969 p2p.start_listen = wpas_start_listen;
3970 p2p.stop_listen = wpas_stop_listen;
3971 p2p.send_probe_resp = wpas_send_probe_resp;
3972 p2p.sd_request = wpas_sd_request;
3973 p2p.sd_response = wpas_sd_response;
3974 p2p.prov_disc_req = wpas_prov_disc_req;
3975 p2p.prov_disc_resp = wpas_prov_disc_resp;
3976 p2p.prov_disc_fail = wpas_prov_disc_fail;
3977 p2p.invitation_process = wpas_invitation_process;
3978 p2p.invitation_received = wpas_invitation_received;
3979 p2p.invitation_result = wpas_invitation_result;
3980 p2p.get_noa = wpas_get_noa;
3981 p2p.go_connected = wpas_go_connected;
3982 p2p.presence_resp = wpas_presence_resp;
3983 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
3984 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
3986 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
3987 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
3988 p2p.dev_name = wpa_s->conf->device_name;
3989 p2p.manufacturer = wpa_s->conf->manufacturer;
3990 p2p.model_name = wpa_s->conf->model_name;
3991 p2p.model_number = wpa_s->conf->model_number;
3992 p2p.serial_number = wpa_s->conf->serial_number;
3994 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3995 p2p.config_methods = wpa_s->wps->config_methods;
3998 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3999 wpa_printf(MSG_ERROR,
4000 "P2P: Failed to configure supported channel list");
4004 if (wpa_s->conf->p2p_listen_reg_class &&
4005 wpa_s->conf->p2p_listen_channel) {
4006 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
4007 p2p.channel = wpa_s->conf->p2p_listen_channel;
4008 p2p.channel_forced = 1;
4011 * Pick one of the social channels randomly as the listen
4014 if (p2p_config_get_random_social(&p2p, &p2p.reg_class,
4015 &p2p.channel) != 0) {
4016 wpa_printf(MSG_ERROR,
4017 "P2P: Failed to select random social channel as listen channel");
4020 p2p.channel_forced = 0;
4022 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d:%d",
4023 p2p.reg_class, p2p.channel);
4025 if (wpa_s->conf->p2p_oper_reg_class &&
4026 wpa_s->conf->p2p_oper_channel) {
4027 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
4028 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
4029 p2p.cfg_op_channel = 1;
4030 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
4031 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4035 * Use random operation channel from 2.4 GHz band social
4036 * channels (1, 6, 11) or band 60 GHz social channel (2) if no
4037 * other preference is indicated.
4039 if (p2p_config_get_random_social(&p2p, &p2p.op_reg_class,
4040 &p2p.op_channel) != 0) {
4041 wpa_printf(MSG_ERROR,
4042 "P2P: Failed to select random social channel as operation channel");
4045 p2p.cfg_op_channel = 0;
4046 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
4047 "%d:%d", p2p.op_reg_class, p2p.op_channel);
4050 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
4051 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
4052 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
4055 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
4056 os_memcpy(p2p.country, wpa_s->conf->country, 2);
4057 p2p.country[2] = 0x04;
4059 os_memcpy(p2p.country, "XX\x04", 3);
4061 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
4064 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
4065 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
4066 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
4068 p2p.concurrent_operations = !!(wpa_s->drv_flags &
4069 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
4071 p2p.max_peers = 100;
4073 if (wpa_s->conf->p2p_ssid_postfix) {
4074 p2p.ssid_postfix_len =
4075 os_strlen(wpa_s->conf->p2p_ssid_postfix);
4076 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
4077 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
4078 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
4079 p2p.ssid_postfix_len);
4082 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
4084 p2p.max_listen = wpa_s->max_remain_on_chan;
4086 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
4087 wpa_s->conf->p2p_passphrase_len <= 63)
4088 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
4090 p2p.passphrase_len = 8;
4092 global->p2p = p2p_init(&p2p);
4093 if (global->p2p == NULL)
4095 global->p2p_init_wpa_s = wpa_s;
4097 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
4098 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
4100 p2p_add_wps_vendor_extension(
4101 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4104 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4111 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4112 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4114 * This function deinitialize per-interface P2P data.
4116 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4118 if (wpa_s->driver && wpa_s->drv_priv)
4119 wpa_drv_probe_req_report(wpa_s, 0);
4121 if (wpa_s->go_params) {
4122 /* Clear any stored provisioning info */
4123 p2p_clear_provisioning_info(
4125 wpa_s->go_params->peer_device_addr);
4128 os_free(wpa_s->go_params);
4129 wpa_s->go_params = NULL;
4130 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4131 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4132 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4133 wpa_s->p2p_long_listen = 0;
4134 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4135 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4136 wpas_p2p_remove_pending_group_interface(wpa_s);
4137 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4138 wpas_p2p_listen_work_done(wpa_s);
4139 if (wpa_s->p2p_send_action_work) {
4140 os_free(wpa_s->p2p_send_action_work->ctx);
4141 radio_work_done(wpa_s->p2p_send_action_work);
4142 wpa_s->p2p_send_action_work = NULL;
4144 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4146 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4147 wpa_s->p2p_oob_dev_pw = NULL;
4149 /* TODO: remove group interface from the driver if this wpa_s instance
4150 * is on top of a P2P group interface */
4155 * wpas_p2p_deinit_global - Deinitialize global P2P module
4156 * @global: Pointer to global data from wpa_supplicant_init()
4158 * This function deinitializes the global (per device) P2P module.
4160 static void wpas_p2p_deinit_global(struct wpa_global *global)
4162 struct wpa_supplicant *wpa_s, *tmp;
4164 wpa_s = global->ifaces;
4166 wpas_p2p_service_flush(global->p2p_init_wpa_s);
4168 /* Remove remaining P2P group interfaces */
4169 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4170 wpa_s = wpa_s->next;
4172 tmp = global->ifaces;
4175 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4180 /* Disconnect from the P2P group and deinit the interface */
4181 wpas_p2p_disconnect(tmp);
4185 * Deinit GO data on any possibly remaining interface (if main
4186 * interface is used as GO).
4188 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4189 if (wpa_s->ap_iface)
4190 wpas_p2p_group_deinit(wpa_s);
4193 p2p_deinit(global->p2p);
4195 global->p2p_init_wpa_s = NULL;
4199 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4201 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4202 wpa_s->conf->p2p_no_group_iface)
4203 return 0; /* separate interface disabled per configuration */
4204 if (wpa_s->drv_flags &
4205 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4206 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4207 return 1; /* P2P group requires a new interface in every case
4209 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4210 return 0; /* driver does not support concurrent operations */
4211 if (wpa_s->global->ifaces->next)
4212 return 1; /* more that one interface already in use */
4213 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4214 return 1; /* this interface is already in use */
4219 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4220 const u8 *peer_addr,
4221 enum p2p_wps_method wps_method,
4222 int go_intent, const u8 *own_interface_addr,
4223 unsigned int force_freq, int persistent_group,
4224 struct wpa_ssid *ssid, unsigned int pref_freq)
4226 if (persistent_group && wpa_s->conf->persistent_reconnect)
4227 persistent_group = 2;
4230 * Increase GO config timeout if HT40 is used since it takes some time
4231 * to scan channels for coex purposes before the BSS can be started.
4233 p2p_set_config_timeout(wpa_s->global->p2p,
4234 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4236 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4237 go_intent, own_interface_addr, force_freq,
4238 persistent_group, ssid ? ssid->ssid : NULL,
4239 ssid ? ssid->ssid_len : 0,
4240 wpa_s->p2p_pd_before_go_neg, pref_freq,
4241 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4246 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4247 const u8 *peer_addr,
4248 enum p2p_wps_method wps_method,
4249 int go_intent, const u8 *own_interface_addr,
4250 unsigned int force_freq, int persistent_group,
4251 struct wpa_ssid *ssid, unsigned int pref_freq)
4253 if (persistent_group && wpa_s->conf->persistent_reconnect)
4254 persistent_group = 2;
4256 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4257 go_intent, own_interface_addr, force_freq,
4258 persistent_group, ssid ? ssid->ssid : NULL,
4259 ssid ? ssid->ssid_len : 0, pref_freq,
4260 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4265 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4267 wpa_s->p2p_join_scan_count++;
4268 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4269 wpa_s->p2p_join_scan_count);
4270 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4271 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4272 " for join operationg - stop join attempt",
4273 MAC2STR(wpa_s->pending_join_iface_addr));
4274 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4275 if (wpa_s->p2p_auto_pd) {
4276 wpa_s->p2p_auto_pd = 0;
4277 wpa_msg_global(wpa_s, MSG_INFO,
4278 P2P_EVENT_PROV_DISC_FAILURE
4279 " p2p_dev_addr=" MACSTR " status=N/A",
4280 MAC2STR(wpa_s->pending_join_dev_addr));
4283 wpa_msg_global(wpa_s->parent, MSG_INFO,
4284 P2P_EVENT_GROUP_FORMATION_FAILURE);
4289 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4292 unsigned int num, i;
4293 struct wpa_used_freq_data *freqs;
4295 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4296 /* Multiple channels are supported and not all are in use */
4300 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4301 sizeof(struct wpa_used_freq_data));
4305 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4306 wpa_s->num_multichan_concurrent);
4308 for (i = 0; i < num; i++) {
4309 if (freqs[i].freq == freq) {
4310 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4317 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4326 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4327 const u8 *peer_dev_addr)
4329 struct wpa_bss *bss;
4332 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4335 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4336 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4341 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4343 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4344 "%ld.%06ld (%supdated in last scan)",
4345 bss->last_update.sec, bss->last_update.usec,
4346 updated ? "": "not ");
4352 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4353 struct wpa_scan_results *scan_res)
4355 struct wpa_bss *bss = NULL;
4357 u8 iface_addr[ETH_ALEN];
4359 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4361 if (wpa_s->global->p2p_disabled)
4364 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4365 scan_res ? (int) scan_res->num : -1,
4366 wpa_s->p2p_auto_join ? "auto_" : "");
4369 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4371 if (wpa_s->p2p_auto_pd) {
4372 int join = wpas_p2p_peer_go(wpa_s,
4373 wpa_s->pending_join_dev_addr);
4375 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4376 wpa_s->auto_pd_scan_retry++;
4377 bss = wpa_bss_get_bssid_latest(
4378 wpa_s, wpa_s->pending_join_dev_addr);
4381 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4382 "the peer " MACSTR " at %d MHz",
4383 wpa_s->auto_pd_scan_retry,
4385 pending_join_dev_addr),
4387 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4395 wpa_s->p2p_auto_pd = 0;
4396 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4397 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4398 MAC2STR(wpa_s->pending_join_dev_addr), join);
4399 if (p2p_prov_disc_req(wpa_s->global->p2p,
4400 wpa_s->pending_join_dev_addr,
4401 wpa_s->pending_pd_config_methods, join,
4402 0, wpa_s->user_initiated_pd) < 0) {
4403 wpa_s->p2p_auto_pd = 0;
4404 wpa_msg_global(wpa_s, MSG_INFO,
4405 P2P_EVENT_PROV_DISC_FAILURE
4406 " p2p_dev_addr=" MACSTR " status=N/A",
4407 MAC2STR(wpa_s->pending_join_dev_addr));
4412 if (wpa_s->p2p_auto_join) {
4413 int join = wpas_p2p_peer_go(wpa_s,
4414 wpa_s->pending_join_dev_addr);
4416 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4417 "running a GO -> use GO Negotiation");
4418 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4419 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4420 wpa_s->p2p_persistent_group, 0, 0, 0,
4421 wpa_s->p2p_go_intent,
4422 wpa_s->p2p_connect_freq,
4423 wpa_s->p2p_persistent_id,
4424 wpa_s->p2p_pd_before_go_neg,
4430 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4431 "try to join the group", join ? "" :
4434 wpa_s->p2p_fallback_to_go_neg = 1;
4437 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4438 wpa_s->pending_join_iface_addr);
4440 p2p_get_interface_addr(wpa_s->global->p2p,
4441 wpa_s->pending_join_dev_addr,
4443 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0
4444 && !wpa_bss_get_bssid(wpa_s, wpa_s->pending_join_iface_addr)) {
4445 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4446 "address for join from " MACSTR " to " MACSTR
4447 " based on newly discovered P2P peer entry",
4448 MAC2STR(wpa_s->pending_join_iface_addr),
4449 MAC2STR(iface_addr));
4450 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4453 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4454 wpa_s->pending_join_iface_addr);
4457 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4458 "from P2P peer table: %d MHz", freq);
4460 if (wpa_s->p2p_join_ssid_len) {
4461 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4462 MACSTR " and SSID %s",
4463 MAC2STR(wpa_s->pending_join_iface_addr),
4464 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4465 wpa_s->p2p_join_ssid_len));
4466 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4467 wpa_s->p2p_join_ssid,
4468 wpa_s->p2p_join_ssid_len);
4471 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4472 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4473 bss = wpa_bss_get_bssid_latest(wpa_s,
4474 wpa_s->pending_join_iface_addr);
4478 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4479 "from BSS table: %d MHz (SSID %s)", freq,
4480 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4485 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4486 wpa_msg_global(wpa_s->parent, MSG_INFO,
4487 P2P_EVENT_GROUP_FORMATION_FAILURE
4488 "reason=FREQ_CONFLICT");
4492 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4493 "prior to joining an existing group (GO " MACSTR
4495 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4496 wpa_s->pending_pd_before_join = 1;
4498 switch (wpa_s->pending_join_wps_method) {
4499 case WPS_PIN_DISPLAY:
4500 method = WPS_CONFIG_KEYPAD;
4502 case WPS_PIN_KEYPAD:
4503 method = WPS_CONFIG_DISPLAY;
4506 method = WPS_CONFIG_PUSHBUTTON;
4513 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4514 wpa_s->pending_join_dev_addr) ==
4517 * We have already performed provision discovery for
4518 * joining the group. Proceed directly to join
4519 * operation without duplicated provision discovery. */
4520 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4521 "with " MACSTR " already done - proceed to "
4523 MAC2STR(wpa_s->pending_join_dev_addr));
4524 wpa_s->pending_pd_before_join = 0;
4528 if (p2p_prov_disc_req(wpa_s->global->p2p,
4529 wpa_s->pending_join_dev_addr, method, 1,
4530 freq, wpa_s->user_initiated_pd) < 0) {
4531 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4532 "Discovery Request before joining an "
4534 wpa_s->pending_pd_before_join = 0;
4540 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4541 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4542 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4543 wpas_p2p_check_join_scan_limit(wpa_s);
4547 /* Start join operation immediately */
4548 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4552 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4553 const u8 *ssid, size_t ssid_len)
4556 struct wpa_driver_scan_params params;
4557 struct wpabuf *wps_ie, *ies;
4559 int freqs[2] = { 0, 0 };
4561 os_memset(¶ms, 0, sizeof(params));
4563 /* P2P Wildcard SSID */
4564 params.num_ssids = 1;
4565 if (ssid && ssid_len) {
4566 params.ssids[0].ssid = ssid;
4567 params.ssids[0].ssid_len = ssid_len;
4568 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4569 wpa_s->p2p_join_ssid_len = ssid_len;
4571 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4572 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4573 wpa_s->p2p_join_ssid_len = 0;
4576 wpa_s->wps->dev.p2p = 1;
4577 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4578 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4580 if (wps_ie == NULL) {
4581 wpas_p2p_scan_res_join(wpa_s, NULL);
4585 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4586 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4588 wpabuf_free(wps_ie);
4589 wpas_p2p_scan_res_join(wpa_s, NULL);
4592 wpabuf_put_buf(ies, wps_ie);
4593 wpabuf_free(wps_ie);
4595 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4597 params.p2p_probe = 1;
4598 params.extra_ies = wpabuf_head(ies);
4599 params.extra_ies_len = wpabuf_len(ies);
4604 * If freq is not provided, check the operating freq of the GO
4605 * and use a single channel scan on if possible.
4607 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4608 wpa_s->pending_join_iface_addr);
4614 params.freqs = freqs;
4618 * Run a scan to update BSS table and start Provision Discovery once
4619 * the new scan results become available.
4621 ret = wpa_drv_scan(wpa_s, ¶ms);
4623 os_get_reltime(&wpa_s->scan_trigger_time);
4624 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4625 wpa_s->own_scan_requested = 1;
4631 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4633 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4634 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4635 wpas_p2p_check_join_scan_limit(wpa_s);
4640 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4642 struct wpa_supplicant *wpa_s = eloop_ctx;
4643 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4647 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4648 const u8 *dev_addr, enum p2p_wps_method wps_method,
4649 int auto_join, int op_freq,
4650 const u8 *ssid, size_t ssid_len)
4652 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4653 MACSTR " dev " MACSTR " op_freq=%d)%s",
4654 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4655 auto_join ? " (auto_join)" : "");
4656 if (ssid && ssid_len) {
4657 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4658 wpa_ssid_txt(ssid, ssid_len));
4661 wpa_s->p2p_auto_pd = 0;
4662 wpa_s->p2p_auto_join = !!auto_join;
4663 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4664 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4665 wpa_s->pending_join_wps_method = wps_method;
4667 /* Make sure we are not running find during connection establishment */
4668 wpas_p2p_stop_find(wpa_s);
4670 wpa_s->p2p_join_scan_count = 0;
4671 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4676 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4677 const u8 *ssid, size_t ssid_len)
4679 struct wpa_supplicant *group;
4680 struct p2p_go_neg_results res;
4681 struct wpa_bss *bss;
4683 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4686 if (group != wpa_s) {
4687 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4688 sizeof(group->p2p_pin));
4689 group->p2p_wps_method = wpa_s->p2p_wps_method;
4692 * Need to mark the current interface for p2p_group_formation
4693 * when a separate group interface is not used. This is needed
4694 * to allow p2p_cancel stop a pending p2p_connect-join.
4695 * wpas_p2p_init_group_interface() addresses this for the case
4696 * where a separate group interface is used.
4698 wpa_s->global->p2p_group_formation = wpa_s;
4701 group->p2p_in_provisioning = 1;
4702 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4704 os_memset(&res, 0, sizeof(res));
4705 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4706 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4708 res.wps_method = wpa_s->pending_join_wps_method;
4709 if (freq && ssid && ssid_len) {
4711 res.ssid_len = ssid_len;
4712 os_memcpy(res.ssid, ssid, ssid_len);
4714 bss = wpa_bss_get_bssid_latest(wpa_s,
4715 wpa_s->pending_join_iface_addr);
4717 res.freq = bss->freq;
4718 res.ssid_len = bss->ssid_len;
4719 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4720 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4722 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4726 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4727 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4729 wpa_drv_cancel_remain_on_channel(wpa_s);
4730 wpa_s->off_channel_freq = 0;
4731 wpa_s->roc_waiting_drv_freq = 0;
4733 wpas_start_wps_enrollee(group, &res);
4736 * Allow a longer timeout for join-a-running-group than normal 15
4737 * second group formation timeout since the GO may not have authorized
4738 * our connection yet.
4740 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4741 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4748 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4749 int *force_freq, int *pref_freq, int go)
4751 struct wpa_used_freq_data *freqs;
4752 int res, best_freq, num_unused;
4753 unsigned int freq_in_use = 0, num, i;
4755 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4756 sizeof(struct wpa_used_freq_data));
4760 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4761 wpa_s->num_multichan_concurrent);
4764 * It is possible that the total number of used frequencies is bigger
4765 * than the number of frequencies used for P2P, so get the system wide
4766 * number of unused frequencies.
4768 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4770 wpa_printf(MSG_DEBUG,
4771 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4772 freq, wpa_s->num_multichan_concurrent, num, num_unused);
4777 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4779 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4781 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4782 "(%u MHz) is not supported for P2P uses",
4788 for (i = 0; i < num; i++) {
4789 if (freqs[i].freq == freq)
4793 if (num_unused <= 0 && !freq_in_use) {
4794 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4799 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4800 "requested channel (%u MHz)", freq);
4805 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
4807 /* We have a candidate frequency to use */
4808 if (best_freq > 0) {
4809 if (*pref_freq == 0 && num_unused > 0) {
4810 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4812 *pref_freq = best_freq;
4814 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4816 *force_freq = best_freq;
4818 } else if (num_unused > 0) {
4819 wpa_printf(MSG_DEBUG,
4820 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4823 wpa_printf(MSG_DEBUG,
4824 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4838 * wpas_p2p_connect - Request P2P Group Formation to be started
4839 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4840 * @peer_addr: Address of the peer P2P Device
4841 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4842 * @persistent_group: Whether to create a persistent group
4843 * @auto_join: Whether to select join vs. GO Negotiation automatically
4844 * @join: Whether to join an existing group (as a client) instead of starting
4845 * Group Owner negotiation; @peer_addr is BSSID in that case
4846 * @auth: Whether to only authorize the connection instead of doing that and
4847 * initiating Group Owner negotiation
4848 * @go_intent: GO Intent or -1 to use default
4849 * @freq: Frequency for the group or 0 for auto-selection
4850 * @persistent_id: Persistent group credentials to use for forcing GO
4851 * parameters or -1 to generate new values (SSID/passphrase)
4852 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4853 * interoperability workaround when initiating group formation
4854 * @ht40: Start GO with 40 MHz channel width
4855 * @vht: Start GO with VHT support
4856 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4857 * failure, -2 on failure due to channel not currently available,
4858 * -3 if forced channel is not supported
4860 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4861 const char *pin, enum p2p_wps_method wps_method,
4862 int persistent_group, int auto_join, int join, int auth,
4863 int go_intent, int freq, int persistent_id, int pd,
4866 int force_freq = 0, pref_freq = 0;
4868 enum wpa_driver_if_type iftype;
4870 struct wpa_ssid *ssid = NULL;
4872 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4875 if (persistent_id >= 0) {
4876 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4877 if (ssid == NULL || ssid->disabled != 2 ||
4878 ssid->mode != WPAS_MODE_P2P_GO)
4882 os_free(wpa_s->global->add_psk);
4883 wpa_s->global->add_psk = NULL;
4885 wpa_s->global->p2p_fail_on_wps_complete = 0;
4888 go_intent = wpa_s->conf->p2p_go_intent;
4891 wpa_s->p2p_long_listen = 0;
4893 wpa_s->p2p_wps_method = wps_method;
4894 wpa_s->p2p_persistent_group = !!persistent_group;
4895 wpa_s->p2p_persistent_id = persistent_id;
4896 wpa_s->p2p_go_intent = go_intent;
4897 wpa_s->p2p_connect_freq = freq;
4898 wpa_s->p2p_fallback_to_go_neg = 0;
4899 wpa_s->p2p_pd_before_go_neg = !!pd;
4900 wpa_s->p2p_go_ht40 = !!ht40;
4901 wpa_s->p2p_go_vht = !!vht;
4904 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4905 else if (wps_method == WPS_PIN_DISPLAY) {
4906 ret = wps_generate_pin();
4907 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4909 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4912 wpa_s->p2p_pin[0] = '\0';
4914 if (join || auto_join) {
4915 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4917 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4918 "connect a running group from " MACSTR,
4919 MAC2STR(peer_addr));
4920 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4923 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4924 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4926 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4927 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4931 os_get_reltime(&wpa_s->p2p_auto_started);
4932 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4934 wpa_s->p2p_auto_started.sec,
4935 wpa_s->p2p_auto_started.usec);
4937 wpa_s->user_initiated_pd = 1;
4938 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4939 auto_join, freq, NULL, 0) < 0)
4944 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4948 wpas_p2p_set_own_freq_preference(wpa_s,
4949 force_freq ? force_freq : pref_freq);
4951 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4953 if (wpa_s->create_p2p_iface) {
4954 /* Prepare to add a new interface for the group */
4955 iftype = WPA_IF_P2P_GROUP;
4956 if (go_intent == 15)
4957 iftype = WPA_IF_P2P_GO;
4958 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4959 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4960 "interface for the group");
4964 if_addr = wpa_s->pending_interface_addr;
4966 if_addr = wpa_s->own_addr;
4969 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
4971 force_freq, persistent_group, ssid,
4977 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4978 go_intent, if_addr, force_freq,
4979 persistent_group, ssid, pref_freq) < 0) {
4980 if (wpa_s->create_p2p_iface)
4981 wpas_p2p_remove_pending_group_interface(wpa_s);
4989 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4990 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4991 * @freq: Frequency of the channel in MHz
4992 * @duration: Duration of the stay on the channel in milliseconds
4994 * This callback is called when the driver indicates that it has started the
4995 * requested remain-on-channel duration.
4997 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4998 unsigned int freq, unsigned int duration)
5000 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5002 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
5003 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
5004 wpa_s->pending_listen_duration);
5005 wpa_s->pending_listen_freq = 0;
5007 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
5008 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
5014 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
5015 unsigned int timeout)
5017 /* Limit maximum Listen state time based on driver limitation. */
5018 if (timeout > wpa_s->max_remain_on_chan)
5019 timeout = wpa_s->max_remain_on_chan;
5021 return p2p_listen(wpa_s->global->p2p, timeout);
5026 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
5027 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5028 * @freq: Frequency of the channel in MHz
5030 * This callback is called when the driver indicates that a remain-on-channel
5031 * operation has been completed, i.e., the duration on the requested channel
5034 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
5037 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
5038 "(p2p_long_listen=%d ms pending_action_tx=%p)",
5039 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
5040 wpas_p2p_listen_work_done(wpa_s);
5041 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5043 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
5044 return; /* P2P module started a new operation */
5045 if (offchannel_pending_action_tx(wpa_s))
5047 if (wpa_s->p2p_long_listen > 0)
5048 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
5049 if (wpa_s->p2p_long_listen > 0) {
5050 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
5051 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
5054 * When listen duration is over, stop listen & update p2p_state
5057 p2p_stop_listen(wpa_s->global->p2p);
5063 * wpas_p2p_group_remove - Remove a P2P group
5064 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5065 * @ifname: Network interface name of the group interface or "*" to remove all
5067 * Returns: 0 on success, -1 on failure
5069 * This function is used to remove a P2P group. This can be used to disconnect
5070 * from a group in which the local end is a P2P Client or to end a P2P Group in
5071 * case the local end is the Group Owner. If a virtual network interface was
5072 * created for this group, that interface will be removed. Otherwise, only the
5073 * configured P2P group network will be removed from the interface.
5075 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
5077 struct wpa_global *global = wpa_s->global;
5078 struct wpa_supplicant *calling_wpa_s = wpa_s;
5080 if (os_strcmp(ifname, "*") == 0) {
5081 struct wpa_supplicant *prev;
5082 wpa_s = global->ifaces;
5085 wpa_s = wpa_s->next;
5086 if (prev->p2p_group_interface !=
5087 NOT_P2P_GROUP_INTERFACE ||
5088 (prev->current_ssid &&
5089 prev->current_ssid->p2p_group))
5090 wpas_p2p_disconnect_safely(prev, calling_wpa_s);
5095 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5096 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5100 return wpas_p2p_disconnect_safely(wpa_s, calling_wpa_s);
5104 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5109 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5111 if (wpa_s->best_24_freq > 0 &&
5112 p2p_supported_freq_go(wpa_s->global->p2p,
5113 wpa_s->best_24_freq)) {
5114 freq = wpa_s->best_24_freq;
5115 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5116 "channel: %d MHz", freq);
5118 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5120 freq = 2412 + (r % 3) * 25;
5121 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5122 "channel: %d MHz", freq);
5127 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5129 if (wpa_s->best_5_freq > 0 &&
5130 p2p_supported_freq_go(wpa_s->global->p2p,
5131 wpa_s->best_5_freq)) {
5132 freq = wpa_s->best_5_freq;
5133 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5134 "channel: %d MHz", freq);
5136 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5138 freq = 5180 + (r % 4) * 20;
5139 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5140 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5141 "5 GHz channel for P2P group");
5144 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5145 "channel: %d MHz", freq);
5149 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5150 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5151 "(%u MHz) is not supported for P2P uses",
5160 static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5161 struct p2p_go_neg_results *params,
5162 const struct p2p_channels *channels)
5166 /* first try some random selection of the social channels */
5167 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
5170 for (i = 0; i < 3; i++) {
5171 params->freq = 2412 + ((r + i) % 3) * 25;
5172 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5173 freq_included(channels, params->freq) &&
5174 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5178 /* try all channels in reg. class 81 */
5179 for (i = 0; i < 11; i++) {
5180 params->freq = 2412 + i * 5;
5181 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5182 freq_included(channels, params->freq) &&
5183 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5187 /* try social channel class 180 channel 2 */
5188 params->freq = 58320 + 1 * 2160;
5189 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5190 freq_included(channels, params->freq) &&
5191 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5194 /* try all channels in reg. class 180 */
5195 for (i = 0; i < 4; i++) {
5196 params->freq = 58320 + i * 2160;
5197 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5198 freq_included(channels, params->freq) &&
5199 p2p_supported_freq(wpa_s->global->p2p, params->freq))
5203 wpa_printf(MSG_DEBUG, "P2P: No 2.4 and 60 GHz channel allowed");
5206 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5212 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5213 struct p2p_go_neg_results *params,
5214 int freq, int ht40, int vht,
5215 const struct p2p_channels *channels)
5217 struct wpa_used_freq_data *freqs;
5218 unsigned int pref_freq, cand_freq;
5219 unsigned int num, i;
5221 os_memset(params, 0, sizeof(*params));
5222 params->role_go = 1;
5223 params->ht40 = ht40;
5226 if (!freq_included(channels, freq)) {
5227 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5231 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5232 "frequency %d MHz", freq);
5233 params->freq = freq;
5234 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5235 wpa_s->conf->p2p_oper_channel >= 1 &&
5236 wpa_s->conf->p2p_oper_channel <= 11 &&
5237 freq_included(channels,
5238 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5239 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5240 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5241 "frequency %d MHz", params->freq);
5242 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5243 wpa_s->conf->p2p_oper_reg_class == 116 ||
5244 wpa_s->conf->p2p_oper_reg_class == 117 ||
5245 wpa_s->conf->p2p_oper_reg_class == 124 ||
5246 wpa_s->conf->p2p_oper_reg_class == 126 ||
5247 wpa_s->conf->p2p_oper_reg_class == 127) &&
5248 freq_included(channels,
5249 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
5250 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5251 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5252 "frequency %d MHz", params->freq);
5253 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5254 wpa_s->best_overall_freq > 0 &&
5255 p2p_supported_freq_go(wpa_s->global->p2p,
5256 wpa_s->best_overall_freq) &&
5257 freq_included(channels, wpa_s->best_overall_freq)) {
5258 params->freq = wpa_s->best_overall_freq;
5259 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5260 "channel %d MHz", params->freq);
5261 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5262 wpa_s->best_24_freq > 0 &&
5263 p2p_supported_freq_go(wpa_s->global->p2p,
5264 wpa_s->best_24_freq) &&
5265 freq_included(channels, wpa_s->best_24_freq)) {
5266 params->freq = wpa_s->best_24_freq;
5267 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5268 "channel %d MHz", params->freq);
5269 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5270 wpa_s->best_5_freq > 0 &&
5271 p2p_supported_freq_go(wpa_s->global->p2p,
5272 wpa_s->best_5_freq) &&
5273 freq_included(channels, wpa_s->best_5_freq)) {
5274 params->freq = wpa_s->best_5_freq;
5275 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5276 "channel %d MHz", params->freq);
5277 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5279 params->freq = pref_freq;
5280 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5281 "channels", params->freq);
5283 /* no preference, select some channel */
5284 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
5288 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5289 sizeof(struct wpa_used_freq_data));
5293 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5294 wpa_s->num_multichan_concurrent);
5296 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5298 /* First try the best used frequency if possible */
5299 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5300 params->freq = cand_freq;
5302 /* Try any of the used frequencies */
5303 for (i = 0; i < num; i++) {
5304 if (freq_included(channels, freqs[i].freq)) {
5305 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5307 params->freq = freqs[i].freq;
5313 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5314 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5318 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5322 for (i = 0; i < num; i++) {
5323 if (freqs[i].freq == freq)
5328 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5330 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5334 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5344 static struct wpa_supplicant *
5345 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5348 struct wpa_supplicant *group_wpa_s;
5350 if (!wpas_p2p_create_iface(wpa_s)) {
5351 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5353 wpa_s->p2p_first_connection_timeout = 0;
5357 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5358 WPA_IF_P2P_CLIENT) < 0) {
5359 wpa_msg_global(wpa_s, MSG_ERROR,
5360 "P2P: Failed to add group interface");
5363 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5364 if (group_wpa_s == NULL) {
5365 wpa_msg_global(wpa_s, MSG_ERROR,
5366 "P2P: Failed to initialize group interface");
5367 wpas_p2p_remove_pending_group_interface(wpa_s);
5371 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5372 group_wpa_s->ifname);
5373 group_wpa_s->p2p_first_connection_timeout = 0;
5379 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5380 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5381 * @persistent_group: Whether to create a persistent group
5382 * @freq: Frequency for the group or 0 to indicate no hardcoding
5383 * @ht40: Start GO with 40 MHz channel width
5384 * @vht: Start GO with VHT support
5385 * Returns: 0 on success, -1 on failure
5387 * This function creates a new P2P group with the local end as the Group Owner,
5388 * i.e., without using Group Owner Negotiation.
5390 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5391 int freq, int ht40, int vht)
5393 struct p2p_go_neg_results params;
5395 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5398 os_free(wpa_s->global->add_psk);
5399 wpa_s->global->add_psk = NULL;
5401 /* Make sure we are not running find during connection establishment */
5402 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5403 wpas_p2p_stop_find_oper(wpa_s);
5405 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5409 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5412 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5413 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5414 "(%u MHz) is not supported for P2P uses",
5418 p2p_go_params(wpa_s->global->p2p, ¶ms);
5419 params.persistent_group = persistent_group;
5421 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5424 wpas_start_wps_go(wpa_s, ¶ms, 0);
5430 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5431 struct wpa_ssid *params, int addr_allocated,
5434 struct wpa_ssid *ssid;
5436 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5439 wpa_s->p2p_last_4way_hs_fail = NULL;
5441 wpa_supplicant_ap_deinit(wpa_s);
5443 ssid = wpa_config_add_network(wpa_s->conf);
5446 wpa_config_set_network_defaults(ssid);
5447 ssid->temporary = 1;
5448 ssid->proto = WPA_PROTO_RSN;
5449 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5450 ssid->group_cipher = WPA_CIPHER_CCMP;
5451 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5452 ssid->ssid = os_malloc(params->ssid_len);
5453 if (ssid->ssid == NULL) {
5454 wpa_config_remove_network(wpa_s->conf, ssid->id);
5457 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5458 ssid->ssid_len = params->ssid_len;
5459 ssid->p2p_group = 1;
5460 ssid->export_keys = 1;
5461 if (params->psk_set) {
5462 os_memcpy(ssid->psk, params->psk, 32);
5465 if (params->passphrase)
5466 ssid->passphrase = os_strdup(params->passphrase);
5468 wpa_s->show_group_started = 1;
5469 wpa_s->p2p_in_invitation = 1;
5470 wpa_s->p2p_invite_go_freq = freq;
5472 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5474 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5475 wpas_p2p_group_formation_timeout,
5476 wpa_s->parent, NULL);
5477 wpa_supplicant_select_network(wpa_s, ssid);
5483 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5484 struct wpa_ssid *ssid, int addr_allocated,
5485 int force_freq, int neg_freq, int ht40,
5486 int vht, const struct p2p_channels *channels,
5487 int connection_timeout)
5489 struct p2p_go_neg_results params;
5492 if (ssid->disabled != 2 || ssid->ssid == NULL)
5495 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5496 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5497 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5502 os_free(wpa_s->global->add_psk);
5503 wpa_s->global->add_psk = NULL;
5505 /* Make sure we are not running find during connection establishment */
5506 wpas_p2p_stop_find_oper(wpa_s);
5508 wpa_s->p2p_fallback_to_go_neg = 0;
5510 if (force_freq > 0) {
5511 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5515 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5516 if (freq < 0 || (freq > 0 && !freq_included(channels, freq)))
5520 if (ssid->mode == WPAS_MODE_INFRA)
5521 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5523 if (ssid->mode != WPAS_MODE_P2P_GO)
5526 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
5530 params.psk_set = ssid->psk_set;
5532 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5533 if (ssid->passphrase) {
5534 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5535 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5536 "persistent group");
5539 os_strlcpy(params.passphrase, ssid->passphrase,
5540 sizeof(params.passphrase));
5542 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5543 params.ssid_len = ssid->ssid_len;
5544 params.persistent_group = 1;
5546 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5550 wpa_s->p2p_first_connection_timeout = connection_timeout;
5551 wpas_start_wps_go(wpa_s, ¶ms, 0);
5557 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5558 struct wpabuf *proberesp_ies)
5560 struct wpa_supplicant *wpa_s = ctx;
5561 if (wpa_s->ap_iface) {
5562 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5563 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5564 wpabuf_free(beacon_ies);
5565 wpabuf_free(proberesp_ies);
5569 wpabuf_free(hapd->p2p_beacon_ie);
5570 hapd->p2p_beacon_ie = beacon_ies;
5572 wpabuf_free(hapd->p2p_probe_resp_ie);
5573 hapd->p2p_probe_resp_ie = proberesp_ies;
5575 wpabuf_free(beacon_ies);
5576 wpabuf_free(proberesp_ies);
5578 wpa_supplicant_ap_update_beacon(wpa_s);
5582 static void wpas_p2p_idle_update(void *ctx, int idle)
5584 struct wpa_supplicant *wpa_s = ctx;
5585 if (!wpa_s->ap_iface)
5587 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
5589 if (wpa_s->global->p2p_fail_on_wps_complete &&
5590 wpa_s->p2p_in_provisioning) {
5591 wpas_p2p_grpform_fail_after_wps(wpa_s);
5594 wpas_p2p_set_group_idle_timeout(wpa_s);
5596 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5600 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
5601 struct wpa_ssid *ssid)
5603 struct p2p_group *group;
5604 struct p2p_group_config *cfg;
5606 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5609 cfg = os_zalloc(sizeof(*cfg));
5613 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
5614 cfg->persistent_group = 2;
5615 else if (ssid->p2p_persistent_group)
5616 cfg->persistent_group = 1;
5617 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5618 if (wpa_s->max_stations &&
5619 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5620 cfg->max_clients = wpa_s->max_stations;
5622 cfg->max_clients = wpa_s->conf->max_num_sta;
5623 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5624 cfg->ssid_len = ssid->ssid_len;
5625 cfg->freq = ssid->frequency;
5626 cfg->cb_ctx = wpa_s;
5627 cfg->ie_update = wpas_p2p_ie_update;
5628 cfg->idle_update = wpas_p2p_idle_update;
5630 group = p2p_group_init(wpa_s->global->p2p, cfg);
5633 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
5634 p2p_group_notif_formation_done(group);
5635 wpa_s->p2p_group = group;
5640 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5643 struct wpa_ssid *ssid = wpa_s->current_ssid;
5645 if (!wpa_s->p2p_in_provisioning) {
5646 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5647 "provisioning not in progress");
5651 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5652 u8 go_dev_addr[ETH_ALEN];
5653 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5654 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5656 /* Clear any stored provisioning info */
5657 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5660 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5662 wpa_s->p2p_go_group_formation_completed = 1;
5663 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5665 * Use a separate timeout for initial data connection to
5666 * complete to allow the group to be removed automatically if
5667 * something goes wrong in this step before the P2P group idle
5668 * timeout mechanism is taken into use.
5670 wpa_dbg(wpa_s, MSG_DEBUG,
5671 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5672 P2P_MAX_INITIAL_CONN_WAIT);
5673 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5674 wpas_p2p_group_formation_timeout,
5675 wpa_s->parent, NULL);
5678 * Use a separate timeout for initial data connection to
5679 * complete to allow the group to be removed automatically if
5680 * the client does not complete data connection successfully.
5682 wpa_dbg(wpa_s, MSG_DEBUG,
5683 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5684 P2P_MAX_INITIAL_CONN_WAIT_GO);
5685 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5686 wpas_p2p_group_formation_timeout,
5687 wpa_s->parent, NULL);
5689 * Complete group formation on first successful data connection
5691 wpa_s->p2p_go_group_formation_completed = 0;
5693 if (wpa_s->global->p2p)
5694 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5695 wpas_group_formation_completed(wpa_s, 1);
5699 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5700 struct wps_event_fail *fail)
5702 if (!wpa_s->p2p_in_provisioning) {
5703 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5704 "provisioning not in progress");
5708 if (wpa_s->go_params) {
5709 p2p_clear_provisioning_info(
5711 wpa_s->go_params->peer_device_addr);
5714 wpas_notify_p2p_wps_failed(wpa_s, fail);
5716 if (wpa_s == wpa_s->global->p2p_group_formation) {
5718 * Allow some time for the failed WPS negotiation exchange to
5719 * complete, but remove the group since group formation cannot
5720 * succeed after provisioning failure.
5722 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5723 wpa_s->global->p2p_fail_on_wps_complete = 1;
5724 eloop_deplete_timeout(0, 50000,
5725 wpas_p2p_group_formation_timeout,
5726 wpa_s->parent, NULL);
5731 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5733 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5734 !wpa_s->p2p_in_provisioning)
5737 wpas_p2p_grpform_fail_after_wps(wpa_s);
5743 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5744 const char *config_method,
5745 enum wpas_p2p_prov_disc_use use)
5749 wpa_s->p2p_fallback_to_go_neg = 0;
5750 wpa_s->pending_pd_use = NORMAL_PD;
5751 if (os_strncmp(config_method, "display", 7) == 0)
5752 config_methods = WPS_CONFIG_DISPLAY;
5753 else if (os_strncmp(config_method, "keypad", 6) == 0)
5754 config_methods = WPS_CONFIG_KEYPAD;
5755 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5756 os_strncmp(config_method, "pushbutton", 10) == 0)
5757 config_methods = WPS_CONFIG_PUSHBUTTON;
5759 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5763 if (use == WPAS_P2P_PD_AUTO) {
5764 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5765 wpa_s->pending_pd_config_methods = config_methods;
5766 wpa_s->p2p_auto_pd = 1;
5767 wpa_s->p2p_auto_join = 0;
5768 wpa_s->pending_pd_before_join = 0;
5769 wpa_s->auto_pd_scan_retry = 0;
5770 wpas_p2p_stop_find(wpa_s);
5771 wpa_s->p2p_join_scan_count = 0;
5772 os_get_reltime(&wpa_s->p2p_auto_started);
5773 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5774 wpa_s->p2p_auto_started.sec,
5775 wpa_s->p2p_auto_started.usec);
5776 wpas_p2p_join_scan(wpa_s, NULL);
5780 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5783 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
5784 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5789 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5792 return p2p_scan_result_text(ies, ies_len, buf, end);
5796 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5798 if (!offchannel_pending_action_tx(wpa_s))
5801 wpas_p2p_action_tx_clear(wpa_s);
5803 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5804 "operation request");
5805 offchannel_clear_pending_action_tx(wpa_s);
5809 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5810 enum p2p_discovery_type type,
5811 unsigned int num_req_dev_types, const u8 *req_dev_types,
5812 const u8 *dev_id, unsigned int search_delay)
5814 wpas_p2p_clear_pending_action_tx(wpa_s);
5815 wpa_s->p2p_long_listen = 0;
5817 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5818 wpa_s->p2p_in_provisioning)
5821 wpa_supplicant_cancel_sched_scan(wpa_s);
5823 return p2p_find(wpa_s->global->p2p, timeout, type,
5824 num_req_dev_types, req_dev_types, dev_id,
5829 static void wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
5831 wpas_p2p_clear_pending_action_tx(wpa_s);
5832 wpa_s->p2p_long_listen = 0;
5833 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5834 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5836 if (wpa_s->global->p2p)
5837 p2p_stop_find(wpa_s->global->p2p);
5841 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5843 wpas_p2p_stop_find_oper(wpa_s);
5844 wpas_p2p_remove_pending_group_interface(wpa_s);
5848 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5850 struct wpa_supplicant *wpa_s = eloop_ctx;
5851 wpa_s->p2p_long_listen = 0;
5855 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5859 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5862 wpa_supplicant_cancel_sched_scan(wpa_s);
5863 wpas_p2p_clear_pending_action_tx(wpa_s);
5867 * This is a request for unlimited Listen state. However, at
5868 * least for now, this is mapped to a Listen state for one
5873 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5874 wpa_s->p2p_long_listen = 0;
5877 * Stop previous find/listen operation to avoid trying to request a new
5878 * remain-on-channel operation while the driver is still running the
5881 if (wpa_s->global->p2p)
5882 p2p_stop_find(wpa_s->global->p2p);
5884 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5885 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5886 wpa_s->p2p_long_listen = timeout * 1000;
5887 eloop_register_timeout(timeout, 0,
5888 wpas_p2p_long_listen_timeout,
5896 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5897 u8 *buf, size_t len, int p2p_group)
5899 struct wpabuf *p2p_ie;
5902 if (wpa_s->global->p2p_disabled)
5904 if (wpa_s->conf->p2p_disabled)
5906 if (wpa_s->global->p2p == NULL)
5911 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5912 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5914 wpabuf_free(p2p_ie);
5920 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
5921 const u8 *dst, const u8 *bssid,
5922 const u8 *ie, size_t ie_len, int ssi_signal)
5924 if (wpa_s->global->p2p_disabled)
5926 if (wpa_s->global->p2p == NULL)
5929 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5931 case P2P_PREQ_NOT_P2P:
5932 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5935 case P2P_PREQ_MALFORMED:
5936 case P2P_PREQ_NOT_LISTEN:
5937 case P2P_PREQ_NOT_PROCESSED:
5938 default: /* make gcc happy */
5940 case P2P_PREQ_PROCESSED:
5946 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5947 const u8 *sa, const u8 *bssid,
5948 u8 category, const u8 *data, size_t len, int freq)
5950 if (wpa_s->global->p2p_disabled)
5952 if (wpa_s->global->p2p == NULL)
5955 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5960 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5962 if (wpa_s->global->p2p_disabled)
5964 if (wpa_s->global->p2p == NULL)
5967 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5971 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5973 p2p_group_deinit(wpa_s->p2p_group);
5974 wpa_s->p2p_group = NULL;
5976 wpa_s->ap_configured_cb = NULL;
5977 wpa_s->ap_configured_cb_ctx = NULL;
5978 wpa_s->ap_configured_cb_data = NULL;
5979 wpa_s->connect_without_scan = NULL;
5983 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5985 wpa_s->p2p_long_listen = 0;
5987 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5990 return p2p_reject(wpa_s->global->p2p, addr);
5994 /* Invite to reinvoke a persistent group */
5995 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5996 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
5997 int ht40, int vht, int pref_freq)
5999 enum p2p_invite_role role;
6003 int no_pref_freq_given = pref_freq == 0;
6005 wpa_s->global->p2p_invite_group = NULL;
6007 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
6009 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
6011 wpa_s->p2p_persistent_go_freq = freq;
6012 wpa_s->p2p_go_ht40 = !!ht40;
6013 if (ssid->mode == WPAS_MODE_P2P_GO) {
6014 role = P2P_INVITE_ROLE_GO;
6015 if (peer_addr == NULL) {
6016 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
6017 "address in invitation command");
6020 if (wpas_p2p_create_iface(wpa_s)) {
6021 if (wpas_p2p_add_group_interface(wpa_s,
6022 WPA_IF_P2P_GO) < 0) {
6023 wpa_printf(MSG_ERROR, "P2P: Failed to "
6024 "allocate a new interface for the "
6028 bssid = wpa_s->pending_interface_addr;
6030 bssid = wpa_s->own_addr;
6032 role = P2P_INVITE_ROLE_CLIENT;
6033 peer_addr = ssid->bssid;
6035 wpa_s->pending_invite_ssid_id = ssid->id;
6037 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6038 role == P2P_INVITE_ROLE_GO);
6042 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6045 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
6046 no_pref_freq_given && pref_freq > 0 &&
6047 wpa_s->num_multichan_concurrent > 1 &&
6048 wpas_p2p_num_unused_channels(wpa_s) > 0) {
6049 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
6054 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6055 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
6060 /* Invite to join an active group */
6061 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
6062 const u8 *peer_addr, const u8 *go_dev_addr)
6064 struct wpa_global *global = wpa_s->global;
6065 enum p2p_invite_role role;
6067 struct wpa_ssid *ssid;
6069 int freq = 0, force_freq = 0, pref_freq = 0;
6072 wpa_s->p2p_persistent_go_freq = 0;
6073 wpa_s->p2p_go_ht40 = 0;
6074 wpa_s->p2p_go_vht = 0;
6076 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6077 if (os_strcmp(wpa_s->ifname, ifname) == 0)
6080 if (wpa_s == NULL) {
6081 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
6085 ssid = wpa_s->current_ssid;
6087 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
6092 wpa_s->global->p2p_invite_group = wpa_s;
6093 persistent = ssid->p2p_persistent_group &&
6094 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
6095 ssid->ssid, ssid->ssid_len);
6097 if (ssid->mode == WPAS_MODE_P2P_GO) {
6098 role = P2P_INVITE_ROLE_ACTIVE_GO;
6099 bssid = wpa_s->own_addr;
6100 if (go_dev_addr == NULL)
6101 go_dev_addr = wpa_s->global->p2p_dev_addr;
6102 freq = ssid->frequency;
6104 role = P2P_INVITE_ROLE_CLIENT;
6105 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
6106 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
6107 "invite to current group");
6110 bssid = wpa_s->bssid;
6111 if (go_dev_addr == NULL &&
6112 !is_zero_ether_addr(wpa_s->go_dev_addr))
6113 go_dev_addr = wpa_s->go_dev_addr;
6114 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6115 (int) wpa_s->assoc_freq;
6117 wpa_s->parent->pending_invite_ssid_id = -1;
6119 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6122 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
6123 role == P2P_INVITE_ROLE_ACTIVE_GO);
6126 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
6128 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6129 ssid->ssid, ssid->ssid_len, force_freq,
6130 go_dev_addr, persistent, pref_freq, -1);
6134 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6136 struct wpa_ssid *ssid = wpa_s->current_ssid;
6137 u8 go_dev_addr[ETH_ALEN];
6138 int network_id = -1;
6144 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6145 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6146 wpa_s->parent, NULL);
6149 if (!wpa_s->show_group_started || !ssid)
6152 wpa_s->show_group_started = 0;
6154 os_memset(go_dev_addr, 0, ETH_ALEN);
6155 if (ssid->bssid_set)
6156 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6157 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6159 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6161 if (wpa_s->global->p2p_group_formation == wpa_s)
6162 wpa_s->global->p2p_group_formation = NULL;
6164 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6165 (int) wpa_s->assoc_freq;
6168 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6169 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
6170 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6171 ip[0], ip[1], ip[2], ip[3],
6172 ip[4], ip[5], ip[6], ip[7],
6173 ip[8], ip[9], ip[10], ip[11]);
6176 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6177 ssid->passphrase == NULL && ssid->psk_set ?
6179 ssid->passphrase, go_dev_addr, persistent,
6183 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6186 network_id = ssid->id;
6187 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6191 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6192 u32 interval1, u32 duration2, u32 interval2)
6196 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6199 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6200 wpa_s->current_ssid == NULL ||
6201 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6204 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6205 wpa_s->own_addr, wpa_s->assoc_freq,
6206 duration1, interval1, duration2, interval2);
6208 wpa_s->waiting_presence_resp = 1;
6214 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6215 unsigned int interval)
6217 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6220 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6224 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6226 if (wpa_s->current_ssid == NULL) {
6228 * current_ssid can be cleared when P2P client interface gets
6229 * disconnected, so assume this interface was used as P2P
6234 return wpa_s->current_ssid->p2p_group &&
6235 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6239 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6241 struct wpa_supplicant *wpa_s = eloop_ctx;
6243 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6244 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6249 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6251 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6255 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6259 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6260 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6262 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6265 timeout = wpa_s->conf->p2p_group_idle;
6266 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6267 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6268 timeout = P2P_MAX_CLIENT_IDLE;
6274 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6275 timeout = 0; /* special client mode no-timeout */
6280 if (wpa_s->p2p_in_provisioning) {
6282 * Use the normal group formation timeout during the
6283 * provisioning phase to avoid terminating this process too
6284 * early due to group idle timeout.
6286 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6287 "during provisioning");
6291 if (wpa_s->show_group_started) {
6293 * Use the normal group formation timeout between the end of
6294 * the provisioning phase and completion of 4-way handshake to
6295 * avoid terminating this process too early due to group idle
6298 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6299 "while waiting for initial 4-way handshake to "
6304 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6306 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6311 /* Returns 1 if the interface was removed */
6312 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6313 u16 reason_code, const u8 *ie, size_t ie_len,
6314 int locally_generated)
6316 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6319 if (!locally_generated)
6320 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6323 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6324 wpa_s->current_ssid &&
6325 wpa_s->current_ssid->p2p_group &&
6326 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6327 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6328 "session is ending");
6329 if (wpas_p2p_group_delete(wpa_s,
6330 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6339 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6340 u16 reason_code, const u8 *ie, size_t ie_len,
6341 int locally_generated)
6343 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6346 if (!locally_generated)
6347 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6352 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6354 struct p2p_data *p2p = wpa_s->global->p2p;
6359 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6362 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6363 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6365 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6366 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6369 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6370 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6372 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6373 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6375 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6376 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6377 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6378 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6379 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6382 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6383 p2p_set_sec_dev_types(p2p,
6384 (void *) wpa_s->conf->sec_device_type,
6385 wpa_s->conf->num_sec_device_types);
6387 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6389 p2p_remove_wps_vendor_extensions(p2p);
6390 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6391 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6393 p2p_add_wps_vendor_extension(
6394 p2p, wpa_s->conf->wps_vendor_ext[i]);
6398 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6399 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6401 country[0] = wpa_s->conf->country[0];
6402 country[1] = wpa_s->conf->country[1];
6404 p2p_set_country(p2p, country);
6407 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6408 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6409 wpa_s->conf->p2p_ssid_postfix ?
6410 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6414 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6415 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6417 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6418 u8 reg_class, channel;
6423 if (wpa_s->conf->p2p_listen_reg_class &&
6424 wpa_s->conf->p2p_listen_channel) {
6425 reg_class = wpa_s->conf->p2p_listen_reg_class;
6426 channel = wpa_s->conf->p2p_listen_channel;
6431 * Pick one of the social channels randomly as the
6434 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6437 channel = 1 + (r % 3) * 5;
6440 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6443 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6446 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6447 u8 op_reg_class, op_channel, cfg_op_channel;
6450 if (wpa_s->conf->p2p_oper_reg_class &&
6451 wpa_s->conf->p2p_oper_channel) {
6452 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6453 op_channel = wpa_s->conf->p2p_oper_channel;
6458 * Use random operation channel from (1, 6, 11)
6459 *if no other preference is indicated.
6461 if (os_get_random((u8 *) &r, sizeof(r)) < 0)
6464 op_channel = 1 + (r % 3) * 5;
6467 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6470 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6474 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6475 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6476 wpa_s->conf->p2p_pref_chan) < 0) {
6477 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6481 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6482 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6487 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6488 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
6492 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6495 if (!wpa_s->ap_iface)
6497 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6502 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6504 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6507 wpa_s->global->cross_connection = enabled;
6508 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6511 struct wpa_supplicant *iface;
6513 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6515 if (iface->cross_connect_enabled == 0)
6518 iface->cross_connect_enabled = 0;
6519 iface->cross_connect_in_use = 0;
6520 wpa_msg_global(iface->parent, MSG_INFO,
6521 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6523 iface->cross_connect_uplink);
6531 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6533 struct wpa_supplicant *iface;
6535 if (!uplink->global->cross_connection)
6538 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6539 if (!iface->cross_connect_enabled)
6541 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6544 if (iface->ap_iface == NULL)
6546 if (iface->cross_connect_in_use)
6549 iface->cross_connect_in_use = 1;
6550 wpa_msg_global(iface->parent, MSG_INFO,
6551 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6552 iface->ifname, iface->cross_connect_uplink);
6557 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6559 struct wpa_supplicant *iface;
6561 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6562 if (!iface->cross_connect_enabled)
6564 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6567 if (!iface->cross_connect_in_use)
6570 wpa_msg_global(iface->parent, MSG_INFO,
6571 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6572 iface->ifname, iface->cross_connect_uplink);
6573 iface->cross_connect_in_use = 0;
6578 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6580 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6581 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6582 wpa_s->cross_connect_disallowed)
6583 wpas_p2p_disable_cross_connect(wpa_s);
6585 wpas_p2p_enable_cross_connect(wpa_s);
6586 if (!wpa_s->ap_iface &&
6587 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6588 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6592 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6594 wpas_p2p_disable_cross_connect(wpa_s);
6595 if (!wpa_s->ap_iface &&
6596 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6598 wpas_p2p_set_group_idle_timeout(wpa_s);
6602 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6604 struct wpa_supplicant *iface;
6606 if (!wpa_s->global->cross_connection)
6609 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6612 if (iface->drv_flags &
6613 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6615 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6618 wpa_s->cross_connect_enabled = 1;
6619 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6620 sizeof(wpa_s->cross_connect_uplink));
6621 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6622 "%s to %s whenever uplink is available",
6623 wpa_s->ifname, wpa_s->cross_connect_uplink);
6625 if (iface->ap_iface || iface->current_ssid == NULL ||
6626 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6627 iface->cross_connect_disallowed ||
6628 iface->wpa_state != WPA_COMPLETED)
6631 wpa_s->cross_connect_in_use = 1;
6632 wpa_msg_global(wpa_s->parent, MSG_INFO,
6633 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6634 wpa_s->ifname, wpa_s->cross_connect_uplink);
6640 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6642 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6643 !wpa_s->p2p_in_provisioning)
6644 return 0; /* not P2P client operation */
6646 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6648 if (wpa_s != wpa_s->parent)
6649 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
6650 wpas_p2p_group_formation_failed(wpa_s);
6655 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6657 struct wpa_supplicant *wpa_s = eloop_ctx;
6658 wpas_p2p_notif_pbc_overlap(wpa_s);
6662 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6664 struct p2p_channels chan, cli_chan;
6665 struct wpa_supplicant *ifs;
6667 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6670 os_memset(&chan, 0, sizeof(chan));
6671 os_memset(&cli_chan, 0, sizeof(cli_chan));
6672 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
6673 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6678 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
6680 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6682 if (!ifs->current_ssid ||
6683 !ifs->current_ssid->p2p_group ||
6684 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6685 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6687 freq = ifs->current_ssid->frequency;
6688 if (freq_included(&chan, freq)) {
6689 wpa_dbg(ifs, MSG_DEBUG,
6690 "P2P GO operating frequency %d MHz in valid range",
6695 wpa_dbg(ifs, MSG_DEBUG,
6696 "P2P GO operating in invalid frequency %d MHz", freq);
6697 /* TODO: Consider using CSA or removing the group within
6699 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6704 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6705 struct wpa_scan_results *scan_res)
6707 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6711 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6713 struct wpa_global *global = wpa_s->global;
6717 if (global->p2p == NULL)
6720 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6722 if (wpa_s->pending_interface_name[0] &&
6723 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6726 peer = p2p_get_go_neg_peer(global->p2p);
6728 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6729 MACSTR, MAC2STR(peer));
6730 p2p_unauthorize(global->p2p, peer);
6734 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6735 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6736 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6740 if (wpa_s->pending_pd_before_join) {
6741 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6742 wpa_s->pending_pd_before_join = 0;
6746 wpas_p2p_stop_find(wpa_s);
6748 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6749 if (wpa_s == global->p2p_group_formation &&
6750 (wpa_s->p2p_in_provisioning ||
6751 wpa_s->parent->pending_interface_type ==
6752 WPA_IF_P2P_CLIENT)) {
6753 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6754 "formation found - cancelling",
6757 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6758 wpa_s->parent, NULL);
6759 if (wpa_s->p2p_in_provisioning) {
6760 wpas_group_formation_completed(wpa_s, 0);
6763 wpas_p2p_group_delete(wpa_s,
6764 P2P_GROUP_REMOVAL_REQUESTED);
6766 } else if (wpa_s->p2p_in_invitation) {
6767 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6770 wpas_p2p_group_formation_failed(wpa_s);
6775 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6783 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6785 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6788 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6789 "being available anymore");
6790 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
6794 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6795 int freq_24, int freq_5, int freq_overall)
6797 struct p2p_data *p2p = wpa_s->global->p2p;
6800 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6804 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6807 struct p2p_data *p2p = wpa_s->global->p2p;
6812 if (hwaddr_aton(addr, peer))
6815 return p2p_unauthorize(p2p, peer);
6820 * wpas_p2p_disconnect - Disconnect from a P2P Group
6821 * @wpa_s: Pointer to wpa_supplicant data
6822 * Returns: 0 on success, -1 on failure
6824 * This can be used to disconnect from a group in which the local end is a P2P
6825 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6826 * virtual network interface was created for this group, that interface will be
6827 * removed. Otherwise, only the configured P2P group network will be removed
6828 * from the interface.
6830 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6836 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6841 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6845 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6848 ret = p2p_in_progress(wpa_s->global->p2p);
6851 * Check whether there is an ongoing WPS provisioning step (or
6852 * other parts of group formation) on another interface since
6853 * p2p_in_progress() does not report this to avoid issues for
6854 * scans during such provisioning step.
6856 if (wpa_s->global->p2p_group_formation &&
6857 wpa_s->global->p2p_group_formation != wpa_s) {
6858 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6859 "in group formation",
6860 wpa_s->global->p2p_group_formation->ifname);
6865 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6866 struct os_reltime now;
6867 os_get_reltime(&now);
6868 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6869 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
6870 /* Wait for the first client has expired */
6871 wpa_s->global->p2p_go_wait_client.sec = 0;
6873 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6882 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6883 struct wpa_ssid *ssid)
6885 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6886 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6887 wpa_s->parent, NULL) > 0) {
6889 * Remove the network by scheduling the group formation
6890 * timeout to happen immediately. The teardown code
6891 * needs to be scheduled to run asynch later so that we
6892 * don't delete data from under ourselves unexpectedly.
6893 * Calling wpas_p2p_group_formation_timeout directly
6894 * causes a series of crashes in WPS failure scenarios.
6896 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6897 "P2P group network getting removed");
6898 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6899 wpa_s->parent, NULL);
6904 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
6905 const u8 *addr, const u8 *ssid,
6911 for (s = wpa_s->conf->ssid; s; s = s->next) {
6912 if (s->disabled != 2)
6915 (ssid_len != s->ssid_len ||
6916 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6919 if (s->mode == WPAS_MODE_P2P_GO)
6923 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6924 return s; /* peer is GO in the persistent group */
6925 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6927 for (i = 0; i < s->num_p2p_clients; i++) {
6928 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6929 addr, ETH_ALEN) == 0)
6930 return s; /* peer is P2P client in persistent
6939 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6942 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6943 wpa_s->parent, NULL) > 0) {
6945 * This can happen if WPS provisioning step is not terminated
6946 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6947 * peer was able to connect, there is no need to time out group
6948 * formation after this, though. In addition, this is used with
6949 * the initial connection wait on the GO as a separate formation
6950 * timeout and as such, expected to be hit after the initial WPS
6951 * provisioning step.
6953 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6955 if (!wpa_s->p2p_go_group_formation_completed &&
6956 !wpa_s->group_formation_reported) {
6958 * GO has not yet notified group formation success since
6959 * the WPS step was not completed cleanly. Do that
6960 * notification now since the P2P Client was able to
6961 * connect and as such, must have received the
6962 * credential from the WPS step.
6964 if (wpa_s->global->p2p)
6965 p2p_wps_success_cb(wpa_s->global->p2p, addr);
6966 wpas_group_formation_completed(wpa_s, 1);
6969 if (!wpa_s->p2p_go_group_formation_completed) {
6970 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6971 wpa_s->p2p_go_group_formation_completed = 1;
6972 wpa_s->global->p2p_group_formation = NULL;
6973 wpa_s->p2p_in_provisioning = 0;
6974 wpa_s->p2p_in_invitation = 0;
6976 wpa_s->global->p2p_go_wait_client.sec = 0;
6979 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6983 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6986 struct wpa_supplicant *group = wpa_s;
6987 if (wpa_s->global->p2p_group_formation)
6988 group = wpa_s->global->p2p_group_formation;
6989 wpa_s = wpa_s->parent;
6990 offchannel_send_action_done(wpa_s);
6992 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
6993 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6994 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6995 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6996 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6997 wpa_s->p2p_persistent_id,
6998 wpa_s->p2p_pd_before_go_neg,
7004 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
7006 if (!wpa_s->p2p_fallback_to_go_neg ||
7007 wpa_s->p2p_in_provisioning <= 5)
7010 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
7011 return 0; /* peer operating as a GO */
7013 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
7014 "fallback to GO Negotiation");
7015 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
7021 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
7023 struct wpa_supplicant *ifs;
7025 if (wpa_s->wpa_state > WPA_SCANNING) {
7026 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
7027 "concurrent operation",
7028 wpa_s->conf->p2p_search_delay);
7029 return wpa_s->conf->p2p_search_delay;
7032 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
7034 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
7035 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
7036 "delay due to concurrent operation on "
7038 wpa_s->conf->p2p_search_delay,
7040 return wpa_s->conf->p2p_search_delay;
7048 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
7049 struct wpa_ssid *s, const u8 *addr,
7052 struct psk_list_entry *psk, *tmp;
7055 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
7057 if ((iface_addr && !psk->p2p &&
7058 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
7059 (!iface_addr && psk->p2p &&
7060 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
7061 wpa_dbg(wpa_s, MSG_DEBUG,
7062 "P2P: Remove persistent group PSK list entry for "
7064 MAC2STR(psk->addr), psk->p2p);
7065 dl_list_del(&psk->list);
7075 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
7076 const u8 *p2p_dev_addr,
7077 const u8 *psk, size_t psk_len)
7079 struct wpa_ssid *ssid = wpa_s->current_ssid;
7080 struct wpa_ssid *persistent;
7081 struct psk_list_entry *p, *last;
7083 if (psk_len != sizeof(p->psk))
7087 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
7088 " p2p_dev_addr=" MACSTR,
7089 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
7090 if (is_zero_ether_addr(p2p_dev_addr))
7091 p2p_dev_addr = NULL;
7093 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
7097 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
7098 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
7099 /* To be added to persistent group once created */
7100 if (wpa_s->global->add_psk == NULL) {
7101 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
7102 if (wpa_s->global->add_psk == NULL)
7105 p = wpa_s->global->add_psk;
7108 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7111 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7113 os_memcpy(p->psk, psk, psk_len);
7117 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
7118 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
7122 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
7125 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
7129 p = os_zalloc(sizeof(*p));
7134 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
7137 os_memcpy(p->addr, mac_addr, ETH_ALEN);
7139 os_memcpy(p->psk, psk, psk_len);
7141 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7142 (last = dl_list_last(&persistent->psk_list,
7143 struct psk_list_entry, list))) {
7144 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7145 MACSTR " (p2p=%u) to make room for a new one",
7146 MAC2STR(last->addr), last->p2p);
7147 dl_list_del(&last->list);
7151 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7152 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7153 p2p_dev_addr == NULL);
7155 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7156 MACSTR, MAC2STR(p2p_dev_addr));
7158 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7161 dl_list_add(&persistent->psk_list, &p->list);
7163 if (wpa_s->parent->conf->update_config &&
7164 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7165 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
7169 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7170 struct wpa_ssid *s, const u8 *addr,
7175 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
7176 if (res > 0 && wpa_s->conf->update_config &&
7177 wpa_config_write(wpa_s->confname, wpa_s->conf))
7178 wpa_dbg(wpa_s, MSG_DEBUG,
7179 "P2P: Failed to update configuration");
7183 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7184 const u8 *peer, int iface_addr)
7186 struct hostapd_data *hapd;
7187 struct hostapd_wpa_psk *psk, *prev, *rem;
7188 struct sta_info *sta;
7190 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7191 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7194 /* Remove per-station PSK entry */
7195 hapd = wpa_s->ap_iface->bss[0];
7197 psk = hapd->conf->ssid.wpa_psk;
7199 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7201 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7202 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7203 MACSTR " iface_addr=%d",
7204 MAC2STR(peer), iface_addr);
7206 prev->next = psk->next;
7208 hapd->conf->ssid.wpa_psk = psk->next;
7218 /* Disconnect from group */
7220 sta = ap_get_sta(hapd, peer);
7222 sta = ap_get_sta_p2p(hapd, peer);
7224 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7225 " (iface_addr=%d) from group",
7226 MAC2STR(peer), iface_addr);
7227 hostapd_drv_sta_deauth(hapd, sta->addr,
7228 WLAN_REASON_DEAUTH_LEAVING);
7229 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7234 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7238 struct wpa_supplicant *w;
7240 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7242 /* Remove from any persistent group */
7243 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
7244 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7247 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
7248 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
7251 /* Remove from any operating group */
7252 for (w = wpa_s->global->ifaces; w; w = w->next)
7253 wpas_p2p_remove_client_go(w, peer, iface_addr);
7257 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7259 struct wpa_supplicant *wpa_s = eloop_ctx;
7260 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7264 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7266 struct wpa_supplicant *wpa_s = eloop_ctx;
7268 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7269 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7273 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7274 struct wpa_ssid *ssid)
7276 struct wpa_supplicant *iface;
7278 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7279 if (!iface->current_ssid ||
7280 iface->current_ssid->frequency == freq ||
7281 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7282 !iface->current_ssid->p2p_group))
7285 /* Remove the connection with least priority */
7286 if (!wpas_is_p2p_prioritized(iface)) {
7287 /* STA connection has priority over existing
7288 * P2P connection, so remove the interface. */
7289 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7290 eloop_register_timeout(0, 0,
7291 wpas_p2p_group_freq_conflict,
7293 /* If connection in progress is P2P connection, do not
7294 * proceed for the connection. */
7300 /* P2P connection has priority, disable the STA network
7302 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7304 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7305 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7306 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7308 /* If P2P connection is in progress, continue
7321 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7323 struct wpa_ssid *ssid = wpa_s->current_ssid;
7325 if (ssid == NULL || !ssid->p2p_group)
7328 if (wpa_s->p2p_last_4way_hs_fail &&
7329 wpa_s->p2p_last_4way_hs_fail == ssid) {
7330 u8 go_dev_addr[ETH_ALEN];
7331 struct wpa_ssid *persistent;
7333 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7335 ssid->ssid_len) <= 0) {
7336 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7340 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7341 MACSTR, MAC2STR(go_dev_addr));
7342 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7345 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7346 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7349 wpa_msg_global(wpa_s->parent, MSG_INFO,
7350 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7353 wpa_s->p2p_last_4way_hs_fail = NULL;
7355 * Remove the group from a timeout to avoid issues with caller
7356 * continuing to use the interface if this is on a P2P group
7359 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7364 wpa_s->p2p_last_4way_hs_fail = ssid;
7369 #ifdef CONFIG_WPS_NFC
7371 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7379 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7383 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7384 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7391 wpabuf_put_be16(ret, wsc_len);
7393 wpabuf_put_buf(ret, wsc);
7394 wpabuf_put_be16(ret, wpabuf_len(p2p));
7395 wpabuf_put_buf(ret, p2p);
7399 wpa_hexdump_buf(MSG_DEBUG,
7400 "P2P: Generated NFC connection handover message", ret);
7404 tmp = ndef_build_p2p(ret);
7407 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7417 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7418 struct wpa_ssid **ssid, u8 *go_dev_addr)
7420 struct wpa_supplicant *iface;
7423 os_memset(go_dev_addr, 0, ETH_ALEN);
7426 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7427 if (iface->wpa_state < WPA_ASSOCIATING ||
7428 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7429 !iface->current_ssid->p2p_group ||
7430 iface->current_ssid->mode != WPAS_MODE_INFRA)
7433 *ssid = iface->current_ssid;
7435 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7436 return iface->assoc_freq;
7442 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7445 struct wpabuf *wsc, *p2p;
7446 struct wpa_ssid *ssid;
7447 u8 go_dev_addr[ETH_ALEN];
7448 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7450 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7451 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7455 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7456 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7457 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7458 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7462 if (cli_freq == 0) {
7463 wsc = wps_build_nfc_handover_req_p2p(
7464 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7467 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7468 go_dev_addr, ssid ? ssid->ssid : NULL,
7469 ssid ? ssid->ssid_len : 0);
7471 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7475 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7478 struct wpabuf *wsc, *p2p;
7479 struct wpa_ssid *ssid;
7480 u8 go_dev_addr[ETH_ALEN];
7481 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7483 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7486 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7487 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7488 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7491 if (cli_freq == 0) {
7492 wsc = wps_build_nfc_handover_sel_p2p(
7494 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7495 DEV_PW_NFC_CONNECTION_HANDOVER,
7496 wpa_s->conf->wps_nfc_dh_pubkey,
7497 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7500 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7501 go_dev_addr, ssid ? ssid->ssid : NULL,
7502 ssid ? ssid->ssid_len : 0);
7504 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7508 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7509 struct p2p_nfc_params *params)
7511 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7512 "connection handover (freq=%d)",
7515 if (params->go_freq && params->go_ssid_len) {
7516 wpa_s->p2p_wps_method = WPS_NFC;
7517 wpa_s->pending_join_wps_method = WPS_NFC;
7518 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7519 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7521 return wpas_p2p_join_start(wpa_s, params->go_freq,
7523 params->go_ssid_len);
7526 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7527 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7528 params->go_freq, -1, 0, 1, 1);
7532 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7533 struct p2p_nfc_params *params, int tag)
7535 int res, persistent;
7536 struct wpa_ssid *ssid;
7538 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7539 "connection handover");
7540 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7541 ssid = wpa_s->current_ssid;
7544 if (ssid->mode != WPAS_MODE_P2P_GO)
7546 if (wpa_s->ap_iface == NULL)
7550 if (wpa_s == NULL) {
7551 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7555 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7556 DEV_PW_NFC_CONNECTION_HANDOVER &&
7557 !wpa_s->parent->p2p_oob_dev_pw) {
7558 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7561 res = wpas_ap_wps_add_nfc_pw(
7562 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7563 wpa_s->parent->p2p_oob_dev_pw,
7564 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7565 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7570 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7574 if (!params->peer ||
7575 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7578 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7579 " to join", MAC2STR(params->peer->p2p_device_addr));
7581 wpa_s->global->p2p_invite_group = wpa_s;
7582 persistent = ssid->p2p_persistent_group &&
7583 wpas_p2p_get_persistent(wpa_s->parent,
7584 params->peer->p2p_device_addr,
7585 ssid->ssid, ssid->ssid_len);
7586 wpa_s->parent->pending_invite_ssid_id = -1;
7588 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7589 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7590 ssid->ssid, ssid->ssid_len, ssid->frequency,
7591 wpa_s->global->p2p_dev_addr, persistent, 0,
7592 wpa_s->parent->p2p_oob_dev_pw_id);
7596 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7597 struct p2p_nfc_params *params,
7600 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7601 "connection handover");
7602 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7603 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7604 forced_freq, -1, 0, 1, 1);
7608 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7609 struct p2p_nfc_params *params,
7614 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7615 "connection handover");
7616 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7617 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7618 forced_freq, -1, 0, 1, 1);
7622 res = wpas_p2p_listen(wpa_s, 60);
7624 p2p_unauthorize(wpa_s->global->p2p,
7625 params->peer->p2p_device_addr);
7632 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7633 const struct wpabuf *data,
7634 int sel, int tag, int forced_freq)
7636 const u8 *pos, *end;
7638 struct p2p_nfc_params params;
7641 os_memset(¶ms, 0, sizeof(params));
7644 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7646 pos = wpabuf_head(data);
7647 end = pos + wpabuf_len(data);
7649 if (end - pos < 2) {
7650 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7654 len = WPA_GET_BE16(pos);
7656 if (len > end - pos) {
7657 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7661 params.wsc_attr = pos;
7662 params.wsc_len = len;
7665 if (end - pos < 2) {
7666 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7670 len = WPA_GET_BE16(pos);
7672 if (len > end - pos) {
7673 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7677 params.p2p_attr = pos;
7678 params.p2p_len = len;
7681 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7682 params.wsc_attr, params.wsc_len);
7683 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7684 params.p2p_attr, params.p2p_len);
7686 wpa_hexdump(MSG_DEBUG,
7687 "P2P: Ignored extra data after P2P attributes",
7691 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
7695 if (params.next_step == NO_ACTION)
7698 if (params.next_step == BOTH_GO) {
7699 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7700 MAC2STR(params.peer->p2p_device_addr));
7704 if (params.next_step == PEER_CLIENT) {
7705 if (!is_zero_ether_addr(params.go_dev_addr)) {
7706 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7707 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7709 MAC2STR(params.peer->p2p_device_addr),
7711 MAC2STR(params.go_dev_addr),
7712 wpa_ssid_txt(params.go_ssid,
7713 params.go_ssid_len));
7715 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7716 "peer=" MACSTR " freq=%d",
7717 MAC2STR(params.peer->p2p_device_addr),
7723 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7724 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7725 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7729 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7730 wpa_s->p2p_oob_dev_pw = NULL;
7732 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7733 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7738 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7739 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7740 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7741 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7742 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7743 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7744 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7748 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7749 "peer OOB Device Password Id %u", id);
7752 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7753 "Device Password Id %u", id);
7754 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7755 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7756 params.oob_dev_pw_len -
7757 WPS_OOB_PUBKEY_HASH_LEN - 2);
7758 wpa_s->p2p_oob_dev_pw_id = id;
7759 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7760 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7761 params.oob_dev_pw_len -
7762 WPS_OOB_PUBKEY_HASH_LEN - 2);
7763 if (wpa_s->p2p_oob_dev_pw == NULL)
7766 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7767 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7768 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7771 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7772 "without Device Password");
7773 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7776 switch (params.next_step) {
7780 /* already covered above */
7783 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
7785 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
7787 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
7789 /* TODO: use own OOB Dev Pw */
7790 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
7797 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7798 const struct wpabuf *data, int forced_freq)
7800 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7803 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7807 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7808 const struct wpabuf *req,
7809 const struct wpabuf *sel, int forced_freq)
7814 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7817 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7819 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7820 wpabuf_head(req), wpabuf_len(req));
7821 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7822 wpabuf_head(sel), wpabuf_len(sel));
7824 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7825 tmp = ndef_parse_p2p(init ? sel : req);
7827 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7831 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7839 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7842 int go_intent = wpa_s->conf->p2p_go_intent;
7843 struct wpa_supplicant *iface;
7845 if (wpa_s->global->p2p == NULL)
7849 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7850 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7852 if (!iface->ap_iface)
7854 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7856 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7858 if (wpa_s->p2p_nfc_tag_enabled)
7859 wpas_p2p_remove_pending_group_interface(wpa_s);
7860 wpa_s->p2p_nfc_tag_enabled = 0;
7864 if (wpa_s->global->p2p_disabled)
7867 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7868 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7869 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7870 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7871 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7872 "to allow static handover cases");
7876 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7878 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7879 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7880 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7881 if (wpa_s->p2p_oob_dev_pw == NULL)
7883 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7885 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
7886 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
7888 * P2P Group Interface present and the command came on group
7889 * interface, so enable the token for the current interface.
7891 wpa_s->create_p2p_iface = 0;
7893 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7896 if (wpa_s->create_p2p_iface) {
7897 enum wpa_driver_if_type iftype;
7898 /* Prepare to add a new interface for the group */
7899 iftype = WPA_IF_P2P_GROUP;
7900 if (go_intent == 15)
7901 iftype = WPA_IF_P2P_GO;
7902 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7903 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7904 "interface for the group");
7908 if_addr = wpa_s->pending_interface_addr;
7910 if_addr = wpa_s->own_addr;
7912 wpa_s->p2p_nfc_tag_enabled = enabled;
7914 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7915 struct hostapd_data *hapd;
7916 if (iface->ap_iface == NULL)
7918 hapd = iface->ap_iface->bss[0];
7919 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7920 hapd->conf->wps_nfc_dh_pubkey =
7921 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7922 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7923 hapd->conf->wps_nfc_dh_privkey =
7924 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7925 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7926 hapd->conf->wps_nfc_dev_pw =
7927 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7928 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7930 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7931 wpa_dbg(iface, MSG_DEBUG,
7932 "P2P: Failed to enable NFC Tag for GO");
7935 p2p_set_authorized_oob_dev_pw_id(
7936 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
7942 #endif /* CONFIG_WPS_NFC */
7945 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
7946 struct wpa_used_freq_data *freqs,
7949 u8 curr_chan, cand, chan;
7952 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
7953 for (i = 0, cand = 0; i < num; i++) {
7954 ieee80211_freq_to_chan(freqs[i].freq, &chan);
7955 if (curr_chan == chan) {
7960 if (chan == 1 || chan == 6 || chan == 11)
7965 wpa_dbg(wpa_s, MSG_DEBUG,
7966 "P2P: Update Listen channel to %u baased on operating channel",
7968 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
7973 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
7975 struct wpa_used_freq_data *freqs;
7976 unsigned int num = wpa_s->num_multichan_concurrent;
7978 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7982 * If possible, optimize the Listen channel to be a channel that is
7983 * already used by one of the other interfaces.
7985 if (!wpa_s->conf->p2p_optimize_listen_chan)
7988 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
7991 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7995 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7997 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
8002 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
8004 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
8005 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
8006 "the management interface is being removed");
8007 wpas_p2p_deinit_global(wpa_s->global);
8012 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
8014 if (wpa_s->ap_iface->bss)
8015 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
8016 wpas_p2p_group_deinit(wpa_s);