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 int 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_scan(void *ctx, enum p2p_scan_type type, int freq,
283 unsigned int num_req_dev_types,
284 const u8 *req_dev_types, const u8 *dev_id, u16 pw_id)
286 struct wpa_supplicant *wpa_s = ctx;
287 struct wpa_driver_scan_params *params = NULL;
288 struct wpabuf *wps_ie, *ies;
292 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
295 if (wpa_s->p2p_scan_work) {
296 wpa_dbg(wpa_s, MSG_INFO, "P2P: Reject scan trigger since one is already pending");
300 params = os_zalloc(sizeof(*params));
304 /* P2P Wildcard SSID */
305 params->num_ssids = 1;
306 n = os_malloc(P2P_WILDCARD_SSID_LEN);
309 os_memcpy(n, P2P_WILDCARD_SSID, P2P_WILDCARD_SSID_LEN);
310 params->ssids[0].ssid = n;
311 params->ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
313 wpa_s->wps->dev.p2p = 1;
314 wps_ie = wps_build_probe_req_ie(pw_id, &wpa_s->wps->dev,
315 wpa_s->wps->uuid, WPS_REQ_ENROLLEE,
316 num_req_dev_types, req_dev_types);
320 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
321 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
326 wpabuf_put_buf(ies, wps_ie);
329 p2p_scan_ie(wpa_s->global->p2p, ies, dev_id);
331 params->p2p_probe = 1;
332 n = os_malloc(wpabuf_len(ies));
337 os_memcpy(n, wpabuf_head(ies), wpabuf_len(ies));
338 params->extra_ies = n;
339 params->extra_ies_len = wpabuf_len(ies);
343 case P2P_SCAN_SOCIAL:
344 params->freqs = os_malloc(4 * sizeof(int));
345 if (params->freqs == NULL)
347 params->freqs[0] = 2412;
348 params->freqs[1] = 2437;
349 params->freqs[2] = 2462;
350 params->freqs[3] = 0;
354 case P2P_SCAN_SOCIAL_PLUS_ONE:
355 params->freqs = os_malloc(5 * sizeof(int));
356 if (params->freqs == NULL)
358 params->freqs[0] = 2412;
359 params->freqs[1] = 2437;
360 params->freqs[2] = 2462;
361 params->freqs[3] = freq;
362 params->freqs[4] = 0;
366 radio_remove_works(wpa_s, "p2p-scan", 0);
367 if (radio_add_work(wpa_s, 0, "p2p-scan", 0, wpas_p2p_trigger_scan_cb,
373 wpa_scan_free_params(params);
378 static enum wpa_driver_if_type wpas_p2p_if_type(int p2p_group_interface)
380 switch (p2p_group_interface) {
381 case P2P_GROUP_INTERFACE_PENDING:
382 return WPA_IF_P2P_GROUP;
383 case P2P_GROUP_INTERFACE_GO:
384 return WPA_IF_P2P_GO;
385 case P2P_GROUP_INTERFACE_CLIENT:
386 return WPA_IF_P2P_CLIENT;
389 return WPA_IF_P2P_GROUP;
393 static struct wpa_supplicant * wpas_get_p2p_group(struct wpa_supplicant *wpa_s,
395 size_t ssid_len, int *go)
399 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
400 for (s = wpa_s->conf->ssid; s; s = s->next) {
401 if (s->disabled != 0 || !s->p2p_group ||
402 s->ssid_len != ssid_len ||
403 os_memcmp(ssid, s->ssid, ssid_len) != 0)
405 if (s->mode == WPAS_MODE_P2P_GO &&
406 s != wpa_s->current_ssid)
409 *go = s->mode == WPAS_MODE_P2P_GO;
418 static int wpas_p2p_group_delete(struct wpa_supplicant *wpa_s,
419 enum p2p_group_removal_reason removal_reason)
421 struct wpa_ssid *ssid;
425 ssid = wpa_s->current_ssid;
428 * The current SSID was not known, but there may still be a
429 * pending P2P group interface waiting for provisioning or a
430 * P2P group that is trying to reconnect.
432 ssid = wpa_s->conf->ssid;
434 if (ssid->p2p_group && ssid->disabled != 2)
439 wpa_s->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)
441 wpa_printf(MSG_ERROR, "P2P: P2P group interface "
446 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO)
448 else if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT ||
449 (ssid && ssid->mode == WPAS_MODE_INFRA)) {
450 wpa_s->reassociate = 0;
451 wpa_s->disconnected = 1;
452 wpa_supplicant_deauthenticate(wpa_s,
453 WLAN_REASON_DEAUTH_LEAVING);
457 if (wpa_s->cross_connect_in_use) {
458 wpa_s->cross_connect_in_use = 0;
459 wpa_msg_global(wpa_s->parent, MSG_INFO,
460 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
461 wpa_s->ifname, wpa_s->cross_connect_uplink);
463 switch (removal_reason) {
464 case P2P_GROUP_REMOVAL_REQUESTED:
465 reason = " reason=REQUESTED";
467 case P2P_GROUP_REMOVAL_FORMATION_FAILED:
468 reason = " reason=FORMATION_FAILED";
470 case P2P_GROUP_REMOVAL_IDLE_TIMEOUT:
471 reason = " reason=IDLE";
473 case P2P_GROUP_REMOVAL_UNAVAILABLE:
474 reason = " reason=UNAVAILABLE";
476 case P2P_GROUP_REMOVAL_GO_ENDING_SESSION:
477 reason = " reason=GO_ENDING_SESSION";
479 case P2P_GROUP_REMOVAL_PSK_FAILURE:
480 reason = " reason=PSK_FAILURE";
482 case P2P_GROUP_REMOVAL_FREQ_CONFLICT:
483 reason = " reason=FREQ_CONFLICT";
489 if (removal_reason != P2P_GROUP_REMOVAL_SILENT) {
490 wpa_msg_global(wpa_s->parent, MSG_INFO,
491 P2P_EVENT_GROUP_REMOVED "%s %s%s",
492 wpa_s->ifname, gtype, reason);
495 if (eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL) > 0)
496 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group freq_conflict timeout");
497 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
498 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
499 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
500 wpa_s->parent, NULL) > 0) {
501 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group formation "
503 wpa_s->p2p_in_provisioning = 0;
506 wpa_s->p2p_in_invitation = 0;
509 * Make sure wait for the first client does not remain active after the
510 * group has been removed.
512 wpa_s->global->p2p_go_wait_client.sec = 0;
514 if (removal_reason != P2P_GROUP_REMOVAL_SILENT && ssid)
515 wpas_notify_p2p_group_removed(wpa_s, ssid, gtype);
517 if (wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE) {
518 struct wpa_global *global;
520 enum wpa_driver_if_type type;
521 wpa_printf(MSG_DEBUG, "P2P: Remove group interface %s",
523 global = wpa_s->global;
524 ifname = os_strdup(wpa_s->ifname);
525 type = wpas_p2p_if_type(wpa_s->p2p_group_interface);
526 wpa_supplicant_remove_iface(wpa_s->global, wpa_s, 0);
527 wpa_s = global->ifaces;
529 wpa_drv_if_remove(wpa_s, type, ifname);
534 if (!wpa_s->p2p_go_group_formation_completed) {
535 wpa_s->global->p2p_group_formation = NULL;
536 wpa_s->p2p_in_provisioning = 0;
539 wpa_s->show_group_started = 0;
540 os_free(wpa_s->go_params);
541 wpa_s->go_params = NULL;
543 wpa_s->waiting_presence_resp = 0;
545 wpa_printf(MSG_DEBUG, "P2P: Remove temporary group network");
546 if (ssid && (ssid->p2p_group ||
547 ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION ||
548 (ssid->key_mgmt & WPA_KEY_MGMT_WPS))) {
550 if (ssid == wpa_s->current_ssid) {
551 wpa_sm_set_config(wpa_s->wpa, NULL);
552 eapol_sm_notify_config(wpa_s->eapol, NULL, NULL);
553 wpa_s->current_ssid = NULL;
556 * Networks objects created during any P2P activities are not
557 * exposed out as they might/will confuse certain non-P2P aware
558 * applications since these network objects won't behave like
561 * Likewise, we don't send out network removed signals for such
564 wpa_config_remove_network(wpa_s->conf, id);
565 wpa_supplicant_clear_status(wpa_s);
566 wpa_supplicant_cancel_sched_scan(wpa_s);
568 wpa_printf(MSG_DEBUG, "P2P: Temporary group network not "
572 wpa_supplicant_ap_deinit(wpa_s);
574 wpa_drv_deinit_p2p_cli(wpa_s);
580 static int wpas_p2p_persistent_group(struct wpa_supplicant *wpa_s,
582 const u8 *ssid, size_t ssid_len)
590 if (wpa_s->go_params)
591 bssid = wpa_s->go_params->peer_interface_addr;
593 bssid = wpa_s->bssid;
595 bss = wpa_bss_get(wpa_s, bssid, ssid, ssid_len);
596 if (bss == NULL && wpa_s->go_params &&
597 !is_zero_ether_addr(wpa_s->go_params->peer_device_addr))
598 bss = wpa_bss_get_p2p_dev_addr(
599 wpa_s, wpa_s->go_params->peer_device_addr);
601 u8 iface_addr[ETH_ALEN];
602 if (p2p_get_interface_addr(wpa_s->global->p2p, bssid,
604 bss = wpa_bss_get(wpa_s, iface_addr, ssid, ssid_len);
607 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
608 "group is persistent - BSS " MACSTR " not found",
613 p2p = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
615 p2p = wpa_bss_get_vendor_ie_multi_beacon(bss,
618 wpa_printf(MSG_DEBUG, "P2P: Could not figure out whether "
619 "group is persistent - BSS " MACSTR
620 " did not include P2P IE", MAC2STR(bssid));
621 wpa_hexdump(MSG_DEBUG, "P2P: Probe Response IEs",
622 (u8 *) (bss + 1), bss->ie_len);
623 wpa_hexdump(MSG_DEBUG, "P2P: Beacon IEs",
624 ((u8 *) bss + 1) + bss->ie_len,
629 group_capab = p2p_get_group_capab(p2p);
630 addr = p2p_get_go_dev_addr(p2p);
631 wpa_printf(MSG_DEBUG, "P2P: Checking whether group is persistent: "
632 "group_capab=0x%x", group_capab);
634 os_memcpy(go_dev_addr, addr, ETH_ALEN);
635 wpa_printf(MSG_DEBUG, "P2P: GO Device Address " MACSTR,
638 os_memset(go_dev_addr, 0, ETH_ALEN);
641 wpa_printf(MSG_DEBUG, "P2P: BSS " MACSTR " group_capab=0x%x "
642 "go_dev_addr=" MACSTR,
643 MAC2STR(bssid), group_capab, MAC2STR(go_dev_addr));
645 return group_capab & P2P_GROUP_CAPAB_PERSISTENT_GROUP;
649 static int wpas_p2p_store_persistent_group(struct wpa_supplicant *wpa_s,
650 struct wpa_ssid *ssid,
651 const u8 *go_dev_addr)
656 wpa_printf(MSG_DEBUG, "P2P: Storing credentials for a persistent "
657 "group (GO Dev Addr " MACSTR ")", MAC2STR(go_dev_addr));
658 for (s = wpa_s->conf->ssid; s; s = s->next) {
659 if (s->disabled == 2 &&
660 os_memcmp(go_dev_addr, s->bssid, ETH_ALEN) == 0 &&
661 s->ssid_len == ssid->ssid_len &&
662 os_memcmp(ssid->ssid, s->ssid, ssid->ssid_len) == 0)
667 wpa_printf(MSG_DEBUG, "P2P: Update existing persistent group "
669 if (ssid->passphrase && !s->passphrase)
671 else if (ssid->passphrase && s->passphrase &&
672 os_strcmp(ssid->passphrase, s->passphrase) != 0)
675 wpa_printf(MSG_DEBUG, "P2P: Create a new persistent group "
678 s = wpa_config_add_network(wpa_s->conf);
683 * Instead of network_added we emit persistent_group_added
684 * notification. Also to keep the defense checks in
685 * persistent_group obj registration method, we set the
686 * relevant flags in s to designate it as a persistent group.
689 s->p2p_persistent_group = 1;
690 wpas_notify_persistent_group_added(wpa_s, s);
691 wpa_config_set_network_defaults(s);
695 s->p2p_persistent_group = 1;
698 os_memcpy(s->bssid, go_dev_addr, ETH_ALEN);
699 s->mode = ssid->mode;
700 s->auth_alg = WPA_AUTH_ALG_OPEN;
701 s->key_mgmt = WPA_KEY_MGMT_PSK;
702 s->proto = WPA_PROTO_RSN;
703 s->pairwise_cipher = WPA_CIPHER_CCMP;
705 if (ssid->passphrase) {
706 os_free(s->passphrase);
707 s->passphrase = os_strdup(ssid->passphrase);
711 os_memcpy(s->psk, ssid->psk, 32);
713 if (s->passphrase && !s->psk_set)
714 wpa_config_update_psk(s);
715 if (s->ssid == NULL || s->ssid_len < ssid->ssid_len) {
717 s->ssid = os_malloc(ssid->ssid_len);
720 s->ssid_len = ssid->ssid_len;
721 os_memcpy(s->ssid, ssid->ssid, s->ssid_len);
723 if (ssid->mode == WPAS_MODE_P2P_GO && wpa_s->global->add_psk) {
724 dl_list_add(&s->psk_list, &wpa_s->global->add_psk->list);
725 wpa_s->global->add_psk = NULL;
729 if (changed && wpa_s->conf->update_config &&
730 wpa_config_write(wpa_s->confname, wpa_s->conf)) {
731 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
738 static void wpas_p2p_add_persistent_group_client(struct wpa_supplicant *wpa_s,
741 struct wpa_ssid *ssid, *s;
746 ssid = wpa_s->current_ssid;
747 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
748 !ssid->p2p_persistent_group)
751 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
752 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
755 if (s->ssid_len == ssid->ssid_len &&
756 os_memcmp(s->ssid, ssid->ssid, s->ssid_len) == 0)
763 for (i = 0; s->p2p_client_list && i < s->num_p2p_clients; i++) {
764 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN, addr,
768 if (i == s->num_p2p_clients - 1)
769 return; /* already the most recent entry */
771 /* move the entry to mark it most recent */
772 os_memmove(s->p2p_client_list + i * ETH_ALEN,
773 s->p2p_client_list + (i + 1) * ETH_ALEN,
774 (s->num_p2p_clients - i - 1) * ETH_ALEN);
775 os_memcpy(s->p2p_client_list +
776 (s->num_p2p_clients - 1) * ETH_ALEN, addr, ETH_ALEN);
781 if (!found && s->num_p2p_clients < P2P_MAX_STORED_CLIENTS) {
782 n = os_realloc_array(s->p2p_client_list,
783 s->num_p2p_clients + 1, ETH_ALEN);
786 os_memcpy(n + s->num_p2p_clients * ETH_ALEN, addr, ETH_ALEN);
787 s->p2p_client_list = n;
788 s->num_p2p_clients++;
789 } else if (!found && s->p2p_client_list) {
790 /* Not enough room for an additional entry - drop the oldest
792 os_memmove(s->p2p_client_list,
793 s->p2p_client_list + ETH_ALEN,
794 (s->num_p2p_clients - 1) * ETH_ALEN);
795 os_memcpy(s->p2p_client_list +
796 (s->num_p2p_clients - 1) * ETH_ALEN,
800 if (wpa_s->parent->conf->update_config &&
801 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
802 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
806 static void wpas_p2p_group_started(struct wpa_supplicant *wpa_s,
807 int go, struct wpa_ssid *ssid, int freq,
808 const u8 *psk, const char *passphrase,
809 const u8 *go_dev_addr, int persistent,
812 const char *ssid_txt;
816 wpa_snprintf_hex(psk_txt, sizeof(psk_txt), psk, 32);
821 ssid_txt = wpa_ssid_txt(ssid->ssid, ssid->ssid_len);
825 if (passphrase && passphrase[0] == '\0')
828 wpa_msg_global(wpa_s->parent, MSG_INFO,
829 P2P_EVENT_GROUP_STARTED
830 "%s %s ssid=\"%s\" freq=%d%s%s%s%s%s go_dev_addr="
832 wpa_s->ifname, go ? "GO" : "client", ssid_txt, freq,
833 psk ? " psk=" : "", psk_txt,
834 passphrase ? " passphrase=\"" : "",
835 passphrase ? passphrase : "",
836 passphrase ? "\"" : "",
837 MAC2STR(go_dev_addr),
838 persistent ? " [PERSISTENT]" : "", extra);
842 static void wpas_group_formation_completed(struct wpa_supplicant *wpa_s,
845 struct wpa_ssid *ssid;
848 u8 go_dev_addr[ETH_ALEN];
852 * This callback is likely called for the main interface. Update wpa_s
853 * to use the group interface if a new interface was created for the
856 if (wpa_s->global->p2p_group_formation)
857 wpa_s = wpa_s->global->p2p_group_formation;
858 if (wpa_s->p2p_go_group_formation_completed) {
859 wpa_s->global->p2p_group_formation = NULL;
860 wpa_s->p2p_in_provisioning = 0;
862 wpa_s->p2p_in_invitation = 0;
865 wpa_msg_global(wpa_s->parent, MSG_INFO,
866 P2P_EVENT_GROUP_FORMATION_FAILURE);
867 wpas_p2p_group_delete(wpa_s,
868 P2P_GROUP_REMOVAL_FORMATION_FAILED);
872 wpa_msg_global(wpa_s->parent, MSG_INFO,
873 P2P_EVENT_GROUP_FORMATION_SUCCESS);
875 ssid = wpa_s->current_ssid;
876 if (ssid && ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
877 ssid->mode = WPAS_MODE_P2P_GO;
878 p2p_group_notif_formation_done(wpa_s->p2p_group);
879 wpa_supplicant_ap_mac_addr_filter(wpa_s, NULL);
884 client = ssid->mode == WPAS_MODE_INFRA;
885 if (ssid->mode == WPAS_MODE_P2P_GO) {
886 persistent = ssid->p2p_persistent_group;
887 os_memcpy(go_dev_addr, wpa_s->global->p2p_dev_addr,
890 persistent = wpas_p2p_persistent_group(wpa_s,
895 client = wpa_s->p2p_group_interface ==
896 P2P_GROUP_INTERFACE_CLIENT;
897 os_memset(go_dev_addr, 0, ETH_ALEN);
900 wpa_s->show_group_started = 0;
903 * Indicate event only after successfully completed 4-way
904 * handshake, i.e., when the interface is ready for data
907 wpa_s->show_group_started = 1;
909 wpas_p2p_group_started(wpa_s, 1, ssid,
910 ssid ? ssid->frequency : 0,
911 ssid && ssid->passphrase == NULL &&
912 ssid->psk_set ? ssid->psk : NULL,
913 ssid ? ssid->passphrase : NULL,
914 go_dev_addr, persistent, "");
915 wpas_p2p_cross_connect_setup(wpa_s);
916 wpas_p2p_set_group_idle_timeout(wpa_s);
920 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
923 os_free(wpa_s->global->add_psk);
924 wpa_s->global->add_psk = NULL;
926 if (network_id < 0 && ssid)
927 network_id = ssid->id;
929 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
930 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
935 struct send_action_work {
941 unsigned int wait_time;
946 static void wpas_p2p_send_action_work_timeout(void *eloop_ctx,
949 struct wpa_supplicant *wpa_s = eloop_ctx;
951 if (!wpa_s->p2p_send_action_work)
954 wpa_printf(MSG_DEBUG, "P2P: Send Action frame radio work timed out");
955 os_free(wpa_s->p2p_send_action_work->ctx);
956 radio_work_done(wpa_s->p2p_send_action_work);
957 wpa_s->p2p_send_action_work = NULL;
961 static void wpas_p2p_send_action_tx_status(struct wpa_supplicant *wpa_s,
963 const u8 *dst, const u8 *src,
965 const u8 *data, size_t data_len,
966 enum offchannel_send_action_result
969 enum p2p_send_action_result res = P2P_SEND_ACTION_SUCCESS;
971 if (wpa_s->p2p_send_action_work) {
972 struct send_action_work *awork;
973 awork = wpa_s->p2p_send_action_work->ctx;
974 if (awork->wait_time == 0) {
976 radio_work_done(wpa_s->p2p_send_action_work);
977 wpa_s->p2p_send_action_work = NULL;
980 * In theory, this should not be needed, but number of
981 * places in the P2P code is still using non-zero wait
982 * time for the last Action frame in the sequence and
983 * some of these do not call send_action_done().
985 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
987 eloop_register_timeout(
988 0, awork->wait_time * 1000,
989 wpas_p2p_send_action_work_timeout,
994 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
998 case OFFCHANNEL_SEND_ACTION_SUCCESS:
999 res = P2P_SEND_ACTION_SUCCESS;
1001 case OFFCHANNEL_SEND_ACTION_NO_ACK:
1002 res = P2P_SEND_ACTION_NO_ACK;
1004 case OFFCHANNEL_SEND_ACTION_FAILED:
1005 res = P2P_SEND_ACTION_FAILED;
1009 p2p_send_action_cb(wpa_s->global->p2p, freq, dst, src, bssid, res);
1011 if (result != OFFCHANNEL_SEND_ACTION_SUCCESS &&
1012 wpa_s->pending_pd_before_join &&
1013 (os_memcmp(dst, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
1014 os_memcmp(dst, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0) &&
1015 wpa_s->p2p_fallback_to_go_neg) {
1016 wpa_s->pending_pd_before_join = 0;
1017 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No ACK for PD Req "
1018 "during p2p_connect-auto");
1019 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
1025 static void wpas_send_action_cb(struct wpa_radio_work *work, int deinit)
1027 struct wpa_supplicant *wpa_s = work->wpa_s;
1028 struct send_action_work *awork = work->ctx;
1031 if (work->started) {
1032 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1034 wpa_s->p2p_send_action_work = NULL;
1035 offchannel_send_action_done(wpa_s);
1041 if (offchannel_send_action(wpa_s, awork->freq, awork->dst, awork->src,
1042 awork->bssid, awork->buf, awork->len,
1044 wpas_p2p_send_action_tx_status, 1) < 0) {
1046 radio_work_done(work);
1049 wpa_s->p2p_send_action_work = work;
1053 static int wpas_send_action_work(struct wpa_supplicant *wpa_s,
1054 unsigned int freq, const u8 *dst,
1055 const u8 *src, const u8 *bssid, const u8 *buf,
1056 size_t len, unsigned int wait_time)
1058 struct send_action_work *awork;
1060 if (wpa_s->p2p_send_action_work) {
1061 wpa_printf(MSG_DEBUG, "P2P: Cannot schedule new p2p-send-action work since one is already pending");
1065 awork = os_zalloc(sizeof(*awork) + len);
1070 os_memcpy(awork->dst, dst, ETH_ALEN);
1071 os_memcpy(awork->src, src, ETH_ALEN);
1072 os_memcpy(awork->bssid, bssid, ETH_ALEN);
1074 awork->wait_time = wait_time;
1075 os_memcpy(awork->buf, buf, len);
1077 if (radio_add_work(wpa_s, freq, "p2p-send-action", 0,
1078 wpas_send_action_cb, awork) < 0) {
1087 static int wpas_send_action(void *ctx, unsigned int freq, const u8 *dst,
1088 const u8 *src, const u8 *bssid, const u8 *buf,
1089 size_t len, unsigned int wait_time)
1091 struct wpa_supplicant *wpa_s = ctx;
1092 int listen_freq = -1, send_freq = -1;
1094 if (wpa_s->p2p_listen_work)
1095 listen_freq = wpa_s->p2p_listen_work->freq;
1096 if (wpa_s->p2p_send_action_work)
1097 send_freq = wpa_s->p2p_send_action_work->freq;
1098 if (listen_freq != (int) freq && send_freq != (int) freq) {
1099 wpa_printf(MSG_DEBUG, "P2P: Schedule new radio work for Action frame TX (listen_freq=%d send_freq=%d)",
1100 listen_freq, send_freq);
1101 return wpas_send_action_work(wpa_s, freq, dst, src, bssid, buf,
1105 wpa_printf(MSG_DEBUG, "P2P: Use ongoing radio work for Action frame TX");
1106 return offchannel_send_action(wpa_s, freq, dst, src, bssid, buf, len,
1108 wpas_p2p_send_action_tx_status, 1);
1112 static void wpas_send_action_done(void *ctx)
1114 struct wpa_supplicant *wpa_s = ctx;
1116 if (wpa_s->p2p_send_action_work) {
1117 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout,
1119 os_free(wpa_s->p2p_send_action_work->ctx);
1120 radio_work_done(wpa_s->p2p_send_action_work);
1121 wpa_s->p2p_send_action_work = NULL;
1124 offchannel_send_action_done(wpa_s);
1128 static int wpas_copy_go_neg_results(struct wpa_supplicant *wpa_s,
1129 struct p2p_go_neg_results *params)
1131 if (wpa_s->go_params == NULL) {
1132 wpa_s->go_params = os_malloc(sizeof(*params));
1133 if (wpa_s->go_params == NULL)
1136 os_memcpy(wpa_s->go_params, params, sizeof(*params));
1141 static void wpas_start_wps_enrollee(struct wpa_supplicant *wpa_s,
1142 struct p2p_go_neg_results *res)
1144 wpa_printf(MSG_DEBUG, "P2P: Start WPS Enrollee for peer " MACSTR
1145 " dev_addr " MACSTR " wps_method %d",
1146 MAC2STR(res->peer_interface_addr),
1147 MAC2STR(res->peer_device_addr), res->wps_method);
1148 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Start WPS Enrollee for SSID",
1149 res->ssid, res->ssid_len);
1150 wpa_supplicant_ap_deinit(wpa_s);
1151 wpas_copy_go_neg_results(wpa_s, res);
1152 if (res->wps_method == WPS_PBC) {
1153 wpas_wps_start_pbc(wpa_s, res->peer_interface_addr, 1);
1154 #ifdef CONFIG_WPS_NFC
1155 } else if (res->wps_method == WPS_NFC) {
1156 wpas_wps_start_nfc(wpa_s, res->peer_device_addr,
1157 res->peer_interface_addr,
1158 wpa_s->parent->p2p_oob_dev_pw,
1159 wpa_s->parent->p2p_oob_dev_pw_id, 1,
1160 wpa_s->parent->p2p_oob_dev_pw_id ==
1161 DEV_PW_NFC_CONNECTION_HANDOVER ?
1162 wpa_s->parent->p2p_peer_oob_pubkey_hash :
1165 #endif /* CONFIG_WPS_NFC */
1167 u16 dev_pw_id = DEV_PW_DEFAULT;
1168 if (wpa_s->p2p_wps_method == WPS_PIN_KEYPAD)
1169 dev_pw_id = DEV_PW_REGISTRAR_SPECIFIED;
1170 wpas_wps_start_pin(wpa_s, res->peer_interface_addr,
1171 wpa_s->p2p_pin, 1, dev_pw_id);
1176 static void wpas_p2p_add_psk_list(struct wpa_supplicant *wpa_s,
1177 struct wpa_ssid *ssid)
1179 struct wpa_ssid *persistent;
1180 struct psk_list_entry *psk;
1181 struct hostapd_data *hapd;
1183 if (!wpa_s->ap_iface)
1186 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
1188 if (persistent == NULL)
1191 hapd = wpa_s->ap_iface->bss[0];
1193 dl_list_for_each(psk, &persistent->psk_list, struct psk_list_entry,
1195 struct hostapd_wpa_psk *hpsk;
1197 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add persistent group PSK entry for "
1199 MAC2STR(psk->addr), psk->p2p);
1200 hpsk = os_zalloc(sizeof(*hpsk));
1203 os_memcpy(hpsk->psk, psk->psk, PMK_LEN);
1205 os_memcpy(hpsk->p2p_dev_addr, psk->addr, ETH_ALEN);
1207 os_memcpy(hpsk->addr, psk->addr, ETH_ALEN);
1208 hpsk->next = hapd->conf->ssid.wpa_psk;
1209 hapd->conf->ssid.wpa_psk = hpsk;
1214 static void p2p_go_configured(void *ctx, void *data)
1216 struct wpa_supplicant *wpa_s = ctx;
1217 struct p2p_go_neg_results *params = data;
1218 struct wpa_ssid *ssid;
1219 int network_id = -1;
1221 ssid = wpa_s->current_ssid;
1222 if (ssid && ssid->mode == WPAS_MODE_P2P_GO) {
1223 wpa_printf(MSG_DEBUG, "P2P: Group setup without provisioning");
1224 if (wpa_s->global->p2p_group_formation == wpa_s)
1225 wpa_s->global->p2p_group_formation = NULL;
1226 wpas_p2p_group_started(wpa_s, 1, ssid, ssid->frequency,
1227 params->passphrase[0] == '\0' ?
1230 wpa_s->global->p2p_dev_addr,
1231 params->persistent_group, "");
1233 os_get_reltime(&wpa_s->global->p2p_go_wait_client);
1234 if (params->persistent_group) {
1235 network_id = wpas_p2p_store_persistent_group(
1236 wpa_s->parent, ssid,
1237 wpa_s->global->p2p_dev_addr);
1238 wpas_p2p_add_psk_list(wpa_s, ssid);
1241 network_id = ssid->id;
1242 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 0);
1243 wpas_p2p_cross_connect_setup(wpa_s);
1244 wpas_p2p_set_group_idle_timeout(wpa_s);
1246 if (wpa_s->p2p_first_connection_timeout) {
1247 wpa_dbg(wpa_s, MSG_DEBUG,
1248 "P2P: Start group formation timeout of %d seconds until first data connection on GO",
1249 wpa_s->p2p_first_connection_timeout);
1250 wpa_s->p2p_go_group_formation_completed = 0;
1251 wpa_s->global->p2p_group_formation = wpa_s;
1252 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1253 wpa_s->parent, NULL);
1254 eloop_register_timeout(
1255 wpa_s->p2p_first_connection_timeout, 0,
1256 wpas_p2p_group_formation_timeout,
1257 wpa_s->parent, NULL);
1263 wpa_printf(MSG_DEBUG, "P2P: Setting up WPS for GO provisioning");
1264 if (wpa_supplicant_ap_mac_addr_filter(wpa_s,
1265 params->peer_interface_addr)) {
1266 wpa_printf(MSG_DEBUG, "P2P: Failed to setup MAC address "
1270 if (params->wps_method == WPS_PBC) {
1271 wpa_supplicant_ap_wps_pbc(wpa_s, params->peer_interface_addr,
1272 params->peer_device_addr);
1273 #ifdef CONFIG_WPS_NFC
1274 } else if (params->wps_method == WPS_NFC) {
1275 if (wpa_s->parent->p2p_oob_dev_pw_id !=
1276 DEV_PW_NFC_CONNECTION_HANDOVER &&
1277 !wpa_s->parent->p2p_oob_dev_pw) {
1278 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
1281 wpas_ap_wps_add_nfc_pw(
1282 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
1283 wpa_s->parent->p2p_oob_dev_pw,
1284 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
1285 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
1286 #endif /* CONFIG_WPS_NFC */
1287 } else if (wpa_s->p2p_pin[0])
1288 wpa_supplicant_ap_wps_pin(wpa_s, params->peer_interface_addr,
1289 wpa_s->p2p_pin, NULL, 0, 0);
1290 os_free(wpa_s->go_params);
1291 wpa_s->go_params = NULL;
1295 static void wpas_start_wps_go(struct wpa_supplicant *wpa_s,
1296 struct p2p_go_neg_results *params,
1297 int group_formation)
1299 struct wpa_ssid *ssid;
1301 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Starting GO");
1302 if (wpas_copy_go_neg_results(wpa_s, params) < 0) {
1303 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not copy GO Negotiation "
1308 ssid = wpa_config_add_network(wpa_s->conf);
1310 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not add network for GO");
1314 wpa_s->show_group_started = 0;
1316 wpa_config_set_network_defaults(ssid);
1317 ssid->temporary = 1;
1318 ssid->p2p_group = 1;
1319 ssid->p2p_persistent_group = params->persistent_group;
1320 ssid->mode = group_formation ? WPAS_MODE_P2P_GROUP_FORMATION :
1322 ssid->frequency = params->freq;
1323 ssid->ht40 = params->ht40;
1324 ssid->vht = params->vht;
1325 ssid->ssid = os_zalloc(params->ssid_len + 1);
1327 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
1328 ssid->ssid_len = params->ssid_len;
1330 ssid->auth_alg = WPA_AUTH_ALG_OPEN;
1331 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
1332 ssid->proto = WPA_PROTO_RSN;
1333 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
1334 if (os_strlen(params->passphrase) > 0) {
1335 ssid->passphrase = os_strdup(params->passphrase);
1336 if (ssid->passphrase == NULL) {
1337 wpa_msg_global(wpa_s, MSG_ERROR,
1338 "P2P: Failed to copy passphrase for GO");
1339 wpa_config_remove_network(wpa_s->conf, ssid->id);
1343 ssid->passphrase = NULL;
1344 ssid->psk_set = params->psk_set;
1346 os_memcpy(ssid->psk, params->psk, sizeof(ssid->psk));
1347 else if (ssid->passphrase)
1348 wpa_config_update_psk(ssid);
1349 ssid->ap_max_inactivity = wpa_s->parent->conf->p2p_go_max_inactivity;
1351 wpa_s->ap_configured_cb = p2p_go_configured;
1352 wpa_s->ap_configured_cb_ctx = wpa_s;
1353 wpa_s->ap_configured_cb_data = wpa_s->go_params;
1354 wpa_s->scan_req = NORMAL_SCAN_REQ;
1355 wpa_s->connect_without_scan = ssid;
1356 wpa_s->reassociate = 1;
1357 wpa_s->disconnected = 0;
1358 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Request scan (that will be skipped) to "
1360 wpa_supplicant_req_scan(wpa_s, 0, 0);
1364 static void wpas_p2p_clone_config(struct wpa_supplicant *dst,
1365 const struct wpa_supplicant *src)
1367 struct wpa_config *d;
1368 const struct wpa_config *s;
1373 #define C(n) if (s->n) d->n = os_strdup(s->n)
1382 os_memcpy(d->device_type, s->device_type, WPS_DEV_TYPE_LEN);
1383 os_memcpy(d->sec_device_type, s->sec_device_type,
1384 sizeof(d->sec_device_type));
1385 d->num_sec_device_types = s->num_sec_device_types;
1387 d->p2p_group_idle = s->p2p_group_idle;
1388 d->p2p_intra_bss = s->p2p_intra_bss;
1389 d->persistent_reconnect = s->persistent_reconnect;
1390 d->max_num_sta = s->max_num_sta;
1391 d->pbc_in_m1 = s->pbc_in_m1;
1392 d->ignore_old_scan_res = s->ignore_old_scan_res;
1393 d->beacon_int = s->beacon_int;
1394 d->dtim_period = s->dtim_period;
1395 d->disassoc_low_ack = s->disassoc_low_ack;
1396 d->disable_scan_offload = s->disable_scan_offload;
1398 if (s->wps_nfc_dh_privkey && s->wps_nfc_dh_pubkey) {
1399 d->wps_nfc_dh_privkey = wpabuf_dup(s->wps_nfc_dh_privkey);
1400 d->wps_nfc_dh_pubkey = wpabuf_dup(s->wps_nfc_dh_pubkey);
1405 static void wpas_p2p_get_group_ifname(struct wpa_supplicant *wpa_s,
1406 char *ifname, size_t len)
1408 char *ifname_ptr = wpa_s->ifname;
1410 if (os_strncmp(wpa_s->ifname, P2P_MGMT_DEVICE_PREFIX,
1411 os_strlen(P2P_MGMT_DEVICE_PREFIX)) == 0) {
1412 ifname_ptr = os_strrchr(wpa_s->ifname, '-') + 1;
1415 os_snprintf(ifname, len, "p2p-%s-%d", ifname_ptr, wpa_s->p2p_group_idx);
1416 if (os_strlen(ifname) >= IFNAMSIZ &&
1417 os_strlen(wpa_s->ifname) < IFNAMSIZ) {
1418 /* Try to avoid going over the IFNAMSIZ length limit */
1419 os_snprintf(ifname, len, "p2p-%d", wpa_s->p2p_group_idx);
1424 static int wpas_p2p_add_group_interface(struct wpa_supplicant *wpa_s,
1425 enum wpa_driver_if_type type)
1427 char ifname[120], force_ifname[120];
1429 if (wpa_s->pending_interface_name[0]) {
1430 wpa_printf(MSG_DEBUG, "P2P: Pending virtual interface exists "
1431 "- skip creation of a new one");
1432 if (is_zero_ether_addr(wpa_s->pending_interface_addr)) {
1433 wpa_printf(MSG_DEBUG, "P2P: Pending virtual address "
1434 "unknown?! ifname='%s'",
1435 wpa_s->pending_interface_name);
1441 wpas_p2p_get_group_ifname(wpa_s, ifname, sizeof(ifname));
1442 force_ifname[0] = '\0';
1444 wpa_printf(MSG_DEBUG, "P2P: Create a new interface %s for the group",
1446 wpa_s->p2p_group_idx++;
1448 wpa_s->pending_interface_type = type;
1449 if (wpa_drv_if_add(wpa_s, type, ifname, NULL, NULL, force_ifname,
1450 wpa_s->pending_interface_addr, NULL) < 0) {
1451 wpa_printf(MSG_ERROR, "P2P: Failed to create new group "
1456 if (force_ifname[0]) {
1457 wpa_printf(MSG_DEBUG, "P2P: Driver forced interface name %s",
1459 os_strlcpy(wpa_s->pending_interface_name, force_ifname,
1460 sizeof(wpa_s->pending_interface_name));
1462 os_strlcpy(wpa_s->pending_interface_name, ifname,
1463 sizeof(wpa_s->pending_interface_name));
1464 wpa_printf(MSG_DEBUG, "P2P: Created pending virtual interface %s addr "
1465 MACSTR, wpa_s->pending_interface_name,
1466 MAC2STR(wpa_s->pending_interface_addr));
1472 static void wpas_p2p_remove_pending_group_interface(
1473 struct wpa_supplicant *wpa_s)
1475 if (!wpa_s->pending_interface_name[0] ||
1476 is_zero_ether_addr(wpa_s->pending_interface_addr))
1477 return; /* No pending virtual interface */
1479 wpa_printf(MSG_DEBUG, "P2P: Removing pending group interface %s",
1480 wpa_s->pending_interface_name);
1481 wpa_drv_if_remove(wpa_s, wpa_s->pending_interface_type,
1482 wpa_s->pending_interface_name);
1483 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1484 wpa_s->pending_interface_name[0] = '\0';
1488 static struct wpa_supplicant *
1489 wpas_p2p_init_group_interface(struct wpa_supplicant *wpa_s, int go)
1491 struct wpa_interface iface;
1492 struct wpa_supplicant *group_wpa_s;
1494 if (!wpa_s->pending_interface_name[0]) {
1495 wpa_printf(MSG_ERROR, "P2P: No pending group interface");
1496 if (!wpas_p2p_create_iface(wpa_s))
1499 * Something has forced us to remove the pending interface; try
1500 * to create a new one and hope for the best that we will get
1501 * the same local address.
1503 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
1504 WPA_IF_P2P_CLIENT) < 0)
1508 os_memset(&iface, 0, sizeof(iface));
1509 iface.ifname = wpa_s->pending_interface_name;
1510 iface.driver = wpa_s->driver->name;
1511 if (wpa_s->conf->ctrl_interface == NULL &&
1512 wpa_s->parent != wpa_s &&
1514 (wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE))
1515 iface.ctrl_interface = wpa_s->parent->conf->ctrl_interface;
1517 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
1518 iface.driver_param = wpa_s->conf->driver_param;
1519 group_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
1520 if (group_wpa_s == NULL) {
1521 wpa_printf(MSG_ERROR, "P2P: Failed to create new "
1522 "wpa_supplicant interface");
1525 wpa_s->pending_interface_name[0] = '\0';
1526 group_wpa_s->parent = wpa_s;
1527 group_wpa_s->p2p_group_interface = go ? P2P_GROUP_INTERFACE_GO :
1528 P2P_GROUP_INTERFACE_CLIENT;
1529 wpa_s->global->p2p_group_formation = group_wpa_s;
1531 wpas_p2p_clone_config(group_wpa_s, wpa_s);
1537 static void wpas_p2p_group_formation_timeout(void *eloop_ctx,
1540 struct wpa_supplicant *wpa_s = eloop_ctx;
1541 wpa_printf(MSG_DEBUG, "P2P: Group Formation timed out");
1542 wpas_p2p_group_formation_failed(wpa_s);
1546 void wpas_p2p_group_formation_failed(struct wpa_supplicant *wpa_s)
1548 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1549 wpa_s->parent, NULL);
1550 if (wpa_s->global->p2p)
1551 p2p_group_formation_failed(wpa_s->global->p2p);
1552 wpas_group_formation_completed(wpa_s, 0);
1556 static void wpas_p2p_grpform_fail_after_wps(struct wpa_supplicant *wpa_s)
1558 wpa_printf(MSG_DEBUG, "P2P: Reject group formation due to WPS provisioning failure");
1559 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1560 wpa_s->parent, NULL);
1561 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1562 wpa_s->parent, NULL);
1563 wpa_s->global->p2p_fail_on_wps_complete = 0;
1567 void wpas_p2p_ap_setup_failed(struct wpa_supplicant *wpa_s)
1569 if (wpa_s->global->p2p_group_formation != wpa_s)
1571 /* Speed up group formation timeout since this cannot succeed */
1572 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
1573 wpa_s->parent, NULL);
1574 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
1575 wpa_s->parent, NULL);
1579 static void wpas_go_neg_completed(void *ctx, struct p2p_go_neg_results *res)
1581 struct wpa_supplicant *wpa_s = ctx;
1583 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1584 wpa_drv_cancel_remain_on_channel(wpa_s);
1585 wpa_s->off_channel_freq = 0;
1586 wpa_s->roc_waiting_drv_freq = 0;
1590 wpa_msg_global(wpa_s, MSG_INFO,
1591 P2P_EVENT_GO_NEG_FAILURE "status=%d",
1593 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1594 wpas_p2p_remove_pending_group_interface(wpa_s);
1598 if (wpa_s->p2p_go_ht40)
1600 if (wpa_s->p2p_go_vht)
1603 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_SUCCESS "role=%s "
1604 "freq=%d ht40=%d peer_dev=" MACSTR " peer_iface=" MACSTR
1606 res->role_go ? "GO" : "client", res->freq, res->ht40,
1607 MAC2STR(res->peer_device_addr),
1608 MAC2STR(res->peer_interface_addr),
1609 p2p_wps_method_text(res->wps_method));
1610 wpas_notify_p2p_go_neg_completed(wpa_s, res);
1612 if (res->role_go && wpa_s->p2p_persistent_id >= 0) {
1613 struct wpa_ssid *ssid;
1614 ssid = wpa_config_get_network(wpa_s->conf,
1615 wpa_s->p2p_persistent_id);
1616 if (ssid && ssid->disabled == 2 &&
1617 ssid->mode == WPAS_MODE_P2P_GO && ssid->passphrase) {
1618 size_t len = os_strlen(ssid->passphrase);
1619 wpa_printf(MSG_DEBUG, "P2P: Override passphrase based "
1620 "on requested persistent group");
1621 os_memcpy(res->passphrase, ssid->passphrase, len);
1622 res->passphrase[len] = '\0';
1626 if (wpa_s->create_p2p_iface) {
1627 struct wpa_supplicant *group_wpa_s =
1628 wpas_p2p_init_group_interface(wpa_s, res->role_go);
1629 if (group_wpa_s == NULL) {
1630 wpas_p2p_remove_pending_group_interface(wpa_s);
1631 eloop_cancel_timeout(wpas_p2p_long_listen_timeout,
1633 wpas_p2p_group_formation_failed(wpa_s);
1636 if (group_wpa_s != wpa_s) {
1637 os_memcpy(group_wpa_s->p2p_pin, wpa_s->p2p_pin,
1638 sizeof(group_wpa_s->p2p_pin));
1639 group_wpa_s->p2p_wps_method = wpa_s->p2p_wps_method;
1641 os_memset(wpa_s->pending_interface_addr, 0, ETH_ALEN);
1642 wpa_s->pending_interface_name[0] = '\0';
1643 group_wpa_s->p2p_in_provisioning = 1;
1646 wpas_start_wps_go(group_wpa_s, res, 1);
1648 wpas_start_wps_enrollee(group_wpa_s, res);
1650 wpa_s->p2p_in_provisioning = 1;
1651 wpa_s->global->p2p_group_formation = wpa_s;
1654 wpas_start_wps_go(wpa_s, res, 1);
1656 wpas_start_wps_enrollee(ctx, res);
1659 wpa_s->p2p_long_listen = 0;
1660 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
1662 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
1663 eloop_register_timeout(15 + res->peer_config_timeout / 100,
1664 (res->peer_config_timeout % 100) * 10000,
1665 wpas_p2p_group_formation_timeout, wpa_s, NULL);
1669 static void wpas_go_neg_req_rx(void *ctx, const u8 *src, u16 dev_passwd_id)
1671 struct wpa_supplicant *wpa_s = ctx;
1672 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_GO_NEG_REQUEST MACSTR
1673 " dev_passwd_id=%u", MAC2STR(src), dev_passwd_id);
1675 wpas_notify_p2p_go_neg_req(wpa_s, src, dev_passwd_id);
1679 static void wpas_dev_found(void *ctx, const u8 *addr,
1680 const struct p2p_peer_info *info,
1683 #ifndef CONFIG_NO_STDOUT_DEBUG
1684 struct wpa_supplicant *wpa_s = ctx;
1685 char devtype[WPS_DEV_TYPE_BUFSIZE];
1686 char *wfd_dev_info_hex = NULL;
1688 #ifdef CONFIG_WIFI_DISPLAY
1689 wfd_dev_info_hex = wifi_display_subelem_hex(info->wfd_subelems,
1690 WFD_SUBELEM_DEVICE_INFO);
1691 #endif /* CONFIG_WIFI_DISPLAY */
1693 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_FOUND MACSTR
1694 " p2p_dev_addr=" MACSTR
1695 " pri_dev_type=%s name='%s' config_methods=0x%x "
1696 "dev_capab=0x%x group_capab=0x%x%s%s%s",
1697 MAC2STR(addr), MAC2STR(info->p2p_device_addr),
1698 wps_dev_type_bin2str(info->pri_dev_type, devtype,
1700 info->device_name, info->config_methods,
1701 info->dev_capab, info->group_capab,
1702 wfd_dev_info_hex ? " wfd_dev_info=0x" : "",
1703 wfd_dev_info_hex ? wfd_dev_info_hex : "",
1704 info->vendor_elems ? " vendor_elems=1" : "");
1706 os_free(wfd_dev_info_hex);
1707 #endif /* CONFIG_NO_STDOUT_DEBUG */
1709 wpas_notify_p2p_device_found(ctx, info->p2p_device_addr, new_device);
1713 static void wpas_dev_lost(void *ctx, const u8 *dev_addr)
1715 struct wpa_supplicant *wpa_s = ctx;
1717 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_DEVICE_LOST
1718 "p2p_dev_addr=" MACSTR, MAC2STR(dev_addr));
1720 wpas_notify_p2p_device_lost(wpa_s, dev_addr);
1724 static void wpas_find_stopped(void *ctx)
1726 struct wpa_supplicant *wpa_s = ctx;
1727 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_FIND_STOPPED);
1731 struct wpas_p2p_listen_work {
1733 unsigned int duration;
1734 struct wpabuf *probe_resp_ie;
1738 static void wpas_p2p_listen_work_free(struct wpas_p2p_listen_work *lwork)
1742 wpabuf_free(lwork->probe_resp_ie);
1747 static void wpas_p2p_listen_work_done(struct wpa_supplicant *wpa_s)
1749 struct wpas_p2p_listen_work *lwork;
1751 if (!wpa_s->p2p_listen_work)
1754 lwork = wpa_s->p2p_listen_work->ctx;
1755 wpas_p2p_listen_work_free(lwork);
1756 radio_work_done(wpa_s->p2p_listen_work);
1757 wpa_s->p2p_listen_work = NULL;
1761 static void wpas_start_listen_cb(struct wpa_radio_work *work, int deinit)
1763 struct wpa_supplicant *wpa_s = work->wpa_s;
1764 struct wpas_p2p_listen_work *lwork = work->ctx;
1767 if (work->started) {
1768 wpa_s->p2p_listen_work = NULL;
1769 wpas_stop_listen(wpa_s);
1771 wpas_p2p_listen_work_free(lwork);
1775 wpa_s->p2p_listen_work = work;
1777 wpa_drv_set_ap_wps_ie(wpa_s, NULL, lwork->probe_resp_ie, NULL);
1779 if (wpa_drv_probe_req_report(wpa_s, 1) < 0) {
1780 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver to "
1781 "report received Probe Request frames");
1782 wpas_p2p_listen_work_done(wpa_s);
1786 wpa_s->pending_listen_freq = lwork->freq;
1787 wpa_s->pending_listen_duration = lwork->duration;
1789 if (wpa_drv_remain_on_channel(wpa_s, lwork->freq, lwork->duration) < 0)
1791 wpa_printf(MSG_DEBUG, "P2P: Failed to request the driver "
1792 "to remain on channel (%u MHz) for Listen "
1793 "state", lwork->freq);
1794 wpas_p2p_listen_work_done(wpa_s);
1795 wpa_s->pending_listen_freq = 0;
1798 wpa_s->off_channel_freq = 0;
1799 wpa_s->roc_waiting_drv_freq = lwork->freq;
1803 static int wpas_start_listen(void *ctx, unsigned int freq,
1804 unsigned int duration,
1805 const struct wpabuf *probe_resp_ie)
1807 struct wpa_supplicant *wpa_s = ctx;
1808 struct wpas_p2p_listen_work *lwork;
1810 if (wpa_s->p2p_listen_work) {
1811 wpa_printf(MSG_DEBUG, "P2P: Reject start_listen since p2p_listen_work already exists");
1815 lwork = os_zalloc(sizeof(*lwork));
1819 lwork->duration = duration;
1820 if (probe_resp_ie) {
1821 lwork->probe_resp_ie = wpabuf_dup(probe_resp_ie);
1822 if (lwork->probe_resp_ie == NULL) {
1823 wpas_p2p_listen_work_free(lwork);
1828 if (radio_add_work(wpa_s, freq, "p2p-listen", 0, wpas_start_listen_cb,
1830 wpas_p2p_listen_work_free(lwork);
1838 static void wpas_stop_listen(void *ctx)
1840 struct wpa_supplicant *wpa_s = ctx;
1841 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
1842 wpa_drv_cancel_remain_on_channel(wpa_s);
1843 wpa_s->off_channel_freq = 0;
1844 wpa_s->roc_waiting_drv_freq = 0;
1846 wpa_drv_set_ap_wps_ie(wpa_s, NULL, NULL, NULL);
1847 wpa_drv_probe_req_report(wpa_s, 0);
1848 wpas_p2p_listen_work_done(wpa_s);
1852 static int wpas_send_probe_resp(void *ctx, const struct wpabuf *buf)
1854 struct wpa_supplicant *wpa_s = ctx;
1855 return wpa_drv_send_mlme(wpa_s, wpabuf_head(buf), wpabuf_len(buf), 1);
1860 * DNS Header section is used only to calculate compression pointers, so the
1861 * contents of this data does not matter, but the length needs to be reserved
1862 * in the virtual packet.
1864 #define DNS_HEADER_LEN 12
1867 * 27-octet in-memory packet from P2P specification containing two implied
1868 * queries for _tcp.lcoal. PTR IN and _udp.local. PTR IN
1870 #define P2P_SD_IN_MEMORY_LEN 27
1872 static int p2p_sd_dns_uncompress_label(char **upos, char *uend, u8 *start,
1873 u8 **spos, const u8 *end)
1875 while (*spos < end) {
1876 u8 val = ((*spos)[0] & 0xc0) >> 6;
1879 if (val == 1 || val == 2) {
1880 /* These are reserved values in RFC 1035 */
1881 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1882 "sequence starting with 0x%x", val);
1891 if (*spos + 2 > end) {
1892 wpa_printf(MSG_DEBUG, "P2P: No room for full "
1893 "DNS offset field");
1897 offset = (((*spos)[0] & 0x3f) << 8) | (*spos)[1];
1898 if (offset >= *spos - start) {
1899 wpa_printf(MSG_DEBUG, "P2P: Invalid DNS "
1900 "pointer offset %u", offset);
1905 spos_tmp = start + offset;
1906 return p2p_sd_dns_uncompress_label(upos, uend, start,
1912 len = (*spos)[0] & 0x3f;
1917 if (*spos + len > end) {
1918 wpa_printf(MSG_DEBUG, "P2P: Invalid domain name "
1919 "sequence - no room for label with length "
1924 if (*upos + len + 2 > uend)
1927 os_memcpy(*upos, *spos, len);
1939 /* Uncompress domain names per RFC 1035 using the P2P SD in-memory packet.
1940 * Returns -1 on parsing error (invalid input sequence), -2 if output buffer is
1941 * not large enough */
1942 static int p2p_sd_dns_uncompress(char *buf, size_t buf_len, const u8 *msg,
1943 size_t msg_len, size_t offset)
1945 /* 27-octet in-memory packet from P2P specification */
1946 const char *prefix = "\x04_tcp\x05local\x00\x00\x0C\x00\x01"
1947 "\x04_udp\xC0\x11\x00\x0C\x00\x01";
1948 u8 *tmp, *end, *spos;
1954 if (offset > msg_len)
1957 tmp = os_malloc(DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN + msg_len);
1960 spos = tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN;
1961 end = spos + msg_len;
1964 os_memset(tmp, 0, DNS_HEADER_LEN);
1965 os_memcpy(tmp + DNS_HEADER_LEN, prefix, P2P_SD_IN_MEMORY_LEN);
1966 os_memcpy(tmp + DNS_HEADER_LEN + P2P_SD_IN_MEMORY_LEN, msg, msg_len);
1969 uend = buf + buf_len;
1971 ret = p2p_sd_dns_uncompress_label(&upos, uend, tmp, &spos, end);
1980 } else if (upos[-1] == '.')
1988 static struct p2p_srv_bonjour *
1989 wpas_p2p_service_get_bonjour(struct wpa_supplicant *wpa_s,
1990 const struct wpabuf *query)
1992 struct p2p_srv_bonjour *bsrv;
1995 len = wpabuf_len(query);
1996 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
1997 struct p2p_srv_bonjour, list) {
1998 if (len == wpabuf_len(bsrv->query) &&
1999 os_memcmp(wpabuf_head(query), wpabuf_head(bsrv->query),
2007 static struct p2p_srv_upnp *
2008 wpas_p2p_service_get_upnp(struct wpa_supplicant *wpa_s, u8 version,
2009 const char *service)
2011 struct p2p_srv_upnp *usrv;
2013 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2014 struct p2p_srv_upnp, list) {
2015 if (version == usrv->version &&
2016 os_strcmp(service, usrv->service) == 0)
2023 static void wpas_sd_add_proto_not_avail(struct wpabuf *resp, u8 srv_proto,
2028 if (wpabuf_tailroom(resp) < 5)
2031 /* Length (to be filled) */
2032 len_pos = wpabuf_put(resp, 2);
2033 wpabuf_put_u8(resp, srv_proto);
2034 wpabuf_put_u8(resp, srv_trans_id);
2036 wpabuf_put_u8(resp, P2P_SD_PROTO_NOT_AVAILABLE);
2037 /* Response Data: empty */
2038 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2042 static void wpas_sd_all_bonjour(struct wpa_supplicant *wpa_s,
2043 struct wpabuf *resp, u8 srv_trans_id)
2045 struct p2p_srv_bonjour *bsrv;
2048 wpa_printf(MSG_DEBUG, "P2P: SD Request for all Bonjour services");
2050 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2051 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2055 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2056 struct p2p_srv_bonjour, list) {
2057 if (wpabuf_tailroom(resp) <
2058 5 + wpabuf_len(bsrv->query) + wpabuf_len(bsrv->resp))
2060 /* Length (to be filled) */
2061 len_pos = wpabuf_put(resp, 2);
2062 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2063 wpabuf_put_u8(resp, srv_trans_id);
2065 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2066 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2067 wpabuf_head(bsrv->resp),
2068 wpabuf_len(bsrv->resp));
2070 wpabuf_put_buf(resp, bsrv->query); /* Key */
2071 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2072 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2078 static int match_bonjour_query(struct p2p_srv_bonjour *bsrv, const u8 *query,
2081 char str_rx[256], str_srv[256];
2083 if (query_len < 3 || wpabuf_len(bsrv->query) < 3)
2084 return 0; /* Too short to include DNS Type and Version */
2085 if (os_memcmp(query + query_len - 3,
2086 wpabuf_head_u8(bsrv->query) + wpabuf_len(bsrv->query) - 3,
2088 return 0; /* Mismatch in DNS Type or Version */
2089 if (query_len == wpabuf_len(bsrv->query) &&
2090 os_memcmp(query, wpabuf_head(bsrv->query), query_len - 3) == 0)
2091 return 1; /* Binary match */
2093 if (p2p_sd_dns_uncompress(str_rx, sizeof(str_rx), query, query_len - 3,
2095 return 0; /* Failed to uncompress query */
2096 if (p2p_sd_dns_uncompress(str_srv, sizeof(str_srv),
2097 wpabuf_head(bsrv->query),
2098 wpabuf_len(bsrv->query) - 3, 0))
2099 return 0; /* Failed to uncompress service */
2101 return os_strcmp(str_rx, str_srv) == 0;
2105 static void wpas_sd_req_bonjour(struct wpa_supplicant *wpa_s,
2106 struct wpabuf *resp, u8 srv_trans_id,
2107 const u8 *query, size_t query_len)
2109 struct p2p_srv_bonjour *bsrv;
2113 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for Bonjour",
2115 if (dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2116 wpa_printf(MSG_DEBUG, "P2P: Bonjour protocol not available");
2117 wpas_sd_add_proto_not_avail(resp, P2P_SERV_BONJOUR,
2122 if (query_len == 0) {
2123 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2127 dl_list_for_each(bsrv, &wpa_s->global->p2p_srv_bonjour,
2128 struct p2p_srv_bonjour, list) {
2129 if (!match_bonjour_query(bsrv, query, query_len))
2132 if (wpabuf_tailroom(resp) <
2133 5 + query_len + wpabuf_len(bsrv->resp))
2138 /* Length (to be filled) */
2139 len_pos = wpabuf_put(resp, 2);
2140 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2141 wpabuf_put_u8(resp, srv_trans_id);
2144 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2145 wpa_hexdump_ascii(MSG_DEBUG, "P2P: Matching Bonjour service",
2146 wpabuf_head(bsrv->resp),
2147 wpabuf_len(bsrv->resp));
2150 wpabuf_put_data(resp, query, query_len); /* Key */
2151 wpabuf_put_buf(resp, bsrv->resp); /* Value */
2153 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2157 wpa_printf(MSG_DEBUG, "P2P: Requested Bonjour service not "
2159 if (wpabuf_tailroom(resp) < 5)
2162 /* Length (to be filled) */
2163 len_pos = wpabuf_put(resp, 2);
2164 wpabuf_put_u8(resp, P2P_SERV_BONJOUR);
2165 wpabuf_put_u8(resp, srv_trans_id);
2168 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2169 /* Response Data: empty */
2170 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2176 static void wpas_sd_all_upnp(struct wpa_supplicant *wpa_s,
2177 struct wpabuf *resp, u8 srv_trans_id)
2179 struct p2p_srv_upnp *usrv;
2182 wpa_printf(MSG_DEBUG, "P2P: SD Request for all UPnP services");
2184 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2185 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2189 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2190 struct p2p_srv_upnp, list) {
2191 if (wpabuf_tailroom(resp) < 5 + 1 + os_strlen(usrv->service))
2194 /* Length (to be filled) */
2195 len_pos = wpabuf_put(resp, 2);
2196 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2197 wpabuf_put_u8(resp, srv_trans_id);
2200 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2202 wpabuf_put_u8(resp, usrv->version);
2203 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2205 wpabuf_put_str(resp, usrv->service);
2206 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos -
2212 static void wpas_sd_req_upnp(struct wpa_supplicant *wpa_s,
2213 struct wpabuf *resp, u8 srv_trans_id,
2214 const u8 *query, size_t query_len)
2216 struct p2p_srv_upnp *usrv;
2222 wpa_hexdump_ascii(MSG_DEBUG, "P2P: SD Request for UPnP",
2225 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp)) {
2226 wpa_printf(MSG_DEBUG, "P2P: UPnP protocol not available");
2227 wpas_sd_add_proto_not_avail(resp, P2P_SERV_UPNP,
2232 if (query_len == 0) {
2233 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2237 if (wpabuf_tailroom(resp) < 5)
2240 /* Length (to be filled) */
2241 len_pos = wpabuf_put(resp, 2);
2242 wpabuf_put_u8(resp, P2P_SERV_UPNP);
2243 wpabuf_put_u8(resp, srv_trans_id);
2246 str = os_malloc(query_len);
2249 os_memcpy(str, query + 1, query_len - 1);
2250 str[query_len - 1] = '\0';
2252 dl_list_for_each(usrv, &wpa_s->global->p2p_srv_upnp,
2253 struct p2p_srv_upnp, list) {
2254 if (version != usrv->version)
2257 if (os_strcmp(str, "ssdp:all") != 0 &&
2258 os_strstr(usrv->service, str) == NULL)
2261 if (wpabuf_tailroom(resp) < 2)
2265 wpabuf_put_u8(resp, P2P_SD_SUCCESS);
2267 wpabuf_put_u8(resp, version);
2269 wpabuf_put_u8(resp, ',');
2273 wpa_printf(MSG_DEBUG, "P2P: Matching UPnP Service: %s",
2275 if (wpabuf_tailroom(resp) < os_strlen(usrv->service))
2277 wpabuf_put_str(resp, usrv->service);
2282 wpa_printf(MSG_DEBUG, "P2P: Requested UPnP service not "
2285 wpabuf_put_u8(resp, P2P_SD_REQUESTED_INFO_NOT_AVAILABLE);
2286 /* Response Data: empty */
2289 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2293 #ifdef CONFIG_WIFI_DISPLAY
2294 static void wpas_sd_req_wfd(struct wpa_supplicant *wpa_s,
2295 struct wpabuf *resp, u8 srv_trans_id,
2296 const u8 *query, size_t query_len)
2302 wpa_hexdump(MSG_DEBUG, "P2P: SD Request for WFD", query, query_len);
2304 if (!wpa_s->global->wifi_display) {
2305 wpa_printf(MSG_DEBUG, "P2P: WFD protocol not available");
2306 wpas_sd_add_proto_not_avail(resp, P2P_SERV_WIFI_DISPLAY,
2311 if (query_len < 1) {
2312 wpa_printf(MSG_DEBUG, "P2P: Missing WFD Requested Device "
2317 if (wpabuf_tailroom(resp) < 5)
2322 wpa_printf(MSG_DEBUG, "P2P: WSD for device role 0x%x", role);
2324 /* TODO: role specific handling */
2326 /* Length (to be filled) */
2327 len_pos = wpabuf_put(resp, 2);
2328 wpabuf_put_u8(resp, P2P_SERV_WIFI_DISPLAY);
2329 wpabuf_put_u8(resp, srv_trans_id);
2330 wpabuf_put_u8(resp, P2P_SD_SUCCESS); /* Status Code */
2332 while (pos < query + query_len) {
2333 if (*pos < MAX_WFD_SUBELEMS &&
2334 wpa_s->global->wfd_subelem[*pos] &&
2335 wpabuf_tailroom(resp) >=
2336 wpabuf_len(wpa_s->global->wfd_subelem[*pos])) {
2337 wpa_printf(MSG_DEBUG, "P2P: Add WSD response "
2338 "subelement %u", *pos);
2339 wpabuf_put_buf(resp, wpa_s->global->wfd_subelem[*pos]);
2344 WPA_PUT_LE16(len_pos, (u8 *) wpabuf_put(resp, 0) - len_pos - 2);
2346 #endif /* CONFIG_WIFI_DISPLAY */
2349 static void wpas_sd_request(void *ctx, int freq, const u8 *sa, u8 dialog_token,
2350 u16 update_indic, const u8 *tlvs, size_t tlvs_len)
2352 struct wpa_supplicant *wpa_s = ctx;
2353 const u8 *pos = tlvs;
2354 const u8 *end = tlvs + tlvs_len;
2357 struct wpabuf *resp;
2358 u8 srv_proto, srv_trans_id;
2362 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Request TLVs",
2364 buf_len = 2 * tlvs_len + 1;
2365 buf = os_malloc(buf_len);
2367 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2368 wpa_msg_ctrl(wpa_s, MSG_INFO, P2P_EVENT_SERV_DISC_REQ "%d "
2370 freq, MAC2STR(sa), dialog_token, update_indic,
2375 if (wpa_s->p2p_sd_over_ctrl_iface) {
2376 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2377 update_indic, tlvs, tlvs_len);
2378 return; /* to be processed by an external program */
2381 resp = wpabuf_alloc(10000);
2385 while (pos + 1 < end) {
2386 wpa_printf(MSG_DEBUG, "P2P: Service Request TLV");
2387 slen = WPA_GET_LE16(pos);
2389 if (pos + slen > end || slen < 2) {
2390 wpa_printf(MSG_DEBUG, "P2P: Unexpected Query Data "
2395 tlv_end = pos + slen;
2398 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2400 srv_trans_id = *pos++;
2401 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2404 wpa_hexdump(MSG_MSGDUMP, "P2P: Query Data",
2405 pos, tlv_end - pos);
2408 if (wpa_s->force_long_sd) {
2409 wpa_printf(MSG_DEBUG, "P2P: SD test - force long "
2411 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2412 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2416 switch (srv_proto) {
2417 case P2P_SERV_ALL_SERVICES:
2418 wpa_printf(MSG_DEBUG, "P2P: Service Discovery Request "
2419 "for all services");
2420 if (dl_list_empty(&wpa_s->global->p2p_srv_upnp) &&
2421 dl_list_empty(&wpa_s->global->p2p_srv_bonjour)) {
2422 wpa_printf(MSG_DEBUG, "P2P: No service "
2423 "discovery protocols available");
2424 wpas_sd_add_proto_not_avail(
2425 resp, P2P_SERV_ALL_SERVICES,
2429 wpas_sd_all_bonjour(wpa_s, resp, srv_trans_id);
2430 wpas_sd_all_upnp(wpa_s, resp, srv_trans_id);
2432 case P2P_SERV_BONJOUR:
2433 wpas_sd_req_bonjour(wpa_s, resp, srv_trans_id,
2434 pos, tlv_end - pos);
2437 wpas_sd_req_upnp(wpa_s, resp, srv_trans_id,
2438 pos, tlv_end - pos);
2440 #ifdef CONFIG_WIFI_DISPLAY
2441 case P2P_SERV_WIFI_DISPLAY:
2442 wpas_sd_req_wfd(wpa_s, resp, srv_trans_id,
2443 pos, tlv_end - pos);
2445 #endif /* CONFIG_WIFI_DISPLAY */
2447 wpa_printf(MSG_DEBUG, "P2P: Unavailable service "
2448 "protocol %u", srv_proto);
2449 wpas_sd_add_proto_not_avail(resp, srv_proto,
2458 wpas_notify_p2p_sd_request(wpa_s, freq, sa, dialog_token,
2459 update_indic, tlvs, tlvs_len);
2461 wpas_p2p_sd_response(wpa_s, freq, sa, dialog_token, resp);
2467 static void wpas_sd_response(void *ctx, const u8 *sa, u16 update_indic,
2468 const u8 *tlvs, size_t tlvs_len)
2470 struct wpa_supplicant *wpa_s = ctx;
2471 const u8 *pos = tlvs;
2472 const u8 *end = tlvs + tlvs_len;
2478 wpa_hexdump(MSG_MSGDUMP, "P2P: Service Discovery Response TLVs",
2480 if (tlvs_len > 1500) {
2481 /* TODO: better way for handling this */
2482 wpa_msg_ctrl(wpa_s, MSG_INFO,
2483 P2P_EVENT_SERV_DISC_RESP MACSTR
2484 " %u <long response: %u bytes>",
2485 MAC2STR(sa), update_indic,
2486 (unsigned int) tlvs_len);
2488 buf_len = 2 * tlvs_len + 1;
2489 buf = os_malloc(buf_len);
2491 wpa_snprintf_hex(buf, buf_len, tlvs, tlvs_len);
2492 wpa_msg_ctrl(wpa_s, MSG_INFO,
2493 P2P_EVENT_SERV_DISC_RESP MACSTR " %u %s",
2494 MAC2STR(sa), update_indic, buf);
2500 u8 srv_proto, srv_trans_id, status;
2502 wpa_printf(MSG_DEBUG, "P2P: Service Response TLV");
2503 slen = WPA_GET_LE16(pos);
2505 if (pos + slen > end || slen < 3) {
2506 wpa_printf(MSG_DEBUG, "P2P: Unexpected Response Data "
2510 tlv_end = pos + slen;
2513 wpa_printf(MSG_DEBUG, "P2P: Service Protocol Type %u",
2515 srv_trans_id = *pos++;
2516 wpa_printf(MSG_DEBUG, "P2P: Service Transaction ID %u",
2519 wpa_printf(MSG_DEBUG, "P2P: Status Code ID %u",
2522 wpa_hexdump(MSG_MSGDUMP, "P2P: Response Data",
2523 pos, tlv_end - pos);
2528 wpas_notify_p2p_sd_response(wpa_s, sa, update_indic, tlvs, tlvs_len);
2532 u64 wpas_p2p_sd_request(struct wpa_supplicant *wpa_s, const u8 *dst,
2533 const struct wpabuf *tlvs)
2535 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2537 return (uintptr_t) p2p_sd_request(wpa_s->global->p2p, dst, tlvs);
2541 u64 wpas_p2p_sd_request_upnp(struct wpa_supplicant *wpa_s, const u8 *dst,
2542 u8 version, const char *query)
2544 struct wpabuf *tlvs;
2547 tlvs = wpabuf_alloc(2 + 1 + 1 + 1 + os_strlen(query));
2550 wpabuf_put_le16(tlvs, 1 + 1 + 1 + os_strlen(query));
2551 wpabuf_put_u8(tlvs, P2P_SERV_UPNP); /* Service Protocol Type */
2552 wpabuf_put_u8(tlvs, 1); /* Service Transaction ID */
2553 wpabuf_put_u8(tlvs, version);
2554 wpabuf_put_str(tlvs, query);
2555 ret = wpas_p2p_sd_request(wpa_s, dst, tlvs);
2561 #ifdef CONFIG_WIFI_DISPLAY
2563 static u64 wpas_p2p_sd_request_wfd(struct wpa_supplicant *wpa_s, const u8 *dst,
2564 const struct wpabuf *tlvs)
2566 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2568 return (uintptr_t) p2p_sd_request_wfd(wpa_s->global->p2p, dst, tlvs);
2572 #define MAX_WFD_SD_SUBELEMS 20
2574 static void wfd_add_sd_req_role(struct wpabuf *tlvs, u8 id, u8 role,
2575 const char *subelems)
2582 len = wpabuf_put(tlvs, 2);
2583 wpabuf_put_u8(tlvs, P2P_SERV_WIFI_DISPLAY); /* Service Protocol Type */
2584 wpabuf_put_u8(tlvs, id); /* Service Transaction ID */
2586 wpabuf_put_u8(tlvs, role);
2591 if (val >= 0 && val < 256) {
2592 wpabuf_put_u8(tlvs, val);
2594 if (count == MAX_WFD_SD_SUBELEMS)
2597 pos = os_strchr(pos + 1, ',');
2603 WPA_PUT_LE16(len, (u8 *) wpabuf_put(tlvs, 0) - len - 2);
2607 u64 wpas_p2p_sd_request_wifi_display(struct wpa_supplicant *wpa_s,
2608 const u8 *dst, const char *role)
2610 struct wpabuf *tlvs;
2612 const char *subelems;
2615 subelems = os_strchr(role, ' ');
2616 if (subelems == NULL)
2620 tlvs = wpabuf_alloc(4 * (2 + 1 + 1 + 1 + MAX_WFD_SD_SUBELEMS));
2624 if (os_strstr(role, "[source]"))
2625 wfd_add_sd_req_role(tlvs, id++, 0x00, subelems);
2626 if (os_strstr(role, "[pri-sink]"))
2627 wfd_add_sd_req_role(tlvs, id++, 0x01, subelems);
2628 if (os_strstr(role, "[sec-sink]"))
2629 wfd_add_sd_req_role(tlvs, id++, 0x02, subelems);
2630 if (os_strstr(role, "[source+sink]"))
2631 wfd_add_sd_req_role(tlvs, id++, 0x03, subelems);
2633 ret = wpas_p2p_sd_request_wfd(wpa_s, dst, tlvs);
2638 #endif /* CONFIG_WIFI_DISPLAY */
2641 int wpas_p2p_sd_cancel_request(struct wpa_supplicant *wpa_s, u64 req)
2643 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2645 return p2p_sd_cancel_request(wpa_s->global->p2p,
2646 (void *) (uintptr_t) req);
2650 void wpas_p2p_sd_response(struct wpa_supplicant *wpa_s, int freq,
2651 const u8 *dst, u8 dialog_token,
2652 const struct wpabuf *resp_tlvs)
2654 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
2656 p2p_sd_response(wpa_s->global->p2p, freq, dst, dialog_token,
2661 void wpas_p2p_sd_service_update(struct wpa_supplicant *wpa_s)
2663 if (wpa_s->global->p2p)
2664 p2p_sd_service_update(wpa_s->global->p2p);
2668 static void wpas_p2p_srv_bonjour_free(struct p2p_srv_bonjour *bsrv)
2670 dl_list_del(&bsrv->list);
2671 wpabuf_free(bsrv->query);
2672 wpabuf_free(bsrv->resp);
2677 static void wpas_p2p_srv_upnp_free(struct p2p_srv_upnp *usrv)
2679 dl_list_del(&usrv->list);
2680 os_free(usrv->service);
2685 void wpas_p2p_service_flush(struct wpa_supplicant *wpa_s)
2687 struct p2p_srv_bonjour *bsrv, *bn;
2688 struct p2p_srv_upnp *usrv, *un;
2690 dl_list_for_each_safe(bsrv, bn, &wpa_s->global->p2p_srv_bonjour,
2691 struct p2p_srv_bonjour, list)
2692 wpas_p2p_srv_bonjour_free(bsrv);
2694 dl_list_for_each_safe(usrv, un, &wpa_s->global->p2p_srv_upnp,
2695 struct p2p_srv_upnp, list)
2696 wpas_p2p_srv_upnp_free(usrv);
2698 wpas_p2p_sd_service_update(wpa_s);
2702 int wpas_p2p_service_add_bonjour(struct wpa_supplicant *wpa_s,
2703 struct wpabuf *query, struct wpabuf *resp)
2705 struct p2p_srv_bonjour *bsrv;
2707 bsrv = os_zalloc(sizeof(*bsrv));
2710 bsrv->query = query;
2712 dl_list_add(&wpa_s->global->p2p_srv_bonjour, &bsrv->list);
2714 wpas_p2p_sd_service_update(wpa_s);
2719 int wpas_p2p_service_del_bonjour(struct wpa_supplicant *wpa_s,
2720 const struct wpabuf *query)
2722 struct p2p_srv_bonjour *bsrv;
2724 bsrv = wpas_p2p_service_get_bonjour(wpa_s, query);
2727 wpas_p2p_srv_bonjour_free(bsrv);
2728 wpas_p2p_sd_service_update(wpa_s);
2733 int wpas_p2p_service_add_upnp(struct wpa_supplicant *wpa_s, u8 version,
2734 const char *service)
2736 struct p2p_srv_upnp *usrv;
2738 if (wpas_p2p_service_get_upnp(wpa_s, version, service))
2739 return 0; /* Already listed */
2740 usrv = os_zalloc(sizeof(*usrv));
2743 usrv->version = version;
2744 usrv->service = os_strdup(service);
2745 if (usrv->service == NULL) {
2749 dl_list_add(&wpa_s->global->p2p_srv_upnp, &usrv->list);
2751 wpas_p2p_sd_service_update(wpa_s);
2756 int wpas_p2p_service_del_upnp(struct wpa_supplicant *wpa_s, u8 version,
2757 const char *service)
2759 struct p2p_srv_upnp *usrv;
2761 usrv = wpas_p2p_service_get_upnp(wpa_s, version, service);
2764 wpas_p2p_srv_upnp_free(usrv);
2765 wpas_p2p_sd_service_update(wpa_s);
2770 static void wpas_prov_disc_local_display(struct wpa_supplicant *wpa_s,
2771 const u8 *peer, const char *params,
2772 unsigned int generated_pin)
2774 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_SHOW_PIN MACSTR
2775 " %08d%s", MAC2STR(peer), generated_pin, params);
2779 static void wpas_prov_disc_local_keypad(struct wpa_supplicant *wpa_s,
2780 const u8 *peer, const char *params)
2782 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_ENTER_PIN MACSTR
2783 "%s", MAC2STR(peer), params);
2787 static void wpas_prov_disc_req(void *ctx, const u8 *peer, u16 config_methods,
2788 const u8 *dev_addr, const u8 *pri_dev_type,
2789 const char *dev_name, u16 supp_config_methods,
2790 u8 dev_capab, u8 group_capab, const u8 *group_id,
2791 size_t group_id_len)
2793 struct wpa_supplicant *wpa_s = ctx;
2794 char devtype[WPS_DEV_TYPE_BUFSIZE];
2796 u8 empty_dev_type[8];
2797 unsigned int generated_pin = 0;
2798 struct wpa_supplicant *group = NULL;
2801 for (group = wpa_s->global->ifaces; group; group = group->next)
2803 struct wpa_ssid *s = group->current_ssid;
2805 s->mode == WPAS_MODE_P2P_GO &&
2806 group_id_len - ETH_ALEN == s->ssid_len &&
2807 os_memcmp(group_id + ETH_ALEN, s->ssid,
2813 if (pri_dev_type == NULL) {
2814 os_memset(empty_dev_type, 0, sizeof(empty_dev_type));
2815 pri_dev_type = empty_dev_type;
2817 os_snprintf(params, sizeof(params), " p2p_dev_addr=" MACSTR
2818 " pri_dev_type=%s name='%s' config_methods=0x%x "
2819 "dev_capab=0x%x group_capab=0x%x%s%s",
2821 wps_dev_type_bin2str(pri_dev_type, devtype,
2823 dev_name, supp_config_methods, dev_capab, group_capab,
2824 group ? " group=" : "",
2825 group ? group->ifname : "");
2826 params[sizeof(params) - 1] = '\0';
2828 if (config_methods & WPS_CONFIG_DISPLAY) {
2829 generated_pin = wps_generate_pin();
2830 wpas_prov_disc_local_display(wpa_s, peer, params,
2832 } else if (config_methods & WPS_CONFIG_KEYPAD)
2833 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2834 else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2835 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_REQ
2836 MACSTR "%s", MAC2STR(peer), params);
2838 wpas_notify_p2p_provision_discovery(wpa_s, peer, 1 /* request */,
2839 P2P_PROV_DISC_SUCCESS,
2840 config_methods, generated_pin);
2844 static void wpas_prov_disc_resp(void *ctx, const u8 *peer, u16 config_methods)
2846 struct wpa_supplicant *wpa_s = ctx;
2847 unsigned int generated_pin = 0;
2850 if (wpa_s->pending_pd_before_join &&
2851 (os_memcmp(peer, wpa_s->pending_join_dev_addr, ETH_ALEN) == 0 ||
2852 os_memcmp(peer, wpa_s->pending_join_iface_addr, ETH_ALEN) == 0)) {
2853 wpa_s->pending_pd_before_join = 0;
2854 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2855 "join-existing-group operation");
2856 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2860 if (wpa_s->pending_pd_use == AUTO_PD_JOIN ||
2861 wpa_s->pending_pd_use == AUTO_PD_GO_NEG)
2862 os_snprintf(params, sizeof(params), " peer_go=%d",
2863 wpa_s->pending_pd_use == AUTO_PD_JOIN);
2867 if (config_methods & WPS_CONFIG_DISPLAY)
2868 wpas_prov_disc_local_keypad(wpa_s, peer, params);
2869 else if (config_methods & WPS_CONFIG_KEYPAD) {
2870 generated_pin = wps_generate_pin();
2871 wpas_prov_disc_local_display(wpa_s, peer, params,
2873 } else if (config_methods & WPS_CONFIG_PUSHBUTTON)
2874 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_PBC_RESP
2875 MACSTR "%s", MAC2STR(peer), params);
2877 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2878 P2P_PROV_DISC_SUCCESS,
2879 config_methods, generated_pin);
2883 static void wpas_prov_disc_fail(void *ctx, const u8 *peer,
2884 enum p2p_prov_disc_status status)
2886 struct wpa_supplicant *wpa_s = ctx;
2888 if (wpa_s->p2p_fallback_to_go_neg) {
2889 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: PD for p2p_connect-auto "
2890 "failed - fall back to GO Negotiation");
2891 wpas_p2p_fallback_to_go_neg(wpa_s, 0);
2895 if (status == P2P_PROV_DISC_TIMEOUT_JOIN) {
2896 wpa_s->pending_pd_before_join = 0;
2897 wpa_printf(MSG_DEBUG, "P2P: Starting pending "
2898 "join-existing-group operation (no ACK for PD "
2900 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
2904 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_PROV_DISC_FAILURE
2905 " p2p_dev_addr=" MACSTR " status=%d",
2906 MAC2STR(peer), status);
2908 wpas_notify_p2p_provision_discovery(wpa_s, peer, 0 /* response */,
2913 static int freq_included(const struct p2p_channels *channels, unsigned int freq)
2915 if (channels == NULL)
2916 return 1; /* Assume no restrictions */
2917 return p2p_channels_includes_freq(channels, freq);
2923 * Pick the best frequency to use from all the currently used frequencies.
2925 static int wpas_p2p_pick_best_used_freq(struct wpa_supplicant *wpa_s,
2926 struct wpa_used_freq_data *freqs,
2931 /* find a candidate freq that is supported by P2P */
2932 for (c = 0; c < num; c++)
2933 if (p2p_supported_freq(wpa_s->global->p2p, freqs[c].freq))
2939 /* once we have a candidate, try to find a 'better' one */
2940 for (i = c + 1; i < num; i++) {
2941 if (!p2p_supported_freq(wpa_s->global->p2p, freqs[i].freq))
2945 * 1. Infrastructure station interfaces have higher preference.
2946 * 2. P2P Clients have higher preference.
2949 if (freqs[i].flags & WPA_FREQ_USED_BY_INFRA_STATION) {
2954 if ((freqs[i].flags & WPA_FREQ_USED_BY_P2P_CLIENT))
2957 return freqs[c].freq;
2961 static u8 wpas_invitation_process(void *ctx, const u8 *sa, const u8 *bssid,
2962 const u8 *go_dev_addr, const u8 *ssid,
2963 size_t ssid_len, int *go, u8 *group_bssid,
2964 int *force_freq, int persistent_group,
2965 const struct p2p_channels *channels,
2968 struct wpa_supplicant *wpa_s = ctx;
2970 struct wpa_used_freq_data *freqs;
2971 struct wpa_supplicant *grp;
2974 if (!persistent_group) {
2975 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
2976 " to join an active group (SSID: %s)",
2977 MAC2STR(sa), wpa_ssid_txt(ssid, ssid_len));
2978 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
2979 (os_memcmp(go_dev_addr, wpa_s->p2p_auth_invite, ETH_ALEN)
2981 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0)) {
2982 wpa_printf(MSG_DEBUG, "P2P: Accept previously "
2983 "authorized invitation");
2987 #ifdef CONFIG_WPS_NFC
2988 if (dev_pw_id >= 0 && wpa_s->parent->p2p_nfc_tag_enabled &&
2989 dev_pw_id == wpa_s->parent->p2p_oob_dev_pw_id) {
2990 wpa_printf(MSG_DEBUG, "P2P: Accept invitation based on local enabled NFC Tag");
2991 wpa_s->parent->p2p_wps_method = WPS_NFC;
2992 wpa_s->parent->pending_join_wps_method = WPS_NFC;
2993 os_memcpy(wpa_s->parent->pending_join_dev_addr,
2994 go_dev_addr, ETH_ALEN);
2995 os_memcpy(wpa_s->parent->pending_join_iface_addr,
2999 #endif /* CONFIG_WPS_NFC */
3002 * Do not accept the invitation automatically; notify user and
3005 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3008 grp = wpas_get_p2p_group(wpa_s, ssid, ssid_len, go);
3010 wpa_printf(MSG_DEBUG, "P2P: Accept invitation to already "
3011 "running persistent group");
3013 os_memcpy(group_bssid, grp->own_addr, ETH_ALEN);
3017 if (!is_zero_ether_addr(wpa_s->p2p_auth_invite) &&
3018 os_memcmp(sa, wpa_s->p2p_auth_invite, ETH_ALEN) == 0) {
3019 wpa_printf(MSG_DEBUG, "P2P: Accept previously initiated "
3020 "invitation to re-invoke a persistent group");
3021 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
3022 } else if (!wpa_s->conf->persistent_reconnect)
3023 return P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE;
3025 for (s = wpa_s->conf->ssid; s; s = s->next) {
3026 if (s->disabled == 2 &&
3027 os_memcmp(s->bssid, go_dev_addr, ETH_ALEN) == 0 &&
3028 s->ssid_len == ssid_len &&
3029 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3034 wpa_printf(MSG_DEBUG, "P2P: Invitation from " MACSTR
3035 " requested reinvocation of an unknown group",
3037 return P2P_SC_FAIL_UNKNOWN_GROUP;
3040 if (s->mode == WPAS_MODE_P2P_GO && !wpas_p2p_create_iface(wpa_s)) {
3042 if (wpa_s->wpa_state >= WPA_AUTHENTICATING) {
3043 wpa_printf(MSG_DEBUG, "P2P: The only available "
3044 "interface is already in use - reject "
3046 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3048 os_memcpy(group_bssid, wpa_s->own_addr, ETH_ALEN);
3049 } else if (s->mode == WPAS_MODE_P2P_GO) {
3051 if (wpas_p2p_add_group_interface(wpa_s, WPA_IF_P2P_GO) < 0)
3053 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
3054 "interface address for the group");
3055 return P2P_SC_FAIL_UNABLE_TO_ACCOMMODATE;
3057 os_memcpy(group_bssid, wpa_s->pending_interface_addr,
3062 wpas_p2p_set_own_freq_preference(wpa_s, 0);
3065 freqs = os_calloc(wpa_s->num_multichan_concurrent,
3066 sizeof(struct wpa_used_freq_data));
3068 int num_channels = wpa_s->num_multichan_concurrent;
3069 int num = wpas_p2p_valid_oper_freqs(wpa_s, freqs, num_channels);
3070 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
3074 /* Get one of the frequencies currently in use */
3075 if (best_freq > 0) {
3076 wpa_printf(MSG_DEBUG, "P2P: Trying to prefer a channel already used by one of the interfaces");
3077 wpas_p2p_set_own_freq_preference(wpa_s, best_freq);
3079 if (wpa_s->num_multichan_concurrent < 2 ||
3080 wpas_p2p_num_unused_channels(wpa_s) < 1) {
3081 wpa_printf(MSG_DEBUG, "P2P: No extra channels available - trying to force channel to match a channel already used by one of the interfaces");
3082 *force_freq = best_freq;
3086 if (*force_freq > 0 && wpa_s->num_multichan_concurrent > 1 &&
3087 wpas_p2p_num_unused_channels(wpa_s) > 0) {
3089 /* We are the client */
3090 wpa_printf(MSG_DEBUG, "P2P: Peer was found to be "
3091 "running a GO but we are capable of MCC, "
3092 "figure out the best channel to use");
3094 } else if (!freq_included(channels, *force_freq)) {
3095 /* We are the GO, and *force_freq is not in the
3097 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
3098 "in intersection but we are capable of MCC, "
3099 "figure out the best channel to use",
3105 return P2P_SC_SUCCESS;
3109 static void wpas_invitation_received(void *ctx, const u8 *sa, const u8 *bssid,
3110 const u8 *ssid, size_t ssid_len,
3111 const u8 *go_dev_addr, u8 status,
3114 struct wpa_supplicant *wpa_s = ctx;
3117 for (s = wpa_s->conf->ssid; s; s = s->next) {
3118 if (s->disabled == 2 &&
3119 s->ssid_len == ssid_len &&
3120 os_memcmp(ssid, s->ssid, ssid_len) == 0)
3124 if (status == P2P_SC_SUCCESS) {
3125 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3126 " was accepted; op_freq=%d MHz, SSID=%s",
3127 MAC2STR(sa), op_freq, wpa_ssid_txt(ssid, ssid_len));
3129 int go = s->mode == WPAS_MODE_P2P_GO;
3130 wpas_p2p_group_add_persistent(
3131 wpa_s, s, go, 0, op_freq, 0, 0, NULL,
3132 go ? P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE : 0);
3134 wpa_s->user_initiated_pd = 0;
3135 wpas_p2p_join(wpa_s, bssid, go_dev_addr,
3136 wpa_s->p2p_wps_method, 0, op_freq,
3142 if (status != P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3143 wpa_printf(MSG_DEBUG, "P2P: Invitation from peer " MACSTR
3144 " was rejected (status %u)", MAC2STR(sa), status);
3150 wpa_msg_global(wpa_s, MSG_INFO,
3151 P2P_EVENT_INVITATION_RECEIVED
3152 "sa=" MACSTR " go_dev_addr=" MACSTR
3153 " bssid=" MACSTR " unknown-network",
3154 MAC2STR(sa), MAC2STR(go_dev_addr),
3157 wpa_msg_global(wpa_s, MSG_INFO,
3158 P2P_EVENT_INVITATION_RECEIVED
3159 "sa=" MACSTR " go_dev_addr=" MACSTR
3161 MAC2STR(sa), MAC2STR(go_dev_addr));
3166 if (s->mode == WPAS_MODE_P2P_GO && op_freq) {
3167 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3168 "sa=" MACSTR " persistent=%d freq=%d",
3169 MAC2STR(sa), s->id, op_freq);
3171 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RECEIVED
3172 "sa=" MACSTR " persistent=%d",
3173 MAC2STR(sa), s->id);
3178 static void wpas_remove_persistent_peer(struct wpa_supplicant *wpa_s,
3179 struct wpa_ssid *ssid,
3180 const u8 *peer, int inv)
3187 for (i = 0; ssid->p2p_client_list && i < ssid->num_p2p_clients; i++) {
3188 if (os_memcmp(ssid->p2p_client_list + i * ETH_ALEN, peer,
3192 if (i >= ssid->num_p2p_clients || !ssid->p2p_client_list) {
3193 if (ssid->mode != WPAS_MODE_P2P_GO &&
3194 os_memcmp(ssid->bssid, peer, ETH_ALEN) == 0) {
3195 wpa_printf(MSG_DEBUG, "P2P: Remove persistent group %d "
3196 "due to invitation result", ssid->id);
3197 wpas_notify_network_removed(wpa_s, ssid);
3198 wpa_config_remove_network(wpa_s->conf, ssid->id);
3201 return; /* Peer not found in client list */
3204 wpa_printf(MSG_DEBUG, "P2P: Remove peer " MACSTR " from persistent "
3205 "group %d client list%s",
3206 MAC2STR(peer), ssid->id,
3207 inv ? " due to invitation result" : "");
3208 os_memmove(ssid->p2p_client_list + i * ETH_ALEN,
3209 ssid->p2p_client_list + (i + 1) * ETH_ALEN,
3210 (ssid->num_p2p_clients - i - 1) * ETH_ALEN);
3211 ssid->num_p2p_clients--;
3212 if (wpa_s->parent->conf->update_config &&
3213 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
3214 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
3218 static void wpas_remove_persistent_client(struct wpa_supplicant *wpa_s,
3221 struct wpa_ssid *ssid;
3223 wpa_s = wpa_s->global->p2p_invite_group;
3225 return; /* No known invitation group */
3226 ssid = wpa_s->current_ssid;
3227 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GO ||
3228 !ssid->p2p_persistent_group)
3229 return; /* Not operating as a GO in persistent group */
3230 ssid = wpas_p2p_get_persistent(wpa_s->parent, peer,
3231 ssid->ssid, ssid->ssid_len);
3232 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3236 static void wpas_invitation_result(void *ctx, int status, const u8 *bssid,
3237 const struct p2p_channels *channels,
3238 const u8 *peer, int neg_freq,
3241 struct wpa_supplicant *wpa_s = ctx;
3242 struct wpa_ssid *ssid;
3246 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3247 "status=%d " MACSTR,
3248 status, MAC2STR(bssid));
3250 wpa_msg_global(wpa_s, MSG_INFO, P2P_EVENT_INVITATION_RESULT
3251 "status=%d ", status);
3253 wpas_notify_p2p_invitation_result(wpa_s, status, bssid);
3255 wpa_printf(MSG_DEBUG, "P2P: Invitation result - status=%d peer=" MACSTR,
3256 status, MAC2STR(peer));
3257 if (wpa_s->pending_invite_ssid_id == -1) {
3258 if (status == P2P_SC_FAIL_UNKNOWN_GROUP)
3259 wpas_remove_persistent_client(wpa_s, peer);
3260 return; /* Invitation to active group */
3263 if (status == P2P_SC_FAIL_INFO_CURRENTLY_UNAVAILABLE) {
3264 wpa_printf(MSG_DEBUG, "P2P: Waiting for peer to start another "
3265 "invitation exchange to indicate readiness for "
3269 if (status != P2P_SC_SUCCESS) {
3270 if (status == P2P_SC_FAIL_UNKNOWN_GROUP) {
3271 ssid = wpa_config_get_network(
3272 wpa_s->conf, wpa_s->pending_invite_ssid_id);
3273 wpas_remove_persistent_peer(wpa_s, ssid, peer, 1);
3275 wpas_p2p_remove_pending_group_interface(wpa_s);
3279 ssid = wpa_config_get_network(wpa_s->conf,
3280 wpa_s->pending_invite_ssid_id);
3282 wpa_printf(MSG_ERROR, "P2P: Could not find persistent group "
3283 "data matching with invitation");
3288 * The peer could have missed our ctrl::ack frame for Invitation
3289 * Response and continue retransmitting the frame. To reduce the
3290 * likelihood of the peer not getting successful TX status for the
3291 * Invitation Response frame, wait a short time here before starting
3292 * the persistent group so that we will remain on the current channel to
3293 * acknowledge any possible retransmission from the peer.
3295 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: 50 ms wait on current channel before "
3296 "starting persistent group");
3299 if (neg_freq > 0 && ssid->mode == WPAS_MODE_P2P_GO &&
3300 freq_included(channels, neg_freq))
3302 else if (peer_oper_freq > 0 && ssid->mode != WPAS_MODE_P2P_GO &&
3303 freq_included(channels, peer_oper_freq))
3304 freq = peer_oper_freq;
3308 wpa_printf(MSG_DEBUG, "P2P: Persistent group invitation success - op_freq=%d MHz SSID=%s",
3309 freq, wpa_ssid_txt(ssid->ssid, ssid->ssid_len));
3310 wpas_p2p_group_add_persistent(wpa_s, ssid,
3311 ssid->mode == WPAS_MODE_P2P_GO,
3312 wpa_s->p2p_persistent_go_freq,
3314 wpa_s->p2p_go_ht40, wpa_s->p2p_go_vht,
3316 ssid->mode == WPAS_MODE_P2P_GO ?
3317 P2P_MAX_INITIAL_CONN_WAIT_GO_REINVOKE :
3322 static int wpas_p2p_disallowed_freq(struct wpa_global *global,
3325 if (freq_range_list_includes(&global->p2p_go_avoid_freq, freq))
3327 return freq_range_list_includes(&global->p2p_disallow_freq, freq);
3331 static void wpas_p2p_add_chan(struct p2p_reg_class *reg, u8 chan)
3333 reg->channel[reg->channels] = chan;
3338 static int wpas_p2p_default_channels(struct wpa_supplicant *wpa_s,
3339 struct p2p_channels *chan,
3340 struct p2p_channels *cli_chan)
3344 os_memset(cli_chan, 0, sizeof(*cli_chan));
3346 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for 2.4 GHz "
3349 /* Operating class 81 - 2.4 GHz band channels 1..13 */
3350 chan->reg_class[cla].reg_class = 81;
3351 chan->reg_class[cla].channels = 0;
3352 for (i = 0; i < 11; i++) {
3353 if (!wpas_p2p_disallowed_freq(wpa_s->global, 2412 + i * 5))
3354 wpas_p2p_add_chan(&chan->reg_class[cla], i + 1);
3356 if (chan->reg_class[cla].channels)
3359 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for lower 5 GHz "
3362 /* Operating class 115 - 5 GHz, channels 36-48 */
3363 chan->reg_class[cla].reg_class = 115;
3364 chan->reg_class[cla].channels = 0;
3365 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 36 * 5))
3366 wpas_p2p_add_chan(&chan->reg_class[cla], 36);
3367 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 40 * 5))
3368 wpas_p2p_add_chan(&chan->reg_class[cla], 40);
3369 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 44 * 5))
3370 wpas_p2p_add_chan(&chan->reg_class[cla], 44);
3371 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 48 * 5))
3372 wpas_p2p_add_chan(&chan->reg_class[cla], 48);
3373 if (chan->reg_class[cla].channels)
3376 wpa_printf(MSG_DEBUG, "P2P: Enable operating classes for higher 5 GHz "
3379 /* Operating class 124 - 5 GHz, channels 149,153,157,161 */
3380 chan->reg_class[cla].reg_class = 124;
3381 chan->reg_class[cla].channels = 0;
3382 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 149 * 5))
3383 wpas_p2p_add_chan(&chan->reg_class[cla], 149);
3384 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 153 * 5))
3385 wpas_p2p_add_chan(&chan->reg_class[cla], 153);
3386 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 156 * 5))
3387 wpas_p2p_add_chan(&chan->reg_class[cla], 157);
3388 if (!wpas_p2p_disallowed_freq(wpa_s->global, 5000 + 161 * 5))
3389 wpas_p2p_add_chan(&chan->reg_class[cla], 161);
3390 if (chan->reg_class[cla].channels)
3393 chan->reg_classes = cla;
3398 static struct hostapd_hw_modes * get_mode(struct hostapd_hw_modes *modes,
3400 enum hostapd_hw_mode mode)
3404 for (i = 0; i < num_modes; i++) {
3405 if (modes[i].mode == mode)
3414 NOT_ALLOWED, PASSIVE_ONLY, ALLOWED
3417 static int has_channel(struct wpa_global *global,
3418 struct hostapd_hw_modes *mode, u8 chan, int *flags)
3423 freq = (mode->mode == HOSTAPD_MODE_IEEE80211A ? 5000 : 2407) +
3425 if (wpas_p2p_disallowed_freq(global, freq))
3428 for (i = 0; i < mode->num_channels; i++) {
3429 if (mode->channels[i].chan == chan) {
3431 *flags = mode->channels[i].flag;
3432 if (mode->channels[i].flag &
3433 (HOSTAPD_CHAN_DISABLED |
3434 HOSTAPD_CHAN_RADAR))
3436 if (mode->channels[i].flag &
3437 (HOSTAPD_CHAN_PASSIVE_SCAN |
3438 HOSTAPD_CHAN_NO_IBSS))
3439 return PASSIVE_ONLY;
3448 struct p2p_oper_class_map {
3449 enum hostapd_hw_mode mode;
3454 enum { BW20, BW40PLUS, BW40MINUS, BW80 } bw;
3457 static struct p2p_oper_class_map op_class[] = {
3458 { HOSTAPD_MODE_IEEE80211G, 81, 1, 13, 1, BW20 },
3459 #if 0 /* Do not enable HT40 on 2 GHz for now */
3460 { HOSTAPD_MODE_IEEE80211G, 83, 1, 9, 1, BW40PLUS },
3461 { HOSTAPD_MODE_IEEE80211G, 84, 5, 13, 1, BW40MINUS },
3463 { HOSTAPD_MODE_IEEE80211A, 115, 36, 48, 4, BW20 },
3464 { HOSTAPD_MODE_IEEE80211A, 124, 149, 161, 4, BW20 },
3465 { HOSTAPD_MODE_IEEE80211A, 116, 36, 44, 8, BW40PLUS },
3466 { HOSTAPD_MODE_IEEE80211A, 117, 40, 48, 8, BW40MINUS },
3467 { HOSTAPD_MODE_IEEE80211A, 126, 149, 157, 8, BW40PLUS },
3468 { HOSTAPD_MODE_IEEE80211A, 127, 153, 161, 8, BW40MINUS },
3471 * IEEE P802.11ac/D7.0 Table E-4 actually talks about channel center
3472 * frequency index 42, 58, 106, 122, 138, 155 with channel spacing of
3473 * 80 MHz, but currently use the following definition for simplicity
3474 * (these center frequencies are not actual channels, which makes
3475 * has_channel() fail). wpas_p2p_verify_80mhz() should take care of
3476 * removing invalid channels.
3478 { HOSTAPD_MODE_IEEE80211A, 128, 36, 161, 4, BW80 },
3479 { -1, 0, 0, 0, 0, BW20 }
3483 static int wpas_p2p_get_center_80mhz(struct wpa_supplicant *wpa_s,
3484 struct hostapd_hw_modes *mode,
3487 u8 center_channels[] = { 42, 58, 106, 122, 138, 155 };
3490 if (mode->mode != HOSTAPD_MODE_IEEE80211A)
3493 for (i = 0; i < ARRAY_SIZE(center_channels); i++)
3495 * In 80 MHz, the bandwidth "spans" 12 channels (e.g., 36-48),
3496 * so the center channel is 6 channels away from the start/end.
3498 if (channel >= center_channels[i] - 6 &&
3499 channel <= center_channels[i] + 6)
3500 return center_channels[i];
3506 static enum chan_allowed wpas_p2p_verify_80mhz(struct wpa_supplicant *wpa_s,
3507 struct hostapd_hw_modes *mode,
3512 enum chan_allowed res, ret = ALLOWED;
3514 center_chan = wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3517 if (center_chan >= 58 && center_chan <= 138)
3518 return NOT_ALLOWED; /* Do not allow DFS channels for P2P */
3520 /* check all the channels are available */
3521 for (i = 0; i < 4; i++) {
3522 int adj_chan = center_chan - 6 + i * 4;
3524 res = has_channel(wpa_s->global, mode, adj_chan, &flags);
3525 if (res == NOT_ALLOWED)
3527 if (res == PASSIVE_ONLY)
3530 if (i == 0 && !(flags & HOSTAPD_CHAN_VHT_10_70))
3532 if (i == 1 && !(flags & HOSTAPD_CHAN_VHT_30_50))
3534 if (i == 2 && !(flags & HOSTAPD_CHAN_VHT_50_30))
3536 if (i == 3 && !(flags & HOSTAPD_CHAN_VHT_70_10))
3544 static enum chan_allowed wpas_p2p_verify_channel(struct wpa_supplicant *wpa_s,
3545 struct hostapd_hw_modes *mode,
3549 enum chan_allowed res, res2;
3551 res2 = res = has_channel(wpa_s->global, mode, channel, &flag);
3552 if (bw == BW40MINUS) {
3553 if (!(flag & HOSTAPD_CHAN_HT40MINUS))
3555 res2 = has_channel(wpa_s->global, mode, channel - 4, NULL);
3556 } else if (bw == BW40PLUS) {
3557 if (!(flag & HOSTAPD_CHAN_HT40PLUS))
3559 res2 = has_channel(wpa_s->global, mode, channel + 4, NULL);
3560 } else if (bw == BW80) {
3561 res2 = wpas_p2p_verify_80mhz(wpa_s, mode, channel, bw);
3564 if (res == NOT_ALLOWED || res2 == NOT_ALLOWED)
3566 if (res == PASSIVE_ONLY || res2 == PASSIVE_ONLY)
3567 return PASSIVE_ONLY;
3572 static int wpas_p2p_setup_channels(struct wpa_supplicant *wpa_s,
3573 struct p2p_channels *chan,
3574 struct p2p_channels *cli_chan)
3576 struct hostapd_hw_modes *mode;
3577 int cla, op, cli_cla;
3579 if (wpa_s->hw.modes == NULL) {
3580 wpa_printf(MSG_DEBUG, "P2P: Driver did not support fetching "
3581 "of all supported channels; assume dualband "
3583 return wpas_p2p_default_channels(wpa_s, chan, cli_chan);
3588 for (op = 0; op_class[op].op_class; op++) {
3589 struct p2p_oper_class_map *o = &op_class[op];
3591 struct p2p_reg_class *reg = NULL, *cli_reg = NULL;
3593 mode = get_mode(wpa_s->hw.modes, wpa_s->hw.num_modes, o->mode);
3596 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3597 enum chan_allowed res;
3598 res = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3599 if (res == ALLOWED) {
3601 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u",
3603 reg = &chan->reg_class[cla];
3605 reg->reg_class = o->op_class;
3607 reg->channel[reg->channels] = ch;
3609 } else if (res == PASSIVE_ONLY &&
3610 wpa_s->conf->p2p_add_cli_chan) {
3611 if (cli_reg == NULL) {
3612 wpa_printf(MSG_DEBUG, "P2P: Add operating class %u (client only)",
3614 cli_reg = &cli_chan->reg_class[cli_cla];
3616 cli_reg->reg_class = o->op_class;
3618 cli_reg->channel[cli_reg->channels] = ch;
3619 cli_reg->channels++;
3623 wpa_hexdump(MSG_DEBUG, "P2P: Channels",
3624 reg->channel, reg->channels);
3627 wpa_hexdump(MSG_DEBUG, "P2P: Channels (client only)",
3628 cli_reg->channel, cli_reg->channels);
3632 chan->reg_classes = cla;
3633 cli_chan->reg_classes = cli_cla;
3639 int wpas_p2p_get_ht40_mode(struct wpa_supplicant *wpa_s,
3640 struct hostapd_hw_modes *mode, u8 channel)
3643 enum chan_allowed ret;
3645 for (op = 0; op_class[op].op_class; op++) {
3646 struct p2p_oper_class_map *o = &op_class[op];
3649 for (ch = o->min_chan; ch <= o->max_chan; ch += o->inc) {
3650 if (o->mode != HOSTAPD_MODE_IEEE80211A ||
3651 o->bw == BW20 || ch != channel)
3653 ret = wpas_p2p_verify_channel(wpa_s, mode, ch, o->bw);
3655 return (o->bw == BW40MINUS) ? -1 : 1;
3662 int wpas_p2p_get_vht80_center(struct wpa_supplicant *wpa_s,
3663 struct hostapd_hw_modes *mode, u8 channel)
3665 if (!wpas_p2p_verify_channel(wpa_s, mode, channel, BW80))
3668 return wpas_p2p_get_center_80mhz(wpa_s, mode, channel);
3672 static int wpas_get_noa(void *ctx, const u8 *interface_addr, u8 *buf,
3675 struct wpa_supplicant *wpa_s = ctx;
3677 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3678 if (os_memcmp(wpa_s->own_addr, interface_addr, ETH_ALEN) == 0)
3684 return wpa_drv_get_noa(wpa_s, buf, buf_len);
3688 struct wpa_supplicant * wpas_get_p2p_go_iface(struct wpa_supplicant *wpa_s,
3689 const u8 *ssid, size_t ssid_len)
3691 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3692 struct wpa_ssid *s = wpa_s->current_ssid;
3695 if (s->mode != WPAS_MODE_P2P_GO &&
3696 s->mode != WPAS_MODE_AP &&
3697 s->mode != WPAS_MODE_P2P_GROUP_FORMATION)
3699 if (s->ssid_len != ssid_len ||
3700 os_memcmp(s, s->ssid, ssid_len) != 0)
3710 struct wpa_supplicant * wpas_get_p2p_client_iface(struct wpa_supplicant *wpa_s,
3711 const u8 *peer_dev_addr)
3713 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3714 struct wpa_ssid *ssid = wpa_s->current_ssid;
3717 if (ssid->mode != WPAS_MODE_INFRA)
3719 if (wpa_s->wpa_state != WPA_COMPLETED &&
3720 wpa_s->wpa_state != WPA_GROUP_HANDSHAKE)
3722 if (os_memcmp(wpa_s->go_dev_addr, peer_dev_addr, ETH_ALEN) == 0)
3730 static int wpas_go_connected(void *ctx, const u8 *dev_addr)
3732 struct wpa_supplicant *wpa_s = ctx;
3734 return wpas_get_p2p_client_iface(wpa_s, dev_addr) != NULL;
3738 static int wpas_is_concurrent_session_active(void *ctx)
3740 struct wpa_supplicant *wpa_s = ctx;
3741 struct wpa_supplicant *ifs;
3743 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
3746 if (ifs->wpa_state > WPA_ASSOCIATED)
3753 static void wpas_p2p_debug_print(void *ctx, int level, const char *msg)
3755 struct wpa_supplicant *wpa_s = ctx;
3756 wpa_msg_global(wpa_s, level, "P2P: %s", msg);
3760 int wpas_p2p_add_p2pdev_interface(struct wpa_supplicant *wpa_s)
3762 struct wpa_interface iface;
3763 struct wpa_supplicant *p2pdev_wpa_s;
3765 char force_name[100];
3768 os_snprintf(ifname, sizeof(ifname), P2P_MGMT_DEVICE_PREFIX "%s",
3770 force_name[0] = '\0';
3771 wpa_s->pending_interface_type = WPA_IF_P2P_DEVICE;
3772 ret = wpa_drv_if_add(wpa_s, WPA_IF_P2P_DEVICE, ifname, NULL, NULL,
3773 force_name, wpa_s->pending_interface_addr, NULL);
3775 wpa_printf(MSG_DEBUG, "P2P: Failed to create P2P Device interface");
3778 os_strlcpy(wpa_s->pending_interface_name, ifname,
3779 sizeof(wpa_s->pending_interface_name));
3781 os_memset(&iface, 0, sizeof(iface));
3783 iface.ifname = wpa_s->pending_interface_name;
3784 iface.driver = wpa_s->driver->name;
3785 iface.driver_param = wpa_s->conf->driver_param;
3788 * If a P2P Device configuration file was given, use it as the interface
3789 * configuration file (instead of using parent's configuration file.
3791 if (wpa_s->conf_p2p_dev) {
3792 iface.confname = wpa_s->conf_p2p_dev;
3793 iface.ctrl_interface = NULL;
3795 iface.confname = wpa_s->confname;
3796 iface.ctrl_interface = wpa_s->conf->ctrl_interface;
3798 iface.conf_p2p_dev = NULL;
3800 p2pdev_wpa_s = wpa_supplicant_add_iface(wpa_s->global, &iface);
3801 if (!p2pdev_wpa_s) {
3802 wpa_printf(MSG_DEBUG, "P2P: Failed to add P2P Device interface");
3805 p2pdev_wpa_s->parent = wpa_s;
3807 wpa_s->pending_interface_name[0] = '\0';
3812 static void wpas_presence_resp(void *ctx, const u8 *src, u8 status,
3813 const u8 *noa, size_t noa_len)
3815 struct wpa_supplicant *wpa_s, *intf = ctx;
3818 for (wpa_s = intf->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
3819 if (wpa_s->waiting_presence_resp)
3823 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No group interface was waiting for presence response");
3826 wpa_s->waiting_presence_resp = 0;
3828 wpa_snprintf_hex(hex, sizeof(hex), noa, noa_len);
3829 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_PRESENCE_RESPONSE "src=" MACSTR
3830 " status=%u noa=%s", MAC2STR(src), status, hex);
3834 static int _wpas_p2p_in_progress(void *ctx)
3836 struct wpa_supplicant *wpa_s = ctx;
3837 return wpas_p2p_in_progress(wpa_s);
3842 * wpas_p2p_init - Initialize P2P module for %wpa_supplicant
3843 * @global: Pointer to global data from wpa_supplicant_init()
3844 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
3845 * Returns: 0 on success, -1 on failure
3847 int wpas_p2p_init(struct wpa_global *global, struct wpa_supplicant *wpa_s)
3849 struct p2p_config p2p;
3853 if (wpa_s->conf->p2p_disabled)
3856 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
3862 os_memset(&p2p, 0, sizeof(p2p));
3864 p2p.debug_print = wpas_p2p_debug_print;
3865 p2p.p2p_scan = wpas_p2p_scan;
3866 p2p.send_action = wpas_send_action;
3867 p2p.send_action_done = wpas_send_action_done;
3868 p2p.go_neg_completed = wpas_go_neg_completed;
3869 p2p.go_neg_req_rx = wpas_go_neg_req_rx;
3870 p2p.dev_found = wpas_dev_found;
3871 p2p.dev_lost = wpas_dev_lost;
3872 p2p.find_stopped = wpas_find_stopped;
3873 p2p.start_listen = wpas_start_listen;
3874 p2p.stop_listen = wpas_stop_listen;
3875 p2p.send_probe_resp = wpas_send_probe_resp;
3876 p2p.sd_request = wpas_sd_request;
3877 p2p.sd_response = wpas_sd_response;
3878 p2p.prov_disc_req = wpas_prov_disc_req;
3879 p2p.prov_disc_resp = wpas_prov_disc_resp;
3880 p2p.prov_disc_fail = wpas_prov_disc_fail;
3881 p2p.invitation_process = wpas_invitation_process;
3882 p2p.invitation_received = wpas_invitation_received;
3883 p2p.invitation_result = wpas_invitation_result;
3884 p2p.get_noa = wpas_get_noa;
3885 p2p.go_connected = wpas_go_connected;
3886 p2p.presence_resp = wpas_presence_resp;
3887 p2p.is_concurrent_session_active = wpas_is_concurrent_session_active;
3888 p2p.is_p2p_in_progress = _wpas_p2p_in_progress;
3890 os_memcpy(wpa_s->global->p2p_dev_addr, wpa_s->own_addr, ETH_ALEN);
3891 os_memcpy(p2p.dev_addr, wpa_s->global->p2p_dev_addr, ETH_ALEN);
3892 p2p.dev_name = wpa_s->conf->device_name;
3893 p2p.manufacturer = wpa_s->conf->manufacturer;
3894 p2p.model_name = wpa_s->conf->model_name;
3895 p2p.model_number = wpa_s->conf->model_number;
3896 p2p.serial_number = wpa_s->conf->serial_number;
3898 os_memcpy(p2p.uuid, wpa_s->wps->uuid, 16);
3899 p2p.config_methods = wpa_s->wps->config_methods;
3902 if (wpa_s->conf->p2p_listen_reg_class &&
3903 wpa_s->conf->p2p_listen_channel) {
3904 p2p.reg_class = wpa_s->conf->p2p_listen_reg_class;
3905 p2p.channel = wpa_s->conf->p2p_listen_channel;
3906 p2p.channel_forced = 1;
3910 * Pick one of the social channels randomly as the listen
3913 os_get_random((u8 *) &r, sizeof(r));
3914 p2p.channel = 1 + (r % 3) * 5;
3915 p2p.channel_forced = 0;
3917 wpa_printf(MSG_DEBUG, "P2P: Own listen channel: %d", p2p.channel);
3919 if (wpa_s->conf->p2p_oper_reg_class &&
3920 wpa_s->conf->p2p_oper_channel) {
3921 p2p.op_reg_class = wpa_s->conf->p2p_oper_reg_class;
3922 p2p.op_channel = wpa_s->conf->p2p_oper_channel;
3923 p2p.cfg_op_channel = 1;
3924 wpa_printf(MSG_DEBUG, "P2P: Configured operating channel: "
3925 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3928 p2p.op_reg_class = 81;
3930 * Use random operation channel from (1, 6, 11) if no other
3931 * preference is indicated.
3933 os_get_random((u8 *) &r, sizeof(r));
3934 p2p.op_channel = 1 + (r % 3) * 5;
3935 p2p.cfg_op_channel = 0;
3936 wpa_printf(MSG_DEBUG, "P2P: Random operating channel: "
3937 "%d:%d", p2p.op_reg_class, p2p.op_channel);
3940 if (wpa_s->conf->p2p_pref_chan && wpa_s->conf->num_p2p_pref_chan) {
3941 p2p.pref_chan = wpa_s->conf->p2p_pref_chan;
3942 p2p.num_pref_chan = wpa_s->conf->num_p2p_pref_chan;
3945 if (wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
3946 os_memcpy(p2p.country, wpa_s->conf->country, 2);
3947 p2p.country[2] = 0x04;
3949 os_memcpy(p2p.country, "XX\x04", 3);
3951 if (wpas_p2p_setup_channels(wpa_s, &p2p.channels, &p2p.cli_channels)) {
3952 wpa_printf(MSG_ERROR, "P2P: Failed to configure supported "
3957 os_memcpy(p2p.pri_dev_type, wpa_s->conf->device_type,
3960 p2p.num_sec_dev_types = wpa_s->conf->num_sec_device_types;
3961 os_memcpy(p2p.sec_dev_type, wpa_s->conf->sec_device_type,
3962 p2p.num_sec_dev_types * WPS_DEV_TYPE_LEN);
3964 p2p.concurrent_operations = !!(wpa_s->drv_flags &
3965 WPA_DRIVER_FLAGS_P2P_CONCURRENT);
3967 p2p.max_peers = 100;
3969 if (wpa_s->conf->p2p_ssid_postfix) {
3970 p2p.ssid_postfix_len =
3971 os_strlen(wpa_s->conf->p2p_ssid_postfix);
3972 if (p2p.ssid_postfix_len > sizeof(p2p.ssid_postfix))
3973 p2p.ssid_postfix_len = sizeof(p2p.ssid_postfix);
3974 os_memcpy(p2p.ssid_postfix, wpa_s->conf->p2p_ssid_postfix,
3975 p2p.ssid_postfix_len);
3978 p2p.p2p_intra_bss = wpa_s->conf->p2p_intra_bss;
3980 p2p.max_listen = wpa_s->max_remain_on_chan;
3982 if (wpa_s->conf->p2p_passphrase_len >= 8 &&
3983 wpa_s->conf->p2p_passphrase_len <= 63)
3984 p2p.passphrase_len = wpa_s->conf->p2p_passphrase_len;
3986 p2p.passphrase_len = 8;
3988 global->p2p = p2p_init(&p2p);
3989 if (global->p2p == NULL)
3991 global->p2p_init_wpa_s = wpa_s;
3993 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
3994 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
3996 p2p_add_wps_vendor_extension(
3997 global->p2p, wpa_s->conf->wps_vendor_ext[i]);
4000 p2p_set_no_go_freq(global->p2p, &wpa_s->conf->p2p_no_go_freq);
4007 * wpas_p2p_deinit - Deinitialize per-interface P2P data
4008 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4010 * This function deinitialize per-interface P2P data.
4012 void wpas_p2p_deinit(struct wpa_supplicant *wpa_s)
4014 if (wpa_s->driver && wpa_s->drv_priv)
4015 wpa_drv_probe_req_report(wpa_s, 0);
4017 if (wpa_s->go_params) {
4018 /* Clear any stored provisioning info */
4019 p2p_clear_provisioning_info(
4021 wpa_s->go_params->peer_device_addr);
4024 os_free(wpa_s->go_params);
4025 wpa_s->go_params = NULL;
4026 eloop_cancel_timeout(wpas_p2p_psk_failure_removal, wpa_s, NULL);
4027 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4028 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4029 wpa_s->p2p_long_listen = 0;
4030 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
4031 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
4032 wpas_p2p_remove_pending_group_interface(wpa_s);
4033 eloop_cancel_timeout(wpas_p2p_group_freq_conflict, wpa_s, NULL);
4034 wpas_p2p_listen_work_done(wpa_s);
4035 if (wpa_s->p2p_send_action_work) {
4036 os_free(wpa_s->p2p_send_action_work->ctx);
4037 radio_work_done(wpa_s->p2p_send_action_work);
4038 wpa_s->p2p_send_action_work = NULL;
4040 eloop_cancel_timeout(wpas_p2p_send_action_work_timeout, wpa_s, NULL);
4042 wpabuf_free(wpa_s->p2p_oob_dev_pw);
4043 wpa_s->p2p_oob_dev_pw = NULL;
4045 /* TODO: remove group interface from the driver if this wpa_s instance
4046 * is on top of a P2P group interface */
4051 * wpas_p2p_deinit_global - Deinitialize global P2P module
4052 * @global: Pointer to global data from wpa_supplicant_init()
4054 * This function deinitializes the global (per device) P2P module.
4056 static void wpas_p2p_deinit_global(struct wpa_global *global)
4058 struct wpa_supplicant *wpa_s, *tmp;
4060 wpa_s = global->ifaces;
4062 wpas_p2p_service_flush(wpa_s);
4064 if (global->p2p == NULL)
4067 /* Remove remaining P2P group interfaces */
4068 while (wpa_s && wpa_s->p2p_group_interface != NOT_P2P_GROUP_INTERFACE)
4069 wpa_s = wpa_s->next;
4071 tmp = global->ifaces;
4074 tmp->p2p_group_interface == NOT_P2P_GROUP_INTERFACE)) {
4079 /* Disconnect from the P2P group and deinit the interface */
4080 wpas_p2p_disconnect(tmp);
4084 * Deinit GO data on any possibly remaining interface (if main
4085 * interface is used as GO).
4087 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4088 if (wpa_s->ap_iface)
4089 wpas_p2p_group_deinit(wpa_s);
4092 p2p_deinit(global->p2p);
4094 global->p2p_init_wpa_s = NULL;
4098 static int wpas_p2p_create_iface(struct wpa_supplicant *wpa_s)
4100 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_DEDICATED_P2P_DEVICE) &&
4101 wpa_s->conf->p2p_no_group_iface)
4102 return 0; /* separate interface disabled per configuration */
4103 if (wpa_s->drv_flags &
4104 (WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE |
4105 WPA_DRIVER_FLAGS_P2P_MGMT_AND_NON_P2P))
4106 return 1; /* P2P group requires a new interface in every case
4108 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CONCURRENT))
4109 return 0; /* driver does not support concurrent operations */
4110 if (wpa_s->global->ifaces->next)
4111 return 1; /* more that one interface already in use */
4112 if (wpa_s->wpa_state >= WPA_AUTHENTICATING)
4113 return 1; /* this interface is already in use */
4118 static int wpas_p2p_start_go_neg(struct wpa_supplicant *wpa_s,
4119 const u8 *peer_addr,
4120 enum p2p_wps_method wps_method,
4121 int go_intent, const u8 *own_interface_addr,
4122 unsigned int force_freq, int persistent_group,
4123 struct wpa_ssid *ssid, unsigned int pref_freq)
4125 if (persistent_group && wpa_s->conf->persistent_reconnect)
4126 persistent_group = 2;
4129 * Increase GO config timeout if HT40 is used since it takes some time
4130 * to scan channels for coex purposes before the BSS can be started.
4132 p2p_set_config_timeout(wpa_s->global->p2p,
4133 wpa_s->p2p_go_ht40 ? 255 : 100, 20);
4135 return p2p_connect(wpa_s->global->p2p, peer_addr, wps_method,
4136 go_intent, own_interface_addr, force_freq,
4137 persistent_group, ssid ? ssid->ssid : NULL,
4138 ssid ? ssid->ssid_len : 0,
4139 wpa_s->p2p_pd_before_go_neg, pref_freq,
4140 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4145 static int wpas_p2p_auth_go_neg(struct wpa_supplicant *wpa_s,
4146 const u8 *peer_addr,
4147 enum p2p_wps_method wps_method,
4148 int go_intent, const u8 *own_interface_addr,
4149 unsigned int force_freq, int persistent_group,
4150 struct wpa_ssid *ssid, unsigned int pref_freq)
4152 if (persistent_group && wpa_s->conf->persistent_reconnect)
4153 persistent_group = 2;
4155 return p2p_authorize(wpa_s->global->p2p, peer_addr, wps_method,
4156 go_intent, own_interface_addr, force_freq,
4157 persistent_group, ssid ? ssid->ssid : NULL,
4158 ssid ? ssid->ssid_len : 0, pref_freq,
4159 wps_method == WPS_NFC ? wpa_s->p2p_oob_dev_pw_id :
4164 static void wpas_p2p_check_join_scan_limit(struct wpa_supplicant *wpa_s)
4166 wpa_s->p2p_join_scan_count++;
4167 wpa_printf(MSG_DEBUG, "P2P: Join scan attempt %d",
4168 wpa_s->p2p_join_scan_count);
4169 if (wpa_s->p2p_join_scan_count > P2P_MAX_JOIN_SCAN_ATTEMPTS) {
4170 wpa_printf(MSG_DEBUG, "P2P: Failed to find GO " MACSTR
4171 " for join operationg - stop join attempt",
4172 MAC2STR(wpa_s->pending_join_iface_addr));
4173 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4174 if (wpa_s->p2p_auto_pd) {
4175 wpa_s->p2p_auto_pd = 0;
4176 wpa_msg_global(wpa_s, MSG_INFO,
4177 P2P_EVENT_PROV_DISC_FAILURE
4178 " p2p_dev_addr=" MACSTR " status=N/A",
4179 MAC2STR(wpa_s->pending_join_dev_addr));
4182 wpa_msg_global(wpa_s->parent, MSG_INFO,
4183 P2P_EVENT_GROUP_FORMATION_FAILURE);
4188 static int wpas_check_freq_conflict(struct wpa_supplicant *wpa_s, int freq)
4191 unsigned int num, i;
4192 struct wpa_used_freq_data *freqs;
4194 if (wpas_p2p_num_unused_channels(wpa_s) > 0) {
4195 /* Multiple channels are supported and not all are in use */
4199 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4200 sizeof(struct wpa_used_freq_data));
4204 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4205 wpa_s->num_multichan_concurrent);
4207 for (i = 0; i < num; i++) {
4208 if (freqs[i].freq == freq) {
4209 wpa_printf(MSG_DEBUG, "P2P: Frequency %d MHz in use by another virtual interface and can be used",
4216 wpa_printf(MSG_DEBUG, "P2P: No valid operating frequencies");
4225 static int wpas_p2p_peer_go(struct wpa_supplicant *wpa_s,
4226 const u8 *peer_dev_addr)
4228 struct wpa_bss *bss;
4231 bss = wpa_bss_get_p2p_dev_addr(wpa_s, peer_dev_addr);
4234 if (bss->last_update_idx < wpa_s->bss_update_idx) {
4235 wpa_printf(MSG_DEBUG, "P2P: Peer BSS entry not updated in the "
4240 updated = os_reltime_before(&wpa_s->p2p_auto_started,
4242 wpa_printf(MSG_DEBUG, "P2P: Current BSS entry for peer updated at "
4243 "%ld.%06ld (%supdated in last scan)",
4244 bss->last_update.sec, bss->last_update.usec,
4245 updated ? "": "not ");
4251 static void wpas_p2p_scan_res_join(struct wpa_supplicant *wpa_s,
4252 struct wpa_scan_results *scan_res)
4254 struct wpa_bss *bss = NULL;
4256 u8 iface_addr[ETH_ALEN];
4258 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4260 if (wpa_s->global->p2p_disabled)
4263 wpa_printf(MSG_DEBUG, "P2P: Scan results received (%d BSS) for %sjoin",
4264 scan_res ? (int) scan_res->num : -1,
4265 wpa_s->p2p_auto_join ? "auto_" : "");
4268 wpas_p2p_scan_res_handler(wpa_s, scan_res);
4270 if (wpa_s->p2p_auto_pd) {
4271 int join = wpas_p2p_peer_go(wpa_s,
4272 wpa_s->pending_join_dev_addr);
4274 wpa_s->auto_pd_scan_retry < P2P_AUTO_PD_SCAN_ATTEMPTS) {
4275 wpa_s->auto_pd_scan_retry++;
4276 bss = wpa_bss_get_bssid_latest(
4277 wpa_s, wpa_s->pending_join_dev_addr);
4280 wpa_printf(MSG_DEBUG, "P2P: Scan retry %d for "
4281 "the peer " MACSTR " at %d MHz",
4282 wpa_s->auto_pd_scan_retry,
4284 pending_join_dev_addr),
4286 wpas_p2p_join_scan_req(wpa_s, freq, NULL, 0);
4294 wpa_s->p2p_auto_pd = 0;
4295 wpa_s->pending_pd_use = join ? AUTO_PD_JOIN : AUTO_PD_GO_NEG;
4296 wpa_printf(MSG_DEBUG, "P2P: Auto PD with " MACSTR " join=%d",
4297 MAC2STR(wpa_s->pending_join_dev_addr), join);
4298 if (p2p_prov_disc_req(wpa_s->global->p2p,
4299 wpa_s->pending_join_dev_addr,
4300 wpa_s->pending_pd_config_methods, join,
4301 0, wpa_s->user_initiated_pd) < 0) {
4302 wpa_s->p2p_auto_pd = 0;
4303 wpa_msg_global(wpa_s, MSG_INFO,
4304 P2P_EVENT_PROV_DISC_FAILURE
4305 " p2p_dev_addr=" MACSTR " status=N/A",
4306 MAC2STR(wpa_s->pending_join_dev_addr));
4311 if (wpa_s->p2p_auto_join) {
4312 int join = wpas_p2p_peer_go(wpa_s,
4313 wpa_s->pending_join_dev_addr);
4315 wpa_printf(MSG_DEBUG, "P2P: Peer was not found to be "
4316 "running a GO -> use GO Negotiation");
4317 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr,
4318 wpa_s->p2p_pin, wpa_s->p2p_wps_method,
4319 wpa_s->p2p_persistent_group, 0, 0, 0,
4320 wpa_s->p2p_go_intent,
4321 wpa_s->p2p_connect_freq,
4322 wpa_s->p2p_persistent_id,
4323 wpa_s->p2p_pd_before_go_neg,
4329 wpa_printf(MSG_DEBUG, "P2P: Peer was found running GO%s -> "
4330 "try to join the group", join ? "" :
4333 wpa_s->p2p_fallback_to_go_neg = 1;
4336 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4337 wpa_s->pending_join_iface_addr);
4339 p2p_get_interface_addr(wpa_s->global->p2p,
4340 wpa_s->pending_join_dev_addr,
4342 os_memcmp(iface_addr, wpa_s->pending_join_dev_addr, ETH_ALEN) != 0)
4344 wpa_printf(MSG_DEBUG, "P2P: Overwrite pending interface "
4345 "address for join from " MACSTR " to " MACSTR
4346 " based on newly discovered P2P peer entry",
4347 MAC2STR(wpa_s->pending_join_iface_addr),
4348 MAC2STR(iface_addr));
4349 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr,
4352 freq = p2p_get_oper_freq(wpa_s->global->p2p,
4353 wpa_s->pending_join_iface_addr);
4356 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4357 "from P2P peer table: %d MHz", freq);
4359 if (wpa_s->p2p_join_ssid_len) {
4360 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4361 MACSTR " and SSID %s",
4362 MAC2STR(wpa_s->pending_join_iface_addr),
4363 wpa_ssid_txt(wpa_s->p2p_join_ssid,
4364 wpa_s->p2p_join_ssid_len));
4365 bss = wpa_bss_get(wpa_s, wpa_s->pending_join_iface_addr,
4366 wpa_s->p2p_join_ssid,
4367 wpa_s->p2p_join_ssid_len);
4370 wpa_printf(MSG_DEBUG, "P2P: Trying to find target GO BSS entry based on BSSID "
4371 MACSTR, MAC2STR(wpa_s->pending_join_iface_addr));
4372 bss = wpa_bss_get_bssid_latest(wpa_s,
4373 wpa_s->pending_join_iface_addr);
4377 wpa_printf(MSG_DEBUG, "P2P: Target GO operating frequency "
4378 "from BSS table: %d MHz (SSID %s)", freq,
4379 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4384 if (wpas_check_freq_conflict(wpa_s, freq) > 0) {
4385 wpa_msg_global(wpa_s->parent, MSG_INFO,
4386 P2P_EVENT_GROUP_FORMATION_FAILURE
4387 "reason=FREQ_CONFLICT");
4391 wpa_printf(MSG_DEBUG, "P2P: Send Provision Discovery Request "
4392 "prior to joining an existing group (GO " MACSTR
4394 MAC2STR(wpa_s->pending_join_dev_addr), freq);
4395 wpa_s->pending_pd_before_join = 1;
4397 switch (wpa_s->pending_join_wps_method) {
4398 case WPS_PIN_DISPLAY:
4399 method = WPS_CONFIG_KEYPAD;
4401 case WPS_PIN_KEYPAD:
4402 method = WPS_CONFIG_DISPLAY;
4405 method = WPS_CONFIG_PUSHBUTTON;
4412 if ((p2p_get_provisioning_info(wpa_s->global->p2p,
4413 wpa_s->pending_join_dev_addr) ==
4416 * We have already performed provision discovery for
4417 * joining the group. Proceed directly to join
4418 * operation without duplicated provision discovery. */
4419 wpa_printf(MSG_DEBUG, "P2P: Provision discovery "
4420 "with " MACSTR " already done - proceed to "
4422 MAC2STR(wpa_s->pending_join_dev_addr));
4423 wpa_s->pending_pd_before_join = 0;
4427 if (p2p_prov_disc_req(wpa_s->global->p2p,
4428 wpa_s->pending_join_dev_addr, method, 1,
4429 freq, wpa_s->user_initiated_pd) < 0) {
4430 wpa_printf(MSG_DEBUG, "P2P: Failed to send Provision "
4431 "Discovery Request before joining an "
4433 wpa_s->pending_pd_before_join = 0;
4439 wpa_printf(MSG_DEBUG, "P2P: Failed to find BSS/GO - try again later");
4440 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4441 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4442 wpas_p2p_check_join_scan_limit(wpa_s);
4446 /* Start join operation immediately */
4447 wpas_p2p_join_start(wpa_s, 0, NULL, 0);
4451 static void wpas_p2p_join_scan_req(struct wpa_supplicant *wpa_s, int freq,
4452 const u8 *ssid, size_t ssid_len)
4455 struct wpa_driver_scan_params params;
4456 struct wpabuf *wps_ie, *ies;
4458 int freqs[2] = { 0, 0 };
4460 os_memset(¶ms, 0, sizeof(params));
4462 /* P2P Wildcard SSID */
4463 params.num_ssids = 1;
4464 if (ssid && ssid_len) {
4465 params.ssids[0].ssid = ssid;
4466 params.ssids[0].ssid_len = ssid_len;
4467 os_memcpy(wpa_s->p2p_join_ssid, ssid, ssid_len);
4468 wpa_s->p2p_join_ssid_len = ssid_len;
4470 params.ssids[0].ssid = (u8 *) P2P_WILDCARD_SSID;
4471 params.ssids[0].ssid_len = P2P_WILDCARD_SSID_LEN;
4472 wpa_s->p2p_join_ssid_len = 0;
4475 wpa_s->wps->dev.p2p = 1;
4476 wps_ie = wps_build_probe_req_ie(DEV_PW_DEFAULT, &wpa_s->wps->dev,
4477 wpa_s->wps->uuid, WPS_REQ_ENROLLEE, 0,
4479 if (wps_ie == NULL) {
4480 wpas_p2p_scan_res_join(wpa_s, NULL);
4484 ielen = p2p_scan_ie_buf_len(wpa_s->global->p2p);
4485 ies = wpabuf_alloc(wpabuf_len(wps_ie) + ielen);
4487 wpabuf_free(wps_ie);
4488 wpas_p2p_scan_res_join(wpa_s, NULL);
4491 wpabuf_put_buf(ies, wps_ie);
4492 wpabuf_free(wps_ie);
4494 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
4496 params.p2p_probe = 1;
4497 params.extra_ies = wpabuf_head(ies);
4498 params.extra_ies_len = wpabuf_len(ies);
4503 * If freq is not provided, check the operating freq of the GO
4504 * and use a single channel scan on if possible.
4506 oper_freq = p2p_get_oper_freq(wpa_s->global->p2p,
4507 wpa_s->pending_join_iface_addr);
4513 params.freqs = freqs;
4517 * Run a scan to update BSS table and start Provision Discovery once
4518 * the new scan results become available.
4520 ret = wpa_drv_scan(wpa_s, ¶ms);
4522 os_get_reltime(&wpa_s->scan_trigger_time);
4523 wpa_s->scan_res_handler = wpas_p2p_scan_res_join;
4524 wpa_s->own_scan_requested = 1;
4530 wpa_printf(MSG_DEBUG, "P2P: Failed to start scan for join - "
4532 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
4533 eloop_register_timeout(1, 0, wpas_p2p_join_scan, wpa_s, NULL);
4534 wpas_p2p_check_join_scan_limit(wpa_s);
4539 static void wpas_p2p_join_scan(void *eloop_ctx, void *timeout_ctx)
4541 struct wpa_supplicant *wpa_s = eloop_ctx;
4542 wpas_p2p_join_scan_req(wpa_s, 0, NULL, 0);
4546 static int wpas_p2p_join(struct wpa_supplicant *wpa_s, const u8 *iface_addr,
4547 const u8 *dev_addr, enum p2p_wps_method wps_method,
4548 int auto_join, int op_freq,
4549 const u8 *ssid, size_t ssid_len)
4551 wpa_printf(MSG_DEBUG, "P2P: Request to join existing group (iface "
4552 MACSTR " dev " MACSTR " op_freq=%d)%s",
4553 MAC2STR(iface_addr), MAC2STR(dev_addr), op_freq,
4554 auto_join ? " (auto_join)" : "");
4555 if (ssid && ssid_len) {
4556 wpa_printf(MSG_DEBUG, "P2P: Group SSID specified: %s",
4557 wpa_ssid_txt(ssid, ssid_len));
4560 wpa_s->p2p_auto_pd = 0;
4561 wpa_s->p2p_auto_join = !!auto_join;
4562 os_memcpy(wpa_s->pending_join_iface_addr, iface_addr, ETH_ALEN);
4563 os_memcpy(wpa_s->pending_join_dev_addr, dev_addr, ETH_ALEN);
4564 wpa_s->pending_join_wps_method = wps_method;
4566 /* Make sure we are not running find during connection establishment */
4567 wpas_p2p_stop_find(wpa_s);
4569 wpa_s->p2p_join_scan_count = 0;
4570 wpas_p2p_join_scan_req(wpa_s, op_freq, ssid, ssid_len);
4575 static int wpas_p2p_join_start(struct wpa_supplicant *wpa_s, int freq,
4576 const u8 *ssid, size_t ssid_len)
4578 struct wpa_supplicant *group;
4579 struct p2p_go_neg_results res;
4580 struct wpa_bss *bss;
4582 group = wpas_p2p_get_group_iface(wpa_s, 0, 0);
4585 if (group != wpa_s) {
4586 os_memcpy(group->p2p_pin, wpa_s->p2p_pin,
4587 sizeof(group->p2p_pin));
4588 group->p2p_wps_method = wpa_s->p2p_wps_method;
4591 * Need to mark the current interface for p2p_group_formation
4592 * when a separate group interface is not used. This is needed
4593 * to allow p2p_cancel stop a pending p2p_connect-join.
4594 * wpas_p2p_init_group_interface() addresses this for the case
4595 * where a separate group interface is used.
4597 wpa_s->global->p2p_group_formation = wpa_s;
4600 group->p2p_in_provisioning = 1;
4601 group->p2p_fallback_to_go_neg = wpa_s->p2p_fallback_to_go_neg;
4603 os_memset(&res, 0, sizeof(res));
4604 os_memcpy(res.peer_device_addr, wpa_s->pending_join_dev_addr, ETH_ALEN);
4605 os_memcpy(res.peer_interface_addr, wpa_s->pending_join_iface_addr,
4607 res.wps_method = wpa_s->pending_join_wps_method;
4608 if (freq && ssid && ssid_len) {
4610 res.ssid_len = ssid_len;
4611 os_memcpy(res.ssid, ssid, ssid_len);
4613 bss = wpa_bss_get_bssid_latest(wpa_s,
4614 wpa_s->pending_join_iface_addr);
4616 res.freq = bss->freq;
4617 res.ssid_len = bss->ssid_len;
4618 os_memcpy(res.ssid, bss->ssid, bss->ssid_len);
4619 wpa_printf(MSG_DEBUG, "P2P: Join target GO operating frequency from BSS table: %d MHz (SSID %s)",
4621 wpa_ssid_txt(bss->ssid, bss->ssid_len));
4625 if (wpa_s->off_channel_freq || wpa_s->roc_waiting_drv_freq) {
4626 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel prior to "
4628 wpa_drv_cancel_remain_on_channel(wpa_s);
4629 wpa_s->off_channel_freq = 0;
4630 wpa_s->roc_waiting_drv_freq = 0;
4632 wpas_start_wps_enrollee(group, &res);
4635 * Allow a longer timeout for join-a-running-group than normal 15
4636 * second group formation timeout since the GO may not have authorized
4637 * our connection yet.
4639 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s, NULL);
4640 eloop_register_timeout(60, 0, wpas_p2p_group_formation_timeout,
4647 static int wpas_p2p_setup_freqs(struct wpa_supplicant *wpa_s, int freq,
4648 int *force_freq, int *pref_freq, int go)
4650 struct wpa_used_freq_data *freqs;
4651 int res, best_freq, num_unused;
4652 unsigned int freq_in_use = 0, num, i;
4654 freqs = os_calloc(wpa_s->num_multichan_concurrent,
4655 sizeof(struct wpa_used_freq_data));
4659 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
4660 wpa_s->num_multichan_concurrent);
4663 * It is possible that the total number of used frequencies is bigger
4664 * than the number of frequencies used for P2P, so get the system wide
4665 * number of unused frequencies.
4667 num_unused = wpas_p2p_num_unused_channels(wpa_s);
4669 wpa_printf(MSG_DEBUG,
4670 "P2P: Setup freqs: freq=%d num_MCC=%d shared_freqs=%u num_unused=%d",
4671 freq, wpa_s->num_multichan_concurrent, num, num_unused);
4676 ret = p2p_supported_freq(wpa_s->global->p2p, freq);
4678 ret = p2p_supported_freq_cli(wpa_s->global->p2p, freq);
4680 wpa_printf(MSG_DEBUG, "P2P: The forced channel "
4681 "(%u MHz) is not supported for P2P uses",
4687 for (i = 0; i < num; i++) {
4688 if (freqs[i].freq == freq)
4692 if (num_unused <= 0 && !freq_in_use) {
4693 wpa_printf(MSG_DEBUG, "P2P: Cannot start P2P group on %u MHz as there are no available channels",
4698 wpa_printf(MSG_DEBUG, "P2P: Trying to force us to use the "
4699 "requested channel (%u MHz)", freq);
4704 best_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
4706 /* We have a candidate frequency to use */
4707 if (best_freq > 0) {
4708 if (*pref_freq == 0 && num_unused > 0) {
4709 wpa_printf(MSG_DEBUG, "P2P: Try to prefer a frequency (%u MHz) we are already using",
4711 *pref_freq = best_freq;
4713 wpa_printf(MSG_DEBUG, "P2P: Try to force us to use frequency (%u MHz) which is already in use",
4715 *force_freq = best_freq;
4717 } else if (num_unused > 0) {
4718 wpa_printf(MSG_DEBUG,
4719 "P2P: Current operating channels are not available for P2P. Try to use another channel");
4722 wpa_printf(MSG_DEBUG,
4723 "P2P: All channels are in use and none of them are P2P enabled. Cannot start P2P group");
4737 * wpas_p2p_connect - Request P2P Group Formation to be started
4738 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4739 * @peer_addr: Address of the peer P2P Device
4740 * @pin: PIN to use during provisioning or %NULL to indicate PBC mode
4741 * @persistent_group: Whether to create a persistent group
4742 * @auto_join: Whether to select join vs. GO Negotiation automatically
4743 * @join: Whether to join an existing group (as a client) instead of starting
4744 * Group Owner negotiation; @peer_addr is BSSID in that case
4745 * @auth: Whether to only authorize the connection instead of doing that and
4746 * initiating Group Owner negotiation
4747 * @go_intent: GO Intent or -1 to use default
4748 * @freq: Frequency for the group or 0 for auto-selection
4749 * @persistent_id: Persistent group credentials to use for forcing GO
4750 * parameters or -1 to generate new values (SSID/passphrase)
4751 * @pd: Whether to send Provision Discovery prior to GO Negotiation as an
4752 * interoperability workaround when initiating group formation
4753 * @ht40: Start GO with 40 MHz channel width
4754 * @vht: Start GO with VHT support
4755 * Returns: 0 or new PIN (if pin was %NULL) on success, -1 on unspecified
4756 * failure, -2 on failure due to channel not currently available,
4757 * -3 if forced channel is not supported
4759 int wpas_p2p_connect(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
4760 const char *pin, enum p2p_wps_method wps_method,
4761 int persistent_group, int auto_join, int join, int auth,
4762 int go_intent, int freq, int persistent_id, int pd,
4765 int force_freq = 0, pref_freq = 0;
4767 enum wpa_driver_if_type iftype;
4769 struct wpa_ssid *ssid = NULL;
4771 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4774 if (persistent_id >= 0) {
4775 ssid = wpa_config_get_network(wpa_s->conf, persistent_id);
4776 if (ssid == NULL || ssid->disabled != 2 ||
4777 ssid->mode != WPAS_MODE_P2P_GO)
4781 os_free(wpa_s->global->add_psk);
4782 wpa_s->global->add_psk = NULL;
4784 wpa_s->global->p2p_fail_on_wps_complete = 0;
4787 go_intent = wpa_s->conf->p2p_go_intent;
4790 wpa_s->p2p_long_listen = 0;
4792 wpa_s->p2p_wps_method = wps_method;
4793 wpa_s->p2p_persistent_group = !!persistent_group;
4794 wpa_s->p2p_persistent_id = persistent_id;
4795 wpa_s->p2p_go_intent = go_intent;
4796 wpa_s->p2p_connect_freq = freq;
4797 wpa_s->p2p_fallback_to_go_neg = 0;
4798 wpa_s->p2p_pd_before_go_neg = !!pd;
4799 wpa_s->p2p_go_ht40 = !!ht40;
4800 wpa_s->p2p_go_vht = !!vht;
4803 os_strlcpy(wpa_s->p2p_pin, pin, sizeof(wpa_s->p2p_pin));
4804 else if (wps_method == WPS_PIN_DISPLAY) {
4805 ret = wps_generate_pin();
4806 os_snprintf(wpa_s->p2p_pin, sizeof(wpa_s->p2p_pin), "%08d",
4808 wpa_printf(MSG_DEBUG, "P2P: Randomly generated PIN: %s",
4811 wpa_s->p2p_pin[0] = '\0';
4813 if (join || auto_join) {
4814 u8 iface_addr[ETH_ALEN], dev_addr[ETH_ALEN];
4816 wpa_printf(MSG_DEBUG, "P2P: Authorize invitation to "
4817 "connect a running group from " MACSTR,
4818 MAC2STR(peer_addr));
4819 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
4822 os_memcpy(dev_addr, peer_addr, ETH_ALEN);
4823 if (p2p_get_interface_addr(wpa_s->global->p2p, peer_addr,
4825 os_memcpy(iface_addr, peer_addr, ETH_ALEN);
4826 p2p_get_dev_addr(wpa_s->global->p2p, peer_addr,
4830 os_get_reltime(&wpa_s->p2p_auto_started);
4831 wpa_printf(MSG_DEBUG, "P2P: Auto join started at "
4833 wpa_s->p2p_auto_started.sec,
4834 wpa_s->p2p_auto_started.usec);
4836 wpa_s->user_initiated_pd = 1;
4837 if (wpas_p2p_join(wpa_s, iface_addr, dev_addr, wps_method,
4838 auto_join, freq, NULL, 0) < 0)
4843 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
4847 wpas_p2p_set_own_freq_preference(wpa_s,
4848 force_freq ? force_freq : pref_freq);
4850 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
4852 if (wpa_s->create_p2p_iface) {
4853 /* Prepare to add a new interface for the group */
4854 iftype = WPA_IF_P2P_GROUP;
4855 if (go_intent == 15)
4856 iftype = WPA_IF_P2P_GO;
4857 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
4858 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
4859 "interface for the group");
4863 if_addr = wpa_s->pending_interface_addr;
4865 if_addr = wpa_s->own_addr;
4868 if (wpas_p2p_auth_go_neg(wpa_s, peer_addr, wps_method,
4870 force_freq, persistent_group, ssid,
4876 if (wpas_p2p_start_go_neg(wpa_s, peer_addr, wps_method,
4877 go_intent, if_addr, force_freq,
4878 persistent_group, ssid, pref_freq) < 0) {
4879 if (wpa_s->create_p2p_iface)
4880 wpas_p2p_remove_pending_group_interface(wpa_s);
4888 * wpas_p2p_remain_on_channel_cb - Indication of remain-on-channel start
4889 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4890 * @freq: Frequency of the channel in MHz
4891 * @duration: Duration of the stay on the channel in milliseconds
4893 * This callback is called when the driver indicates that it has started the
4894 * requested remain-on-channel duration.
4896 void wpas_p2p_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4897 unsigned int freq, unsigned int duration)
4899 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4901 if (wpa_s->off_channel_freq == wpa_s->pending_listen_freq) {
4902 p2p_listen_cb(wpa_s->global->p2p, wpa_s->pending_listen_freq,
4903 wpa_s->pending_listen_duration);
4904 wpa_s->pending_listen_freq = 0;
4906 wpa_printf(MSG_DEBUG, "P2P: Ignore remain-on-channel callback (off_channel_freq=%u pending_listen_freq=%d freq=%u duration=%u)",
4907 wpa_s->off_channel_freq, wpa_s->pending_listen_freq,
4913 static int wpas_p2p_listen_start(struct wpa_supplicant *wpa_s,
4914 unsigned int timeout)
4916 /* Limit maximum Listen state time based on driver limitation. */
4917 if (timeout > wpa_s->max_remain_on_chan)
4918 timeout = wpa_s->max_remain_on_chan;
4920 return p2p_listen(wpa_s->global->p2p, timeout);
4925 * wpas_p2p_cancel_remain_on_channel_cb - Remain-on-channel timeout
4926 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4927 * @freq: Frequency of the channel in MHz
4929 * This callback is called when the driver indicates that a remain-on-channel
4930 * operation has been completed, i.e., the duration on the requested channel
4933 void wpas_p2p_cancel_remain_on_channel_cb(struct wpa_supplicant *wpa_s,
4936 wpa_printf(MSG_DEBUG, "P2P: Cancel remain-on-channel callback "
4937 "(p2p_long_listen=%d ms pending_action_tx=%p)",
4938 wpa_s->p2p_long_listen, offchannel_pending_action_tx(wpa_s));
4939 wpas_p2p_listen_work_done(wpa_s);
4940 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
4942 if (p2p_listen_end(wpa_s->global->p2p, freq) > 0)
4943 return; /* P2P module started a new operation */
4944 if (offchannel_pending_action_tx(wpa_s))
4946 if (wpa_s->p2p_long_listen > 0)
4947 wpa_s->p2p_long_listen -= wpa_s->max_remain_on_chan;
4948 if (wpa_s->p2p_long_listen > 0) {
4949 wpa_printf(MSG_DEBUG, "P2P: Continuing long Listen state");
4950 wpas_p2p_listen_start(wpa_s, wpa_s->p2p_long_listen);
4953 * When listen duration is over, stop listen & update p2p_state
4956 p2p_stop_listen(wpa_s->global->p2p);
4962 * wpas_p2p_group_remove - Remove a P2P group
4963 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
4964 * @ifname: Network interface name of the group interface or "*" to remove all
4966 * Returns: 0 on success, -1 on failure
4968 * This function is used to remove a P2P group. This can be used to disconnect
4969 * from a group in which the local end is a P2P Client or to end a P2P Group in
4970 * case the local end is the Group Owner. If a virtual network interface was
4971 * created for this group, that interface will be removed. Otherwise, only the
4972 * configured P2P group network will be removed from the interface.
4974 int wpas_p2p_group_remove(struct wpa_supplicant *wpa_s, const char *ifname)
4976 struct wpa_global *global = wpa_s->global;
4978 if (os_strcmp(ifname, "*") == 0) {
4979 struct wpa_supplicant *prev;
4980 wpa_s = global->ifaces;
4983 wpa_s = wpa_s->next;
4984 if (prev->p2p_group_interface !=
4985 NOT_P2P_GROUP_INTERFACE ||
4986 (prev->current_ssid &&
4987 prev->current_ssid->p2p_group))
4988 wpas_p2p_disconnect(prev);
4993 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
4994 if (os_strcmp(wpa_s->ifname, ifname) == 0)
4998 return wpas_p2p_disconnect(wpa_s);
5002 static int wpas_p2p_select_go_freq(struct wpa_supplicant *wpa_s, int freq)
5007 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 2.4 GHz "
5009 if (wpa_s->best_24_freq > 0 &&
5010 p2p_supported_freq_go(wpa_s->global->p2p,
5011 wpa_s->best_24_freq)) {
5012 freq = wpa_s->best_24_freq;
5013 wpa_printf(MSG_DEBUG, "P2P: Use best 2.4 GHz band "
5014 "channel: %d MHz", freq);
5016 os_get_random((u8 *) &r, sizeof(r));
5017 freq = 2412 + (r % 3) * 25;
5018 wpa_printf(MSG_DEBUG, "P2P: Use random 2.4 GHz band "
5019 "channel: %d MHz", freq);
5024 wpa_printf(MSG_DEBUG, "P2P: Request to start GO on 5 GHz "
5026 if (wpa_s->best_5_freq > 0 &&
5027 p2p_supported_freq_go(wpa_s->global->p2p,
5028 wpa_s->best_5_freq)) {
5029 freq = wpa_s->best_5_freq;
5030 wpa_printf(MSG_DEBUG, "P2P: Use best 5 GHz band "
5031 "channel: %d MHz", freq);
5033 os_get_random((u8 *) &r, sizeof(r));
5034 freq = 5180 + (r % 4) * 20;
5035 if (!p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5036 wpa_printf(MSG_DEBUG, "P2P: Could not select "
5037 "5 GHz channel for P2P group");
5040 wpa_printf(MSG_DEBUG, "P2P: Use random 5 GHz band "
5041 "channel: %d MHz", freq);
5045 if (freq > 0 && !p2p_supported_freq_go(wpa_s->global->p2p, freq)) {
5046 wpa_printf(MSG_DEBUG, "P2P: The forced channel for GO "
5047 "(%u MHz) is not supported for P2P uses",
5056 static int wpas_p2p_select_freq_no_pref(struct wpa_supplicant *wpa_s,
5057 struct p2p_go_neg_results *params,
5058 const struct p2p_channels *channels)
5062 /* first try some random selection of the social channels */
5063 os_get_random((u8 *) &r, sizeof(r));
5065 for (i = 0; i < 3; i++) {
5066 params->freq = 2412 + ((r + i) % 3) * 25;
5067 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5068 freq_included(channels, params->freq))
5072 /* try all channels in reg. class 81 */
5073 for (i = 0; i < 11; i++) {
5074 params->freq = 2412 + i * 5;
5075 if (!wpas_p2p_disallowed_freq(wpa_s->global, params->freq) &&
5076 freq_included(channels, params->freq))
5080 wpa_printf(MSG_DEBUG, "P2P: No 2.4 GHz channel allowed");
5083 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz (no preference known)",
5089 static int wpas_p2p_init_go_params(struct wpa_supplicant *wpa_s,
5090 struct p2p_go_neg_results *params,
5091 int freq, int ht40, int vht,
5092 const struct p2p_channels *channels)
5094 struct wpa_used_freq_data *freqs;
5095 unsigned int pref_freq, cand_freq;
5096 unsigned int num, i;
5098 os_memset(params, 0, sizeof(*params));
5099 params->role_go = 1;
5100 params->ht40 = ht40;
5103 if (!freq_included(channels, freq)) {
5104 wpa_printf(MSG_DEBUG, "P2P: Forced GO freq %d MHz not "
5108 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on forced "
5109 "frequency %d MHz", freq);
5110 params->freq = freq;
5111 } else if (wpa_s->conf->p2p_oper_reg_class == 81 &&
5112 wpa_s->conf->p2p_oper_channel >= 1 &&
5113 wpa_s->conf->p2p_oper_channel <= 11 &&
5114 freq_included(channels,
5115 2407 + 5 * wpa_s->conf->p2p_oper_channel)) {
5116 params->freq = 2407 + 5 * wpa_s->conf->p2p_oper_channel;
5117 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5118 "frequency %d MHz", params->freq);
5119 } else if ((wpa_s->conf->p2p_oper_reg_class == 115 ||
5120 wpa_s->conf->p2p_oper_reg_class == 116 ||
5121 wpa_s->conf->p2p_oper_reg_class == 117 ||
5122 wpa_s->conf->p2p_oper_reg_class == 124 ||
5123 wpa_s->conf->p2p_oper_reg_class == 126 ||
5124 wpa_s->conf->p2p_oper_reg_class == 127) &&
5125 freq_included(channels,
5126 5000 + 5 * wpa_s->conf->p2p_oper_channel)) {
5127 params->freq = 5000 + 5 * wpa_s->conf->p2p_oper_channel;
5128 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on configured "
5129 "frequency %d MHz", params->freq);
5130 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5131 wpa_s->best_overall_freq > 0 &&
5132 p2p_supported_freq_go(wpa_s->global->p2p,
5133 wpa_s->best_overall_freq) &&
5134 freq_included(channels, wpa_s->best_overall_freq)) {
5135 params->freq = wpa_s->best_overall_freq;
5136 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best overall "
5137 "channel %d MHz", params->freq);
5138 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5139 wpa_s->best_24_freq > 0 &&
5140 p2p_supported_freq_go(wpa_s->global->p2p,
5141 wpa_s->best_24_freq) &&
5142 freq_included(channels, wpa_s->best_24_freq)) {
5143 params->freq = wpa_s->best_24_freq;
5144 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 2.4 GHz "
5145 "channel %d MHz", params->freq);
5146 } else if (wpa_s->conf->p2p_oper_channel == 0 &&
5147 wpa_s->best_5_freq > 0 &&
5148 p2p_supported_freq_go(wpa_s->global->p2p,
5149 wpa_s->best_5_freq) &&
5150 freq_included(channels, wpa_s->best_5_freq)) {
5151 params->freq = wpa_s->best_5_freq;
5152 wpa_printf(MSG_DEBUG, "P2P: Set GO freq based on best 5 GHz "
5153 "channel %d MHz", params->freq);
5154 } else if ((pref_freq = p2p_get_pref_freq(wpa_s->global->p2p,
5156 params->freq = pref_freq;
5157 wpa_printf(MSG_DEBUG, "P2P: Set GO freq %d MHz from preferred "
5158 "channels", params->freq);
5160 /* no preference, select some channel */
5161 if (wpas_p2p_select_freq_no_pref(wpa_s, params, channels) < 0)
5165 freqs = os_calloc(wpa_s->num_multichan_concurrent,
5166 sizeof(struct wpa_used_freq_data));
5170 num = wpas_p2p_valid_oper_freqs(wpa_s, freqs,
5171 wpa_s->num_multichan_concurrent);
5173 cand_freq = wpas_p2p_pick_best_used_freq(wpa_s, freqs, num);
5175 /* First try the best used frequency if possible */
5176 if (!freq && cand_freq > 0 && freq_included(channels, cand_freq)) {
5177 params->freq = cand_freq;
5179 /* Try any of the used frequencies */
5180 for (i = 0; i < num; i++) {
5181 if (freq_included(channels, freqs[i].freq)) {
5182 wpa_printf(MSG_DEBUG, "P2P: Force GO on a channel we are already using (%u MHz)",
5184 params->freq = freqs[i].freq;
5190 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5191 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using");
5195 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on any of the channels we are already using. Use one of the free channels");
5199 for (i = 0; i < num; i++) {
5200 if (freqs[i].freq == freq)
5205 if (wpas_p2p_num_unused_channels(wpa_s) <= 0) {
5207 wpa_printf(MSG_DEBUG, "P2P: Cannot force GO on freq (%u MHz) as all the channels are in use", freq);
5211 wpa_printf(MSG_DEBUG, "P2P: Use one of the free channels");
5221 static struct wpa_supplicant *
5222 wpas_p2p_get_group_iface(struct wpa_supplicant *wpa_s, int addr_allocated,
5225 struct wpa_supplicant *group_wpa_s;
5227 if (!wpas_p2p_create_iface(wpa_s)) {
5228 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use same interface for group "
5230 wpa_s->p2p_first_connection_timeout = 0;
5234 if (wpas_p2p_add_group_interface(wpa_s, go ? WPA_IF_P2P_GO :
5235 WPA_IF_P2P_CLIENT) < 0) {
5236 wpa_msg_global(wpa_s, MSG_ERROR,
5237 "P2P: Failed to add group interface");
5240 group_wpa_s = wpas_p2p_init_group_interface(wpa_s, go);
5241 if (group_wpa_s == NULL) {
5242 wpa_msg_global(wpa_s, MSG_ERROR,
5243 "P2P: Failed to initialize group interface");
5244 wpas_p2p_remove_pending_group_interface(wpa_s);
5248 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use separate group interface %s",
5249 group_wpa_s->ifname);
5250 group_wpa_s->p2p_first_connection_timeout = 0;
5256 * wpas_p2p_group_add - Add a new P2P group with local end as Group Owner
5257 * @wpa_s: Pointer to wpa_supplicant data from wpa_supplicant_add_iface()
5258 * @persistent_group: Whether to create a persistent group
5259 * @freq: Frequency for the group or 0 to indicate no hardcoding
5260 * @ht40: Start GO with 40 MHz channel width
5261 * @vht: Start GO with VHT support
5262 * Returns: 0 on success, -1 on failure
5264 * This function creates a new P2P group with the local end as the Group Owner,
5265 * i.e., without using Group Owner Negotiation.
5267 int wpas_p2p_group_add(struct wpa_supplicant *wpa_s, int persistent_group,
5268 int freq, int ht40, int vht)
5270 struct p2p_go_neg_results params;
5272 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5275 os_free(wpa_s->global->add_psk);
5276 wpa_s->global->add_psk = NULL;
5278 /* Make sure we are not running find during connection establishment */
5279 wpa_printf(MSG_DEBUG, "P2P: Stop any on-going P2P FIND");
5280 wpas_p2p_stop_find_oper(wpa_s);
5282 freq = wpas_p2p_select_go_freq(wpa_s, freq);
5286 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, NULL))
5289 !p2p_supported_freq_go(wpa_s->global->p2p, params.freq)) {
5290 wpa_printf(MSG_DEBUG, "P2P: The selected channel for GO "
5291 "(%u MHz) is not supported for P2P uses",
5295 p2p_go_params(wpa_s->global->p2p, ¶ms);
5296 params.persistent_group = persistent_group;
5298 wpa_s = wpas_p2p_get_group_iface(wpa_s, 0, 1);
5301 wpas_start_wps_go(wpa_s, ¶ms, 0);
5307 static int wpas_start_p2p_client(struct wpa_supplicant *wpa_s,
5308 struct wpa_ssid *params, int addr_allocated,
5311 struct wpa_ssid *ssid;
5313 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 0);
5316 wpa_s->p2p_last_4way_hs_fail = NULL;
5318 wpa_supplicant_ap_deinit(wpa_s);
5320 ssid = wpa_config_add_network(wpa_s->conf);
5323 wpa_config_set_network_defaults(ssid);
5324 ssid->temporary = 1;
5325 ssid->proto = WPA_PROTO_RSN;
5326 ssid->pairwise_cipher = WPA_CIPHER_CCMP;
5327 ssid->group_cipher = WPA_CIPHER_CCMP;
5328 ssid->key_mgmt = WPA_KEY_MGMT_PSK;
5329 ssid->ssid = os_malloc(params->ssid_len);
5330 if (ssid->ssid == NULL) {
5331 wpa_config_remove_network(wpa_s->conf, ssid->id);
5334 os_memcpy(ssid->ssid, params->ssid, params->ssid_len);
5335 ssid->ssid_len = params->ssid_len;
5336 ssid->p2p_group = 1;
5337 ssid->export_keys = 1;
5338 if (params->psk_set) {
5339 os_memcpy(ssid->psk, params->psk, 32);
5342 if (params->passphrase)
5343 ssid->passphrase = os_strdup(params->passphrase);
5345 wpa_s->show_group_started = 1;
5346 wpa_s->p2p_in_invitation = 1;
5347 wpa_s->p2p_invite_go_freq = freq;
5349 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5351 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5352 wpas_p2p_group_formation_timeout,
5353 wpa_s->parent, NULL);
5354 wpa_supplicant_select_network(wpa_s, ssid);
5360 int wpas_p2p_group_add_persistent(struct wpa_supplicant *wpa_s,
5361 struct wpa_ssid *ssid, int addr_allocated,
5362 int force_freq, int neg_freq, int ht40,
5363 int vht, const struct p2p_channels *channels,
5364 int connection_timeout)
5366 struct p2p_go_neg_results params;
5369 if (ssid->disabled != 2 || ssid->ssid == NULL)
5372 if (wpas_get_p2p_group(wpa_s, ssid->ssid, ssid->ssid_len, &go) &&
5373 go == (ssid->mode == WPAS_MODE_P2P_GO)) {
5374 wpa_printf(MSG_DEBUG, "P2P: Requested persistent group is "
5379 os_free(wpa_s->global->add_psk);
5380 wpa_s->global->add_psk = NULL;
5382 /* Make sure we are not running find during connection establishment */
5383 wpas_p2p_stop_find_oper(wpa_s);
5385 wpa_s->p2p_fallback_to_go_neg = 0;
5387 if (force_freq > 0) {
5388 freq = wpas_p2p_select_go_freq(wpa_s, force_freq);
5392 freq = wpas_p2p_select_go_freq(wpa_s, neg_freq);
5393 if (freq < 0 || (freq > 0 && !freq_included(channels, freq)))
5397 if (ssid->mode == WPAS_MODE_INFRA)
5398 return wpas_start_p2p_client(wpa_s, ssid, addr_allocated, freq);
5400 if (ssid->mode != WPAS_MODE_P2P_GO)
5403 if (wpas_p2p_init_go_params(wpa_s, ¶ms, freq, ht40, vht, channels))
5407 params.psk_set = ssid->psk_set;
5409 os_memcpy(params.psk, ssid->psk, sizeof(params.psk));
5410 if (ssid->passphrase) {
5411 if (os_strlen(ssid->passphrase) >= sizeof(params.passphrase)) {
5412 wpa_printf(MSG_ERROR, "P2P: Invalid passphrase in "
5413 "persistent group");
5416 os_strlcpy(params.passphrase, ssid->passphrase,
5417 sizeof(params.passphrase));
5419 os_memcpy(params.ssid, ssid->ssid, ssid->ssid_len);
5420 params.ssid_len = ssid->ssid_len;
5421 params.persistent_group = 1;
5423 wpa_s = wpas_p2p_get_group_iface(wpa_s, addr_allocated, 1);
5427 wpa_s->p2p_first_connection_timeout = connection_timeout;
5428 wpas_start_wps_go(wpa_s, ¶ms, 0);
5434 static void wpas_p2p_ie_update(void *ctx, struct wpabuf *beacon_ies,
5435 struct wpabuf *proberesp_ies)
5437 struct wpa_supplicant *wpa_s = ctx;
5438 if (wpa_s->ap_iface) {
5439 struct hostapd_data *hapd = wpa_s->ap_iface->bss[0];
5440 if (!(hapd->conf->p2p & P2P_GROUP_OWNER)) {
5441 wpabuf_free(beacon_ies);
5442 wpabuf_free(proberesp_ies);
5446 wpabuf_free(hapd->p2p_beacon_ie);
5447 hapd->p2p_beacon_ie = beacon_ies;
5449 wpabuf_free(hapd->p2p_probe_resp_ie);
5450 hapd->p2p_probe_resp_ie = proberesp_ies;
5452 wpabuf_free(beacon_ies);
5453 wpabuf_free(proberesp_ies);
5455 wpa_supplicant_ap_update_beacon(wpa_s);
5459 static void wpas_p2p_idle_update(void *ctx, int idle)
5461 struct wpa_supplicant *wpa_s = ctx;
5462 if (!wpa_s->ap_iface)
5464 wpa_printf(MSG_DEBUG, "P2P: GO - group %sidle", idle ? "" : "not ");
5466 if (wpa_s->global->p2p_fail_on_wps_complete &&
5467 wpa_s->p2p_in_provisioning) {
5468 wpas_p2p_grpform_fail_after_wps(wpa_s);
5471 wpas_p2p_set_group_idle_timeout(wpa_s);
5473 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL);
5477 struct p2p_group * wpas_p2p_group_init(struct wpa_supplicant *wpa_s,
5478 struct wpa_ssid *ssid)
5480 struct p2p_group *group;
5481 struct p2p_group_config *cfg;
5483 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5486 cfg = os_zalloc(sizeof(*cfg));
5490 if (ssid->p2p_persistent_group && wpa_s->conf->persistent_reconnect)
5491 cfg->persistent_group = 2;
5492 else if (ssid->p2p_persistent_group)
5493 cfg->persistent_group = 1;
5494 os_memcpy(cfg->interface_addr, wpa_s->own_addr, ETH_ALEN);
5495 if (wpa_s->max_stations &&
5496 wpa_s->max_stations < wpa_s->conf->max_num_sta)
5497 cfg->max_clients = wpa_s->max_stations;
5499 cfg->max_clients = wpa_s->conf->max_num_sta;
5500 os_memcpy(cfg->ssid, ssid->ssid, ssid->ssid_len);
5501 cfg->ssid_len = ssid->ssid_len;
5502 cfg->freq = ssid->frequency;
5503 cfg->cb_ctx = wpa_s;
5504 cfg->ie_update = wpas_p2p_ie_update;
5505 cfg->idle_update = wpas_p2p_idle_update;
5507 group = p2p_group_init(wpa_s->global->p2p, cfg);
5510 if (ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION)
5511 p2p_group_notif_formation_done(group);
5512 wpa_s->p2p_group = group;
5517 void wpas_p2p_wps_success(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5520 struct wpa_ssid *ssid = wpa_s->current_ssid;
5522 if (!wpa_s->p2p_in_provisioning) {
5523 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS success event - P2P "
5524 "provisioning not in progress");
5528 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5529 u8 go_dev_addr[ETH_ALEN];
5530 os_memcpy(go_dev_addr, wpa_s->bssid, ETH_ALEN);
5531 wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
5533 /* Clear any stored provisioning info */
5534 p2p_clear_provisioning_info(wpa_s->global->p2p, go_dev_addr);
5537 eloop_cancel_timeout(wpas_p2p_group_formation_timeout, wpa_s->parent,
5539 wpa_s->p2p_go_group_formation_completed = 1;
5540 if (ssid && ssid->mode == WPAS_MODE_INFRA) {
5542 * Use a separate timeout for initial data connection to
5543 * complete to allow the group to be removed automatically if
5544 * something goes wrong in this step before the P2P group idle
5545 * timeout mechanism is taken into use.
5547 wpa_dbg(wpa_s, MSG_DEBUG,
5548 "P2P: Re-start group formation timeout (%d seconds) as client for initial connection",
5549 P2P_MAX_INITIAL_CONN_WAIT);
5550 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT, 0,
5551 wpas_p2p_group_formation_timeout,
5552 wpa_s->parent, NULL);
5555 * Use a separate timeout for initial data connection to
5556 * complete to allow the group to be removed automatically if
5557 * the client does not complete data connection successfully.
5559 wpa_dbg(wpa_s, MSG_DEBUG,
5560 "P2P: Re-start group formation timeout (%d seconds) as GO for initial connection",
5561 P2P_MAX_INITIAL_CONN_WAIT_GO);
5562 eloop_register_timeout(P2P_MAX_INITIAL_CONN_WAIT_GO, 0,
5563 wpas_p2p_group_formation_timeout,
5564 wpa_s->parent, NULL);
5566 * Complete group formation on first successful data connection
5568 wpa_s->p2p_go_group_formation_completed = 0;
5570 if (wpa_s->global->p2p)
5571 p2p_wps_success_cb(wpa_s->global->p2p, peer_addr);
5572 wpas_group_formation_completed(wpa_s, 1);
5576 void wpas_p2p_wps_failed(struct wpa_supplicant *wpa_s,
5577 struct wps_event_fail *fail)
5579 if (!wpa_s->p2p_in_provisioning) {
5580 wpa_printf(MSG_DEBUG, "P2P: Ignore WPS fail event - P2P "
5581 "provisioning not in progress");
5585 if (wpa_s->go_params) {
5586 p2p_clear_provisioning_info(
5588 wpa_s->go_params->peer_device_addr);
5591 wpas_notify_p2p_wps_failed(wpa_s, fail);
5593 if (wpa_s == wpa_s->global->p2p_group_formation) {
5595 * Allow some time for the failed WPS negotiation exchange to
5596 * complete, but remove the group since group formation cannot
5597 * succeed after provisioning failure.
5599 wpa_printf(MSG_DEBUG, "P2P: WPS step failed during group formation - reject connection from timeout");
5600 wpa_s->global->p2p_fail_on_wps_complete = 1;
5601 eloop_deplete_timeout(0, 50000,
5602 wpas_p2p_group_formation_timeout,
5603 wpa_s->parent, NULL);
5608 int wpas_p2p_wps_eapol_cb(struct wpa_supplicant *wpa_s)
5610 if (!wpa_s->global->p2p_fail_on_wps_complete ||
5611 !wpa_s->p2p_in_provisioning)
5614 wpas_p2p_grpform_fail_after_wps(wpa_s);
5620 int wpas_p2p_prov_disc(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5621 const char *config_method,
5622 enum wpas_p2p_prov_disc_use use)
5626 wpa_s->p2p_fallback_to_go_neg = 0;
5627 wpa_s->pending_pd_use = NORMAL_PD;
5628 if (os_strncmp(config_method, "display", 7) == 0)
5629 config_methods = WPS_CONFIG_DISPLAY;
5630 else if (os_strncmp(config_method, "keypad", 6) == 0)
5631 config_methods = WPS_CONFIG_KEYPAD;
5632 else if (os_strncmp(config_method, "pbc", 3) == 0 ||
5633 os_strncmp(config_method, "pushbutton", 10) == 0)
5634 config_methods = WPS_CONFIG_PUSHBUTTON;
5636 wpa_printf(MSG_DEBUG, "P2P: Unknown config method");
5640 if (use == WPAS_P2P_PD_AUTO) {
5641 os_memcpy(wpa_s->pending_join_dev_addr, peer_addr, ETH_ALEN);
5642 wpa_s->pending_pd_config_methods = config_methods;
5643 wpa_s->p2p_auto_pd = 1;
5644 wpa_s->p2p_auto_join = 0;
5645 wpa_s->pending_pd_before_join = 0;
5646 wpa_s->auto_pd_scan_retry = 0;
5647 wpas_p2p_stop_find(wpa_s);
5648 wpa_s->p2p_join_scan_count = 0;
5649 os_get_reltime(&wpa_s->p2p_auto_started);
5650 wpa_printf(MSG_DEBUG, "P2P: Auto PD started at %ld.%06ld",
5651 wpa_s->p2p_auto_started.sec,
5652 wpa_s->p2p_auto_started.usec);
5653 wpas_p2p_join_scan(wpa_s, NULL);
5657 if (wpa_s->global->p2p == NULL || wpa_s->global->p2p_disabled)
5660 return p2p_prov_disc_req(wpa_s->global->p2p, peer_addr,
5661 config_methods, use == WPAS_P2P_PD_FOR_JOIN,
5666 int wpas_p2p_scan_result_text(const u8 *ies, size_t ies_len, char *buf,
5669 return p2p_scan_result_text(ies, ies_len, buf, end);
5673 static void wpas_p2p_clear_pending_action_tx(struct wpa_supplicant *wpa_s)
5675 if (!offchannel_pending_action_tx(wpa_s))
5678 wpa_printf(MSG_DEBUG, "P2P: Drop pending Action TX due to new "
5679 "operation request");
5680 offchannel_clear_pending_action_tx(wpa_s);
5684 int wpas_p2p_find(struct wpa_supplicant *wpa_s, unsigned int timeout,
5685 enum p2p_discovery_type type,
5686 unsigned int num_req_dev_types, const u8 *req_dev_types,
5687 const u8 *dev_id, unsigned int search_delay)
5689 wpas_p2p_clear_pending_action_tx(wpa_s);
5690 wpa_s->p2p_long_listen = 0;
5692 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL ||
5693 wpa_s->p2p_in_provisioning)
5696 wpa_supplicant_cancel_sched_scan(wpa_s);
5698 return p2p_find(wpa_s->global->p2p, timeout, type,
5699 num_req_dev_types, req_dev_types, dev_id,
5704 static int wpas_p2p_stop_find_oper(struct wpa_supplicant *wpa_s)
5706 wpas_p2p_clear_pending_action_tx(wpa_s);
5707 wpa_s->p2p_long_listen = 0;
5708 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5709 eloop_cancel_timeout(wpas_p2p_join_scan, wpa_s, NULL);
5711 if (wpa_s->global->p2p)
5712 p2p_stop_find(wpa_s->global->p2p);
5718 void wpas_p2p_stop_find(struct wpa_supplicant *wpa_s)
5720 if (wpas_p2p_stop_find_oper(wpa_s) > 0)
5722 wpas_p2p_remove_pending_group_interface(wpa_s);
5726 static void wpas_p2p_long_listen_timeout(void *eloop_ctx, void *timeout_ctx)
5728 struct wpa_supplicant *wpa_s = eloop_ctx;
5729 wpa_s->p2p_long_listen = 0;
5733 int wpas_p2p_listen(struct wpa_supplicant *wpa_s, unsigned int timeout)
5737 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5740 wpa_supplicant_cancel_sched_scan(wpa_s);
5741 wpas_p2p_clear_pending_action_tx(wpa_s);
5745 * This is a request for unlimited Listen state. However, at
5746 * least for now, this is mapped to a Listen state for one
5751 eloop_cancel_timeout(wpas_p2p_long_listen_timeout, wpa_s, NULL);
5752 wpa_s->p2p_long_listen = 0;
5755 * Stop previous find/listen operation to avoid trying to request a new
5756 * remain-on-channel operation while the driver is still running the
5759 if (wpa_s->global->p2p)
5760 p2p_stop_find(wpa_s->global->p2p);
5762 res = wpas_p2p_listen_start(wpa_s, timeout * 1000);
5763 if (res == 0 && timeout * 1000 > wpa_s->max_remain_on_chan) {
5764 wpa_s->p2p_long_listen = timeout * 1000;
5765 eloop_register_timeout(timeout, 0,
5766 wpas_p2p_long_listen_timeout,
5774 int wpas_p2p_assoc_req_ie(struct wpa_supplicant *wpa_s, struct wpa_bss *bss,
5775 u8 *buf, size_t len, int p2p_group)
5777 struct wpabuf *p2p_ie;
5780 if (wpa_s->global->p2p_disabled)
5782 if (wpa_s->global->p2p == NULL)
5787 p2p_ie = wpa_bss_get_vendor_ie_multi(bss, P2P_IE_VENDOR_TYPE);
5788 ret = p2p_assoc_req_ie(wpa_s->global->p2p, bss->bssid, buf, len,
5790 wpabuf_free(p2p_ie);
5796 int wpas_p2p_probe_req_rx(struct wpa_supplicant *wpa_s, const u8 *addr,
5797 const u8 *dst, const u8 *bssid,
5798 const u8 *ie, size_t ie_len, int ssi_signal)
5800 if (wpa_s->global->p2p_disabled)
5802 if (wpa_s->global->p2p == NULL)
5805 switch (p2p_probe_req_rx(wpa_s->global->p2p, addr, dst, bssid,
5807 case P2P_PREQ_NOT_P2P:
5808 wpas_notify_preq(wpa_s, addr, dst, bssid, ie, ie_len,
5811 case P2P_PREQ_MALFORMED:
5812 case P2P_PREQ_NOT_LISTEN:
5813 case P2P_PREQ_NOT_PROCESSED:
5814 default: /* make gcc happy */
5816 case P2P_PREQ_PROCESSED:
5822 void wpas_p2p_rx_action(struct wpa_supplicant *wpa_s, const u8 *da,
5823 const u8 *sa, const u8 *bssid,
5824 u8 category, const u8 *data, size_t len, int freq)
5826 if (wpa_s->global->p2p_disabled)
5828 if (wpa_s->global->p2p == NULL)
5831 p2p_rx_action(wpa_s->global->p2p, da, sa, bssid, category, data, len,
5836 void wpas_p2p_scan_ie(struct wpa_supplicant *wpa_s, struct wpabuf *ies)
5838 if (wpa_s->global->p2p_disabled)
5840 if (wpa_s->global->p2p == NULL)
5843 p2p_scan_ie(wpa_s->global->p2p, ies, NULL);
5847 static void wpas_p2p_group_deinit(struct wpa_supplicant *wpa_s)
5849 p2p_group_deinit(wpa_s->p2p_group);
5850 wpa_s->p2p_group = NULL;
5852 wpa_s->ap_configured_cb = NULL;
5853 wpa_s->ap_configured_cb_ctx = NULL;
5854 wpa_s->ap_configured_cb_data = NULL;
5855 wpa_s->connect_without_scan = NULL;
5859 int wpas_p2p_reject(struct wpa_supplicant *wpa_s, const u8 *addr)
5861 wpa_s->p2p_long_listen = 0;
5863 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5866 return p2p_reject(wpa_s->global->p2p, addr);
5870 /* Invite to reinvoke a persistent group */
5871 int wpas_p2p_invite(struct wpa_supplicant *wpa_s, const u8 *peer_addr,
5872 struct wpa_ssid *ssid, const u8 *go_dev_addr, int freq,
5873 int ht40, int vht, int pref_freq)
5875 enum p2p_invite_role role;
5879 int no_pref_freq_given = pref_freq == 0;
5881 wpa_s->global->p2p_invite_group = NULL;
5883 os_memcpy(wpa_s->p2p_auth_invite, peer_addr, ETH_ALEN);
5885 os_memset(wpa_s->p2p_auth_invite, 0, ETH_ALEN);
5887 wpa_s->p2p_persistent_go_freq = freq;
5888 wpa_s->p2p_go_ht40 = !!ht40;
5889 if (ssid->mode == WPAS_MODE_P2P_GO) {
5890 role = P2P_INVITE_ROLE_GO;
5891 if (peer_addr == NULL) {
5892 wpa_printf(MSG_DEBUG, "P2P: Missing peer "
5893 "address in invitation command");
5896 if (wpas_p2p_create_iface(wpa_s)) {
5897 if (wpas_p2p_add_group_interface(wpa_s,
5898 WPA_IF_P2P_GO) < 0) {
5899 wpa_printf(MSG_ERROR, "P2P: Failed to "
5900 "allocate a new interface for the "
5904 bssid = wpa_s->pending_interface_addr;
5906 bssid = wpa_s->own_addr;
5908 role = P2P_INVITE_ROLE_CLIENT;
5909 peer_addr = ssid->bssid;
5911 wpa_s->pending_invite_ssid_id = ssid->id;
5913 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5914 role == P2P_INVITE_ROLE_GO);
5918 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5921 if (wpa_s->parent->conf->p2p_ignore_shared_freq &&
5922 no_pref_freq_given && pref_freq > 0 &&
5923 wpa_s->num_multichan_concurrent > 1 &&
5924 wpas_p2p_num_unused_channels(wpa_s) > 0) {
5925 wpa_printf(MSG_DEBUG, "P2P: Ignore own channel preference %d MHz for invitation due to p2p_ignore_shared_freq=1 configuration",
5930 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
5931 ssid->ssid, ssid->ssid_len, force_freq, go_dev_addr,
5936 /* Invite to join an active group */
5937 int wpas_p2p_invite_group(struct wpa_supplicant *wpa_s, const char *ifname,
5938 const u8 *peer_addr, const u8 *go_dev_addr)
5940 struct wpa_global *global = wpa_s->global;
5941 enum p2p_invite_role role;
5943 struct wpa_ssid *ssid;
5945 int freq = 0, force_freq = 0, pref_freq = 0;
5948 wpa_s->p2p_persistent_go_freq = 0;
5949 wpa_s->p2p_go_ht40 = 0;
5950 wpa_s->p2p_go_vht = 0;
5952 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
5953 if (os_strcmp(wpa_s->ifname, ifname) == 0)
5956 if (wpa_s == NULL) {
5957 wpa_printf(MSG_DEBUG, "P2P: Interface '%s' not found", ifname);
5961 ssid = wpa_s->current_ssid;
5963 wpa_printf(MSG_DEBUG, "P2P: No current SSID to use for "
5968 wpa_s->global->p2p_invite_group = wpa_s;
5969 persistent = ssid->p2p_persistent_group &&
5970 wpas_p2p_get_persistent(wpa_s->parent, peer_addr,
5971 ssid->ssid, ssid->ssid_len);
5973 if (ssid->mode == WPAS_MODE_P2P_GO) {
5974 role = P2P_INVITE_ROLE_ACTIVE_GO;
5975 bssid = wpa_s->own_addr;
5976 if (go_dev_addr == NULL)
5977 go_dev_addr = wpa_s->global->p2p_dev_addr;
5978 freq = ssid->frequency;
5980 role = P2P_INVITE_ROLE_CLIENT;
5981 if (wpa_s->wpa_state < WPA_ASSOCIATED) {
5982 wpa_printf(MSG_DEBUG, "P2P: Not associated - cannot "
5983 "invite to current group");
5986 bssid = wpa_s->bssid;
5987 if (go_dev_addr == NULL &&
5988 !is_zero_ether_addr(wpa_s->go_dev_addr))
5989 go_dev_addr = wpa_s->go_dev_addr;
5990 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
5991 (int) wpa_s->assoc_freq;
5993 wpa_s->parent->pending_invite_ssid_id = -1;
5995 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
5998 res = wpas_p2p_setup_freqs(wpa_s, freq, &force_freq, &pref_freq,
5999 role == P2P_INVITE_ROLE_ACTIVE_GO);
6002 wpas_p2p_set_own_freq_preference(wpa_s, force_freq);
6004 return p2p_invite(wpa_s->global->p2p, peer_addr, role, bssid,
6005 ssid->ssid, ssid->ssid_len, force_freq,
6006 go_dev_addr, persistent, pref_freq, -1);
6010 void wpas_p2p_completed(struct wpa_supplicant *wpa_s)
6012 struct wpa_ssid *ssid = wpa_s->current_ssid;
6013 u8 go_dev_addr[ETH_ALEN];
6014 int network_id = -1;
6020 if (ssid == NULL || ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION) {
6021 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6022 wpa_s->parent, NULL);
6025 if (!wpa_s->show_group_started || !ssid)
6028 wpa_s->show_group_started = 0;
6030 os_memset(go_dev_addr, 0, ETH_ALEN);
6031 if (ssid->bssid_set)
6032 os_memcpy(go_dev_addr, ssid->bssid, ETH_ALEN);
6033 persistent = wpas_p2p_persistent_group(wpa_s, go_dev_addr, ssid->ssid,
6035 os_memcpy(wpa_s->go_dev_addr, go_dev_addr, ETH_ALEN);
6037 if (wpa_s->global->p2p_group_formation == wpa_s)
6038 wpa_s->global->p2p_group_formation = NULL;
6040 freq = wpa_s->current_bss ? wpa_s->current_bss->freq :
6041 (int) wpa_s->assoc_freq;
6044 if (wpa_sm_get_p2p_ip_addr(wpa_s->wpa, ip) == 0) {
6045 os_snprintf(ip_addr, sizeof(ip_addr), " ip_addr=%u.%u.%u.%u "
6046 "ip_mask=%u.%u.%u.%u go_ip_addr=%u.%u.%u.%u",
6047 ip[0], ip[1], ip[2], ip[3],
6048 ip[4], ip[5], ip[6], ip[7],
6049 ip[8], ip[9], ip[10], ip[11]);
6052 wpas_p2p_group_started(wpa_s, 0, ssid, freq,
6053 ssid->passphrase == NULL && ssid->psk_set ?
6055 ssid->passphrase, go_dev_addr, persistent,
6059 network_id = wpas_p2p_store_persistent_group(wpa_s->parent,
6062 network_id = ssid->id;
6063 wpas_notify_p2p_group_started(wpa_s, ssid, network_id, 1);
6067 int wpas_p2p_presence_req(struct wpa_supplicant *wpa_s, u32 duration1,
6068 u32 interval1, u32 duration2, u32 interval2)
6072 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6075 if (wpa_s->wpa_state < WPA_ASSOCIATED ||
6076 wpa_s->current_ssid == NULL ||
6077 wpa_s->current_ssid->mode != WPAS_MODE_INFRA)
6080 ret = p2p_presence_req(wpa_s->global->p2p, wpa_s->bssid,
6081 wpa_s->own_addr, wpa_s->assoc_freq,
6082 duration1, interval1, duration2, interval2);
6084 wpa_s->waiting_presence_resp = 1;
6090 int wpas_p2p_ext_listen(struct wpa_supplicant *wpa_s, unsigned int period,
6091 unsigned int interval)
6093 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6096 return p2p_ext_listen(wpa_s->global->p2p, period, interval);
6100 static int wpas_p2p_is_client(struct wpa_supplicant *wpa_s)
6102 if (wpa_s->current_ssid == NULL) {
6104 * current_ssid can be cleared when P2P client interface gets
6105 * disconnected, so assume this interface was used as P2P
6110 return wpa_s->current_ssid->p2p_group &&
6111 wpa_s->current_ssid->mode == WPAS_MODE_INFRA;
6115 static void wpas_p2p_group_idle_timeout(void *eloop_ctx, void *timeout_ctx)
6117 struct wpa_supplicant *wpa_s = eloop_ctx;
6119 if (wpa_s->conf->p2p_group_idle == 0 && !wpas_p2p_is_client(wpa_s)) {
6120 wpa_printf(MSG_DEBUG, "P2P: Ignore group idle timeout - "
6125 wpa_printf(MSG_DEBUG, "P2P: Group idle timeout reached - terminate "
6127 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_IDLE_TIMEOUT);
6131 static void wpas_p2p_set_group_idle_timeout(struct wpa_supplicant *wpa_s)
6135 if (eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6136 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6138 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6141 timeout = wpa_s->conf->p2p_group_idle;
6142 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA &&
6143 (timeout == 0 || timeout > P2P_MAX_CLIENT_IDLE))
6144 timeout = P2P_MAX_CLIENT_IDLE;
6150 if (wpa_s->current_ssid->mode == WPAS_MODE_INFRA)
6151 timeout = 0; /* special client mode no-timeout */
6156 if (wpa_s->p2p_in_provisioning) {
6158 * Use the normal group formation timeout during the
6159 * provisioning phase to avoid terminating this process too
6160 * early due to group idle timeout.
6162 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6163 "during provisioning");
6167 if (wpa_s->show_group_started) {
6169 * Use the normal group formation timeout between the end of
6170 * the provisioning phase and completion of 4-way handshake to
6171 * avoid terminating this process too early due to group idle
6174 wpa_printf(MSG_DEBUG, "P2P: Do not use P2P group idle timeout "
6175 "while waiting for initial 4-way handshake to "
6180 wpa_printf(MSG_DEBUG, "P2P: Set P2P group idle timeout to %u seconds",
6182 eloop_register_timeout(timeout, 0, wpas_p2p_group_idle_timeout,
6187 /* Returns 1 if the interface was removed */
6188 int wpas_p2p_deauth_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6189 u16 reason_code, const u8 *ie, size_t ie_len,
6190 int locally_generated)
6192 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6195 if (!locally_generated)
6196 p2p_deauth_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6199 if (reason_code == WLAN_REASON_DEAUTH_LEAVING && !locally_generated &&
6200 wpa_s->current_ssid &&
6201 wpa_s->current_ssid->p2p_group &&
6202 wpa_s->current_ssid->mode == WPAS_MODE_INFRA) {
6203 wpa_printf(MSG_DEBUG, "P2P: GO indicated that the P2P Group "
6204 "session is ending");
6205 if (wpas_p2p_group_delete(wpa_s,
6206 P2P_GROUP_REMOVAL_GO_ENDING_SESSION)
6215 void wpas_p2p_disassoc_notif(struct wpa_supplicant *wpa_s, const u8 *bssid,
6216 u16 reason_code, const u8 *ie, size_t ie_len,
6217 int locally_generated)
6219 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6222 if (!locally_generated)
6223 p2p_disassoc_notif(wpa_s->global->p2p, bssid, reason_code, ie,
6228 void wpas_p2p_update_config(struct wpa_supplicant *wpa_s)
6230 struct p2p_data *p2p = wpa_s->global->p2p;
6235 if (!(wpa_s->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE))
6238 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_NAME)
6239 p2p_set_dev_name(p2p, wpa_s->conf->device_name);
6241 if (wpa_s->conf->changed_parameters & CFG_CHANGED_DEVICE_TYPE)
6242 p2p_set_pri_dev_type(p2p, wpa_s->conf->device_type);
6245 (wpa_s->conf->changed_parameters & CFG_CHANGED_CONFIG_METHODS))
6246 p2p_set_config_methods(p2p, wpa_s->wps->config_methods);
6248 if (wpa_s->wps && (wpa_s->conf->changed_parameters & CFG_CHANGED_UUID))
6249 p2p_set_uuid(p2p, wpa_s->wps->uuid);
6251 if (wpa_s->conf->changed_parameters & CFG_CHANGED_WPS_STRING) {
6252 p2p_set_manufacturer(p2p, wpa_s->conf->manufacturer);
6253 p2p_set_model_name(p2p, wpa_s->conf->model_name);
6254 p2p_set_model_number(p2p, wpa_s->conf->model_number);
6255 p2p_set_serial_number(p2p, wpa_s->conf->serial_number);
6258 if (wpa_s->conf->changed_parameters & CFG_CHANGED_SEC_DEVICE_TYPE)
6259 p2p_set_sec_dev_types(p2p,
6260 (void *) wpa_s->conf->sec_device_type,
6261 wpa_s->conf->num_sec_device_types);
6263 if (wpa_s->conf->changed_parameters & CFG_CHANGED_VENDOR_EXTENSION) {
6265 p2p_remove_wps_vendor_extensions(p2p);
6266 for (i = 0; i < MAX_WPS_VENDOR_EXT; i++) {
6267 if (wpa_s->conf->wps_vendor_ext[i] == NULL)
6269 p2p_add_wps_vendor_extension(
6270 p2p, wpa_s->conf->wps_vendor_ext[i]);
6274 if ((wpa_s->conf->changed_parameters & CFG_CHANGED_COUNTRY) &&
6275 wpa_s->conf->country[0] && wpa_s->conf->country[1]) {
6277 country[0] = wpa_s->conf->country[0];
6278 country[1] = wpa_s->conf->country[1];
6280 p2p_set_country(p2p, country);
6283 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_SSID_POSTFIX) {
6284 p2p_set_ssid_postfix(p2p, (u8 *) wpa_s->conf->p2p_ssid_postfix,
6285 wpa_s->conf->p2p_ssid_postfix ?
6286 os_strlen(wpa_s->conf->p2p_ssid_postfix) :
6290 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_INTRA_BSS)
6291 p2p_set_intra_bss_dist(p2p, wpa_s->conf->p2p_intra_bss);
6293 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_LISTEN_CHANNEL) {
6294 u8 reg_class, channel;
6299 if (wpa_s->conf->p2p_listen_reg_class &&
6300 wpa_s->conf->p2p_listen_channel) {
6301 reg_class = wpa_s->conf->p2p_listen_reg_class;
6302 channel = wpa_s->conf->p2p_listen_channel;
6307 * Pick one of the social channels randomly as the
6310 os_get_random((u8 *) &r, sizeof(r));
6311 channel = 1 + (r % 3) * 5;
6314 ret = p2p_set_listen_channel(p2p, reg_class, channel,
6317 wpa_printf(MSG_ERROR, "P2P: Own listen channel update "
6320 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_OPER_CHANNEL) {
6321 u8 op_reg_class, op_channel, cfg_op_channel;
6324 if (wpa_s->conf->p2p_oper_reg_class &&
6325 wpa_s->conf->p2p_oper_channel) {
6326 op_reg_class = wpa_s->conf->p2p_oper_reg_class;
6327 op_channel = wpa_s->conf->p2p_oper_channel;
6332 * Use random operation channel from (1, 6, 11)
6333 *if no other preference is indicated.
6335 os_get_random((u8 *) &r, sizeof(r));
6336 op_channel = 1 + (r % 3) * 5;
6339 ret = p2p_set_oper_channel(p2p, op_reg_class, op_channel,
6342 wpa_printf(MSG_ERROR, "P2P: Own oper channel update "
6346 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PREF_CHAN) {
6347 if (p2p_set_pref_chan(p2p, wpa_s->conf->num_p2p_pref_chan,
6348 wpa_s->conf->p2p_pref_chan) < 0) {
6349 wpa_printf(MSG_ERROR, "P2P: Preferred channel list "
6353 if (p2p_set_no_go_freq(p2p, &wpa_s->conf->p2p_no_go_freq) < 0) {
6354 wpa_printf(MSG_ERROR, "P2P: No GO channel list "
6359 if (wpa_s->conf->changed_parameters & CFG_CHANGED_P2P_PASSPHRASE_LEN)
6360 p2p_set_passphrase_len(p2p, wpa_s->conf->p2p_passphrase_len);
6364 int wpas_p2p_set_noa(struct wpa_supplicant *wpa_s, u8 count, int start,
6367 if (!wpa_s->ap_iface)
6369 return hostapd_p2p_set_noa(wpa_s->ap_iface->bss[0], count, start,
6374 int wpas_p2p_set_cross_connect(struct wpa_supplicant *wpa_s, int enabled)
6376 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6379 wpa_s->global->cross_connection = enabled;
6380 p2p_set_cross_connect(wpa_s->global->p2p, enabled);
6383 struct wpa_supplicant *iface;
6385 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
6387 if (iface->cross_connect_enabled == 0)
6390 iface->cross_connect_enabled = 0;
6391 iface->cross_connect_in_use = 0;
6392 wpa_msg_global(iface->parent, MSG_INFO,
6393 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6395 iface->cross_connect_uplink);
6403 static void wpas_p2p_enable_cross_connect(struct wpa_supplicant *uplink)
6405 struct wpa_supplicant *iface;
6407 if (!uplink->global->cross_connection)
6410 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6411 if (!iface->cross_connect_enabled)
6413 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6416 if (iface->ap_iface == NULL)
6418 if (iface->cross_connect_in_use)
6421 iface->cross_connect_in_use = 1;
6422 wpa_msg_global(iface->parent, MSG_INFO,
6423 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6424 iface->ifname, iface->cross_connect_uplink);
6429 static void wpas_p2p_disable_cross_connect(struct wpa_supplicant *uplink)
6431 struct wpa_supplicant *iface;
6433 for (iface = uplink->global->ifaces; iface; iface = iface->next) {
6434 if (!iface->cross_connect_enabled)
6436 if (os_strcmp(uplink->ifname, iface->cross_connect_uplink) !=
6439 if (!iface->cross_connect_in_use)
6442 wpa_msg_global(iface->parent, MSG_INFO,
6443 P2P_EVENT_CROSS_CONNECT_DISABLE "%s %s",
6444 iface->ifname, iface->cross_connect_uplink);
6445 iface->cross_connect_in_use = 0;
6450 void wpas_p2p_notif_connected(struct wpa_supplicant *wpa_s)
6452 if (wpa_s->ap_iface || wpa_s->current_ssid == NULL ||
6453 wpa_s->current_ssid->mode != WPAS_MODE_INFRA ||
6454 wpa_s->cross_connect_disallowed)
6455 wpas_p2p_disable_cross_connect(wpa_s);
6457 wpas_p2p_enable_cross_connect(wpa_s);
6458 if (!wpa_s->ap_iface &&
6459 eloop_cancel_timeout(wpas_p2p_group_idle_timeout, wpa_s, NULL) > 0)
6460 wpa_printf(MSG_DEBUG, "P2P: Cancelled P2P group idle timeout");
6464 void wpas_p2p_notif_disconnected(struct wpa_supplicant *wpa_s)
6466 wpas_p2p_disable_cross_connect(wpa_s);
6467 if (!wpa_s->ap_iface &&
6468 !eloop_is_timeout_registered(wpas_p2p_group_idle_timeout,
6470 wpas_p2p_set_group_idle_timeout(wpa_s);
6474 static void wpas_p2p_cross_connect_setup(struct wpa_supplicant *wpa_s)
6476 struct wpa_supplicant *iface;
6478 if (!wpa_s->global->cross_connection)
6481 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
6484 if (iface->drv_flags &
6485 WPA_DRIVER_FLAGS_P2P_DEDICATED_INTERFACE)
6487 if (iface->drv_flags & WPA_DRIVER_FLAGS_P2P_CAPABLE)
6490 wpa_s->cross_connect_enabled = 1;
6491 os_strlcpy(wpa_s->cross_connect_uplink, iface->ifname,
6492 sizeof(wpa_s->cross_connect_uplink));
6493 wpa_printf(MSG_DEBUG, "P2P: Enable cross connection from "
6494 "%s to %s whenever uplink is available",
6495 wpa_s->ifname, wpa_s->cross_connect_uplink);
6497 if (iface->ap_iface || iface->current_ssid == NULL ||
6498 iface->current_ssid->mode != WPAS_MODE_INFRA ||
6499 iface->cross_connect_disallowed ||
6500 iface->wpa_state != WPA_COMPLETED)
6503 wpa_s->cross_connect_in_use = 1;
6504 wpa_msg_global(wpa_s->parent, MSG_INFO,
6505 P2P_EVENT_CROSS_CONNECT_ENABLE "%s %s",
6506 wpa_s->ifname, wpa_s->cross_connect_uplink);
6512 int wpas_p2p_notif_pbc_overlap(struct wpa_supplicant *wpa_s)
6514 if (wpa_s->p2p_group_interface != P2P_GROUP_INTERFACE_CLIENT &&
6515 !wpa_s->p2p_in_provisioning)
6516 return 0; /* not P2P client operation */
6518 wpa_printf(MSG_DEBUG, "P2P: Terminate connection due to WPS PBC "
6520 if (wpa_s != wpa_s->parent)
6521 wpa_msg_ctrl(wpa_s->parent, MSG_INFO, WPS_EVENT_OVERLAP);
6522 wpas_p2p_group_formation_failed(wpa_s);
6527 void wpas_p2p_pbc_overlap_cb(void *eloop_ctx, void *timeout_ctx)
6529 struct wpa_supplicant *wpa_s = eloop_ctx;
6530 wpas_p2p_notif_pbc_overlap(wpa_s);
6534 void wpas_p2p_update_channel_list(struct wpa_supplicant *wpa_s)
6536 struct p2p_channels chan, cli_chan;
6537 struct wpa_supplicant *ifs;
6539 if (wpa_s->global == NULL || wpa_s->global->p2p == NULL)
6542 os_memset(&chan, 0, sizeof(chan));
6543 os_memset(&cli_chan, 0, sizeof(cli_chan));
6544 if (wpas_p2p_setup_channels(wpa_s, &chan, &cli_chan)) {
6545 wpa_printf(MSG_ERROR, "P2P: Failed to update supported "
6550 p2p_update_channel_list(wpa_s->global->p2p, &chan, &cli_chan);
6552 for (ifs = wpa_s->global->ifaces; ifs; ifs = ifs->next) {
6554 if (!ifs->current_ssid ||
6555 !ifs->current_ssid->p2p_group ||
6556 (ifs->current_ssid->mode != WPAS_MODE_P2P_GO &&
6557 ifs->current_ssid->mode != WPAS_MODE_P2P_GROUP_FORMATION))
6559 freq = ifs->current_ssid->frequency;
6560 if (freq_included(&chan, freq)) {
6561 wpa_dbg(ifs, MSG_DEBUG,
6562 "P2P GO operating frequency %d MHz in valid range",
6567 wpa_dbg(ifs, MSG_DEBUG,
6568 "P2P GO operating in invalid frequency %d MHz", freq);
6569 /* TODO: Consider using CSA or removing the group within
6571 wpa_msg(ifs, MSG_INFO, P2P_EVENT_REMOVE_AND_REFORM_GROUP);
6576 static void wpas_p2p_scan_res_ignore(struct wpa_supplicant *wpa_s,
6577 struct wpa_scan_results *scan_res)
6579 wpa_printf(MSG_DEBUG, "P2P: Ignore scan results");
6583 int wpas_p2p_cancel(struct wpa_supplicant *wpa_s)
6585 struct wpa_global *global = wpa_s->global;
6589 if (global->p2p == NULL)
6592 wpa_printf(MSG_DEBUG, "P2P: Request to cancel group formation");
6594 if (wpa_s->pending_interface_name[0] &&
6595 !is_zero_ether_addr(wpa_s->pending_interface_addr))
6598 peer = p2p_get_go_neg_peer(global->p2p);
6600 wpa_printf(MSG_DEBUG, "P2P: Unauthorize pending GO Neg peer "
6601 MACSTR, MAC2STR(peer));
6602 p2p_unauthorize(global->p2p, peer);
6606 if (wpa_s->scan_res_handler == wpas_p2p_scan_res_join) {
6607 wpa_printf(MSG_DEBUG, "P2P: Stop pending scan for join");
6608 wpa_s->scan_res_handler = wpas_p2p_scan_res_ignore;
6612 if (wpa_s->pending_pd_before_join) {
6613 wpa_printf(MSG_DEBUG, "P2P: Stop pending PD before join");
6614 wpa_s->pending_pd_before_join = 0;
6618 wpas_p2p_stop_find(wpa_s);
6620 for (wpa_s = global->ifaces; wpa_s; wpa_s = wpa_s->next) {
6621 if (wpa_s == global->p2p_group_formation &&
6622 (wpa_s->p2p_in_provisioning ||
6623 wpa_s->parent->pending_interface_type ==
6624 WPA_IF_P2P_CLIENT)) {
6625 wpa_printf(MSG_DEBUG, "P2P: Interface %s in group "
6626 "formation found - cancelling",
6629 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6630 wpa_s->parent, NULL);
6631 if (wpa_s->p2p_in_provisioning) {
6632 wpas_group_formation_completed(wpa_s, 0);
6635 wpas_p2p_group_delete(wpa_s,
6636 P2P_GROUP_REMOVAL_REQUESTED);
6638 } else if (wpa_s->p2p_in_invitation) {
6639 wpa_printf(MSG_DEBUG, "P2P: Interface %s in invitation found - cancelling",
6642 wpas_p2p_group_formation_failed(wpa_s);
6647 wpa_printf(MSG_DEBUG, "P2P: No ongoing group formation found");
6655 void wpas_p2p_interface_unavailable(struct wpa_supplicant *wpa_s)
6657 if (wpa_s->current_ssid == NULL || !wpa_s->current_ssid->p2p_group)
6660 wpa_printf(MSG_DEBUG, "P2P: Remove group due to driver resource not "
6661 "being available anymore");
6662 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_UNAVAILABLE);
6666 void wpas_p2p_update_best_channels(struct wpa_supplicant *wpa_s,
6667 int freq_24, int freq_5, int freq_overall)
6669 struct p2p_data *p2p = wpa_s->global->p2p;
6672 p2p_set_best_channels(p2p, freq_24, freq_5, freq_overall);
6676 int wpas_p2p_unauthorize(struct wpa_supplicant *wpa_s, const char *addr)
6679 struct p2p_data *p2p = wpa_s->global->p2p;
6684 if (hwaddr_aton(addr, peer))
6687 return p2p_unauthorize(p2p, peer);
6692 * wpas_p2p_disconnect - Disconnect from a P2P Group
6693 * @wpa_s: Pointer to wpa_supplicant data
6694 * Returns: 0 on success, -1 on failure
6696 * This can be used to disconnect from a group in which the local end is a P2P
6697 * Client or to end a P2P Group in case the local end is the Group Owner. If a
6698 * virtual network interface was created for this group, that interface will be
6699 * removed. Otherwise, only the configured P2P group network will be removed
6700 * from the interface.
6702 int wpas_p2p_disconnect(struct wpa_supplicant *wpa_s)
6708 return wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_REQUESTED) < 0 ?
6713 int wpas_p2p_in_progress(struct wpa_supplicant *wpa_s)
6717 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
6720 ret = p2p_in_progress(wpa_s->global->p2p);
6723 * Check whether there is an ongoing WPS provisioning step (or
6724 * other parts of group formation) on another interface since
6725 * p2p_in_progress() does not report this to avoid issues for
6726 * scans during such provisioning step.
6728 if (wpa_s->global->p2p_group_formation &&
6729 wpa_s->global->p2p_group_formation != wpa_s) {
6730 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Another interface (%s) "
6731 "in group formation",
6732 wpa_s->global->p2p_group_formation->ifname);
6737 if (!ret && wpa_s->global->p2p_go_wait_client.sec) {
6738 struct os_reltime now;
6739 os_get_reltime(&now);
6740 if (os_reltime_expired(&now, &wpa_s->global->p2p_go_wait_client,
6741 P2P_MAX_INITIAL_CONN_WAIT_GO)) {
6742 /* Wait for the first client has expired */
6743 wpa_s->global->p2p_go_wait_client.sec = 0;
6745 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Waiting for initial client connection during group formation");
6754 void wpas_p2p_network_removed(struct wpa_supplicant *wpa_s,
6755 struct wpa_ssid *ssid)
6757 if (wpa_s->p2p_in_provisioning && ssid->p2p_group &&
6758 eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6759 wpa_s->parent, NULL) > 0) {
6761 * Remove the network by scheduling the group formation
6762 * timeout to happen immediately. The teardown code
6763 * needs to be scheduled to run asynch later so that we
6764 * don't delete data from under ourselves unexpectedly.
6765 * Calling wpas_p2p_group_formation_timeout directly
6766 * causes a series of crashes in WPS failure scenarios.
6768 wpa_printf(MSG_DEBUG, "P2P: Canceled group formation due to "
6769 "P2P group network getting removed");
6770 eloop_register_timeout(0, 0, wpas_p2p_group_formation_timeout,
6771 wpa_s->parent, NULL);
6776 struct wpa_ssid * wpas_p2p_get_persistent(struct wpa_supplicant *wpa_s,
6777 const u8 *addr, const u8 *ssid,
6783 for (s = wpa_s->conf->ssid; s; s = s->next) {
6784 if (s->disabled != 2)
6787 (ssid_len != s->ssid_len ||
6788 os_memcmp(ssid, s->ssid, ssid_len) != 0))
6791 if (s->mode == WPAS_MODE_P2P_GO)
6795 if (os_memcmp(s->bssid, addr, ETH_ALEN) == 0)
6796 return s; /* peer is GO in the persistent group */
6797 if (s->mode != WPAS_MODE_P2P_GO || s->p2p_client_list == NULL)
6799 for (i = 0; i < s->num_p2p_clients; i++) {
6800 if (os_memcmp(s->p2p_client_list + i * ETH_ALEN,
6801 addr, ETH_ALEN) == 0)
6802 return s; /* peer is P2P client in persistent
6811 void wpas_p2p_notify_ap_sta_authorized(struct wpa_supplicant *wpa_s,
6814 if (eloop_cancel_timeout(wpas_p2p_group_formation_timeout,
6815 wpa_s->parent, NULL) > 0) {
6817 * This can happen if WPS provisioning step is not terminated
6818 * cleanly (e.g., P2P Client does not send WSC_Done). Since the
6819 * peer was able to connect, there is no need to time out group
6820 * formation after this, though. In addition, this is used with
6821 * the initial connection wait on the GO as a separate formation
6822 * timeout and as such, expected to be hit after the initial WPS
6823 * provisioning step.
6825 wpa_printf(MSG_DEBUG, "P2P: Canceled P2P group formation timeout on data connection");
6827 if (!wpa_s->p2p_go_group_formation_completed) {
6828 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Marking group formation completed on GO on first data connection");
6829 wpa_s->p2p_go_group_formation_completed = 1;
6830 wpa_s->global->p2p_group_formation = NULL;
6831 wpa_s->p2p_in_provisioning = 0;
6832 wpa_s->p2p_in_invitation = 0;
6834 wpa_s->global->p2p_go_wait_client.sec = 0;
6837 wpas_p2p_add_persistent_group_client(wpa_s, addr);
6841 static void wpas_p2p_fallback_to_go_neg(struct wpa_supplicant *wpa_s,
6844 struct wpa_supplicant *group = wpa_s;
6845 if (wpa_s->global->p2p_group_formation)
6846 group = wpa_s->global->p2p_group_formation;
6847 wpa_s = wpa_s->parent;
6848 offchannel_send_action_done(wpa_s);
6850 wpas_p2p_group_delete(group, P2P_GROUP_REMOVAL_SILENT);
6851 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Fall back to GO Negotiation");
6852 wpas_p2p_connect(wpa_s, wpa_s->pending_join_dev_addr, wpa_s->p2p_pin,
6853 wpa_s->p2p_wps_method, wpa_s->p2p_persistent_group, 0,
6854 0, 0, wpa_s->p2p_go_intent, wpa_s->p2p_connect_freq,
6855 wpa_s->p2p_persistent_id,
6856 wpa_s->p2p_pd_before_go_neg,
6862 int wpas_p2p_scan_no_go_seen(struct wpa_supplicant *wpa_s)
6864 if (!wpa_s->p2p_fallback_to_go_neg ||
6865 wpa_s->p2p_in_provisioning <= 5)
6868 if (wpas_p2p_peer_go(wpa_s, wpa_s->pending_join_dev_addr) > 0)
6869 return 0; /* peer operating as a GO */
6871 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: GO not found for p2p_connect-auto - "
6872 "fallback to GO Negotiation");
6873 wpas_p2p_fallback_to_go_neg(wpa_s, 1);
6879 unsigned int wpas_p2p_search_delay(struct wpa_supplicant *wpa_s)
6881 struct wpa_supplicant *ifs;
6883 if (wpa_s->wpa_state > WPA_SCANNING) {
6884 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search delay due to "
6885 "concurrent operation",
6886 wpa_s->conf->p2p_search_delay);
6887 return wpa_s->conf->p2p_search_delay;
6890 dl_list_for_each(ifs, &wpa_s->radio->ifaces, struct wpa_supplicant,
6892 if (ifs != wpa_s && ifs->wpa_state > WPA_SCANNING) {
6893 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Use %u ms search "
6894 "delay due to concurrent operation on "
6896 wpa_s->conf->p2p_search_delay,
6898 return wpa_s->conf->p2p_search_delay;
6906 static int wpas_p2p_remove_psk_entry(struct wpa_supplicant *wpa_s,
6907 struct wpa_ssid *s, const u8 *addr,
6910 struct psk_list_entry *psk, *tmp;
6913 dl_list_for_each_safe(psk, tmp, &s->psk_list, struct psk_list_entry,
6915 if ((iface_addr && !psk->p2p &&
6916 os_memcmp(addr, psk->addr, ETH_ALEN) == 0) ||
6917 (!iface_addr && psk->p2p &&
6918 os_memcmp(addr, psk->addr, ETH_ALEN) == 0)) {
6919 wpa_dbg(wpa_s, MSG_DEBUG,
6920 "P2P: Remove persistent group PSK list entry for "
6922 MAC2STR(psk->addr), psk->p2p);
6923 dl_list_del(&psk->list);
6933 void wpas_p2p_new_psk_cb(struct wpa_supplicant *wpa_s, const u8 *mac_addr,
6934 const u8 *p2p_dev_addr,
6935 const u8 *psk, size_t psk_len)
6937 struct wpa_ssid *ssid = wpa_s->current_ssid;
6938 struct wpa_ssid *persistent;
6939 struct psk_list_entry *p, *last;
6941 if (psk_len != sizeof(p->psk))
6945 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR
6946 " p2p_dev_addr=" MACSTR,
6947 MAC2STR(mac_addr), MAC2STR(p2p_dev_addr));
6948 if (is_zero_ether_addr(p2p_dev_addr))
6949 p2p_dev_addr = NULL;
6951 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: New PSK for addr=" MACSTR,
6955 if (ssid->mode == WPAS_MODE_P2P_GROUP_FORMATION) {
6956 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: new_psk_cb during group formation");
6957 /* To be added to persistent group once created */
6958 if (wpa_s->global->add_psk == NULL) {
6959 wpa_s->global->add_psk = os_zalloc(sizeof(*p));
6960 if (wpa_s->global->add_psk == NULL)
6963 p = wpa_s->global->add_psk;
6966 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6969 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6971 os_memcpy(p->psk, psk, psk_len);
6975 if (ssid->mode != WPAS_MODE_P2P_GO || !ssid->p2p_persistent_group) {
6976 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Ignore new_psk_cb on not-persistent GO");
6980 persistent = wpas_p2p_get_persistent(wpa_s->parent, NULL, ssid->ssid,
6983 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not find persistent group information to store the new PSK");
6987 p = os_zalloc(sizeof(*p));
6992 os_memcpy(p->addr, p2p_dev_addr, ETH_ALEN);
6995 os_memcpy(p->addr, mac_addr, ETH_ALEN);
6997 os_memcpy(p->psk, psk, psk_len);
6999 if (dl_list_len(&persistent->psk_list) > P2P_MAX_STORED_CLIENTS &&
7000 (last = dl_list_last(&persistent->psk_list,
7001 struct psk_list_entry, list))) {
7002 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove oldest PSK entry for "
7003 MACSTR " (p2p=%u) to make room for a new one",
7004 MAC2STR(last->addr), last->p2p);
7005 dl_list_del(&last->list);
7009 wpas_p2p_remove_psk_entry(wpa_s->parent, persistent,
7010 p2p_dev_addr ? p2p_dev_addr : mac_addr,
7011 p2p_dev_addr == NULL);
7013 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for p2p_dev_addr="
7014 MACSTR, MAC2STR(p2p_dev_addr));
7016 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Add new PSK for addr=" MACSTR,
7019 dl_list_add(&persistent->psk_list, &p->list);
7021 if (wpa_s->parent->conf->update_config &&
7022 wpa_config_write(wpa_s->parent->confname, wpa_s->parent->conf))
7023 wpa_printf(MSG_DEBUG, "P2P: Failed to update configuration");
7027 static void wpas_p2p_remove_psk(struct wpa_supplicant *wpa_s,
7028 struct wpa_ssid *s, const u8 *addr,
7033 res = wpas_p2p_remove_psk_entry(wpa_s, s, addr, iface_addr);
7034 if (res > 0 && wpa_s->conf->update_config &&
7035 wpa_config_write(wpa_s->confname, wpa_s->conf))
7036 wpa_dbg(wpa_s, MSG_DEBUG,
7037 "P2P: Failed to update configuration");
7041 static void wpas_p2p_remove_client_go(struct wpa_supplicant *wpa_s,
7042 const u8 *peer, int iface_addr)
7044 struct hostapd_data *hapd;
7045 struct hostapd_wpa_psk *psk, *prev, *rem;
7046 struct sta_info *sta;
7048 if (wpa_s->ap_iface == NULL || wpa_s->current_ssid == NULL ||
7049 wpa_s->current_ssid->mode != WPAS_MODE_P2P_GO)
7052 /* Remove per-station PSK entry */
7053 hapd = wpa_s->ap_iface->bss[0];
7055 psk = hapd->conf->ssid.wpa_psk;
7057 if ((iface_addr && os_memcmp(peer, psk->addr, ETH_ALEN) == 0) ||
7059 os_memcmp(peer, psk->p2p_dev_addr, ETH_ALEN) == 0)) {
7060 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove operating group PSK entry for "
7061 MACSTR " iface_addr=%d",
7062 MAC2STR(peer), iface_addr);
7064 prev->next = psk->next;
7066 hapd->conf->ssid.wpa_psk = psk->next;
7076 /* Disconnect from group */
7078 sta = ap_get_sta(hapd, peer);
7080 sta = ap_get_sta_p2p(hapd, peer);
7082 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disconnect peer " MACSTR
7083 " (iface_addr=%d) from group",
7084 MAC2STR(peer), iface_addr);
7085 hostapd_drv_sta_deauth(hapd, sta->addr,
7086 WLAN_REASON_DEAUTH_LEAVING);
7087 ap_sta_deauthenticate(hapd, sta, WLAN_REASON_DEAUTH_LEAVING);
7092 void wpas_p2p_remove_client(struct wpa_supplicant *wpa_s, const u8 *peer,
7096 struct wpa_supplicant *w;
7098 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Remove client " MACSTR, MAC2STR(peer));
7100 /* Remove from any persistent group */
7101 for (s = wpa_s->parent->conf->ssid; s; s = s->next) {
7102 if (s->disabled != 2 || s->mode != WPAS_MODE_P2P_GO)
7105 wpas_remove_persistent_peer(wpa_s, s, peer, 0);
7106 wpas_p2p_remove_psk(wpa_s->parent, s, peer, iface_addr);
7109 /* Remove from any operating group */
7110 for (w = wpa_s->global->ifaces; w; w = w->next)
7111 wpas_p2p_remove_client_go(w, peer, iface_addr);
7115 static void wpas_p2p_psk_failure_removal(void *eloop_ctx, void *timeout_ctx)
7117 struct wpa_supplicant *wpa_s = eloop_ctx;
7118 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_PSK_FAILURE);
7122 static void wpas_p2p_group_freq_conflict(void *eloop_ctx, void *timeout_ctx)
7124 struct wpa_supplicant *wpa_s = eloop_ctx;
7126 wpa_printf(MSG_DEBUG, "P2P: Frequency conflict - terminate group");
7127 wpas_p2p_group_delete(wpa_s, P2P_GROUP_REMOVAL_FREQ_CONFLICT);
7131 int wpas_p2p_handle_frequency_conflicts(struct wpa_supplicant *wpa_s, int freq,
7132 struct wpa_ssid *ssid)
7134 struct wpa_supplicant *iface;
7136 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7137 if (!iface->current_ssid ||
7138 iface->current_ssid->frequency == freq ||
7139 (iface->p2p_group_interface == NOT_P2P_GROUP_INTERFACE &&
7140 !iface->current_ssid->p2p_group))
7143 /* Remove the connection with least priority */
7144 if (!wpas_is_p2p_prioritized(iface)) {
7145 /* STA connection has priority over existing
7146 * P2P connection, so remove the interface. */
7147 wpa_printf(MSG_DEBUG, "P2P: Removing P2P connection due to single channel concurrent mode frequency conflict");
7148 eloop_register_timeout(0, 0,
7149 wpas_p2p_group_freq_conflict,
7151 /* If connection in progress is P2P connection, do not
7152 * proceed for the connection. */
7158 /* P2P connection has priority, disable the STA network
7160 wpa_supplicant_disable_network(wpa_s->global->ifaces,
7162 wpa_msg(wpa_s->global->ifaces, MSG_INFO,
7163 WPA_EVENT_FREQ_CONFLICT " id=%d", ssid->id);
7164 os_memset(wpa_s->global->ifaces->pending_bssid, 0,
7166 /* If P2P connection is in progress, continue
7179 int wpas_p2p_4way_hs_failed(struct wpa_supplicant *wpa_s)
7181 struct wpa_ssid *ssid = wpa_s->current_ssid;
7183 if (ssid == NULL || !ssid->p2p_group)
7186 if (wpa_s->p2p_last_4way_hs_fail &&
7187 wpa_s->p2p_last_4way_hs_fail == ssid) {
7188 u8 go_dev_addr[ETH_ALEN];
7189 struct wpa_ssid *persistent;
7191 if (wpas_p2p_persistent_group(wpa_s, go_dev_addr,
7193 ssid->ssid_len) <= 0) {
7194 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Could not determine whether 4-way handshake failures were for a persistent group");
7198 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Two 4-way handshake failures for a P2P group - go_dev_addr="
7199 MACSTR, MAC2STR(go_dev_addr));
7200 persistent = wpas_p2p_get_persistent(wpa_s->parent, go_dev_addr,
7203 if (persistent == NULL || persistent->mode != WPAS_MODE_INFRA) {
7204 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No matching persistent group stored");
7207 wpa_msg_global(wpa_s->parent, MSG_INFO,
7208 P2P_EVENT_PERSISTENT_PSK_FAIL "%d",
7211 wpa_s->p2p_last_4way_hs_fail = NULL;
7213 * Remove the group from a timeout to avoid issues with caller
7214 * continuing to use the interface if this is on a P2P group
7217 eloop_register_timeout(0, 0, wpas_p2p_psk_failure_removal,
7222 wpa_s->p2p_last_4way_hs_fail = ssid;
7227 #ifdef CONFIG_WPS_NFC
7229 static struct wpabuf * wpas_p2p_nfc_handover(int ndef, struct wpabuf *wsc,
7237 wpa_printf(MSG_DEBUG, "P2P: No p2p buffer for handover");
7241 wsc_len = wsc ? wpabuf_len(wsc) : 0;
7242 ret = wpabuf_alloc(2 + wsc_len + 2 + wpabuf_len(p2p));
7249 wpabuf_put_be16(ret, wsc_len);
7251 wpabuf_put_buf(ret, wsc);
7252 wpabuf_put_be16(ret, wpabuf_len(p2p));
7253 wpabuf_put_buf(ret, p2p);
7257 wpa_hexdump_buf(MSG_DEBUG,
7258 "P2P: Generated NFC connection handover message", ret);
7262 tmp = ndef_build_p2p(ret);
7265 wpa_printf(MSG_DEBUG, "P2P: Failed to NDEF encapsulate handover request");
7275 static int wpas_p2p_cli_freq(struct wpa_supplicant *wpa_s,
7276 struct wpa_ssid **ssid, u8 *go_dev_addr)
7278 struct wpa_supplicant *iface;
7281 os_memset(go_dev_addr, 0, ETH_ALEN);
7284 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7285 if (iface->wpa_state < WPA_ASSOCIATING ||
7286 iface->current_ssid == NULL || iface->assoc_freq == 0 ||
7287 !iface->current_ssid->p2p_group ||
7288 iface->current_ssid->mode != WPAS_MODE_INFRA)
7291 *ssid = iface->current_ssid;
7293 os_memcpy(go_dev_addr, iface->go_dev_addr, ETH_ALEN);
7294 return iface->assoc_freq;
7300 struct wpabuf * wpas_p2p_nfc_handover_req(struct wpa_supplicant *wpa_s,
7303 struct wpabuf *wsc, *p2p;
7304 struct wpa_ssid *ssid;
7305 u8 go_dev_addr[ETH_ALEN];
7306 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7308 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL) {
7309 wpa_printf(MSG_DEBUG, "P2P: P2P disabled - cannot build handover request");
7313 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7314 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7315 &wpa_s->conf->wps_nfc_dh_privkey) < 0) {
7316 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: No DH key available for handover request");
7320 if (cli_freq == 0) {
7321 wsc = wps_build_nfc_handover_req_p2p(
7322 wpa_s->parent->wps, wpa_s->conf->wps_nfc_dh_pubkey);
7325 p2p = p2p_build_nfc_handover_req(wpa_s->global->p2p, cli_freq,
7326 go_dev_addr, ssid ? ssid->ssid : NULL,
7327 ssid ? ssid->ssid_len : 0);
7329 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7333 struct wpabuf * wpas_p2p_nfc_handover_sel(struct wpa_supplicant *wpa_s,
7336 struct wpabuf *wsc, *p2p;
7337 struct wpa_ssid *ssid;
7338 u8 go_dev_addr[ETH_ALEN];
7339 int cli_freq = wpas_p2p_cli_freq(wpa_s, &ssid, go_dev_addr);
7341 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7344 if (!tag && wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7345 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7346 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7349 if (cli_freq == 0) {
7350 wsc = wps_build_nfc_handover_sel_p2p(
7352 tag ? wpa_s->conf->wps_nfc_dev_pw_id :
7353 DEV_PW_NFC_CONNECTION_HANDOVER,
7354 wpa_s->conf->wps_nfc_dh_pubkey,
7355 tag ? wpa_s->conf->wps_nfc_dev_pw : NULL);
7358 p2p = p2p_build_nfc_handover_sel(wpa_s->global->p2p, cli_freq,
7359 go_dev_addr, ssid ? ssid->ssid : NULL,
7360 ssid ? ssid->ssid_len : 0);
7362 return wpas_p2p_nfc_handover(ndef, wsc, p2p);
7366 static int wpas_p2p_nfc_join_group(struct wpa_supplicant *wpa_s,
7367 struct p2p_nfc_params *params)
7369 wpa_printf(MSG_DEBUG, "P2P: Initiate join-group based on NFC "
7370 "connection handover (freq=%d)",
7373 if (params->go_freq && params->go_ssid_len) {
7374 wpa_s->p2p_wps_method = WPS_NFC;
7375 wpa_s->pending_join_wps_method = WPS_NFC;
7376 os_memset(wpa_s->pending_join_iface_addr, 0, ETH_ALEN);
7377 os_memcpy(wpa_s->pending_join_dev_addr, params->go_dev_addr,
7379 return wpas_p2p_join_start(wpa_s, params->go_freq,
7381 params->go_ssid_len);
7384 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7385 WPS_NFC, 0, 0, 1, 0, wpa_s->conf->p2p_go_intent,
7386 params->go_freq, -1, 0, 1, 1);
7390 static int wpas_p2p_nfc_auth_join(struct wpa_supplicant *wpa_s,
7391 struct p2p_nfc_params *params, int tag)
7393 int res, persistent;
7394 struct wpa_ssid *ssid;
7396 wpa_printf(MSG_DEBUG, "P2P: Authorize join-group based on NFC "
7397 "connection handover");
7398 for (wpa_s = wpa_s->global->ifaces; wpa_s; wpa_s = wpa_s->next) {
7399 ssid = wpa_s->current_ssid;
7402 if (ssid->mode != WPAS_MODE_P2P_GO)
7404 if (wpa_s->ap_iface == NULL)
7408 if (wpa_s == NULL) {
7409 wpa_printf(MSG_DEBUG, "P2P: Could not find GO interface");
7413 if (wpa_s->parent->p2p_oob_dev_pw_id !=
7414 DEV_PW_NFC_CONNECTION_HANDOVER &&
7415 !wpa_s->parent->p2p_oob_dev_pw) {
7416 wpa_printf(MSG_DEBUG, "P2P: No NFC Dev Pw known");
7419 res = wpas_ap_wps_add_nfc_pw(
7420 wpa_s, wpa_s->parent->p2p_oob_dev_pw_id,
7421 wpa_s->parent->p2p_oob_dev_pw,
7422 wpa_s->parent->p2p_peer_oob_pk_hash_known ?
7423 wpa_s->parent->p2p_peer_oob_pubkey_hash : NULL);
7428 wpa_printf(MSG_DEBUG, "P2P: Negotiated handover - wait for peer to join without invitation");
7432 if (!params->peer ||
7433 !(params->peer->dev_capab & P2P_DEV_CAPAB_INVITATION_PROCEDURE))
7436 wpa_printf(MSG_DEBUG, "P2P: Static handover - invite peer " MACSTR
7437 " to join", MAC2STR(params->peer->p2p_device_addr));
7439 wpa_s->global->p2p_invite_group = wpa_s;
7440 persistent = ssid->p2p_persistent_group &&
7441 wpas_p2p_get_persistent(wpa_s->parent,
7442 params->peer->p2p_device_addr,
7443 ssid->ssid, ssid->ssid_len);
7444 wpa_s->parent->pending_invite_ssid_id = -1;
7446 return p2p_invite(wpa_s->global->p2p, params->peer->p2p_device_addr,
7447 P2P_INVITE_ROLE_ACTIVE_GO, wpa_s->own_addr,
7448 ssid->ssid, ssid->ssid_len, ssid->frequency,
7449 wpa_s->global->p2p_dev_addr, persistent, 0,
7450 wpa_s->parent->p2p_oob_dev_pw_id);
7454 static int wpas_p2p_nfc_init_go_neg(struct wpa_supplicant *wpa_s,
7455 struct p2p_nfc_params *params,
7458 wpa_printf(MSG_DEBUG, "P2P: Initiate GO Negotiation based on NFC "
7459 "connection handover");
7460 return wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7461 WPS_NFC, 0, 0, 0, 0, wpa_s->conf->p2p_go_intent,
7462 forced_freq, -1, 0, 1, 1);
7466 static int wpas_p2p_nfc_resp_go_neg(struct wpa_supplicant *wpa_s,
7467 struct p2p_nfc_params *params,
7472 wpa_printf(MSG_DEBUG, "P2P: Authorize GO Negotiation based on NFC "
7473 "connection handover");
7474 res = wpas_p2p_connect(wpa_s, params->peer->p2p_device_addr, NULL,
7475 WPS_NFC, 0, 0, 0, 1, wpa_s->conf->p2p_go_intent,
7476 forced_freq, -1, 0, 1, 1);
7480 res = wpas_p2p_listen(wpa_s, 60);
7482 p2p_unauthorize(wpa_s->global->p2p,
7483 params->peer->p2p_device_addr);
7490 static int wpas_p2p_nfc_connection_handover(struct wpa_supplicant *wpa_s,
7491 const struct wpabuf *data,
7492 int sel, int tag, int forced_freq)
7494 const u8 *pos, *end;
7496 struct p2p_nfc_params params;
7499 os_memset(¶ms, 0, sizeof(params));
7502 wpa_hexdump_buf(MSG_DEBUG, "P2P: Received NFC tag payload", data);
7504 pos = wpabuf_head(data);
7505 end = pos + wpabuf_len(data);
7507 if (end - pos < 2) {
7508 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of WSC "
7512 len = WPA_GET_BE16(pos);
7514 if (pos + len > end) {
7515 wpa_printf(MSG_DEBUG, "P2P: Not enough data for WSC "
7519 params.wsc_attr = pos;
7520 params.wsc_len = len;
7523 if (end - pos < 2) {
7524 wpa_printf(MSG_DEBUG, "P2P: Not enough data for Length of P2P "
7528 len = WPA_GET_BE16(pos);
7530 if (pos + len > end) {
7531 wpa_printf(MSG_DEBUG, "P2P: Not enough data for P2P "
7535 params.p2p_attr = pos;
7536 params.p2p_len = len;
7539 wpa_hexdump(MSG_DEBUG, "P2P: WSC attributes",
7540 params.wsc_attr, params.wsc_len);
7541 wpa_hexdump(MSG_DEBUG, "P2P: P2P attributes",
7542 params.p2p_attr, params.p2p_len);
7544 wpa_hexdump(MSG_DEBUG,
7545 "P2P: Ignored extra data after P2P attributes",
7549 res = p2p_process_nfc_connection_handover(wpa_s->global->p2p, ¶ms);
7553 if (params.next_step == NO_ACTION)
7556 if (params.next_step == BOTH_GO) {
7557 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_BOTH_GO "peer=" MACSTR,
7558 MAC2STR(params.peer->p2p_device_addr));
7562 if (params.next_step == PEER_CLIENT) {
7563 if (!is_zero_ether_addr(params.go_dev_addr)) {
7564 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7565 "peer=" MACSTR " freq=%d go_dev_addr=" MACSTR
7567 MAC2STR(params.peer->p2p_device_addr),
7569 MAC2STR(params.go_dev_addr),
7570 wpa_ssid_txt(params.go_ssid,
7571 params.go_ssid_len));
7573 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_PEER_CLIENT
7574 "peer=" MACSTR " freq=%d",
7575 MAC2STR(params.peer->p2p_device_addr),
7581 if (wpas_p2p_cli_freq(wpa_s, NULL, NULL)) {
7582 wpa_msg(wpa_s, MSG_INFO, P2P_EVENT_NFC_WHILE_CLIENT "peer="
7583 MACSTR, MAC2STR(params.peer->p2p_device_addr));
7587 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7588 wpa_s->p2p_oob_dev_pw = NULL;
7590 if (params.oob_dev_pw_len < WPS_OOB_PUBKEY_HASH_LEN + 2) {
7591 wpa_printf(MSG_DEBUG, "P2P: No peer OOB Dev Pw "
7596 id = WPA_GET_BE16(params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN);
7597 wpa_printf(MSG_DEBUG, "P2P: Peer OOB Dev Pw %u", id);
7598 wpa_hexdump(MSG_DEBUG, "P2P: Peer OOB Public Key hash",
7599 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7600 os_memcpy(wpa_s->p2p_peer_oob_pubkey_hash,
7601 params.oob_dev_pw, WPS_OOB_PUBKEY_HASH_LEN);
7602 wpa_s->p2p_peer_oob_pk_hash_known = 1;
7606 wpa_printf(MSG_DEBUG, "P2P: Static handover - invalid "
7607 "peer OOB Device Password Id %u", id);
7610 wpa_printf(MSG_DEBUG, "P2P: Static handover - use peer OOB "
7611 "Device Password Id %u", id);
7612 wpa_hexdump_key(MSG_DEBUG, "P2P: Peer OOB Device Password",
7613 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7614 params.oob_dev_pw_len -
7615 WPS_OOB_PUBKEY_HASH_LEN - 2);
7616 wpa_s->p2p_oob_dev_pw_id = id;
7617 wpa_s->p2p_oob_dev_pw = wpabuf_alloc_copy(
7618 params.oob_dev_pw + WPS_OOB_PUBKEY_HASH_LEN + 2,
7619 params.oob_dev_pw_len -
7620 WPS_OOB_PUBKEY_HASH_LEN - 2);
7621 if (wpa_s->p2p_oob_dev_pw == NULL)
7624 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL &&
7625 wps_nfc_gen_dh(&wpa_s->conf->wps_nfc_dh_pubkey,
7626 &wpa_s->conf->wps_nfc_dh_privkey) < 0)
7629 wpa_printf(MSG_DEBUG, "P2P: Using abbreviated WPS handshake "
7630 "without Device Password");
7631 wpa_s->p2p_oob_dev_pw_id = DEV_PW_NFC_CONNECTION_HANDOVER;
7634 switch (params.next_step) {
7638 /* already covered above */
7641 return wpas_p2p_nfc_join_group(wpa_s, ¶ms);
7643 return wpas_p2p_nfc_auth_join(wpa_s, ¶ms, tag);
7645 return wpas_p2p_nfc_init_go_neg(wpa_s, ¶ms, forced_freq);
7647 /* TODO: use own OOB Dev Pw */
7648 return wpas_p2p_nfc_resp_go_neg(wpa_s, ¶ms, forced_freq);
7655 int wpas_p2p_nfc_tag_process(struct wpa_supplicant *wpa_s,
7656 const struct wpabuf *data, int forced_freq)
7658 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7661 return wpas_p2p_nfc_connection_handover(wpa_s, data, 1, 1, forced_freq);
7665 int wpas_p2p_nfc_report_handover(struct wpa_supplicant *wpa_s, int init,
7666 const struct wpabuf *req,
7667 const struct wpabuf *sel, int forced_freq)
7672 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7675 wpa_printf(MSG_DEBUG, "NFC: P2P connection handover reported");
7677 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Req",
7678 wpabuf_head(req), wpabuf_len(req));
7679 wpa_hexdump_ascii(MSG_DEBUG, "NFC: Sel",
7680 wpabuf_head(sel), wpabuf_len(sel));
7682 wpa_printf(MSG_DEBUG, "NFC: Forced freq %d", forced_freq);
7683 tmp = ndef_parse_p2p(init ? sel : req);
7685 wpa_printf(MSG_DEBUG, "P2P: Could not parse NDEF");
7689 ret = wpas_p2p_nfc_connection_handover(wpa_s, tmp, init, 0,
7697 int wpas_p2p_nfc_tag_enabled(struct wpa_supplicant *wpa_s, int enabled)
7700 int go_intent = wpa_s->conf->p2p_go_intent;
7701 struct wpa_supplicant *iface;
7703 if (wpa_s->global->p2p == NULL)
7707 wpa_printf(MSG_DEBUG, "P2P: Disable use of own NFC Tag");
7708 for (iface = wpa_s->global->ifaces; iface; iface = iface->next)
7710 if (!iface->ap_iface)
7712 hostapd_wps_nfc_token_disable(iface->ap_iface->bss[0]);
7714 p2p_set_authorized_oob_dev_pw_id(wpa_s->global->p2p, 0,
7716 if (wpa_s->p2p_nfc_tag_enabled)
7717 wpas_p2p_remove_pending_group_interface(wpa_s);
7718 wpa_s->p2p_nfc_tag_enabled = 0;
7722 if (wpa_s->global->p2p_disabled)
7725 if (wpa_s->conf->wps_nfc_dh_pubkey == NULL ||
7726 wpa_s->conf->wps_nfc_dh_privkey == NULL ||
7727 wpa_s->conf->wps_nfc_dev_pw == NULL ||
7728 wpa_s->conf->wps_nfc_dev_pw_id < 0x10) {
7729 wpa_printf(MSG_DEBUG, "P2P: NFC password token not configured "
7730 "to allow static handover cases");
7734 wpa_printf(MSG_DEBUG, "P2P: Enable use of own NFC Tag");
7736 wpa_s->p2p_oob_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7737 wpabuf_free(wpa_s->p2p_oob_dev_pw);
7738 wpa_s->p2p_oob_dev_pw = wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7739 if (wpa_s->p2p_oob_dev_pw == NULL)
7741 wpa_s->p2p_peer_oob_pk_hash_known = 0;
7743 if (wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_GO ||
7744 wpa_s->p2p_group_interface == P2P_GROUP_INTERFACE_CLIENT) {
7746 * P2P Group Interface present and the command came on group
7747 * interface, so enable the token for the current interface.
7749 wpa_s->create_p2p_iface = 0;
7751 wpa_s->create_p2p_iface = wpas_p2p_create_iface(wpa_s);
7754 if (wpa_s->create_p2p_iface) {
7755 enum wpa_driver_if_type iftype;
7756 /* Prepare to add a new interface for the group */
7757 iftype = WPA_IF_P2P_GROUP;
7758 if (go_intent == 15)
7759 iftype = WPA_IF_P2P_GO;
7760 if (wpas_p2p_add_group_interface(wpa_s, iftype) < 0) {
7761 wpa_printf(MSG_ERROR, "P2P: Failed to allocate a new "
7762 "interface for the group");
7766 if_addr = wpa_s->pending_interface_addr;
7768 if_addr = wpa_s->own_addr;
7770 wpa_s->p2p_nfc_tag_enabled = enabled;
7772 for (iface = wpa_s->global->ifaces; iface; iface = iface->next) {
7773 struct hostapd_data *hapd;
7774 if (iface->ap_iface == NULL)
7776 hapd = iface->ap_iface->bss[0];
7777 wpabuf_free(hapd->conf->wps_nfc_dh_pubkey);
7778 hapd->conf->wps_nfc_dh_pubkey =
7779 wpabuf_dup(wpa_s->conf->wps_nfc_dh_pubkey);
7780 wpabuf_free(hapd->conf->wps_nfc_dh_privkey);
7781 hapd->conf->wps_nfc_dh_privkey =
7782 wpabuf_dup(wpa_s->conf->wps_nfc_dh_privkey);
7783 wpabuf_free(hapd->conf->wps_nfc_dev_pw);
7784 hapd->conf->wps_nfc_dev_pw =
7785 wpabuf_dup(wpa_s->conf->wps_nfc_dev_pw);
7786 hapd->conf->wps_nfc_dev_pw_id = wpa_s->conf->wps_nfc_dev_pw_id;
7788 if (hostapd_wps_nfc_token_enable(iface->ap_iface->bss[0]) < 0) {
7789 wpa_dbg(iface, MSG_DEBUG,
7790 "P2P: Failed to enable NFC Tag for GO");
7793 p2p_set_authorized_oob_dev_pw_id(
7794 wpa_s->global->p2p, wpa_s->conf->wps_nfc_dev_pw_id, go_intent,
7800 #endif /* CONFIG_WPS_NFC */
7803 static void wpas_p2p_optimize_listen_channel(struct wpa_supplicant *wpa_s,
7804 struct wpa_used_freq_data *freqs,
7807 u8 curr_chan, cand, chan;
7810 curr_chan = p2p_get_listen_channel(wpa_s->global->p2p);
7811 for (i = 0, cand = 0; i < num; i++) {
7812 ieee80211_freq_to_chan(freqs[i].freq, &chan);
7813 if (curr_chan == chan) {
7818 if (chan == 1 || chan == 6 || chan == 11)
7823 wpa_dbg(wpa_s, MSG_DEBUG,
7824 "P2P: Update Listen channel to %u baased on operating channel",
7826 p2p_set_listen_channel(wpa_s->global->p2p, 81, cand, 0);
7831 void wpas_p2p_indicate_state_change(struct wpa_supplicant *wpa_s)
7833 struct wpa_used_freq_data *freqs;
7834 unsigned int num = wpa_s->num_multichan_concurrent;
7836 if (wpa_s->global->p2p_disabled || wpa_s->global->p2p == NULL)
7840 * If possible, optimize the Listen channel to be a channel that is
7841 * already used by one of the other interfaces.
7843 if (!wpa_s->conf->p2p_optimize_listen_chan)
7846 if (!wpa_s->current_ssid || wpa_s->wpa_state != WPA_COMPLETED)
7849 freqs = os_calloc(num, sizeof(struct wpa_used_freq_data));
7853 num = get_shared_radio_freqs_data(wpa_s, freqs, num);
7855 wpas_p2p_optimize_listen_channel(wpa_s, freqs, num);
7860 void wpas_p2p_deinit_iface(struct wpa_supplicant *wpa_s)
7862 if (wpa_s == wpa_s->parent)
7863 wpas_p2p_group_remove(wpa_s, "*");
7864 if (wpa_s == wpa_s->global->p2p_init_wpa_s && wpa_s->global->p2p) {
7865 wpa_dbg(wpa_s, MSG_DEBUG, "P2P: Disable P2P since removing "
7866 "the management interface is being removed");
7867 wpas_p2p_deinit_global(wpa_s->global);
7872 void wpas_p2p_ap_deinit(struct wpa_supplicant *wpa_s)
7874 if (wpa_s->ap_iface->bss)
7875 wpa_s->ap_iface->bss[0]->p2p_group = NULL;
7876 wpas_p2p_group_deinit(wpa_s);